mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
module.exports = function (math) {
|
|
var util = require('../../util/index'),
|
|
|
|
BigNumber = math.type.BigNumber,
|
|
Complex = require('../../type/Complex'),
|
|
collection = require('../../type/collection'),
|
|
|
|
object = util.object,
|
|
isNumber = util.number.isNumber,
|
|
isBoolean = util['boolean'].isBoolean,
|
|
isCollection = collection.isCollection,
|
|
isComplex = Complex.isComplex;
|
|
|
|
/**
|
|
* Get the real part of a complex number.
|
|
*
|
|
* re(x)
|
|
*
|
|
* For matrices, the function is evaluated element wise.
|
|
*
|
|
* @param {Number | BigNumber | Complex | Array | Matrix | Boolean} x
|
|
* @return {Number | BigNumber | Array | Matrix} re
|
|
*/
|
|
math.re = function re(x) {
|
|
if (arguments.length != 1) {
|
|
throw new math.error.ArgumentsError('re', arguments.length, 1);
|
|
}
|
|
|
|
if (isNumber(x)) {
|
|
return x;
|
|
}
|
|
|
|
if (x instanceof BigNumber) {
|
|
return new BigNumber(x);
|
|
}
|
|
|
|
if (isComplex(x)) {
|
|
return x.re;
|
|
}
|
|
|
|
if (isCollection(x)) {
|
|
return collection.deepMap(x, re);
|
|
}
|
|
|
|
if (isBoolean(x)) {
|
|
return +x;
|
|
}
|
|
|
|
// return a clone of the value itself for all non-complex values
|
|
return object.clone(x);
|
|
};
|
|
};
|