@@ -342,8 +342,11 @@ function _extracted_variable(p1, p2)
342342end
343343
344344function multivariate_gcd (p1:: APL , p2:: APL , var, algo, m1:: MA.MutableTrait , m2:: MA.MutableTrait )
345- q = univariate_gcd (isolate_variable (p1, var, m1), isolate_variable (p2, var, m2), algo, MA. IsMutable (), MA. IsMutable ())
346- return sum (coefficient (t) * monomial (t) for t in terms (q)):: MA.promote_operation (gcd, typeof (p1), typeof (p2))
345+ q1 = isolate_variable (p1, var, m1)
346+ q2 = isolate_variable (p2, var, m2)
347+ q = univariate_gcd (q1, q2, algo, MA. IsMutable (), MA. IsMutable ())
348+ P = MA. promote_operation (gcd, typeof (p1), typeof (p2))
349+ return flatten_variable! (termtype (P), q):: P
347350end
348351
349352_vector (t:: AbstractVector ) = collect (t)
@@ -379,6 +382,17 @@ function isolate_variable(poly::APL, var::AbstractVariable, mutability::MA.Mutab
379382 return polynomial! (new_terms, SortedUniqState ())
380383end
381384
385+ function flatten_variable! (:: Type{TT} , poly:: APL ) where {TT<: AbstractTerm }
386+ ts = TT[]
387+ for t in terms (poly)
388+ m = monomial (t)
389+ for _t in terms (coefficient (t))
390+ push! (ts, _t * m)
391+ end
392+ end
393+ return polynomial! (ts)
394+ end
395+
382396_polynomial (ts, state, :: MA.IsNotMutable ) = polynomial (ts, state)
383397_polynomial (ts, state, :: MA.IsMutable ) = polynomial! (ts, state)
384398
0 commit comments