@@ -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)
0 commit comments