Mercurial > hg > may
view yetilab/matrix/test/test_matrix.yeti @ 222:77c6a81c577f matrix_opaque_immutable
Move block directory -> vector
author | Chris Cannam |
---|---|
date | Sat, 11 May 2013 15:58:36 +0100 |
parents | 937f908cae52 |
children | ac1373067054 |
line wrap: on
line source
module yetilab.matrix.test.test_matrix; mat = load yetilab.matrix.matrix; vec = load yetilab.vector.vector; load yetilab.vector.vectortype; load yetilab.matrix.matrixtype; import yeti.lang: FailureException; { compare, compareUsing } = load yetilab.test.test; compareMatrices = compareUsing mat.equal; makeTests name flipper = (constMatrix n s = flipper (mat.constMatrix n s); zeroMatrix s = flipper (mat.zeroMatrix s); randomMatrix s = flipper (mat.randomMatrix s); identityMatrix s = flipper (mat.identityMatrix s); generate f s = flipper (mat.generate f s); newMatrix t d = flipper (mat.newMatrix t (map vec.fromList d)); [ "constMatrixEmpty-\(name)": \( m = constMatrix 2 { rows = 0, columns = 0 }; compare (mat.size m) { columns = 0, rows = 0 } ), "constMatrixEmpty2-\(name)": \( compare (mat.size (constMatrix 2 { rows = 0, columns = 4 })) { columns = 0, rows = 0 } and compare (mat.size (constMatrix 2 { rows = 4, columns = 0 })) { columns = 0, rows = 0 } ), "constMatrix-\(name)": \( m = constMatrix 2 { rows = 3, columns = 4 }; compare (mat.size m) { columns = 4, rows = 3 } and all id (map do row: compare (vec.list (mat.getRow row m)) [2,2,2,2] done [0..2]) and all id (map do col: compare (vec.list (mat.getColumn col m)) [2,2,2] done [0..3]) ), "randomMatrixEmpty-\(name)": \( m = randomMatrix { rows = 0, columns = 0 }; compare (mat.size m) { columns = 0, rows = 0 } ), "randomMatrix-\(name)": \( m = randomMatrix { rows = 3, columns = 4 }; compare (mat.size m) { columns = 4, rows = 3 } ), "zeroMatrixEmpty-\(name)": \( m = zeroMatrix { rows = 0, columns = 0 }; compare (mat.size m) { columns = 0, rows = 0 } ), "zeroMatrix-\(name)": \( m = zeroMatrix { rows = 3, columns = 4 }; compare (mat.size m) { columns = 4, rows = 3 } and all id (map do row: compare (vec.list (mat.getRow row m)) [0,0,0,0] done [0..2]) and all id (map do col: compare (vec.list (mat.getColumn col m)) [0,0,0] done [0..3]) ), "identityMatrixEmpty-\(name)": \( m = identityMatrix { rows = 0, columns = 0 }; compare (mat.size m) { columns = 0, rows = 0 } ), "identityMatrix-\(name)": \( m = identityMatrix { rows = 3, columns = 4 }; compare (mat.size m) { columns = 4, rows = 3 } and all id (map do row: compare (vec.list (mat.getRow row m)) [1,1,1,1] done [0..2]) and all id (map do col: compare (vec.list (mat.getColumn col m)) [1,1,1] done [0..3]) ), "generateEmpty-\(name)": \( m = generate do row col: 0 done { rows = 0, columns = 0 }; compare (mat.size m) { columns = 0, rows = 0 } ), "generate-\(name)": \( m = generate do row col: row * 10 + col done { rows = 2, columns = 3 }; compare (vec.list (mat.getRow 0 m)) [0,1,2] and compare (vec.list (mat.getRow 1 m)) [10,11,12] ), "widthAndHeight-\(name)": \( m = constMatrix 2 { rows = 3, columns = 4 }; compare (mat.size m) { columns = mat.width m, rows = mat.height m } ), "equal-\(name)": \( m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; n = m; p = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]]; q = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]]; r = newMatrix (ColumnMajor ()) [[1,4],[2,5]]; compareMatrices m n and compareMatrices m p and compareMatrices n p and not mat.equal m q and not mat.equal m r ), "getAt-\(name)": \( generator row col = row * 10 + col; m = generate generator { rows = 2, columns = 3 }; all id (map do row: all id (map do col: mat.getAt row col m == generator row col done [0..2]) done [0..1]) ), /*!!! "setAt-\(name)": \( generator row col = row * 10 + col; m = generate generator { rows = 2, columns = 3 }; mat.setAt 1 2 16 m; compare (mat.getAt 1 2 m) 16 and compare (mat.getAt 1 1 m) 11 and compare (mat.getAt 0 2 m) 2 ), "copyOfEqual-\(name)": \( m = constMatrix 2 { rows = 3, columns = 4 }; m'' = mat.copyOf m; compareMatrices m'' m ), "copyOfAlias-\(name)": \( m = constMatrix 2 { rows = 3, columns = 4 }; m' = m; m'' = mat.copyOf m; mat.setAt 0 0 6 m; compareMatrices m' m and not mat.equal m m''; ), */ "transposedEmpty-\(name)": \( compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 0 }))) { columns = 0, rows = 0 } and compare (mat.size (mat.transposed (constMatrix 2 { rows = 0, columns = 4 }))) { columns = 0, rows = 0 } and compare (mat.size (mat.transposed (constMatrix 2 { rows = 4, columns = 0 }))) { columns = 0, rows = 0 } ), "transposedSize-\(name)": \( compare (mat.size (mat.transposed (constMatrix 2 { rows = 3, columns = 4 }))) { columns = 3, rows = 4 } ), "transposed-\(name)": \( generator row col = row * 10 + col; m = generate generator { rows = 2, columns = 3 }; m' = mat.transposed m; all id (map do row: all id // like getAt test, but with col/row flipped (map do col: mat.getAt col row m' == generator row col done [0..2]) done [0..1]) ), "transposed-back-\(name)": \( m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; compareMatrices m (mat.transposed (mat.transposed m)) and not mat.equal m (mat.transposed m); ), "flipped-\(name)": \( m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; m' = mat.flipped m; m'' = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]]; compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m''; ), "flipped-back-\(name)": \( m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; compareMatrices m (mat.flipped (mat.flipped m)); ), "flipped-empty-\(name)": \( m = constMatrix 2 { rows = 0, columns = 4 }; compareMatrices (mat.flipped m) (mat.flipped (constMatrix 0 { rows = 0, columns = 0 })); ), "toRowMajor-\(name)": \( m = newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]; m' = mat.toRowMajor m; m'' = newMatrix (RowMajor ()) [[1,2,3],[4,5,6]]; m''' = mat.toRowMajor m''; compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m'' and compareMatrices m m'''; ), "toColumnMajor-\(name)": \( m = newMatrix (RowMajor ()) [[1,4],[2,5],[3,6]]; m' = mat.toColumnMajor m; m'' = newMatrix (ColumnMajor ()) [[1,2,3],[4,5,6]]; m''' = mat.toColumnMajor m''; compareMatrices m m' and compareMatrices m m'' and compareMatrices m' m'' and compareMatrices m m'''; ), "scaled-\(name)": \( compareMatrices (mat.scaled 0.5 (constMatrix 2 { rows = 3, columns = 4 })) (constMatrix 1 { rows = 3, columns = 4 }) and compareMatrices (mat.scaled 0.5 (constMatrix (-3) { rows = 3, columns = 4 })) (constMatrix (-1.5) { rows = 3, columns = 4 }) and compareMatrices (mat.scaled 0.5 (constMatrix 2 { rows = 0, columns = 2 })) (constMatrix 5 { rows = 0, columns = 0 }) ), "sum-\(name)": \( compareMatrices (mat.sum (constMatrix 2 { rows = 3, columns = 4 }) (constMatrix 1 { rows = 3, columns = 4 })) (constMatrix 3 { rows = 3, columns = 4 }) ), "sumFail-\(name)": \( try \() (mat.sum (constMatrix 2 { rows = 3, columns = 4 }) (constMatrix 1 { rows = 3, columns = 5 })); false; catch FailureException e: true yrt ), "product-\(name)": \( compareMatrices (mat.product (constMatrix 2 { rows = 4, columns = 2 }) (constMatrix 3 { rows = 2, columns = 3 })) (constMatrix 12 { rows = 4, columns = 3 }) and compareMatrices (mat.product (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]) (newMatrix (ColumnMajor ()) [[7,9,11],[8,10,12]])) (newMatrix (ColumnMajor ()) [[58,139],[64,154]]) ), "productFail-\(name)": \( try \() (mat.product (constMatrix 2 { rows = 4, columns = 2 }) (constMatrix 3 { rows = 3, columns = 2 })); false; catch FailureException e: true yrt ), "resizedTo-\(name)": \( compareMatrices (mat.resizedTo { rows = 2, columns = 2 } (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])) (newMatrix (ColumnMajor ()) [[1,4],[2,5]]) and compareMatrices (mat.resizedTo { rows = 3, columns = 4 } (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])) (newMatrix (ColumnMajor ()) [[1,4,0],[2,5,0],[3,6,0],[0,0,0]]) and compareMatrices (mat.resizedTo { rows = 1, columns = 1 } (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])) (newMatrix (RowMajor ()) [[1]]) and compareMatrices (mat.resizedTo { rows = 2, columns = 3 } (mat.zeroSizeMatrix ())) (newMatrix (RowMajor ()) [[0,0,0],[0,0,0]]) ), "zeroSizeMatrix-\(name)": \( compareMatrices (mat.zeroSizeMatrix ()) (newMatrix (ColumnMajor ()) []) ), "asRows-\(name)": \( compare (map vec.list (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) [[1,2,3],[4,5,6]]; ), "asColumns-\(name)": \( compare (map vec.list (mat.asColumns (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]))) [[1,4],[2,5],[3,6]]; ), "concat-horiz-\(name)": \( compareMatrices (mat.concat (Horizontal ()) [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]), (newMatrix (RowMajor ()) [[3],[6]])]) (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]]) ), "concatFail-horiz-\(name)": \( try \() (mat.concat (Horizontal ()) [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]), (newMatrix (ColumnMajor ()) [[3],[6]])]); false catch FailureException e: true yrt ), "concat-vert-\(name)": \( compareMatrices (mat.concat (Vertical ()) [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]), (newMatrix (RowMajor ()) [[3,6]])]) (newMatrix (ColumnMajor ()) [[1,4,3],[2,5,6]]) ), "concatFail-vert-\(name)": \( try \() (mat.concat (Vertical ()) [(newMatrix (ColumnMajor ()) [[1,4],[2,5]]), (newMatrix (RowMajor ()) [[3],[6]])]); false catch FailureException e: true yrt ), "rowSlice-\(name)": \( compareMatrices (mat.rowSlice 1 2 (newMatrix (RowMajor ()) [[1,2],[3,4],[5,6],[7,8]])) (newMatrix (RowMajor ()) [[3,4],[5,6]]) ), "columnSlice-\(name)": \( compareMatrices (mat.columnSlice 1 2 (newMatrix (RowMajor ()) [[1,2,3,4],[5,6,7,8]])) (newMatrix (RowMajor ()) [[2,3],[6,7]]) ), ]); colhash = makeTests "column-major" id; rowhash = makeTests "row-major" mat.flipped; all = [:]; for (keys colhash) do k: all[k] := colhash[k] done; for (keys rowhash) do k: all[k] := rowhash[k] done; all is hash<string, () -> boolean>;