-
Notifications
You must be signed in to change notification settings - Fork 0
/
posev_test.go
56 lines (49 loc) · 1.16 KB
/
posev_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
package posev
import (
"github.com/mitsuse/matrix-go/dense"
//~ "log"
"math"
"testing"
)
func TestPowerTopEigen(t *testing.T) {
eps := float64(0.00001)
a := dense.New(3, 3)(
-261, 209, -49,
-530, 422, -98,
-800, 631, -144,
)
v, e := PowerTopEigen(a, 40, eps)
eps = float64(0.001)
if math.Abs(e-float64(10)) > eps {
t.Errorf("Top eigenvector failed: %f", e)
}
if d := DeltaEigen(e, v, a); d > eps {
t.Errorf("Eigenvector check failed: %f", d)
}
}
func TestPowerTopSingular(t *testing.T) {
eps := float64(0.00001)
b := dense.New(4, 5)(
1, 0, 0, 0, 2,
0, 0, 3, 0, 0,
0, 0, 0, 0, 0,
0, 4, 0, 0, 0,
)
c := []float64{4, 3, math.Sqrt(5), 0}
u, v, s := PowerTopKSingular(b, 4, 40, eps)
m, n := b.Shape()
eps = float64(0.001)
for i := 0; i < 4; i++ {
if math.Abs(s[i]-c[i]) > eps {
t.Errorf("Top %d singular value failed: %f", i, s[i])
}
ui := u.View(0, i, m, 1)
vi := v.View(0, i, n, 1)
if d := DeltaSingular(s[i], ui, vi, b); d > eps {
t.Errorf("Left %d singular vector check failed: %f", i, d)
}
if d := DeltaSingular(s[i], vi, ui, b.Transpose()); d > eps {
t.Errorf("Right %d singular vector check failed: %f", i, d)
}
}
}