From abff8efdbc142ff3fdd7a2a2cfe9c4fd8235b0f4 Mon Sep 17 00:00:00 2001 From: josdejong Date: Sat, 19 Oct 2013 22:01:10 +0200 Subject: [PATCH] Mixed matrix input now always returns a Matrix (needed to mace collection static) --- docs/options.md | 9 +++++---- lib/function/matrix/eye.js | 3 ++- lib/function/matrix/zeros.js | 3 ++- lib/type/collection.js | 5 ++--- test/function/arithmetic/add.test.js | 18 ------------------ 5 files changed, 11 insertions(+), 27 deletions(-) diff --git a/docs/options.md b/docs/options.md index 4472817e9..d5e777222 100644 --- a/docs/options.md +++ b/docs/options.md @@ -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: diff --git a/lib/function/matrix/eye.js b/lib/function/matrix/eye.js index 5eb2646b5..ecf608a47 100644 --- a/lib/function/matrix/eye.js +++ b/lib/function/matrix/eye.js @@ -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 diff --git a/lib/function/matrix/zeros.js b/lib/function/matrix/zeros.js index 831997f46..7b0210dc0 100644 --- a/lib/function/matrix/zeros.js +++ b/lib/function/matrix/zeros.js @@ -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 diff --git a/lib/type/collection.js b/lib/type/collection.js index 45a889392..f919a387e 100644 --- a/lib/type/collection.js +++ b/lib/type/collection.js @@ -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 { diff --git a/test/function/arithmetic/add.test.js b/test/function/arithmetic/add.test.js index 2b99fe5a4..6e2d1a14f 100644 --- a/test/function/arithmetic/add.test.js +++ b/test/function/arithmetic/add.test.js @@ -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; }); });