@@ -15,8 +15,8 @@ export Pade, padeval
15
15
16
16
import Compat. lastindex
17
17
18
- import Base: start, next, done, length, size, eltype, collect, eachindex
19
- import Base: getindex, setindex!, copy, zero, one, convert, gcd
18
+ import Base: length, size, eltype, collect, eachindex
19
+ import Base: getindex, setindex!, copy, zero, one, convert, gcd
20
20
import Base: show, print, * , / , // , - , + , == , isapprox, divrem, div, rem, eltype
21
21
import Base: promote_rule, truncate, chop, conj, transpose, hash
22
22
import Base: isequal
@@ -146,10 +146,16 @@ eltype(::Type{Poly{T}}) where {T} = Poly{T}
146
146
length (p:: Poly ) = length (coeffs (p))
147
147
lastindex (p:: Poly ) = length (p) - 1
148
148
149
- start (p:: Poly ) = start (coeffs (p)) - 1
150
- next (p:: Poly , state) = (temp = fill! (similar (coeffs (p)), 0 ); temp[state+ 1 ] = p[state]; (Poly (temp), state+ 1 ))
151
- done (p:: Poly , state) = state > degree (p)
152
-
149
+ if VERSION < v " 0.7.0-DEV.5126"
150
+ import Base: start, next, done
151
+ start (p:: Poly ) = start (coeffs (p)) - 1
152
+ next (p:: Poly , state) = (temp = fill! (similar (coeffs (p)), 0 ); temp[state+ 1 ] = p[state]; (Poly (temp), state+ 1 ))
153
+ done (p:: Poly , state) = state > degree (p)
154
+ else
155
+ import Base: iterate
156
+ Base. iterate (p:: Poly ) = (p[0 ] * one (p), 1 )
157
+ Base. iterate (p:: Poly , state) = state <= degree (p) ? (p[state]* variable (p)^ (state), state+ 1 ) : nothing
158
+ end
153
159
154
160
# shortcut for collect(eltype, collection)
155
161
collect (p:: Poly{T} ) where {T} = collect (Poly{T}, p)
@@ -161,9 +167,9 @@ size(p::Poly, i::Integer) = size(p.a, i)
161
167
degree(p::Poly)
162
168
163
169
Return the degree of the polynomial `p`, i.e. the highest exponent in the polynomial that
164
- has a nonzero coefficient.
170
+ has a nonzero coefficient. The degree of the zero polynomial is defined to be -1.
165
171
"""
166
- degree (p:: Poly ) = length (p) - 1
172
+ degree (p:: Poly ) = iszero (p) ? - 1 : length (p) - 1
167
173
168
174
"""
169
175
coeffs(p::Poly)
@@ -244,7 +250,7 @@ the `p`-norm is
244
250
||q||_p = (|q_0|^p + \\ ldots + |q_n|^p)^{1/p}
245
251
``
246
252
"""
247
- norm (q:: Poly , args ... ) = norm (coeffs (q), args ... )
253
+ norm (q:: Poly , p :: Real = 2 ) = norm (coeffs (q), p )
248
254
249
255
250
256
"""
@@ -270,7 +276,7 @@ function setindex!(p::Poly, value, idx::Int)
270
276
n = length (p. a)
271
277
if n ≤ idx
272
278
resize! (p. a, idx+ 1 )
273
- p. a[n+ 1 : idx] = 0
279
+ p. a[n+ 1 : idx] . = 0
274
280
end
275
281
p. a[idx+ 1 ] = value
276
282
return p
@@ -352,6 +358,9 @@ function *(p1::Poly{T}, p2::Poly{S}) where {T,S}
352
358
Poly (a,p1. var)
353
359
end
354
360
361
+ # quiet this deprecation https://github.com/JuliaLang/julia/pull/23332
362
+ import Base: ^
363
+ ^ (p:: Poly , n:: Integer ) = Base. power_by_squaring (p,n)
355
364
356
365
# are any values NaN
357
366
hasnan (p:: Poly ) = reduce (| , (isnan .(p. a)))
@@ -706,17 +715,17 @@ function polyfit(x, y, n::Int=length(x)-1, sym::Symbol=:x)
706
715
# here unsure, whether similar(float(x[1]),...), or similar(x,...)
707
716
# however similar may yield unwanted surprise in case of e.g. x::Int
708
717
#
709
- A= similar (float .(x[1 : 1 ]), length (x), n+ 1 )
718
+ T = eltype (float (x[1 ]))
719
+ A = Array {T} (undef, length (x), n+ 1 )
710
720
#
711
721
# TODO : add support for poly coef bitmap
712
722
# (i.e. polynomial with some terms fixed to zero)
713
723
#
714
- A[:,1 ]= 1
724
+ A[:,1 ] . = 1
715
725
for i= 1 : n
716
- A[:,i+ 1 ]= A[:,i] .* x # cumulative product more precise than x.^n
726
+ A[:,i+ 1 ] . = A[:,i] .* x # cumulative product more precise than x.^n
717
727
end
718
- Aqr= qrfact (A) # returns QR object, not a matrix
719
- p= Aqr\ y # least squares solution via QR
728
+ p = A \ float .(y)
720
729
Poly (p, sym)
721
730
end
722
731
polyfit (x,y,sym:: Symbol ) = polyfit (x,y,length (x)- 1 , sym)
0 commit comments