94 lines
2.2 KiB
JavaScript

var math = require('../../math.js'),
util = require('../../util/index.js'),
Complex = require('../../type/Complex.js').Complex,
Matrix = require('../../type/Matrix.js').Matrix,
Unit = require('../../type/Unit.js').Unit,
collection = require('../../type/collection.js'),
isNumber = util.number.isNumber,
isString = util.string.isString,
isComplex = Complex.isComplex,
isUnit = Unit.isUnit,
isCollection = collection.isCollection;
/**
* Subtract two values
*
* x - y
* subtract(x, y)
*
* For matrices, the function is evaluated element wise.
*
* @param {Number | Complex | Unit | Array | Matrix} x
* @param {Number | Complex | Unit | Array | Matrix} y
* @return {Number | Complex | Unit | Array | Matrix} res
*/
math.subtract = function subtract(x, y) {
if (arguments.length != 2) {
throw new util.error.ArgumentsError('subtract', arguments.length, 2);
}
if (isNumber(x)) {
if (isNumber(y)) {
// number - number
return x - y;
}
else if (isComplex(y)) {
// number - complex
return Complex.create (
x - y.re,
- y.im
);
}
}
else if (isComplex(x)) {
if (isNumber(y)) {
// complex - number
return Complex.create (
x.re - y,
x.im
)
}
else if (isComplex(y)) {
// complex - complex
return Complex.create (
x.re - y.re,
x.im - y.im
)
}
}
else if (isUnit(x)) {
if (isUnit(y)) {
if (!x.equalBase(y)) {
throw new Error('Units do not match');
}
if (x.value == null) {
throw new Error('Unit on left hand side of operator - has an undefined value');
}
if (y.value == null) {
throw new Error('Unit on right hand side of operator - has an undefined value');
}
var res = x.clone();
res.value -= y.value;
res.fixPrefix = false;
return res;
}
}
if (isCollection(x) || isCollection(y)) {
return collection.map2(x, y, subtract);
}
if (x.valueOf() !== x || y.valueOf() !== y) {
// fallback on the objects primitive values
return subtract(x.valueOf(), y.valueOf());
}
throw new util.error.UnsupportedTypeError('subtract', x, y);
};