forked from taketo1024/swm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.swift
204 lines (167 loc) · 4.78 KB
/
Complex.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// Complex.swift
// SwiftyMath
//
// Created by Taketo Sano on 2018/03/16.
// Copyright © 2018年 Taketo Sano. All rights reserved.
//
import Foundation
public typealias ComplexNumber = Complex<𝐑>
public typealias 𝐂 = ComplexNumber
public struct Complex<R: Ring>: Ring {
private let x: R
private let y: R
public init(from x: 𝐙) {
self.init(R(from: x))
}
public init(_ x: R) {
self.init(x, .zero)
}
public init(_ x: R, _ y: R) {
self.x = x
self.y = y
}
public static var imaginaryUnit: Complex<R> {
return Complex(.zero, .identity)
}
public var realPart: R {
return x
}
public var imaginaryPart: R {
return y
}
public var conjugate: Complex<R> {
return Complex(x, -y)
}
public var inverse: Complex? {
let r2 = x * x + y * y
if let inv = r2.inverse {
return Complex(x * inv, -y * inv)
} else {
return nil
}
}
public static func ==(lhs: Complex<R>, rhs: Complex<R>) -> Bool {
return (lhs.x == rhs.x) && (lhs.y == rhs.y)
}
public static func +(a: Complex<R>, b: Complex<R>) -> Complex<R> {
return Complex(a.x + b.x, a.y + b.y)
}
public static prefix func -(a: Complex<R>) -> Complex<R> {
return Complex(-a.x, -a.y)
}
public static func *(a: Complex<R>, b: Complex<R>) -> Complex<R> {
return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x)
}
public var hashValue: Int {
let p = 104743
return (x.hashValue % p) &+ (y.hashValue % p) * p
}
public var description: String {
return (x != .zero && y != .zero) ? "\(x) + \(y)i" :
(y == .identity) ? "i" :
(y != .zero) ? "\(y)i"
: "\(x)"
}
public static var symbol: String {
if R.self == 𝐑.self {
return "𝐂"
} else {
return "\(R.symbol)[i]"
}
}
}
public enum _𝐑or𝐙 {
case 𝐑(𝐑)
case 𝐙(𝐙)
}
public protocol _𝐑or𝐙Protocol {
var _value: _𝐑or𝐙 { get }
}
extension 𝐑: _𝐑or𝐙Protocol {
public var _value: _𝐑or𝐙 {
return .𝐑(self)
}
}
extension 𝐙: _𝐑or𝐙Protocol {
public var _value: _𝐑or𝐙 {
return .𝐙(self)
}
}
extension Complex where R: _𝐑or𝐙Protocol {
enum _𝐑or𝐙 {
case 𝐑(Complex<𝐑>)
case 𝐙(Complex<𝐙>)
}
var _value: _𝐑or𝐙 {
switch (self.x._value, self.y._value) {
case (.𝐑(let x), .𝐑(let y)):
return .𝐑(Complex<𝐑>(x, y))
case (.𝐙(let x), .𝐙(let y)):
return .𝐙(Complex<𝐙>(x, y))
default:
fatalError()
}
}
}
extension Complex: Field, NormedSpace where R == 𝐑 {
public init(from r: 𝐐) {
self.init(r)
}
public init(_ x: 𝐙) {
self.init(𝐑(x), 0)
}
public init(_ x: 𝐐) {
self.init(𝐑(x), 0)
}
public init(r: 𝐑, θ: 𝐑) {
self.init(r * cos(θ), r * sin(θ))
}
public var abs: 𝐑 {
return √(x * x + y * y)
}
public var norm: 𝐑 {
return abs
}
public var arg: 𝐑 {
let r = self.norm
if(r == 0) {
return 0
}
let t = acos(x / r)
return (y >= 0) ? t : 2 * π - t
}
}
public typealias GaussInt = Complex<𝐙>
extension Complex: EuclideanRing where R: _𝐑or𝐙Protocol { // 👈
public func eucDiv(by b: Complex<R>) -> (q: Complex<R>, r: Complex<R>) {
switch (self._value, b._value) {
case (.𝐑(let zelf), .𝐑(let b)):
return (zelf * b.inverse! as! Complex<R>, .zero) // Use default implement copy, But
case (.𝐙(let zelf), .𝐙(let b)):
fatalError("TODO")
default:
fatalError()
}
}
public var eucDegree: Int {
switch self._value {
case .𝐑(let zelf):
return zelf == .zero ? 0 : 1 // Use default implement copy
case .𝐙(let zelf):
fatalError("TODO")
}
}
}
extension Complex: ExpressibleByIntegerLiteral where R: ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = R.IntegerLiteralType
public init(integerLiteral n: R.IntegerLiteralType) {
self.init(R(integerLiteral: n))
}
}
extension Complex: ExpressibleByFloatLiteral where R: ExpressibleByFloatLiteral {
public typealias FloatLiteralType = R.FloatLiteralType
public init(floatLiteral x: R.FloatLiteralType) {
self.init(R(floatLiteral: x))
}
}