Mercurial > hg > may
comparison yetilab/matrix/test/test_matrix.yeti @ 223:51af10e6cd0d
Merge from matrix_opaque_immutable branch
author | Chris Cannam |
---|---|
date | Sat, 11 May 2013 16:00:58 +0100 |
parents | 77c6a81c577f |
children | ac1373067054 |
comparison
equal
deleted
inserted
replaced
207:cd2caf235e1f | 223:51af10e6cd0d |
---|---|
1 | 1 |
2 module yetilab.matrix.test.test_matrix; | 2 module yetilab.matrix.test.test_matrix; |
3 | 3 |
4 mat = load yetilab.matrix.matrix; | 4 mat = load yetilab.matrix.matrix; |
5 block = load yetilab.block.block; | 5 vec = load yetilab.vector.vector; |
6 | |
7 load yetilab.vector.vectortype; | |
8 load yetilab.matrix.matrixtype; | |
6 | 9 |
7 import yeti.lang: FailureException; | 10 import yeti.lang: FailureException; |
8 | 11 |
9 { compare, compareUsing } = load yetilab.test.test; | 12 { compare, compareUsing } = load yetilab.test.test; |
10 | 13 |
14 (constMatrix n s = flipper (mat.constMatrix n s); | 17 (constMatrix n s = flipper (mat.constMatrix n s); |
15 zeroMatrix s = flipper (mat.zeroMatrix s); | 18 zeroMatrix s = flipper (mat.zeroMatrix s); |
16 randomMatrix s = flipper (mat.randomMatrix s); | 19 randomMatrix s = flipper (mat.randomMatrix s); |
17 identityMatrix s = flipper (mat.identityMatrix s); | 20 identityMatrix s = flipper (mat.identityMatrix s); |
18 generate f s = flipper (mat.generate f s); | 21 generate f s = flipper (mat.generate f s); |
19 newMatrix t d = flipper (mat.newMatrix t (map block.fromList d)); | 22 newMatrix t d = flipper (mat.newMatrix t (map vec.fromList d)); |
20 [ | 23 [ |
21 | 24 |
22 "constMatrixEmpty-\(name)": \( | 25 "constMatrixEmpty-\(name)": \( |
23 m = constMatrix 2 { rows = 0, columns = 0 }; | 26 m = constMatrix 2 { rows = 0, columns = 0 }; |
24 compare m.size { columns = 0, rows = 0 } | 27 compare (mat.size m) { columns = 0, rows = 0 } |
25 ), | 28 ), |
26 | 29 |
27 "constMatrixEmpty2-\(name)": \( | 30 "constMatrixEmpty2-\(name)": \( |
28 compare (constMatrix 2 { rows = 0, columns = 4 }).size { columns = 0, rows = 0 } and | 31 compare (mat.size (constMatrix 2 { rows = 0, columns = 4 })) { columns = 0, rows = 0 } and |
29 compare (constMatrix 2 { rows = 4, columns = 0 }).size { columns = 0, rows = 0 } | 32 compare (mat.size (constMatrix 2 { rows = 4, columns = 0 })) { columns = 0, rows = 0 } |
30 ), | 33 ), |
31 | 34 |
32 "constMatrix-\(name)": \( | 35 "constMatrix-\(name)": \( |
33 m = constMatrix 2 { rows = 3, columns = 4 }; | 36 m = constMatrix 2 { rows = 3, columns = 4 }; |
34 compare m.size { columns = 4, rows = 3 } and | 37 compare (mat.size m) { columns = 4, rows = 3 } and |
35 all id (map do row: compare (block.list (m.getRow row)) [2,2,2,2] done [0..2]) and | 38 all id (map do row: compare (vec.list (mat.getRow row m)) [2,2,2,2] done [0..2]) and |
36 all id (map do col: compare (block.list (m.getColumn col)) [2,2,2] done [0..3]) | 39 all id (map do col: compare (vec.list (mat.getColumn col m)) [2,2,2] done [0..3]) |
37 ), | 40 ), |
38 | 41 |
39 "randomMatrixEmpty-\(name)": \( | 42 "randomMatrixEmpty-\(name)": \( |
40 m = randomMatrix { rows = 0, columns = 0 }; | 43 m = randomMatrix { rows = 0, columns = 0 }; |
41 compare m.size { columns = 0, rows = 0 } | 44 compare (mat.size m) { columns = 0, rows = 0 } |
42 ), | 45 ), |
43 | 46 |
44 "randomMatrix-\(name)": \( | 47 "randomMatrix-\(name)": \( |
45 m = randomMatrix { rows = 3, columns = 4 }; | 48 m = randomMatrix { rows = 3, columns = 4 }; |
46 compare m.size { columns = 4, rows = 3 } | 49 compare (mat.size m) { columns = 4, rows = 3 } |
47 ), | 50 ), |
48 | 51 |
49 "zeroMatrixEmpty-\(name)": \( | 52 "zeroMatrixEmpty-\(name)": \( |
50 m = zeroMatrix { rows = 0, columns = 0 }; | 53 m = zeroMatrix { rows = 0, columns = 0 }; |
51 compare m.size { columns = 0, rows = 0 } | 54 compare (mat.size m) { columns = 0, rows = 0 } |
52 ), | 55 ), |
53 | 56 |
54 "zeroMatrix-\(name)": \( | 57 "zeroMatrix-\(name)": \( |
55 m = zeroMatrix { rows = 3, columns = 4 }; | 58 m = zeroMatrix { rows = 3, columns = 4 }; |
56 compare m.size { columns = 4, rows = 3 } and | 59 compare (mat.size m) { columns = 4, rows = 3 } and |
57 all id (map do row: compare (block.list (m.getRow row)) [0,0,0,0] done [0..2]) and | 60 all id (map do row: compare (vec.list (mat.getRow row m)) [0,0,0,0] done [0..2]) and |
58 all id (map do col: compare (block.list (m.getColumn col)) [0,0,0] done [0..3]) | 61 all id (map do col: compare (vec.list (mat.getColumn col m)) [0,0,0] done [0..3]) |
59 ), | 62 ), |
60 | 63 |
61 "identityMatrixEmpty-\(name)": \( | 64 "identityMatrixEmpty-\(name)": \( |
62 m = identityMatrix { rows = 0, columns = 0 }; | 65 m = identityMatrix { rows = 0, columns = 0 }; |
63 compare m.size { columns = 0, rows = 0 } | 66 compare (mat.size m) { columns = 0, rows = 0 } |
64 ), | 67 ), |
65 | 68 |
66 "identityMatrix-\(name)": \( | 69 "identityMatrix-\(name)": \( |
67 m = identityMatrix { rows = 3, columns = 4 }; | 70 m = identityMatrix { rows = 3, columns = 4 }; |
68 compare m.size { columns = 4, rows = 3 } and | 71 compare (mat.size m) { columns = 4, rows = 3 } and |
69 all id (map do row: compare (block.list (m.getRow row)) [1,1,1,1] done [0..2]) and | 72 all id (map do row: compare (vec.list (mat.getRow row m)) [1,1,1,1] done [0..2]) and |
70 all id (map do col: compare (block.list (m.getColumn col)) [1,1,1] done [0..3]) | 73 all id (map do col: compare (vec.list (mat.getColumn col m)) [1,1,1] done [0..3]) |
71 ), | 74 ), |
72 | 75 |
73 "generateEmpty-\(name)": \( | 76 "generateEmpty-\(name)": \( |
74 m = generate do row col: 0 done { rows = 0, columns = 0 }; | 77 m = generate do row col: 0 done { rows = 0, columns = 0 }; |
75 compare m.size { columns = 0, rows = 0 } | 78 compare (mat.size m) { columns = 0, rows = 0 } |
76 ), | 79 ), |
77 | 80 |
78 "generate-\(name)": \( | 81 "generate-\(name)": \( |
79 m = generate do row col: row * 10 + col done { rows = 2, columns = 3 }; | 82 m = generate do row col: row * 10 + col done { rows = 2, columns = 3 }; |
80 compare (block.list (m.getRow 0)) [0,1,2] and | 83 compare (vec.list (mat.getRow 0 m)) [0,1,2] and |
81 compare (block.list (m.getRow 1)) [10,11,12] | 84 compare (vec.list (mat.getRow 1 m)) [10,11,12] |
82 ), | 85 ), |
83 | 86 |
84 "widthAndHeight-\(name)": \( | 87 "widthAndHeight-\(name)": \( |
85 m = constMatrix 2 { rows = 3, columns = 4 }; | 88 m = constMatrix 2 { rows = 3, columns = 4 }; |
86 compare m.size { columns = mat.width m, rows = mat.height m } | 89 compare (mat.size m) { columns = mat.width m, rows = mat.height m } |
87 ), | 90 ), |
88 | 91 |
89 "equal-\(name)": \( | 92 "equal-\(name)": \( |
90 m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; | 93 m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; |
91 n = m; | 94 n = m; |
102 "getAt-\(name)": \( | 105 "getAt-\(name)": \( |
103 generator row col = row * 10 + col; | 106 generator row col = row * 10 + col; |
104 m = generate generator { rows = 2, columns = 3 }; | 107 m = generate generator { rows = 2, columns = 3 }; |
105 all id | 108 all id |
106 (map do row: all id | 109 (map do row: all id |
107 (map do col: m.getAt row col == generator row col done [0..2]) | 110 (map do col: mat.getAt row col m == generator row col done [0..2]) |
108 done [0..1]) | 111 done [0..1]) |
109 ), | 112 ), |
110 | 113 /*!!! |
111 "setAt-\(name)": \( | 114 "setAt-\(name)": \( |
112 generator row col = row * 10 + col; | 115 generator row col = row * 10 + col; |
113 m = generate generator { rows = 2, columns = 3 }; | 116 m = generate generator { rows = 2, columns = 3 }; |
114 m.setAt 1 2 16; | 117 mat.setAt 1 2 16 m; |
115 compare (m.getAt 1 2) 16 and | 118 compare (mat.getAt 1 2 m) 16 and |
116 compare (m.getAt 1 1) 11 and | 119 compare (mat.getAt 1 1 m) 11 and |
117 compare (m.getAt 0 2) 2 | 120 compare (mat.getAt 0 2 m) 2 |
118 ), | 121 ), |
119 | 122 |
120 "copyOfEqual-\(name)": \( | 123 "copyOfEqual-\(name)": \( |
121 m = constMatrix 2 { rows = 3, columns = 4 }; | 124 m = constMatrix 2 { rows = 3, columns = 4 }; |
122 m'' = mat.copyOf m; | 125 m'' = mat.copyOf m; |
125 | 128 |
126 "copyOfAlias-\(name)": \( | 129 "copyOfAlias-\(name)": \( |
127 m = constMatrix 2 { rows = 3, columns = 4 }; | 130 m = constMatrix 2 { rows = 3, columns = 4 }; |
128 m' = m; | 131 m' = m; |
129 m'' = mat.copyOf m; | 132 m'' = mat.copyOf m; |
130 m.setAt 0 0 6; | 133 mat.setAt 0 0 6 m; |
131 compareMatrices m' m and not mat.equal m m''; | 134 compareMatrices m' m and not mat.equal m m''; |
132 ), | 135 ), |
133 | 136 */ |
134 "transposedEmpty-\(name)": \( | 137 "transposedEmpty-\(name)": \( |
135 compare (mat.transposed (constMatrix 2 { rows = 0, columns = 0 })).size { columns = 0, rows = 0 } and | 138 compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 0 }))) { columns = 0, rows = 0 } and |
136 compare (mat.transposed (constMatrix 2 { rows = 0, columns = 4 })).size { columns = 0, rows = 0 } and | 139 compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 4 }))) { columns = 0, rows = 0 } and |
137 compare (mat.transposed (constMatrix 2 { rows = 4, columns = 0 })).size { columns = 0, rows = 0 } | 140 compare (mat.size (mat.transposed (constMatrix 2 { rows = 4, columns = 0 }))) { columns = 0, rows = 0 } |
138 ), | 141 ), |
139 | 142 |
140 "transposedSize-\(name)": \( | 143 "transposedSize-\(name)": \( |
141 compare (mat.transposed (constMatrix 2 { rows = 3, columns = 4 })).size { columns = 3, rows = 4 } | 144 compare (mat.size (mat.transposed (constMatrix 2 { rows = 3, columns = 4 }))) { columns = 3, rows = 4 } |
142 ), | 145 ), |
143 | 146 |
144 "transposed-\(name)": \( | 147 "transposed-\(name)": \( |
145 generator row col = row * 10 + col; | 148 generator row col = row * 10 + col; |
146 m = generate generator { rows = 2, columns = 3 }; | 149 m = generate generator { rows = 2, columns = 3 }; |
147 m' = mat.transposed m; | 150 m' = mat.transposed m; |
148 all id | 151 all id |
149 (map do row: all id | 152 (map do row: all id |
150 // like getAt test, but with col/row flipped | 153 // like getAt test, but with col/row flipped |
151 (map do col: m'.getAt col row == generator row col done [0..2]) | 154 (map do col: mat.getAt col row m' == generator row col done [0..2]) |
152 done [0..1]) | 155 done [0..1]) |
153 ), | 156 ), |
154 | 157 |
155 "transposed-back-\(name)": \( | 158 "transposed-back-\(name)": \( |
156 m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; | 159 m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; |
268 (newMatrix (ColumnMajor ()) []) | 271 (newMatrix (ColumnMajor ()) []) |
269 ), | 272 ), |
270 | 273 |
271 "asRows-\(name)": \( | 274 "asRows-\(name)": \( |
272 compare | 275 compare |
273 (map block.list | 276 (map vec.list |
274 (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) | 277 (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) |
275 [[1,2,3],[4,5,6]]; | 278 [[1,2,3],[4,5,6]]; |
276 ), | 279 ), |
277 | 280 |
278 "asColumns-\(name)": \( | 281 "asColumns-\(name)": \( |
279 compare | 282 compare |
280 (map block.list | 283 (map vec.list |
281 (mat.asColumns (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) | 284 (mat.asColumns (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) |
282 [[1,4],[2,5],[3,6]]; | 285 [[1,4],[2,5],[3,6]]; |
283 ), | 286 ), |
284 | 287 |
285 "concat-horiz-\(name)": \( | 288 "concat-horiz-\(name)": \( |