mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.
119 lines (102 loc) • 4.04 kB
JavaScript
module.exports = function (math) {
var util = require('../../util/index'),
BigNumber = math.type.BigNumber,
Complex = require('../../type/Complex'),
collection = require('../../type/collection'),
isCollection = collection.isCollection,
isNumber = util.number.isNumber,
isString = util.string.isString,
isComplex = Complex.isComplex;
/**
* Create a complex value or convert a value to a complex value.
*
* The method accepts the following arguments:
* complex() creates a complex value with zero
* as real and imaginary part.
* complex(re : number, im : string) creates a complex value with provided
* values for real and imaginary part.
* complex(re : number) creates a complex value with provided
* real value and zero imaginary part.
* complex(complex : Complex) clones the provided complex value.
* complex(arg : string) parses a string into a complex value.
* complex(array : Array) converts the elements of the array
* or matrix element wise into a
* complex value.
* complex({re: number, im: number}) creates a complex value with provided
* values for real an imaginary part.
* complex({r: number, phi: number}) creates a complex value with provided
* polar coordinates
*
* Example usage:
* var a = math.complex(3, -4); // 3 - 4i
* a.re = 5; // a = 5 - 4i
* var i = a.im; // -4;
* var b = math.complex('2 + 6i'); // 2 + 6i
* var c = math.complex(); // 0 + 0i
* var d = math.add(a, b); // 5 + 2i
*
* @param {* | Array | Matrix} [args]
* @return {Complex | Array | Matrix} value
*/
math.complex = function complex(args) {
switch (arguments.length) {
case 0:
// no parameters. Set re and im zero
return new Complex(0, 0);
case 1:
// parse string into a complex number
var arg = arguments[0];
if (isNumber(arg)) {
return new Complex(arg, 0);
}
if (arg instanceof BigNumber) {
// convert to Number
return new Complex(arg.toNumber(), 0);
}
if (isComplex(arg)) {
// create a clone
return arg.clone();
}
if (isString(arg)) {
var c = Complex.parse(arg);
if (c) {
return c;
}
else {
throw new SyntaxError('String "' + arg + '" is no valid complex number');
}
}
if (isCollection(arg)) {
return collection.deepMap(arg, complex);
}
if (typeof arg === 'object') {
if('re' in arg && 'im' in arg) {
return new Complex(arg.re, arg.im);
} else if ('r' in arg && 'phi' in arg) {
return Complex.fromPolar(arg.r, arg.phi);
}
}
throw new TypeError('Two numbers, single string or an fitting object expected in function complex');
case 2:
// re and im provided
var re = arguments[0],
im = arguments[1];
// convert re to number
if (re instanceof BigNumber) {
re = re.toNumber();
}
// convert im to number
if (im instanceof BigNumber) {
im = im.toNumber();
}
if (isNumber(re) && isNumber(im)) {
return new Complex(re, im);
}
else {
throw new TypeError('Two numbers or a single string expected in function complex');
}
default:
throw new math.error.ArgumentsError('complex', arguments.length, 0, 2);
}
};
};