diff --git a/lib/type/Matrix.js b/lib/type/Matrix.js index fe9643b5d..16cd38705 100644 --- a/lib/type/Matrix.js +++ b/lib/type/Matrix.js @@ -82,12 +82,16 @@ module.exports = function (config) { }; /** - * Resize the matrix - * @param {Number[]} size + * Resize the matrix to the given size. Returns a copy of the matrix when + * `copy=true`, otherwise return the matrix itself (resize in place). + * + * @param {Number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. - * @return {Matrix} self The matrix itself is returned + * @param {boolean} [copy] Return a resized copy of the matrix + * + * @return {Matrix} The resized matrix */ Matrix.prototype.resize = function (size, defaultValue) { // must be implemented by each of the Matrix implementations diff --git a/lib/type/matrix/CcsMatrix.js b/lib/type/matrix/CcsMatrix.js index 195e9a529..e270374a0 100644 --- a/lib/type/matrix/CcsMatrix.js +++ b/lib/type/matrix/CcsMatrix.js @@ -395,7 +395,7 @@ module.exports = function (math) { }; /** - * Resize the matrix to the given size. Returns a copy of the matrix when the + * Resize the matrix to the given size. Returns a copy of the matrix when * `copy=true`, otherwise return the matrix itself (resize in place). * * @param {Number[]} size The new size the matrix should have. @@ -404,7 +404,7 @@ module.exports = function (math) { * be filled with zeros. * @param {boolean} [copy] Return a resized copy of the matrix * - * @return {CcsMatrix} The resized matrix + * @return {Matrix} The resized matrix */ CcsMatrix.prototype.resize = function (size, defaultValue, copy) { // validate arguments diff --git a/lib/type/matrix/DenseMatrix.js b/lib/type/matrix/DenseMatrix.js index d1618f8de..07e675126 100644 --- a/lib/type/matrix/DenseMatrix.js +++ b/lib/type/matrix/DenseMatrix.js @@ -326,19 +326,30 @@ module.exports = function (math) { } /** - * Resize the matrix - * @param {Number[]} size + * Resize the matrix to the given size. Returns a copy of the matrix when + * `copy=true`, otherwise return the matrix itself (resize in place). + * + * @param {Number[]} size The new size the matrix should have. * @param {*} [defaultValue=0] Default value, filled in on new entries. * If not provided, the matrix elements will * be filled with zeros. - * @return {DenseMatrix} self The matrix itself is returned + * @param {boolean} [copy] Return a resized copy of the matrix + * + * @return {Matrix} The resized matrix */ - DenseMatrix.prototype.resize = function (size, defaultValue) { - this._size = object.clone(size); - this._data = array.resize(this._data, this._size, defaultValue); - - // return the matrix itself - return this; + DenseMatrix.prototype.resize = function (size, defaultValue, copy) { + // matrix to resize + var m = copy ? this.clone() : this; + // resize matrix + return _resize(m, size, defaultValue); + }; + + var _resize = function (matrix, size, defaultValue) { + // resize matrix + matrix._size = object.clone(size); + matrix._data = array.resize(matrix._data, matrix._size, defaultValue); + // return matrix + return matrix; }; /** @@ -369,7 +380,7 @@ module.exports = function (math) { if (changed) { // resize only when size is changed - matrix.resize(newSize, defaultValue); + _resize(matrix, newSize, defaultValue); } } diff --git a/test/type/matrix/DenseMatrix.test.js b/test/type/matrix/DenseMatrix.test.js index c701a345f..8e4bd17f6 100644 --- a/test/type/matrix/DenseMatrix.test.js +++ b/test/type/matrix/DenseMatrix.test.js @@ -171,6 +171,36 @@ describe('DenseMatrix', function() { m.resize([3], math.uninitialized); assert.deepEqual(m.valueOf(), arr(uninit, uninit, uninit)); }); + + it('should return a different matrix when copy=true', function() { + var m1 = new DenseMatrix( + [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] + ]); + var m2 = m1.resize([2, 2], 0, true); + assert(m1 !== m2); + // original matrix cannot be modified + assert.deepEqual(m1._size, [4, 4]); + assert.deepEqual( + m1._data, + [ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0] + ]); + // new matrix should have correct size + assert.deepEqual(m2._size, [2, 2]); + assert.deepEqual( + m2._data, + [ + [0, 0], + [0, 0] + ]); + }); }); describe('get', function () {