Skip to content

Commit 6625231

Browse files
committed
Merge pull request #39 from pwl/master
Resolves #38 by adding a `trunctate` function
2 parents 3339466 + 319dd82 commit 6625231

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/Polynomials.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ export Poly, poly
99
export degree, coeffs
1010
export polyval, polyint, polyder, roots, polyfit
1111
export Pade, padeval
12+
export truncate!
1213

1314
import Base: length, endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
1415
import Base: show, print, *, /, //, -, +, ==, divrem, div, rem, eltype
15-
import Base: promote_rule
16+
import Base: promote_rule, truncate
1617
if VERSION >= v"0.4"
1718
import Base.call
1819
end
@@ -67,7 +68,7 @@ immutable Poly{T<:Number}
6768
return new(zeros(T,1), symbol(var))
6869
else
6970
# determine the last nonzero element and truncate a accordingly
70-
a_last = max(1,findlast( p->(abs(p) > 2*eps(T)), a))
71+
a_last = max(1,findlast(a))
7172
new(a[1:a_last], symbol(var))
7273
end
7374
end
@@ -136,6 +137,18 @@ coeffs(p::Poly) = p.a
136137

137138
"""
138139
140+
`truncate{T}(p::Poly{T}; reltol = eps(T), abstol = eps(T))`: returns a polynomial with coefficients a_i truncated to zero if |a_i| <= reltol*maxabs(a)+abstol
141+
142+
"""
143+
function truncate{T}(p::Poly{T}; reltol = eps(T), abstol = eps(T))
144+
a = coeffs(p)
145+
amax = maxabs(a)
146+
anew = map(ai -> abs(ai) <= amax*reltol+abstol ? zero(T) : ai, a)
147+
return Poly(anew)
148+
end
149+
150+
"""
151+
139152
* `norm(q::Poly, [p])`: return `p` norm of polynomial `q`
140153
141154
"""
@@ -253,7 +266,7 @@ function divrem{T, S}(num::Poly{T}, den::Poly{S})
253266

254267
return pQ, pR
255268
end
256-
269+
257270
@deprecate /(num::Poly, den::Poly) div(num::Poly, den::Poly)
258271
div(num::Poly, den::Poly) = divrem(num, den)[1]
259272
rem(num::Poly, den::Poly) = divrem(num, den)[2]
@@ -266,7 +279,7 @@ function ==(p1::Poly, p2::Poly)
266279
end
267280
end
268281

269-
"""
282+
"""
270283
* `polyval(p::Poly, x::Number)`: Evaluate the polynomial `p` at `x` using Horner's method.
271284
272285
Example:
@@ -314,7 +327,7 @@ polyint(Poly([1, 0, -1])) # Poly(x - 0.3333333333333333x^3)
314327
polyint(Poly([1, 0, -1]), 2) # Poly(2.0 + x - 0.3333333333333333x^3)
315328
```
316329
317-
"""
330+
"""
318331
function polyint{T}(p::Poly{T}, k::Number=0)
319332
n = length(p)
320333
R = typeof(one(T)/1)
@@ -379,7 +392,7 @@ Examples:
379392
roots(Poly([1, 0, -1])) # [-1.0, 1.0]
380393
roots(Poly([1, 0, 1])) # [0.0+1.0im, 0.0-1.0im]
381394
roots(Poly([0, 0, 1])) # [0.0, 0.0]
382-
roots(poly([1,2,3,4])) # [1.0,2.0,3.0,4.0]
395+
roots(poly([1,2,3,4])) # [1.0,2.0,3.0,4.0]
383396
```
384397
"""
385398
function roots{T}(p::Poly{T})
@@ -461,12 +474,12 @@ Original by [ggggggggg](https://github.com/Keno/Polynomials.jl/issues/19)
461474
function polyfit(x, y, n::Int=length(x)-1, sym::Symbol=:x)
462475
length(x) == length(y) || throw(DomainError)
463476
1 <= n <= length(x) - 1 || throw(DomainError)
464-
477+
465478
A = [ float(x[i])^p for i = 1:length(x), p = 0:n ]
466479
Poly(A \ y, sym)
467480
end
468481
polyfit(x,y,sym::Symbol) = polyfit(x,y,length(x)-1, sym)
469-
482+
470483
### Pull in others
471484
include("pade.jl")
472485

test/runtests.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ sprint(show, pNULL)
5555
if VERSION >= v"0.4"
5656
@test pN(-.125) == 276.9609375
5757
@test pN([0.1, 0.2, 0.3]) == polyval(pN, [0.1, 0.2, 0.3])
58-
end
58+
end
5959

6060
@test poly([-1,-1]) == p3
6161
@test roots(p0)==roots(p1)==roots(pNULL)==[]
@@ -141,3 +141,15 @@ p = polyfit(xs, ys, :t)
141141
p = polyfit(xs, ys, 2)
142142
@test maximum(abs(map(x->polyval(p, x), xs) - ys)) <= 0.03
143143

144+
145+
## truncation
146+
p1 = Poly([1,1]/10)
147+
p2 = Poly([1,2]/10)
148+
p3 = Poly([1,3]/10)
149+
psum = p1 + p2 - p3
150+
@test degree(psum) == 1 # will have wrong degree
151+
@test degree(truncate(psum)) == 0 # the degree should be correct after truncation
152+
153+
@test truncate(Poly([2,1]),reltol=1/2,abstol=0) == Poly([2])
154+
@test truncate(Poly([2,1]),reltol=1,abstol=0) == Poly([0])
155+
@test truncate(Poly([2,1]),reltol=0,abstol=1) == Poly([2])

0 commit comments

Comments
 (0)