Mercurial > hg > may
changeset 513:d4bd9303a239 sized_matrix
Reorder newSparseMatrix args to match other now size-first construction functions
author | Chris Cannam |
---|---|
date | Wed, 20 Nov 2013 14:49:47 +0000 |
parents | a165dbdc7ffb |
children | ec3e04180aee |
files | src/may/matrix.yeti src/may/matrix/test/test_matrix.yeti |
diffstat | 2 files changed, 38 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/src/may/matrix.yeti Wed Nov 20 14:43:14 2013 +0000 +++ b/src/may/matrix.yeti Wed Nov 20 14:49:47 2013 +0000 @@ -257,7 +257,7 @@ // Make a sparse matrix from entries whose i, j values are known to be // within range -makeSparse type size data = +makeSparse size type data = (isRow = case type of RowMajor (): true; ColumnMajor (): false esac; ordered = sortBy do a b: @@ -302,8 +302,8 @@ // resizedTo. //!!! doc: i is row number, j is column number (throughout, for sparse stuff). Would calling them row/column be better? //!!! what to do with this, now that it doesn't match the former newMatrix? -newSparseMatrix type size data = - makeSparse type size +newSparseMatrix size type data = + makeSparse size type (filter do { i, j, v }: i == int i and i >= 0 and i < size.rows and @@ -313,12 +313,12 @@ toSparse m = if isSparse? m then m else - makeSparse (typeOf m) (size m) (enumerateDense m); + makeSparse (size m) (typeOf m) (enumerateDense m); fi; toDense m = { - size = m.size, + size = (size m), data = if not (isSparse? m) then m.data elif isRowMajor? m then @@ -334,7 +334,7 @@ transposed m = { - size = flippedSize m.size, + size = flippedSize (size m), data = case m.data of DenseRows d: DenseCols d; @@ -346,7 +346,7 @@ flipped m = if isSparse? m then - makeSparse (flippedTypeOf m) (size m) (enumerateSparse m) + makeSparse (size m) (flippedTypeOf m) (enumerateSparse m) else if isRowMajor? m then generate do row col: at' m row col done (size m); @@ -407,7 +407,6 @@ equal = equal' (==) vec.equal; -//!!! nb makeSparse takes type,size while this currently takes size,type - fix newMatrixOfSize size type rowscols = //!!! NB does not copy data if type == RowMajor () then { @@ -477,7 +476,7 @@ kk = keys h[i]; map2 do j v: { i, j, v } done kk (map (at h[i]) kk) done (keys h)); - makeSparse (typeOf m1) (size m1) entries); + makeSparse (size m1) (typeOf m1) entries); sum' m1 m2 = if (size m1) != (size m2) @@ -500,37 +499,37 @@ scaled factor m = if isSparse? m then - makeSparse (typeOf m) (size m) + makeSparse (size m) (typeOf m) (map do { i, j, v }: { i, j, v = factor * v } done (enumerate m)) elif isRowMajor? m then - newMatrixOfSize m.size (typeOf m) (map (vec.scaled factor) (asRows m)); + newMatrixOfSize (size m) (typeOf m) (map (vec.scaled factor) (asRows m)); else - newMatrixOfSize m.size (typeOf m) (map (vec.scaled factor) (asColumns m)); + newMatrixOfSize (size m) (typeOf m) (map (vec.scaled factor) (asColumns m)); fi; abs' m = if isSparse? m then - makeSparse (typeOf m) (size m) + makeSparse (size m) (typeOf m) (map do { i, j, v }: { i, j, v = abs v } done (enumerate m)) elif isRowMajor? m then - newMatrixOfSize m.size (typeOf m) (map vec.abs (asRows m)); + newMatrixOfSize (size m) (typeOf m) (map vec.abs (asRows m)); else - newMatrixOfSize m.size (typeOf m) (map vec.abs (asColumns m)); + newMatrixOfSize (size m) (typeOf m) (map vec.abs (asColumns m)); fi; negative m = if isSparse? m then - makeSparse (typeOf m) (size m) + makeSparse (size m) (typeOf m) (map do { i, j, v }: { i, j, v = (-v) } done (enumerate m)) elif isRowMajor? m then - newMatrixOfSize m.size (typeOf m) (map vec.negative (asRows m)); + newMatrixOfSize (size m) (typeOf m) (map vec.negative (asRows m)); else - newMatrixOfSize m.size (typeOf m) (map vec.negative (asColumns m)); + newMatrixOfSize (size m) (typeOf m) (map vec.negative (asColumns m)); fi; //!!! doc: filter by predicate, always returns sparse matrix filter' f m = - makeSparse (typeOf m) (size m) + makeSparse (size m) (typeOf m) (map do { i, j, v }: { i, j, v = if f v then v else 0 fi } done (enumerate m)); @@ -617,7 +616,7 @@ { i, j = j', v = hout[i] } done (keys hout); done (nonEmptySlices d)); - makeSparse (ColumnMajor ()) size (concat entries)); + makeSparse size (ColumnMajor ()) (concat entries)); SparseCSR _: sparseProduct size m1 (flipped m2); _: failWith "sparseProduct called for non-sparse matrices"; @@ -688,7 +687,7 @@ done (enumerate m)) ++ acc); _: acc; esac; - makeSparse (typeOf first) { rows, columns } + makeSparse { rows, columns } (typeOf first) if direction == Vertical () then entries 0 0 1 0 mm [] else entries 0 0 0 1 mm [] fi); @@ -792,7 +791,7 @@ elif isSparse? m then // don't call makeSparse directly: want to discard // out-of-range cells - newSparseMatrix (typeOf m) newsize (enumerateSparse m) + newSparseMatrix newsize (typeOf m) (enumerateSparse m) elif (height m) == 0 or (width m) == 0 then zeroMatrixWithTypeOf m newsize; else @@ -834,10 +833,10 @@ fi; transformRows rf m = - newMatrixOfSize m.size (RowMajor ()) (map rf (asRows m)); + newMatrixOfSize (size m) (RowMajor ()) (map rf (asRows m)); transformColumns cf m = - newMatrixOfSize m.size (ColumnMajor ()) (map cf (asColumns m)); + newMatrixOfSize (size m) (ColumnMajor ()) (map cf (asColumns m)); format m = strJoin "\n" @@ -969,7 +968,7 @@ fromColumns is list<vec.vector_t> -> matrix_t, newRowVector is vec.vector_t -> matrix_t, newColumnVector is vec.vector_t -> matrix_t, - newSparseMatrix is (ColumnMajor () | RowMajor ()) -> { rows is number, columns is number } -> list<{ i is number, j is number, v is number }> -> matrix_t, + newSparseMatrix is { rows is number, columns is number } -> (ColumnMajor () | RowMajor ()) -> list<{ i is number, j is number, v is number }> -> matrix_t, enumerate is matrix_t -> list<{ i is number, j is number, v is number }>, format is matrix_t -> string, print is matrix_t -> (),
--- a/src/may/matrix/test/test_matrix.yeti Wed Nov 20 14:43:14 2013 +0000 +++ b/src/may/matrix/test/test_matrix.yeti Wed Nov 20 14:49:47 2013 +0000 @@ -245,12 +245,12 @@ ), "sparseSum-\(name)": \( - s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + s = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 2, v = 2 }, { i = 1, j = 1, v = 4 }, ]; - t = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + t = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 1, v = 7 }, { i = 1, j = 0, v = 5 }, { i = 1, j = 1, v = -4 }, // NB this means [1,1] -> 0, sparse zero @@ -261,7 +261,7 @@ compareMatrices tot (mat.sum (mat.toDense s) (mat.toDense t)) and compareMatrices tot (mat.sum s (mat.toDense t)) and compareMatrices tot - (mat.newSparseMatrix (RowMajor ()) { rows = 2, columns = 3 } [ + (mat.newSparseMatrix { rows = 2, columns = 3 } (RowMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 1, v = 7 }, { i = 0, j = 2, v = 2 }, @@ -288,12 +288,12 @@ ), "sparseDifference-\(name)": \( - s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + s = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 2, v = 2 }, { i = 1, j = 1, v = 4 }, ]; - t = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + t = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 1, v = 7 }, { i = 1, j = 0, v = 5 }, { i = 1, j = 1, v = 6 }, @@ -304,7 +304,7 @@ compareMatrices diff (mat.difference (mat.toDense s) (mat.toDense t)) and compareMatrices diff (mat.difference s (mat.toDense t)) and compareMatrices diff - (mat.newSparseMatrix (RowMajor ()) { rows = 2, columns = 3 } [ + (mat.newSparseMatrix { rows = 2, columns = 3 } (RowMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 1, v = -7 }, { i = 0, j = 2, v = 2 }, @@ -331,12 +331,12 @@ ), "sparseProduct-\(name)": \( - s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + s = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 2, v = 2 }, { i = 1, j = 1, v = 4 }, ]; - t = mat.newSparseMatrix (ColumnMajor ()) { rows = 3, columns = 2 } [ + t = mat.newSparseMatrix { rows = 3, columns = 2 } (ColumnMajor ()) [ { i = 0, j = 1, v = 7 }, { i = 1, j = 0, v = 5 }, { i = 2, j = 0, v = 6 }, @@ -347,7 +347,7 @@ compareMatrices prod (mat.product (mat.toDense s) (mat.toDense t)) and compareMatrices prod (mat.product s (mat.toDense t)) and compareMatrices prod - (mat.newSparseMatrix (RowMajor ()) { rows = 2, columns = 2 } [ + (mat.newSparseMatrix { rows = 2, columns = 2 } (RowMajor ()) [ { i = 0, j = 0, v = 12 }, { i = 0, j = 1, v = 7 }, { i = 1, j = 0, v = 20 }, @@ -397,10 +397,10 @@ (fromRows [[]])) and compareMatrices (mat.zeroMatrix { rows = 0, columns = 0 }) - (mat.newSparseMatrix (ColumnMajor ()) { rows = 0, columns = 0 } []) and + (mat.newSparseMatrix { rows = 0, columns = 0 } (ColumnMajor ()) []) and compareMatrices (mat.zeroMatrix { rows = 1, columns = 0 }) - (mat.newSparseMatrix (ColumnMajor ()) { rows = 1, columns = 0 } []) + (mat.newSparseMatrix { rows = 1, columns = 0 } (ColumnMajor ()) []) ), "asRows-\(name)": \( @@ -557,20 +557,20 @@ ), "newSparseMatrix-\(name)": \( - s = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + s = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 2, v = 2 }, { i = 1, j = 1, v = 4 }, ]; // If there are zeros in the entries list, they should not end up // in the sparse data - t = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + t = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = 0, j = 0, v = 1 }, { i = 0, j = 2, v = 0 }, { i = 1, j = 1, v = 4 }, ]; // Any out-of-range or non-integer i, j should be ignored too - u = mat.newSparseMatrix (ColumnMajor ()) { rows = 2, columns = 3 } [ + u = mat.newSparseMatrix { rows = 2, columns = 3 } (ColumnMajor ()) [ { i = -1, j = 0, v = 1 }, { i = 0, j = 4, v = 3 }, { i = 1, j = 1.5, v = 4 },