1 | function _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 |
|
3 | import Complex from 'complex.js';
|
4 | import { format } from '../../utils/number';
|
5 | import { isNumber, isUnit } from '../../utils/is';
|
6 | import { factory } from '../../utils/factory';
|
7 | var name = 'Complex';
|
8 | var dependencies = [];
|
9 | export var createComplexClass = factory(name, dependencies, function () {
|
10 | |
11 |
|
12 |
|
13 | Complex.prototype.type = 'Complex';
|
14 | Complex.prototype.isComplex = true;
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Complex.prototype.toJSON = function () {
|
22 | return {
|
23 | mathjs: 'Complex',
|
24 | re: this.re,
|
25 | im: this.im
|
26 | };
|
27 | };
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | Complex.prototype.toPolar = function () {
|
36 | return {
|
37 | r: this.abs(),
|
38 | phi: this.arg()
|
39 | };
|
40 | };
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
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);
|
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 |
|
75 | str = strRe;
|
76 | } else if (re === 0) {
|
77 |
|
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 |
|
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 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
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 |
|
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 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | Complex.fromJSON = function (json) {
|
170 | return new Complex(json);
|
171 | };
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
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 |