NumericAnnex Reference (100% documented)

View on GitHub

Install in Dash

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

Stored Properties

  • real

    The real component of the complex value.

    Declaration

    Swift

    public var real: T
  • imaginary

    The imaginary component of the complex value.

    Declaration

    Swift

    public var imaginary: T

Initializers

  • init(real:imaginary:)

    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.

  • init(r:theta:)

    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.

Initializers (Constrained)

  • init(_:)

    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.

  • init(_:)

    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.

Static Properties

  • i

    The imaginary unit i.

    Declaration

    Swift

    public static var i: Complex

Computed Properties

  • argument

    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
  • isCanonical

    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 or Double value is canonical.

    Declaration

    Swift

    public struct Complex<T : Real> : Codable
    where T : Codable & _ExpressibleByBuiltinFloatLiteral
  • isFinite

    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
  • isInfinite

    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 and isInfinite do not form a dichotomy because NaN is neither finite nor infinite.

    Declaration

    Swift

    public var isInfinite: Bool
  • isNaN

    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
  • isSignalingNaN

    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
  • isZero

    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
  • magnitude

    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
  • squaredMagnitude

    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, if a.magnitude > b.magnitude, then a.squaredMagnitude > b.squaredMagnitude.

    Declaration

    Swift

    public var squaredMagnitude: T

Methods

  • conjugate()

    Returns the complex conjugate of this value, obtained by reversing the sign of the imaginary component.

    Declaration

    Swift

    public func conjugate() -> Complex
  • projection()

    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
  • reciprocal()

    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

ExpressibleByFloatLiteral

  • init(floatLiteral:)

    Declaration

    Swift

    public init(floatLiteral value: T)

ExpressibleByIntegerLiteral

  • init(integerLiteral:)

    Declaration

    Swift

    public init(integerLiteral value: Int)

CustomStringConvertible

  • description

    Declaration

    Swift

    public var description: String

Equatable

  • ==(_:_:)

    Declaration

    Swift

    public static func == (lhs: Complex, rhs: Complex) -> Bool

Hashable

  • hashValue

    Declaration

    Swift

    public var hashValue: Int

Numeric

  • init(exactly:)

    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)

SignedNumeric

  • -(_:)

    Declaration

    Swift

    public static prefix func - (operand: Complex) -> Complex
  • negate()

    Declaration

    Swift

    public mutating func negate()

Math

  • pi

    Declaration

    Swift

    public static var pi: Complex
  • e

    Declaration

    Swift

    public static var e: Complex
  • phi

    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)
  • naturalExponential()

    Declaration

    Swift

    public func naturalExponential() -> Complex
  • naturalLogarithm()

    Declaration

    Swift

    public func naturalLogarithm() -> Complex
  • commonLogarithm()

    Declaration

    Swift

    public func commonLogarithm() -> Complex
  • squareRoot()

    Declaration

    Swift

    public func squareRoot() -> Complex
  • cubeRoot()

    Declaration

    Swift

    public func cubeRoot() -> Complex
  • sine()

    Declaration

    Swift

    public func sine() -> Complex
  • cosine()

    Declaration

    Swift

    public func cosine() -> Complex
  • tangent()

    Declaration

    Swift

    public func tangent() -> Complex
  • inverseSine()

    Declaration

    Swift

    public func inverseSine() -> Complex
  • inverseCosine()

    Declaration

    Swift

    public func inverseCosine() -> Complex
  • inverseTangent()

    Declaration

    Swift

    public func inverseTangent() -> Complex
  • hyperbolicSine()

    Declaration

    Swift

    public func hyperbolicSine() -> Complex
  • hyperbolicCosine()

    Declaration

    Swift

    public func hyperbolicCosine() -> Complex
  • hyperbolicTangent()

    Declaration

    Swift

    public func hyperbolicTangent() -> Complex
  • inverseHyperbolicSine()

    Declaration

    Swift

    public func inverseHyperbolicSine() -> Complex
  • inverseHyperbolicCosine()

    Declaration

    Swift

    public func inverseHyperbolicCosine() -> Complex
  • inverseHyperbolicTangent()

    Declaration

    Swift

    public func inverseHyperbolicTangent() -> Complex