mirror of
https://github.com/apple/swift-numerics
synced 2026-11-25 04:47:53 +00:00
Introduce Field protocol
For now, this mostly allows one to write code generic over Real and Complex types. TBD whether it should also include finite fields, or be restricted to characteristic-zero. Also bikeshedding to be done on exactly what operations should fall into this protocol and what it should be named. This is just to get discussion started.
This commit is contained in:
1 parent
2c12891617
commit
53af148f0f
3 files changed
+65
-2
No files matched your search
@@ -9,6 +9,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Real
|
||||
|
||||
// MARK: - Additive structure
|
||||
extension Complex: AdditiveArithmetic {
|
||||
@_transparent
|
||||
@@ -62,7 +64,7 @@ extension Complex {
|
||||
}
|
||||
|
||||
// MARK: - Multiplicative structure
|
||||
extension Complex: Numeric {
|
||||
extension Complex: Field {
|
||||
@_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)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
//===--- Field.swift ------------------------------------------*- swift -*-===//
|
||||
//
|
||||
// This source file is part of the Swift Numerics open source project
|
||||
//
|
||||
// Copyright (c) 2019 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// TODO: refining Numeric is slightly annoying because Numeric has the
|
||||
// magnitude property and associated type, which doesn't quite make sense
|
||||
// for all fields. However, simply picking magnitude = self in those cases
|
||||
// is probably a sensible workaround, so this isn't catastrophic. Still,
|
||||
// slightly annoying.
|
||||
|
||||
/// A type modeling an algebraic field.
|
||||
///
|
||||
/// This protocol refines `Numeric`, adding division and reciprocal operations.
|
||||
public protocol Field: Numeric {
|
||||
|
||||
static func /=(a: inout Self, b: Self)
|
||||
|
||||
static func /(a: Self, b: Self) -> Self
|
||||
|
||||
/// The (approximate) reciprocal (mulitplicative inverse) of this number, if one is representable.
|
||||
///
|
||||
/// If reciprocal is non-nil, you can replace division by self with multiplication by reciprocal and
|
||||
/// either get exact the same result (for finite fields) or approximately the same result up to a
|
||||
/// typical rounding error (for floating-point formats).
|
||||
///
|
||||
/// If self is zero, or if a reciprocal would overflow or underflow such that it cannot be accurately
|
||||
/// represented, the result is nil. Implementations should be *conservative*; it is OK to return
|
||||
/// nil even in some cases where a reciprocal can be represented. For this reason, a default
|
||||
/// implementation that simply always returns nil is provided.
|
||||
var reciprocal: Self? { get }
|
||||
}
|
||||
|
||||
extension Field {
|
||||
@_transparent
|
||||
public static func /(a: Self, b: Self) -> Self {
|
||||
var a = a
|
||||
a /= b
|
||||
return a
|
||||
}
|
||||
|
||||
public var reciprocal: Self? {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -26,7 +26,8 @@
|
||||
/// -
|
||||
/// - `ElementaryFunctions`
|
||||
/// - `RealFunctions`
|
||||
public protocol Real: FloatingPoint, RealFunctions {
|
||||
/// - `Field`
|
||||
public protocol Real: FloatingPoint, RealFunctions, Field {
|
||||
}
|
||||
|
||||
// While `Real` does not provide any additional customization points,
|
||||
@@ -79,4 +80,13 @@ extension Real {
|
||||
public static func sqrt(_ x: Self) -> Self {
|
||||
return x.squareRoot()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var reciprocal: Self? {
|
||||
let recip = 1/self
|
||||
if recip.isNormal || isZero || !isFinite {
|
||||
return recip
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user