-
Notifications
You must be signed in to change notification settings - Fork 32
Add bindings for type qfb_t #282
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
Open
remyoudompheng
wants to merge
1
commit into
flintlib:main
Choose a base branch
from
remyoudompheng:qfb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -72,7 +72,7 @@ modules=( | |
# "dlog" | ||
# "bool_mat" | ||
# "perm" | ||
# "qfb" | ||
"qfb" | ||
# "nf" | ||
# "nf_elem" | ||
# "fmpzi" | ||
|
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
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,35 @@ | ||
from flint.flintlib.types.flint cimport fmpz_t, slong, ulong | ||
from flint.flintlib.types.qfb cimport qfb_t | ||
|
||
# unknown type qfb | ||
# unknown type qfb_hash_t | ||
|
||
|
||
cdef extern from "flint/qfb.h": | ||
void qfb_init(qfb_t q) | ||
void qfb_clear(qfb_t q) | ||
# void qfb_array_clear(qfb ** forms, slong num) | ||
# qfb_hash_t * qfb_hash_init(slong depth) | ||
# void qfb_hash_clear(qfb_hash_t * qhash, slong depth) | ||
# void qfb_hash_insert(qfb_hash_t * qhash, qfb_t q, qfb_t q2, slong iter, slong depth) | ||
# slong qfb_hash_find(qfb_hash_t * qhash, qfb_t q, slong depth) | ||
void qfb_set(qfb_t f, qfb_t g) | ||
int qfb_equal(qfb_t f, qfb_t g) | ||
void qfb_print(qfb_t q) | ||
void qfb_discriminant(fmpz_t D, qfb_t f) | ||
void qfb_reduce(qfb_t r, qfb_t f, fmpz_t D) | ||
int qfb_is_reduced(qfb_t r) | ||
# slong qfb_reduced_forms(qfb ** forms, slong d) | ||
# slong qfb_reduced_forms_large(qfb ** forms, slong d) | ||
void qfb_nucomp(qfb_t r, const qfb_t f, const qfb_t g, fmpz_t D, fmpz_t L) | ||
void qfb_nudupl(qfb_t r, const qfb_t f, fmpz_t D, fmpz_t L) | ||
void qfb_pow_ui(qfb_t r, qfb_t f, fmpz_t D, ulong exp) | ||
void qfb_pow(qfb_t r, qfb_t f, fmpz_t D, fmpz_t exp) | ||
void qfb_inverse(qfb_t r, qfb_t f) | ||
int qfb_is_principal_form(qfb_t f, fmpz_t D) | ||
void qfb_principal_form(qfb_t f, fmpz_t D) | ||
int qfb_is_primitive(qfb_t f) | ||
void qfb_prime_form(qfb_t r, fmpz_t D, fmpz_t p) | ||
int qfb_exponent_element(fmpz_t exponent, qfb_t f, fmpz_t n, ulong B1, ulong B2_sqrt) | ||
int qfb_exponent(fmpz_t exponent, fmpz_t n, ulong B1, ulong B2_sqrt, slong c) | ||
int qfb_exponent_grh(fmpz_t exponent, fmpz_t n, ulong B1, ulong B2_sqrt) |
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,11 @@ | ||
from flint.flintlib.types.flint cimport fmpz_t | ||
|
||
cdef extern from "flint/qfb.h": | ||
|
||
ctypedef struct qfb_struct: | ||
fmpz_t a | ||
fmpz_t b | ||
fmpz_t c | ||
|
||
ctypedef qfb_struct qfb_t[1] | ||
|
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
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 |
---|---|---|
|
@@ -30,6 +30,8 @@ exts = [ | |
'fq_default', | ||
'fq_default_poly', | ||
|
||
'qfb', | ||
|
||
'arf', | ||
|
||
'arb', | ||
|
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,7 @@ | ||
from flint.flintlib.functions.qfb cimport * | ||
from flint.types.fmpz cimport fmpz | ||
|
||
cdef class qfb: | ||
cdef qfb_t val | ||
# the discriminant, stored for convenience | ||
cdef fmpz D |
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,132 @@ | ||
from flint.flintlib.functions.fmpz cimport fmpz_abs, fmpz_root, fmpz_set | ||
from flint.types.fmpz cimport fmpz, any_as_fmpz | ||
from flint.utils.typecheck cimport typecheck | ||
|
||
cdef class qfb: | ||
""" | ||
The qfb type represents definite binary quadratic forms | ||
over Z, with composition, inverse and power operations | ||
compatible with the class group of a given discriminant. | ||
|
||
Some operations require the form to be primitive. | ||
""" | ||
def __cinit__(self): | ||
qfb_init(self.val) | ||
self.D = fmpz(0) | ||
|
||
def __dealloc__(self): | ||
qfb_clear(self.val) | ||
|
||
def __init__(self, a, b, c): | ||
a_fmpz = any_as_fmpz(a) | ||
b_fmpz = any_as_fmpz(b) | ||
c_fmpz = any_as_fmpz(c) | ||
if a_fmpz is NotImplemented: | ||
raise TypeError(f"Incorrect type {type(a)} for qfb coefficient") | ||
if b_fmpz is NotImplemented: | ||
raise TypeError(f"Incorrect type {type(b)} for qfb coefficient") | ||
if c_fmpz is NotImplemented: | ||
raise TypeError(f"Incorrect type {type(c)} for qfb coefficient") | ||
fmpz_set(self.val[0].a, (<fmpz>a_fmpz).val) | ||
fmpz_set(self.val[0].b, (<fmpz>b_fmpz).val) | ||
fmpz_set(self.val[0].c, (<fmpz>c_fmpz).val) | ||
D = fmpz() | ||
qfb_discriminant(D.val, self.val) | ||
self.D = D | ||
|
||
def __repr__(self): | ||
a, b, c = self.coefficients() | ||
return f"qfb({a}, {b}, {c})" | ||
|
||
def __eq__(self, other): | ||
if self is other: | ||
return True | ||
|
||
if typecheck(other, qfb): | ||
return bool(qfb_equal(self.val, (<qfb>other).val)) | ||
|
||
return False | ||
|
||
def __mul__(q1, q2): | ||
"Returns a reduced form equivalent to the composition of q1 and q2" | ||
if not q1.is_primitive(): | ||
raise ValueError(f"{q1} is not primitive") | ||
|
||
cdef qfb res = qfb.__new__(qfb) | ||
cdef fmpz_t L | ||
fmpz_abs(L, q1.D.val) | ||
fmpz_root(L, L, 4) | ||
qfb_nucomp(res.val, q1.val, (<qfb>q2).val, q1.D.val, L) | ||
qfb_reduce(res.val, res.val, q1.D.val) | ||
res.D = q1.D | ||
return res | ||
|
||
def __pow__(q, e, mod): | ||
"Returns a reduced form equivalent to the e-th iterated composition of q" | ||
if mod is not None: | ||
raise NotImplementedError("modular exponentiation") | ||
|
||
if not q.is_primitive(): | ||
raise ValueError(f"{q} is not primitive") | ||
|
||
e_fmpz = any_as_fmpz(e) | ||
if e_fmpz is NotImplemented: | ||
raise TypeError(f"exponent cannot be cast to an fmpz type: {e}") | ||
|
||
# qfb_pow does not support negative exponents and will loop forever | ||
# if a negative integer is provided. | ||
e_abs = abs(e_fmpz) | ||
|
||
cdef qfb res = qfb.__new__(qfb) | ||
qfb_pow(res.val, q.val, q.D.val, (<fmpz>e_abs).val) | ||
if e_fmpz < 0: | ||
qfb_inverse(res.val, res.val) | ||
res.D = q.D | ||
return res | ||
|
||
def coefficients(self): | ||
""" | ||
Returns coefficients (a, b, c) of the form as a polynomial q(x,y)=ax²+bxy+cy² | ||
""" | ||
a = fmpz() | ||
fmpz_set(a.val, self.val[0].a) | ||
b = fmpz() | ||
fmpz_set(b.val, self.val[0].b) | ||
c = fmpz() | ||
fmpz_set(c.val, self.val[0].c) | ||
return a, b, c | ||
|
||
def discriminant(self): | ||
return self.D | ||
|
||
def is_reduced(self): | ||
return bool(qfb_is_reduced(self.val)) | ||
|
||
def is_primitive(self): | ||
return bool(qfb_is_primitive(self.val)) | ||
|
||
def inverse(self): | ||
cdef qfb res = qfb.__new__(qfb) | ||
qfb_inverse(res.val, self.val) | ||
res.D = self.D | ||
return res | ||
|
||
def reduce(self): | ||
cdef qfb res = qfb.__new__(qfb) | ||
qfb_reduce(res.val, self.val, self.D.val) | ||
res.D = self.D | ||
return res | ||
|
||
@classmethod | ||
def prime_form(cls, D, p): | ||
""" | ||
Returns the unique reduced form with 0 < b ≤ p. Requires that p is prime. | ||
""" | ||
|
||
d_fmpz = any_as_fmpz(D) | ||
p_fmpz = any_as_fmpz(p) | ||
|
||
cdef qfb res = qfb.__new__(qfb) | ||
qfb_prime_form(res.val, (<fmpz>d_fmpz).val, (<fmpz>p_fmpz).val) | ||
res.D = d_fmpz | ||
return res |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc for
qfb_nucomp
says that q1 and q2 need to have common discriminant.What happens here if they don't have the same discriminant?
Is it difficult to check?