Skip to content

Commit 8b9eecb

Browse files
committed
feat: add repeat method
1 parent 89b4242 commit 8b9eecb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/matrix.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,20 @@ class Matrix extends Array {
370370
* rows of the matrix, and colRep times the number of columns of the matrix
371371
* @param {number} rowRep - Number of times the rows should be repeated
372372
* @param {number} colRep - Number of times the columns should be re
373+
* @example
374+
* var matrix = new Matrix([[1,2]]);
375+
* matrix.repeat(2); // [[1,2],[1,2]]
373376
*/
374-
rep(rowRep, colRep) {
377+
repeat(rowRep, colRep) {
375378
rowRep = rowRep || 1;
376-
colRep = colRep || 0;
379+
colRep = colRep || 1;
377380
var matrix = new Matrix(this.rows * rowRep, this.columns *colRep);
378-
381+
for(var i=0; i<rowRep; i++) {
382+
for(var j=0; j<colRep; j++) {
383+
matrix.setSubMatrix(this, this.rows*i, this.columns * j);
384+
}
385+
}
386+
return matrix;
379387
}
380388

381389
/**

test/matrix/utility.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,11 @@ describe('utility methods', function () {
120120
matrix.setSubMatrix([[1,2]], 1,1);
121121
}).should.throw(/Argument out of range/);
122122
});
123+
124+
it('repeat matrix', function () {
125+
var matrix = new Matrix([[1,2],[3,4]]);
126+
matrix.repeat().to2DArray().should.eql([[1,2],[3,4]]);
127+
matrix.repeat(2,2).to2DArray().should.eql([[1,2,1,2],[3,4,3,4],[1,2,1,2],[3,4,3,4]]);
128+
matrix.repeat(1,2).to2DArray().should.eql([[1,2,1,2],[3,4,3,4]]);
129+
});
123130
});

0 commit comments

Comments
 (0)