-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdense_test.go
173 lines (136 loc) · 3.78 KB
/
dense_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
package market
import (
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gonum.org/v1/gonum/mat"
)
var mtx10 = mat.NewDense(4, 5, []float64{
+0.944853346337906500,
+0.328601067704537230,
+0.011573183932084063,
-0.242048667319836990,
+0.885613734373423400,
-0.681501551465435000,
+0.812079966562488300,
-0.551271155078584300,
-0.000000000000000000,
-0.000000000000000000,
-0.000000000000000000,
-0.000000000000000000,
+0.207552675260212820,
-0.000000000000000000,
+0.669421525553018500,
+0.402696290353813800,
+0.756536462138819500,
+0.795981993703873700,
+0.419371027177237500,
+0.393836704188575100,
})
var mtx11 = mat.NewDense(5, 5, []float64{11, 0, 0, 0, 15, 0, 22, 23, 24, 0, 0, 23, 33, 0, 35, 0, 24, 0, 44, 0, 15, 0, 35, 0, 55})
var mtx12 = mat.NewDense(5, 5, []float64{0, 0, 0, 0, -15, 0, 0, -23, -24, 0, 0, 23, 0, 0, -35, 0, 24, 0, 0, 0, 15, 0, 35, 0, 0})
var mtx13 = mat.NewDense(4, 5, []float64{0, 0, 0, 0, 0, 0, -2, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 3, 0, 0})
var mtx14 = mtx11
var mtx15 = mtx12
func TestNewDense(t *testing.T) {
m := NewDense(mtx10)
assert.True(t, mat.Equal(m.ToMatrix(), mtx10))
}
func TestDenseToDense(t *testing.T) {
m := NewDense(mtx10)
assert.True(t, mat.Equal(m.ToDense(), mtx10))
}
func TestDenseToMatrix(t *testing.T) {
m := NewDense(mtx10)
assert.True(t, mat.Equal(m.ToMatrix(), mtx10))
}
func TestDenseMarshalTextTo(t *testing.T) {
var b strings.Builder
m := NewDense(mtx10)
_, err := m.MarshalTextTo(&b)
assert.Nil(t, err)
mm, err := ioutil.ReadFile(filepath.Join("testdata", "mmtype-10.mtx"))
assert.Nil(t, err)
assert.Equal(t, b.String(), string(mm))
}
func TestDenseMarshalText(t *testing.T) {
m := NewDense(mtx10)
mm1, err := m.MarshalText()
assert.Nil(t, err)
mm2, err := ioutil.ReadFile(filepath.Join("testdata", "mmtype-10.mtx"))
assert.Nil(t, err)
assert.Equal(t, string(mm1), string(mm2))
}
func TestDenseUnmarshalText(t *testing.T) {
b, err := ioutil.ReadFile(filepath.Join("testdata", "mmtype-10.mtx"))
assert.Nil(t, err)
var mm Dense
assert.Nil(t, mm.UnmarshalText(b))
if !mat.Equal(mm.ToMatrix(), mtx10) {
t.Errorf(
"\ngot:\n %v\nwant:\n %v\n",
mat.Formatted(mm.ToMatrix(), mat.Prefix(" "), mat.Squeeze()),
mat.Formatted(mtx10, mat.Prefix(" "), mat.Squeeze()),
)
}
}
func TestDenseUnmarshalTextFrom(t *testing.T) {
c := map[string]mat.Matrix{
"mmtype-10.mtx": mtx10, // real general
"mmtype-11.mtx": mtx11, // real symmetric
"mmtype-12.mtx": mtx12, // real skew-symmetric
"mmtype-13.mtx": mtx13, // integer general
"mmtype-14.mtx": mtx14, // integer symmetric
"mmtype-15.mtx": mtx15, // integer skew-symmetric
}
for k, v := range c {
f, _ := os.Open(filepath.Join("testdata", k))
defer f.Close()
var mm Dense
if _, err := mm.UnmarshalTextFrom(f); err != nil {
t.Errorf("%v", err)
}
if !mat.Equal(mm.ToMatrix(), v) {
t.Errorf(
"\ngot:\n %v\nwant:\n %v\n",
mat.Formatted(mm.ToMatrix(), mat.Prefix(" "), mat.Squeeze()),
mat.Formatted(v, mat.Prefix(" "), mat.Squeeze()),
)
}
}
}
func BenchmarkDenseMarshalTextTo(b *testing.B) {
for i := 1; i <= 1000; i *= 10 {
a := mat.NewDense(i, i, nil)
for j := 0; j < i*i; j++ {
a.Set(j%i, int(j/i), math.Sqrt(float64(j)))
}
m := NewDense(a)
b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
for k := 0; k < b.N; k++ {
m.MarshalTextTo(io.Discard)
}
})
}
}
func BenchmarkDenseUnmarshalTextFrom(b *testing.B) {
for i := 1; i <= 1000; i *= 10 {
a := mat.NewDense(i, i, nil)
for j := 0; j < i*i; j++ {
a.Set(j%i, int(j/i), math.Sqrt(float64(j)))
}
m := NewDense(a)
t, _ := m.MarshalText()
b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
for k := 0; k < b.N; k++ {
m.UnmarshalText(t)
}
})
}
}