Skip to content

Commit 1317bb0

Browse files
committed
Fix composition with no-generator polynomial
1 parent 128ae01 commit 1317bb0

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/flint/test/test_all.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,6 +2815,11 @@ def quick_poly():
28152815
assert raises(lambda: p.subs({"a": 1}), ValueError)
28162816
assert raises(lambda: p.subs({"x0": 0, "x1": 1, "x2": 2}), ValueError)
28172817

2818+
no_gens_ctx = C.get_context(0)
2819+
no_gens_p = P("2", no_gens_ctx)
2820+
assert no_gens_p.compose(ctx=ctx1).context() is ctx1
2821+
assert raises(lambda: no_gens_p.compose(), ValueError)
2822+
28182823
assert raises(lambda: p.compose(p, P(ctx=ctx1)), IncompatibleContextError)
28192824

28202825
assert bool(P(ctx=ctx)) is False

src/flint/types/fmpq_mpoly.pyx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ cdef class fmpq_mpoly(flint_mpoly):
649649
raise ValueError("unreasonably large polynomial") # pragma: no cover
650650
return res
651651

652-
def compose(self, *args) -> fmpq_mpoly:
652+
def compose(self, *args, ctx=None) -> fmpq_mpoly:
653653
"""
654654
Compose this polynomial with other fmpq_mpolys. All arguments must share the same context, it may different
655655
from this polynomials context.
@@ -677,12 +677,22 @@ cdef class fmpq_mpoly(flint_mpoly):
677677
raise ValueError("not enough arguments provided")
678678
elif nargs > nvars:
679679
raise ValueError("more arguments provided than variables")
680+
elif self.ctx.nvars() == 0 and ctx is None:
681+
raise ValueError("a context must be provided when composing a polynomial with no generators")
680682
elif not all(typecheck(arg, fmpq_mpoly) for arg in args):
681683
raise TypeError("all arguments must be fmpq_mpolys")
682684

683-
res_ctx = (<fmpq_mpoly> args[0]).ctx
684-
if not all((<fmpq_mpoly> args[i]).ctx is res_ctx for i in range(1, len(args))):
685-
raise IncompatibleContextError("all arguments must share the same context")
685+
if ctx is None:
686+
res_ctx = (<fmpq_mpoly> args[0]).ctx
687+
elif typecheck(ctx, fmpq_mpoly_ctx):
688+
res_ctx = <fmpq_mpoly_ctx>ctx
689+
else:
690+
raise TypeError(f"provided context ({ctx}) is not a fmpq_mpoly_ctx")
691+
692+
if not all((<fmpq_mpoly> arg).ctx is res_ctx for arg in args):
693+
raise IncompatibleContextError(
694+
"all arguments must share the " + ("same" if ctx is not None else "provided") + " context"
695+
)
686696

687697
C = fmpq_mpoly_vec(args, res_ctx, double_indirect=True)
688698
res = create_fmpq_mpoly(res_ctx)

src/flint/types/fmpz_mpoly.pyx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ cdef class fmpz_mpoly(flint_mpoly):
631631
raise ValueError("unreasonably large polynomial") # pragma: no cover
632632
return res
633633

634-
def compose(self, *args) -> fmpz_mpoly:
634+
def compose(self, *args, ctx=None) -> fmpz_mpoly:
635635
"""
636636
Compose this polynomial with other fmpz_mpolys. All arguments must share the same context, it may different
637637
from this polynomials context.
@@ -659,12 +659,22 @@ cdef class fmpz_mpoly(flint_mpoly):
659659
raise ValueError("not enough arguments provided")
660660
elif nargs > nvars:
661661
raise ValueError("more arguments provided than variables")
662+
elif self.ctx.nvars() == 0 and ctx is None:
663+
raise ValueError("a context must be provided when composing a polynomial with no generators")
662664
elif not all(typecheck(arg, fmpz_mpoly) for arg in args):
663665
raise TypeError("all arguments must be fmpz_mpolys")
664666

665-
res_ctx = (<fmpz_mpoly> args[0]).ctx
666-
if not all((<fmpz_mpoly> args[i]).ctx is res_ctx for i in range(1, len(args))):
667-
raise IncompatibleContextError("all arguments must share the same context")
667+
if ctx is None:
668+
res_ctx = (<fmpz_mpoly> args[0]).ctx
669+
elif typecheck(ctx, fmpz_mpoly_ctx):
670+
res_ctx = <fmpz_mpoly_ctx>ctx
671+
else:
672+
raise TypeError(f"provided context ({ctx}) is not a fmpz_mpoly_ctx")
673+
674+
if not all((<fmpz_mpoly> arg).ctx is res_ctx for arg in args):
675+
raise IncompatibleContextError(
676+
"all arguments must share the " + ("same" if ctx is not None else "provided") + " context"
677+
)
668678

669679
C = fmpz_mpoly_vec(args, res_ctx, double_indirect=True)
670680
res = create_fmpz_mpoly(res_ctx)

0 commit comments

Comments
 (0)