1
- export polynomial, polynomialtype, terms, nterms, coefficients, monomials
1
+ export polynomial, polynomial!, polynomialtype, terms, nterms, coefficients, monomials
2
2
export coefficienttype, monomialtype
3
3
export mindegree, maxdegree, extdegree
4
4
export leadingterm, leadingcoefficient, leadingmonomial
@@ -49,17 +49,32 @@ Calling `polynomial([2, 4, 1], [x, x^2*y, x*y])` should return ``4x^2y + xy + 2x
49
49
polynomial (p:: AbstractPolynomial ) = p
50
50
polynomial (p:: APL{T} , :: Type{T} ) where T = polynomial (terms (p))
51
51
polynomial (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)])
56
52
function polynomial (Q:: AbstractMatrix , mv:: AbstractVector )
57
53
LinearAlgebra. dot (mv, Q * mv)
58
54
end
59
55
function polynomial (Q:: AbstractMatrix , mv:: AbstractVector , :: Type{T} ) where T
60
56
polynomial (polynomial (Q, mv), T)
61
57
end
62
58
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
+
63
78
"""
64
79
polynomialtype(p::AbstractPolynomialLike)
65
80
@@ -85,25 +100,6 @@ polynomialtype(::Union{P, Type{P}}, ::Type{T}) where {P <: APL, T} = polynomialt
85
100
polynomialtype (:: Union{AbstractVector{PT}, Type{<:AbstractVector{PT}}} ) where PT <: APL = polynomialtype (PT)
86
101
polynomialtype (:: Union{AbstractVector{PT}, Type{<:AbstractVector{PT}}} , :: Type{T} ) where {PT <: APL , T} = polynomialtype (PT, T)
87
102
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
107
103
function uniqterms! (ts:: AbstractVector{<: AbstractTerm} )
108
104
i = firstindex (ts)
109
105
for j in Iterators. drop (eachindex (ts), 1 )
@@ -126,8 +122,6 @@ function uniqterms!(ts::AbstractVector{<: AbstractTerm})
126
122
end
127
123
ts
128
124
end
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))
131
125
132
126
"""
133
127
terms(p::AbstractPolynomialLike)
@@ -358,7 +352,7 @@ Returns `p / leadingcoefficient(p)` where the leading coefficient of the returne
358
352
"""
359
353
function monic (p:: APL )
360
354
α = leadingcoefficient (p)
361
- polynomial (_divtoone .(terms (p), α))
355
+ polynomial! (_divtoone .(terms (p), α))
362
356
end
363
357
monic (m:: AbstractMonomialLike ) = m
364
358
monic (t:: AbstractTermLike{T} ) where T = one (T) * monomial (t)
@@ -386,14 +380,14 @@ function mapcoefficientsnz(f::Function, p::AbstractPolynomialLike)
386
380
# Invariant: p has only nonzero coefficient
387
381
# therefore f(α) will be nonzero for every coefficient α of p
388
382
# hence we can use Uniq
389
- polynomial (mapcoefficientsnz .(f, terms (p)), SortedUniqState ())
383
+ polynomial! (mapcoefficientsnz .(f, terms (p)), SortedUniqState ())
390
384
end
391
385
mapcoefficientsnz (f:: Function , t:: AbstractTermLike ) = f (coefficient (t)) * monomial (t)
392
386
393
387
Base. round (t:: AbstractTermLike ; args... ) = round (coefficient (t); args... ) * monomial (t)
394
388
function Base. round (p:: AbstractPolynomialLike ; args... )
395
389
# round(0.1) is zero so we cannot use SortedUniqState
396
- polynomial (round .(terms (p); args... ), SortedState ())
390
+ polynomial! (round .(terms (p); args... ), SortedState ())
397
391
end
398
392
399
393
Base. ndims (:: Type{<:AbstractPolynomialLike} ) = 0
0 commit comments