@@ -129,6 +129,10 @@ function Base.gcd(p1::APL{T}, p2::APL{S}, algo::AbstractUnivariateGCDAlgorithm=G
129
129
return inflate (g, shift, defl):: MA.promote_operation (gcd, typeof (p1), typeof (p2))
130
130
end
131
131
132
+ function Base. gcd (t1:: AbstractTermLike{T} , t2:: AbstractTermLike{S} , algo:: AbstractUnivariateGCDAlgorithm = GeneralizedEuclideanAlgorithm ()) where {T, S}
133
+ return term (_coefficient_gcd (coefficient (t1), coefficient (t2)), gcd (monomial (t1), monomial (t2)))
134
+ end
135
+
132
136
# Inspired from to `AbstractAlgebra.deflation`
133
137
function deflation (p:: AbstractPolynomialLike )
134
138
if iszero (p)
@@ -508,6 +512,18 @@ _simplifier(a, b, algo) = _gcd(a, b, algo)
508
512
# which makes the size of the `BigInt`s grow significantly which slows things down.
509
513
_simplifier (a:: Rational , b:: Rational , algo) = gcd (a. num, b. num) // gcd (a. den, b. den)
510
514
515
+ # Largely inspired from from `YingboMa/SIMDPolynomials.jl`.
516
+ function termwise_content (p:: APL )
517
+ ts = terms (p)
518
+ length (ts) == 1 && return first (ts)
519
+ g = gcd (ts[1 ], ts[2 ])
520
+ isone (g) || for i in 3 : length (ts)
521
+ g = gcd (g, ts[i])
522
+ isone (g) && break
523
+ end
524
+ return g
525
+ end
526
+
511
527
"""
512
528
content(poly::AbstractPolynomialLike{T}, algo::AbstractUnivariateGCDAlgorithm) where {T}
513
529
@@ -524,11 +540,28 @@ function content(poly::APL{T}, algo::AbstractUnivariateGCDAlgorithm) where {T}
524
540
P = MA. promote_operation (gcd, T, T)
525
541
# This is tricky to infer a `content` calls `gcd` which calls `content`, etc...
526
542
# To help Julia break the loop, we annotate the result here.
527
- return reduce (
528
- (a, b) -> _simplifier (a, b, algo),
529
- coefficients (poly),
530
- init = zero (P),
531
- ):: P
543
+ coefs = coefficients (poly)
544
+ length (coefs) == 0 && return zero (T)
545
+ length (coefs) == 1 && return first (coefs)
546
+ # Largely inspired from from `YingboMa/SIMDPolynomials.jl`.
547
+ if T <: APL
548
+ for i in eachindex (coefs)
549
+ if nterms (coefs[i]) == 1
550
+ g = gcd (termwise_content (coefs[1 ]), termwise_content (coefs[2 ]))
551
+ isone (g) || for i in 3 : length (coefs)
552
+ g = gcd (g, termwise_content (coefs[i]))
553
+ isone (g) && break
554
+ end
555
+ return convert (P, g)
556
+ end
557
+ end
558
+ end
559
+ g = gcd (coefs[1 ], coefs[2 ]):: P
560
+ isone (g) || for i in 3 : length (coefs)
561
+ g = _simplifier (g, coefs[i], algo):: P
562
+ isone (g) && break
563
+ end
564
+ return g:: P
532
565
end
533
566
function content (poly:: APL{T} , algo:: AbstractUnivariateGCDAlgorithm ) where {T<: AbstractFloat }
534
567
return one (T)
0 commit comments