UNPKG

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