Skip to content

Commit c8782c5

Browse files
committed
Simplify subs method, allow generator index
1 parent fb10785 commit c8782c5

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/flint/types/fmpq_mpoly.pyx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ cdef class fmpq_mpoly(flint_mpoly):
691691

692692
def subs(self, dict_args) -> fmpq_mpoly:
693693
"""
694-
Partial evaluate this polynomial with select constants. Keys must be generator names,
694+
Partial evaluate this polynomial with select constants. Keys must be generator names or generator indices,
695695
all values must be fmpq.
696696

697697
>>> from flint import Ordering
@@ -703,26 +703,18 @@ cdef class fmpq_mpoly(flint_mpoly):
703703
"""
704704
cdef:
705705
fmpq_mpoly res
706-
slong i, nargs
707-
708-
args = tuple((i, dict_args[x]) for i, x in enumerate(self.ctx.names()) if x in dict_args)
709-
nargs = len(args)
710-
711-
# If we've been provided with an invalid generator name then the length of our filter
712-
# args will be less than what we've been provided with.
713-
if nargs < len(dict_args):
714-
raise ValueError("unknown generator name provided")
706+
slong i
715707

716-
args_fmpq = tuple(any_as_fmpq(v) for _, v in args)
717-
for arg in args_fmpq:
718-
if arg is NotImplemented:
719-
raise TypeError(f"cannot coerce argument ('{arg}') to fmpq")
708+
args = tuple((self.ctx.variable_to_index(k), any_as_fmpq(v)) for k, v in dict_args.items())
709+
for _, v in args:
710+
if v is NotImplemented:
711+
raise TypeError(f"cannot coerce argument ('{v}') to fmpq")
720712

721713
# Partial application with args in Q. We evaluate the polynomial one variable at a time
722714
res = create_fmpq_mpoly(self.ctx)
723715

724716
fmpq_mpoly_set(res.val, self.val, self.ctx.val)
725-
for (i, _), arg in zip(args, args_fmpq):
717+
for i, arg in args:
726718
if fmpq_mpoly_evaluate_one_fmpq(res.val, res.val, i, (<fmpq>arg).val, self.ctx.val) == 0:
727719
raise ValueError("unreasonably large polynomial") # pragma: no cover
728720
return res

src/flint/types/fmpz_mpoly.pyx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ cdef class fmpz_mpoly(flint_mpoly):
673673

674674
def subs(self, dict_args) -> fmpz_mpoly:
675675
"""
676-
Partial evaluate this polynomial with select constants. Keys must be generator names,
676+
Partial evaluate this polynomial with select constants. Keys must be generator names or generator indices,
677677
all values must be fmpz.
678678

679679
>>> from flint import Ordering
@@ -685,26 +685,18 @@ cdef class fmpz_mpoly(flint_mpoly):
685685
"""
686686
cdef:
687687
fmpz_mpoly res
688-
slong i, nargs
689-
690-
args = tuple((i, dict_args[x]) for i, x in enumerate(self.ctx.names()) if x in dict_args)
691-
nargs = len(args)
692-
693-
# If we've been provided with an invalid keyword arg then the length of our filter
694-
# args will be less than what we've been provided with.
695-
if nargs < len(dict_args):
696-
raise ValueError("unknown generator name provided")
688+
slong i
697689

698-
args_fmpz = tuple(any_as_fmpz(v) for _, v in args)
699-
for arg in args_fmpz:
700-
if arg is NotImplemented:
701-
raise TypeError(f"cannot coerce argument ('{arg}') to fmpz")
690+
args = tuple((self.ctx.variable_to_index(k), any_as_fmpz(v)) for k, v in dict_args.items())
691+
for _, v in args:
692+
if v is NotImplemented:
693+
raise TypeError(f"cannot coerce argument ('{v}') to fmpz")
702694

703695
# Partial application with args in Z. We evaluate the polynomial one variable at a time
704696
res = create_fmpz_mpoly(self.ctx)
705697

706698
fmpz_mpoly_set(res.val, self.val, self.ctx.val)
707-
for (i, _), arg in zip(args, args_fmpz):
699+
for i, arg in args:
708700
if fmpz_mpoly_evaluate_one_fmpz(res.val, res.val, i, (<fmpz>arg).val, self.ctx.val) == 0:
709701
raise ValueError("unreasonably large polynomial") # pragma: no cover
710702
return res

0 commit comments

Comments
 (0)