mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
126 lines
4.3 KiB
JavaScript
126 lines
4.3 KiB
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.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.complex() // creates a complex value with zero
|
|
* // as real and imaginary part.
|
|
* math.complex(re : number, im : string) // creates a complex value with provided
|
|
* // values for real and imaginary part.
|
|
* math.complex(re : number) // creates a complex value with provided
|
|
* // real value and zero imaginary part.
|
|
* math.complex(complex : Complex) // clones the provided complex value.
|
|
* math.complex(arg : string) // parses a string into a complex value.
|
|
* math.complex(array : Array) // converts the elements of the array
|
|
* // or matrix element wise into a
|
|
* // complex value.
|
|
* math.complex({re: number, im: number}) // creates a complex value with provided
|
|
* // values for real an imaginary part.
|
|
* math.complex({r: number, phi: number}) // creates a complex value with provided
|
|
* // polar coordinates
|
|
*
|
|
* Examples:
|
|
*
|
|
* var a = math.complex(3, -4); // a = Complex 3 - 4i
|
|
* a.re = 5; // a = Complex 5 - 4i
|
|
* var i = a.im; // Number -4;
|
|
* var b = math.complex('2 + 6i'); // Complex 2 + 6i
|
|
* var c = math.complex(); // Complex 0 + 0i
|
|
* var d = math.add(a, b); // Complex 5 + 2i
|
|
*
|
|
* See also:
|
|
*
|
|
* bignumber, boolean, index, matrix, number, string, unit
|
|
*
|
|
* @param {* | Array | Matrix} [args]
|
|
* Arguments specifying the real and imaginary part of the complex number
|
|
* @return {Complex | Array | Matrix} Returns a complex 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);
|
|
}
|
|
};
|
|
};
|