mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
module.exports = function (math) {
|
|
var util = require('../../util/index'),
|
|
|
|
BigNumber = require('bignumber.js'),
|
|
Complex = require('../../type/Complex'),
|
|
collection = require('../../type/collection'),
|
|
|
|
isNumBool = util.number.isNumBool,
|
|
isComplex = Complex.isComplex,
|
|
isCollection = collection.isCollection;
|
|
|
|
/**
|
|
* Compute the cube of a value
|
|
*
|
|
* x .* x .* x
|
|
* cube(x)
|
|
*
|
|
* For matrices, the function is evaluated element wise.
|
|
*
|
|
* @param {Number | BigNumber | Boolean | Complex | Array | Matrix} x
|
|
* @return {Number | BigNumber | Complex | Array | Matrix} res
|
|
*/
|
|
math.cube = function cube(x) {
|
|
if (arguments.length != 1) {
|
|
throw new util.error.ArgumentsError('cube', arguments.length, 1);
|
|
}
|
|
|
|
if (isNumBool(x)) {
|
|
return x * x * x;
|
|
}
|
|
|
|
if (isComplex(x)) {
|
|
return math.multiply(math.multiply(x, x), x);
|
|
}
|
|
|
|
if (x instanceof BigNumber) {
|
|
return x.times(x).times(x);
|
|
}
|
|
|
|
if (isCollection(x)) {
|
|
return collection.deepMap(x, cube);
|
|
}
|
|
|
|
throw new util.error.UnsupportedTypeError('cube', x);
|
|
};
|
|
};
|