mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Mixed matrix input now always returns a Matrix (needed to mace collection static)
This commit is contained in:
parent
e61d2f965b
commit
abff8efdbc
@ -8,12 +8,13 @@ Math.js contains a number of global options. The options are defined in
|
||||
The precision is used by the function `math.format` and by the `toString`
|
||||
functions of units, complex numbers, and matrices.
|
||||
|
||||
- `matrix.defaultType`. The default type of matrix output for functions. Available
|
||||
values are: `"array"` or `"matrix"` (default).
|
||||
- `matrix.defaultType`. The default type of matrix output for functions.
|
||||
Available values are: `"array"` or `"matrix"` (default).
|
||||
Where possible, the type of matrix output from functions is determined from
|
||||
the function input: An array as input will return an Array, a Matrix as input
|
||||
will return a Matrix. In case of mixed input or no matrix as input, the type
|
||||
of output is determined by the option `math.matrix.defaultType`.
|
||||
will return a Matrix. In case of no matrix as input, the type of output is
|
||||
determined by the option `math.matrix.defaultType`. In case of mixed matrix
|
||||
inputs, a matrix will be returned always.
|
||||
|
||||
Example usage:
|
||||
|
||||
|
||||
@ -5,7 +5,8 @@ module.exports = function (math) {
|
||||
collection = require('../../type/collection.js'),
|
||||
|
||||
isNumber = util.number.isNumber,
|
||||
isInteger = util.number.isInteger;
|
||||
isInteger = util.number.isInteger,
|
||||
isArray = Array.isArray;
|
||||
|
||||
/**
|
||||
* Create an identity matrix with size m x n
|
||||
|
||||
@ -4,7 +4,8 @@ module.exports = function (math) {
|
||||
Matrix = require('../../type/Matrix.js'),
|
||||
collection = require('../../type/collection.js'),
|
||||
|
||||
array = util.array;
|
||||
array = util.array,
|
||||
isArray = Array.isArray;
|
||||
|
||||
/**
|
||||
* create a matrix filled with zeros
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
// utility methods for arrays and matrices
|
||||
|
||||
var util = require('../util/index.js'),
|
||||
options = require('../options.js'),
|
||||
|
||||
Matrix = require('./Matrix.js'),
|
||||
|
||||
@ -104,7 +103,7 @@ exports.deepMap2 = function deepMap2(array1, array2, callback) {
|
||||
else if (array2 instanceof Matrix) {
|
||||
// callback(array, matrix)
|
||||
res = deepMap2(array1, array2.valueOf(), callback);
|
||||
return (options.matrix.defaultType === 'array') ? res : new Matrix(res);
|
||||
return new Matrix(res);
|
||||
}
|
||||
else {
|
||||
// callback(array, object)
|
||||
@ -125,7 +124,7 @@ exports.deepMap2 = function deepMap2(array1, array2, callback) {
|
||||
// callback(matrix, array)
|
||||
// callback(matrix, object)
|
||||
res = deepMap2(array1.valueOf(), array2, callback);
|
||||
return (options.matrix.defaultType === 'array') ? res : new Matrix(res);
|
||||
return new Matrix(res);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@ -63,30 +63,12 @@ describe('add', function() {
|
||||
});
|
||||
|
||||
it('should add a matrix and an array correctly', function() {
|
||||
var old = math.options.matrix.defaultType;
|
||||
|
||||
var a = [1,2,3];
|
||||
var b = math.matrix([3,2,1]);
|
||||
var c = add(a, b);
|
||||
|
||||
// test default option value
|
||||
assert.ok(c instanceof math.type.Matrix);
|
||||
assert.deepEqual(c, math.matrix([4,4,4]));
|
||||
|
||||
// test option default === 'array'
|
||||
math.options.matrix.defaultType = 'array';
|
||||
var d = add(a, b);
|
||||
assert.ok(Array.isArray(d));
|
||||
assert.deepEqual(d, [4,4,4]);
|
||||
|
||||
// test option default === 'matrix'
|
||||
math.options.matrix.defaultType = 'matrix';
|
||||
var e = add(a, b);
|
||||
assert.ok(e instanceof math.type.Matrix);
|
||||
assert.deepEqual(e, math.matrix([4,4,4]));
|
||||
|
||||
// restore original setting
|
||||
math.options.matrix.defaultType = old;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user