CCS tests

This commit is contained in:
rjbaucells 2015-03-06 22:21:36 -05:00
parent b7654a9ac0
commit a072041fac
2 changed files with 87 additions and 21 deletions

View File

@ -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 = [];

View File

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