mirror of
https://github.com/apple/swift-numerics
synced 2026-11-22 04:30:45 +00:00
Further review edits from Nate.
This commit is contained in:
11 files changed
+34
-40
No files matched your search
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019-2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -14,7 +14,7 @@ import RealModule
|
||||
extension Complex: AdditiveArithmetic {
|
||||
/// The additive identity, with real and imaginary parts both zero.
|
||||
///
|
||||
/// See also: `one`, `i`, `infinity`
|
||||
/// See also: ``one``, ``i``, ``infinity``
|
||||
@_transparent
|
||||
public static var zero: Complex {
|
||||
Complex(0, 0)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019-2024 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -13,6 +13,8 @@ import RealModule
|
||||
|
||||
extension Complex: AlgebraicField {
|
||||
/// The multiplicative identity `1 + 0i`.
|
||||
///
|
||||
/// See also: ``zero``, ``i``, ``infinity``
|
||||
@_transparent
|
||||
public static var one: Complex {
|
||||
Complex(1, 0)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019-2020 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -43,16 +43,14 @@ extension Complex: Numeric {
|
||||
/// Equal to `max(abs(real), abs(imaginary))`.
|
||||
///
|
||||
/// If you need to work with the Euclidean norm (a.k.a. 2-norm) instead,
|
||||
/// use the `length` or `lengthSquared` properties. If you just need to
|
||||
/// know "how big" a number is, use this property.
|
||||
/// use the ``length`` or ``lengthSquared`` properties. If you just need
|
||||
/// to know "how big" a number is, use this property.
|
||||
///
|
||||
/// **Edge cases:**
|
||||
///
|
||||
/// - If `z` is not finite, `z.magnitude` is infinity.
|
||||
/// - If `z` is zero, `z.magnitude` is zero.
|
||||
/// - Otherwise, `z.magnitude` is finite and non-zero.
|
||||
///
|
||||
/// See also `.length` and `.lengthSquared`.
|
||||
@_transparent
|
||||
public var magnitude: RealType {
|
||||
guard isFinite else { return .infinity }
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -11,11 +11,8 @@
|
||||
|
||||
import RealModule
|
||||
|
||||
/// A [complex number](https://en.wikipedia.org/wiki/Complex_number).
|
||||
///
|
||||
/// `Complex` is an `AlgebraicField`, so it has all the normal arithmetic
|
||||
/// operators. It conforms to `ElementaryFunctions`, so it has all the usual
|
||||
/// math functions.
|
||||
// A [complex number](https://en.wikipedia.org/wiki/Complex_number).
|
||||
// See Documentation.docc/Complex.md for more details.
|
||||
@frozen
|
||||
public struct Complex<RealType> where RealType: Real {
|
||||
// A note on the `x` and `y` properties
|
||||
@@ -92,7 +89,7 @@ extension Complex {
|
||||
extension Complex {
|
||||
/// The imaginary unit.
|
||||
///
|
||||
/// See also `.zero`, `.one` and `.infinity`.
|
||||
/// See also ``zero``, ``one`` and ``infinity``.
|
||||
@_transparent
|
||||
public static var i: Complex {
|
||||
Complex(0, 1)
|
||||
@@ -100,7 +97,7 @@ extension Complex {
|
||||
|
||||
/// The point at infinity.
|
||||
///
|
||||
/// See also `.zero`, `.one` and `.i`.
|
||||
/// See also ``zero``, ``one`` and ``i``.
|
||||
@_transparent
|
||||
public static var infinity: Complex {
|
||||
Complex(.infinity, 0)
|
||||
@@ -110,7 +107,7 @@ extension Complex {
|
||||
///
|
||||
/// A complex value is finite if neither component is an infinity or nan.
|
||||
///
|
||||
/// See also `.isNormal`, `.isSubnormal` and `.isZero`.
|
||||
/// See also ``isNormal``, ``isSubnormal`` and ``isZero``.
|
||||
@_transparent
|
||||
public var isFinite: Bool {
|
||||
x.isFinite && y.isFinite
|
||||
@@ -123,7 +120,7 @@ extension Complex {
|
||||
/// one of the components is normal if its exponent allows a full-precision
|
||||
/// representation.
|
||||
///
|
||||
/// See also `.isFinite`, `.isSubnormal` and `.isZero`.
|
||||
/// See also ``isFinite``, ``isSubnormal`` and ``isZero``.
|
||||
@_transparent
|
||||
public var isNormal: Bool {
|
||||
isFinite && (x.isNormal || y.isNormal)
|
||||
@@ -135,7 +132,7 @@ extension Complex {
|
||||
/// When the result of a computation is subnormal, underflow has occurred and
|
||||
/// the result generally does not have full precision.
|
||||
///
|
||||
/// See also `.isFinite`, `.isNormal` and `.isZero`.
|
||||
/// See also ``isFinite``, ``isNormal`` and ``isZero``.
|
||||
@_transparent
|
||||
public var isSubnormal: Bool {
|
||||
isFinite && !isNormal && !isZero
|
||||
@@ -146,7 +143,7 @@ extension Complex {
|
||||
/// A complex number is zero if *both* the real and imaginary components
|
||||
/// are zero.
|
||||
///
|
||||
/// See also `.isFinite`, `.isNormal` and `isSubnormal`.
|
||||
/// See also ``isFinite``, ``isNormal`` and ``isSubnormal``.
|
||||
@_transparent
|
||||
public var isZero: Bool {
|
||||
x == 0 && y == 0
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 - 2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -26,15 +26,15 @@ extension Complex {
|
||||
/// because the length can be as much as sqrt(2) times larger than
|
||||
/// either component, and thus may not be representable in the real type.
|
||||
///
|
||||
/// For most use cases, you can use the cheaper `.magnitude`
|
||||
/// For most use cases, you can use the cheaper ``magnitude``
|
||||
/// property (which computes the ∞-norm) instead, which always produces
|
||||
/// a representable result. See <doc:Magnitude> for more details.
|
||||
///
|
||||
/// Edge cases:
|
||||
/// - If a complex value is not finite, its `.length` is `infinity`.
|
||||
/// - If a complex value is not finite, its `length` is `infinity`.
|
||||
///
|
||||
/// See also `.magnitude`, `.lengthSquared`, `.phase`, `.polar`
|
||||
/// and `init(r:θ:)`.
|
||||
/// See also ``lengthSquared``, ``phase``, ``polar``
|
||||
/// and ``init(length:phase:)``.
|
||||
@_transparent
|
||||
public var length: RealType {
|
||||
let naive = lengthSquared
|
||||
@@ -42,7 +42,7 @@ extension Complex {
|
||||
return .sqrt(naive)
|
||||
}
|
||||
|
||||
// Internal implementation detail of `length`, moving slow path off
|
||||
// Internal implementation detail of ``length``, moving slow path off
|
||||
// of the inline function. Note that even `carefulLength` can overflow
|
||||
// for finite inputs, but only when the result is outside the range
|
||||
// of representable values.
|
||||
@@ -54,21 +54,19 @@ extension Complex {
|
||||
|
||||
/// The squared length `(real*real + imaginary*imaginary)`.
|
||||
///
|
||||
/// This property is more efficient to compute than `length`, but is
|
||||
/// This property is more efficient to compute than ``length``, but is
|
||||
/// highly prone to overflow or underflow; for finite values that are
|
||||
/// not well-scaled, `lengthSquared` is often either zero or
|
||||
/// infinity, even when `length` is a finite number. Use this property
|
||||
/// only when you are certain that this value is well-scaled.
|
||||
///
|
||||
/// For many cases, `.magnitude` can be used instead, which is similarly
|
||||
/// For many cases, ``magnitude`` can be used instead, which is similarly
|
||||
/// cheap to compute and always returns a representable value.
|
||||
///
|
||||
/// Note that because of how `lengthSquared` is used, it is a primary
|
||||
/// design goal that it be as fast as possible. Therefore, it does not
|
||||
/// normalize infinities, and may return either `.infinity` or `.nan`
|
||||
/// for non-finite values.
|
||||
///
|
||||
/// See also ``length`` and ``magnitude``.
|
||||
@_transparent
|
||||
public var lengthSquared: RealType {
|
||||
x*x + y*y
|
||||
@@ -93,7 +91,7 @@ extension Complex {
|
||||
/// - If the complex value is zero or non-finite, phase is `.nan`.
|
||||
/// - If the complex value is non-finite, length is `.infinity`.
|
||||
///
|
||||
/// See also: `.length`, `.phase` and `init(r:θ:)`.
|
||||
/// See also: ``length``, ``phase`` and ``init(length:phase:)``.
|
||||
public var polar: (length: RealType, phase: RealType) {
|
||||
(length, phase)
|
||||
}
|
||||
@@ -116,7 +114,7 @@ extension Complex {
|
||||
/// ```
|
||||
/// - Otherwise, `θ` must be finite, or a precondition failure occurs.
|
||||
///
|
||||
/// See also `.length`, `.phase` and `.polar`.
|
||||
/// See also ``length``, ``phase`` and ``polar``.
|
||||
@inlinable
|
||||
public init(length: RealType, phase: RealType) {
|
||||
if phase.isFinite {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019-2021 Apple Inc. and the Swift Numerics project authors
|
||||
// Copyright (c) 2019-2025 Apple Inc. and the Swift Numerics project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
@@ -33,10 +33,9 @@ extension Complex {
|
||||
/// `self` divided by the real value `a`.
|
||||
///
|
||||
/// More efficient than `self / Complex(a)`. May not produce exactly the
|
||||
/// same result, but will always be more accurate when they differ.
|
||||
/// same result, but will always be more accurate if they differ.
|
||||
@inlinable @inline(__always)
|
||||
public func divided(by a: RealType) -> Complex {
|
||||
// See implementation notes for `multiplied` above.
|
||||
Complex(x/a, y/a)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user