mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
142 lines
3.9 KiB
JavaScript
142 lines
3.9 KiB
JavaScript
/**
|
|
* Multiply two values. x + y or multiply(x, y)
|
|
* @param {Number | Complex | Unit | Array} x
|
|
* @param {Number | Complex | Unit | Array} y
|
|
* @return {Number | Complex | Unit | Array} res
|
|
*/
|
|
function multiply(x, y) {
|
|
if (arguments.length != 2) {
|
|
throw newArgumentsError('multiply', arguments.length, 2);
|
|
}
|
|
|
|
if (isNumber(x)) {
|
|
if (isNumber(y)) {
|
|
// number * number
|
|
return x * y;
|
|
}
|
|
else if (y instanceof Complex) {
|
|
// number * complex
|
|
return multiplyComplex(new Complex(x, 0), y);
|
|
}
|
|
else if (y instanceof Unit) {
|
|
res = y.clone();
|
|
res.value *= x;
|
|
return res;
|
|
}
|
|
}
|
|
else if (x instanceof Complex) {
|
|
if (isNumber(y)) {
|
|
// complex * number
|
|
return multiplyComplex(x, new Complex(y, 0));
|
|
}
|
|
else if (y instanceof Complex) {
|
|
// complex * complex
|
|
return multiplyComplex(x, y);
|
|
}
|
|
}
|
|
else if (x instanceof Unit) {
|
|
if (isNumber(y)) {
|
|
res = x.clone();
|
|
res.value *= y;
|
|
return res;
|
|
}
|
|
}
|
|
else if (x instanceof Array) {
|
|
if (y instanceof Array) {
|
|
// matrix * matrix
|
|
var sizeX = util.size(x);
|
|
var sizeY = util.size(y);
|
|
|
|
if (sizeX.length != 2) {
|
|
throw new Error('Can only multiply a 2 dimensional matrix ' +
|
|
'(A has ' + sizeX.length + ' dimensions)');
|
|
}
|
|
if (sizeY.length != 2) {
|
|
throw new Error('Can only multiply a 2 dimensional matrix ' +
|
|
'(B has ' + sizeY.length + ' dimensions)');
|
|
}
|
|
if (sizeX[1] != sizeY[0]) {
|
|
throw new RangeError('Dimensions mismatch in multiplication. ' +
|
|
'Columns of A must match rows of B ' +
|
|
'(A is ' + sizeX[0] + 'x' + sizeX[1] +
|
|
', B is ' + sizeY[0] + 'x' + sizeY[1] + ', ' +
|
|
sizeY[1] + ' != ' + sizeY[0] + ')');
|
|
}
|
|
|
|
// TODO: performance of matrix multiplication can be improved
|
|
var res = [];
|
|
var rows = sizeX[0];
|
|
var cols = sizeY[1];
|
|
var num = sizeX[1];
|
|
for (var r = 0; r < rows; r++) {
|
|
res[r] = [];
|
|
for (var c = 0; c < cols; c++) {
|
|
var result = null;
|
|
for (var n = 0; n < num; n++) {
|
|
var p = multiply(x[r][n], y[n][c]);
|
|
result = (result == null) ? p : add(result, p);
|
|
}
|
|
res[r][c] = result;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
else {
|
|
// matrix * scalar
|
|
return util.map2(x, y, multiply);
|
|
}
|
|
}
|
|
|
|
if (y instanceof Array) {
|
|
// scalar * matrix
|
|
return util.map2(x, y, multiply);
|
|
}
|
|
|
|
if (x.valueOf() !== x || y.valueOf() !== y) {
|
|
// fallback on the objects primitive values
|
|
return multiply(x.valueOf(), y.valueOf());
|
|
}
|
|
|
|
throw newUnsupportedTypeError('multiply', x, y);
|
|
}
|
|
|
|
/**
|
|
* Multiply two complex numbers. x * y or multiply(x, y)
|
|
* @param {Complex} x
|
|
* @param {Complex} y
|
|
* @return {Complex} res
|
|
* @private
|
|
*/
|
|
function multiplyComplex (x, y) {
|
|
return new Complex(
|
|
x.re * y.re - x.im * y.im,
|
|
x.re * y.im + x.im * y.re
|
|
);
|
|
}
|
|
|
|
math.multiply = multiply;
|
|
|
|
/**
|
|
* Function documentation
|
|
*/
|
|
multiply.doc = {
|
|
'name': 'multiply',
|
|
'category': 'Operators',
|
|
'syntax': [
|
|
'x * y',
|
|
'multiply(x, y)'
|
|
],
|
|
'description': 'multiply two values.',
|
|
'examples': [
|
|
'2.1 * 3.6',
|
|
'ans / 3.6',
|
|
'2 * 3 + 4',
|
|
'2 * (3 + 4)',
|
|
'3 * 2.1 km'
|
|
],
|
|
'seealso': [
|
|
'divide'
|
|
]
|
|
};
|