Browse Source

added first test case for general nevile

Stephen McQuay 5 years ago
parent
commit
b761ad7ddc
3 changed files with 80 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 65
    0
      govector_test.go
  3. 14
    0
      interp.go

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
1 1
 tags
2
+.*.swp

+ 65
- 0
govector_test.go View File

@@ -1,6 +1,7 @@
1 1
 package govector
2 2
 
3 3
 import (
4
+	"errors"
4 5
 	"math"
5 6
 	"testing"
6 7
 )
@@ -648,3 +649,67 @@ func TestExtrapolate(t *testing.T) {
648 649
 		}
649 650
 	}
650 651
 }
652
+
653
+func TestNeville(t *testing.T) {
654
+	knownFails := []struct {
655
+		points   []Point2d
656
+		ts       []float64
657
+		t        float64
658
+		expected Point2d
659
+		err      error
660
+	}{
661
+		{
662
+			points: []Point2d{
663
+				{0, 0},
664
+				{1, 1},
665
+				{2, 2},
666
+			},
667
+			ts: []float64{
668
+				1.0 / 3.0,
669
+				2.0 / 3.0,
670
+			},
671
+			err: errors.New("Incompatable slice lengths len(P): 3 != len(ts): 2"),
672
+		},
673
+	}
674
+	for _, test := range knownFails {
675
+		p, err := Neville(test.points, test.ts, test.t)
676
+		if p != nil {
677
+			t.Errorf("p should've been nil: %+v", p)
678
+
679
+		}
680
+		if err.Error() != test.err.Error() {
681
+			t.Errorf("expected err: '%s', unexpectd err: '%s'", test.err, err)
682
+		}
683
+	}
684
+	tests := []struct {
685
+		points   []Point2d
686
+		ts       []float64
687
+		t        float64
688
+		expected Point2d
689
+	}{
690
+		{
691
+			points: []Point2d{
692
+				{0, 0},
693
+				{1, 1},
694
+				{2, 2},
695
+			},
696
+			ts: []float64{
697
+				0.0,
698
+				1.0 / 3.0,
699
+				2.0 / 3.0,
700
+			},
701
+			t:        1.0,
702
+			expected: Point2d{3, 3},
703
+		},
704
+	}
705
+	for _, test := range tests {
706
+		p, err := Neville(test.points, test.ts, test.t)
707
+		if err != nil {
708
+			t.Errorf("yeah, this should've been nil: %s", err)
709
+		}
710
+		if p == nil {
711
+		} else if *p != test.expected {
712
+			t.Errorf("wrong: expected: %+v, actual: %+v", test.expected, p)
713
+		}
714
+	}
715
+}

+ 14
- 0
interp.go View File

@@ -2,6 +2,7 @@ package govector
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"fmt"
5 6
 )
6 7
 
7 8
 // explicitly implements Neville's algorithm for three points at very
@@ -21,3 +22,16 @@ func Extrapolate(P []Point2d) (*Point2d, error) {
21 22
 	p012 := p01.ToVector().Scale(-1.0 / 2.0).ToPoint().Add(p12.ToVector().Scale(3.0 / 2.0))
22 23
 	return &p012, nil
23 24
 }
25
+
26
+func Neville(P []Point2d, ts []float64, t float64) (*Point2d, error) {
27
+	if len(P) != len(ts) {
28
+		return nil, errors.New(
29
+			fmt.Sprintf(
30
+				"Incompatable slice lengths len(P): %d != len(ts): %d",
31
+				len(P),
32
+				len(ts),
33
+			),
34
+		)
35
+	}
36
+	return nil, errors.New("smb")
37
+}

Loading…
Cancel
Save