@@ -9,10 +9,11 @@ export Poly, poly
9
9
export degree, coeffs
10
10
export polyval, polyint, polyder, roots, polyfit
11
11
export Pade, padeval
12
+ export truncate!
12
13
13
14
import Base: length, endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
14
15
import Base: show, print, * , / , // , - , + , == , divrem, div, rem, eltype
15
- import Base: promote_rule
16
+ import Base: promote_rule, truncate
16
17
if VERSION >= v " 0.4"
17
18
import Base. call
18
19
end
@@ -67,7 +68,7 @@ immutable Poly{T<:Number}
67
68
return new (zeros (T,1 ), symbol (var))
68
69
else
69
70
# 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))
71
72
new (a[1 : a_last], symbol (var))
72
73
end
73
74
end
@@ -136,6 +137,18 @@ coeffs(p::Poly) = p.a
136
137
137
138
"""
138
139
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
+
139
152
* `norm(q::Poly, [p])`: return `p` norm of polynomial `q`
140
153
141
154
"""
@@ -253,7 +266,7 @@ function divrem{T, S}(num::Poly{T}, den::Poly{S})
253
266
254
267
return pQ, pR
255
268
end
256
-
269
+
257
270
@deprecate / (num:: Poly , den:: Poly ) div (num:: Poly , den:: Poly )
258
271
div (num:: Poly , den:: Poly ) = divrem (num, den)[1 ]
259
272
rem (num:: Poly , den:: Poly ) = divrem (num, den)[2 ]
@@ -266,7 +279,7 @@ function ==(p1::Poly, p2::Poly)
266
279
end
267
280
end
268
281
269
- """
282
+ """
270
283
* `polyval(p::Poly, x::Number)`: Evaluate the polynomial `p` at `x` using Horner's method.
271
284
272
285
Example:
@@ -314,7 +327,7 @@ polyint(Poly([1, 0, -1])) # Poly(x - 0.3333333333333333x^3)
314
327
polyint(Poly([1, 0, -1]), 2) # Poly(2.0 + x - 0.3333333333333333x^3)
315
328
```
316
329
317
- """
330
+ """
318
331
function polyint {T} (p:: Poly{T} , k:: Number = 0 )
319
332
n = length (p)
320
333
R = typeof (one (T)/ 1 )
@@ -379,7 +392,7 @@ Examples:
379
392
roots(Poly([1, 0, -1])) # [-1.0, 1.0]
380
393
roots(Poly([1, 0, 1])) # [0.0+1.0im, 0.0-1.0im]
381
394
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]
383
396
```
384
397
"""
385
398
function roots {T} (p:: Poly{T} )
@@ -461,12 +474,12 @@ Original by [ggggggggg](https://github.com/Keno/Polynomials.jl/issues/19)
461
474
function polyfit (x, y, n:: Int = length (x)- 1 , sym:: Symbol = :x )
462
475
length (x) == length (y) || throw (DomainError)
463
476
1 <= n <= length (x) - 1 || throw (DomainError)
464
-
477
+
465
478
A = [ float (x[i])^ p for i = 1 : length (x), p = 0 : n ]
466
479
Poly (A \ y, sym)
467
480
end
468
481
polyfit (x,y,sym:: Symbol ) = polyfit (x,y,length (x)- 1 , sym)
469
-
482
+
470
483
# ## Pull in others
471
484
include (" pade.jl" )
472
485
0 commit comments