Skip to content

Commit d222395

Browse files
authored
Add flatten_variable (#238)
1 parent a58b33f commit d222395

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/gcd.jl

+16-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,11 @@ function _extracted_variable(p1, p2)
342342
end
343343

344344
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
347350
end
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())
380383
end
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

Comments
 (0)