Skip to content

Commit

Permalink
Merge pull request #97 from GiacomoPope/add_fq
Browse files Browse the repository at this point in the history
Include the `fq_default` type
  • Loading branch information
oscarbenjamin authored Aug 12, 2024
2 parents c4847e7 + f789f22 commit 09af886
Show file tree
Hide file tree
Showing 14 changed files with 1,659 additions and 7 deletions.
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

0 comments on commit 09af886

Please sign in to comment.