Usage

Arguments

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.

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 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_
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) = √(x^2 + y^2),
and φ = Arg(z), x = r*cos(φ) and
y = r*sin(φ). They are all
internal generic primitive functions: methods can be
defined for them
individually or via the Complex
group generic.

Matrix multiplications (%*%, crossprod,
tcrossprod) are also defined for complex matrices
(matrix), and so are solve,
eigen or svd.

Internally, complex numbers are stored as a pair of double
precision numbers, either or both of which can be NaN
(including NA, see NA_complex_ and above) or
plus or minus infinity.

S4 methods

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

Re, Im, Mod, Arg and Conj
constitute the S4 group generic
Complex and so S4 methods can be
set for them individually or via the group generic.

Note

Operations and functions involving complex NaN mostly
rely on the C library's handling of double complex arithmetic,
which typically returns complex(re=NaN, im=NaN) (but we have
not seen a guarantee for that).
For + and -, R's own handling works strictly
“coordinate wise”.