Complex
public struct Complex<T : Real> : Codable
where T : Codable & _ExpressibleByBuiltinFloatLiteral
A type to represent a complex value in Cartesian form.
Note
Complex64
is a type alias for Complex<Float>
and Complex128
is
a type alias for Complex<Double>
.
Create new instances of Complex<T>
using integer or floating-point
literals and the imaginary unit Complex<T>.i
. For example:
let x = 2 + 4 * .i // `x` is of type `Complex<Double>`
let y = 3.5 + 7 * .i // `y` is of type `Complex<Double>`
let z: Complex64 = .e + .pi * .i // `z` is of type `Complex<Float>`
Additional Considerations
Floating-point types have special values that represent infinity or NaN
(not a number
). Complex functions in different languages may return
different results when working with special values.
Many complex functions have branch cuts, which are curves in the complex plane across which a function is discontinuous. Different languages may adopt different branch cut structures for the same complex function.
Implementations in Complex<T>
adhere to the C standard (Annex G) as
closely as possible with respect to special values and branch cuts.
To users unfamiliar with complex functions, the principal value returned by
some complex functions may be unexpected. For example,
Double.cbrt(-8) == -2
, which is the real root, while
Complex.cbrt(-8) == 2 * Complex.exp(.i * .pi / 3)
, which is the
principal root.
Topics
- pi
- e
- phi
- /(_:_:)
- /=(_:_:)
- **(_:_:)
- **=(_:_:)
- naturalExponential()
- naturalLogarithm()
- commonLogarithm()
- squareRoot()
- cubeRoot()
- sine()
- cosine()
- tangent()
- inverseSine()
- inverseCosine()
- inverseTangent()
- hyperbolicSine()
- hyperbolicCosine()
- hyperbolicTangent()
- inverseHyperbolicSine()
- inverseHyperbolicCosine()
- inverseHyperbolicTangent()
-
The real component of the complex value.
Declaration
Swift
public var real: T
-
The imaginary component of the complex value.
Declaration
Swift
public var imaginary: T
-
Creates a new value from the given real and imaginary components.
Declaration
Swift
public init(real: T = 0, imaginary: T = 0)
Parameters
real
-
The new value’s real component.
imaginary
-
The new value’s imaginary component.
-
Creates a new value from the given polar coordinates
(r, theta)
.Declaration
Swift
public init(r: T, theta: T)
Parameters
r
-
The new value’s radial coordinate.
theta
-
The new value’s angular coordinate.
-
Creates a new value from the given real component, rounded to the closest possible representation.
Note
This initializer creates only instances of
Complex<T> where T : BinaryFloatingPoint
.Declaration
Swift
public init(_ real: Float)
Parameters
real
-
The value to convert to a real component of type
T
.
-
Creates a new value from the given real component, rounded to the closest possible representation.
Note
This initializer creates only instances of
Complex<T> where T : BinaryFloatingPoint
.Declaration
Swift
public init(_ real: Double)
Parameters
real
-
The value to convert to a real component of type
T
.
-
The principal argument (phase angle) of this value.
Special cases are handled as if calling
T.atan2(imaginary, real)
. The result lies in the range [-π, π].Declaration
Swift
public var argument: T
-
A Boolean value indicating whether the instance’s representation is in canonical form.
A complex value is represented in canonical form if the its real and imaginary components are both represented in canonical form, as defined in the IEEE 754 specification. Every
Float
orDouble
value is canonical.Declaration
Swift
public struct Complex<T : Real> : Codable where T : Codable & _ExpressibleByBuiltinFloatLiteral
-
A Boolean value indicating whether the instance is finite.
A complex value is finite if its real and imaginary components are both finite. A component is finite if it is not infinity or NaN.
Declaration
Swift
public var isFinite: Bool
-
A Boolean value indicating whether the instance is infinite.
A complex value is infinite if at least one of its components (real or imaginary) is infinite, even if the other component is NaN.
Note that
isFinite
andisInfinite
do not form a dichotomy because NaN is neither finite nor infinite.Declaration
Swift
public var isInfinite: Bool
-
A Boolean value indicating whether the instance is NaN (
not a number
).A complex value is NaN if at least one of its components (real or imaginary) is NaN and the other component is not infinite.
Because NaN is not equal to any value, including NaN, use this property instead of the equal-to operator (
==
) or not-equal-to operator (!=
) to test whether a value is or is not NaN.This property is
true
for both quiet and signaling NaNs.Declaration
Swift
public var isNaN: Bool
-
A Boolean value indicating whether the instance is a signaling NaN.
A complex value is a signaling NaN if at least one of its components (real or imaginary) is a signaling NaN and the other component is not infinite.
Signaling NaNs typically raise the Invalid flag when used in general computing operations.
Declaration
Swift
public var isSignalingNaN: Bool
-
A Boolean value indicating whether the instance is equal to zero.
A complex value is equal to zero if its real and imaginary components both represent either
-0.0
or+0.0
.Declaration
Swift
public var isZero: Bool
-
The magnitude (modulus, absolute value) of this value.
Special cases are handled as if calling
T.hypot(real, imaginary)
.Declaration
Swift
public var magnitude: T
-
The squared magnitude (field norm, absolute square) of this value.
This is less costly to compute than
magnitude
and, in some cases, can be used in its place. For example, ifa.magnitude > b.magnitude
, thena.squaredMagnitude > b.squaredMagnitude
.Declaration
Swift
public var squaredMagnitude: T
-
Returns the complex conjugate of this value, obtained by reversing the sign of the imaginary component.
Declaration
Swift
public func conjugate() -> Complex
-
Returns the projection of this value onto the Riemann sphere.
For most values
z
,z.projection() == z
. The projection of any complex infinity is positive real infinity. The sign of the imaginary component is preserved in the sign of zero; that is,z.projection().imaginary.sign == z.imaginary.sign
.Declaration
Swift
public func projection() -> Complex
-
Returns the reciprocal (multiplicative inverse) of this value, or NaN (
not a number
) if this value is infinite, NaN, or zero.Declaration
Swift
public func reciprocal() -> Complex
-
Declaration
Swift
public init(integerLiteral value: Int)
-
Declaration
Swift
public init?<U>(exactly source: U) where U : BinaryInteger
-
Declaration
Swift
public static func + (lhs: Complex, rhs: Complex) -> Complex
-
Declaration
Swift
public static func += (lhs: inout Complex, rhs: Complex)
-
Declaration
Swift
public static func - (lhs: Complex, rhs: Complex) -> Complex
-
Declaration
Swift
public static func -= (lhs: inout Complex, rhs: Complex)
-
Declaration
Swift
public static func * (lhs: Complex, rhs: Complex) -> Complex
-
Declaration
Swift
public static func *= (lhs: inout Complex, rhs: Complex)
-
Declaration
Swift
public static prefix func - (operand: Complex) -> Complex
-
Declaration
Swift
public mutating func negate()
-
Declaration
Swift
public static var pi: Complex
-
Declaration
Swift
public static var e: Complex
-
Declaration
Swift
public static var phi: Complex
-
Declaration
Swift
public static func / (lhs: Complex, rhs: Complex) -> Complex
-
Declaration
Swift
public static func /= (lhs: inout Complex, rhs: Complex)
-
Declaration
Swift
public static func ** (lhs: Complex, rhs: Complex) -> Complex
-
Declaration
Swift
public static func **= (lhs: inout Complex, rhs: Complex)
-
Declaration
Swift
public func naturalExponential() -> Complex
-
Declaration
Swift
public func naturalLogarithm() -> Complex
-
Declaration
Swift
public func commonLogarithm() -> Complex
-
Declaration
Swift
public func squareRoot() -> Complex
-
Declaration
Swift
public func cubeRoot() -> Complex
-
Declaration
Swift
public func sine() -> Complex
-
Declaration
Swift
public func cosine() -> Complex
-
Declaration
Swift
public func tangent() -> Complex
-
Declaration
Swift
public func inverseSine() -> Complex
-
Declaration
Swift
public func inverseCosine() -> Complex
-
Declaration
Swift
public func inverseTangent() -> Complex
-
Declaration
Swift
public func hyperbolicSine() -> Complex
-
Declaration
Swift
public func hyperbolicCosine() -> Complex
-
Declaration
Swift
public func hyperbolicTangent() -> Complex
-
Declaration
Swift
public func inverseHyperbolicSine() -> Complex
-
Declaration
Swift
public func inverseHyperbolicCosine() -> Complex
-
Declaration
Swift
public func inverseHyperbolicTangent() -> Complex