From b761ad7ddcef11a6bf6755107c683324df895aae Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sun, 12 Jan 2014 21:28:52 -0800 Subject: [PATCH] added first test case for general nevile --- .gitignore | 1 + govector_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ interp.go | 14 +++++++++++ 3 files changed, 80 insertions(+) diff --git a/.gitignore b/.gitignore index 6e92f57..3aa41b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ tags +.*.swp diff --git a/govector_test.go b/govector_test.go index 077b64c..6fde00b 100644 --- a/govector_test.go +++ b/govector_test.go @@ -1,6 +1,7 @@ package govector import ( + "errors" "math" "testing" ) @@ -648,3 +649,67 @@ func TestExtrapolate(t *testing.T) { } } } + +func TestNeville(t *testing.T) { + knownFails := []struct { + points []Point2d + ts []float64 + t float64 + expected Point2d + err error + }{ + { + points: []Point2d{ + {0, 0}, + {1, 1}, + {2, 2}, + }, + ts: []float64{ + 1.0 / 3.0, + 2.0 / 3.0, + }, + err: errors.New("Incompatable slice lengths len(P): 3 != len(ts): 2"), + }, + } + for _, test := range knownFails { + p, err := Neville(test.points, test.ts, test.t) + if p != nil { + t.Errorf("p should've been nil: %+v", p) + + } + if err.Error() != test.err.Error() { + t.Errorf("expected err: '%s', unexpectd err: '%s'", test.err, err) + } + } + tests := []struct { + points []Point2d + ts []float64 + t float64 + expected Point2d + }{ + { + points: []Point2d{ + {0, 0}, + {1, 1}, + {2, 2}, + }, + ts: []float64{ + 0.0, + 1.0 / 3.0, + 2.0 / 3.0, + }, + t: 1.0, + expected: Point2d{3, 3}, + }, + } + for _, test := range tests { + p, err := Neville(test.points, test.ts, test.t) + if err != nil { + t.Errorf("yeah, this should've been nil: %s", err) + } + if p == nil { + } else if *p != test.expected { + t.Errorf("wrong: expected: %+v, actual: %+v", test.expected, p) + } + } +} diff --git a/interp.go b/interp.go index 0322983..40dad2e 100644 --- a/interp.go +++ b/interp.go @@ -2,6 +2,7 @@ package govector import ( "errors" + "fmt" ) // explicitly implements Neville's algorithm for three points at very @@ -21,3 +22,16 @@ func Extrapolate(P []Point2d) (*Point2d, error) { p012 := p01.ToVector().Scale(-1.0 / 2.0).ToPoint().Add(p12.ToVector().Scale(3.0 / 2.0)) return &p012, nil } + +func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) { + if len(P) != len(ts) { + return nil, errors.New( + fmt.Sprintf( + "Incompatable slice lengths len(P): %d != len(ts): %d", + len(P), + len(ts), + ), + ) + } + return nil, errors.New("smb") +}