This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper_test.go
117 lines (106 loc) · 2.89 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gokalman
import (
"fmt"
"testing"
"github.com/gonum/matrix/mat64"
)
func Robot1DMatrices() (F, G mat64.Matrix, Δt float64) {
Δt = 0.1
F = mat64.NewDense(2, 2, []float64{1, Δt, 0, 1})
G = mat64.NewDense(2, 1, []float64{0.5 * Δt * Δt, Δt})
return
}
func Midterm2Matrices() (F, G mat64.Matrix, Δt float64) {
Δt = 0.01
F = mat64.NewDense(3, 3, []float64{1, 0.01, 5e-5, 0, 1, 0.01, 0, 0, 1})
G = mat64.NewDense(3, 1, []float64{(5e-7) / 3, 5e-5, 0.01})
return
}
func assertPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("code did not panic")
}
}()
f()
}
func TestIsNil(t *testing.T) {
if !IsNil(nil) {
t.Fatal("nil said to not be nil")
}
if IsNil(Identity(2)) {
t.Fatal("i22 said to be nil")
}
if !IsNil(mat64.NewSymDense(2, []float64{0, 0, 0, 0})) {
t.Fatal("zeros 4x4 said to NOT be nil")
}
}
func TestIdentity(t *testing.T) {
n := 3
i33s := Identity(n)
i33d := DenseIdentity(n)
for _, i33 := range []mat64.Matrix{i33s, i33d} {
if r, c := i33.Dims(); r != n || r != c {
t.Fatalf("i11 has dimensions (%dx%d)", r, c)
}
for i := 0; i < n; i++ {
if i33.At(i, i) != 1 {
t.Fatalf("i33(%d,%d) != 1", i, i)
}
for j := 0; j < n; j++ {
if i != j && i33.At(i, j) != 0 {
t.Fatalf("i33(%d,%d) != 0", i, j)
}
}
}
}
}
func TestAsSymDense(t *testing.T) {
d := mat64.NewDense(3, 3, []float64{1, 0.1, 2, 0.1, 3, 5, 2, 5, 7})
dsym, err := AsSymDense(d)
if err != nil {
t.Fatal("AsSymDense failed on 3x3")
}
r, c := d.Dims()
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if i == j {
continue
}
if dsym.At(i, j) != d.At(i, j) {
t.Fatalf("returned symmetric matrix invalid (%d,%d): %+v %+v", i, j, dsym, d)
}
}
}
_, err = AsSymDense(mat64.NewDense(3, 3, []float64{1, 0, 3, 0, 1, 0, 1, 2, 1}))
if err == nil {
t.Fatal("non symmetric matrix did not fail")
}
_, err = AsSymDense(mat64.NewDense(2, 3, []float64{1, 0, 1, 1, 2, 3}))
if err == nil {
t.Fatal("non square matrix did not fail")
}
}
func TestCheckDims(t *testing.T) {
i22 := Identity(2)
i33 := Identity(3)
methods := []DimensionAgreement{rows2cols, cols2rows, cols2cols, rows2rows, rowsAndcols}
for _, meth := range methods {
if err := checkMatDims(i22, i22, "i22", "i22", meth); err != nil {
t.Fatalf("method %+v fails: %s", meth, err)
}
if err := checkMatDims(i22, i33, "i22", "i33", meth); err == nil {
t.Fatalf("method %+v does not error when using i22 and i33 ", meth)
}
}
}
func TestHouseholderTransf(t *testing.T) {
A := mat64.NewDense(3, 3, []float64{1, -2, -1, 2, -1, 1, 1, 1, 2})
HouseholderTransf(A, 2, 1)
Apost := mat64.NewDense(3, 3, []float64{-2.449489742783178, 1.224744871391589, -1.2247448713915892, 0, -2.121320343559643, -2.121320343559643, 0, 0, 0})
if !mat64.EqualApprox(A, Apost, 1e-15) {
A.Sub(A, Apost)
fmt.Printf("Delta:\n%+v\n", mat64.Formatted(A))
t.Fatal("Householder fails")
}
}