/
main.coffee
98 lines (83 loc) · 1.25 KB
/
main.coffee
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
runTest = require 'ava'
clone = require 'clone'
Matrix = require '..'
runTest 'Build a matrix from colum-major arrays', (test) ->
matrix = Matrix.fromColums [
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
]
test.same matrix.toRows(), [
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
]
runTest 'Multiply a 3x2 matrix by a 2x3 matrix', (test) ->
matrix = Matrix.fromRows [
[1, 2, 3]
[4, 5, 6]
]
test.same(
matrix
.multiply [
[7, 8]
[9, 10]
[11, 12]
]
.toRows(),
[
[58, 64]
[139, 154]
]
)
runTest 'Multiply a 3x1 matrix by a 4x3 matrix', (test) ->
matrix = Matrix.fromRows [
[3, 4, 2]
]
test.same(
matrix.multiply([
[13, 9, 7, 15]
[8, 7, 4, 6]
[6, 4, 0, 3]
]).toRows(),
[
[83, 63, 37, 75]
]
)
runTest 'Multiply a 1x3 matrix by a 4x4 matrix', (test) ->
matrix = Matrix.fromRows [
[1, 0, 0, 7]
[0, 1, 0, 6]
[0, 0, 1, 8]
[0, 0, 0, 1]
]
test.same(
matrix.multiply([
[3]
[4]
[2]
[1]
]).toRows(),
[
[10]
[10]
[10]
[1]
]
)
runTest 'Create a matrix from a continuous array', (test) ->
matrix = Matrix.fromValues(
[1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1]
)
test.same(
matrix.toRows(),
[
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
]
)