[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  complex: Complex Numbers and Basic Functionality

##  Description

Basic functions which support complex arithmetic in  R  , in addition to the
arithmetic operators ` + ` , ` - ` , ` * ` , ` / ` , and ` ^ ` .

##  Usage

    
    
    complex(length.out = 0, real = numeric(), imaginary = numeric(),
            modulus = 1, argument = 0)
    as.complex(x, …)
    is.complex(x)
    
    Re(z)
    Im(z)
    Mod(z)
    Arg(z)
    Conj(z)

##  Arguments

length.out

numeric. Desired length of the output vector, inputs being recycled as needed.

real

numeric vector.

imaginary

numeric vector.

modulus

numeric vector.

argument

numeric vector.

x

an object, probably of mode ` complex ` .

z

an object of mode ` complex ` , or one of a class for which a methods has been
defined.

…

further arguments passed to or from other methods.

##  S4 methods

` as.complex ` is primitive and can have S4 methods set.

` Re ` , ` Im ` , ` Mod ` , ` Arg ` and ` Conj ` constitute the S4 group
generic ` [ Complex
](/link/Complex?package=base&version=3.6.2&to=%3DS4groupGeneric) ` and so S4
methods can be set for them individually or via the group generic.

##  Details

Complex vectors can be created with ` complex ` . The vector can be specified
either by giving its length, its real and imaginary parts, or modulus and
argument. (Giving just the length generates a vector of complex zeroes.)

` as.complex ` attempts to coerce its argument to be of complex type: like ` [
as.vector ](/link/as.vector?package=base&version=3.6.2) ` it strips attributes
including names. Up to  R  versions 3.2.x, all forms of ` NA ` and ` NaN `
were coerced to a complex ` NA ` , i.e., the ` [ NA_complex_
](/link/NA_complex_?package=base&version=3.6.2) ` constant, for which both the
real and imaginary parts are ` NA ` . Since  R  3.3.0, typically only objects
which are ` NA ` in parts are coerced to complex ` NA ` , but others with `
NaN ` parts, are _not_ . As a consequence, complex arithmetic where only ` NaN
` 's (but no ` NA ` 's) are involved typically will _not_ give complex ` NA `
but complex numbers with real or imaginary parts of ` NaN ` .

Note that ` is.complex ` and ` is.numeric ` are never both ` TRUE ` .

The functions ` Re ` , ` Im ` , ` Mod ` , ` Arg ` and ` Conj ` have their
usual interpretation as returning the real part, imaginary part, modulus,
argument and complex conjugate for complex values. The modulus and argument
are also called the _polar coordinates_ . If \\(z = x + i y\\) with real
\\(x\\) and \\(y\\), for \\(r = Mod(z) = \sqrt{x^2 + y^2}\\), and \\(\phi =
Arg(z)\\), \\(x = r*\cos(\phi)\\) and \\(y = r*\sin(\phi)\\). They are all [
internal generic ](/link/internal%20generic?package=base&version=3.6.2) [
primitive ](/link/primitive?package=base&version=3.6.2) functions: methods can
be defined for them individually or _via_ the ` [ Complex
](/link/Complex?package=base&version=3.6.2&to=%3DS3groupGeneric) ` group
generic.

In addition to the arithmetic operators (see [ Arithmetic
](/link/Arithmetic?package=base&version=3.6.2) ) ` + ` , ` - ` , ` * ` , ` / `
, and ` ^ ` , the elementary trigonometric, logarithmic, exponential, square
root and hyperbolic functions are implemented for complex values.

Matrix multiplications ( ` [ %*% ](/link/%25*%25?package=base&version=3.6.2) `
, ` [ crossprod ](/link/crossprod?package=base&version=3.6.2) ` , ` [
tcrossprod ](/link/tcrossprod?package=base&version=3.6.2) ` ) are also defined
for complex matrices ( ` [ matrix ](/link/matrix?package=base&version=3.6.2) `
), and so are ` [ solve ](/link/solve?package=base&version=3.6.2) ` , ` [
eigen ](/link/eigen?package=base&version=3.6.2) ` or ` [ svd
](/link/svd?package=base&version=3.6.2) ` .

Internally, complex numbers are stored as a pair o