Added general Neville's Algorithm

This commit is contained in:
sm
2014-01-12 23:05:25 -08:00
parent b761ad7ddc
commit 282bcb448f
2 changed files with 123 additions and 7 deletions
+15 -4
View File
@@ -6,9 +6,7 @@ import (
)
// explicitly implements Neville's algorithm for three points at very
// particular parameter values.
// TODO: update this to take an arbitrary parameter value, and arbitrary slice
// of Point2d
// particular parameter values: ts[]{0, 1/3, 2/3}, extrapolate to 1.
func Extrapolate(P []Point2d) (*Point2d, error) {
if len(P) != 3 {
return nil, errors.New("only works for len(P) == 3")
@@ -23,6 +21,8 @@ func Extrapolate(P []Point2d) (*Point2d, error) {
return &p012, nil
}
// Implements Neville's Algorithm for a slice of points (P) at parameter
// values(ts), to parameter value (t). Returns the point and an error
func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) {
if len(P) != len(ts) {
return nil, errors.New(
@@ -33,5 +33,16 @@ func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) {
),
)
}
return nil, errors.New("smb")
Q := []Point2d(P)
order := len(P) - 1
for i := 0; i < order; i++ {
for j := 0; j < order-i; j++ {
var tLeft float32 = float32(ts[i+j+1] - t)
var tRight float32 = float32(t - ts[j])
Q[j] = Q[j].ToVector().Scale(tLeft).ToPoint().Add(P[j+1].ToVector().Scale(tRight))
Q[j] = Q[j].ToVector().Scale(float32(1.0 / (ts[i+j+1] - ts[j]))).ToPoint()
}
}
return &Q[0], nil
}