-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathjs.js
25600 lines (21870 loc) · 692 KB
/
mathjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* math.js
* https://github.com/josdejong/mathjs
*
* Math.js is an extensive math library for JavaScript and Node.js,
* It features real and complex numbers, units, matrices, a large set of
* mathematical functions, and a flexible expression parser.
*
* @version 0.25.0
* @date 2014-07-01
*
* @license
* Copyright (C) 2013-2014 Jos de Jong <wjosdejong@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define(factory);
else if(typeof exports === 'object')
exports["math"] = factory();
else
root["math"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
var object = __webpack_require__(3);
/**
* math.js factory function.
*
* @param {Object} [config] Available configuration options:
* {String} matrix
* A string 'matrix' (default) or 'array'.
* {String} number
* A string 'number' (default) or 'bignumber'
* {Number} precision
* The number of significant digits for BigNumbers.
* Not applicable for Numbers.
*/
function factory (config) {
// simple test for ES5 support
if (typeof Object.create !== 'function') {
throw new Error('ES5 not supported by this JavaScript engine. ' +
'Please load the es5-shim and es5-sham library for compatibility.');
}
// create namespace (and factory for a new instance)
function math (config) {
return factory(config);
}
// create configuration options. These are private
var _config = {
// type of default matrix output. Choose 'matrix' (default) or 'array'
matrix: 'matrix',
// type of default number output. Choose 'number' (default) or 'bignumber'
number: 'number',
// number of significant digits in BigNumbers
precision: 64,
// minimum relative difference between two compared values,
// used by all comparison functions
epsilon: 1e-14
};
/**
* Set configuration options for math.js, and get current options
* @param {Object} [options] Available options:
* {String} matrix
* A string 'matrix' (default) or 'array'.
* {String} number
* A string 'number' (default) or 'bignumber'
* {Number} precision
* The number of significant digits for BigNumbers.
* Not applicable for Numbers.
* @return {Object} Returns the current configuration
*/
math.config = function config(options) {
if (options) {
// merge options
object.deepExtend(_config, options);
if (options.precision) {
math.type.BigNumber.config({
precision: options.precision
});
}
// TODO: remove deprecated setting some day (deprecated since version 0.17.0)
if (options.number && options.number.defaultType) {
throw new Error('setting `number.defaultType` is deprecated. Use `number` instead.')
}
// TODO: remove deprecated setting some day (deprecated since version 0.17.0)
if (options.number && options.number.precision) {
throw new Error('setting `number.precision` is deprecated. Use `precision` instead.')
}
// TODO: remove deprecated setting some day (deprecated since version 0.17.0)
if (options.matrix && options.matrix.defaultType) {
throw new Error('setting `matrix.defaultType` is deprecated. Use `matrix` instead.')
}
// TODO: remove deprecated setting some day (deprecated since version 0.15.0)
if (options.matrix && options.matrix['default']) {
throw new Error('setting `matrix.default` is deprecated. Use `matrix` instead.')
}
// TODO: remove deprecated setting some day (deprecated since version 0.20.0)
if (options.decimals) {
throw new Error('setting `decimals` is deprecated. Use `precision` instead.')
}
}
// return a clone of the settings
return object.clone(_config);
};
// create a new BigNumber factory for this instance of math.js
var BigNumber = __webpack_require__(123).constructor();
// extend BigNumber with a function clone
if (typeof BigNumber.prototype.clone !== 'function') {
/**
* Clone a bignumber
* @return {BigNumber} clone
*/
BigNumber.prototype.clone = function clone() {
return new BigNumber(this);
};
}
// extend BigNumber with a function convert
if (typeof BigNumber.convert !== 'function') {
/**
* Try to convert a Number in to a BigNumber.
* If the number has 15 or mor significant digits, the Number cannot be
* converted to BigNumber and will return the original number.
* @param {Number} number
* @return {BigNumber | Number} bignumber
*/
BigNumber.convert = function convert(number) {
if (digits(number) > 15) {
return number;
}
else {
return new BigNumber(number);
}
};
}
else {
throw new Error('Cannot add function convert to BigNumber: function already exists');
}
// errors
math.error = __webpack_require__(4);
// types (Matrix, Complex, Unit, ...)
math.type = {};
math.type.Complex = __webpack_require__(5);
math.type.Range = __webpack_require__(6);
math.type.Index = __webpack_require__(7);
math.type.Matrix = __webpack_require__(8);
math.type.Unit = __webpack_require__(9);
math.type.Help = __webpack_require__(10);
math.type.BigNumber = BigNumber;
math.collection = __webpack_require__(11);
// expression (parse, Parser, nodes, docs)
math.expression = {};
math.expression.node = __webpack_require__(14);
math.expression.parse = __webpack_require__(12);
math.expression.Parser = __webpack_require__(13);
math.expression.docs = __webpack_require__(15);
// expression parser
__webpack_require__(17)(math, _config);
__webpack_require__(18)(math, _config);
__webpack_require__(19)(math, _config);
__webpack_require__(20)(math, _config);
// functions - arithmetic
__webpack_require__(21)(math, _config);
__webpack_require__(22)(math, _config);
__webpack_require__(23)(math, _config);
__webpack_require__(24)(math, _config);
__webpack_require__(25)(math, _config);
__webpack_require__(26)(math, _config);
__webpack_require__(27)(math, _config);
__webpack_require__(28)(math, _config);
__webpack_require__(29)(math, _config);
__webpack_require__(30)(math, _config);
__webpack_require__(31)(math, _config);
__webpack_require__(32)(math, _config);
__webpack_require__(33)(math, _config);
__webpack_require__(34)(math, _config);
__webpack_require__(35)(math, _config);
__webpack_require__(36)(math, _config);
__webpack_require__(37)(math, _config);
__webpack_require__(38)(math, _config);
__webpack_require__(39)(math, _config);
__webpack_require__(40)(math, _config);
__webpack_require__(41)(math, _config);
__webpack_require__(42)(math, _config);
__webpack_require__(43)(math, _config);
__webpack_require__(44)(math, _config);
__webpack_require__(45)(math, _config);
__webpack_require__(46)(math, _config);
__webpack_require__(47)(math, _config);
// functions - comparison
__webpack_require__(48)(math, _config);
__webpack_require__(49)(math, _config);
__webpack_require__(50)(math, _config);
__webpack_require__(51)(math, _config);
__webpack_require__(52)(math, _config);
__webpack_require__(53)(math, _config);
__webpack_require__(54)(math, _config);
__webpack_require__(55)(math, _config);
// functions - complex
__webpack_require__(56)(math, _config);
__webpack_require__(57)(math, _config);
__webpack_require__(58)(math, _config);
__webpack_require__(59)(math, _config);
// functions - construction
__webpack_require__(60)(math, _config);
__webpack_require__(61)(math, _config);
__webpack_require__(62)(math, _config);
__webpack_require__(63)(math, _config);
__webpack_require__(64)(math, _config);
__webpack_require__(65)(math, _config);
__webpack_require__(66)(math, _config);
__webpack_require__(67)(math, _config);
__webpack_require__(68)(math, _config);
__webpack_require__(69)(math, _config);
// functions - matrix
__webpack_require__(70)(math, _config);
__webpack_require__(71)(math, _config);
__webpack_require__(72)(math, _config);
__webpack_require__(73)(math, _config);
__webpack_require__(74)(math, _config);
__webpack_require__(75)(math, _config);
__webpack_require__(76)(math, _config);
__webpack_require__(77)(math, _config);
__webpack_require__(78)(math, _config);
__webpack_require__(79)(math, _config);
__webpack_require__(80)(math, _config);
__webpack_require__(81)(math, _config);
__webpack_require__(82)(math, _config);
// functions - probability
__webpack_require__(83)(math, _config);
__webpack_require__(84)(math, _config);
__webpack_require__(85)(math, _config);
__webpack_require__(86)(math, _config);
__webpack_require__(87)(math, _config);
__webpack_require__(88)(math, _config);
__webpack_require__(89)(math, _config);
// functions - statistics
__webpack_require__(90)(math, _config);
__webpack_require__(91)(math, _config);
__webpack_require__(92)(math, _config);
__webpack_require__(93)(math, _config);
__webpack_require__(94)(math, _config);
__webpack_require__(95)(math, _config);
__webpack_require__(96)(math, _config);
__webpack_require__(97)(math, _config);
// functions - trigonometry
__webpack_require__(98)(math, _config);
__webpack_require__(99)(math, _config);
__webpack_require__(100)(math, _config);
__webpack_require__(101)(math, _config);
__webpack_require__(102)(math, _config);
__webpack_require__(103)(math, _config);
__webpack_require__(104)(math, _config);
__webpack_require__(105)(math, _config);
__webpack_require__(106)(math, _config);
__webpack_require__(107)(math, _config);
__webpack_require__(108)(math, _config);
__webpack_require__(109)(math, _config);
__webpack_require__(110)(math, _config);
__webpack_require__(111)(math, _config);
__webpack_require__(112)(math, _config);
__webpack_require__(113)(math, _config);
// functions - units
__webpack_require__(114)(math, _config);
// functions - utils
__webpack_require__(115)(math, _config);
__webpack_require__(116)(math, _config);
__webpack_require__(117)(math, _config);
__webpack_require__(118)(math, _config);
__webpack_require__(119)(math, _config);
__webpack_require__(120)(math, _config);
__webpack_require__(121)(math, _config);
// TODO: deprecated since version 0.25.0, remove some day.
math.ifElse = function () {
throw new Error('Function ifElse is deprecated. Use the conditional operator instead.');
};
// constants
__webpack_require__(2)(math, _config);
// selector (we initialize after all functions are loaded)
math.chaining = {};
math.chaining.Selector = __webpack_require__(16)(math, _config);
// apply provided configuration options
math.config(_config); // apply the default options
math.config(config); // apply custom options
// return the new instance
return math;
}
// create a default instance of math.js
var math = factory();
if (typeof window !== 'undefined') {
window.mathjs = math; // TODO: deprecate the mathjs namespace some day (replaced with 'math' since version 0.25.0)
}
// export the default instance
module.exports = factory();
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
module.exports = function (math) {
var Complex = __webpack_require__(5);
math.version = __webpack_require__(122);
math.pi = Math.PI;
math.e = Math.E;
math.tau = Math.PI * 2;
math.phi = 1.61803398874989484820458683436563811772030917980576286213545; // golden ratio, (1+sqrt(5))/2
math.i = new Complex(0, 1);
math['Infinity'] = Infinity;
math['NaN'] = NaN;
math['true'] = true;
math['false'] = false;
math['null'] = null;
// uppercase constants (for compatibility with built-in Math)
math.E = Math.E;
math.LN2 = Math.LN2;
math.LN10 = Math.LN10;
math.LOG2E = Math.LOG2E;
math.LOG10E = Math.LOG10E;
math.PI = Math.PI;
math.SQRT1_2 = Math.SQRT1_2;
math.SQRT2 = Math.SQRT2;
};
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
/**
* Clone an object
*
* clone(x)
*
* Can clone any primitive type, array, and object.
* If x has a function clone, this function will be invoked to clone the object.
*
* @param {*} x
* @return {*} clone
*/
exports.clone = function clone(x) {
var type = typeof x;
// immutable primitive types
if (type === 'number' || type === 'string' || type === 'boolean' ||
x === null || x === undefined) {
return x;
}
// use clone function of the object when available
if (typeof x.clone === 'function') {
return x.clone();
}
// array
if (Array.isArray(x)) {
return x.map(function (value) {
return clone(value);
});
}
if (x instanceof Number) return new Number(x.valueOf());
if (x instanceof String) return new String(x.valueOf());
if (x instanceof Boolean) return new Boolean(x.valueOf());
if (x instanceof Date) return new Date(x.valueOf());
if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x); // TODO: clone a RegExp
// object
var m = {};
for (var key in x) {
if (x.hasOwnProperty(key)) {
m[key] = clone(x[key]);
}
}
return m;
};
/**
* Extend object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @return {Object} a
*/
exports.extend = function extend (a, b) {
for (var prop in b) {
if (b.hasOwnProperty(prop)) {
a[prop] = b[prop];
}
}
return a;
};
/**
* Deep extend an object a with the properties of object b
* @param {Object} a
* @param {Object} b
* @returns {Object}
*/
exports.deepExtend = function deepExtend (a, b) {
// TODO: add support for Arrays to deepExtend
if (Array.isArray(b)) {
throw new TypeError('Arrays are not supported by deepExtend');
}
for (var prop in b) {
if (b.hasOwnProperty(prop)) {
if (b[prop] && b[prop].constructor === Object) {
if (a[prop] === undefined) {
a[prop] = {};
}
if (a[prop].constructor === Object) {
deepExtend(a[prop], b[prop]);
}
else {
a[prop] = b[prop];
}
} else if (Array.isArray(b[prop])) {
throw new TypeError('Arrays are not supported by deepExtend');
} else {
a[prop] = b[prop];
}
}
}
return a;
};
/**
* Deep test equality of all fields in two pairs of arrays or objects.
* @param {Array | Object} a
* @param {Array | Object} b
* @returns {boolean}
*/
exports.deepEqual = function deepEqual (a, b) {
var prop, i, len;
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length != b.length) {
return false;
}
for (i = 0, len = a.length; i < len; i++) {
if (!exports.deepEqual(a[i], b[i])) {
return false;
}
}
return true;
}
else if (a instanceof Object) {
if (Array.isArray(b) || !(b instanceof Object)) {
return false;
}
for (prop in a) {
//noinspection JSUnfilteredForInLoop
if (!exports.deepEqual(a[prop], b[prop])) {
return false;
}
}
for (prop in b) {
//noinspection JSUnfilteredForInLoop
if (!exports.deepEqual(a[prop], b[prop])) {
return false;
}
}
return true;
}
else {
return (typeof a === typeof b) && (a == b);
}
};
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
exports.ArgumentsError = __webpack_require__(124);
exports.DimensionError = __webpack_require__(125);
exports.IndexError = __webpack_require__(126);
exports.UnsupportedTypeError = __webpack_require__(127);
// TODO: implement an InvalidValueError?
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
var util = __webpack_require__(128),
Unit = __webpack_require__(9),
number = util.number,
isNumber = util.number.isNumber,
isUnit = Unit.isUnit,
isString = util.string.isString;
/**
* @constructor Complex
*
* A complex value can be constructed in the following ways:
* var a = new Complex();
* var b = new Complex(re, im);
* var c = Complex.parse(str);
*
* Example usage:
* var a = new Complex(3, -4); // 3 - 4i
* a.re = 5; // a = 5 - 4i
* var i = a.im; // -4;
* var b = Complex.parse('2 + 6i'); // 2 + 6i
* var c = new Complex(); // 0 + 0i
* var d = math.add(a, b); // 5 + 2i
*
* @param {Number} re The real part of the complex value
* @param {Number} [im] The imaginary part of the complex value
*/
function Complex(re, im) {
if (!(this instanceof Complex)) {
throw new SyntaxError('Constructor must be called with the new operator');
}
switch (arguments.length) {
case 0:
this.re = 0;
this.im = 0;
break;
case 1:
var arg = arguments[0];
if (typeof arg === 'object') {
if('re' in arg && 'im' in arg) {
var construct = new Complex(arg.re, arg.im); // pass on input validation
this.re = construct.re;
this.im = construct.im;
break;
} else if ('r' in arg && 'phi' in arg) {
var construct = Complex.fromPolar(arg.r, arg.phi);
this.re = construct.re;
this.im = construct.im;
break;
}
}
throw new SyntaxError('Object with the re and im or r and phi properties expected.');
case 2:
if (!isNumber(re) || !isNumber(im)) {
throw new TypeError('Two numbers expected in Complex constructor');
}
this.re = re;
this.im = im;
break;
default:
throw new SyntaxError('One, two or three arguments expected in Complex constructor');
}
}
/**
* Test whether value is a Complex value
* @param {*} value
* @return {Boolean} isComplex
*/
Complex.isComplex = function (value) {
return (value instanceof Complex);
};
// private variables and functions for the parser
var text, index, c;
function skipWhitespace() {
while (c == ' ' || c == '\t') {
next();
}
}
function isDigitDot (c) {
return ((c >= '0' && c <= '9') || c == '.');
}
function isDigit (c) {
return ((c >= '0' && c <= '9'));
}
function next() {
index++;
c = text.charAt(index);
}
function revert(oldIndex) {
index = oldIndex;
c = text.charAt(index);
}
function parseNumber () {
var number = '';
var oldIndex;
oldIndex = index;
if (c == '+') {
next();
}
else if (c == '-') {
number += c;
next();
}
if (!isDigitDot(c)) {
// a + or - must be followed by a digit
revert(oldIndex);
return null;
}
// get number, can have a single dot
if (c == '.') {
number += c;
next();
if (!isDigit(c)) {
// this is no legal number, it is just a dot
revert(oldIndex);
return null;
}
}
else {
while (isDigit(c)) {
number += c;
next();
}
if (c == '.') {
number += c;
next();
}
}
while (isDigit(c)) {
number += c;
next();
}
// check for exponential notation like "2.3e-4" or "1.23e50"
if (c == 'E' || c == 'e') {
number += c;
next();
if (c == '+' || c == '-') {
number += c;
next();
}
// Scientific notation MUST be followed by an exponent
if (!isDigit(c)) {
// this is no legal number, exponent is missing.
revert(oldIndex);
return null;
}
while (isDigit(c)) {
number += c;
next();
}
}
return number;
}
function parseComplex () {
// check for 'i', '-i', '+i'
var cnext = text.charAt(index + 1);
if (c == 'I' || c == 'i') {
next();
return '1';
}
else if ((c == '+' || c == '-') && (cnext == 'I' || cnext == 'i')) {
var number = (c == '+') ? '1' : '-1';
next();
next();
return number;
}
return null;
}
/**
* Parse a complex number from a string. For example Complex.parse("2 + 3i")
* will return a Complex value where re = 2, im = 3.
* Returns null if provided string does not contain a valid complex number.
* @param {String} str
* @returns {Complex | null} complex
*/
Complex.parse = function (str) {
text = str;
index = -1;
c = '';
if (!isString(text)) {
return null;
}
next();
skipWhitespace();
var first = parseNumber();
if (first) {
if (c == 'I' || c == 'i') {
// pure imaginary number
next();
skipWhitespace();
if (c) {
// garbage at the end. not good.
return null;
}
return new Complex(0, Number(first));
}
else {
// complex and real part
skipWhitespace();
var separator = c;
if (separator != '+' && separator != '-') {
// pure real number
skipWhitespace();
if (c) {
// garbage at the end. not good.
return null;
}
return new Complex(Number(first), 0);
}
else {
// complex and real part
next();
skipWhitespace();
var second = parseNumber();
if (second) {
if (c != 'I' && c != 'i') {
// 'i' missing at the end of the complex number
return null;
}
next();
}
else {
second = parseComplex();
if (!second) {
// imaginary number missing after separator
return null;
}
}
if (separator == '-') {
if (second[0] == '-') {
second = '+' + second.substring(1);
}
else {
second = '-' + second;
}
}
next();
skipWhitespace();
if (c) {
// garbage at the end. not good.
return null;
}
return new Complex(Number(first), Number(second));
}
}
}
else {
// check for 'i', '-i', '+i'
first = parseComplex();
if (first) {
skipWhitespace();
if (c) {
// garbage at the end. not good.
return null;
}
return new Complex(0, Number(first));
}
}
return null;
};
/**
* Create a complex number from polar coordinates
*
* Usage:
*
* Complex.fromPolar(r: Number, phi: Number) : Complex
* Complex.fromPolar({r: Number, phi: Number}) : Complex
*
* @param {*} args...
* @return {Complex}
*/
Complex.fromPolar = function (args) {
switch (arguments.length) {
case 1:
var arg = arguments[0];
if(typeof arg === 'object') {
return Complex.fromPolar(arg.r, arg.phi);
}
throw new TypeError('Input has to be an object with r and phi keys.');
case 2:
var r = arguments[0],
phi = arguments[1];
if(isNumber(r)) {
if (isUnit(phi) && phi.hasBase(Unit.BASE_UNITS.ANGLE)) {
// convert unit to a number in radians
phi = phi.toNumber('rad');
}
if(isNumber(phi)) {
return new Complex(r * Math.cos(phi), r * Math.sin(phi));
}
throw new TypeError('Phi is not a number nor an angle unit.');
} else {
throw new TypeError('Radius r is not a number.');
}
default:
throw new SyntaxError('Wrong number of arguments in function fromPolar');
}
};
/*
* Return the value of the complex number in polar notation
* The angle phi will be set in the interval of [-pi, pi].
* @return {{r: number, phi: number}} Returns and object with properties r and phi.
*/
Complex.prototype.toPolar = function() {
return {
r: Math.sqrt(this.re * this.re + this.im * this.im),
phi: Math.atan2(this.im, this.re)
};
};
/**
* Create a copy of the complex value
* @return {Complex} clone
*/
Complex.prototype.clone = function () {
return new Complex(this.re, this.im);
};
/**
* Test whether this complex number equals an other complex value.
* Two complex numbers are equal when both their real and imaginary parts
* are equal.
* @param {Complex} other
* @return {boolean} isEqual
*/
Complex.prototype.equals = function (other) {
return (this.re === other.re) && (this.im === other.im);
};
/**
* Get a string representation of the complex number,
* with optional formatting options.
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available
* options.
* @return {String} str
*/
Complex.prototype.format = function (options) {
var str = '',
strRe = number.format(this.re, options),
strIm = number.format(this.im, options);
if (this.im == 0) {
// real value
str = strRe;
}
else if (this.re == 0) {
// purely complex value
if (this.im == 1) {
str = 'i';
}
else if (this.im == -1) {
str = '-i';
}
else {
str = strIm + 'i';
}
}
else {
// complex value