| 42333 |
ripley |
1 |
% File src/library/base/man/complex.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 84322 |
maechler |
3 |
% Copyright 1995-2023 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{complex}
|
| 69410 |
maechler |
7 |
\title{Complex Numbers and Basic Functionality}
|
| 56186 |
murdoch |
8 |
\alias{complex}
|
| 15168 |
pd |
9 |
\alias{as.complex}
|
|
|
10 |
\alias{is.complex}
|
|
|
11 |
\alias{Re}
|
|
|
12 |
\alias{Im}
|
|
|
13 |
\alias{Mod}
|
|
|
14 |
\alias{Arg}
|
|
|
15 |
\alias{Conj}
|
|
|
16 |
\description{
|
| 69410 |
maechler |
17 |
Basic functions which support complex arithmetic in \R, in addition to
|
|
|
18 |
the arithmetic operators \code{+}, \code{-}, \code{*}, \code{/}, and \code{^}.
|
| 15168 |
pd |
19 |
}
|
| 2 |
r |
20 |
\usage{
|
| 1188 |
maechler |
21 |
complex(length.out = 0, real = numeric(), imaginary = numeric(),
|
|
|
22 |
modulus = 1, argument = 0)
|
| 10525 |
ripley |
23 |
as.complex(x, \dots)
|
|
|
24 |
is.complex(x)
|
| 2 |
r |
25 |
|
| 40182 |
ripley |
26 |
Re(z)
|
|
|
27 |
Im(z)
|
|
|
28 |
Mod(z)
|
|
|
29 |
Arg(z)
|
|
|
30 |
Conj(z)
|
| 2 |
r |
31 |
}
|
| 10525 |
ripley |
32 |
\arguments{
|
| 15168 |
pd |
33 |
\item{length.out}{numeric. Desired length of the output vector,
|
|
|
34 |
inputs being recycled as needed.}
|
| 10525 |
ripley |
35 |
\item{real}{numeric vector.}
|
|
|
36 |
\item{imaginary}{numeric vector.}
|
|
|
37 |
\item{modulus}{numeric vector.}
|
| 15168 |
pd |
38 |
\item{argument}{numeric vector.}
|
| 15518 |
ripley |
39 |
\item{x}{an object, probably of mode \code{complex}.}
|
| 40182 |
ripley |
40 |
\item{z}{an object of mode \code{complex}, or one of a class for which
|
|
|
41 |
a methods has been defined.}
|
| 15518 |
ripley |
42 |
\item{\dots}{further arguments passed to or from other methods.}
|
| 10525 |
ripley |
43 |
}
|
|
|
44 |
\details{
|
| 48 |
hornik |
45 |
Complex vectors can be created with \code{complex}. The vector can be
|
| 2248 |
maechler |
46 |
specified either by giving its length, its real and imaginary parts, or
|
| 36334 |
ripley |
47 |
modulus and argument. (Giving just the length generates a vector of
|
| 10525 |
ripley |
48 |
complex zeroes.)
|
| 36361 |
maechler |
49 |
|
| 24601 |
ripley |
50 |
\code{as.complex} attempts to coerce its argument to be of complex
|
| 39870 |
ripley |
51 |
type: like \code{\link{as.vector}} it strips attributes including
|
| 85475 |
maechler |
52 |
names.
|
|
|
53 |
Since \R version 4.4.0, \code{as.complex(x)} for \dQuote{number-like}
|
|
|
54 |
\code{x}, i.e., types \code{"logical"}, \code{"integer"}, and
|
|
|
55 |
\code{"double"}, will always keep imaginary part zero, now also for
|
| 88896 |
smeyer |
56 |
\code{NA}s.
|
| 85475 |
maechler |
57 |
Up to \R versions 3.2.x, all forms of \code{NA} and \code{NaN}
|
| 69410 |
maechler |
58 |
were coerced to a complex \code{NA}, i.e., the \code{\link{NA_complex_}}
|
|
|
59 |
constant, for which both the real and imaginary parts are \code{NA}.
|
|
|
60 |
Since \R 3.3.0, typically only objects which are \code{NA} in parts
|
|
|
61 |
are coerced to complex \code{NA}, but others with \code{NaN} parts,
|
|
|
62 |
are \emph{not}. As a consequence, complex arithmetic where only
|
| 88896 |
smeyer |
63 |
\code{NaN}s (but no \code{NA}s) are involved typically will
|
| 69410 |
maechler |
64 |
\emph{not} give complex \code{NA} but complex numbers with real or
|
|
|
65 |
imaginary parts of \code{NaN}.
|
| 85475 |
maechler |
66 |
All of these many different complex numbers fulfill \code{is.na(.)} but
|
|
|
67 |
only one of them is identical to \code{NA_complex_}.
|
| 10525 |
ripley |
68 |
|
| 15168 |
pd |
69 |
Note that \code{is.complex} and \code{is.numeric} are never both
|
|
|
70 |
\code{TRUE}.
|
| 2 |
r |
71 |
|
| 48 |
hornik |
72 |
The functions \code{Re}, \code{Im}, \code{Mod}, \code{Arg} and
|
|
|
73 |
\code{Conj} have their usual interpretation as returning the real
|
|
|
74 |
part, imaginary part, modulus, argument and complex conjugate for
|
| 51318 |
ripley |
75 |
complex values. The modulus and argument are also called the \emph{polar
|
| 47643 |
ripley |
76 |
coordinates}. If \eqn{z = x + i y} with real \eqn{x} and \eqn{y}, for
|
| 50807 |
ripley |
77 |
\eqn{r = Mod(z) = \sqrt{x^2 + y^2}}{r = Mod(z) = \sqrt(x^2 + y^2)},
|
| 82751 |
smeyer |
78 |
and \eqn{\phi = Arg(z)}, \eqn{x = r \cos(\phi)}{x = r*cos(\phi)} and
|
|
|
79 |
\eqn{y = r \sin(\phi)}{y = r*sin(\phi)}. They are all
|
| 51318 |
ripley |
80 |
\link{internal generic} \link{primitive} functions: methods can be
|
|
|
81 |
defined for them
|
| 51317 |
ripley |
82 |
individually or \emph{via} the \code{\link[=S3groupGeneric]{Complex}}
|
|
|
83 |
group generic.
|
| 2248 |
maechler |
84 |
|
| 69410 |
maechler |
85 |
In addition to the arithmetic operators (see \link{Arithmetic})
|
|
|
86 |
\code{+}, \code{-}, \code{*}, \code{/}, and \code{^}, the elementary
|
|
|
87 |
trigonometric, logarithmic, exponential, square root and hyperbolic
|
|
|
88 |
functions are implemented for complex values.
|
| 54462 |
ripley |
89 |
|
| 69410 |
maechler |
90 |
Matrix multiplications (\code{\link{\%*\%}}, \code{\link{crossprod}},
|
|
|
91 |
\code{\link{tcrossprod}}) are also defined for complex matrices
|
|
|
92 |
(\code{\link{matrix}}), and so are \code{\link{solve}},
|
|
|
93 |
\code{\link{eigen}} or \code{\link{svd}}.
|
|
|
94 |
|
| 54462 |
ripley |
95 |
Internally, complex numbers are stored as a pair of \link{double}
|
| 69410 |
maechler |
96 |
precision numbers, either or both of which can be \code{\link{NaN}}
|
|
|
97 |
(including \code{NA}, see \code{\link{NA_complex_}} and above) or
|
| 54462 |
ripley |
98 |
plus or minus infinity.
|
| 2 |
r |
99 |
}
|
| 69410 |
maechler |
100 |
\note{
|
|
|
101 |
Operations and functions involving complex \code{\link{NaN}} mostly
|
|
|
102 |
rely on the C library's handling of \samp{double complex} arithmetic,
|
|
|
103 |
which typically returns \code{complex(re=NaN, im=NaN)} (but we have
|
|
|
104 |
not seen a guarantee for that).
|
|
|
105 |
For \code{+} and \code{-}, \R's own handling works strictly
|
|
|
106 |
\dQuote{coordinate wise}.
|
|
|
107 |
|
|
|
108 |
Operations involving complex \code{NA}, i.e., \code{\link{NA_complex_}}, return
|
|
|
109 |
\code{\link{NA_complex_}}.
|
| 85505 |
maechler |
110 |
|
|
|
111 |
Only since \R version 4.4.0, \code{as.complex("1i")} gives \code{1i},
|
|
|
112 |
it returned \code{NA_complex_} with a warning, previously.
|
| 69410 |
maechler |
113 |
}
|
|
|
114 |
\seealso{
|
|
|
115 |
\code{\link{Arithmetic}}; \code{\link{polyroot}} finds all \eqn{n}
|
|
|
116 |
complex roots of a polynomial of degree \eqn{n}.
|
|
|
117 |
}
|
| 41393 |
ripley |
118 |
\section{S4 methods}{
|
|
|
119 |
\code{as.complex} is primitive and can have S4 methods set.
|
|
|
120 |
|
|
|
121 |
\code{Re}, \code{Im}, \code{Mod}, \code{Arg} and \code{Conj}
|
|
|
122 |
constitute the S4 group generic
|
| 49567 |
ripley |
123 |
\code{\link[=S4groupGeneric]{Complex}} and so S4 methods can be
|
| 41393 |
ripley |
124 |
set for them individually or via the group generic.
|
|
|
125 |
}
|
| 24300 |
ripley |
126 |
\references{
|
| 88581 |
hornik |
127 |
\bibshow{R:Becker+Chambers+Wilks:1988}
|
| 24300 |
ripley |
128 |
}
|
| 2 |
r |
129 |
\examples{
|
| 41508 |
ripley |
130 |
require(graphics)
|
|
|
131 |
|
| 23013 |
ripley |
132 |
0i ^ (-3:3)
|
|
|
133 |
|
| 61150 |
ripley |
134 |
matrix(1i^ (-6:5), nrow = 4) #- all columns are the same
|
| 2248 |
maechler |
135 |
|
|
|
136 |
|
| 48 |
hornik |
137 |
## create a complex normal vector
|
| 41593 |
ripley |
138 |
z <- complex(real = stats::rnorm(100), imaginary = stats::rnorm(100))
|
| 48 |
hornik |
139 |
## or also (less efficiently):
|
| 2 |
r |
140 |
z2 <- 1:2 + 1i*(8:9)
|
| 1188 |
maechler |
141 |
|
| 2248 |
maechler |
142 |
## The Arg(.) is an angle:
|
| 80079 |
hornik |
143 |
zz <- (rep(1:4, length.out = 9) + 1i*(9:1))/10
|
| 61150 |
ripley |
144 |
zz.shift <- complex(modulus = Mod(zz), argument = Arg(zz) + pi)
|
|
|
145 |
plot(zz, xlim = c(-1,1), ylim = c(-1,1), col = "red", asp = 1,
|
| 1188 |
maechler |
146 |
main = expression(paste("Rotation by "," ", pi == 180^o)))
|
| 61150 |
ripley |
147 |
abline(h = 0, v = 0, col = "blue", lty = 3)
|
|
|
148 |
points(zz.shift, col = "orange")
|
| 69423 |
maechler |
149 |
|
| 85233 |
maechler |
150 |
## as.complex(<some NA>): numbers keep Im = 0:
|
|
|
151 |
NAs <- vapply(list(NA, NA_integer_, NA_real_, NA_character_, NA_complex_),
|
| 84322 |
maechler |
152 |
as.complex, 0+0i)
|
| 85233 |
maechler |
153 |
stopifnot(is.na(NAs), is.na(Re(NAs))) # has always been true
|
| 85475 |
maechler |
154 |
showC <- function(z) noquote(paste0("(", Re(z), ",", Im(z), ")"))
|
|
|
155 |
showC(NAs)
|
| 87286 |
smeyer |
156 |
Im(NAs) # [0 0 0 NA NA] \\\\ in R <= 4.3.x was [NA NA 0 NA NA]
|
| 85233 |
maechler |
157 |
stopifnot(Im(NAs)[1:3] == 0)
|
| 88377 |
smeyer |
158 |
stopifnot(Im(NA_real_ + 0i) == 0) # has always been true
|
| 84322 |
maechler |
159 |
|
| 69423 |
maechler |
160 |
|
|
|
161 |
## The exact result of this *depends* on the platform, compiler, math-library:
|
|
|
162 |
(NpNA <- NaN + NA_complex_) ; str(NpNA) # *behaves* as 'cplx NA' ..
|
|
|
163 |
stopifnot(is.na(NpNA), is.na(NA_complex_), is.na(Re(NA_complex_)), is.na(Im(NA_complex_)))
|
| 85233 |
maechler |
164 |
showC(NpNA)# but does not always show '(NaN,NA)'
|
| 69423 |
maechler |
165 |
## and this is not TRUE everywhere:
|
|
|
166 |
identical(NpNA, NA_complex_)
|
| 85233 |
maechler |
167 |
showC(NA_complex_) # always == (NA,NA)
|
| 2 |
r |
168 |
}
|
| 286 |
maechler |
169 |
\keyword{complex}
|