Removed array/matrix support from function ifElse

This commit is contained in:
jos 2014-06-08 21:22:57 +02:00
parent 3399909318
commit 45982fbba0
3 changed files with 6 additions and 106 deletions

View File

@ -7,8 +7,7 @@ module.exports = {
'description': 'Executes a conditional expression.',
'examples': [
'ifElse(10 > 0, 1, 0)',
'ifElse("", true, false)',
'ifElse([4, 6, 0, -1], true, false)'
'ifElse("", true, false)'
],
'seealso': []
};

View File

@ -7,33 +7,26 @@ module.exports = function (math) {
Unit = require('../../type/Unit'),
collection = require('../../type/collection'),
deepEqual = util.object.deepEqual,
isNumber = util.number.isNumber,
isString = util.string.isString,
isBoolean = util['boolean'].isBoolean,
isComplex = Complex.isComplex,
isUnit = Unit.isUnit,
isCollection = collection.isCollection;
isUnit = Unit.isUnit;
/**
* Execute a conditional expression.
*
* In case of a matrix or array, the test is done element wise, the
* true and false part can be either a matrix/array with the same size
* of the condition, or a scalar value.
*
* Syntax:
*
* math.ifElse(condition, trueExpr, falseExpr
* math.ifElse(condition, trueExpr, falseExpr)
*
* Examples:
*
* var math = mathjs();
*
* math.ifElse(true, 'yes', 'no'); // returns 'yes'
* math.ifElse([4, 6, 0, -1], true, false); // returns [true, true, false, true]
*
* @param {Number | Boolean | String | Complex | BigNumber | Unit | Matrix | Array} condition
* @param {Number | Boolean | String | Complex | BigNumber | Unit} condition
* The conditional expression
* @param {*} trueExpr The true expression
* @param {*} falseExpr The false expression
@ -68,63 +61,6 @@ module.exports = function (math) {
return falseExpr;
}
if (isCollection(condition)) {
return _ifElseCollection(condition, trueExpr, falseExpr);
}
throw new math.error.UnsupportedTypeError('ifElse', math['typeof'](condition));
};
/**
* Execute the if-else condition element wise
* @param {Matrix | Array} condition
* @param {*} trueExpr
* @param {*} falseExpr
* @returns {*}
* @private
*/
function _ifElseCollection(condition, trueExpr, falseExpr) {
var asMatrix = (condition instanceof Matrix) ||
(trueExpr instanceof Matrix) ||
(falseExpr instanceof Matrix);
// change an array into a matrix
if (!(condition instanceof Matrix)) condition = new Matrix(condition);
// change the true expression into a matrix and check whether the size
// matches with the condition matrix
if (isCollection(trueExpr)) {
if (!(trueExpr instanceof Matrix)) trueExpr = new Matrix(trueExpr);
if (!deepEqual(condition.size(), trueExpr.size())) {
throw new RangeError('Dimension mismatch ([' +
condition.size().join(', ') + '] != [' +
trueExpr.size().join(', ')
+ '])');
throw new math.error.DimensionError(condition.size(), trueExpr.size());
}
}
// change the false expression into a matrix and check whether the size
// matches with the condition matrix
if (isCollection(falseExpr)) {
if (!(falseExpr instanceof Matrix)) falseExpr = new Matrix(falseExpr);
if (!deepEqual(condition.size(), falseExpr.size())) {
throw new math.error.DimensionError(condition.size(), falseExpr.size());
}
}
// do the actual conditional test element wise
var trueIsMatrix = trueExpr instanceof Matrix,
falseIsMatrix = falseExpr instanceof Matrix;
var result = condition.map(function (value, index) {
return math.ifElse(value,
trueIsMatrix ? trueExpr.get(index) : trueExpr,
falseIsMatrix ? falseExpr.get(index) : falseExpr
);
});
return asMatrix ? result : result.valueOf();
}
};

View File

@ -50,43 +50,6 @@ describe('ifElse', function() {
assert.equal(ifElse(undefined, 1, 0), 0);
});
it('should evaluate array conditions', function() {
assert.deepEqual(ifElse([1, 0, 1], 1, 0), [1, 0, 1]);
assert.deepEqual(ifElse([[1, 0], [0, 0]], 1, 0), [[1, 0], [0, 0]]);
assert.deepEqual(ifElse([[1, 1], [1, 1]], [[1,2],[3,4]], [[5,6],[7,8]]), [[1,2],[3,4]]);
assert.deepEqual(ifElse([[1, 0], [0, 1]], 123, [[5,6],[7,8]]), [[123,6],[7,123]]);
assert.deepEqual(ifElse([[1, 0], [0, 1]], [[1,2],[3,4]], 123), [[1,123],[123,4]]);
assert.deepEqual(ifElse([[0, 0], [0, 0]], [[1,2],[3,4]], [[5,6],[7,8]]), [[5,6],[7,8]]);
});
it('should evaluate matrix conditions', function() {
assert.deepEqual(ifElse(math.matrix([1, 0, 1]), 1, 0), math.matrix([1, 0, 1]));
assert.deepEqual(ifElse(math.matrix([[1, 0], [0, 0]]), 1, 0),
math.matrix([[1, 0], [0, 0]]));
assert.deepEqual(ifElse(math.matrix([[1, 1], [1, 1]]), math.matrix([[1,2],[3,4]]), math.matrix([[5,6],[7,8]])),
math.matrix([[1,2],[3,4]]));
assert.deepEqual(ifElse(math.matrix([[1, 0], [0, 1]]), 123, [[5,6],[7,8]]),
math.matrix([[123,6],[7,123]]));
assert.deepEqual(ifElse(math.matrix([[1, 0], [0, 1]]), math.matrix([[1,2],[3,4]]), 123),
math.matrix([[1,123],[123,4]]));
assert.deepEqual(ifElse(math.matrix([[0, 0], [0, 0]]), math.matrix([[1,2],[3,4]]), math.matrix([[5,6],[7,8]])),
math.matrix([[5,6],[7,8]]));
});
it('should throw an error when matrix dimensions mismatch', function() {
assert.throws(function () {
ifElse(math.matrix([[1, 1], [1, 1]]), math.matrix([[1,2,3],[4,5,6]]), 1);
});
assert.throws(function () {
ifElse(math.matrix([[1, 1], [1, 1]]), 1, math.matrix([[1,2,3],[4,5,6]]));
});
assert.throws(function () {
ifElse(math.matrix([[1, 1], [1, 1]]), 1, math.matrix([1,2]));
});
});
it('should throw an error if called with invalid number of arguments', function() {
assert.throws(function() { ifElse(true); });
assert.throws(function() { ifElse(true, true); });
@ -94,6 +57,8 @@ describe('ifElse', function() {
});
it('should throw an error if called with invalid type of arguments', function() {
assert.throws(function() { ifElse([], 1, 0); }, math.type.UnsupportedTypeError);
assert.throws(function() { ifElse(math.matrix(), 1, 0); }, math.type.UnsupportedTypeError);
assert.throws(function() { ifElse(new Date(), 1, 0); }, math.type.UnsupportedTypeError);
assert.throws(function() { ifElse(/regexp/, 1, 0); }, math.type.UnsupportedTypeError);
});