-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrmsd_test.go
244 lines (213 loc) · 5.06 KB
/
rmsd_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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package structure
import (
"fmt"
"math/rand"
"testing"
"time"
matrix "github.com/skelterjohn/go.matrix"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func ExampleRmsd() {
// If you add a test, make sure you add a corresponding "RMSD: ..."
// to the output test at the end of this function.
tests := [][2][]Coords{
{
{
atom(-2.803, -15.373, 24.556),
atom(0.893, -16.062, 25.147),
atom(1.368, -12.371, 25.885),
atom(-1.651, -12.153, 28.177),
atom(-0.440, -15.218, 30.068),
atom(2.551, -13.273, 31.372),
atom(0.105, -11.330, 33.567),
},
{
atom(-14.739, -18.673, 15.040),
atom(-12.473, -15.810, 16.074),
atom(-14.802, -13.307, 14.408),
atom(-17.782, -14.852, 16.171),
atom(-16.124, -14.617, 19.584),
atom(-15.029, -11.037, 18.902),
atom(-18.577, -10.001, 17.996),
},
},
}
for _, test := range tests {
rms := RMSD(test[0], test[1])
fmt.Printf("RMSD: %f\n", rms)
rms = rmsd(test[0], test[1])
fmt.Printf("RMSD: %f\n", rms)
}
// Output:
// RMSD: 0.719106
// RMSD: 0.719106
}
func TestCovariant(t *testing.T) {
cols := 11
tests1 := randomMatrices(10000, 3, cols)
tests2 := randomMatrices(10000, 3, cols)
for i, test1 := range tests1 {
test2 := tests2[i]
// Compute our covariant
tC_ := covariant_3x3(cols, test1, test2)
tC := tmat(tC_[:])
// Now compute the "correct" covariant.
mat1 := matrix.MakeDenseMatrix(test1, 3, cols)
mat2 := matrix.MakeDenseMatrix(test2, 3, cols)
aC_, _ := mat1.TimesDense(mat2.Transpose())
aC := tmat(aC_.Array())
if !tC.equal(aC) {
t.Fatalf("The covariant of\n%s\nand\n%s\nis\n%s\nbut we said\n%s\n",
tmat(test1), tmat(test2), aC, tC)
}
}
}
func Test_3x3_times_3xN(t *testing.T) {
cols := 11
tests1 := randomMatrices(10000, 3, 3)
tests2 := randomMatrices(10000, 3, cols)
for i, test1 := range tests1 {
test2 := tests2[i]
// Compute our product.
tC_ := mult_3x3_3xN(cols, test1, test2)
tC := tmat(tC_[:])
// Now compute the "correct" product.
mat1 := matrix.MakeDenseMatrix(test1, 3, 3)
mat2 := matrix.MakeDenseMatrix(test2, 3, cols)
aC_, _ := mat1.TimesDense(mat2)
aC := tmat(aC_.Array())
if !tC.equal(aC) {
t.Fatalf("The product of\n%s\nand\n%s\nis\n%s\nbut we said\n%s\n",
tmat(test1), tmat(test2), aC, tC)
}
}
}
func TestSvd(t *testing.T) {
tests := randomMatrices3(10000)
for _, test := range tests {
// Compute our SVD.
tU_, tV_ := matrix3(test).svd()
tU, tV := tmat(tU_[:]), tmat(tV_[:])
// Now compute the "correct" SVD.
mat := matrix.MakeDenseMatrix(test[:], 3, 3)
U, _, V, _ := mat.SVD()
aU, aV := tmat(U.Array()), tmat(V.Array())
// Now compare them.
if !aU.equal(tU) {
t.Fatalf("With matrix\n%s\nU =\n%s\nbut we said\n%s\n",
tmat(test[:]), aU, tU)
}
if !aV.equal(tV) {
t.Fatalf("With matrix\n%s\n, V =\n%s\n, but we said\n%s\n",
tmat(test[:]), aV, tV)
}
}
}
func BenchmarkMySvd(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
test := matrix3(randomMatrix3())
b.StartTimer()
test.svd()
}
}
func BenchmarkGoMatrixSvd(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
test := randomMatrix3()
mat := matrix.MakeDenseMatrix(test[:], 3, 3)
b.StartTimer()
mat.SVD()
}
}
func BenchmarkMyRmsd(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
atoms1 := randomAtoms(11)
atoms2 := randomAtoms(11)
b.StartTimer()
rmsd(atoms1, atoms2)
}
}
func BenchmarkQCRmsd(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
atoms1 := randomAtoms(11)
atoms2 := randomAtoms(11)
b.StartTimer()
RMSD(atoms1, atoms2)
}
}
func BenchmarkQCRmsdMemory(b *testing.B) {
mem := NewMemory(11)
for i := 0; i < b.N; i++ {
b.StopTimer()
atoms1 := randomAtoms(11)
atoms2 := randomAtoms(11)
b.StartTimer()
RMSDMem(mem, atoms1, atoms2)
}
}
type tmat []float64
func (m tmat) String() string {
return fmt.Sprintf(`
|%f %f %f|
|%f %f %f|
|%f %f %f|
`, m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
}
func (m1 tmat) equal(m2 tmat) bool {
for i := 0; i < 9; i++ {
if m1[i] != m2[i] {
return false
}
}
return true
}
func randomMatrices3(cnt int) [][9]float64 {
ms := make([][9]float64, cnt)
for i := 0; i < cnt; i++ {
ms[i] = randomMatrix3()
}
return ms
}
func randomMatrix3() (m [9]float64) {
for i := 0; i < 9; i++ {
m[i] = rand.Float64() * float64(rand.Intn(100000))
}
return
}
func randomMatrices(cnt, rows, cols int) [][]float64 {
ms := make([][]float64, cnt)
for i := 0; i < cnt; i++ {
ms[i] = randomMatrix(rows, cols)
}
return ms
}
func randomMatrix(rows, cols int) (m []float64) {
m = make([]float64, rows*cols)
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
m[r*cols+c] = rand.Float64() * float64(rand.Intn(100000))
}
}
return
}
func randomAtoms(cnt int) []Coords {
atoms := make([]Coords, cnt)
for i := 0; i < cnt; i++ {
atoms[i] = randomAtom()
}
return atoms
}
func randomAtom() Coords {
return atom(
rand.Float64()*float64(rand.Intn(500)),
rand.Float64()*float64(rand.Intn(500)),
rand.Float64()*float64(rand.Intn(500)))
}
func atom(x, y, z float64) Coords {
return Coords{x, y, z}
}