Mercurial > hg > may
changeset 243:1313764cb89c sparse
Separate out thresholding from toSparse. Not entirely sure this is a good thing
author | Chris Cannam |
---|---|
date | Mon, 20 May 2013 16:45:34 +0100 |
parents | 0ac8672d12b2 |
children | bb5fec8db722 |
files | yetilab/matrix/matrix.yeti yetilab/matrix/test/test_matrix.yeti |
diffstat | 2 files changed, 40 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/yetilab/matrix/matrix.yeti Mon May 20 16:22:17 2013 +0100 +++ b/yetilab/matrix/matrix.yeti Mon May 20 16:45:34 2013 +0100 @@ -206,28 +206,27 @@ extent = minorSize, }); -toSparse threshold m = +toSparse m = if isSparse? m then m else { rows, columns } = size m; - enumerate threshold m ii jj = + enumerate m ii jj = case ii of i::irest: case jj of j::rest: v = getAt i j m; - if abs v > threshold then - { i, j, v } :. \(enumerate threshold m ii rest) - else enumerate threshold m ii rest + if v != 0 then { i, j, v } :. \(enumerate m ii rest) + else enumerate m ii rest fi; - _: enumerate threshold m irest [0..columns-1]; + _: enumerate m irest [0..columns-1]; esac; _: []; esac; makeSparse if isRowMajor? m then RowMajor () else ColMajor () fi (size m) - (enumerate threshold m [0..rows-1] [0..columns-1]); + (enumerate m [0..rows-1] [0..columns-1]); fi; toDense m = @@ -299,9 +298,9 @@ equal' comparator vecComparator (flipped m1) m2; elif isSparse? m1 != isSparse? m2 then if isSparse? m1 then - equal' comparator vecComparator m1 (toSparse 0 m2) + equal' comparator vecComparator m1 (toSparse m2) else - equal' comparator vecComparator (toSparse 0 m1) m2 + equal' comparator vecComparator (toSparse m1) m2 fi else equal'' comparator vecComparator m1 m2 @@ -332,6 +331,11 @@ scaled factor m = //!!! v inefficient generate do row col: factor * (getAt row col m) done (size m); +thresholded threshold m = //!!! v inefficient; and should take a threshold function? + generate do row col: + v = getAt row col m; if (abs v) > threshold then v else 0 fi + done (size m); + sum' m1 m2 = if (size m1) != (size m2) then failWith "Matrices are not the same size: \(size m1), \(size m2)"; @@ -480,6 +484,7 @@ toSparse, toDense, scaled, + thresholded, resizedTo, asRows, asColumns, @@ -519,9 +524,10 @@ flipped is matrix -> matrix, toRowMajor is matrix -> matrix, toColumnMajor is matrix -> matrix, - toSparse is number -> matrix -> matrix, + toSparse is matrix -> matrix, toDense is matrix -> matrix, scaled is number -> matrix -> matrix, + thresholded is number -> matrix -> matrix, resizedTo is { .rows is number, .columns is number } -> matrix -> matrix, asRows is matrix -> list<vector>, asColumns is matrix -> list<vector>,
--- a/yetilab/matrix/test/test_matrix.yeti Mon May 20 16:22:17 2013 +0100 +++ b/yetilab/matrix/test/test_matrix.yeti Mon May 20 16:45:34 2013 +0100 @@ -354,16 +354,37 @@ compare (mat.sparsity (newMatrix (ColumnMajor ()) [[0,0,0],[0,0,0]])) 0 ), +"toSparse-\(name)": \( + m = newMatrix (ColumnMajor ()) [[1,2,0],[-1,-4,6],[0,0,3]]; + compareMatrices (mat.toSparse m) m and + compareMatrices (mat.toDense (mat.toSparse m)) m and + compare (mat.sparsity (mat.toSparse m)) (6/9) +), + +"toDense-\(name)": \( + m = newMatrix (ColumnMajor ()) [[1,2,0],[-1,-4,6],[0,0,3]]; + compareMatrices (mat.toDense m) m and + compareMatrices (mat.toSparse (mat.toDense m)) m +), + +"thresholded-\(name)": \( + m = newMatrix (ColumnMajor ()) [[1,2,0],[-1,-4,6],[0,0,3]]; + compareMatrices + (mat.thresholded 2 m) + (newMatrix (ColumnMajor ()) [[0,0,0],[0,-4,6],[0,0,3]]) and + compare (mat.sparsity (mat.thresholded 2 m)) (3/9) +), + ]); colhash = makeTests "column-dense" id; rowhash = makeTests "row-dense" mat.flipped; -sparsecolhash = makeTests "column-sparse" (mat.toSparse 0); +sparsecolhash = makeTests "column-sparse" mat.toSparse; // there are two possible orders for constructing a sparse row-major // matrix from a dense col-major one, so test them both: -sparserowhash1 = makeTests "row-sparse-a" ((mat.toSparse 0) . (mat.flipped)); -sparserowhash2 = makeTests "row-sparse-b" ((mat.flipped) . (mat.toSparse 0)); +sparserowhash1 = makeTests "row-sparse-a" (mat.toSparse . mat.flipped); +sparserowhash2 = makeTests "row-sparse-b" (mat.flipped . mat.toSparse); all = [:]; for [ colhash, rowhash, sparsecolhash, sparserowhash1, sparserowhash2 ] do h: