Skip to content

Commit 10c19be

Browse files
committed
Add alias for keys/value to monoms/coeffs, add terms function
1 parent fbc6854 commit 10c19be

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/flint/flint_base/flint_base.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ cdef class flint_mpoly(flint_elem):
265265
def __pos__(self):
266266
return self
267267

268+
def keys(self):
269+
return self.monoms()
270+
271+
def values(self):
272+
return self.coeffs()
273+
268274
def items(self):
269275
"""
270276
Return the exponent vectors and coefficient of each term.

src/flint/test/test_all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,8 @@ def quick_poly():
27942794
assert p.keys() == [(2, 2), (1, 0), (0, 1), (0, 0)]
27952795
assert p.values() == [4, 3, 2, 1]
27962796
assert list(p.items()) == list(zip([(2, 2), (1, 0), (0, 1), (0, 0)], [4, 3, 2, 1]))
2797+
assert sum(ctx.from_dict({exp_vec: coeff}) for exp_vec, coeff in p.items()) == p
2798+
assert sum(p.terms()) == p
27972799

27982800
assert p.subs({"x1": S(0), "x0": S(0)}) == ctx.from_dict({(0, 0): 1})
27992801
assert p.compose(p.subs({"x1": 0}), ctx.from_dict({(0, 1): 1})) == mpoly({

src/flint/types/fmpq_mpoly.pyx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ cdef class fmpq_mpoly(flint_mpoly):
584584
raise ValueError("Unreasonably large polynomial") # pragma: no cover
585585
return vres
586586

587-
def keys(self):
587+
def monoms(self):
588588
"""
589589
Return the exponent vectors of each term as a tuple of fmpz.
590590
@@ -606,7 +606,7 @@ cdef class fmpq_mpoly(flint_mpoly):
606606

607607
return res
608608

609-
def values(self):
609+
def coeffs(self):
610610
"""
611611
Return the coefficients of each term as a fmpq.
612612
@@ -629,6 +629,29 @@ cdef class fmpq_mpoly(flint_mpoly):
629629

630630
return res
631631

632+
def terms(self):
633+
"""
634+
Return the terms of this polynomial as a list of fmpq_mpolys.
635+
636+
>>> from flint import Ordering
637+
>>> ctx = fmpq_mpoly_ctx.get_context(2, Ordering.lex, 'x')
638+
>>> f = ctx.from_dict({(0, 0): 1, (1, 0): 2, (0, 1): 3, (1, 1): 4})
639+
>>> f.terms()
640+
[4*x0*x1, 2*x0, 3*x1, 1]
641+
642+
"""
643+
cdef:
644+
fmpq_mpoly term
645+
slong i
646+
647+
res = []
648+
for i in range(len(self)):
649+
term = create_fmpq_mpoly(self.ctx)
650+
fmpq_mpoly_get_term(term.val, self.val, i, self.ctx.val)
651+
res.append(term)
652+
653+
return res
654+
632655
def subs(self, dict_args) -> fmpq_mpoly:
633656
"""
634657
Partial evaluate this polynomial.

src/flint/types/fmpz_mpoly.pyx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ cdef class fmpz_mpoly(flint_mpoly):
566566
raise ValueError("Unreasonably large polynomial") # pragma: no cover
567567
return vres
568568

569-
def keys(self):
569+
def monoms(self):
570570
"""
571571
Return the exponent vectors of each term as a tuple of fmpz.
572572
@@ -588,7 +588,7 @@ cdef class fmpz_mpoly(flint_mpoly):
588588

589589
return res
590590

591-
def values(self):
591+
def coeffs(self):
592592
"""
593593
Return the coefficients of each term as a fmpz
594594
@@ -611,6 +611,29 @@ cdef class fmpz_mpoly(flint_mpoly):
611611

612612
return res
613613

614+
def terms(self):
615+
"""
616+
Return the terms of this polynomial as a list of fmpz_mpolys.
617+
618+
>>> from flint import Ordering
619+
>>> ctx = fmpz_mpoly_ctx.get_context(2, Ordering.lex, 'x')
620+
>>> f = ctx.from_dict({(0, 0): 1, (1, 0): 2, (0, 1): 3, (1, 1): 4})
621+
>>> f.terms()
622+
[4*x0*x1, 2*x0, 3*x1, 1]
623+
624+
"""
625+
cdef:
626+
fmpz_mpoly term
627+
slong i
628+
629+
res = []
630+
for i in range(len(self)):
631+
term = create_fmpz_mpoly(self.ctx)
632+
fmpz_mpoly_get_term(term.val, self.val, i, self.ctx.val)
633+
res.append(term)
634+
635+
return res
636+
614637
def subs(self, dict_args) -> fmpz_mpoly:
615638
"""
616639
Partial evaluate this polynomial with select constants. All arguments must be fmpz.

0 commit comments

Comments
 (0)