1 | import Complex from 'complex.js';
|
2 | import { format } from '../../utils/number.js';
|
3 | import { isNumber, isUnit } from '../../utils/is.js';
|
4 | import { factory } from '../../utils/factory.js';
|
5 | var name = 'Complex';
|
6 | var dependencies = [];
|
7 | export var createComplexClass = /* #__PURE__ */factory(name, dependencies, () => {
|
8 | /**
|
9 | * Attach type information
|
10 | */
|
11 | Complex.prototype.type = 'Complex';
|
12 | Complex.prototype.isComplex = true;
|
13 | /**
|
14 | * Get a JSON representation of the complex number
|
15 | * @returns {Object} Returns a JSON object structured as:
|
16 | * `{"mathjs": "Complex", "re": 2, "im": 3}`
|
17 | */
|
18 |
|
19 | Complex.prototype.toJSON = function () {
|
20 | return {
|
21 | mathjs: 'Complex',
|
22 | re: this.re,
|
23 | im: this.im
|
24 | };
|
25 | };
|
26 | /*
|
27 | * Return the value of the complex number in polar notation
|
28 | * The angle phi will be set in the interval of [-pi, pi].
|
29 | * @return {{r: number, phi: number}} Returns and object with properties r and phi.
|
30 | */
|
31 |
|
32 |
|
33 | Complex.prototype.toPolar = function () {
|
34 | return {
|
35 | r: this.abs(),
|
36 | phi: this.arg()
|
37 | };
|
38 | };
|
39 | /**
|
40 | * Get a string representation of the complex number,
|
41 | * with optional formatting options.
|
42 | * @param {Object | number | Function} [options] Formatting options. See
|
43 | * lib/utils/number:format for a
|
44 | * description of the available
|
45 | * options.
|
46 | * @return {string} str
|
47 | */
|
48 |
|
49 |
|
50 | Complex.prototype.format = function (options) {
|
51 | var str = '';
|
52 | var im = this.im;
|
53 | var re = this.re;
|
54 | var strRe = format(this.re, options);
|
55 | var strIm = format(this.im, options); // round either re or im when smaller than the configured precision
|
56 |
|
57 | var precision = isNumber(options) ? options : options ? options.precision : null;
|
58 |
|
59 | if (precision !== null) {
|
60 | var epsilon = Math.pow(10, -precision);
|
61 |
|
62 | if (Math.abs(re / im) < epsilon) {
|
63 | re = 0;
|
64 | }
|
65 |
|
66 | if (Math.abs(im / re) < epsilon) {
|
67 | im = 0;
|
68 | }
|
69 | }
|
70 |
|
71 | if (im === 0) {
|
72 | // real value
|
73 | str = strRe;
|
74 | } else if (re === 0) {
|
75 | // purely complex value
|
76 | if (im === 1) {
|
77 | str = 'i';
|
78 | } else if (im === -1) {
|
79 | str = '-i';
|
80 | } else {
|
81 | str = strIm + 'i';
|
82 | }
|
83 | } else {
|
84 | // complex value
|
85 | if (im < 0) {
|
86 | if (im === -1) {
|
87 | str = strRe + ' - i';
|
88 | } else {
|
89 | str = strRe + ' - ' + strIm.substring(1) + 'i';
|
90 | }
|
91 | } else {
|
92 | if (im === 1) {
|
93 | str = strRe + ' + i';
|
94 | } else {
|
95 | str = strRe + ' + ' + strIm + 'i';
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 | return str;
|
101 | };
|
102 | /**
|
103 | * Create a complex number from polar coordinates
|
104 | *
|
105 | * Usage:
|
106 | *
|
107 | * Complex.fromPolar(r: number, phi: number) : Complex
|
108 | * Complex.fromPolar({r: number, phi: number}) : Complex
|
109 | *
|
110 | * @param {*} args...
|
111 | * @return {Complex}
|
112 | */
|
113 |
|
114 |
|
115 | Complex.fromPolar = function (args) {
|
116 | switch (arguments.length) {
|
117 | case 1:
|
118 | {
|
119 | var arg = arguments[0];
|
120 |
|
121 | if (typeof arg === 'object') {
|
122 | return Complex(arg);
|
123 | } else {
|
124 | throw new TypeError('Input has to be an object with r and phi keys.');
|
125 | }
|
126 | }
|
127 |
|
128 | case 2:
|
129 | {
|
130 | var r = arguments[0];
|
131 | var phi = arguments[1];
|
132 |
|
133 | if (isNumber(r)) {
|
134 | if (isUnit(phi) && phi.hasBase('ANGLE')) {
|
135 | // convert unit to a number in radians
|
136 | phi = phi.toNumber('rad');
|
137 | }
|
138 |
|
139 | if (isNumber(phi)) {
|
140 | return new Complex({
|
141 | r: r,
|
142 | phi: phi
|
143 | });
|
144 | }
|
145 |
|
146 | throw new TypeError('Phi is not a number nor an angle unit.');
|
147 | } else {
|
148 | throw new TypeError('Radius r is not a number.');
|
149 | }
|
150 | }
|
151 |
|
152 | default:
|
153 | throw new SyntaxError('Wrong number of arguments in function fromPolar');
|
154 | }
|
155 | };
|
156 |
|
157 | Complex.prototype.valueOf = Complex.prototype.toString;
|
158 | /**
|
159 | * Create a Complex number from a JSON object
|
160 | * @param {Object} json A JSON Object structured as
|
161 | * {"mathjs": "Complex", "re": 2, "im": 3}
|
162 | * All properties are optional, default values
|
163 | * for `re` and `im` are 0.
|
164 | * @return {Complex} Returns a new Complex number
|
165 | */
|
166 |
|
167 | Complex.fromJSON = function (json) {
|
168 | return new Complex(json);
|
169 | };
|
170 | /**
|
171 | * Compare two complex numbers, `a` and `b`:
|
172 | *
|
173 | * - Returns 1 when the real part of `a` is larger than the real part of `b`
|
174 | * - Returns -1 when the real part of `a` is smaller than the real part of `b`
|
175 | * - Returns 1 when the real parts are equal
|
176 | * and the imaginary part of `a` is larger than the imaginary part of `b`
|
177 | * - Returns -1 when the real parts are equal
|
178 | * and the imaginary part of `a` is smaller than the imaginary part of `b`
|
179 | * - Returns 0 when both real and imaginary parts are equal.
|
180 | *
|
181 | * @params {Complex} a
|
182 | * @params {Complex} b
|
183 | * @returns {number} Returns the comparison result: -1, 0, or 1
|
184 | */
|
185 |
|
186 |
|
187 | Complex.compare = function (a, b) {
|
188 | if (a.re > b.re) {
|
189 | return 1;
|
190 | }
|
191 |
|
192 | if (a.re < b.re) {
|
193 | return -1;
|
194 | }
|
195 |
|
196 | if (a.im > b.im) {
|
197 | return 1;
|
198 | }
|
199 |
|
200 | if (a.im < b.im) {
|
201 | return -1;
|
202 | }
|
203 |
|
204 | return 0;
|
205 | };
|
206 |
|
207 | return Complex;
|
208 | }, {
|
209 | isClass: true
|
210 | }); |
\ | No newline at end of file |