-
Notifications
You must be signed in to change notification settings - Fork 227
Feat: Eisenstein integers #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a834f5
feat: implement Eisenstein integers arithmetic
yelhousni dda7a27
feat: half-GCD for Eisenstein integers
yelhousni 6f69c7e
test: half-GCD test with bigger integers
yelhousni 9b31869
style: clean comments
yelhousni 705a31a
refactor: move eisenstein under field/
yelhousni 7febb56
fix: apply review suggestions
yelhousni 12d53bf
fix: makes linter happy
yelhousni fa1b905
fix: consider all possible remainders
yelhousni 9a81265
Merge branch 'master' into feat/eisenstein
yelhousni 13fa612
fix: use sqrt in eisenstein halfgcd condition
yelhousni 507de62
perf(eisentein/half-GCD): only 1 remainder option
yelhousni 2e01a9f
refactor: apply review suggestions
yelhousni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // The Eisenstein integers form a commutative ring of algebraic integers in the | ||
| // algebraic number field Q(ω) – the third cyclotomic field. These are of the | ||
| // form z = a + bω, where a and b are integers and ω is a primitive third root | ||
| // of unity i.e. ω²+ω+1 = 0. | ||
| package eisenstein | ||
|
|
||
| import ( | ||
| "math/big" | ||
| ) | ||
|
|
||
| // A ComplexNumber represents an arbitrary-precision Eisenstein integer. | ||
| type ComplexNumber struct { | ||
| A0, A1 big.Int | ||
yelhousni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // String implements Stringer interface for fancy printing | ||
| func (z *ComplexNumber) String() string { | ||
| return z.A0.String() + "+(" + z.A1.String() + "*ω)" | ||
| } | ||
|
|
||
| // Equal returns true if z equals x, false otherwise | ||
| func (z *ComplexNumber) Equal(x *ComplexNumber) bool { | ||
| return z.A0.Cmp(&x.A0) == 0 && z.A1.Cmp(&x.A1) == 0 | ||
| } | ||
|
|
||
| // Set sets z to x, and returns z. | ||
| func (z *ComplexNumber) Set(x *ComplexNumber) *ComplexNumber { | ||
| z.A0.Set(&x.A0) | ||
| z.A1.Set(&x.A1) | ||
| return z | ||
| } | ||
|
|
||
| // SetZero sets z to 0, and returns z. | ||
| func (z *ComplexNumber) SetZero() *ComplexNumber { | ||
| z.A0 = *big.NewInt(0) | ||
| z.A1 = *big.NewInt(0) | ||
| return z | ||
| } | ||
|
|
||
| // SetOne sets z to 1, and returns z. | ||
| func (z *ComplexNumber) SetOne() *ComplexNumber { | ||
| z.A0 = *big.NewInt(1) | ||
| z.A1 = *big.NewInt(0) | ||
| return z | ||
| } | ||
|
|
||
| // Neg sets z to the negative of x, and returns z. | ||
| func (z *ComplexNumber) Neg(x *ComplexNumber) *ComplexNumber { | ||
| z.A0.Neg(&x.A0) | ||
| z.A1.Neg(&x.A1) | ||
| return z | ||
| } | ||
|
|
||
| // Conjugate sets z to the conjugate of x, and returns z. | ||
| func (z *ComplexNumber) Conjugate(x *ComplexNumber) *ComplexNumber { | ||
| z.A0.Sub(&x.A0, &x.A1) | ||
| z.A1.Neg(&x.A1) | ||
yelhousni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return z | ||
| } | ||
|
|
||
| // Add sets z to the sum of x and y, and returns z. | ||
| func (z *ComplexNumber) Add(x, y *ComplexNumber) *ComplexNumber { | ||
| z.A0.Add(&x.A0, &y.A0) | ||
| z.A1.Add(&x.A1, &y.A1) | ||
| return z | ||
| } | ||
|
|
||
| // Sub sets z to the difference of x and y, and returns z. | ||
| func (z *ComplexNumber) Sub(x, y *ComplexNumber) *ComplexNumber { | ||
| z.A0.Sub(&x.A0, &y.A0) | ||
| z.A1.Sub(&x.A1, &y.A1) | ||
| return z | ||
| } | ||
|
|
||
| // Mul sets z to the product of x and y, and returns z. | ||
| // | ||
| // Given that ω²+ω+1=0, the explicit formula is: | ||
| // | ||
| // (x0+x1ω)(y0+y1ω) = (x0y0-x1y1) + (x0y1+x1y0-x1y1)ω | ||
| func (z *ComplexNumber) Mul(x, y *ComplexNumber) *ComplexNumber { | ||
| var t [3]big.Int | ||
| var z0, z1 big.Int | ||
| t[0].Mul(&x.A0, &y.A0) | ||
| t[1].Mul(&x.A1, &y.A1) | ||
| z0.Sub(&t[0], &t[1]) | ||
| t[0].Mul(&x.A0, &y.A1) | ||
| t[2].Mul(&x.A1, &y.A0) | ||
| t[0].Add(&t[0], &t[2]) | ||
| z1.Sub(&t[0], &t[1]) | ||
| z.A0.Set(&z0) | ||
| z.A1.Set(&z1) | ||
| return z | ||
| } | ||
|
|
||
| // Norm returns the norm of z. | ||
| // | ||
| // The explicit formula is: | ||
| // | ||
| // N(x0+x1ω) = x0² + x1² - x0*x1 | ||
| func (z *ComplexNumber) Norm() *big.Int { | ||
| norm := new(big.Int) | ||
| temp := new(big.Int) | ||
| norm.Add( | ||
| norm.Mul(&z.A0, &z.A0), | ||
| temp.Mul(&z.A1, &z.A1), | ||
| ) | ||
| norm.Sub( | ||
| norm, | ||
| temp.Mul(&z.A0, &z.A1), | ||
| ) | ||
| return norm | ||
| } | ||
|
|
||
| // Quo sets z to the quotient of x and y, and returns z. | ||
| func (z *ComplexNumber) Quo(x, y *ComplexNumber) *ComplexNumber { | ||
yelhousni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| norm := y.Norm() | ||
yelhousni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| z.Conjugate(y) | ||
| z.Mul(x, z) | ||
| z.A0.Div(&z.A0, norm) | ||
| z.A1.Div(&z.A1, norm) | ||
| return z | ||
| } | ||
yelhousni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // HalfGCD returns the rational reconstruction of a, b. | ||
| // This outputs w, v, u s.t. w = a*u + b*v. | ||
| func HalfGCD(a, b *ComplexNumber) [3]*ComplexNumber { | ||
|
|
||
| var aRun, bRun, u, v, u_, v_, quotient, remainder, t, t1, t2 ComplexNumber | ||
|
|
||
| aRun.Set(a) | ||
| bRun.Set(b) | ||
| u.SetOne() | ||
| v.SetZero() | ||
| u_.SetZero() | ||
| v_.SetOne() | ||
| nbits := a.Norm().BitLen() / 2 | ||
|
|
||
| for aRun.Norm().BitLen() > nbits { | ||
yelhousni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| quotient.Quo(&aRun, &bRun) | ||
| t.Mul(&bRun, "ient) | ||
| remainder.Sub(&aRun, &t) | ||
| t.Mul(&u_, "ient) | ||
| t1.Sub(&u, &t) | ||
| t.Mul(&v_, "ient) | ||
| t2.Sub(&v, &t) | ||
| aRun.Set(&bRun) | ||
| u.Set(&u_) | ||
| v.Set(&v_) | ||
| bRun.Set(&remainder) | ||
| u_.Set(&t1) | ||
| v_.Set(&t2) | ||
| } | ||
|
|
||
| return [3]*ComplexNumber{&aRun, &v, &u} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.