UNPKG

6.38 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createComplexClass = void 0;
7
8var _complex = _interopRequireDefault(require("complex.js"));
9
10var _number = require("../../utils/number");
11
12var _is = require("../../utils/is");
13
14var _factory = require("../../utils/factory");
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
17
18function _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
20var name = 'Complex';
21var dependencies = [];
22var createComplexClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function () {
23 /**
24 * Attach type information
25 */
26 _complex["default"].prototype.type = 'Complex';
27 _complex["default"].prototype.isComplex = true;
28 /**
29 * Get a JSON representation of the complex number
30 * @returns {Object} Returns a JSON object structured as:
31 * `{"mathjs": "Complex", "re": 2, "im": 3}`
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 * Return the value of the complex number in polar notation
43 * The angle phi will be set in the interval of [-pi, pi].
44 * @return {{r: number, phi: number}} Returns and object with properties r and phi.
45 */
46
47
48 _complex["default"].prototype.toPolar = function () {
49 return {
50 r: this.abs(),
51 phi: this.arg()
52 };
53 };
54 /**
55 * Get a string representation of the complex number,
56 * with optional formatting options.
57 * @param {Object | number | Function} [options] Formatting options. See
58 * lib/utils/number:format for a
59 * description of the available
60 * options.
61 * @return {string} str
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); // round either re or im when smaller than the configured precision
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 // real value
88 str = strRe;
89 } else if (re === 0) {
90 // purely complex value
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 // complex value
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 * Create a complex number from polar coordinates
119 *
120 * Usage:
121 *
122 * Complex.fromPolar(r: number, phi: number) : Complex
123 * Complex.fromPolar({r: number, phi: number}) : Complex
124 *
125 * @param {*} args...
126 * @return {Complex}
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 // convert unit to a number in radians
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 * Create a Complex number from a JSON object
175 * @param {Object} json A JSON Object structured as
176 * {"mathjs": "Complex", "re": 2, "im": 3}
177 * All properties are optional, default values
178 * for `re` and `im` are 0.
179 * @return {Complex} Returns a new Complex number
180 */
181
182 _complex["default"].fromJSON = function (json) {
183 return new _complex["default"](json);
184 };
185 /**
186 * Compare two complex numbers, `a` and `b`:
187 *
188 * - Returns 1 when the real part of `a` is larger than the real part of `b`
189 * - Returns -1 when the real part of `a` is smaller than the real part of `b`
190 * - Returns 1 when the real parts are equal
191 * and the imaginary part of `a` is larger than the imaginary part of `b`
192 * - Returns -1 when the real parts are equal
193 * and the imaginary part of `a` is smaller than the imaginary part of `b`
194 * - Returns 0 when both real and imaginary parts are equal.
195 *
196 * @params {Complex} a
197 * @params {Complex} b
198 * @returns {number} Returns the comparison result: -1, 0, or 1
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});
226exports.createComplexClass = createComplexClass;
\No newline at end of file