// // Complex.swift // Arithmosophi /* The MIT License (MIT) Copyright (c) 2015 Eric Marchand (phimage) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #if os(Linux) import Glibc #else import Darwin import CoreGraphics #if os(watchOS) import UIKit #endif #endif public struct Complex { public var real: T = T() public var imaginary: T public init() { let zero = T() self.init(real: zero, imaginary: zero) } public init(real: T, imaginary: T) { self.real = real self.imaginary = imaginary } // conjugate public var conjugate: Complex { return Complex(real: real, imaginary: -imaginary) } // `self * i` public var i: Complex { return Complex(real: -imaginary, imaginary: real) } // norm public var norm: T { return real * real + imaginary * imaginary } // a tuble of real and imaginary value public var tuple: (T, T) { get { return (real, imaginary) } set(t) { (real, imaginary) = t } } public func combine(_ rhs: Complex, combineBehavior: (T, T) -> T) -> Complex { let realPart = combineBehavior(self.real, rhs.real) let imaginaryPart = combineBehavior(self.imaginary, rhs.imaginary) return Complex(real: realPart, imaginary: imaginaryPart) } public var description: String { let zero = T() return self.imaginary < zero ? "\(self.real)-\(-self.imaginary)i" : "\(self.real)+\(self.imaginary)i" } public func map(_ function: (T, T) -> (T, T)) -> Complex { let result = function(self.real, self.imaginary) return Complex(real: result.0, imaginary: result.1) } public func map(_ function: (T) -> T) -> Complex { return Complex(real: function(self.real), imaginary: function(self.imaginary)) } } extension ArithmeticType { var complex: Complex { return Complex(real: self, imaginary: Self()) } } extension Complex where T: Arithmos { public func sqrt() -> Complex { return map { value in return value.sqrt() } } } extension Complex where T: Arithmos { public var description: String { let sig = imaginary.isSignMinus ? "-" : "+" return "\(self.real) \(sig) \(self.imaginary)i" } public var abs: T { get { return real.hypot(imaginary) } set(r) { let f = r / abs // swiftlint:disable:next shorthand_operator real = real * f // swiftlint:disable:next shorthand_operator imaginary = imaginary * f } } public var argument: T { get { return imaginary.atan2(real) } set(t) { let m = abs real = m * t.cos() imaginary = m * t.sin() } } public var projection: Complex { if real.isFinite && imaginary.isFinite { return self } else { return Complex( real: T(1)/T(0), imaginary: imaginary.isSignMinus ? -T(0) : T(0) ) } } } extension Arithmos /*where Self: Equatable*/ { public var isSignMinus: Bool { return self != self.abs() } } extension Complex: Additive, Initializable {} public extension ArithmeticType { // self * 1.0i var i: Complex { return Complex(real: Self()/*zero*/, imaginary: self) } } // MARK: Addable extension Complex: Addable {} public func + (lhs: Complex, rhs: T) -> Complex { return lhs + Complex(real: rhs, imaginary: T()) } public func + (lhs: T, rhs: Complex) -> Complex { return Complex(real: lhs, imaginary: T()) + rhs } public func + (lhs: Complex, rhs: Complex) -> Complex { return lhs.combine(rhs, combineBehavior: +) } // MARK: Substractable extension Complex: Substractable {} public func - (lhs: Complex, rhs: T) -> Complex { return lhs - Complex(real: rhs, imaginary: T()) } public func - (lhs: T, rhs: Complex) -> Complex { return Complex(real: lhs, imaginary: T()) - rhs } public func - (lhs: Complex, rhs: Complex) -> Complex { return lhs.combine(rhs, combineBehavior: -) } // MARK: Multiplicable extension Complex: Multiplicable {} public func * (lhs: Complex, rhs: T) -> Complex { return lhs * Complex(real: rhs, imaginary: T()) } public func * (lhs: T, rhs: Complex) -> Complex { return Complex(real: lhs, imaginary: T()) * rhs } public func * (lhs: Complex, rhs: Complex) -> Complex { let productOfReals = lhs.real * rhs.real let productOfImaginaries = rhs.imaginary * lhs.imaginary let realPart = productOfReals - productOfImaginaries let imaginaryPart = ((lhs.real + lhs.imaginary) * (rhs.real + rhs.imaginary)) - productOfReals - productOfImaginaries return Complex(real: realPart, imaginary: imaginaryPart) } // MARK: Dividable extension Complex: Dividable {} public func / (lhs: Complex, rhs: T) -> Complex { return lhs / Complex(real: rhs, imaginary: T()) } public func / (lhs: T, rhs: Complex) -> Complex { return Complex(real: lhs, imaginary: T()) / rhs } public func / (lhs: Complex, rhs: Complex) -> Complex { let denominator = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary let realPart = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / denominator let imaginaryPart = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / denominator return Complex(real: realPart, imaginary: imaginaryPart) } // MARK: Negatable extension Complex: Negatable {} public prefix func - (instance: Complex) -> Complex { return Complex(real: -instance.real, imaginary: -instance.imaginary) } // MARK: Modulable extension Complex: Modulable {} public func % (lhs: Complex, rhs: Complex) -> Complex { return lhs - (lhs / rhs) * rhs } public func % (lhs: Complex, rhs: T) -> Complex { return lhs - (lhs / rhs) * rhs } public func % (lhs: T, rhs: Complex) -> Complex { return Complex(real: lhs, imaginary: T()) % rhs } public func %= (lhs: inout Complex, rhs: Complex) { lhs = lhs % rhs } public func %= (lhs: inout Complex, rhs: T) { lhs = lhs % rhs } // MARK: Equatable extension Complex: Equatable {} public func == (lhs: Complex, rhs: Complex) -> Bool { return lhs.real == rhs.real && lhs.imaginary == rhs.imaginary } // MARK: Hashable /*extension Complex: Hashable where T: Hashable { public var hashValue: Int { return self.real.hashValue + self.imaginary.hashValue } }*/ // MARK: CGPoint public protocol Complexable { associatedtype AssociatedType: ArithmeticType var complex: Complex {get} init(complex: Complex) } #if !os(Linux) public typealias ComplexCGFloat = Complex extension CGPoint: Complexable { public var complex: ComplexCGFloat { return ComplexCGFloat(real: self.x, imaginary: self.y) } public init(complex: ComplexCGFloat) { self.init(x: complex.real, y: complex.imaginary) } } #endif