-
Notifications
You must be signed in to change notification settings - Fork 2
/
multiply_test.go
100 lines (81 loc) · 1.51 KB
/
multiply_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
package matrix_test
import (
"testing"
"github.com/EclesioMeloJunior/matrix"
"github.com/stretchr/testify/assert"
)
func TestMulltiplyByScalar(t *testing.T) {
a := matrix.Matrix(
[][]int64{
{3, 3},
{2, 2},
},
)
multiplyBy := 3
expected := matrix.Matrix(
[][]int64{
{9, 9},
{6, 6},
},
)
a.MultplyByScalar(int64(multiplyBy))
assert.Equal(t, expected, a)
}
func TestMultiplyByVector(t *testing.T) {
rowVector := matrix.Matrix(
[][]int64{
{1, 2, 3},
},
)
colVector := matrix.Matrix(
[][]int64{
{1},
{2},
{3},
},
)
expected := matrix.Matrix([][]int64{{14}})
new, err := rowVector.Multiply(&colVector)
assert.NoError(t, err)
assert.Equal(t, new, &expected)
}
func TestMultiplyByMatrix(t *testing.T) {
a := matrix.Matrix(
[][]int64{
{2, 5, 9},
{3, 6, 8},
},
)
b := matrix.Matrix(
[][]int64{
{2, 7},
{4, 3},
{5, 2},
},
)
expected := matrix.Matrix(
[][]int64{
{69, 47},
{70, 55},
},
)
m, err := a.Multiply(&b)
assert.Nil(t, err)
assert.Equal(t, &expected, m)
}
func benchmarkMultiplyMatrix(ra, ca, rb, cb int, b *testing.B) {
for n := 0; n < b.N; n++ {
a := matrix.RandInt(ra, ca)
b := matrix.RandInt(rb, cb)
a.Multiply(b)
}
}
func BenchmarkMultiply3by5Times4by3(b *testing.B) {
benchmarkMultiplyMatrix(3, 5, 4, 3, b)
}
func BenchmarkMultiply10by20Times20by60(b *testing.B) {
benchmarkMultiplyMatrix(10, 20, 20, 60, b)
}
func BenchmarkMultiply100by200Times200by100(b *testing.B) {
benchmarkMultiplyMatrix(100, 200, 200, 100, b)
}