forked from apple/swift-numerics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Complex module for finer granularity.
- Loading branch information
1 parent
1939f34
commit ba65b0f
Showing
14 changed files
with
495 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- Complex+AdditiveArithmetic.swift ---------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019-2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import RealModule | ||
|
||
extension Complex: AdditiveArithmetic { | ||
/// The additive identity, with real and imaginary parts both zero. | ||
/// | ||
/// See also: `one`, `i`, `infinity` | ||
@_transparent | ||
public static var zero: Complex { | ||
Complex(0, 0) | ||
} | ||
|
||
@_transparent | ||
public static func +(z: Complex, w: Complex) -> Complex { | ||
return Complex(z.x + w.x, z.y + w.y) | ||
} | ||
|
||
@_transparent | ||
public static func -(z: Complex, w: Complex) -> Complex { | ||
return Complex(z.x - w.x, z.y - w.y) | ||
} | ||
|
||
@_transparent | ||
public static func +=(z: inout Complex, w: Complex) { | ||
z = z + w | ||
} | ||
|
||
@_transparent | ||
public static func -=(z: inout Complex, w: Complex) { | ||
z = z - w | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===--- Complex+Codable.swift --------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019 - 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import RealModule | ||
|
||
// FloatingPoint does not refine Codable, so this is a conditional conformance. | ||
extension Complex: Decodable where RealType: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
var unkeyedContainer = try decoder.unkeyedContainer() | ||
let x = try unkeyedContainer.decode(RealType.self) | ||
let y = try unkeyedContainer.decode(RealType.self) | ||
self.init(x, y) | ||
} | ||
} | ||
|
||
extension Complex: Encodable where RealType: Encodable { | ||
public func encode(to encoder: Encoder) throws { | ||
var unkeyedContainer = encoder.unkeyedContainer() | ||
try unkeyedContainer.encode(x) | ||
try unkeyedContainer.encode(y) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===--- Complex+ElFnsF16.swift -------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2019-2021 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/* | ||
import RealModule | ||
|
||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) | ||
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) | ||
extension Complex { | ||
@_specialize(exported: true, target: exp(_:), where RealType == Float16) | ||
public static func _expF16(_ z: Self) -> Self { | ||
_expImpl(z) | ||
} | ||
} | ||
#endif | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===--- Complex+ElFnsF80.swift -------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2019-2021 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/* | ||
import RealModule | ||
|
||
#if (arch(i386) || arch(x86_64)) && !(os(Windows) || os(Android)) | ||
extension Complex { | ||
@_specialize(exported: true, target: exp(_:), where RealType == Float80) | ||
public static func _expF80(_ z: Self) -> Self { | ||
_expImpl(z) | ||
} | ||
} | ||
#endif | ||
*/ |
2 changes: 1 addition & 1 deletion
2
...s/ComplexModule/ElementaryFunctions.swift → ...xModule/Complex+ElementaryFunctions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===--- Complex+Hashable.swift -------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019 - 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import RealModule | ||
|
||
extension Complex: Hashable { | ||
@_transparent | ||
public static func ==(a: Complex, b: Complex) -> Bool { | ||
// Identify all numbers with either component non-finite as a single | ||
// "point at infinity". | ||
guard a.isFinite || b.isFinite else { return true } | ||
// For finite numbers, equality is defined componentwise. Cases where | ||
// only one of a or b is infinite fall through to here as well, but this | ||
// expression correctly returns false for them so we don't need to handle | ||
// them explicitly. | ||
return a.x == b.x && a.y == b.y | ||
} | ||
|
||
@_transparent | ||
public func hash(into hasher: inout Hasher) { | ||
// There are two equivalence classes to which we owe special attention: | ||
// All zeros should hash to the same value, regardless of sign, and all | ||
// non-finite numbers should hash to the same value, regardless of | ||
// representation. The correct behavior for zero falls out for free from | ||
// the hash behavior of floating-point, but we need to use a | ||
// representative member for any non-finite values. | ||
if isFinite { | ||
hasher.combine(x) | ||
hasher.combine(y) | ||
} else { | ||
hasher.combine(RealType.infinity) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//===--- Complex+IntegerLiteral.swift -------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019 - 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Complex: ExpressibleByIntegerLiteral { | ||
public typealias IntegerLiteralType = RealType.IntegerLiteralType | ||
|
||
@inlinable | ||
public init(integerLiteral value: IntegerLiteralType) { | ||
self.init(RealType(integerLiteral: value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===--- Complex+Numeric.swift --------------------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019 - 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Complex: Numeric { | ||
|
||
@_transparent | ||
public static func *(z: Complex, w: Complex) -> Complex { | ||
return Complex(z.x*w.x - z.y*w.y, z.x*w.y + z.y*w.x) | ||
} | ||
|
||
@_transparent | ||
public static func *=(z: inout Complex, w: Complex) { | ||
z = z * w | ||
} | ||
|
||
/// The complex number with specified real part and zero imaginary part. | ||
/// | ||
/// Equivalent to `Complex(RealType(real), 0)`. | ||
@inlinable | ||
public init<Other: BinaryInteger>(_ real: Other) { | ||
self.init(RealType(real), 0) | ||
} | ||
|
||
/// The complex number with specified real part and zero imaginary part, | ||
/// if it can be constructed without rounding. | ||
@inlinable | ||
public init?<Other: BinaryInteger>(exactly real: Other) { | ||
guard let real = RealType(exactly: real) else { return nil } | ||
self.init(real, 0) | ||
} | ||
|
||
/// The ∞-norm of the value (`max(abs(real), abs(imaginary))`). | ||
/// | ||
/// If you need the Euclidean norm (a.k.a. 2-norm) use the `length` or | ||
/// `lengthSquared` properties instead. | ||
/// | ||
/// Edge cases: | ||
/// - If `z` is not finite, `z.magnitude` is `.infinity`. | ||
/// - If `z` is zero, `z.magnitude` is `0`. | ||
/// - Otherwise, `z.magnitude` is finite and non-zero. | ||
/// | ||
/// See also: | ||
/// - `.length` | ||
/// - `.lengthSquared` | ||
@_transparent | ||
public var magnitude: RealType { | ||
guard isFinite else { return .infinity } | ||
return max(abs(x), abs(y)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===--- Complex+StringConvertible.swift ----------------------*- swift -*-===// | ||
// | ||
// This source file is part of the Swift Numerics open source project | ||
// | ||
// Copyright (c) 2019 - 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Complex: CustomStringConvertible { | ||
public var description: String { | ||
guard isFinite else { return "inf" } | ||
return "(\(x), \(y))" | ||
} | ||
} | ||
|
||
extension Complex: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
"Complex<\(RealType.self)>(\(String(reflecting: x)), \(String(reflecting: y)))" | ||
} | ||
} |
Oops, something went wrong.