Skip to content

Commit b018f97

Browse files
committed
use polyfit with defaults instead of polyinterpolate
1 parent f796a8c commit b018f97

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

src/Polynomials.jl

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Compat
77

88
export Poly, poly
99
export degree, coeffs
10-
export polyval, polyint, polyder, roots, polyfit, polyinterpolate
10+
export polyval, polyint, polyder, roots, polyfit
1111
export Pade, padeval
1212

1313
import Base: length, endof, getindex, setindex!, copy, zero, one, convert, norm, gcd
@@ -441,7 +441,11 @@ end
441441
## Fit degree n polynomial to points
442442
"""
443443
444-
`polyfit(x::AbstractVector, y::AbstractVector)`: Fit a polynomial of degree `n` through the points specified by `x` and `y` where `n <= length(x) - 1` using least squares fit.
444+
`polyfit(x, y, n=length(x)-1, sym=:x )`: Fit a polynomial of degree
445+
`n` through the points specified by `x` and `y` where `n <= length(x)
446+
- 1` using least squares fit. When `n=length(x)-1` (the default), the
447+
interpolating polynomial is returned. The optional fourth argument can
448+
be used to pass the symbol for the returned polynomial.
445449
446450
Example:
447451
@@ -453,55 +457,14 @@ polyfit(xs, ys, 2)
453457
454458
Original by [ggggggggg](https://github.com/Keno/Polynomials.jl/issues/19)
455459
"""
456-
function polyfit(x, y, n)
460+
function polyfit(x, y, n::Int=length(x)-1, sym::Symbol=:x)
457461
length(x) == length(y) || throw(DomainError)
458-
n <= length(x) - 1 || throw(DomainError)
462+
1 <= n <= length(x) - 1 || throw(DomainError)
459463

460464
A = [ float(x[i])^p for i = 1:length(x), p = 0:n ]
461-
Poly(A \ y)
462-
end
463-
464-
## Find interpolating polynomial through the points
465-
"""
466-
467-
* `polyinterpolate(x, y)`: Return interpolating polynomial of degree `n-1` or less.
468-
469-
Example
470-
471-
```
472-
xs = linspace(0, pi, 3)
473-
ys = map(sin, xs)
474-
polyinterpolate(xs, ys)
475-
```
476-
477-
This uses [Algorithm 1](http://www.ams.org/journals/mcom/1970-24-109/S0025-5718-1970-0258240-X/S0025-5718-1970-0258240-X.pdf) of Krogh, *Efficient Algorithms for Polynomial Interpolation and Numerical Differentiation*.
478-
"""
479-
function polyinterpolate(xs, ys; xvar=:x)
480-
length(unique(xs)) == length(xs) || throw(DomainError)
481-
n = length(xs) - 1
482-
etype = eltype(float(ys))
483-
x = Poly([0, one(etype,)])
484-
485-
omega = 1
486-
π = 1
487-
p = Poly(ys[1:1], xvar)
488-
489-
Vii = zeros(etype, n + 1)
490-
Vii[1] = ys[1]
491-
492-
for k = 1:n
493-
V_ik = ys[k+1]
494-
for i=0:(k-1)
495-
V_ik = (Vii[i+1] - V_ik) / (xs[i+1] - xs[k+1])
496-
end
497-
Vii[k+1] = V_ik
498-
omega = x - xs[k-1 + 1]
499-
π = omega * π
500-
p = p + π * Vii[k+1]
501-
end
502-
503-
p
465+
Poly(A \ y, sym)
504466
end
467+
polyfit(x,y,sym::Symbol) = polyfit(x,y,length(x)-1, sym)
505468

506469
### Pull in others
507470
include("pade.jl")

test/runtests.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,8 @@ println("The approximate sum of the convergent series is: ",exp(1)*(-γ-sum([(-1
133133
## polyfit
134134
xs = linspace(0, pi, 10)
135135
ys = sin(xs)
136+
p = polyfit(xs, ys)
137+
p = polyfit(xs, ys, :t)
136138
p = polyfit(xs, ys, 2)
137139
@test maximum(abs(map(x->polyval(p, x), xs) - ys)) <= 0.03
138140

139-
## polyinterpolate
140-
xs = linspace(0, pi, 3)
141-
ys = sin(xs)
142-
p = polyinterpolate(xs, ys)
143-
@test maximum(abs(map(x->polyval(p, x), xs) - ys)) <= 10*eps()

0 commit comments

Comments
 (0)