mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
module.exports = function (math) {
|
|
var util = require('../../util/index'),
|
|
|
|
Complex = require('../../type/Complex'),
|
|
collection = require('../../type/collection'),
|
|
|
|
object = util.object,
|
|
isNumBool = util.number.isNumBool,
|
|
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 | Complex | Array | Matrix | Boolean} x
|
|
* @return {Number | Array | Matrix} re
|
|
*/
|
|
math.re = function re(x) {
|
|
if (arguments.length != 1) {
|
|
throw new util.error.ArgumentsError('re', arguments.length, 1);
|
|
}
|
|
|
|
if (isNumBool(x)) {
|
|
return x;
|
|
}
|
|
|
|
if (isComplex(x)) {
|
|
return x.re;
|
|
}
|
|
|
|
if (isCollection(x)) {
|
|
return collection.deepMap(x, re);
|
|
}
|
|
|
|
if (x.valueOf() !== x) {
|
|
// fallback on the objects primitive value
|
|
return re(x.valueOf());
|
|
}
|
|
|
|
// return a clone of the value itself for all non-complex values
|
|
return object.clone(x);
|
|
};
|
|
};
|