From 53af148f0f0ecbbcff4234c3fb024061d29d7166 Mon Sep 17 00:00:00 2001 From: Stephen Canon Date: Mon, 13 Jan 2020 16:19:19 -0500 Subject: [PATCH] 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. --- Sources/Complex/Arithmetic.swift | 4 ++- Sources/Real/Field.swift | 51 ++++++++++++++++++++++++++++++++ Sources/Real/Real.swift | 12 +++++++- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 Sources/Real/Field.swift diff --git a/Sources/Complex/Arithmetic.swift b/Sources/Complex/Arithmetic.swift index 2c00307..7e61641 100644 --- a/Sources/Complex/Arithmetic.swift +++ b/Sources/Complex/Arithmetic.swift @@ -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) diff --git a/Sources/Real/Field.swift b/Sources/Real/Field.swift new file mode 100644 index 0000000..020a382 --- /dev/null +++ b/Sources/Real/Field.swift @@ -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 + } +} diff --git a/Sources/Real/Real.swift b/Sources/Real/Real.swift index 7ef4374..27c4988 100644 --- a/Sources/Real/Real.swift +++ b/Sources/Real/Real.swift @@ -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 + } }