From a072041facb1ffe2f0056dc78bfafdac85ffab0e Mon Sep 17 00:00:00 2001 From: rjbaucells Date: Fri, 6 Mar 2015 22:21:36 -0500 Subject: [PATCH] CCS tests --- lib/type/matrix/CcsFormat.js | 67 ++++++++++++++++++++++++++++++++++++ test/type/Matrix.test.js | 41 +++++++++++----------- 2 files changed, 87 insertions(+), 21 deletions(-) diff --git a/lib/type/matrix/CcsFormat.js b/lib/type/matrix/CcsFormat.js index 71a844013..1029f1cc2 100644 --- a/lib/type/matrix/CcsFormat.js +++ b/lib/type/matrix/CcsFormat.js @@ -299,6 +299,73 @@ module.exports = function (math) { return _get(this, index); }; + /** + * Create a new matrix with the results of the callback function executed on + * each entry of the matrix. + * @param {function} callback The callback function is invoked with three + * parameters: the value of the element, the index + * of the element, and the DenseFormat being traversed. + * @param {Matrix} matrix The Matrix instance + * @return {DenseFormat} matrix + */ + CcsFormat.prototype.map = function (callback, matrix) { + // result arrays + var values = []; + var index = []; + var ptr = []; + // values index + var k = 0; + // rows and columns + var rows = this._size[0]; + var columns = this._size[1]; + // invoke callback + var invoke = function (v, x, y) { + // invoke callback + v = callback(v, [x, y], matrix); + // check value != 0 + if (!math.equal(v, 0)) { + // store value + values.push(v); + // index + index.push(i); + } + }; + // loop columns + for (var j = 0; j < columns; j++) { + // k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1] + var k0 = this._ptr[j]; + var k1 = this._ptr[j + 1]; + // column pointer + var p = 0; + // check k is within [k0, k1[ + while (k >= k0 && k < k1) { + // row index + var i = this._index[k]; + // zero values + for (var x = p; x < i; x++) + invoke(0, x, j); + // value @ k + invoke(this._values[k], i, j); + // increment k + k++; + // update pointer + p = j + 1; + } + // zero values + for (var y = p; y < rows; y++) + invoke(0, y, j); + } + // store number of values in ptr + ptr.push(values.length); + // return ccs + return new CcsFormat({ + values: values, + index: index, + ptr: ptr, + size: object.clone(this._size) + }); + }; + CcsFormat.diagonal = function (rows, columns, value) { // create arrays var values = []; diff --git a/test/type/Matrix.test.js b/test/type/Matrix.test.js index bacc6d6d2..951db1552 100644 --- a/test/type/Matrix.test.js +++ b/test/type/Matrix.test.js @@ -535,32 +535,33 @@ describe('matrix', function() { }); it('should work on empty matrices', function() { - var m, m2; - m = new Matrix([]); - m2 = m.map(function (value) { return value * 2; }); - assert.deepEqual(m2.valueOf(), []); + var m = new Matrix([]); + var m2 = m.map(function (value) { return value * 2; }); + assert.deepEqual(m2.toArray(), []); }); it('should invoke callback with parameters value, index, obj', function() { var m = new Matrix([[1,2,3], [4,5,6]]); - assert.deepEqual(m.map(function (value, index, obj) { + var m2 = m.map(function (value, index, obj) { return math.clone([value, index, obj === m]); - }).valueOf(), [ + }); + + assert.deepEqual( + m2.toArray(), [ - [1, [0, 0], true ], - [2, [0, 1], true ], - [3, [0, 2], true ] - ], - [ - [4, [1, 0], true ], - [5, [1, 1], true ], - [6, [1, 2], true ] - ] - ]); - + [ + [1, [0, 0], true ], + [2, [0, 1], true ], + [3, [0, 2], true ] + ], + [ + [4, [1, 0], true ], + [5, [1, 1], true ], + [6, [1, 2], true ] + ] + ]); }); - }); describe('forEach', function() { @@ -621,11 +622,9 @@ describe('matrix', function() { var m = new Matrix([[1,2,3], [4,5,6]]); var m4 = m.clone(); assert.deepEqual(m4.size(), [2,3]); - assert.deepEqual(m4.valueOf(), [[1,2,3], [4,5,6]]); + assert.deepEqual(m4.toArray(), [[1,2,3], [4,5,6]]); }); - }); - }); // TODO: extensively test Matrix