-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.rv
More file actions
195 lines (154 loc) · 2.98 KB
/
Copy pathcomplex.rv
File metadata and controls
195 lines (154 loc) · 2.98 KB
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
import { ... } from "./core.rv"
import { ... } from "./numbers.rv"
import { matchTrait } from "./patterns.rv"
import { pi } from "./wasm/numeric.rv"
export { Complex, real, imag }
bundle Complex(re: Real, im: Real)
fn Complex(re: Real, im: Real) {
[re, im] = promote(re, im)
pack(Complex, re, im)
}
fn Complex(re: Real) { Complex(re, zero(re)) }
@extend
fn matchTrait($Number, x: Complex) { Some(x) }
@extend
fn real(Complex(re, im)) { re }
@extend
fn imag(Complex(re, im)) { im }
@extend
fn zero(Complex(re, im)) { Complex(zero(re)) }
@extend
fn one(Complex(re, im)) { Complex(one(re)) }
@extend
fn im(Complex(re, im)) { Complex(zero(re), one(re)) }
@extend
fn promote(Complex(ra, ia), Complex(rb, ib)) {
[ra, rb] = promote(ra, rb)
[ia, ib] = promote(ia, ib)
[Complex(ra, ia), Complex(rb, ib)]
}
@extend
fn coerce(Complex(re, im), x: Real) {
[re, x] = promote(re, x)
[Complex(re, im), Complex(x)]
}
@extend
fn conj(x: Real) { x }
@extend
fn conj(Complex(re, im)) {
Complex(re, -im)
}
@extend
fn abs(z: Complex) {
hypot(real(z), imag(z))
}
@extend
fn abs2(Complex(re, im)) {
re * re + im * im
}
fn arg(Complex(re, im)) {
atan(im, re)
}
@extend
fn Complex(ra, ia) + Complex(rb, ib) {
Complex(ra + rb, ia + ib)
}
@extend
fn (-Complex(re, im)) {
Complex(-re, -im)
}
@extend
fn Complex(ra, ia) - Complex(rb, ib) {
Complex(ra - rb, ia - ib)
}
@extend
fn Complex(ra, ia) * Complex(rb, ib) {
Complex(ra * rb - ia * ib, ra * ib + rb * ia)
}
@extend
fn Complex(re, im) / (x: Real) {
Complex(re / x, im / x)
}
@extend
fn (z: Complex) / (w: Complex) {
(z * conj(w)) / abs2(w)
}
@extend
fn sqrt(Complex(re, im)) {
r = hypot(re, im)
y = Complex(sqrt((r + re) / 2), sqrt((r - re) / 2))
if im < 0 { y = conj(y) }
return y
}
@extend
fn cbrt(z: Complex) {
pow(z, 1.0 / 3.0)
}
@extend
fn exp(Complex(re, im)) {
r = exp(re)
Complex(r * cos(im), r * sin(im))
}
@extend
fn log(z: Complex) {
Complex(log(abs(z)), arg(z))
}
@extend
fn pow(x: Complex, y: Number) {
exp(y * log(x))
}
@extend
fn sin(Complex(re, im)) {
Complex(sin(re) * cosh(im), cos(re) * sinh(im))
}
@extend
fn cos(Complex(re, im)) {
Complex(cos(re) * cosh(im), -sin(re) * sinh(im))
}
@extend
fn tan(z: Complex) {
sin(z) / cos(z)
}
@extend
fn asin(z: Complex) {
-im(z) * log(im(z) * z + sqrt(one(z) - z * z))
}
@extend
fn acos(z: Complex) {
Complex(pi / 2.0) - asin(z)
}
@extend
fn atan(z: Complex) {
im(z) * (log(one(z) - im(z) * z) - log(one(z) + im(z) * z)) / 2
}
@extend
fn sinh(Complex(re, im)) {
Complex(sinh(re) * cos(im), cosh(re) * sin(im))
}
@extend
fn cosh(Complex(re, im)) {
Complex(cosh(re) * cos(im), sinh(re) * sin(im))
}
@extend
fn tanh(z: Complex) {
sinh(z) / cosh(z)
}
@extend
fn asinh(z: Complex) {
log(z + sqrt(z * z + one(z)))
}
@extend
fn acosh(z: Complex) {
log(z + sqrt(z + one(z)) * sqrt(z - one(z)))
}
@extend
fn atanh(z: Complex) {
(log(one(z) + z) - log(one(z) - z)) / 2
}
@extend
fn show(Complex(re, im)) {
show(re)
print(" + ")
show(im)
print("im")
}