Skip to content

Commit fb0a0c9

Browse files
committed
feat: add transposeView
This method allows to manipulate a transpose of a matrix without the need to copy all of its data to a new instance.
1 parent cbefc9b commit fb0a0c9

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

src/abstractMatrix.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

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

5+
var arrayUtils = require('ml-array-utils');
56
var util = require('./util');
7+
var MatrixTransposeView = require('./matrixTransposeView');
68

79
function abstractMatrix(superCtor) {
810
if (superCtor === undefined) superCtor = Object;
@@ -15,8 +17,6 @@ function abstractMatrix(superCtor) {
1517
* @param {number} [nColumns] - Number of columns of the new matrix
1618
*/
1719
class Matrix extends superCtor {
18-
19-
2020
/**
2121
* Constructs a Matrix with the chosen dimensions from a 1D array
2222
* @param {number} newRows - Number of rows
@@ -71,7 +71,6 @@ function abstractMatrix(superCtor) {
7171
* @returns {Matrix} - The new matrix
7272
*/
7373
static empty(rows, columns) {
74-
debugger;
7574
return new this(rows, columns);
7675
}
7776

@@ -232,20 +231,6 @@ function abstractMatrix(superCtor) {
232231
return this;
233232
}
234233

235-
/**
236-
* Creates an exact and independent copy of the matrix
237-
* @returns {Matrix}
238-
*/
239-
clone() {
240-
var newMatrix = new this.constructor(this.rows, this.columns);
241-
for (var row = 0; row < this.rows; row++) {
242-
for (var column = 0; column < this.columns; column++) {
243-
newMatrix.set(row, column, this.get(row, column));
244-
}
245-
}
246-
return newMatrix;
247-
}
248-
249234
/**
250235
* Returns a new 1D array filled row by row with the matrix values
251236
* @returns {Array}
@@ -1184,6 +1169,13 @@ function abstractMatrix(superCtor) {
11841169
}
11851170
return trace;
11861171
}
1172+
1173+
/*
1174+
Matrix views
1175+
*/
1176+
transposeView() {
1177+
return new MatrixTransposeView(this);
1178+
}
11871179
}
11881180

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

14261418
return Matrix;
14271419
}
1428-
1429-
module.exports = abstractMatrix;

src/matrix.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ class Matrix extends abstractMatrix(Array) {
5151
return this[rowIndex][columnIndex];
5252
}
5353

54+
/**
55+
* Creates an exact and independent copy of the matrix
56+
* @returns {Matrix}
57+
*/
58+
clone() {
59+
var newMatrix = new this.constructor(this.rows, this.columns);
60+
for (var row = 0; row < this.rows; row++) {
61+
for (var column = 0; column < this.columns; column++) {
62+
newMatrix.set(row, column, this.get(row, column));
63+
}
64+
}
65+
return newMatrix;
66+
}
67+
5468
/**
5569
* Removes a row from the given index
5670
* @param {number} index - Row index
@@ -121,3 +135,4 @@ class Matrix extends abstractMatrix(Array) {
121135
}
122136

123137
module.exports = Matrix;
138+
Matrix.abstractMatrix = abstractMatrix;

src/matrixTransposeView.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var abstractMatrix = require('./abstractMatrix');
4+
5+
class MatrixTransposeView extends abstractMatrix() {
6+
constructor(matrix) {
7+
super();
8+
this.matrix = matrix;
9+
this.rows = matrix.columns;
10+
this.columns = matrix.rows;
11+
}
12+
13+
set(rowIndex, columnIndex, value) {
14+
this.matrix.set(columnIndex, rowIndex, value);
15+
return this;
16+
}
17+
18+
get(rowIndex, columnIndex) {
19+
return this.matrix.get(columnIndex, rowIndex);
20+
}
21+
}
22+
23+
module.exports = MatrixTransposeView;

test/views/transpose.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var Matrix = require('../..');
4+
5+
describe('Matrix views', function () {
6+
describe('Transpose view', function () {
7+
it('should set and get opposite coordinates', function () {
8+
var m = Matrix.ones(5, 8);
9+
var mtv = m.transposeView();
10+
11+
m.get(1, 0).should.equal(1);
12+
mtv.set(0, 1, 5);
13+
m.get(1, 0).should.equal(5);
14+
15+
m.set(0, 0, 6);
16+
mtv.get(0, 0).should.equal(6);
17+
18+
m.set(2, 1, 10);
19+
mtv.get(2, 1).should.equal(1);
20+
mtv.get(1, 2).should.equal(10);
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)