Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix for FFs implem using flint fq_nmod module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Pierre Flori committed Aug 22, 2014
1 parent de8d00f commit b4fa9ea
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/sage/libs/flint/fmpz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cdef extern from "flint/fmpz.h":

void fmpz_set_ui(fmpz_t res, unsigned long x)
void fmpz_set_si(fmpz_t res, long x)
unsigned long fmpz_get_ui(fmpz_t res)
long fmpz_get_si(fmpz_t res)

void fmpz_clear(fmpz_t f)
void fmpz_print(fmpz_t f)
Expand Down
65 changes: 65 additions & 0 deletions src/sage/rings/finite_rings/element_flint_fq.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,71 @@ cdef class FiniteFieldElement_flint_fq(FinitePolyExtElement):
#def sqrt(FiniteFieldElement_flint_fq self, extend=False, all=False):
#def multiplicative_order(FiniteFieldElement_flint_fq self):

def norm(self):
"""
Return the norm of self down to the prime subfield.
This is the product of the Galois conjugates of self.
EXAMPLES::
sage: S.<b> = GF(5^2, impl="flint_fq"); S
Finite Field in b of size 5^2
sage: b.norm()
2
Next we consider a cubic extension::
sage: S.<a> = GF(5^3, impl="flint_fq"); S
Finite Field in a of size 5^3
sage: a.norm()
2
sage: a * a^5 * (a^25)
2
"""
cdef fmpz_t t
cdef mpz_t tgmp
cdef Integer tint
fmpz_init(t)
tint = Integer.__new__(Integer)
fq_norm(t, self.val, self._cparent)
flint_mpz_init_set_readonly(tgmp, t)
tint.set_from_mpz(tgmp)
flint_mpz_clear_readonly(tgmp)
fmpz_clear(t)
return self.parent().prime_subfield()(tint)

def trace(self):
"""
Return the trace of this element, which is the sum of the
Galois conjugates.
EXAMPLES::
sage: S.<a> = GF(5^3, impl="flint_fq"); S
Finite Field in a of size 5^3
sage: a.trace()
0
sage: a + a^5 + a^25
0
sage: z = a^2 + a + 1
sage: z.trace()
2
sage: z + z^5 + z^25
2
"""
cdef fmpz_t t
cdef mpz_t tgmp
cdef Integer tint
fmpz_init(t)
tint = Integer.__new__(Integer)
fq_trace(t, self.val, self._cparent)
flint_mpz_init_set_readonly(tgmp, t)
tint.set_from_mpz(tgmp)
flint_mpz_clear_readonly(tgmp)
fmpz_clear(t)
return self.parent().prime_subfield()(tint)

# JPF: the following should definitely go into element_base.pyx.
def log(FiniteFieldElement_flint_fq self, FiniteFieldElement_flint_fq base):
"""
Expand Down
67 changes: 61 additions & 6 deletions src/sage/rings/finite_rings/element_flint_fq_nmod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,11 @@ cdef class FiniteFieldElement_flint_fq_nmod(FinitePolyExtElement):
if n == 0:
fq_nmod_zero(self.val, self._cparent)
else:
fmpz_poly_zero(self.val)
p = mpz_get_ui((<Integer>self._parent.characteristic()).value)
nmod_poly_zero(<nmod_poly_struct*>self.val)
for i in xrange(n):
x_INT = Integer(x[i])
fmpz_init_set_readonly(x_flint, <void*>x_INT + mpz_t_offset)
fmpz_poly_set_coeff_fmpz(self.val, i, x_flint)
fmpz_clear_readonly(x_flint)
fq_nmod_reduce(self.val, self._cparent)
x_ui = mpz_fdiv_ui(Integer(x[i]).value, p)
nmod_poly_set_coeff_ui(<nmod_poly_struct*>self.val, i, x_ui)

elif isinstance(x, Rational):
self.construct_from(x % self._parent.characteristic())
Expand Down Expand Up @@ -661,6 +659,63 @@ cdef class FiniteFieldElement_flint_fq_nmod(FinitePolyExtElement):
#def sqrt(FiniteFieldElement_flint_fq_nmod self, extend=False, all=False):
#def multiplicative_order(FiniteFieldElement_flint_fq_nmod self):

def norm(self):
"""
Return the norm of self down to the prime subfield.
This is the product of the Galois conjugates of self.
EXAMPLES::
sage: S.<b> = GF(5^2, impl="flint_fq_nmod"); S
Finite Field in b of size 5^2
sage: b.norm()
2
Next we consider a cubic extension::
sage: S.<a> = GF(5^3, impl="flint_fq_nmod"); S
Finite Field in a of size 5^3
sage: a.norm()
2
sage: a * a^5 * (a^25)
2
"""
cdef fmpz_t t
cdef unsigned long tulong
fmpz_init(t)
fq_nmod_norm(t, self.val, self._cparent)
tulong = fmpz_get_ui(t)
fmpz_clear(t)
return self.parent().prime_subfield()(tulong)

def trace(self):
"""
Return the trace of this element, which is the sum of the
Galois conjugates.
EXAMPLES::
sage: S.<a> = GF(5^3, impl="flint_fq_nmod"); S
Finite Field in a of size 5^3
sage: a.trace()
0
sage: a + a^5 + a^25
0
sage: z = a^2 + a + 1
sage: z.trace()
2
sage: z + z^5 + z^25
2
"""
cdef fmpz_t t
cdef unsigned long tulong
fmpz_init(t)
fq_nmod_trace(t, self.val, self._cparent)
tulong = fmpz_get_ui(t)
fmpz_clear(t)
return self.parent().prime_subfield()(tulong)

# JPF: the following should definitely go into element_base.pyx.
def log(FiniteFieldElement_flint_fq_nmod self, FiniteFieldElement_flint_fq_nmod base):
"""
Expand Down

0 comments on commit b4fa9ea

Please sign in to comment.