mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Dense Matrix resize
This commit is contained in:
parent
090911f3e2
commit
9b481cc86f
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user