Skip to content
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

Include the fq_default type #97

Merged
merged 40 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1bd96ee
Add a VERY rough start on adding fq
GiacomoPope Oct 19, 2023
2488e5f
Merge branch 'flintlib:master' into add_fq
GiacomoPope Oct 19, 2023
20aa30b
Merge branch 'master' into add_fq
GiacomoPope Jan 17, 2024
277794f
Merge branch 'master' into add_fq
GiacomoPope Aug 5, 2024
942152a
Merge branch 'master' into add_fq
GiacomoPope Aug 5, 2024
3d637f6
further work on fq_default with luca de feo
GiacomoPope Aug 6, 2024
b728899
add line to meson build
GiacomoPope Aug 6, 2024
c8fe699
more boilerplate code
GiacomoPope Aug 6, 2024
1307116
add fq_default to the doctests
GiacomoPope Aug 7, 2024
6bf96a7
start on type conversions and comparisons
GiacomoPope Aug 7, 2024
7e68a0c
untested arithemtic
GiacomoPope Aug 7, 2024
7347924
begin adding tests
GiacomoPope Aug 7, 2024
49b882c
clean up enum name, maybe a little hacky...
GiacomoPope Aug 7, 2024
ad5be0b
attempt to fix doctests for all versions
GiacomoPope Aug 7, 2024
ece8c45
add docstrings
GiacomoPope Aug 7, 2024
273deee
remove trailing whitespace
GiacomoPope Aug 7, 2024
78a42dd
remove unused var
GiacomoPope Aug 7, 2024
6e71a59
include equality tests
GiacomoPope Aug 7, 2024
44c3186
move more boilerplate into the flint_base
GiacomoPope Aug 7, 2024
9da32da
remove parsing of list to self in any_to...
GiacomoPope Aug 7, 2024
3c93c45
add to_list method and repr function
GiacomoPope Aug 7, 2024
0a9d276
add tests for fq_default init from context
GiacomoPope Aug 7, 2024
eca0210
some small clean up across the file
GiacomoPope Aug 7, 2024
f2443f8
refactor equality and coercion
GiacomoPope Aug 8, 2024
915837e
attempt to include is_primitive
GiacomoPope Aug 9, 2024
83f2179
use union instead of struct
GiacomoPope Aug 9, 2024
525883f
comment out broken function
GiacomoPope Aug 9, 2024
812fea4
make is_primitive work
GiacomoPope Aug 9, 2024
ef2136a
broken refactor; need help
GiacomoPope Aug 9, 2024
172c9d8
remove is_primitive and modify base template
GiacomoPope Aug 9, 2024
ce8cd4d
modify _arith_ to use cpdef
GiacomoPope Aug 9, 2024
ee4ae24
fix some but not all reviewer comments -- need to stop for the day
GiacomoPope Aug 9, 2024
38b6ce1
change docstrings for the type stuff
GiacomoPope Aug 9, 2024
52f8847
ensure the modulus matches for scalar coercion
GiacomoPope Aug 9, 2024
66c54f4
use try / except for slong conversion
GiacomoPope Aug 9, 2024
163f7cc
move docstring to class
GiacomoPope Aug 9, 2024
e32a6dd
refactor out where the dict is for types
GiacomoPope Aug 9, 2024
876fd2a
move docstring
GiacomoPope Aug 9, 2024
e79b397
add docs
GiacomoPope Aug 9, 2024
f789f22
check modulus for nmod_poly and fmpz_mod_poly
GiacomoPope Aug 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/source/fq_default.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
**fq_default** -- finite fields
===============================================================================

.. autoclass :: flint.fq_default
:members:
:inherited-members:
:undoc-members:

1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Scalar types
fmpq.rst
fmpz_mod.rst
nmod.rst
fq_default.rst
arb.rst
acb.rst
dirichlet.rst
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
("flint.types.fmpq_mpoly", ["src/flint/types/fmpq_mpoly.pyx"]),
("flint.types.fmpz_mpoly_q", ["src/flint/types/fmpz_mpoly_q.pyx"]),

("flint.types.fq_default", ["src/flint/types/fq_default.pyx"]),

("flint.types.arf", ["src/flint/types/arf.pyx"]),
("flint.types.arb", ["src/flint/types/arb.pyx"]),
("flint.types.arb_poly", ["src/flint/types/arb_poly.pyx"]),
Expand Down
2 changes: 2 additions & 0 deletions src/flint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

from .types.fmpq_mpoly import fmpq_mpoly_ctx, fmpq_mpoly, fmpq_mpoly_vec

from .types.fq_default import *

from .types.arf import *
from .types.arb import *
from .types.arb_poly import *
Expand Down
143 changes: 136 additions & 7 deletions src/flint/flint_base/flint_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,136 @@ cdef class flint_elem:


cdef class flint_scalar(flint_elem):
pass
# =================================================
# These are the functions a new class should define
# assumes that addition and multiplication are
# commutative
# =================================================
def is_zero(self):
return False

def _any_as_self(self):
return NotImplemented

def _neg_(self):
return NotImplemented

def _add_(self, other):
return NotImplemented

def _sub_(self, other):
return NotImplemented

def _rsub_(self, other):
return NotImplemented

def _mul_(self, other):
return NotImplemented

def _div_(self, other):
return NotImplemented

def _rdiv_(self, other):
return NotImplemented

def _floordiv_(self, other):
return NotImplemented

def _rfloordiv_(self, other):
return NotImplemented

def _invert_(self):
return NotImplemented

# =================================================
# Generic arithmetic using the above functions
# =================================================

def __pos__(self):
return self

def __neg__(self):
return self._neg_()

def __add__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._add_(other)

def __radd__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._add_(other)

def __sub__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._sub_(other)

def __rsub__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._rsub_(other)

def __mul__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._mul_(other)

def __rmul__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._mul_(other)

def __truediv__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented

if other.is_zero():
raise ZeroDivisionError

return self._div_(other)

def __rtruediv__(self, other):
if self.is_zero():
raise ZeroDivisionError

other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._rdiv_(other)

def __floordiv__(self, other):
other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented

if other.is_zero():
raise ZeroDivisionError

return self._floordiv_(other)

def __rfloordiv__(self, other):
if self.is_zero():
raise ZeroDivisionError

other = self._any_as_self(other)
if other is NotImplemented:
return NotImplemented
return self._rfloordiv_(other)

def __invert__(self):
if self.is_zero():
raise ZeroDivisionError
return self._invert_()



cdef class flint_poly(flint_elem):
Expand All @@ -55,7 +184,7 @@ cdef class flint_poly(flint_elem):
"""
return list(self)

def str(self, bint ascending=False, *args, **kwargs):
def str(self, bint ascending=False, var="x", *args, **kwargs):
"""
Convert to a human-readable string (generic implementation for
all polynomial types).
Expand All @@ -80,20 +209,20 @@ cdef class flint_poly(flint_elem):
s.append("%s" % c)
elif i == 1:
if c == "1":
s.append("x")
s.append(var)
else:
s.append("%s*x" % c)
s.append(f"{c}*{var}")
else:
if c == "1":
s.append("x^%s" % i)
s.append(f"{var}^{i}")
else:
s.append("%s*x^%s" % (c, i))
s.append(f"{c}*{var}^{i}")
return " + ".join(s)

def roots(self):
"""
Computes all the roots in the base ring of the polynomial.
Returns a list of all pairs (*v*, *m*) where *v* is the
Returns a list of all pairs (*v*, *m*) where *v* is the
integer root and *m* is the multiplicity of the root.

To compute complex roots of a polynomial, instead use
Expand Down
109 changes: 109 additions & 0 deletions src/flint/flintlib/fq.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from flint.flintlib.flint cimport flint_bitcnt_t, fmpz_struct, slong, flint_rand_t, ulong
from flint.flintlib.fmpz cimport fmpz_t, fmpz_struct
from flint.flintlib.fmpz_mod cimport fmpz_mod_ctx_t
from flint.flintlib.fmpz_mod_mat cimport fmpz_mod_mat_t
from flint.flintlib.fmpz_poly cimport fmpz_poly_t, fmpz_poly_struct
from flint.flintlib.fmpz_mod_poly cimport fmpz_mod_poly_t, fmpz_mod_poly_struct

cdef extern from "flint/fq.h":

ctypedef fmpz_poly_t fq_t
ctypedef fmpz_poly_struct fq_struct

ctypedef struct fq_ctx_struct:
fmpz_mod_ctx_t ctxp

int sparse_modulus
int is_conway # whether field was initialized with the Flint Conway tables (assures primitivity)

fmpz_struct * a
slong * j
slong len

fmpz_mod_poly_t modulus
fmpz_mod_poly_t inv

char * var

ctypedef fq_ctx_struct fq_ctx_t[1]

void fq_ctx_init(fq_ctx_t ctx, const fmpz_t p, slong d, const char *var)
int _fq_ctx_init_conway(fq_ctx_t ctx, const fmpz_t p, slong d, const char *var)
void fq_ctx_init_conway(fq_ctx_t ctx, const fmpz_t p, slong d, const char *var)
void fq_ctx_init_modulus(fq_ctx_t ctx, const fmpz_mod_poly_t modulus, const fmpz_mod_ctx_t ctxp, const char *var)
void fq_ctx_clear(fq_ctx_t ctx)
const fmpz_mod_poly_struct* fq_ctx_modulus(const fq_ctx_t ctx)
long fq_ctx_degree(const fq_ctx_t ctx)
fmpz_struct * fq_ctx_prime(const fq_ctx_t ctx)
void fq_ctx_order(fmpz_t f, const fq_ctx_t ctx)
# int fq_ctx_fprint(FILE * file, const fq_ctx_t ctx)
void fq_ctx_print(const fq_ctx_t ctx)
void fq_ctx_randtest(fq_ctx_t ctx)
void fq_ctx_randtest_reducible(fq_ctx_t ctx)
void fq_init(fq_t rop, const fq_ctx_t ctx)
void fq_init2(fq_t rop, const fq_ctx_t ctx)
void fq_clear(fq_t rop, const fq_ctx_t ctx)
void _fq_sparse_reduce(fmpz_struct *R, slong lenR, const fq_ctx_t ctx)
void _fq_dense_reduce(fmpz_struct *R, slong lenR, const fq_ctx_t ctx)
void _fq_reduce(fmpz_struct *r, slong lenR, const fq_ctx_t ctx)
void fq_reduce(fq_t rop, const fq_ctx_t ctx)
void fq_add(fq_t rop, const fq_t op1, const fq_t op2, const fq_ctx_t ctx)
void fq_sub(fq_t rop, const fq_t op1, const fq_t op2, const fq_ctx_t ctx)
void fq_sub_one(fq_t rop, const fq_t op1, const fq_ctx_t ctx)
void fq_neg(fq_t rop, const fq_t op, const fq_ctx_t ctx)
void fq_mul(fq_t rop, const fq_t op1, const fq_t op2, const fq_ctx_t ctx)
void fq_mul_fmpz(fq_t rop, const fq_t op, const fmpz_t x, const fq_ctx_t ctx)
void fq_mul_si(fq_t rop, const fq_t op, slong x, const fq_ctx_t ctx)
void fq_mul_ui(fq_t rop, const fq_t op, ulong x, const fq_ctx_t ctx)
void fq_sqr(fq_t rop, const fq_t op, const fq_ctx_t ctx)
void fq_div(fq_t rop, const fq_t op1, const fq_t op2, const fq_ctx_t ctx)
void _fq_inv(fmpz_struct *rop, const fmpz_struct *op, slong len, const fq_ctx_t ctx)
void fq_inv(fq_t rop, const fq_t op, const fq_ctx_t ctx)
void fq_gcdinv(fq_t f, fq_t inv, const fq_t op, const fq_ctx_t ctx)
void _fq_pow(fmpz_struct *rop, const fmpz_struct *op, slong len, const fmpz_t e, const fq_ctx_t ctx)
void fq_pow(fq_t rop, const fq_t op, const fmpz_t e, const fq_ctx_t ctx)
void fq_pow_ui(fq_t rop, const fq_t op, const ulong e, const fq_ctx_t ctx)
int fq_sqrt(fq_t rop, const fq_t op1, const fq_ctx_t ctx)
void fq_pth_root(fq_t rop, const fq_t op1, const fq_ctx_t ctx)
int fq_is_square(const fq_t op, const fq_ctx_t ctx)
# int fq_fprint_pretty(FILE *file, const fq_t op, const fq_ctx_t ctx)
int fq_print_pretty(const fq_t op, const fq_ctx_t ctx)
# void fq_fprint(FILE * file, const fq_t op, const fq_ctx_t ctx)
void fq_print(const fq_t op, const fq_ctx_t ctx)
char * fq_get_str(const fq_t op, const fq_ctx_t ctx)
char * fq_get_str_pretty(const fq_t op, const fq_ctx_t ctx)
void fq_randtest(fq_t rop, flint_rand_t state, const fq_ctx_t ctx)
void fq_randtest_not_zero(fq_t rop, flint_rand_t state, const fq_ctx_t ctx)
void fq_randtest_dense(fq_t rop, flint_rand_t state, const fq_ctx_t ctx)
void fq_rand(fq_t rop, flint_rand_t state, const fq_ctx_t ctx)
void fq_rand_not_zero(fq_t rop, flint_rand_t state, const fq_ctx_t ctx)
void fq_set(fq_t rop, const fq_t op, const fq_ctx_t ctx)
void fq_set_si(fq_t rop, const slong x, const fq_ctx_t ctx)
void fq_set_ui(fq_t rop, const ulong x, const fq_ctx_t ctx)
void fq_set_fmpz(fq_t rop, const fmpz_t x, const fq_ctx_t ctx)
void fq_swap(fq_t op1, fq_t op2, const fq_ctx_t ctx)
void fq_zero(fq_t rop, const fq_ctx_t ctx)
void fq_one(fq_t rop, const fq_ctx_t ctx)
void fq_gen(fq_t rop, const fq_ctx_t ctx)
int fq_get_fmpz(fmpz_t rop, const fq_t op, const fq_ctx_t ctx)
void fq_get_fmpz_poly(fmpz_poly_t a, const fq_t b, const fq_ctx_t ctx)
void fq_get_fmpz_mod_poly(fmpz_mod_poly_t a, const fq_t b, const fq_ctx_t ctx)
void fq_set_fmpz_poly(fq_t a, const fmpz_poly_t b, const fq_ctx_t ctx)
void fq_set_fmpz_mod_poly(fq_t a, const fmpz_mod_poly_t b, const fq_ctx_t ctx)
void fq_get_fmpz_mod_mat(fmpz_mod_mat_t col, const fq_t a, const fq_ctx_t ctx)
void fq_set_fmpz_mod_mat(fq_t a, const fmpz_mod_mat_t col, const fq_ctx_t ctx)
int fq_is_zero(const fq_t op, const fq_ctx_t ctx)
int fq_is_one(const fq_t op, const fq_ctx_t ctx)
int fq_equal(const fq_t op1, const fq_t op2, const fq_ctx_t ctx)
int fq_is_invertible(const fq_t op, const fq_ctx_t ctx)
int fq_is_invertible_f(fq_t f, const fq_t op, const fq_ctx_t ctx)
void _fq_trace(fmpz_t rop, const fmpz_struct *op, slong len, const fq_ctx_t ctx)
void fq_trace(fmpz_t rop, const fq_t op, const fq_ctx_t ctx)
void _fq_norm(fmpz_t rop, const fmpz_struct *op, slong len, const fq_ctx_t ctx)
void fq_norm(fmpz_t rop, const fq_t op, const fq_ctx_t ctx)
void _fq_frobenius(fmpz_struct *rop, const fmpz_struct *op, slong len, slong e, const fq_ctx_t ctx)
void fq_frobenius(fq_t rop, const fq_t op, slong e, const fq_ctx_t ctx)
int fq_multiplicative_order(fmpz_t ord, const fq_t op, const fq_ctx_t ctx)
int fq_is_primitive(const fq_t op, const fq_ctx_t ctx)
void fq_bit_pack(fmpz_t f, const fq_t op, flint_bitcnt_t bit_size, const fq_ctx_t ctx)
void fq_bit_unpack(fq_t rop, const fmpz_t f, flint_bitcnt_t bit_size, const fq_ctx_t ctx)
Loading
Loading