@@ -342,8 +342,11 @@ function _extracted_variable(p1, p2)
342
342
end
343
343
344
344
function 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
347
350
end
348
351
349
352
_vector (t:: AbstractVector ) = collect (t)
@@ -379,6 +382,17 @@ function isolate_variable(poly::APL, var::AbstractVariable, mutability::MA.Mutab
379
382
return polynomial! (new_terms, SortedUniqState ())
380
383
end
381
384
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
+
382
396
_polynomial (ts, state, :: MA.IsNotMutable ) = polynomial (ts, state)
383
397
_polynomial (ts, state, :: MA.IsMutable ) = polynomial! (ts, state)
384
398
0 commit comments