Skip to content

Commit

Permalink
feat: add transposeView
Browse files Browse the repository at this point in the history
This method allows to manipulate a transpose of a matrix without the
need to copy all of its data to a new instance.
  • Loading branch information
targos committed Aug 3, 2016
1 parent cbefc9b commit fb0a0c9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/abstractMatrix.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

var arrayUtils = require('ml-array-utils');
module.exports = abstractMatrix;

var arrayUtils = require('ml-array-utils');
var util = require('./util');
var MatrixTransposeView = require('./matrixTransposeView');

function abstractMatrix(superCtor) {
if (superCtor === undefined) superCtor = Object;
Expand All @@ -15,8 +17,6 @@ function abstractMatrix(superCtor) {
* @param {number} [nColumns] - Number of columns of the new matrix
*/
class Matrix extends superCtor {


/**
* Constructs a Matrix with the chosen dimensions from a 1D array
* @param {number} newRows - Number of rows
Expand Down Expand Up @@ -71,7 +71,6 @@ function abstractMatrix(superCtor) {
* @returns {Matrix} - The new matrix
*/
static empty(rows, columns) {
debugger;
return new this(rows, columns);
}

Expand Down Expand Up @@ -232,20 +231,6 @@ function abstractMatrix(superCtor) {
return this;
}

/**
* Creates an exact and independent copy of the matrix
* @returns {Matrix}
*/
clone() {
var newMatrix = new this.constructor(this.rows, this.columns);
for (var row = 0; row < this.rows; row++) {
for (var column = 0; column < this.columns; column++) {
newMatrix.set(row, column, this.get(row, column));
}
}
return newMatrix;
}

/**
* Returns a new 1D array filled row by row with the matrix values
* @returns {Array}
Expand Down Expand Up @@ -1184,6 +1169,13 @@ function abstractMatrix(superCtor) {
}
return trace;
}

/*
Matrix views
*/
transposeView() {
return new MatrixTransposeView(this);
}
}

Matrix.prototype.klass = 'Matrix';
Expand Down Expand Up @@ -1425,5 +1417,3 @@ function abstractMatrix(superCtor) {

return Matrix;
}

module.exports = abstractMatrix;
15 changes: 15 additions & 0 deletions src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ class Matrix extends abstractMatrix(Array) {
return this[rowIndex][columnIndex];
}

/**
* Creates an exact and independent copy of the matrix
* @returns {Matrix}
*/
clone() {
var newMatrix = new this.constructor(this.rows, this.columns);
for (var row = 0; row < this.rows; row++) {
for (var column = 0; column < this.columns; column++) {
newMatrix.set(row, column, this.get(row, column));
}
}
return newMatrix;
}

/**
* Removes a row from the given index
* @param {number} index - Row index
Expand Down Expand Up @@ -121,3 +135,4 @@ class Matrix extends abstractMatrix(Array) {
}

module.exports = Matrix;
Matrix.abstractMatrix = abstractMatrix;
23 changes: 23 additions & 0 deletions src/matrixTransposeView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var abstractMatrix = require('./abstractMatrix');

class MatrixTransposeView extends abstractMatrix() {
constructor(matrix) {
super();
this.matrix = matrix;
this.rows = matrix.columns;
this.columns = matrix.rows;
}

set(rowIndex, columnIndex, value) {
this.matrix.set(columnIndex, rowIndex, value);
return this;
}

get(rowIndex, columnIndex) {
return this.matrix.get(columnIndex, rowIndex);
}
}

module.exports = MatrixTransposeView;
23 changes: 23 additions & 0 deletions test/views/transpose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

var Matrix = require('../..');

describe('Matrix views', function () {
describe('Transpose view', function () {
it('should set and get opposite coordinates', function () {
var m = Matrix.ones(5, 8);
var mtv = m.transposeView();

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

m.set(0, 0, 6);
mtv.get(0, 0).should.equal(6);

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

0 comments on commit fb0a0c9

Please sign in to comment.