1- export polynomial, polynomialtype, terms, nterms, coefficients, monomials
1+ export polynomial, polynomial!, polynomialtype, terms, nterms, coefficients, monomials
22export coefficienttype, monomialtype
33export mindegree, maxdegree, extdegree
44export leadingterm, leadingcoefficient, leadingmonomial
@@ -49,17 +49,32 @@ Calling `polynomial([2, 4, 1], [x, x^2*y, x*y])` should return ``4x^2y + xy + 2x
4949polynomial (p:: AbstractPolynomial ) = p
5050polynomial (p:: APL{T} , :: Type{T} ) where T = polynomial (terms (p))
5151polynomial (p:: APL{T} ) where T = polynomial (p, T)
52- polynomial (ts:: AbstractVector , s:: ListState = MessyState ()) = sum (ts)
53- polynomial (ts:: AbstractVector{<:AbstractTerm} , s:: SortedUniqState ) = polynomial (coefficient .(ts), monomial .(ts), s)
54- polynomial (a:: AbstractVector , x:: AbstractVector , s:: ListState = MessyState ()) = polynomial ([α * m for (α, m) in zip (a, x)], s)
55- polynomial (f:: Function , mv:: AbstractVector{<:AbstractMonomialLike} ) = polynomial ([f (i) * mv[i] for i in 1 : length (mv)])
5652function polynomial (Q:: AbstractMatrix , mv:: AbstractVector )
5753 LinearAlgebra. dot (mv, Q * mv)
5854end
5955function polynomial (Q:: AbstractMatrix , mv:: AbstractVector , :: Type{T} ) where T
6056 polynomial (polynomial (Q, mv), T)
6157end
6258
59+ polynomial (f:: Function , mv:: AbstractVector{<:AbstractMonomialLike} ) = polynomial! ([f (i) * mv[i] for i in 1 : length (mv)])
60+
61+ polynomial (a:: AbstractVector , x:: AbstractVector , s:: ListState = MessyState ()) = polynomial ([α * m for (α, m) in zip (a, x)], s)
62+
63+ polynomial (ts:: AbstractVector , s:: ListState = MessyState ()) = sum (ts)
64+
65+ polynomial! (ts:: AbstractVector{<:AbstractTerm} , s:: SortedUniqState ) = polynomial (coefficient .(ts), monomial .(ts), s)
66+
67+ function polynomial! (ts:: AbstractVector{<:AbstractTerm} , s:: SortedState )
68+ polynomial! (uniqterms! (ts), SortedUniqState ())
69+ end
70+ function polynomial! (ts:: AbstractVector{<:AbstractTerm} , s:: UnsortedState = MessyState ())
71+ polynomial! (sort! (ts, lt= (> )), sortstate (s))
72+ end
73+
74+ _collect (v:: Vector ) = v
75+ _collect (v:: AbstractVector ) = collect (v)
76+ polynomial (ts:: AbstractVector{<:AbstractTerm} , args:: Vararg{ListState, N} ) where {N} = polynomial! (MA. mutable_copy (_collect (ts)), args... )
77+
6378"""
6479 polynomialtype(p::AbstractPolynomialLike)
6580
@@ -85,25 +100,6 @@ polynomialtype(::Union{P, Type{P}}, ::Type{T}) where {P <: APL, T} = polynomialt
85100polynomialtype (:: Union{AbstractVector{PT}, Type{<:AbstractVector{PT}}} ) where PT <: APL = polynomialtype (PT)
86101polynomialtype (:: Union{AbstractVector{PT}, Type{<:AbstractVector{PT}}} , :: Type{T} ) where {PT <: APL , T} = polynomialtype (PT, T)
87102
88- function uniqterms (ts:: AbstractVector{T} ) where T <: AbstractTerm
89- result = T[]
90- sizehint! (result, length (ts))
91- for t in ts
92- if ! iszero (t)
93- if isempty (result) || monomial (t) != monomial (last (result))
94- push! (result, t)
95- else
96- coef = coefficient (last (result)) + coefficient (t)
97- if iszero (coef)
98- pop! (result)
99- else
100- result[end ] = coef * monomial (t)
101- end
102- end
103- end
104- end
105- result
106- end
107103function uniqterms! (ts:: AbstractVector{<: AbstractTerm} )
108104 i = firstindex (ts)
109105 for j in Iterators. drop (eachindex (ts), 1 )
@@ -126,8 +122,6 @@ function uniqterms!(ts::AbstractVector{<: AbstractTerm})
126122 end
127123 ts
128124end
129- polynomial (ts:: AbstractVector{<:AbstractTerm} , s:: SortedState ) = polynomial (uniqterms (ts), SortedUniqState ())
130- polynomial (ts:: AbstractVector{<:AbstractTerm} , s:: UnsortedState = MessyState ()) = polynomial (sort (ts, lt= (> )), sortstate (s))
131125
132126"""
133127 terms(p::AbstractPolynomialLike)
@@ -358,7 +352,7 @@ Returns `p / leadingcoefficient(p)` where the leading coefficient of the returne
358352"""
359353function monic (p:: APL )
360354 α = leadingcoefficient (p)
361- polynomial (_divtoone .(terms (p), α))
355+ polynomial! (_divtoone .(terms (p), α))
362356end
363357monic (m:: AbstractMonomialLike ) = m
364358monic (t:: AbstractTermLike{T} ) where T = one (T) * monomial (t)
@@ -386,14 +380,14 @@ function mapcoefficientsnz(f::Function, p::AbstractPolynomialLike)
386380 # Invariant: p has only nonzero coefficient
387381 # therefore f(α) will be nonzero for every coefficient α of p
388382 # hence we can use Uniq
389- polynomial (mapcoefficientsnz .(f, terms (p)), SortedUniqState ())
383+ polynomial! (mapcoefficientsnz .(f, terms (p)), SortedUniqState ())
390384end
391385mapcoefficientsnz (f:: Function , t:: AbstractTermLike ) = f (coefficient (t)) * monomial (t)
392386
393387Base. round (t:: AbstractTermLike ; args... ) = round (coefficient (t); args... ) * monomial (t)
394388function Base. round (p:: AbstractPolynomialLike ; args... )
395389 # round(0.1) is zero so we cannot use SortedUniqState
396- polynomial (round .(terms (p); args... ), SortedState ())
390+ polynomial! (round .(terms (p); args... ), SortedState ())
397391end
398392
399393Base. ndims (:: Type{<:AbstractPolynomialLike} ) = 0
0 commit comments