ranea / qipy Goto Github PK
View Code? Open in Web Editor NEWA Python library to work with elements, ideals, ideal classes and class groups from quadratic integer rings.
Home Page: https://ranea.github.io/QiPy/
License: MIT License
A Python library to work with elements, ideals, ideal classes and class groups from quadratic integer rings.
Home Page: https://ranea.github.io/QiPy/
License: MIT License
Gaussian and Eisenstein integers can do Euclidean division. It would be handy to add floordiv and mod (and a divmod
method that lets you choose the rounding function used, and maybe stuff that depends on divmod
like gcd
).
I'm not sure what should happen if you try a//b
on arbitrary QuadraticInteger
values (where d
is not 1, -1, or -3). One option is to just raise ValueError('not a Euclidean domain')
; another is to return a/b
if b|a
and raise an exception otherwise (which you can do just by return a/b
); another is to just ignore the fact that it's not valid Euclidean division (so abs(a % b) <= abs(b)
is not guaranteed) and do it anyway.
Anyway, I haven't looked at your code in detail yet, so I don't have a pull request. But here's some slapped-together code that works for sympy using a simple a+b*w
representation for d==-3
(it's even simpler for -1 and 1, of course):
w = Symbol('w')
W = (-1 + sqrt(3)*I)/2 # or if you prefer exp(2*pi*I/3)
def rewrite_in_W(x):
x = x.subs(w, W)
a, b3 = re(x), im(x)/sqrt(3)
return a+b3, 2*b3
def divmod(x, y, *, round=floor):
qa, qb = rewrite_in_W(x/y)
qa, qb = round(qa), round(qb)
q = qa + qb*w
r = x - q*y
ra, rb = rewrite_in_W(x - q*y)
assert (ra, rb) == (round(ra), round(rb))
r = ra + rb*w
return q, r
assert divmod(-7+7*w, 1+6*w) == (2+w, -3)
assert divmod(-7+7*w, 1+6*w, round=ceil) == (3+2*w, 2-w)
And here's code that does the same with your type from outside:
Ei = QuadraticIntegerRing(-3)
w = (-1 + sqrt(3)*I)/2
def rewrite_in_w(x):
a, b3 = re(x), im(x)/sqrt(3)
return a+b3, 2*b3
def divmod(x, y, *, round=floor):
qa, qb = rewrite_in_w(x.value/y.value)
qa, qb = round(qa), round(qb)
q = qa + qb*w
r = x - q*y
return type(x)(q), type(x)(r)
ei77 = Ei(-7+7*w)
ei16 = Ei(1+6*w)
assert divmod(ei77, ei16) == (Ei(2+w), Ei(-3))
assert divmod(ei77, ei16, round=ceil) == (Ei(3+2*w), Ei(2-w))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.