mirror of
https://github.com/apple/swift-numerics
synced 2026-11-23 04:34:25 +00:00
43 lines
1.6 KiB
Swift
43 lines
1.6 KiB
Swift
//===--- Scale.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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Policy: deliberately not using the * and / operators for these at the
|
|
// moment, because then there's an ambiguity in expressions like 2*z; is
|
|
// that Complex(2) * z or is it RealType(2) * z? This is especially
|
|
// problematic in type inference: suppose we have:
|
|
//
|
|
// let a: RealType = 1
|
|
// let b = 2*a
|
|
//
|
|
// what is the type of b? If we don't have a type context, it's ambiguous.
|
|
// If we have a Complex type context, then b will be inferred to have type
|
|
// Complex! Obviously, that doesn't help anyone.
|
|
|
|
extension Complex {
|
|
/// `self` multiplied by the real value `a`.
|
|
///
|
|
/// Equivalent to `self * Complex(a)`, but may be computed more efficiently.
|
|
@inlinable @inline(__always)
|
|
public func multiplied(by a: RealType) -> Complex {
|
|
Complex(x*a, y*a)
|
|
}
|
|
|
|
/// `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.
|
|
@inlinable @inline(__always)
|
|
public func divided(by a: RealType) -> Complex {
|
|
// See implementation notes for `multiplied` above.
|
|
Complex(x/a, y/a)
|
|
}
|
|
}
|