forked from JuliaMath/Polynomials.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparsePolynomial.jl
325 lines (232 loc) · 7.59 KB
/
SparsePolynomial.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
export SparsePolynomial
"""
SparsePolynomial{T, X}(coeffs::Dict, [var = :x])
Polynomials in the standard basis backed by a dictionary holding the
non-zero coefficients. For polynomials of high degree, this might be
advantageous. Addition and multiplication with constant polynomials
are treated as having no symbol.
# Examples:
```jldoctest
julia> using Polynomials
julia> P = SparsePolynomial
SparsePolynomial
julia> p, q = P([1,2,3]), P([4,3,2,1])
(SparsePolynomial(1 + 2*x + 3*x^2), SparsePolynomial(4 + 3*x + 2*x^2 + x^3))
julia> p + q
SparsePolynomial(5 + 5*x + 5*x^2 + x^3)
julia> p * q
SparsePolynomial(4 + 11*x + 20*x^2 + 14*x^3 + 8*x^4 + 3*x^5)
julia> p + 1
SparsePolynomial(2 + 2*x + 3*x^2)
julia> q * 2
SparsePolynomial(8 + 6*x + 4*x^2 + 2*x^3)
julia> p = Polynomials.basis(P, 10^9) - Polynomials.basis(P,0) # also P(Dict(0=>-1, 10^9=>1))
SparsePolynomial(-1.0 + 1.0*x^1000000000)
julia> p(1)
0.0
```
"""
struct SparsePolynomial{T <: Number, X} <: StandardBasisPolynomial{T, X}
coeffs::Dict{Int, T}
function SparsePolynomial{T, X}(coeffs::AbstractDict{Int, S}) where {T <: Number, X, S}
c = Dict{Int, T}(coeffs)
for (k,v) in coeffs
iszero(v) && pop!(c, k)
end
new{T, X}(c)
end
function SparsePolynomial{T,X}(checked::Val{false}, coeffs::AbstractDict{Int, T}) where {T <: Number, X}
new{T,X}(convert(Dict{Int,S}, coeffs))
end
end
@register SparsePolynomial
function SparsePolynomial{T}(coeffs::AbstractDict{Int, S}, var::SymbolLike=:x) where {T <: Number, S}
SparsePolynomial{T, Symbol(var)}(convert(Dict{Int,T}, coeffs))
end
function SparsePolynomial(coeffs::AbstractDict{Int, T}, var::SymbolLike=:x) where {T <: Number}
SparsePolynomial{T, Symbol(var)}(coeffs)
end
function SparsePolynomial{T,X}(coeffs::AbstractVector{S}) where {T <: Number, X, S}
if Base.has_offset_axes(coeffs)
@warn "ignoring the axis offset of the coefficient vector"
end
offset = firstindex(coeffs)
p = Dict{Int,T}(k - offset => v for (k,v) ∈ pairs(coeffs))
return SparsePolynomial{T,X}(p)
end
# conversion
function Base.convert(P::Type{<:Polynomial}, q::SparsePolynomial)
⟒(P)(coeffs(q), indeterminate(q))
end
function Base.convert(P::Type{<:SparsePolynomial}, q::StandardBasisPolynomial{T}) where {T}
R = promote(eltype(P), T)
⟒(P){R}(coeffs(q), indeterminate(q))
end
## changes to common
degree(p::SparsePolynomial) = isempty(p.coeffs) ? -1 : maximum(keys(p.coeffs))
function isconstant(p::SparsePolynomial)
n = length(keys(p.coeffs))
(n > 1 || (n==1 && iszero(p[0]))) && return false
return true
end
function basis(P::Type{<:SparsePolynomial}, n::Int, var::SymbolLike=:x)
T = eltype(P)
X = Symbol(var)
SparsePolynomial{T,X}(Dict(n=>one(T)))
end
# return coeffs as a vector
# use p.coeffs to get Dictionary
function coeffs(p::SparsePolynomial{T}) where {T}
n = degree(p)
cs = zeros(T, n+1)
for (k,v) in p.coeffs
cs[k+1]=v
end
cs
end
# get/set index
function Base.getindex(p::SparsePolynomial{T}, idx::Int) where {T <: Number}
get(p.coeffs, idx, zero(T))
end
function Base.setindex!(p::SparsePolynomial, value::Number, idx::Int)
idx < 0 && return p
if iszero(value)
haskey(p.coeffs, idx) && pop!(p.coeffs, idx)
else
p.coeffs[idx] = value
end
return p
end
Base.firstindex(p::SparsePolynomial) = sort(collect(keys(p.coeffs)), by=x->x[1])[1]
Base.lastindex(p::SparsePolynomial) = sort(collect(keys(p.coeffs)), by=x->x[1])[end]
Base.eachindex(p::SparsePolynomial) = sort(collect(keys(p.coeffs)), by=x->x[1])
# pairs iterates only over non-zero
# inherits order for underlying dictionary
function Base.iterate(v::PolynomialKeys{SparsePolynomial{T,X}}, state...) where {T,X}
y = iterate(v.p.coeffs, state...)
y == nothing && return nothing
return (y[1][1], y[2])
end
function Base.iterate(v::PolynomialValues{SparsePolynomial{T,X}}, state...) where {T,X}
y = iterate(v.p.coeffs, state...)
y == nothing && return nothing
return (y[1][2], y[2])
end
# only from tail
function chop!(p::SparsePolynomial{T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {T}
for k in sort(collect(keys(p.coeffs)), by=x->x[1], rev=true)
if isapprox(p[k], zero(T); rtol = rtol, atol = atol)
pop!(p.coeffs, k)
else
return p
end
end
return p
end
function truncate!(p::SparsePolynomial{T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {T}
max_coeff = maximum(abs, coeffs(p))
thresh = max_coeff * rtol + atol
for (k,val) in p.coeffs
if abs(val) <= thresh
pop!(p.coeffs,k)
end
end
return p
end
##
## ----
##
function (p::SparsePolynomial{T})(x::S) where {T,S}
tot = zero(T) * _one(x)
for (k,v) in p.coeffs
tot = _muladd(x^k, v, tot)
end
return tot
end
# map: over values -- not keys
function Base.map(fn, p::P, args...) where {P <: SparsePolynomial}
ks, vs = keys(p.coeffs), values(p.coeffs)
vs′ = map(fn, vs, args...)
_convert(p, Dict(Pair.(ks, vs′)))
end
function Base.:+(p1::SparsePolynomial{T,X}, p2::SparsePolynomial{S,Y}) where {T, X, S, Y}
isconstant(p1) && return p2 + p1[0]
isconstant(p2) && return p1 + p2[0]
X != Y && error("SparsePolynomials must have same variable")
R = promote_type(T,S)
p = zero(SparsePolynomial{R,X})
# this allocates in the union
# for i in union(eachindex(p1), eachindex(p2))
# p[i] = p1[i] + p2[i]
# end
# this seems faster
for i in eachindex(p1)
p[i] = p1[i] + p2[i]
end
for i in eachindex(p2)
if iszero(p[i])
@inbounds p[i] = p1[i] + p2[i]
end
end
return p
end
function Base.:+(p::SparsePolynomial{T,X}, c::S) where {T, X, S <: Number}
R = promote_type(T,S)
P = SparsePolynomial
D = Dict{Int, R}(kv for kv ∈ p.coeffs)
D[0] = get(D,0,zero(R)) + c
return SparsePolynomial{R,X}(D)
end
function Base.:*(p1::SparsePolynomial{T,X}, p2::SparsePolynomial{S,Y}) where {T,X,S,Y}
isconstant(p1) && return p2 * p1[0]
isconstant(p2) && return p1 * p2[0]
X != Y && error("SparsePolynomials must have same variable")
R = promote_type(T,S)
P = SparsePolynomial
p = zero(P{R, X})
for i in eachindex(p1)
p1ᵢ = p1[i]
for j in eachindex(p2)
@inbounds p[i+j] = muladd(p1ᵢ, p2[j], p[i+j])
end
end
return p
end
function Base.:*(p::P, c::S) where {T, X, P <: SparsePolynomial{T,X}, S <: Number}
R = promote_type(T,S)
q = zero(⟒(P){R,X})
for k in eachindex(p)
q[k] = p[k] * c
end
return q
end
function derivative(p::SparsePolynomial{T,X}, order::Integer = 1) where {T,X}
order < 0 && error("Order of derivative must be non-negative")
order == 0 && return p
R = eltype(one(T)*1)
P = SparsePolynomial
hasnan(p) && return P{R,X}(Dict(0 => R(NaN)))
n = degree(p)
order > n && return zero(P{R,X})
dpn = zero(P{R,X})
@inbounds for k in eachindex(p)
dpn[k-order] = reduce(*, (k - order + 1):k, init = p[k])
end
return dpn
end
function integrate(p::P, k::S) where {T, X, P<:SparsePolynomial{T,X}, S<:Number}
R = eltype((one(T)+one(S))/1)
Q = SparsePolynomial{R,X}
if hasnan(p) || isnan(k)
return Q(Dict(0 => NaN))
end
∫p = Q(R(k))
for k in eachindex(p)
∫p[k + 1] = p[k] / (k+1)
end
return ∫p
end