Skip to content

Commit

Permalink
start on type conversions and comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 7, 2024
1 parent 1307116 commit 6bf96a7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/flint/flintlib/fq_default.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ cdef extern from "flint/fq_default.h":
void fq_default_get_fmpz_mod_poly(fmpz_mod_poly_t poly, const fq_default_t op, const fq_default_ctx_t ctx)
void fq_default_set_fmpz_mod_poly(fq_default_t op, const fmpz_mod_poly_t poly, const fq_default_ctx_t ctx)
void fq_default_get_fmpz_poly(fmpz_poly_t a, const fq_default_t b, const fq_default_ctx_t ctx)
void fq_default_set_fmpz_poly(fq_default_struct a, const fmpz_poly_t b, const fq_default_ctx_t ctx)
void fq_default_set_fmpz_poly(fq_default_t a, const fmpz_poly_t b, const fq_default_ctx_t ctx)
int fq_default_is_zero(const fq_default_t op, const fq_default_ctx_t ctx)
int fq_default_is_one(const fq_default_t op, const fq_default_ctx_t ctx)
int fq_default_equal(const fq_default_t op1, const fq_default_t op2, const fq_default_ctx_t ctx)
Expand Down
4 changes: 3 additions & 1 deletion src/flint/types/fq_default.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ cdef class fq_default_ctx:
cdef bint _initialized

cdef new_ctype_fq_default(self)
cdef set_list_as_fq_default(self, fq_default_t val, obj)
cdef set_any_as_fq_default(self, fq_default_t val, obj)

cdef any_as_fq_default(self, obj)

@staticmethod
cdef fq_default_ctx c_from_order(fmpz p, int d, char *var, fq_default_type type=*)

Expand Down
104 changes: 98 additions & 6 deletions src/flint/types/fq_default.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from flint.pyflint cimport global_random_state
from flint.types.fmpz cimport fmpz, any_as_fmpz
from flint.types.fmpz_poly cimport fmpz_poly
from flint.types.fmpz_poly cimport fmpz_poly, any_as_fmpz_poly, fmpz_poly_set_list
from flint.types.fmpz_mod_poly cimport fmpz_mod_poly, fmpz_mod_poly_ctx
from flint.types.nmod_poly cimport nmod_poly
from flint.utils.typecheck cimport typecheck

"""
TODO:
- be able to call from __init__
- allow the variable name to be None if the degree is 1
- allow the variable name to be as long as we want
- print the context differently for degree = 1
- decide on __repr__ of elements
"""

cdef class fq_default_ctx:
r"""
Context object for creating :class:`~.fq_default`.
Expand Down Expand Up @@ -84,6 +94,10 @@ cdef class fq_default_ctx:
# TODO: we should allow var to be None when d == 1
if isinstance(var, str):
var = var.encode()

# TODO: Flint only wants one-character inputs
if len(var) > 1:
raise ValueError

return fq_default_ctx.c_from_order(order, d, var, type)

Expand Down Expand Up @@ -243,11 +257,55 @@ cdef class fq_default_ctx:
cdef new_ctype_fq_default(self):
return fq_default.__new__(fq_default, None, self)

cdef set_any_as_fq_default(self, fq_default_t val, obj):
if typecheck(obj, fmpz):
fq_default_set_fmpz(val, (<fmpz>obj).val, self.val)
cdef set_list_as_fq_default(self, fq_default_t fq_ele, obj):
cdef fmpz_poly poly
poly = fmpz_poly.__new__(fmpz_poly)
fmpz_poly_set_list(poly.val, obj)

# Now set the value from the fmpz_poly
fq_default_set_fmpz_poly(fq_ele, poly.val, self.val)

return 0

cdef set_any_as_fq_default(self, fq_default_t fq_ele, obj):
# Converts the list to an fmpz_poly and then sets from this
if typecheck(obj, list):
return self.set_list_as_fq_default(fq_ele, obj)

# Assumes that the modulus of the polynomial matches
# the context for the fq_default
if typecheck(obj, fmpz_mod_poly):
fq_default_set_fmpz_mod_poly(fq_ele, (<fmpz_mod_poly>obj).val, self.val)
return 0
return NotImplemented

# Assumes that the modulus of the polynomial matches
# the context for the fq_default
if typecheck(obj, nmod_poly):
fq_default_set_nmod_poly(fq_ele, (<nmod_poly>obj).val, self.val)
return 0

# If the input is not fmpz_mod_poly or nmod_poly or a list, we cast the
# input to an fmpz_poly and then set from this
poly = any_as_fmpz_poly(obj)
if poly is NotImplemented:
return NotImplemented

fq_default_set_fmpz_poly(fq_ele, (<fmpz_poly>poly).val, self.val)
return 0

cdef any_as_fq_default(self, obj):
# convert from fq_default
if typecheck(obj, fq_default):
if self != (<fq_default>obj).ctx:
raise ValueError("contexts dont match")
return obj

cdef fq_default res
res = self.new_ctype_fq_default()
check = self.set_any_as_fq_default(res.val, obj)
if check is NotImplemented:
return NotImplemented
return res

def __eq__(self, other):
"""
Expand Down Expand Up @@ -334,7 +392,7 @@ cdef class fq_default(flint_scalar):
return pol

def str(self):
return self.polynomial().str(var=self.ctx.var)
return self.polynomial().str(var=self.ctx.var.decode())

def __repr__(self):
# TODO: what do we want here?
Expand All @@ -349,6 +407,24 @@ cdef class fq_default(flint_scalar):
def is_one(self):
return 1 == fq_default_is_zero(self.val, self.ctx.val)

def __richcmp__(self, other, int op):
cdef bint res
if op != 2 and op != 3:
raise TypeError("fq_default cannot be ordered")

if not typecheck(other, fq_default):
other = self.ctx.any_as_fq_default(other)

if typecheck(other, fq_default):
res = (self.ctx == (<fq_default>other).ctx) and \
fq_default_equal(self.val, (<fq_default>other).val, self.ctx.val)
if op == 2:
return res
else:
return not res
else:
return NotImplemented

# =================================================
# Generic arithmetic required by flint_scalar
# =================================================
Expand All @@ -374,6 +450,8 @@ cdef class fq_default(flint_scalar):
return NotImplemented

def _invert_(self):
"""
"""
cdef fq_default res
res = self.ctx.new_ctype_fq_default()
fq_default_inv(res.val, self.val, self.ctx.val)
Expand All @@ -384,12 +462,16 @@ cdef class fq_default(flint_scalar):
# =================================================

def square(self):
"""
"""
cdef fq_default res
res = self.ctx.new_ctype_fq_default()
fq_default_sqr(res.val, self.val, self.ctx.val)
return res

def __pow__(self, e):
"""
"""
return NotImplemented

def sqrt(self):
Expand All @@ -401,9 +483,13 @@ cdef class fq_default(flint_scalar):
raise ValueError("element is not a square")

def is_square(self):
"""
"""
return 1 == fq_default_is_square(self.val, self.ctx.val)

def pth_root(self):
"""
"""
cdef fq_default res
res = self.ctx.new_ctype_fq_default()
fq_default_pth_root(res.val, self.val, self.ctx.val)
Expand All @@ -414,10 +500,16 @@ cdef class fq_default(flint_scalar):
# =================================================

def trace(self):
"""
"""
return NotImplemented

def norm(self):
"""
"""
return NotImplemented

def frobenius(self):
"""
"""
return NotImplemented

0 comments on commit 6bf96a7

Please sign in to comment.