mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
168 lines
4.4 KiB
JavaScript
168 lines
4.4 KiB
JavaScript
module.exports = function(math) {
|
|
var util = require('../../util/index'),
|
|
|
|
BigNumber = math.type.BigNumber,
|
|
Complex = require('../../type/Complex'),
|
|
Matrix = require('../../type/Matrix'),
|
|
Unit = require('../../type/Unit'),
|
|
collection = require('../../type/collection'),
|
|
|
|
isNumber = util.number.isNumber,
|
|
isBoolean = util['boolean'].isBoolean,
|
|
isComplex = Complex.isComplex,
|
|
isUnit = Unit.isUnit,
|
|
isCollection = collection.isCollection;
|
|
|
|
/**
|
|
* Divide two values, `x / y`.
|
|
* To divide matrices, `x` is multiplied with the inverse of `y`: `x * inv(y)`.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.divide(x, y)
|
|
*
|
|
* Examples:
|
|
*
|
|
* math.divide(2, 3); // returns Number 0.6666666666666666
|
|
*
|
|
* var a = math.complex(5, 14);
|
|
* var b = math.complex(4, 1);
|
|
* math.divide(a, b); // returns Complex 2 + 3i
|
|
*
|
|
* var c = [[7, -6], [13, -4]];
|
|
* var d = [[1, 2], [4, 3]];
|
|
* math.divide(c, d); // returns Array [[-9, 4], [-11, 6]]
|
|
*
|
|
* var e = math.unit('18 km');
|
|
* math.divide(e, 4.5); // returns Unit 4 km
|
|
*
|
|
* See also:
|
|
*
|
|
* multiply
|
|
*
|
|
* @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix} x Numerator
|
|
* @param {Number | BigNumber | Boolean | Complex | Array | Matrix} y Denominator
|
|
* @return {Number | BigNumber | Complex | Unit | Array | Matrix} Quotient, `x / y`
|
|
*/
|
|
math.divide = function divide(x, y) {
|
|
if (arguments.length != 2) {
|
|
throw new math.error.ArgumentsError('divide', arguments.length, 2);
|
|
}
|
|
|
|
if (isNumber(x)) {
|
|
if (isNumber(y)) {
|
|
// number / number
|
|
return x / y;
|
|
}
|
|
else if (isComplex(y)) {
|
|
// number / complex
|
|
return _divideComplex(new Complex(x, 0), y);
|
|
}
|
|
}
|
|
|
|
if (isComplex(x)) {
|
|
if (isComplex(y)) {
|
|
// complex / complex
|
|
return _divideComplex(x, y);
|
|
}
|
|
else if (isNumber(y)) {
|
|
// complex / number
|
|
return _divideComplex(x, new Complex(y, 0));
|
|
}
|
|
}
|
|
|
|
if (x instanceof BigNumber) {
|
|
// try to convert to big number
|
|
if (isNumber(y)) {
|
|
y = BigNumber.convert(y);
|
|
}
|
|
else if (isBoolean(y)) {
|
|
y = new BigNumber(y ? 1 : 0);
|
|
}
|
|
|
|
if (y instanceof BigNumber) {
|
|
return x.div(y);
|
|
}
|
|
|
|
// downgrade to Number
|
|
return divide(x.toNumber(), y);
|
|
}
|
|
if (y instanceof BigNumber) {
|
|
// try to convert to big number
|
|
if (isNumber(x)) {
|
|
x = BigNumber.convert(x);
|
|
}
|
|
else if (isBoolean(x)) {
|
|
x = new BigNumber(x ? 1 : 0);
|
|
}
|
|
|
|
if (x instanceof BigNumber) {
|
|
return x.div(y)
|
|
}
|
|
|
|
// downgrade to Number
|
|
return divide(x, y.toNumber());
|
|
}
|
|
|
|
if (isUnit(x)) {
|
|
if (isNumber(y)) {
|
|
var res = x.clone();
|
|
res.value /= y;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
if (isCollection(x)) {
|
|
if (isCollection(y)) {
|
|
// TODO: implement matrix right division using pseudo inverse
|
|
// http://www.mathworks.nl/help/matlab/ref/mrdivide.html
|
|
// http://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html
|
|
// http://stackoverflow.com/questions/12263932/how-does-gnu-octave-matrix-division-work-getting-unexpected-behaviour
|
|
return math.multiply(x, math.inv(y));
|
|
}
|
|
else {
|
|
// matrix / scalar
|
|
return collection.deepMap2(x, y, divide);
|
|
}
|
|
}
|
|
|
|
if (isCollection(y)) {
|
|
// TODO: implement matrix right division using pseudo inverse
|
|
return math.multiply(x, math.inv(y));
|
|
}
|
|
|
|
if (isBoolean(x)) {
|
|
return divide(+x, y);
|
|
}
|
|
if (isBoolean(y)) {
|
|
return divide(x, +y);
|
|
}
|
|
|
|
throw new math.error.UnsupportedTypeError('divide', math['typeof'](x), math['typeof'](y));
|
|
};
|
|
|
|
/**
|
|
* Divide two complex numbers. x / y or divide(x, y)
|
|
* @param {Complex} x
|
|
* @param {Complex} y
|
|
* @return {Complex} res
|
|
* @private
|
|
*/
|
|
function _divideComplex (x, y) {
|
|
var den = y.re * y.re + y.im * y.im;
|
|
if (den != 0) {
|
|
return new Complex(
|
|
(x.re * y.re + x.im * y.im) / den,
|
|
(x.im * y.re - x.re * y.im) / den
|
|
);
|
|
}
|
|
else {
|
|
// both y.re and y.im are zero
|
|
return new Complex(
|
|
(x.re != 0) ? (x.re / 0) : 0,
|
|
(x.im != 0) ? (x.im / 0) : 0
|
|
);
|
|
}
|
|
}
|
|
};
|