UNPKG

3.52 kBJavaScriptView Raw
1import { factory } from '../../../utils/factory.js';
2import { deepMap } from '../../../utils/collection.js';
3var name = 'complex';
4var dependencies = ['typed', 'Complex'];
5export var createComplex = /* #__PURE__ */factory(name, dependencies, _ref => {
6 var {
7 typed,
8 Complex
9 } = _ref;
10
11 /**
12 * Create a complex value or convert a value to a complex value.
13 *
14 * Syntax:
15 *
16 * math.complex() // creates a complex value with zero
17 * // as real and imaginary part.
18 * math.complex(re : number, im : string) // creates a complex value with provided
19 * // values for real and imaginary part.
20 * math.complex(re : number) // creates a complex value with provided
21 * // real value and zero imaginary part.
22 * math.complex(complex : Complex) // clones the provided complex value.
23 * math.complex(arg : string) // parses a string into a complex value.
24 * math.complex(array : Array) // converts the elements of the array
25 * // or matrix element wise into a
26 * // complex value.
27 * math.complex({re: number, im: number}) // creates a complex value with provided
28 * // values for real an imaginary part.
29 * math.complex({r: number, phi: number}) // creates a complex value with provided
30 * // polar coordinates
31 *
32 * Examples:
33 *
34 * const a = math.complex(3, -4) // a = Complex 3 - 4i
35 * a.re = 5 // a = Complex 5 - 4i
36 * const i = a.im // Number -4
37 * const b = math.complex('2 + 6i') // Complex 2 + 6i
38 * const c = math.complex() // Complex 0 + 0i
39 * const d = math.add(a, b) // Complex 5 + 2i
40 *
41 * See also:
42 *
43 * bignumber, boolean, index, matrix, number, string, unit
44 *
45 * @param {* | Array | Matrix} [args]
46 * Arguments specifying the real and imaginary part of the complex number
47 * @return {Complex | Array | Matrix} Returns a complex value
48 */
49 return typed('complex', {
50 '': function _() {
51 return Complex.ZERO;
52 },
53 number: function number(x) {
54 return new Complex(x, 0);
55 },
56 'number, number': function numberNumber(re, im) {
57 return new Complex(re, im);
58 },
59 // TODO: this signature should be redundant
60 'BigNumber, BigNumber': function BigNumberBigNumber(re, im) {
61 return new Complex(re.toNumber(), im.toNumber());
62 },
63 Fraction: function Fraction(x) {
64 return new Complex(x.valueOf(), 0);
65 },
66 Complex: function Complex(x) {
67 return x.clone();
68 },
69 string: function string(x) {
70 return Complex(x); // for example '2 + 3i'
71 },
72 null: function _null(x) {
73 return Complex(0);
74 },
75 Object: function Object(x) {
76 if ('re' in x && 'im' in x) {
77 return new Complex(x.re, x.im);
78 }
79
80 if ('r' in x && 'phi' in x || 'abs' in x && 'arg' in x) {
81 return new Complex(x);
82 }
83
84 throw new Error('Expected object with properties (re and im) or (r and phi) or (abs and arg)');
85 },
86 'Array | Matrix': function ArrayMatrix(x) {
87 return deepMap(x, this);
88 }
89 });
90});
\No newline at end of file