-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian_integer.go
238 lines (215 loc) · 6.61 KB
/
gaussian_integer.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// MIT License
//
// Copyright (c) 2022 Tommy TIAN
//
// 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.
package complex
import (
"math/big"
)
// GaussianInt represents a Gaussian integer, that is, a complex number
// whose real and imaginary parts are both integers.
type GaussianInt struct {
R *big.Int // Real part
I *big.Int // Imaginary part
}
// String returns the string representation of the Gaussian integer.
func (g *GaussianInt) String() string {
rSign := g.R.Sign()
iSign := g.I.Sign()
res := ""
if rSign != 0 {
res += g.R.String()
}
if iSign == 0 {
if res == "" {
return "0"
}
return res
}
if iSign == 1 && rSign != 0 {
res += "+"
}
if g.I.Cmp(bigNeg1) == 0 {
res += "-"
} else if g.I.Cmp(big1) != 0 {
res += g.I.String()
}
res += "i"
return res
}
// NewGaussianInt creates a new Gaussian integer with the specified real and imaginary parts.
func NewGaussianInt(r *big.Int, i *big.Int) *GaussianInt {
return &GaussianInt{
R: new(big.Int).Set(r),
I: new(big.Int).Set(i),
}
}
// Set assigns the value of another Gaussian integer to this one.
func (g *GaussianInt) Set(a *GaussianInt) *GaussianInt {
if g.R == nil {
g.R = new(big.Int)
}
g.R.Set(a.R)
if g.I == nil {
g.I = new(big.Int)
}
g.I.Set(a.I)
return g
}
// Update sets the real and imaginary parts of the Gaussian integer.
func (g *GaussianInt) Update(r, i *big.Int) *GaussianInt {
if g.R == nil {
g.R = new(big.Int)
}
g.R.Set(r)
if g.I == nil {
g.I = new(big.Int)
}
g.I.Set(i)
return g
}
// Add computes the sum of two Gaussian integers and stores the result in the receiver.
func (g *GaussianInt) Add(a, b *GaussianInt) *GaussianInt {
if g.R == nil {
g.R = new(big.Int)
}
g.R.Add(a.R, b.R)
if g.I == nil {
g.I = new(big.Int)
}
g.I.Add(a.I, b.I)
return g
}
// Sub subtracts one Gaussian integer from another and stores the result in the receiver.
func (g *GaussianInt) Sub(a, b *GaussianInt) *GaussianInt {
if g.R == nil {
g.R = new(big.Int)
}
g.R.Sub(a.R, b.R)
if g.I == nil {
g.I = new(big.Int)
}
g.I.Sub(a.I, b.I)
return g
}
// Prod calculates the product of two Gaussian integers and stores the result in the receiver.
func (g *GaussianInt) Prod(a, b *GaussianInt) *GaussianInt {
// Compute: (a.R + a.I*i) * (b.R + b.I*i)
// = (a.R*b.R - a.I*b.I) + (a.R*b.I + a.I*b.R)*i
r := new(big.Int).Mul(a.R, b.R)
opt := iPool.Get().(*big.Int)
defer iPool.Put(opt)
r.Sub(r, opt.Mul(a.I, b.I))
i := new(big.Int).Mul(a.R, b.I)
i.Add(i, opt.Mul(a.I, b.R))
g.R, g.I = r, i
return g
}
// Conj computes the conjugate of the given Gaussian integer and stores it in the receiver.
func (g *GaussianInt) Conj(origin *GaussianInt) *GaussianInt {
// The conjugate of (R + I*i) is (R - I*i).
img := new(big.Int).Neg(origin.I)
g.Update(origin.R, img)
return g
}
// Norm returns the norm of the Gaussian integer (R^2 + I^2).
func (g *GaussianInt) Norm() *big.Int {
norm := new(big.Int).Mul(g.R, g.R)
opt := iPool.Get().(*big.Int)
defer iPool.Put(opt)
opt.Mul(g.I, g.I)
norm.Add(norm, opt)
return norm
}
// Copy creates a deep copy of the Gaussian integer.
func (g *GaussianInt) Copy() *GaussianInt {
return NewGaussianInt(
new(big.Int).Set(g.R),
new(big.Int).Set(g.I),
)
}
// Div performs Euclidean division of two Gaussian integers (a / b).
// The remainder is stored in the receiver, and the quotient is returned as a new Gaussian integer.
func (g *GaussianInt) Div(a, b *GaussianInt) *GaussianInt {
// Compute the conjugate of b.
bConj := new(GaussianInt).Conj(b)
// Numerator = a * conjugate(b)
numerator := new(GaussianInt).Prod(a, bConj)
// Denominator = b * conjugate(b)
denominator := new(GaussianInt).Prod(b, bConj)
// Use the real part of the denominator for the division.
deFloat := fPool.Get().(*big.Float).SetInt(denominator.R)
defer fPool.Put(deFloat)
// Compute the real quotient component.
realScalar := fPool.Get().(*big.Float).SetInt(numerator.R)
defer fPool.Put(realScalar)
realScalar.Quo(realScalar, deFloat)
// Compute the imaginary quotient component.
imgScalar := fPool.Get().(*big.Float).SetInt(numerator.I)
defer fPool.Put(imgScalar)
imgScalar.Quo(imgScalar, deFloat)
// Round the computed float values to the nearest integers.
rsInt := iPool.Get().(*big.Int)
defer iPool.Put(rsInt)
rsInt = roundFloat(realScalar)
isInt := iPool.Get().(*big.Int)
defer iPool.Put(isInt)
isInt = roundFloat(imgScalar)
quotient := NewGaussianInt(rsInt, isInt)
// Compute the remainder: remainder = a - (quotient * b)
opt := new(GaussianInt).Prod(quotient, b)
g.Sub(a, opt)
return quotient
}
// Equals returns true if the Gaussian integer is equal to the given Gaussian integer.
func (g *GaussianInt) Equals(a *GaussianInt) bool {
return g.R.Cmp(a.R) == 0 && g.I.Cmp(a.I) == 0
}
// IsZero returns true if the Gaussian integer is zero.
func (g *GaussianInt) IsZero() bool {
return g.R.Sign() == 0 && g.I.Sign() == 0
}
// IsOne returns true if the Gaussian integer equals one.
func (g *GaussianInt) IsOne() bool {
return g.R.Sign() == 1 && g.I.Sign() == 0
}
// CmpNorm compares the norms of two Gaussian integers.
func (g *GaussianInt) CmpNorm(a *GaussianInt) int {
return g.Norm().Cmp(a.Norm())
}
// GCD calculates the greatest common divisor of two Gaussian integers using the Euclidean algorithm.
// The result is stored in the receiver and also returned as a new Gaussian integer.
func (g *GaussianInt) GCD(a, b *GaussianInt) *GaussianInt {
ac := new(GaussianInt).Set(a)
bc := new(GaussianInt).Set(b)
if ac.CmpNorm(bc) < 0 {
ac, bc = bc, ac
}
remainder := new(GaussianInt)
for {
remainder.Div(ac, bc)
if remainder.IsZero() {
g.Set(bc)
return NewGaussianInt(bc.R, bc.I)
}
ac.Set(bc)
bc.Set(remainder)
}
}