-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvd_test.go
More file actions
165 lines (151 loc) · 3.55 KB
/
Copy pathsvd_test.go
File metadata and controls
165 lines (151 loc) · 3.55 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package svd
import (
"fmt"
"math/cmplx"
"math/rand"
"testing"
)
// conj returns the complex conjugate of a matrix.
func conj(A [][]complex128) [][]complex128 {
m := len(A)
n := len(A[0])
B := make([][]complex128, n)
for i := 0; i < n; i++ {
B[i] = make([]complex128, m)
for k := 0; k < m; k++ {
B[i][k] = cmplx.Conj(A[k][i])
}
}
return B
}
// mul multiplies two matrices and returns the result.
func mul(A [][]complex128, B [][]complex128) [][]complex128 {
m := len(A)
n := len(A[0])
if len(B) != n {
panic("inner matrix dimensions mismatch")
}
l := len(B[0])
C := make([][]complex128, m)
for i := range C {
C[i] = make([]complex128, l)
for k := 0; k < l; k++ {
C[i][k] = dot(A, B, i, k, n)
}
}
return C
}
// dot returns the scalar product of two vectors from matrices A[i][*] and B[*][k], with inner dimension n.
func dot(A, B [][]complex128, i, k, n int) complex128 {
var c complex128
for j := 0; j < n; j++ {
c += A[i][j] * B[j][k]
}
return c
}
// diag returns a matrix with the given vector as the diagonal and zeros otherwise.
func diag(v []float64) [][]complex128 {
A := make([][]complex128, len(v))
for i := range A {
A[i] = make([]complex128, len(v))
A[i][i] = complex(v[i], 0)
}
return A
}
// conjdot returns the scalar product of two vectors, where the first is conjugated.
func conjdot(a, b []complex128) complex128 {
var sum complex128
if len(a) != len(b) {
panic("conjdot: input vector sizes don't match")
}
for i := range a {
sum += cmplx.Conj(a[i]) * b[i]
}
return sum
}
// compare tests if two matrices are similar.
func compare(A, B [][]complex128) (err error, maxerr float64) {
eps := 1.0E-12
if len(A) != len(B) {
return fmt.Errorf("matrix dimensions differ"), 0
}
if len(A) < 1 {
return fmt.Errorf("empty matrix"), 0
}
n := len(A[0])
for i := range A {
if len(A[i]) != n || len(B[i]) != n {
return fmt.Errorf("matrix dimensions differ"), 0
}
for k := range A[i] {
if e := cmplx.Abs(A[i][k] - B[i][k]); e > eps {
return fmt.Errorf("matrix differs by: %v", e), 0
} else if e > maxerr {
maxerr = e
}
}
}
return nil, maxerr
}
// column returns a column from a matrix.
func column(A [][]complex128, col int) []complex128 {
v := make([]complex128, len(A))
for i := range A {
v[i] = A[i][col]
}
return v
}
// isunitary tests if a matrix is unitary.
func isunitary(A [][]complex128) (err error, maxerr float64) {
eps := 1.0E-12
for i := range A[0] {
a := column(A, i)
for k := range A[0] {
b := column(A, k)
s := conjdot(a, b)
target := complex(0.0, 0.0)
if i == k {
target = complex(1.0, 0.0)
}
if e := cmplx.Abs(s - target); e > eps {
fmt.Println(i, k, s, target)
return fmt.Errorf("unitary fails with element difference: %v", e), 0
} else if e > maxerr {
maxerr = e
}
}
}
return nil, maxerr
}
func TestSvd(t *testing.T) {
// Create a random matrix.
m := 10
n := 4
A := make([][]complex128, m)
for i := range A {
A[i] = make([]complex128, n)
for k := range A[i] {
A[i][k] = complex(rand.NormFloat64(), rand.NormFloat64())
}
}
// Do the singular value decomposition.
S, err := New(A)
if err != nil {
t.Fatal(err)
}
// Compare the original with the product of the resulting matrices.
// Check if the product is similar to the original.
E := diag(S.S)
P := mul(mul(S.U, E), conj(S.V))
if err, _ := compare(A, P); err != nil {
t.Fatal(err)
}
// Check for unitarity of U.
if err, _ := isunitary(S.U); err != nil {
t.Fatal(err)
}
// Check for unitarity of V.
if err, _ := isunitary(S.V); err != nil {
t.Fatal(err)
}
}