Skip to content

Commit 8b7f0c6

Browse files
committed
Add gr_ctx factor
1 parent 8ec4fbd commit 8b7f0c6

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/flint/types/_gr.pxd

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -767,17 +767,37 @@ cdef class gr_ctx(flint_ctx):
767767
###
768768
# Factorization
769769

770-
# @cython.final
771-
# cdef inline tuple[gr, list[gr], list[fmpz]] _factor(self, gr x):
772-
# cdef int err
773-
# cdef gr c = self.new_gr()
774-
# cdef gr_vec_t factors
775-
# gr_vec_init(factors, 0, self.ctx_t)
776-
# cdef gr_vec_t exponents
777-
# gr_vec_init(exponents, 0, self.ctx_t)
778-
# err = gr_factor(c.pval, factors, exponents, x.pval, 0, self.ctx_t)
779-
# if err != GR_SUCCESS:
780-
# raise self._error(err, "Cannot factorize x in this context")
770+
@cython.final
771+
cdef inline tuple[gr, list[tuple[gr, int]]] _factor(self, gr x):
772+
cdef int err, i
773+
cdef slong length, exp_s
774+
cdef gr c, f
775+
cdef gr_ptr fac_i, exp_i
776+
cdef gr_vec_t factors, exponents
777+
cdef int flags = 0 # XXX: What is flags?
778+
c = self.new_gr()
779+
gr_vec_init(factors, 0, self.ctx_t)
780+
gr_vec_init(exponents, 0, gr_fmpz_ctx_c.ctx_t)
781+
err = gr_factor(c.pval, factors, exponents, x.pval, flags, self.ctx_t)
782+
if err != GR_SUCCESS:
783+
raise self._error(err, "Failed to factor gr object")
784+
length = gr_vec_length(factors, self.ctx_t)
785+
py_factors = [None] * length
786+
for 0 <= i < length:
787+
f = self.new_gr()
788+
fac_i = gr_vec_entry_ptr(factors, i, self.ctx_t)
789+
err = gr_set(f.pval, fac_i, self.ctx_t)
790+
if err != GR_SUCCESS:
791+
raise self._error(err, "Failed to copy factor.")
792+
exp_i = gr_vec_entry_ptr(exponents, i, gr_fmpz_ctx_c.ctx_t)
793+
err = gr_get_si(&exp_s, exp_i, gr_fmpz_ctx_c.ctx_t)
794+
if err != GR_SUCCESS:
795+
raise self._error(err, "Failed to get integer value of exponent.")
796+
exp = exp_s
797+
py_factors[i] = (f, exp)
798+
gr_vec_clear(factors, self.ctx_t)
799+
gr_vec_clear(exponents, gr_fmpz_ctx_c.ctx_t)
800+
return c, py_factors
781801

782802
###
783803
# Fractions

src/flint/types/_gr.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,14 @@ cdef class gr_ctx(flint_ctx):
642642
###
643643
# Factorization
644644

645-
# def factor(self, x) -> tuple[gr, list[gr], list[gr]]:
646-
# """
647-
# Given an element of the context, this returns a factorization (c, f, e):
648-
# x = c f₁^e₁ ··· fₙ^eₙ, where fₖ will be irreducible or prime depending on the ring.
649-
# The prefactor c stores a unit, sign or coefficient.
650-
# Note that c is an element of the same ring as x.
651-
# """
652-
# return self._factor(self(x))
645+
def factor(self, x) -> tuple[gr, list[tuple[gr, int]]]:
646+
"""
647+
Given an element of the context, this returns a factorization (c, f, e):
648+
x = c f₁^e₁ ··· fₙ^eₙ, where fₖ will be irreducible or prime depending on the ring.
649+
The prefactor c stores a unit, sign or coefficient.
650+
Note that c is an element of the same ring as x.
651+
"""
652+
return self._factor(self(x))
653653
654654
###
655655
# Fractions

0 commit comments

Comments
 (0)