forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex128.go
163 lines (127 loc) · 3.74 KB
/
complex128.go
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
package ring
import (
"math"
"math/big"
)
// NewFloat creates a new big.Float element with "prec" bits of precision
func NewFloat(x float64, prec uint) (y *big.Float) {
y = new(big.Float)
y.SetPrec(prec) // decimal precision
y.SetFloat64(x)
return
}
// Cos implements the arbitrary precision computation of Cos(x)
// Iterative process with an error of ~10^{−0.60206*k} after k iterations.
// ref: Johansson, B. Tomas, An elementary algorithm to evaluate trigonometric functions to high precision, 2018
func Cos(x *big.Float) (cosx *big.Float) {
tmp := new(big.Float)
prec := x.Prec()
k := int(math.Ceil(float64(x.Prec()) / (3.3219280948873626 * 0.60206))) // number of iterations : ceil( prec(log2) / (log10* 0.60206))
t := NewFloat(0.5, prec)
half := new(big.Float).Copy(t)
for i := 1; i < k-1; i++ {
t.Mul(t, half)
}
s := new(big.Float).Mul(x, t)
s.Mul(s, x)
s.Mul(s, t)
four := NewFloat(4.0, prec)
for i := 1; i < k; i++ {
tmp.Sub(four, s)
s.Mul(s, tmp)
}
cosx = new(big.Float).Quo(s, NewFloat(2.0, prec))
cosx.Sub(NewFloat(1.0, prec), cosx)
return
}
// Complex is a type for arbitrary precision complex number
type Complex [2]*big.Float
// NewComplex creates a new arbitrary precision complex number
func NewComplex(a, b *big.Float) (c *Complex) {
c = new(Complex)
for i := 0; i < 2; i++ {
c[i] = new(big.Float)
}
if a != nil {
c[0].Set(a)
}
if b != nil {
c[1].Set(b)
}
return
}
// Set sets a arbitrary precision complex number
func (c *Complex) Set(a *Complex) {
c[0].Set(a[0])
c[1].Set(a[1])
}
// Copy returns a new copy of the target arbitrary precision complex number
func (c *Complex) Copy() *Complex {
return NewComplex(c[0], c[1])
}
// Real returns the real part as a big.Float
func (c *Complex) Real() *big.Float {
return c[0]
}
// Imag returns the imaginary part as a big.Float
func (c *Complex) Imag() *big.Float {
return c[1]
}
// Float64 returns the arbitrary precision complex number as a complex128
func (c *Complex) Float64() complex128 {
a, _ := c[0].Float64()
b, _ := c[1].Float64()
return complex(a, b)
}
// Add adds two arbitrary precision complex numbers together
func (c *Complex) Add(a, b *Complex) {
c[0].Add(a[0], b[0])
c[1].Add(a[1], b[1])
}
// Sub subtracts two arbitrary precision complex numbers together
func (c *Complex) Sub(a, b *Complex) {
c[0].Sub(a[0], b[0])
c[1].Sub(a[1], b[1])
}
// ComplexMultiplier is a struct for the multiplication or division of two arbitrary precision complex numbers
type ComplexMultiplier struct {
tmp0 *big.Float
tmp1 *big.Float
tmp2 *big.Float
tmp3 *big.Float
}
// NewComplexMultiplier creates a new ComplexMultiplier
func NewComplexMultiplier() (cEval *ComplexMultiplier) {
cEval = new(ComplexMultiplier)
cEval.tmp0 = new(big.Float)
cEval.tmp1 = new(big.Float)
cEval.tmp2 = new(big.Float)
cEval.tmp3 = new(big.Float)
return
}
// Mul multiplies two arbitrary precision complex numbers together
func (cEval *ComplexMultiplier) Mul(a, b, c *Complex) {
cEval.tmp0.Mul(a[0], b[0])
cEval.tmp1.Mul(a[1], b[1])
cEval.tmp2.Mul(a[0], b[1])
cEval.tmp3.Mul(a[1], b[0])
c[0].Sub(cEval.tmp0, cEval.tmp1)
c[1].Add(cEval.tmp2, cEval.tmp3)
}
// Div divides two arbitrary precision complex numbers together
func (cEval *ComplexMultiplier) Div(a, b, c *Complex) {
// tmp0 = (a[0] * b[0]) + (a[1] * b[1]) real part
// tmp1 = (a[1] * b[0]) - (a[0] * b[0]) imag part
// tmp2 = (b[0] * b[0]) + (b[1] * b[1]) denominator
cEval.tmp0.Mul(a[0], b[0])
cEval.tmp1.Mul(a[1], b[1])
cEval.tmp2.Mul(a[1], b[0])
cEval.tmp3.Mul(a[0], b[1])
cEval.tmp0.Add(cEval.tmp0, cEval.tmp1)
cEval.tmp1.Sub(cEval.tmp2, cEval.tmp3)
cEval.tmp2.Mul(b[0], b[0])
cEval.tmp3.Mul(b[1], b[1])
cEval.tmp2.Add(cEval.tmp2, cEval.tmp3)
c[0].Quo(cEval.tmp0, cEval.tmp2)
c[1].Quo(cEval.tmp1, cEval.tmp2)
}