view yetilab/matrix/test/test_matrix.yeti @ 178:032c4986b6b0

Implement and test matrix concat
author Chris Cannam
date Thu, 02 May 2013 21:58:58 +0100
parents 370d9350495c
children fd16551c2fc8
line wrap: on
line source

module yetilab.matrix.test.test_matrix;

mat = load yetilab.matrix.matrix;
block = load yetilab.block.block;

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 block.fromList d));
[

"constMatrixEmpty-\(name)": \(
    m = constMatrix 2 { rows = 0, columns = 0 };
    compare m.size { columns = 0, rows = 0 }
),

"constMatrixEmpty2-\(name)": \(
    compare (constMatrix 2 { rows = 0, columns = 4 }).size { columns = 0, rows = 0 } and
        compare (constMatrix 2 { rows = 4, columns = 0 }).size { columns = 0, rows = 0 }
),

"constMatrix-\(name)": \(
    m = constMatrix 2 { rows = 3, columns = 4 };
    compare m.size { columns = 4, rows = 3 } and
        all id (map do row: compare (block.list (m.getRow row)) [2,2,2,2] done [0..2]) and
        all id (map do col: compare (block.list (m.getColumn col)) [2,2,2] done [0..3])
),

"randomMatrixEmpty-\(name)": \(
    m = randomMatrix { rows = 0, columns = 0 };
    compare m.size { columns = 0, rows = 0 }
),

"randomMatrix-\(name)": \(
    m = randomMatrix { rows = 3, columns = 4 };
    compare m.size { columns = 4, rows = 3 }
),

"zeroMatrixEmpty-\(name)": \(
    m = zeroMatrix { rows = 0, columns = 0 };
    compare m.size { columns = 0, rows = 0 }
),

"zeroMatrix-\(name)": \(
    m = zeroMatrix { rows = 3, columns = 4 };
    compare m.size { columns = 4, rows = 3 } and
        all id (map do row: compare (block.list (m.getRow row)) [0,0,0,0] done [0..2]) and
        all id (map do col: compare (block.list (m.getColumn col)) [0,0,0] done [0..3])
),

"identityMatrixEmpty-\(name)": \(
    m = identityMatrix { rows = 0, columns = 0 };
    compare m.size { columns = 0, rows = 0 }
),

"identityMatrix-\(name)": \(
    m = identityMatrix { rows = 3, columns = 4 };
    compare m.size { columns = 4, rows = 3 } and
        all id (map do row: compare (block.list (m.getRow row)) [1,1,1,1] done [0..2]) and
        all id (map do col: compare (block.list (m.getColumn col)) [1,1,1] done [0..3])
),

"generateEmpty-\(name)": \(
    m = generate do row col: 0 done { rows = 0, columns = 0 };
    compare m.size { columns = 0, rows = 0 }
),

"generate-\(name)": \(
    m = generate do row col: row * 10 + col done { rows = 2, columns = 3 };
    compare (block.list (m.getRow 0)) [0,1,2] and
        compare (block.list (m.getRow 1)) [10,11,12]
),

"widthAndHeight-\(name)": \(
    m = constMatrix 2 { rows = 3, columns = 4 };
    compare m.size { 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: m.getAt row col == generator row col done [0..2])
            done [0..1])
),

"setAt-\(name)": \(
    generator row col = row * 10 + col;
    m = generate generator { rows = 2, columns = 3 };
    m.setAt 1 2 16;
    compare (m.getAt 1 2) 16 and
        compare (m.getAt 1 1) 11 and
        compare (m.getAt 0 2) 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;
    m.setAt 0 0 6;
    compareMatrices m' m and not mat.equal m m'';
),

"transposedEmpty-\(name)": \(
    compare (mat.transposed (constMatrix 2 { rows = 0, columns = 0 })).size { columns = 0, rows = 0 } and
        compare (mat.transposed (constMatrix 2 { rows = 0, columns = 4 })).size { columns = 0, rows = 0 } and
        compare (mat.transposed (constMatrix 2 { rows = 4, columns = 0 })).size { columns = 0, rows = 0 }
),

"transposedSize-\(name)": \(
    compare (mat.transposed (constMatrix 2 { rows = 3, columns = 4 })).size { 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: m'.getAt col row == 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]])
),

"zeroSizeMatrix-\(name)": \(
    compareMatrices
       (mat.zeroSizeMatrix ())
       (newMatrix (ColumnMajor ()) [])
),

"asRows-\(name)": \(
    compare
       (map block.list
           (mat.asRows (newMatrix (ColumnMajor ()) [[1,4],[2,5],[3,6]])))
        [[1,2,3],[4,5,6]];
),

"asColumns-\(name)": \(
    compare
       (map block.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
),

]);

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>;