Skip to content

Commit 7c0cfec

Browse files
authored
Merge pull request #91 from Keno/vector
clean up for v0.6
2 parents b78d955 + 7c1e9e2 commit 7c0cfec

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

src/Polynomials.jl

+17-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export polyval, polyint, polyder, roots, polyfit
1313
export Pade, padeval
1414

1515
import Base: length, endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
16-
import Base: show, print, *, /, //, -, +, ==, divrem, div, rem, eltype, .*, .-, .+
16+
import Base: show, print, *, /, //, -, +, ==, divrem, div, rem, eltype
1717
import Base: promote_rule, truncate, chop, call, conj, transpose, dot, hash
1818
import Base: isequal
1919

@@ -156,7 +156,7 @@ variable(var::SymbolLike=:x) = variable(Float64, var)
156156
"""
157157
function truncate{T}(p::Poly{Complex{T}}; reltol = eps(T), abstol = eps(T))
158158
a = coeffs(p)
159-
amax = maxabs(a)
159+
amax = maximum(abs,a)
160160
thresh = amax * reltol + abstol
161161
anew = map(ai -> complex(abs(real(ai)) <= thresh ? zero(T) : real(ai),
162162
abs(imag(ai)) <= thresh ? zero(T) : imag(ai)),
@@ -166,7 +166,7 @@ end
166166

167167
function truncate{T}(p::Poly{T}; reltol = eps(T), abstol = eps(T))
168168
a = coeffs(p)
169-
amax = maxabs(a)
169+
amax = maximum(abs,a)
170170
anew = map(ai -> abs(ai) <= amax*reltol+abstol ? zero(T) : ai, a)
171171
return Poly(anew, p.var)
172172
end
@@ -244,16 +244,10 @@ one{T}(::Type{Poly{T}}) = Poly([one(T)])
244244
dot{T<:Number,S}(p::Poly{S}, c::T) = p * c
245245
dot{T<:Number,S}(c::T, p::Poly{S}) = c * p
246246
dot(p1::Poly, p2::Poly) = p1 * p2
247-
.*{T<:Number,S}(c::T, p::Poly{S}) = Poly(c * p.a, p.var)
248-
.*{T<:Number,S}(p::Poly{S}, c::T) = Poly(p.a * c, p.var)
249247
/(p::Poly, c::Number) = Poly(p.a / c, p.var)
250248
-(p::Poly) = Poly(-p.a, p.var)
251249
-{T<:Number}(p::Poly, c::T) = +(p, -c)
252-
.-{T<:Number}(p::Poly, c::T) = +(p, -c)
253-
.-{T<:Number}(c::T, p::Poly) = +(p, -c)
254250
+{T<:Number}(c::T, p::Poly) = +(p, c)
255-
.+{T<:Number}(c::T, p::Poly) = +(p, c)
256-
.+{T<:Number}(p::Poly, c::T) = +(p, c)
257251
function +{T<:Number}(p::Poly, c::T)
258252
if length(p) < 1
259253
return Poly([c,], p.var)
@@ -304,6 +298,16 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
304298
Poly(a,p1.var)
305299
end
306300

301+
## older . operators
302+
if VERSION < v"0.6.0-dev"
303+
@compat Base.:.+{T<:Number}(c::T, p::Poly) = +(p, c)
304+
@compat Base.:.+{T<:Number}(p::Poly, c::T) = +(p, c)
305+
@compat Base.:.-{T<:Number}(p::Poly, c::T) = +(p, -c)
306+
@compat Base.:.-{T<:Number}(c::T, p::Poly) = +(p, -c)
307+
@compat Base.:.*{T<:Number,S}(c::T, p::Poly{S}) = Poly(c * p.a, p.var)
308+
@compat Base.:.*{T<:Number,S}(p::Poly{S}, c::T) = Poly(p.a * c, p.var)
309+
end
310+
307311
function divrem{T, S}(num::Poly{T}, den::Poly{S})
308312
if num.var != den.var
309313
error("Polynomials must have same variable")
@@ -403,7 +407,7 @@ polyint{T}(p::Poly{T}) = polyint(p, 0)
403407

404408
# if we have coefficients that have `NaN` representation
405409
function polyint{T<:Union{Real,Complex},S<:Number}(p::Poly{T}, k::S)
406-
any(isnan(p.a)) && return Poly(promote_type(T,S)[NaN])
410+
any(map(isnan,p.a)) && return Poly(promote_type(T,S)[NaN])
407411
_polyint(p, k)
408412
end
409413

@@ -415,7 +419,7 @@ end
415419

416420
# if we have both coefficients and initial condition that can take `NaN`
417421
function polyint{T<:Union{Real,Complex},S<:Union{Real,Complex}}(p::Poly{T}, k::S)
418-
(any(isnan(p.a)) || isnan(k)) && return Poly(promote_type(T,S)[NaN])
422+
(any(map(isnan,p.a)) || isnan(k)) && return Poly(promote_type(T,S)[NaN])
419423
_polyint(p, k)
420424
end
421425

@@ -449,7 +453,7 @@ function polyder{T<:Union{Real,Complex}}(p::Poly{T}, order::Int=1)
449453
n = length(p)
450454
order < 0 && error("Order of derivative must be non-negative")
451455
order == 0 && return p
452-
any(isnan(p.a)) && return Poly(T[NaN], p.var)
456+
any(map(isnan,p.a)) && return Poly(T[NaN], p.var)
453457
n <= order && return Poly(T[], p.var)
454458
_polyder(p, order)
455459
end
@@ -537,7 +541,7 @@ gcd(poly([1,1,2]), poly([1,2,3])) # returns (x-1)*(x-2)
537541
```
538542
"""
539543
function gcd{T, S}(a::Poly{T}, b::Poly{S})
540-
if all(abs(b.a).<=2*eps(S))
544+
if all(map(abs,b.a).<=2*eps(S))
541545
return a
542546
else
543547
s, r = divrem(a, b)

test/runtests.jl

+19-18
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ sprint(show, pNULL)
6060
@test roots(p0)==roots(p1)==roots(pNULL)==[]
6161
@test roots(p2) == [-1]
6262
a_roots = copy(pN.a)
63-
@test all(abs(sort(roots(poly(a_roots))) - sort(a_roots)) .< 1e6)
63+
@test all(map(abs,sort(roots(poly(a_roots))) - sort(a_roots)) .< 1e6)
6464
@test length(roots(p5)) == 4
6565
@test roots(pNULL) == []
6666
@test sort(roots(pR)) == [1//2, 3//2]
@@ -76,7 +76,7 @@ p3 = Poly([7, -3, 2, 6])
7676
p4 = p2 * p3
7777
@test divrem(p4, p2) == (p3, zero(p3))
7878
@test p3%p2 == p3
79-
@test all((abs((p2 ÷ p3 - Poly([1/9,2/3])).a)) .< eps())
79+
@test all((map(abs,(p2 ÷ p3 - Poly([1/9,2/3])).a)) .< eps())
8080
@test divrem(p0,p1) == (p0,p0)
8181
@test divrem(p1,p1) == (p1,p0)
8282
@test divrem(p2,p2) == (p1,p0)
@@ -106,25 +106,25 @@ pcpy2 = copy(pcpy1)
106106
#Tests for Pade approximants
107107

108108
println("Test for the exponential function.")
109-
a = Poly(1.//convert(Vector{BigInt},gamma(BigFloat(1):BigFloat(17))),"x")
109+
a = Poly(1.//convert(Vector{BigInt},map(gamma,BigFloat(1):BigFloat(17))),"x")
110110
PQexp = Pade(a,8,8)
111111
@test isapprox(convert(Float64, padeval(PQexp,1.0)), exp(1.0))
112112
@test isapprox(convert(Float64, padeval(PQexp,-1.0)), exp(-1.0))
113113

114114
println("Test for the sine function.")
115-
b = Poly(convert(Vector{BigInt},sinpi((0:16)/2)).//convert(Vector{BigInt},gamma(BigFloat(1):BigFloat(17))),"x")
115+
b = Poly(convert(Vector{BigInt},map(sinpi,(0:16)/2)).//convert(Vector{BigInt},map(gamma,BigFloat(1):BigFloat(17))),"x")
116116
PQsin = Pade(b,8,7)
117117
@test isapprox(convert(Float64, padeval(PQsin,1.0)), sin(1.0))
118118
@test isapprox(convert(Float64, padeval(PQsin,-1.0)),sin(-1.0))
119119

120120
println("Test for the cosine function.")
121-
c = Poly(convert(Vector{BigInt},sinpi((1:17)/2)).//convert(Vector{BigInt},gamma(BigFloat(1):BigFloat(17))),"x")
121+
c = Poly(convert(Vector{BigInt},map(sinpi,(1:17)/2)).//convert(Vector{BigInt},map(gamma,BigFloat(1):BigFloat(17))),"x")
122122
PQcos = Pade(c,8,8)
123123
@test isapprox(convert(Float64, padeval(PQcos,1.0)), cos(1.0))
124124
@test isapprox(convert(Float64, padeval(PQcos,-1.0)), cos(-1.0))
125125

126126
println("Test for the summation of a factorially divergent series.")
127-
d = Poly(convert(Vector{BigInt},(-1).^(0:60).*gamma(BigFloat(1):BigFloat(61.0))).//1,"x")
127+
d = Poly(convert(Vector{BigInt},(-1).^(0:60).* map(gamma,BigFloat(1):BigFloat(61.0))).//1,"x")
128128
PQexpint = Pade(d,30,30)
129129
@compat println("The approximate sum of the divergent series is: ", Float64(padeval(PQexpint,1.0)))
130130
println("The approximate sum of the convergent series is: ",exp(1)*(-γ-sum([(-1).^k/k./gamma(k+1) for k=1:20])))
@@ -134,11 +134,11 @@ println("The approximate sum of the convergent series is: ",exp(1)*(-γ-sum([(-1
134134

135135
## polyfit
136136
xs = linspace(0, pi, 10)
137-
ys = sin(xs)
137+
ys = map(sin,xs)
138138
p = polyfit(xs, ys)
139139
p = polyfit(xs, ys, :t)
140140
p = polyfit(xs, ys, 2)
141-
@test maximum(abs(map(x->polyval(p, x), xs) - ys)) <= 0.03
141+
@test maximum(map(abs,map(x->polyval(p, x), xs) - ys)) <= 0.03
142142

143143

144144
## truncation
@@ -207,7 +207,7 @@ import Base: +, *, -
207207
immutable Mod2 <: Number
208208
v::Bool
209209
end
210-
+(x::Mod2,y::Mod2) = Mod2(x.v$y.v)
210+
+(x::Mod2,y::Mod2) = Mod2(xor(x.v, y.v))
211211
*(x::Mod2,y::Mod2) = Mod2(x.v&y.v)
212212
-(x::Mod2,y::Mod2) = x+y
213213
-(x::Mod2) = x
@@ -239,14 +239,15 @@ p = Poly([1//2, 2//3, 1])
239239

240240

241241
## want to be able to copy and paste
242-
string_eval_poly(p,x) = eval(Expr(:function, Expr(:call, :f, :x), parse(string(p)[6:end-1])))(x)
243-
p = Poly([1,2,3]) # copy and paste
244-
q = Poly([1//1, 2//1, 3//1])
245-
r = Poly([1.0, 2, 3])
246-
@test string_eval_poly(p, 5) == p(5)
247-
@test string_eval_poly(q, 5) == q(5)
248-
@test string_eval_poly(r, 5) == r(5)
249-
242+
if VERSION < v"0.6.0-dev"
243+
string_eval_poly(p,x) = eval(Expr(:function, Expr(:call, :f, :x), parse(string(p)[6:end-1])))(x)
244+
p = Poly([1,2,3]) # copy and paste
245+
q = Poly([1//1, 2//1, 3//1])
246+
r = Poly([1.0, 2, 3])
247+
@test string_eval_poly(p, 5) == p(5)
248+
@test string_eval_poly(q, 5) == q(5)
249+
@test string_eval_poly(r, 5) == r(5)
250+
end
250251
## check hashing
251252
p = poly([1,2,3])
252253
q = poly([1,2,3])
@@ -266,7 +267,7 @@ p2 = Poly([0., 5., Inf])
266267
p3 = Poly([0, NaN])
267268

268269
@test p1 == p2 && !isequal(p1, p2)
269-
@test is(p3, p3) && p3 p3 && isequal(p3, p3)
270+
@test p3 === p3 && p3 p3 && isequal(p3, p3)
270271

271272
## Handling of `NaN`s
272273
p = Poly([NaN, 1, 5])

0 commit comments

Comments
 (0)