added first test case for general nevile
This commit is contained in:
parent
1ed4830d51
commit
b761ad7ddc
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
tags
|
||||
.*.swp
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
interp.go
14
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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user