Skip to content

Commit

Permalink
feat: add selection method and selection view
Browse files Browse the repository at this point in the history
Create a new matrix (or a view) based on row and column indices
  • Loading branch information
stropitek committed Aug 4, 2016
1 parent eb06410 commit 59aa861
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
28 changes: 26 additions & 2 deletions src/abstractMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var util = require('./util');
var MatrixTransposeView = require('./views/transpose');
var MatrixRowView = require('./views/row');
var MatrixSubView = require('./views/sub');
var MatrixSelectionView = require('./views/selection');

function abstractMatrix(superCtor) {
if (superCtor === undefined) superCtor = Object;
Expand Down Expand Up @@ -1157,6 +1158,25 @@ function abstractMatrix(superCtor) {
return this;
}

/**
* Return a new matrix based on a selection of rows and columns
* @param {Array<number>} rowIndices - The row indices to select. Order matters and an index can be more than once.
* @param {Array<number>} columnIndices - The column indices to select. Order matters and an index can be use more than once.
* @returns {Matrix} The new matrix
*/
selection(rowIndices, columnIndices) {
var indices = util.checkIndices(this, rowIndices, columnIndices);
var newMatrix = new this.constructor(rowIndices.length, columnIndices.length);
for (var i = 0; i < indices.row.length; i++) {
var rowIndex = indices.row[i];
for (var j = 0; j < indices.column.length; j++) {
var columnIndex = indices.column[j];
newMatrix[i][j] = this.get(rowIndex, columnIndex);
}
}
return newMatrix;
}

/**
* Returns the trace of the matrix (sum of the diagonal elements)
* @returns {number}
Expand All @@ -1182,8 +1202,12 @@ function abstractMatrix(superCtor) {
return new MatrixRowView(this, row);
}

subMatrixView(rowIndices, columnIndices, startColumn, endColumn) {
return new MatrixSubView(this, rowIndices, columnIndices, startColumn, endColumn);
subMatrixView(startRow, endRow, startColumn, endColumn) {
return new MatrixSubView(this, startRow, endRow, startColumn, endColumn);
}

selectionView(rowIndices, columnIndices) {
return new MatrixSelectionView(this, rowIndices, columnIndices);
}
}

Expand Down
24 changes: 7 additions & 17 deletions src/views/sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ var BaseView = require('./base');
var util = require('../util');

class MatrixSubView extends BaseView {
constructor(matrix, rowIndices, columnIndices, startColumn, endColumn) {
var rowI, columnI;
if(typeof rowIndices === 'number') {
var startRow = rowIndices, endRow = columnIndices;
util.checkRange(matrix, startRow, endRow, startColumn, endColumn);
rowI = util.getRange(startRow, endRow);
columnI = util.getRange(startColumn, endColumn);
} else {
var indices = util.checkIndices(matrix, rowIndices, columnIndices);
rowI = indices.row;
columnI = indices.column;
}
super(matrix, rowI.length, columnI.length);
this.rowIndices = rowI;
this.columnIndices = columnI;
constructor(matrix, startRow, endRow, startColumn, endColumn) {
util.checkRange(matrix, startRow, endRow, startColumn, endColumn);
super(matrix, endRow - startRow + 1, endColumn - startColumn + 1);
this.startRow = startRow;
this.startColumn = startColumn;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(this.rowIndices[rowIndex], this.columnIndices[columnIndex] , value);
this.matrix.set(this.startRow + rowIndex, this.startColumn + columnIndex , value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(this.rowIndices[rowIndex], this.columnIndices[columnIndex]);
return this.matrix.get(this.startRow + rowIndex, this.startColumn + columnIndex);
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/matrix/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ describe('utility methods', function () {
}).should.throw(/Argument out of range/);
});

it('selection matrix', function () {
var matrix = new Matrix([[1, 2], [-5, 3], [2, 4]]);
var selMatrix = matrix.selection([2,1], [1]);
selMatrix.to2DArray().should.eql([[4],[3]]);
});

it('repeat matrix', function () {
var matrix = new Matrix([[1, 2], [3, 4]]);
matrix.repeat().to2DArray().should.eql([[1, 2], [3, 4]]);
Expand Down
20 changes: 1 addition & 19 deletions test/views/sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ var Matrix = require('../..');
describe('Sub view', function () {
it('should correctly remap coordinates', function () {
var m = Matrix.ones(5, 8);
var msv = m.subMatrixView([1,2], [2,1]);

m.get(1, 2).should.equal(1);
msv.set(0, 0, 5);
m.get(1, 2).should.equal(5);

m.get(2,1).should.equal(1);
m.set(2,1, 10);
msv.get(1,1).should.equal(10);

msv = m.subMatrixView(3,4,6,7);
var msv = m.subMatrixView(3,4,6,7);
m.get(4,7).should.equal(1);
msv.set(1, 1, 20);
m.get(4,7).should.equal(20);
Expand All @@ -30,13 +20,5 @@ describe('Sub view', function () {
(function () {
m.subMatrixView(0,1,0,2);
}).should.throw(RangeError);

(function () {
m.subMatrixView([1,1,2], [0,2]);
}).should.throw(RangeError);

(function () {
m.subMatrixView([1,1,2])
}).should.throw(TypeError);
});
});

0 comments on commit 59aa861

Please sign in to comment.