Skip to content

Commit 1c2c796

Browse files
authored
Merge pull request #143 from jverzani/v0.7
degree of zero poly; fix v0.7 warnings
2 parents b8d720a + 5cbcba6 commit 1c2c796

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/Polynomials.jl

+24-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export Pade, padeval
1515

1616
import 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
2020
import Base: show, print, *, /, //, -, +, ==, isapprox, divrem, div, rem, eltype
2121
import Base: promote_rule, truncate, chop, conj, transpose, hash
2222
import Base: isequal
@@ -146,10 +146,16 @@ eltype(::Type{Poly{T}}) where {T} = Poly{T}
146146
length(p::Poly) = length(coeffs(p))
147147
lastindex(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)
155161
collect(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
163169
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.
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)
353359
end
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
357366
hasnan(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)
721730
end
722731
polyfit(x,y,sym::Symbol) = polyfit(x,y,length(x)-1, sym)

test/REQUIRE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SpecialFunctions

test/runtests.jl

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Compat
33
using Compat.Test
44
using Compat.LinearAlgebra
55
using Polynomials
6+
using SpecialFunctions
67

78
import Compat.SparseArrays: sparse, speye, nnz
89

@@ -46,7 +47,7 @@ sprint(show, pNULL)
4647
@test pNULL^3 == pNULL
4748
@test pNULL*pNULL == pNULL
4849

49-
@test map(degree, [pNULL,p0,p1,p2,p3,p4,p5,pN,pR,p1000]) == [0,0,0,1,2,3,4,4,2,999]
50+
@test map(degree, [pNULL,p0,p1,p2,p3,p4,p5,pN,pR,p1000]) == [-1,-1,0,1,2,3,4,4,2,999]
5051

5152
@test polyval(poly(Int[]), 2.) == 1.
5253
@test polyval(pN, -.125) == 276.9609375
@@ -168,7 +169,9 @@ p = polyfit(xs, ys)
168169
p = polyfit(xs, ys, :t)
169170
p = polyfit(xs, ys, 2)
170171
@test maximum(map(abs,map(x->polyval(p, x), xs) - ys)) <= 0.03
171-
172+
#https://stackoverflow.com/questions/50832823/error-with-polyfit-function-julia
173+
# relax type assumptions on x, y
174+
polyfit(Any[1,2,3], Any[2,3,1])
172175

173176
## truncation
174177
p1 = Poly([1,1]/10)
@@ -225,8 +228,8 @@ p1[0:1] = [7,8]
225228
## conjugate of poly (issue #59)
226229
as = [im, 1, 2]
227230
bs = [1, 1, 2]
228-
@test conj(Poly(as)) == Poly(conj(as))
229-
@test conj(Poly(bs)) == Poly(conj(bs))
231+
@test conj(Poly(as)) == Poly(conj.(as))
232+
#@test conj(Poly(bs)) == Poly(conj(bs)) # conj gives warning as no defn on T
230233
## and transpose
231234
@test transpose(Poly(as)) == Poly(as)
232235

@@ -355,7 +358,7 @@ p2s = Poly([1], :s)
355358
# test size
356359
@test size(Poly([0.5, 0.2])) == (2,)
357360
@test size(Poly([0.5, 0.2]), 1) == 2
358-
@test size(Poly([0.5, 0.2]), 1, 2) == (2,1)
361+
# @test size(Poly([0.5, 0.2]), 1, 2) == (2,1) # deprecated in v0.7
359362

360363
# test iteration
361364
p1 = Poly([1,2,0,3])
@@ -393,3 +396,9 @@ p1 = poly([1.,2.,3.])
393396
p2 = poly([1.,2.,6.])
394397

395398
@test (res = roots(gcd(p1, p2)); 1. res && 2. res)
399+
400+
## Getting error on passing Real arrays to polyfit #146
401+
xx = Real[20.0, 30.0, 40.0]
402+
yy = Real[15.7696, 21.4851, 28.2463]
403+
polyfit(xx,yy,2)
404+

0 commit comments

Comments
 (0)