@@ -15,8 +15,8 @@ export Pade, padeval
1515
1616import Compat. lastindex
1717
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
2020import Base: show, print, * , / , // , - , + , == , isapprox, divrem, div, rem, eltype
2121import Base: promote_rule, truncate, chop, conj, transpose, hash
2222import Base: isequal
@@ -146,10 +146,16 @@ eltype(::Type{Poly{T}}) where {T} = Poly{T}
146146length (p:: Poly ) = length (coeffs (p))
147147lastindex (p:: Poly ) = length (p) - 1
148148
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
153159
154160# shortcut for collect(eltype, collection)
155161collect (p:: Poly{T} ) where {T} = collect (Poly{T}, p)
@@ -161,9 +167,9 @@ size(p::Poly, i::Integer) = size(p.a, i)
161167 degree(p::Poly)
162168
163169Return 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.
165171"""
166- degree (p:: Poly ) = length (p) - 1
172+ degree (p:: Poly ) = iszero (p) ? - 1 : length (p) - 1
167173
168174"""
169175 coeffs(p::Poly)
@@ -244,7 +250,7 @@ the `p`-norm is
244250||q||_p = (|q_0|^p + \\ ldots + |q_n|^p)^{1/p}
245251``
246252"""
247- norm (q:: Poly , args ... ) = norm (coeffs (q), args ... )
253+ norm (q:: Poly , p :: Real = 2 ) = norm (coeffs (q), p )
248254
249255
250256"""
@@ -270,7 +276,7 @@ function setindex!(p::Poly, value, idx::Int)
270276 n = length (p. a)
271277 if n ≤ idx
272278 resize! (p. a, idx+ 1 )
273- p. a[n+ 1 : idx] = 0
279+ p. a[n+ 1 : idx] . = 0
274280 end
275281 p. a[idx+ 1 ] = value
276282 return p
@@ -352,6 +358,9 @@ function *(p1::Poly{T}, p2::Poly{S}) where {T,S}
352358 Poly (a,p1. var)
353359end
354360
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)
355364
356365# are any values NaN
357366hasnan (p:: Poly ) = reduce (| , (isnan .(p. a)))
@@ -706,17 +715,17 @@ function polyfit(x, y, n::Int=length(x)-1, sym::Symbol=:x)
706715 # here unsure, whether similar(float(x[1]),...), or similar(x,...)
707716 # however similar may yield unwanted surprise in case of e.g. x::Int
708717 #
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 )
710720 #
711721 # TODO : add support for poly coef bitmap
712722 # (i.e. polynomial with some terms fixed to zero)
713723 #
714- A[:,1 ]= 1
724+ A[:,1 ] . = 1
715725 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
717727 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)
720729 Poly (p, sym)
721730end
722731polyfit (x,y,sym:: Symbol ) = polyfit (x,y,length (x)- 1 , sym)
0 commit comments