1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createComplexClass = void 0;
|
7 |
|
8 | var _complex = _interopRequireDefault(require("complex.js"));
|
9 |
|
10 | var _number = require("../../utils/number");
|
11 |
|
12 | var _is = require("../../utils/is");
|
13 |
|
14 | var _factory = require("../../utils/factory");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
17 |
|
18 | 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); }
|
19 |
|
20 | var name = 'Complex';
|
21 | var dependencies = [];
|
22 | var createComplexClass = (0, _factory.factory)(name, dependencies, function () {
|
23 | |
24 |
|
25 |
|
26 | _complex["default"].prototype.type = 'Complex';
|
27 | _complex["default"].prototype.isComplex = true;
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | _complex["default"].prototype.toJSON = function () {
|
35 | return {
|
36 | mathjs: 'Complex',
|
37 | re: this.re,
|
38 | im: this.im
|
39 | };
|
40 | };
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | _complex["default"].prototype.toPolar = function () {
|
49 | return {
|
50 | r: this.abs(),
|
51 | phi: this.arg()
|
52 | };
|
53 | };
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | _complex["default"].prototype.format = function (options) {
|
66 | var str = '';
|
67 | var im = this.im;
|
68 | var re = this.re;
|
69 | var strRe = (0, _number.format)(this.re, options);
|
70 | var strIm = (0, _number.format)(this.im, options);
|
71 |
|
72 | var precision = (0, _is.isNumber)(options) ? options : options ? options.precision : null;
|
73 |
|
74 | if (precision !== null) {
|
75 | var epsilon = Math.pow(10, -precision);
|
76 |
|
77 | if (Math.abs(re / im) < epsilon) {
|
78 | re = 0;
|
79 | }
|
80 |
|
81 | if (Math.abs(im / re) < epsilon) {
|
82 | im = 0;
|
83 | }
|
84 | }
|
85 |
|
86 | if (im === 0) {
|
87 |
|
88 | str = strRe;
|
89 | } else if (re === 0) {
|
90 |
|
91 | if (im === 1) {
|
92 | str = 'i';
|
93 | } else if (im === -1) {
|
94 | str = '-i';
|
95 | } else {
|
96 | str = strIm + 'i';
|
97 | }
|
98 | } else {
|
99 |
|
100 | if (im < 0) {
|
101 | if (im === -1) {
|
102 | str = strRe + ' - i';
|
103 | } else {
|
104 | str = strRe + ' - ' + strIm.substring(1) + 'i';
|
105 | }
|
106 | } else {
|
107 | if (im === 1) {
|
108 | str = strRe + ' + i';
|
109 | } else {
|
110 | str = strRe + ' + ' + strIm + 'i';
|
111 | }
|
112 | }
|
113 | }
|
114 |
|
115 | return str;
|
116 | };
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | _complex["default"].fromPolar = function (args) {
|
131 | switch (arguments.length) {
|
132 | case 1:
|
133 | {
|
134 | var arg = arguments[0];
|
135 |
|
136 | if (_typeof(arg) === 'object') {
|
137 | return (0, _complex["default"])(arg);
|
138 | } else {
|
139 | throw new TypeError('Input has to be an object with r and phi keys.');
|
140 | }
|
141 | }
|
142 |
|
143 | case 2:
|
144 | {
|
145 | var r = arguments[0];
|
146 | var phi = arguments[1];
|
147 |
|
148 | if ((0, _is.isNumber)(r)) {
|
149 | if ((0, _is.isUnit)(phi) && phi.hasBase('ANGLE')) {
|
150 |
|
151 | phi = phi.toNumber('rad');
|
152 | }
|
153 |
|
154 | if ((0, _is.isNumber)(phi)) {
|
155 | return new _complex["default"]({
|
156 | r: r,
|
157 | phi: phi
|
158 | });
|
159 | }
|
160 |
|
161 | throw new TypeError('Phi is not a number nor an angle unit.');
|
162 | } else {
|
163 | throw new TypeError('Radius r is not a number.');
|
164 | }
|
165 | }
|
166 |
|
167 | default:
|
168 | throw new SyntaxError('Wrong number of arguments in function fromPolar');
|
169 | }
|
170 | };
|
171 |
|
172 | _complex["default"].prototype.valueOf = _complex["default"].prototype.toString;
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | _complex["default"].fromJSON = function (json) {
|
183 | return new _complex["default"](json);
|
184 | };
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | _complex["default"].compare = function (a, b) {
|
203 | if (a.re > b.re) {
|
204 | return 1;
|
205 | }
|
206 |
|
207 | if (a.re < b.re) {
|
208 | return -1;
|
209 | }
|
210 |
|
211 | if (a.im > b.im) {
|
212 | return 1;
|
213 | }
|
214 |
|
215 | if (a.im < b.im) {
|
216 | return -1;
|
217 | }
|
218 |
|
219 | return 0;
|
220 | };
|
221 |
|
222 | return _complex["default"];
|
223 | }, {
|
224 | isClass: true
|
225 | });
|
226 | exports.createComplexClass = createComplexClass; |
\ | No newline at end of file |