complex
complex System Class
Class Precedence List:
complex, number, t
Description:
The type complex includes all mathematical complex numbers other than those included in the type rational. Complexes are expressed in Cartesian form with a real part and an imaginary part, each of which is a real. The real part and imaginary part are either both rational or both of the same float type. The imaginary part can be a float zero, but can never be a rational zero, for such a number is always represented by Common Lisp as a rational rather than a complex .
Compound Type Specifier Kind:
Specializing.
Compound Type Specifier Syntax:
(complex [typespec | *])
Compound Type Specifier Arguments:
typespec—a type specifier that denotes a subtype of type real.
Compound Type Specifier Description:
Every element of this type is a complex whose real part and imaginary part are each of type (upgraded-complex-part-type typespec). This type encompasses those complexes that can result by giving numbers of type typespec to complex.
(complex type-specifier) refers to all complexes that can result from giving numbers of type type specifier to the function complex, plus all other complexes of the same specialized representation.
See Also:
Section 12.1.5.3 (Rule of Canonical Representation for Complex Rationals), Section 2.3.2 (Constructing Numbers from Tokens), Section 22.1.3.1.4 (Printing Complexes)
Notes:
The input syntax for a complex with real part r and imaginary part i is #C(r i). For further details, see Section 2.4 (Standard Macro Characters).
For every float, n, there is a complex which represents the same mathematical number and which can be obtained by (COERCE n ’COMPLEX).
Expanded Reference: complex
Creating Complex Numbers
Complex numbers are created using the complex function or the #C reader macro. A complex number has a real part and an imaginary part.
(complex 3 4)
=> #C(3 4)
#C(3 4)
=> #C(3 4)
(complex 1.5 2.3)
=> #C(1.5 2.3)
(complex 5 0)
=> 5
Type Checking
The complex type specifier can optionally restrict the type of the parts.
(typep #C(3 4) 'complex)
=> T
(typep #C(3 4) 'number)
=> T
(typep #C(3 4) '(complex integer))
=> T
(typep #C(1.5 2.3) '(complex float))
=> T
(typep #C(1.5 2.3) '(complex integer))
=> NIL
(typep 5 'complex)
=> NIL
Accessing Parts
Use realpart and imagpart to extract the components of a complex number.
(realpart #C(3 4))
=> 3
(imagpart #C(3 4))
=> 4
(realpart #C(1.5 2.3))
=> 1.5
(imagpart #C(1.5 2.3))
=> 2.3
Arithmetic with Complex Numbers
Standard arithmetic operations work with complex numbers, following the rules of complex arithmetic.
(+ #C(1 2) #C(3 4))
=> #C(4 6)
(* #C(1 2) #C(3 4))
=> #C(-5 10)
(abs #C(3 4))
=> 5.0
(conjugate #C(3 4))
=> #C(3 -4)
(phase #C(0 1))
=> 1.5707964
Canonicalization
If a complex number is created with a zero imaginary part and both parts are rational, the result is canonicalized to a rational number, not a complex.
(complex 5 0)
=> 5
(typep (complex 5 0) 'complex)
=> NIL
(complex 5.0 0.0)
=> #C(5.0 0.0)
(typep (complex 5.0 0.0) 'complex)
=> T