The R Project SVN R

Rev

Rev 59039 | Rev 69410 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/complex.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2010 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{complex}
7
\title{Complex Vectors}
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{
17
  Basic functions which support complex arithmetic in R.
18
}
2 r 19
\usage{
1188 maechler 20
complex(length.out = 0, real = numeric(), imaginary = numeric(),
21
        modulus = 1, argument = 0)
10525 ripley 22
as.complex(x, \dots)
23
is.complex(x)
2 r 24
 
40182 ripley 25
Re(z)
26
Im(z)
27
Mod(z)
28
Arg(z)
29
Conj(z)
2 r 30
}
10525 ripley 31
\arguments{
15168 pd 32
  \item{length.out}{numeric.  Desired length of the output vector,
33
    inputs being recycled as needed.}
10525 ripley 34
  \item{real}{numeric vector.}
35
  \item{imaginary}{numeric vector.}
36
  \item{modulus}{numeric vector.}
15168 pd 37
  \item{argument}{numeric vector.}
15518 ripley 38
  \item{x}{an object, probably of mode \code{complex}.}
40182 ripley 39
  \item{z}{an object of mode \code{complex}, or one of a class for which
40
    a methods has been defined.}
15518 ripley 41
  \item{\dots}{further arguments passed to or from other methods.}
10525 ripley 42
}
43
\details{
48 hornik 44
  Complex vectors can be created with \code{complex}.  The vector can be
2248 maechler 45
  specified either by giving its length, its real and imaginary parts, or
36334 ripley 46
  modulus and argument.  (Giving just the length generates a vector of
10525 ripley 47
  complex zeroes.)
36361 maechler 48
 
24601 ripley 49
  \code{as.complex} attempts to coerce its argument to be of complex
39870 ripley 50
  type: like \code{\link{as.vector}} it strips attributes including
55555 ripley 51
  names.  All forms of \code{NA} and \code{NaN} are coerced to a complex
39870 ripley 52
  \code{NA}, for which both the real and imaginary parts are \code{NA}.
10525 ripley 53
 
15168 pd 54
  Note that \code{is.complex} and \code{is.numeric} are never both
55
  \code{TRUE}.
2 r 56
 
48 hornik 57
  The functions \code{Re}, \code{Im}, \code{Mod}, \code{Arg} and
58
  \code{Conj} have their usual interpretation as returning the real
59
  part, imaginary part, modulus, argument and complex conjugate for
51318 ripley 60
  complex values.  The modulus and argument are also called the \emph{polar
47643 ripley 61
  coordinates}.  If \eqn{z = x + i y} with real \eqn{x} and \eqn{y}, for
50807 ripley 62
  \eqn{r = Mod(z) = \sqrt{x^2 + y^2}}{r = Mod(z) = \sqrt(x^2 + y^2)},
63
  and \eqn{\phi = Arg(z)}, \eqn{x = r*\cos(\phi)}{x = r*cos(\phi)} and
51318 ripley 64
  \eqn{y = r*\sin(\phi)}{y = r*sin(\phi)}.  They are all
65
  \link{internal generic} \link{primitive} functions: methods can be
66
  defined for them
51317 ripley 67
  individually or \emph{via} the \code{\link[=S3groupGeneric]{Complex}}
68
  group generic.
2248 maechler 69
 
51318 ripley 70
  In addition, the elementary trigonometric, logarithmic, exponential,
54462 ripley 71
  square root and hyperbolic functions are implemented for complex
72
  values.
73
 
74
  Internally, complex numbers are stored as a pair of \link{double}
75
  precision numbers, either or both of which can be \code{\link{NaN}} or
76
  plus or minus infinity.
2 r 77
}
41393 ripley 78
\section{S4 methods}{
79
  \code{as.complex} is primitive and can have S4 methods set.
80
 
81
  \code{Re}, \code{Im}, \code{Mod}, \code{Arg} and \code{Conj}
82
  constitute the S4 group generic
49567 ripley 83
  \code{\link[=S4groupGeneric]{Complex}} and so S4 methods can be
41393 ripley 84
  set for them individually or via the group generic.
85
}
24300 ripley 86
\references{
87
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
88
  \emph{The New S Language}.
47262 ripley 89
  Wadsworth & Brooks/Cole.
24300 ripley 90
}
2 r 91
\examples{
41508 ripley 92
require(graphics)
93
 
23013 ripley 94
0i ^ (-3:3)
95
 
61150 ripley 96
matrix(1i^ (-6:5), nrow = 4) #- all columns are the same
2248 maechler 97
 
98
 
48 hornik 99
## create a complex normal vector
41593 ripley 100
z <- complex(real = stats::rnorm(100), imaginary = stats::rnorm(100))
48 hornik 101
## or also (less efficiently):
2 r 102
z2 <- 1:2 + 1i*(8:9)
1188 maechler 103
 
2248 maechler 104
## The Arg(.) is an angle:
61150 ripley 105
zz <- (rep(1:4, len = 9) + 1i*(9:1))/10
106
zz.shift <- complex(modulus = Mod(zz), argument = Arg(zz) + pi)
107
plot(zz, xlim = c(-1,1), ylim = c(-1,1), col = "red", asp = 1,
1188 maechler 108
     main = expression(paste("Rotation by "," ", pi == 180^o)))
61150 ripley 109
abline(h = 0, v = 0, col = "blue", lty = 3)
110
points(zz.shift, col = "orange")
2 r 111
}
286 maechler 112
\keyword{complex}