To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / src / may / matrix / test / test_matrix.yeti @ 591:eb27901664cd
History | View | Annotate | Download (27.6 KB)
| 1 |
|
|---|---|
| 2 |
module may.matrix.test.test_matrix; |
| 3 |
|
| 4 |
mat = load may.matrix; |
| 5 |
vec = load may.vector; |
| 6 |
|
| 7 |
import yeti.lang: FailureException; |
| 8 |
|
| 9 |
{ compare, compareUsing } = load may.test;
|
| 10 |
|
| 11 |
compareMatrices = compareUsing mat.equal; |
| 12 |
|
| 13 |
makeTests name flipper = |
| 14 |
(constMatrix n s = flipper (mat.constMatrix n s); |
| 15 |
zeroMatrix s = flipper (mat.zeroMatrix s); |
| 16 |
randomMatrix s = flipper (mat.randomMatrix s); |
| 17 |
identityMatrix s = flipper (mat.identityMatrix s); |
| 18 |
generate f s = flipper (mat.generate f s); |
| 19 |
newMatrix s data = flipper (mat.newMatrix s data); |
| 20 |
fromRows d = flipper (mat.fromRows (map vec.fromList d)); |
| 21 |
fromColumns d = flipper (mat.fromColumns (map vec.fromList d)); |
| 22 |
[ |
| 23 |
|
| 24 |
"constMatrixEmpty-\(name)": \( |
| 25 |
m = constMatrix 2 { rows = 0, columns = 0 };
|
| 26 |
compare (mat.size m) { columns = 0, rows = 0 }
|
| 27 |
), |
| 28 |
|
| 29 |
"constMatrixEmpty2-\(name)": \( |
| 30 |
compare (mat.size (constMatrix 2 { rows = 0, columns = 4 })) { columns = 4, rows = 0 } and
|
| 31 |
compare (mat.size (constMatrix 2 { rows = 4, columns = 0 })) { columns = 0, rows = 4 } and
|
| 32 |
compare (vec.list (mat.getColumn 2 (constMatrix 2 { rows = 0, columns = 4 }))) [] and
|
| 33 |
compare (map vec.list (mat.asRows (constMatrix 2 { rows = 4, columns = 0 }))) [[],[],[],[]] and
|
| 34 |
compare (vec.list (mat.getRow 2 (constMatrix 2 { rows = 3, columns = 0 }))) []
|
| 35 |
), |
| 36 |
|
| 37 |
"constMatrix-\(name)": \( |
| 38 |
m = constMatrix 2 { rows = 3, columns = 4 };
|
| 39 |
compare (mat.size m) { columns = 4, rows = 3 } and
|
| 40 |
all id (map do row: compare (vec.list (mat.getRow row m)) [2,2,2,2] done [0..2]) and |
| 41 |
all id (map do col: compare (vec.list (mat.getColumn col m)) [2,2,2] done [0..3]) |
| 42 |
), |
| 43 |
|
| 44 |
"randomMatrixEmpty-\(name)": \( |
| 45 |
m = randomMatrix { rows = 0, columns = 0 };
|
| 46 |
compare (mat.size m) { columns = 0, rows = 0 }
|
| 47 |
), |
| 48 |
|
| 49 |
"randomMatrix-\(name)": \( |
| 50 |
m = randomMatrix { rows = 3, columns = 4 };
|
| 51 |
compare (mat.size m) { columns = 4, rows = 3 }
|
| 52 |
), |
| 53 |
|
| 54 |
"zeroMatrixEmpty-\(name)": \( |
| 55 |
m = zeroMatrix { rows = 0, columns = 0 };
|
| 56 |
compare (mat.size m) { columns = 0, rows = 0 }
|
| 57 |
), |
| 58 |
|
| 59 |
"zeroMatrix-\(name)": \( |
| 60 |
m = zeroMatrix { rows = 3, columns = 4 };
|
| 61 |
compare (mat.size m) { columns = 4, rows = 3 } and
|
| 62 |
all id (map do row: compare (vec.list (mat.getRow row m)) [0,0,0,0] done [0..2]) and |
| 63 |
all id (map do col: compare (vec.list (mat.getColumn col m)) [0,0,0] done [0..3]) |
| 64 |
), |
| 65 |
|
| 66 |
"identityMatrixEmpty-\(name)": \( |
| 67 |
m = identityMatrix { rows = 0, columns = 0 };
|
| 68 |
compare (mat.size m) { columns = 0, rows = 0 }
|
| 69 |
), |
| 70 |
|
| 71 |
"identityMatrix-\(name)": \( |
| 72 |
m = identityMatrix { rows = 3, columns = 4 };
|
| 73 |
compare (mat.size m) { columns = 4, rows = 3 } and
|
| 74 |
all id (map do row: compare (vec.list (mat.getRow row m)) [1,1,1,1] done [0..2]) and |
| 75 |
all id (map do col: compare (vec.list (mat.getColumn col m)) [1,1,1] done [0..3]) |
| 76 |
), |
| 77 |
|
| 78 |
"generateEmpty-\(name)": \( |
| 79 |
m = generate do row col: 6 done { rows = 0, columns = 0 };
|
| 80 |
compare (mat.size m) { columns = 0, rows = 0 }
|
| 81 |
), |
| 82 |
|
| 83 |
"generateEmpty2-\(name)": \( |
| 84 |
m = generate do row col: 6 done { rows = 4, columns = 0 };
|
| 85 |
compare (mat.size m) { columns = 0, rows = 4 }
|
| 86 |
), |
| 87 |
|
| 88 |
"generate-\(name)": \( |
| 89 |
m = generate do row col: row * 10 + col done { rows = 2, columns = 3 };
|
| 90 |
compare (vec.list (mat.getRow 0 m)) [0,1,2] and |
| 91 |
compare (vec.list (mat.getRow 1 m)) [10,11,12] |
| 92 |
), |
| 93 |
|
| 94 |
"widthAndHeight-\(name)": \( |
| 95 |
m = constMatrix 2 { rows = 3, columns = 4 };
|
| 96 |
compare (mat.size m) { columns = mat.width m, rows = mat.height m }
|
| 97 |
), |
| 98 |
|
| 99 |
"newMatrix-rows-\(name)": \( |
| 100 |
rowc rr = Rows (map vec.fromList rr); |
| 101 |
m1 = newMatrix { rows = 0, columns = 0 } (rowc []);
|
| 102 |
m2 = newMatrix { rows = 2, columns = 0 } (rowc [[],[]]);
|
| 103 |
m3 = newMatrix { rows = 3, columns = 2 } (rowc [[1,2],[3,4],[5,6]]);
|
| 104 |
compare (mat.size m1) { rows = 0, columns = 0 } and
|
| 105 |
compare (mat.size m2) { rows = 2, columns = 0 } and
|
| 106 |
compare (mat.size m3) { rows = 3, columns = 2 } and
|
| 107 |
compareMatrices m3 (fromRows [[1,2],[3,4],[5,6]]); |
| 108 |
), |
| 109 |
|
| 110 |
"newMatrix-columns-\(name)": \( |
| 111 |
colc cc = Columns (map vec.fromList cc); |
| 112 |
m1 = newMatrix { rows = 0, columns = 0 } (colc []);
|
| 113 |
m2 = newMatrix { rows = 0, columns = 2 } (colc [[],[]]);
|
| 114 |
m3 = newMatrix { rows = 2, columns = 3 } (colc [[1,2],[3,4],[5,6]]);
|
| 115 |
compare (mat.size m1) { rows = 0, columns = 0 } and
|
| 116 |
compare (mat.size m2) { rows = 0, columns = 2 } and
|
| 117 |
compare (mat.size m3) { rows = 2, columns = 3 } and
|
| 118 |
compareMatrices m3 (fromColumns [[1,2],[3,4],[5,6]]); |
| 119 |
), |
| 120 |
|
| 121 |
"newMatrix-fail-\(name)": \( |
| 122 |
rowc rr = Rows (map vec.fromList rr); |
| 123 |
colc cc = Columns (map vec.fromList cc); |
| 124 |
all id |
| 125 |
(map do t: |
| 126 |
try |
| 127 |
\() (t ()); |
| 128 |
false; |
| 129 |
catch FailureException e: |
| 130 |
true; |
| 131 |
yrt |
| 132 |
done |
| 133 |
[ |
| 134 |
\(newMatrix { rows = 0, columns = 0 } (rowc [[]])),
|
| 135 |
\(newMatrix { rows = 0, columns = 0 } (colc [[]])),
|
| 136 |
\(newMatrix { rows = 2, columns = 0 } (rowc [[]])),
|
| 137 |
\(newMatrix { rows = 0, columns = 2 } (colc [[]])),
|
| 138 |
\(newMatrix { rows = 2, columns = 2 } (rowc [[],[]])),
|
| 139 |
\(newMatrix { rows = 2, columns = 2 } (colc [[],[]])),
|
| 140 |
\(newMatrix { rows = 2, columns = 3 } (rowc [[1,2],[3,4,5]])),
|
| 141 |
\(newMatrix { rows = 3, columns = 2 } (colc [[1,2],[3,4,5]])),
|
| 142 |
\(newMatrix { rows = 2, columns = 3 } (rowc [[1,2,3],[4,5]])),
|
| 143 |
\(newMatrix { rows = 3, columns = 2 } (colc [[1,2,3],[4,5]])),
|
| 144 |
\(newMatrix { rows = 2, columns = 3 } (rowc [[1,2,3],[4,5,6],[7,8,9]])),
|
| 145 |
\(newMatrix { rows = 3, columns = 2 } (colc [[1,2,3],[4,5,6],[7,8,9]])),
|
| 146 |
\(newMatrix { rows = 2, columns = 3 } (rowc [[1,2,3]])),
|
| 147 |
\(newMatrix { rows = 3, columns = 2 } (colc [[1,2,3]]))
|
| 148 |
]); |
| 149 |
), |
| 150 |
|
| 151 |
"equal-\(name)": \( |
| 152 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 153 |
n = m; |
| 154 |
p = fromRows [[1,0,3],[4,5,6]]; |
| 155 |
q = fromColumns [[1,0,3],[4,5,6]]; |
| 156 |
r = fromColumns [[1,4],[0,5]]; |
| 157 |
compareMatrices m n and |
| 158 |
compareMatrices m p and |
| 159 |
compareMatrices n p and |
| 160 |
not mat.equal m q and |
| 161 |
not mat.equal m r |
| 162 |
), |
| 163 |
|
| 164 |
"equalUnder-\(name)": \( |
| 165 |
p = fromColumns [[1,2,3],[4,5,6]]; |
| 166 |
q = fromColumns [[1,2,3],[4,5,6]]; |
| 167 |
r = fromColumns [[4,3,1],[3,1,2]]; |
| 168 |
s = fromColumns [[1,4,5],[6,7,8]]; |
| 169 |
t = fromColumns [[1,4,5],[6,7,9]]; |
| 170 |
mat.equalUnder (==) p p and |
| 171 |
mat.equalUnder (==) p q and |
| 172 |
mat.equalUnder (!=) p r and |
| 173 |
mat.equalUnder do a b: a % 2 == b % 2 done p s and |
| 174 |
not mat.equalUnder do a b: a % 2 == b % 2 done p t |
| 175 |
), |
| 176 |
|
| 177 |
"at-\(name)": \( |
| 178 |
generator row col = row * 10 + col; |
| 179 |
m = generate generator { rows = 2, columns = 3 };
|
| 180 |
all id |
| 181 |
(map do row: all id |
| 182 |
(map do col: mat.at m row col == generator row col done [0..2]) |
| 183 |
done [0..1]) |
| 184 |
), |
| 185 |
|
| 186 |
"transposedEmpty-\(name)": \( |
| 187 |
compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 0 }))) { columns = 0, rows = 0 } and
|
| 188 |
compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 4 }))) { columns = 0, rows = 4 } and
|
| 189 |
compare (mat.size (mat.transposed (constMatrix 2 { rows = 4, columns = 0 }))) { columns = 4, rows = 0 }
|
| 190 |
), |
| 191 |
|
| 192 |
"transposedSize-\(name)": \( |
| 193 |
compare (mat.size (mat.transposed (constMatrix 2 { rows = 3, columns = 4 }))) { columns = 3, rows = 4 }
|
| 194 |
), |
| 195 |
|
| 196 |
"transposed-\(name)": \( |
| 197 |
generator row col = row * 10 + col; |
| 198 |
m = generate generator { rows = 2, columns = 3 };
|
| 199 |
m' = mat.transposed m; |
| 200 |
all id |
| 201 |
(map do row: all id |
| 202 |
// like at test, but with col/row flipped |
| 203 |
(map do col: mat.at m' col row == generator row col done [0..2]) |
| 204 |
done [0..1]) |
| 205 |
), |
| 206 |
|
| 207 |
"transposed-back-\(name)": \( |
| 208 |
m = fromColumns [[1,4],[2,5],[3,6]]; |
| 209 |
compareMatrices m (mat.transposed (mat.transposed m)) and |
| 210 |
not mat.equal m (mat.transposed m); |
| 211 |
), |
| 212 |
|
| 213 |
"flipped-\(name)": \( |
| 214 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 215 |
m' = mat.flipped m; |
| 216 |
m'' = fromRows [[1,0,3],[4,5,6]]; |
| 217 |
compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m''; |
| 218 |
), |
| 219 |
|
| 220 |
"flipped-back-\(name)": \( |
| 221 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 222 |
compareMatrices m (mat.flipped (mat.flipped m)); |
| 223 |
), |
| 224 |
|
| 225 |
"flipped-empty-\(name)": \( |
| 226 |
m = constMatrix 2 { rows = 0, columns = 4 };
|
| 227 |
compareMatrices (mat.flipped m) (mat.flipped (constMatrix 0 { rows = 0, columns = 4 }));
|
| 228 |
), |
| 229 |
|
| 230 |
"toRowMajor-\(name)": \( |
| 231 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 232 |
m' = mat.toRowMajor m; |
| 233 |
m'' = fromRows [[1,0,3],[4,5,6]]; |
| 234 |
m''' = mat.toRowMajor m''; |
| 235 |
compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m'' |
| 236 |
and compareMatrices m m'''; |
| 237 |
), |
| 238 |
|
| 239 |
"toColumnMajor-\(name)": \( |
| 240 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 241 |
m' = mat.toColumnMajor m; |
| 242 |
m'' = fromColumns [[1,0,3],[4,5,6]]; |
| 243 |
m''' = mat.toColumnMajor m''; |
| 244 |
compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m'' |
| 245 |
and compareMatrices m m'''; |
| 246 |
), |
| 247 |
|
| 248 |
"diagonal-\(name)": \( |
| 249 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 250 |
compare (vec.list (mat.getDiagonal 0 m)) [1,5] and |
| 251 |
compare (vec.list (mat.getDiagonal 1 m)) [4] and |
| 252 |
compare (vec.list (mat.getDiagonal (-1) m)) [0,6] |
| 253 |
), |
| 254 |
|
| 255 |
"scaled-\(name)": \( |
| 256 |
compareMatrices |
| 257 |
(mat.scaled 0.5 (constMatrix 2 { rows = 3, columns = 4 }))
|
| 258 |
(constMatrix 1 { rows = 3, columns = 4 }) and
|
| 259 |
compareMatrices |
| 260 |
(mat.scaled 0.5 (constMatrix (-3) { rows = 3, columns = 4 }))
|
| 261 |
(constMatrix (-1.5) { rows = 3, columns = 4 }) and
|
| 262 |
compareMatrices |
| 263 |
(mat.scaled 0.5 (constMatrix 2 { rows = 0, columns = 2 }))
|
| 264 |
(constMatrix 5 { rows = 0, columns = 2 })
|
| 265 |
), |
| 266 |
|
| 267 |
"mapRows-\(name)": \( |
| 268 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 269 |
m' = fromColumns [[1,0,3],[4,5,6]]; |
| 270 |
m'' = fromRows [[2,8],[0,10],[6,12]]; |
| 271 |
compareMatrices (mat.mapRows (vec.scaled 2) m) m'' and |
| 272 |
compareMatrices (mat.mapRows (vec.scaled 2) m') m'' |
| 273 |
), |
| 274 |
|
| 275 |
"mapRows-extend-\(name)": \( |
| 276 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 277 |
m' = mat.mapRows do r: vec.concat [r, r] done m; |
| 278 |
compareMatrices m' (mat.concatHorizontal [ m, m ]) |
| 279 |
), |
| 280 |
|
| 281 |
"mapRows-shrink-\(name)": \( |
| 282 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 283 |
m' = mat.mapRows do r: vec.resizedTo 1 r done m; |
| 284 |
compareMatrices m' (fromRows [[1],[0],[3]]); |
| 285 |
), |
| 286 |
|
| 287 |
"mapRows-fail-\(name)": \( |
| 288 |
try |
| 289 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 290 |
\() (mat.mapRows |
| 291 |
do r: |
| 292 |
if vec.at r 0 == 1 |
| 293 |
then vec.concat [r, r] |
| 294 |
else vec.resizedTo 1 r |
| 295 |
fi |
| 296 |
done m); |
| 297 |
false |
| 298 |
catch FailureException e: |
| 299 |
true |
| 300 |
yrt |
| 301 |
), |
| 302 |
|
| 303 |
"mapColumns-\(name)": \( |
| 304 |
m = fromRows [[1,4],[0,5],[3,6]]; |
| 305 |
m' = fromColumns [[1,0,3],[4,5,6]]; |
| 306 |
m'' = fromRows [[2,8],[0,10],[6,12]]; |
| 307 |
compareMatrices (mat.mapColumns (vec.scaled 2) m) m'' and |
| 308 |
compareMatrices (mat.mapColumns (vec.scaled 2) m') m'' |
| 309 |
), |
| 310 |
|
| 311 |
"mapColumns-extend-\(name)": \( |
| 312 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 313 |
m' = mat.mapColumns do r: vec.concat [r, r] done m; |
| 314 |
compareMatrices m' (mat.concatVertical [ m, m ]) |
| 315 |
), |
| 316 |
|
| 317 |
"mapColumns-shrink-\(name)": \( |
| 318 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 319 |
m' = mat.mapColumns do r: vec.resizedTo 1 r done m; |
| 320 |
compareMatrices m' (fromColumns [[1],[0],[3]]); |
| 321 |
), |
| 322 |
|
| 323 |
"mapColumns-fail-\(name)": \( |
| 324 |
try |
| 325 |
m = fromColumns [[1,4],[0,5],[3,6]]; |
| 326 |
\() (mat.mapColumns |
| 327 |
do r: |
| 328 |
if vec.at r 0 == 1 |
| 329 |
then vec.concat [r, r] |
| 330 |
else vec.resizedTo 1 r |
| 331 |
fi |
| 332 |
done m); |
| 333 |
false |
| 334 |
catch FailureException e: |
| 335 |
true |
| 336 |
yrt |
| 337 |
), |
| 338 |
|
| 339 |
"minValue-\(name)": \( |
| 340 |
compare (mat.minValue (fromRows [[1,2],[3,4],[5,-1]])) (-1) and |
| 341 |
compare (mat.minValue (fromRows [[1,2],[3,0],[5,-1]])) (-1) and |
| 342 |
compare (mat.minValue (fromRows [[1,2],[3,0],[5,1]])) 0 and |
| 343 |
compare (mat.minValue (fromRows [[],[],[]])) 0 |
| 344 |
), |
| 345 |
|
| 346 |
"maxValue-\(name)": \( |
| 347 |
compare (mat.maxValue (fromRows [[1,2],[3,4],[5,-1]])) 5 and |
| 348 |
compare (mat.maxValue (fromRows [[1,2],[3,0],[5,-1]])) 5 and |
| 349 |
compare (mat.maxValue (fromRows [[-1,-2],[-3,0],[-5,-1]])) 0 and |
| 350 |
compare (mat.maxValue (fromRows [[-1,-2],[-3,-4],[-5,-1]])) (-1) and |
| 351 |
compare (mat.maxValue (fromRows [[],[],[]])) 0 |
| 352 |
), |
| 353 |
|
| 354 |
"total-\(name)": \( |
| 355 |
compare (mat.total (fromRows [[1,2],[3,4],[5,-1]])) 14 and |
| 356 |
compare (mat.total (fromRows [[1,2],[3,0],[5,-1]])) 10 and |
| 357 |
compare (mat.total (fromRows [[-1,-2],[-3,0],[-5,-1]])) (-12) and |
| 358 |
compare (mat.total (fromRows [[-1,-2],[-3,-4],[-5,-1]])) (-16) and |
| 359 |
compare (mat.total (fromRows [[],[],[]])) 0 |
| 360 |
), |
| 361 |
|
| 362 |
"sum-\(name)": \( |
| 363 |
compareMatrices |
| 364 |
(mat.sum [constMatrix 2 { rows = 3, columns = 4 },
|
| 365 |
constMatrix 1 { rows = 3, columns = 4 }])
|
| 366 |
(constMatrix 3 { rows = 3, columns = 4 }) and
|
| 367 |
compareMatrices |
| 368 |
(mat.sum [constMatrix 2 { rows = 3, columns = 4 },
|
| 369 |
constMatrix 1 { rows = 3, columns = 4 },
|
| 370 |
fromRows [[1,2,3,4],[5,6,7,8],[9,10,11,12]]]) |
| 371 |
(fromRows [[4,5,6,7],[8,9,10,11],[12,13,14,15]]) |
| 372 |
), |
| 373 |
|
| 374 |
"sumFail-\(name)": \( |
| 375 |
try |
| 376 |
\() (mat.sum [constMatrix 2 { rows = 3, columns = 4 },
|
| 377 |
constMatrix 1 { rows = 3, columns = 5 }]);
|
| 378 |
false; |
| 379 |
catch FailureException e: |
| 380 |
true |
| 381 |
yrt |
| 382 |
), |
| 383 |
|
| 384 |
"sparseSum-\(name)": \( |
| 385 |
s = mat.newSparseMatrix { rows = 2, columns = 3 }
|
| 386 |
(Columns [ |
| 387 |
{ i = 0, j = 0, v = 1 },
|
| 388 |
{ i = 0, j = 2, v = 2 },
|
| 389 |
{ i = 1, j = 1, v = 4 },
|
| 390 |
]); |
| 391 |
t = mat.newSparseMatrix { rows = 2, columns = 3 }
|
| 392 |
(Columns [ |
| 393 |
{ i = 0, j = 1, v = 7 },
|
| 394 |
{ i = 1, j = 0, v = 5 },
|
| 395 |
{ i = 1, j = 1, v = -4 }, // NB this means [1,1] -> 0, sparse zero
|
| 396 |
]); |
| 397 |
tot = mat.sum [s, t]; |
| 398 |
mat.isSparse? tot and |
| 399 |
compareMatrices tot (mat.sum [mat.toDense s, t]) and |
| 400 |
compareMatrices tot (mat.sum [mat.toDense s, mat.toDense t]) and |
| 401 |
compareMatrices tot (mat.sum [s, mat.toDense t]) and |
| 402 |
compareMatrices tot |
| 403 |
(mat.newSparseMatrix { rows = 2, columns = 3 } (Rows [
|
| 404 |
{ i = 0, j = 0, v = 1 },
|
| 405 |
{ i = 0, j = 1, v = 7 },
|
| 406 |
{ i = 0, j = 2, v = 2 },
|
| 407 |
{ i = 1, j = 0, v = 5 },
|
| 408 |
])) and |
| 409 |
compare (mat.density tot) (4/6) |
| 410 |
), |
| 411 |
|
| 412 |
"difference-\(name)": \( |
| 413 |
compareMatrices |
| 414 |
(mat.difference (constMatrix 2 { rows = 3, columns = 4 })
|
| 415 |
(constMatrix 1 { rows = 3, columns = 4 }))
|
| 416 |
(constMatrix 1 { rows = 3, columns = 4 })
|
| 417 |
), |
| 418 |
|
| 419 |
"differenceFail-\(name)": \( |
| 420 |
try |
| 421 |
\() (mat.difference (constMatrix 2 { rows = 3, columns = 4 })
|
| 422 |
(constMatrix 1 { rows = 3, columns = 5 }));
|
| 423 |
false; |
| 424 |
catch FailureException e: |
| 425 |
true |
| 426 |
yrt |
| 427 |
), |
| 428 |
|
| 429 |
"sparseDifference-\(name)": \( |
| 430 |
s = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 431 |
{ i = 0, j = 0, v = 1 },
|
| 432 |
{ i = 0, j = 2, v = 2 },
|
| 433 |
{ i = 1, j = 1, v = 4 },
|
| 434 |
]); |
| 435 |
t = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 436 |
{ i = 0, j = 1, v = 7 },
|
| 437 |
{ i = 1, j = 0, v = 5 },
|
| 438 |
{ i = 1, j = 1, v = 6 },
|
| 439 |
]); |
| 440 |
diff = mat.difference s t; |
| 441 |
mat.isSparse? diff and |
| 442 |
compareMatrices diff (mat.difference (mat.toDense s) t) and |
| 443 |
compareMatrices diff (mat.difference (mat.toDense s) (mat.toDense t)) and |
| 444 |
compareMatrices diff (mat.difference s (mat.toDense t)) and |
| 445 |
compareMatrices diff |
| 446 |
(mat.newSparseMatrix { rows = 2, columns = 3 } (Rows [
|
| 447 |
{ i = 0, j = 0, v = 1 },
|
| 448 |
{ i = 0, j = 1, v = -7 },
|
| 449 |
{ i = 0, j = 2, v = 2 },
|
| 450 |
{ i = 1, j = 0, v = -5 },
|
| 451 |
{ i = 1, j = 1, v = -2 },
|
| 452 |
])) |
| 453 |
), |
| 454 |
|
| 455 |
"abs-\(name)": \( |
| 456 |
compareMatrices |
| 457 |
(mat.abs (fromColumns [[-1,4],[2,-5],[-3,0]])) |
| 458 |
(fromColumns [[1,4],[2,5],[3,0]]) |
| 459 |
), |
| 460 |
|
| 461 |
"product-\(name)": \( |
| 462 |
compareMatrices |
| 463 |
(mat.product (constMatrix 2 { rows = 4, columns = 2 })
|
| 464 |
(constMatrix 3 { rows = 2, columns = 3 }))
|
| 465 |
(constMatrix 12 { rows = 4, columns = 3 }) and
|
| 466 |
compareMatrices |
| 467 |
(mat.product (fromColumns [[1,4],[2,5],[3,6]]) |
| 468 |
(fromColumns [[7,9,11],[8,10,12]])) |
| 469 |
(fromColumns [[58,139],[64,154]]) |
| 470 |
), |
| 471 |
|
| 472 |
"entryWiseProduct-\(name)": \( |
| 473 |
compareMatrices |
| 474 |
(mat.entryWiseProduct |
| 475 |
[fromRows [[1,2,3],[4,5,0]], |
| 476 |
fromRows [[6,7,8],[0,1,2]]]) |
| 477 |
(fromRows [[6,14,24],[0,5,0]]) and |
| 478 |
compareMatrices |
| 479 |
(mat.entryWiseProduct |
| 480 |
[fromRows [[1,2,3],[4,5,0]], |
| 481 |
fromColumns [[6,0],[7,1],[8,2]]]) |
| 482 |
(fromRows [[6,14,24],[0,5,0]]) |
| 483 |
), |
| 484 |
|
| 485 |
"entryWiseDivide-\(name)": \( |
| 486 |
compareMatrices |
| 487 |
(mat.entryWiseDivide |
| 488 |
(fromRows [[1,2,3],[4,5,0]]) |
| 489 |
(fromRows [[6,7,8],[1,2,3]])) |
| 490 |
(fromRows [[1/6,2/7,3/8],[4,5/2,0]]) and |
| 491 |
compareMatrices |
| 492 |
(mat.entryWiseDivide |
| 493 |
(fromRows [[1,2,3],[4,5,0]]) |
| 494 |
(fromColumns [[6,1],[7,2],[8,3]])) |
| 495 |
(fromRows [[1/6,2/7,3/8],[4,5/2,0]]); |
| 496 |
), |
| 497 |
|
| 498 |
"sparseProduct-\(name)": \( |
| 499 |
s = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 500 |
{ i = 0, j = 0, v = 1 },
|
| 501 |
{ i = 0, j = 2, v = 2 },
|
| 502 |
{ i = 1, j = 1, v = 4 },
|
| 503 |
]); |
| 504 |
t = mat.newSparseMatrix { rows = 3, columns = 2 } (Columns [
|
| 505 |
{ i = 0, j = 1, v = 7 },
|
| 506 |
{ i = 1, j = 0, v = 5 },
|
| 507 |
{ i = 2, j = 0, v = 6 },
|
| 508 |
]); |
| 509 |
prod = mat.product s t; |
| 510 |
mat.isSparse? prod and |
| 511 |
compareMatrices prod (mat.product (mat.toDense s) t) and |
| 512 |
compareMatrices prod (mat.product (mat.toDense s) (mat.toDense t)) and |
| 513 |
compareMatrices prod (mat.product s (mat.toDense t)) and |
| 514 |
compareMatrices prod |
| 515 |
(mat.newSparseMatrix { rows = 2, columns = 2 } (Rows [
|
| 516 |
{ i = 0, j = 0, v = 12 },
|
| 517 |
{ i = 0, j = 1, v = 7 },
|
| 518 |
{ i = 1, j = 0, v = 20 },
|
| 519 |
])) |
| 520 |
), |
| 521 |
|
| 522 |
"productFail-\(name)": \( |
| 523 |
try |
| 524 |
\() (mat.product (constMatrix 2 { rows = 4, columns = 2 })
|
| 525 |
(constMatrix 3 { rows = 3, columns = 2 }));
|
| 526 |
false; |
| 527 |
catch FailureException e: |
| 528 |
true |
| 529 |
yrt |
| 530 |
), |
| 531 |
|
| 532 |
"resizedTo-\(name)": \( |
| 533 |
compareMatrices |
| 534 |
(mat.resizedTo { rows = 2, columns = 2 }
|
| 535 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 536 |
(fromColumns [[1,4],[2,5]]) and |
| 537 |
compareMatrices |
| 538 |
(mat.resizedTo { rows = 3, columns = 4 }
|
| 539 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 540 |
(fromColumns [[1,4,0],[2,5,0],[3,6,0],[0,0,0]]) and |
| 541 |
compareMatrices |
| 542 |
(mat.resizedTo { rows = 1, columns = 1 }
|
| 543 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 544 |
(fromRows [[1]]) and |
| 545 |
compareMatrices |
| 546 |
(mat.resizedTo { rows = 2, columns = 3 }
|
| 547 |
(mat.zeroMatrix { rows = 0, columns = 0 }))
|
| 548 |
(fromRows [[0,0,0],[0,0,0]]) and |
| 549 |
mat.isSparse? |
| 550 |
(mat.resizedTo { rows = 1, columns = 1 }
|
| 551 |
(mat.toSparse (fromColumns [[1,4],[2,5],[3,6]]))) |
| 552 |
), |
| 553 |
|
| 554 |
"tiledTo-\(name)": \( |
| 555 |
compareMatrices |
| 556 |
(mat.tiledTo { rows = 2, columns = 2 }
|
| 557 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 558 |
(fromColumns [[1,4],[2,5]]) and |
| 559 |
compareMatrices |
| 560 |
(mat.tiledTo { rows = 3, columns = 4 }
|
| 561 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 562 |
(fromColumns [[1,4,1],[2,5,2],[3,6,3],[1,4,1]]) and |
| 563 |
compareMatrices |
| 564 |
(mat.tiledTo { rows = 7, columns = 2 }
|
| 565 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 566 |
(fromColumns [[1,4,1,4,1,4,1],[2,5,2,5,2,5,2]]) and |
| 567 |
compareMatrices |
| 568 |
(mat.tiledTo { rows = 1, columns = 1 }
|
| 569 |
(fromColumns [[1,4],[2,5],[3,6]])) |
| 570 |
(fromRows [[1]]) and |
| 571 |
compareMatrices |
| 572 |
(mat.tiledTo { rows = 2, columns = 3 }
|
| 573 |
(mat.zeroMatrix { rows = 0, columns = 0 }))
|
| 574 |
(fromRows [[0,0,0],[0,0,0]]) and |
| 575 |
compareMatrices |
| 576 |
(mat.tiledTo { rows = 0, columns = 0 }
|
| 577 |
(mat.zeroMatrix { rows = 4, columns = 3 }))
|
| 578 |
(fromRows []) |
| 579 |
), |
| 580 |
|
| 581 |
"zeroSizeMatrix-\(name)": \( |
| 582 |
compareMatrices |
| 583 |
(mat.zeroMatrix { rows = 0, columns = 0 })
|
| 584 |
(fromColumns []) and |
| 585 |
compareMatrices |
| 586 |
(mat.zeroMatrix { rows = 0, columns = 1 })
|
| 587 |
(fromColumns [[]]) and |
| 588 |
(not mat.equal (fromColumns [[]]) |
| 589 |
(fromRows [[]])) and |
| 590 |
compareMatrices |
| 591 |
(mat.zeroMatrix { rows = 0, columns = 0 })
|
| 592 |
(mat.newSparseMatrix { rows = 0, columns = 0 } (Columns [])) and
|
| 593 |
compareMatrices |
| 594 |
(mat.zeroMatrix { rows = 1, columns = 0 })
|
| 595 |
(mat.newSparseMatrix { rows = 1, columns = 0 } (Columns []))
|
| 596 |
), |
| 597 |
|
| 598 |
"asRows-\(name)": \( |
| 599 |
compare |
| 600 |
(map vec.list |
| 601 |
(mat.asRows (fromColumns [[1,4],[0,5],[3,6]]))) |
| 602 |
[[1,0,3],[4,5,6]]; |
| 603 |
), |
| 604 |
|
| 605 |
"asColumns-\(name)": \( |
| 606 |
compare |
| 607 |
(map vec.list |
| 608 |
(mat.asColumns (fromColumns [[1,4],[0,5],[3,6]]))) |
| 609 |
[[1,4],[0,5],[3,6]]; |
| 610 |
), |
| 611 |
|
| 612 |
"repeated-horiz-\(name)": \( |
| 613 |
compareMatrices |
| 614 |
(mat.repeatedHorizontal 2 (fromColumns [[1,4],[0,5]])) |
| 615 |
(fromColumns [[1,4],[0,5],[1,4],[0,5]]) and |
| 616 |
compareMatrices |
| 617 |
(mat.repeatedHorizontal 2 (fromRows [[1,0],[4,5]])) |
| 618 |
(fromColumns [[1,4],[0,5],[1,4],[0,5]]) and |
| 619 |
compareMatrices |
| 620 |
(mat.repeatedHorizontal 0 (fromColumns [[1,4],[0,5]])) |
| 621 |
(fromColumns []) and |
| 622 |
compareMatrices |
| 623 |
(mat.repeatedHorizontal 4 (fromColumns [[]])) |
| 624 |
(fromColumns [[],[],[],[]]) |
| 625 |
), |
| 626 |
|
| 627 |
"repeated-vert-\(name)": \( |
| 628 |
compareMatrices |
| 629 |
(mat.repeatedVertical 2 (fromRows [[1,4],[0,5]])) |
| 630 |
(fromRows [[1,4],[0,5],[1,4],[0,5]]) and |
| 631 |
compareMatrices |
| 632 |
(mat.repeatedVertical 2 (fromColumns [[1,0],[4,5]])) |
| 633 |
(fromRows [[1,4],[0,5],[1,4],[0,5]]) and |
| 634 |
compareMatrices |
| 635 |
(mat.repeatedVertical 0 (fromRows [[1,4],[0,5]])) |
| 636 |
(fromRows []) and |
| 637 |
compareMatrices |
| 638 |
(mat.repeatedVertical 4 (fromRows [[]])) |
| 639 |
(fromRows [[],[],[],[]]) |
| 640 |
), |
| 641 |
|
| 642 |
"concat-horiz-\(name)": \( |
| 643 |
compareMatrices |
| 644 |
(mat.concatHorizontal |
| 645 |
[(fromColumns [[1,4],[0,5]]), |
| 646 |
(fromRows [[3],[6]])]) |
| 647 |
(fromColumns [[1,4],[0,5],[3,6]]) |
| 648 |
), |
| 649 |
|
| 650 |
"concatEmpty-horiz-\(name)": \( |
| 651 |
compareMatrices |
| 652 |
(mat.concatHorizontal |
| 653 |
[(fromColumns [[]]), |
| 654 |
(fromColumns [[],[]]), |
| 655 |
(mat.zeroMatrix { rows = 0, columns = 6 })])
|
| 656 |
(mat.zeroMatrix { rows = 0, columns = 9 }) and
|
| 657 |
compareMatrices |
| 658 |
(mat.concatHorizontal |
| 659 |
[(fromRows [[]]), |
| 660 |
(fromRows [[1,2]]), |
| 661 |
(mat.zeroMatrix { rows = 1, columns = 0 })])
|
| 662 |
(fromRows [[1,2]]) |
| 663 |
), |
| 664 |
|
| 665 |
"sparseConcat-horiz-\(name)": \( |
| 666 |
s = mat.concatHorizontal |
| 667 |
[mat.toSparse (fromColumns [[1,4],[0,5]]), |
| 668 |
mat.toSparse (fromRows [[3],[6]])]; |
| 669 |
compareMatrices s (fromColumns [[1,4],[0,5],[3,6]]) and |
| 670 |
compare (mat.isSparse? s) true and |
| 671 |
compare (mat.density s) (5/6) |
| 672 |
), |
| 673 |
|
| 674 |
"concatFail-horiz-\(name)": \( |
| 675 |
try |
| 676 |
\() (mat.concatHorizontal |
| 677 |
[(fromColumns [[1,4],[0,5]]), |
| 678 |
(fromColumns [[3],[6]])]); |
| 679 |
false |
| 680 |
catch FailureException e: |
| 681 |
true |
| 682 |
yrt |
| 683 |
), |
| 684 |
|
| 685 |
"concat-vert-\(name)": \( |
| 686 |
compareMatrices |
| 687 |
(mat.concatVertical |
| 688 |
[(fromColumns [[1,4],[0,5]]), |
| 689 |
(fromRows [[3,6]])]) |
| 690 |
(fromColumns [[1,4,3],[0,5,6]]) |
| 691 |
), |
| 692 |
|
| 693 |
"sparseConcat-vert-\(name)": \( |
| 694 |
s = mat.concatVertical |
| 695 |
[mat.toSparse (fromColumns [[1,4],[0,5]]), |
| 696 |
mat.toSparse (fromRows [[3,6]])]; |
| 697 |
compareMatrices s (fromColumns [[1,4,3],[0,5,6]]) and |
| 698 |
compare (mat.isSparse? s) true and |
| 699 |
compare (mat.density s) (5/6) |
| 700 |
), |
| 701 |
|
| 702 |
"concatFail-vert-\(name)": \( |
| 703 |
try |
| 704 |
\() (mat.concatVertical |
| 705 |
[(fromColumns [[1,4],[0,5]]), |
| 706 |
(fromRows [[3],[6]])]); |
| 707 |
false |
| 708 |
catch FailureException e: |
| 709 |
true |
| 710 |
yrt |
| 711 |
), |
| 712 |
|
| 713 |
"rowSlice-\(name)": \( |
| 714 |
compareMatrices |
| 715 |
(mat.rowSlice (fromRows [[1,0],[3,4],[0,6],[7,8]]) 1 3) |
| 716 |
(fromRows [[3,4],[0,6]]) and |
| 717 |
compareMatrices |
| 718 |
(mat.rowSlice (fromRows [[1,0],[3,4],[0,6],[7,8]]) 3 6) |
| 719 |
(fromRows [[7,8]]) |
| 720 |
), |
| 721 |
|
| 722 |
"columnSlice-\(name)": \( |
| 723 |
compareMatrices |
| 724 |
(mat.columnSlice (fromRows [[1,0,3,4],[0,6,7,8]]) 1 3) |
| 725 |
(fromRows [[0,3],[6,7]]) and |
| 726 |
compareMatrices |
| 727 |
(mat.columnSlice (fromRows [[1,0,3,4],[0,6,7,8]]) 2 5) |
| 728 |
(fromRows [[3,4],[7,8]]) |
| 729 |
), |
| 730 |
|
| 731 |
"density-\(name)": \( |
| 732 |
compare (mat.density (fromColumns [[1,2,0],[0,5,0]])) (3/6) and |
| 733 |
compare (mat.density (fromColumns [[1,2,3],[4,5,6]])) (6/6) and |
| 734 |
compare (mat.density (fromColumns [[0,0,0],[0,0,0]])) 0 |
| 735 |
), |
| 736 |
|
| 737 |
"nonZeroValues-\(name)": \( |
| 738 |
compare (mat.nonZeroValues (fromColumns [[1,2,0],[0,5,0]])) 3 and |
| 739 |
compare (mat.nonZeroValues (fromColumns [[1,2,3],[4,5,6]])) 6 and |
| 740 |
compare (mat.nonZeroValues (fromColumns [[0,0,0],[0,0,0]])) 0 |
| 741 |
), |
| 742 |
|
| 743 |
"toSparse-\(name)": \( |
| 744 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 745 |
compareMatrices (mat.toSparse m) m and |
| 746 |
compareMatrices (mat.toDense (mat.toSparse m)) m and |
| 747 |
compare (mat.density (mat.toSparse m)) (6/9) |
| 748 |
), |
| 749 |
|
| 750 |
"toDense-\(name)": \( |
| 751 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 752 |
compareMatrices (mat.toDense m) m and |
| 753 |
compareMatrices (mat.toSparse (mat.toDense m)) m |
| 754 |
), |
| 755 |
|
| 756 |
"filter-\(name)": \( |
| 757 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 758 |
compareMatrices |
| 759 |
(mat.filter (> 2) m) |
| 760 |
(fromColumns [[0,0,0],[0,0,6],[0,0,3]]) and |
| 761 |
compare (mat.isSparse? (mat.filter (> 2) m)) true and |
| 762 |
compare (mat.density (mat.filter (> 2) m)) (2/9) |
| 763 |
), |
| 764 |
|
| 765 |
"all-\(name)": \( |
| 766 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 767 |
compare (mat.all (== 9) m) false and |
| 768 |
compare (mat.all (!= 9) m) true and |
| 769 |
compare (mat.all (== 2) m) false and |
| 770 |
compare (mat.all (!= 2) m) false |
| 771 |
), |
| 772 |
|
| 773 |
"any-\(name)": \( |
| 774 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 775 |
compare (mat.any (== 9) m) false and |
| 776 |
compare (mat.any (!= 9) m) true and |
| 777 |
compare (mat.any (== 2) m) true and |
| 778 |
compare (mat.any (!= 2) m) true |
| 779 |
), |
| 780 |
|
| 781 |
"newSparseMatrix-\(name)": \( |
| 782 |
s = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 783 |
{ i = 0, j = 0, v = 1 },
|
| 784 |
{ i = 0, j = 2, v = 2 },
|
| 785 |
{ i = 1, j = 1, v = 4 },
|
| 786 |
]); |
| 787 |
// If there are zeros in the entries list, they should not end up |
| 788 |
// in the sparse data |
| 789 |
t = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 790 |
{ i = 0, j = 0, v = 1 },
|
| 791 |
{ i = 0, j = 2, v = 0 },
|
| 792 |
{ i = 1, j = 1, v = 4 },
|
| 793 |
]); |
| 794 |
// Any out-of-range or non-integer i, j should be ignored too |
| 795 |
u = mat.newSparseMatrix { rows = 2, columns = 3 } (Columns [
|
| 796 |
{ i = -1, j = 0, v = 1 },
|
| 797 |
{ i = 0, j = 4, v = 3 },
|
| 798 |
{ i = 1, j = 1.5, v = 4 },
|
| 799 |
]); |
| 800 |
compare (mat.density s) (3/6) and |
| 801 |
compare (mat.density t) (2/6) and |
| 802 |
compareMatrices s (fromRows [[1,0,2],[0,4,0]]) and |
| 803 |
compareMatrices t (fromRows [[1,0,0],[0,4,0]]) and |
| 804 |
compareMatrices u (fromRows [[0,0,0],[0,0,0]]) |
| 805 |
), |
| 806 |
|
| 807 |
"enumerate-\(name)": \( |
| 808 |
m = fromColumns [[1,2,0],[-1,-4,6],[0,0,3]]; |
| 809 |
all = [ |
| 810 |
{ i = 0, j = 0, v = 1 },
|
| 811 |
{ i = 1, j = 0, v = 2 },
|
| 812 |
{ i = 2, j = 0, v = 0 },
|
| 813 |
{ i = 0, j = 1, v = -1 },
|
| 814 |
{ i = 1, j = 1, v = -4 },
|
| 815 |
{ i = 2, j = 1, v = 6 },
|
| 816 |
{ i = 0, j = 2, v = 0 },
|
| 817 |
{ i = 1, j = 2, v = 0 },
|
| 818 |
{ i = 2, j = 2, v = 3 },
|
| 819 |
]; |
| 820 |
sortEntries = |
| 821 |
sortBy do a b: |
| 822 |
if a.i == b.i then a.j < b.j else a.i < b.i fi |
| 823 |
done; |
| 824 |
compare |
| 825 |
(sortEntries (mat.enumerate m)) |
| 826 |
(sortEntries |
| 827 |
(if mat.isSparse? m then filter do d: d.v != 0 done all else all fi)); |
| 828 |
), |
| 829 |
|
| 830 |
]); |
| 831 |
|
| 832 |
colhash = makeTests "column-dense" id; |
| 833 |
rowhash = makeTests "row-dense" mat.flipped; |
| 834 |
sparsecolhash = makeTests "column-sparse" mat.toSparse; |
| 835 |
|
| 836 |
// there are two possible orders for constructing a sparse row-major |
| 837 |
// matrix from a dense col-major one, so test them both: |
| 838 |
sparserowhash1 = makeTests "row-sparse-a" (mat.toSparse . mat.flipped); |
| 839 |
sparserowhash2 = makeTests "row-sparse-b" (mat.flipped . mat.toSparse); |
| 840 |
|
| 841 |
all = [:]; |
| 842 |
for [ colhash, rowhash, sparsecolhash, sparserowhash1, sparserowhash2 ] do h: |
| 843 |
for (keys h) do k: all[k] := h[k] done; |
| 844 |
done; |
| 845 |
|
| 846 |
all is hash<string, () -> boolean>; |
| 847 |
|
| 848 |
|