This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
nerdamer.core.js
12510 lines (11604 loc) · 650 KB
/
nerdamer.core.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
/*
* Author : Martin Donk
* Website : http://www.nerdamer.com
* Email : martin.r.donk@gmail.com
* Source : https://github.com/jiggzson/nerdamer
*/
/* global trig, trigh, Infinity, define, arguments2Array, NaN */
//externals ====================================================================
/* BigInterger.js v1.6.40 https://github.com/peterolson/BigInteger.js/blob/master/LICENSE */
//var nerdamerBigInt = typeof nerdamerBigInt !== 'undefined' ? nerdamerBigInt : require("big-integer");
/* big.js v5.2.2 https://github.com/MikeMcl/big.js/LICENCE */
//var nerdamerBigDecimal = typeof nerdamerBigDecimal !== 'undefined' ? nerdamerBigDecimal : require('big.js');
var nerdamer = (function (imports) {
"use strict";
//version ======================================================================
var version = '1.1.13';
//inits ========================================================================
var _ = new Parser(); //nerdamer's parser
//import bigInt
var bigInt = imports.bigInt;
var bigDec = imports.bigDec;
//set the precision to js precision
bigDec.set({
precision: 250
});
var Groups = {};
//container of pregenerated primes
var PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113
, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557,
563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031,
1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171,
1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303,
1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471,
1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607,
1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753,
1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083];
//Settings =====================================================================
var CUSTOM_OPERATORS = {};
var Settings = {
//Enables/Disables call peekers. False means callPeekers are disabled and true means callPeekers are enabled.
callPeekers: false,
//the max number up to which to cache primes. Making this too high causes performance issues
init_primes: 1000,
exclude: [],
//If you don't care about division by zero for example then this can be set to true.
//Has some nasty side effects so choose carefully.
suppress_errors: false,
//the global used to invoke the libary to parse to a number. Normally cos(9) for example returns
//cos(9) for convenience but parse to number will always try to return a number if set to true.
PARSE2NUMBER: false,
//this flag forces the a clone to be returned when add, subtract, etc... is called
SAFE: false,
//the symbol to use for imaginary symbols
IMAGINARY: 'i',
//the modules used to link numeric function holders
FUNCTION_MODULES: [Math],
//Allow certain characters
ALLOW_CHARS: ['π'],
//Allow nerdamer to convert multi-character variables
USE_MULTICHARACTER_VARS: true,
//Allow changing of power operator
POWER_OPERATOR: '^',
//The variable validation regex
//VALIDATION_REGEX: /^[a-z_][a-z\d\_]*$/i
VALIDATION_REGEX: /^[a-z_αAβBγΓδΔϵEζZηHθΘιIκKλΛμMνNξΞoOπΠρPσΣτTυϒϕΦχXψΨωΩ∞][0-9a-z_αAβBγΓδΔϵEζZηHθΘιIκKλΛμMνNξΞoOπΠρPσΣτTυϒϕΦχXψΨωΩ]*$/i,
// The regex used to determine which characters should be included in implied multiplication
IMPLIED_MULTIPLICATION_REGEX: /([\+\-\/\*]*[0-9]+)([a-z_αAβBγΓδΔϵEζZηHθΘιIκKλΛμMνNξΞoOπΠρPσΣτTυϒϕΦχXψΨωΩ]+[\+\-\/\*]*)/gi,
//Aliases
ALIASES: {
'π': 'pi',
'∞': 'Infinity'
},
POSITIVE_MULTIPLIERS: false,
//Cached items
CACHE: {},
//Print out warnings or not
SILENCE_WARNINGS: false,
// Precision
PRECISION: 21,
// The Expression defaults to this value for decimal places
EXPRESSION_DECP: 19,
// The text function defaults to this value for decimal places
DEFAULT_DECP: 16,
//function mappings
VECTOR: 'vector',
PARENTHESIS: 'parens',
SQRT: 'sqrt',
ABS: 'abs',
FACTORIAL: 'factorial',
DOUBLEFACTORIAL: 'dfactorial',
//reference pi and e
LONG_PI: '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214' +
'808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196',
LONG_E: '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466' +
'39193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901',
PI: Math.PI,
E: Math.E,
LOG: 'log',
LOG10: 'log10',
LOG10_LATEX: 'log_{10}',
MAX_EXP: 200000,
//The number of scientific place to round to
SCIENTIFIC_MAX_DECIMAL_PLACES: 14,
//True if ints should not be converted to
SCIENTIFIC_IGNORE_ZERO_EXPONENTS: true
};
(function () {
Settings.CACHE.roots = {};
var x = 40,
y = 40;
for(var i = 2; i <= x; i++) {
for(var j = 2; j <= y; j++) {
var nthpow = bigInt(i).pow(j);
Settings.CACHE.roots[nthpow + '-' + j] = i;
}
}
})();
//Add the groups. These have been reorganized as of v0.5.1 to make CP the highest group
//The groups that help with organizing during parsing. Note that for FN is still a function even
//when it's raised to a symbol, which typically results in an EX
var N = Groups.N = 1, // A number
P = Groups.P = 2, // A number with a rational power e.g. 2^(3/5).
S = Groups.S = 3, // A single variable e.g. x.
EX = Groups.EX = 4, // An exponential
FN = Groups.FN = 5, // A function
PL = Groups.PL = 6, // A symbol/expression having same name with different powers e.g. 1/x + x^2
CB = Groups.CB = 7, // A symbol/expression composed of one or more variables through multiplication e.g. x*y
CP = Groups.CP = 8; // A symbol/expression composed of one variable and any other symbol or number x+1 or x+y
var CONST_HASH = Settings.CONST_HASH = '#';
var PARENTHESIS = Settings.PARENTHESIS;
var SQRT = Settings.SQRT;
var ABS = Settings.ABS;
var FACTORIAL = Settings.FACTORIAL;
var DOUBLEFACTORIAL = Settings.DOUBLEFACTORIAL;
//the storage container "memory" for parsed expressions
var EXPRESSIONS = [];
//variables
var VARS = {};
//the container used to store all the reserved functions
var RESERVED = [];
var WARNINGS = [];
/**
* Use this when errors are suppressible
* @param {String} msg
* @param {object} ErrorObj
*/
var err = function (msg, ErrorObj) {
if(!Settings.suppress_errors) {
if(ErrorObj)
throw new ErrorObj(msg);
else
throw new Error(msg);
}
};
//Utils ========================================================================
var customError = function (name) {
var E = function (message) {
this.name = name;
this.message = message !== undefined ? message : '';
var error = new Error(this.message);
error.name = this.name;
this.stack = error.stack;
}; //create an empty error
E.prototype = Object.create(Error.prototype);
return E;
};
/**
* Checks to see if value is one of nerdamer's reserved names
* @param {String} value
* @return boolean
*/
var isReserved = function (value) {
return RESERVED.indexOf(value) !== -1;
};
/**
* Checks to see that all symbols in array are the same
* @param {Symbol[]} arr
* @returns {bool}
*/
var allSame = function (arr) {
var last = arr[0];
for(var i = 1, l = arr.length; i < l; i++)
if(!arr[i].equals(last))
return false;
return true;
};
/**
* Used to pass warnings or low severity errors about the library
* @param msg
*/
var warn = function (msg) {
WARNINGS.push(msg);
if(Settings.SHOW_WARNINGS && console && console.warn) {
console.warn(msg);
}
};
/**
* Enforces rule: "must start with a letter or underscore and
* can have any number of underscores, letters, and numbers thereafter."
* @param name The name of the symbol being checked
* @param {String} typ - The type of symbols that's being validated
* @throws {Exception} - Throws an exception on fail
*/
var validateName = function (name, typ) {
typ = typ || 'variable';
if(Settings.ALLOW_CHARS.indexOf(name) !== -1)
return;
var regex = Settings.VALIDATION_REGEX;
if(!(regex.test(name))) {
throw new InvalidVariableNameError(name + ' is not a valid ' + typ + ' name');
}
};
/**
* Convert number from scientific format to decimal format
* @param {Number} num
*/
var scientificToDecimal = function (num) {
var nsign = Math.sign(num);
//remove the sign
num = Math.abs(num);
//if the number is in scientific notation remove it
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
var zero = '0',
parts = String(num).toLowerCase().split('e'), //split into coeff and exponent
e = parts.pop(), //store the exponential part
l = Math.abs(e), //get the number of zeros
sign = e / l,
coeff_array = parts[0].split('.');
if(sign === -1) {
l = l - coeff_array[0].length;
if(l < 0) {
num = coeff_array[0].slice(0, l) + '.' + coeff_array[0].slice(l) + (coeff_array.length === 2 ? coeff_array[1] : '');
}
else {
num = zero + '.' + new Array(l + 1).join(zero) + coeff_array.join('');
}
}
else {
var dec = coeff_array[1];
if(dec)
l = l - dec.length;
if(l < 0) {
num = coeff_array[0] + dec.slice(0, l) + '.' + dec.slice(l);
}
else {
num = coeff_array.join('') + new Array(l + 1).join(zero);
}
}
}
return nsign < 0 ? '-' + num : num;
};
/**
* Checks if number is a prime number
* @param {Number} n - the number to be checked
*/
var isPrime = function (n) {
var q = Math.floor(Math.sqrt(n));
for(var i = 2; i <= q; i++) {
if(n % i === 0)
return false;
}
return true;
};
/**
* Generates an object with known variable value for evaluation
* @param {String} variable
* @param {any} value Any stringifyable object
* @returns {Object}
*/
var knownVariable = function (variable, value) {
var o = {};
o[variable] = value;
return o;
};
/**
* Checks if n is a number
* @param {any} n
*/
var isNumber = function (n) {
return /^\d+\.?\d*$/.test(n);
};
/**
* Checks to see if an array contains only numeric values
* @param {Array} arr
*/
var allNumeric = function (arr) {
for(var i = 0; i < arr.length; i++)
if(!isNumber(arr[i]))
return false;
return true;
};
/**
* Checks to see if a number or Symbol is a fraction
* @param {Number|Symbol} num
* @returns {boolean}
*/
var isFraction = function (num) {
if(isSymbol(num))
return isFraction(num.multiplier.toDecimal());
return (num % 1 !== 0);
};
/**
* Checks to see if the object provided is a Symbol
* @param {Object} obj
*/
var isSymbol = function (obj) {
return (obj instanceof Symbol);
};
/**
* Checks to see if the object provided is an Expression
* @param {Object} obj
*/
var isExpression = function (obj) {
return (obj instanceof Expression);
};
/**
* This method traverses the symbol structure and grabs all the variables in a symbol. The variable
* names are then returned in alphabetical order.
* @param {Symbol} obj
* @param {Boolean} poly
* @param {Object} vars - An object containing the variables. Do not pass this in as it generated
* automatically. In the future this will be a Collector object.
* @returns {String[]} - An array containing variable names
*/
var variables = function (obj, poly, vars) {
vars = vars || {
c: [],
add: function (value) {
if(this.c.indexOf(value) === -1 && isNaN(value))
this.c.push(value);
}
};
if(isSymbol(obj)) {
var group = obj.group,
prevgroup = obj.previousGroup;
if(group === EX)
variables(obj.power, poly, vars);
if(group === CP || group === CB || prevgroup === CP || prevgroup === CB) {
for(var x in obj.symbols) {
variables(obj.symbols[x], poly, vars);
}
}
else if(group === S || prevgroup === S) {
//very crude needs fixing. TODO
if(!(obj.value === 'e' || obj.value === 'pi' || obj.value === Settings.IMAGINARY))
vars.add(obj.value);
}
else if(group === PL || prevgroup === PL) {
variables(firstObject(obj.symbols), poly, vars);
}
else if(group === EX) {
if(!isNaN(obj.value))
vars.add(obj.value);
variables(obj.power, poly, vars);
}
else if(group === FN && !poly) {
for(var i = 0; i < obj.args.length; i++) {
variables(obj.args[i], poly, vars);
}
}
}
return vars.c.sort();
};
/**
* Returns the sum of an array
* @param {Array} arr
* @param {boolean} toNumber
* @returns {Symbol}
*/
var arraySum = function (arr, toNumber) {
var sum = new Symbol(0);
for(var i = 0; i < arr.length; i++) {
var x = arr[i];
// Convert to symbol if not
sum = _.add(sum, !isSymbol(x) ? _.parse(x) : x);
}
return toNumber ? Number(sum) : sum;
};
/**
* Separates out the variables into terms of variabls.
* e.g. x+y+x*y+sqrt(2)+pi returns
* {x: x, y: y, x y: x*y, constants: sqrt(2)+pi
* @param {type} symbol
* @param {type} o
* @returns {undefined}
* @throws {Error} for expontentials
*/
var separate = function (symbol, o) {
symbol = _.expand(symbol);
o = o || {};
var insert = function (key, sym) {
if(!o[key])
o[key] = new Symbol(0);
o[key] = _.add(o[key], sym.clone());
};
symbol.each(function (x) {
if(x.isConstant('all')) {
insert('constants', x);
}
else if(x.group === S) {
insert(x.value, x);
}
else if(x.group === FN && (x.fname === ABS || x.fname === '')) {
separate(x.args[0]);
}
else if(x.group === EX || x.group === FN) {
throw new Error('Unable to separate. Term cannot be a function!');
}
else {
insert(variables(x).join(' '), x);
}
});
return o;
};
/**
* Fills holes in an array with zero symbol or generates one with n zeroes
* @param {Array} arr
* @param {Number} n
*/
var fillHoles = function (arr, n) {
n = n || arr.length;
for(var i = 0; i < n; i++) {
var sym = arr[i];
if(!sym)
arr[i] = new Symbol(0);
}
return arr;
};
/**
*
* Checks to see if the object provided is a Vector
* @param {Object} obj
*/
var isVector = function (obj) {
return (obj instanceof Vector);
};
/**
* Checks to see if the object provided is a Matrix
* @param {Object} obj
*/
var isMatrix = function (obj) {
return (obj instanceof Matrix);
};
var isSet = function (obj) {
return (obj instanceof Set);
};
/**
* Checks to see if a symbol is in group N
* @param {Symbol} symbol
*/
var isNumericSymbol = function (symbol) {
return symbol.group === N || symbol.group === P;
};
/**
* Checks to see if a symbol is a variable with no multiplier nor power
* @param {Symbol} symbol
*/
var isVariableSymbol = function (symbol) {
return symbol.group === S && symbol.multiplier.equals(1) && symbol.power.equals(1);
};
/**
* Checks to see if the object provided is an Array
* @param {Object} arr
*/
var isArray = function (arr) {
return Array.isArray(arr);
};
/**
* Checks to see if a number is an integer
* @param {Number} num
*/
var isInt = function (num) {
return /^[-+]?\d+e?\+?\d*$/gim.test(num.toString());
};
/**
* @param {Number|Symbol} obj
* @returns {boolean}
*/
var isNegative = function (obj) {
if(isSymbol(obj)) {
obj = obj.multiplier;
}
return obj.lessThan(0);
};
/**
* Safely stringify object
* @param o
*/
var stringify = function (o) {
if(!o)
return o;
return String(o);
};
/**
* @param {String} str
* @returns {String} - returns a formatted string surrounded by brackets
*/
var inBrackets = function (str) {
return '(' + str + ')';
};
/**
* A helper function to replace parts of string
* @param {String} str - The original string
* @param {Integer} from - The starting index
* @param {Integer} to - The ending index
* @param {String} with_str - The replacement string
* @returns {String} - A formatted string
*/
var stringReplace = function (str, from, to, with_str) {
return str.substr(0, from) + with_str + str.substr(to, str.length);
};
/**
* the Parser uses this to check if it's allowed to convert the obj to type Symbol
* @param {Object} obj
* @returns {boolean}
*/
var customType = function (obj) {
return obj !== undefined && obj.custom;
};
/**
* Checks to see if numbers are both negative or are both positive
* @param {Number} a
* @param {Number} b
* @returns {boolean}
*/
var sameSign = function (a, b) {
return (a < 0) === (b < 0);
};
/**
* A helper function to replace multiple occurences in a string. Takes multiple arguments
* @example format('{0} nice, {0} sweet', 'something')
* //returns 'something nice, something sweet'
*/
var format = function () {
var args = [].slice.call(arguments),
str = args.shift();
var new_str = str.replace(/{(\d+)}/g, function (match, index) {
var arg = args[index];
return typeof arg === 'function' ? arg() : arg;
});
return new_str;
};
/**
* Generates an array with values within a range. Multiplies by a step if provided
* @param {Number} start
* @param {Number} end
* @param {Number} step
*/
var range = function (start, end, step) {
var arr = [];
step = step || 1;
for(var i = start; i <= end; i++)
arr.push(i * step);
return arr;
};
/**
* Returns an array of all the keys in an array
* @param {Object} obj
* @returns {Array}
*/
var keys = Object.keys;
/**
* Returns the first encountered item in an object. Items do not have a fixed order in objects
* so only use if you need any first random or if there's only one item in the object
* @param {Object} obj
* @param {String} key Return this key as first object
* @param {Boolean} both
* @returns {*}
*/
var firstObject = function (obj, key, both) {
for(var x in obj)
break;
if(key)
return x;
if(both)
return {
key: x,
obj: obj[x]
};
return obj[x];
};
/**
* Substitutes out variables for two symbols, parses them to a number and them compares them numerically
* @param {Symbol} sym1
* @param {Symbol} sym2
* @param {String[]} vars - an optional array of variables to use
* @returns {bool}
*/
var compare = function (sym1, sym2, vars) {
var n = 5; //a random number between 1 and 5 is good enough
var scope = {}; // scope object with random numbers generated using vars
var comparison;
for(var i = 0; i < vars.length; i++)
scope[vars[i]] = new Symbol(Math.floor(Math.random() * n) + 1);
block('PARSE2NUMBER', function () {
comparison = _.parse(sym1, scope).equals(_.parse(sym2, scope));
});
return comparison;
};
/**
* Is used to set a user defined function using the function assign operator
* @param {String} name
* @param {String[]} params_array
* @param {String} body
* @returns {Boolean}
*/
var setFunction = function (name, params_array, body) {
validateName(name);
if(!isReserved(name)) {
params_array = params_array || variables(_.parse(body));
// The function gets set to PARSER.mapped function which is just
// a generic function call.
_.functions[name] = [_.mapped_function, params_array.length, {
name: name,
params: params_array,
body: body
}];
return body;
}
return null;
};
/**
* Returns the minimum number in an array
* @param {Array} arr
* @returns {Number}
*/
var arrayMax = function (arr) {
return Math.max.apply(undefined, arr);
};
/**
* Returns the maximum number in an array
* @param {Array} arr
* @returns {Number}
*/
var arrayMin = function (arr) {
return Math.min.apply(undefined, arr);
};
/**
* Checks to see if two arrays are equal
* @param {Array} arr1
* @param {Array} arr2
*/
var arrayEqual = function (arr1, arr2) {
arr1.sort();
arr2.sort();
// The must be of the same length
if(arr1.length === arr2.length) {
for(var i = 0; i < arr1.length; i++) {
// If any two items don't match we're done
if(arr1[i] !== arr2[i]) {
return false;
}
}
// Otherwise they're equal
return true;
}
return false;
};
/**
* Clones array with clonable items
* @param {Array} arr
* @returns {Array}
*/
var arrayClone = function (arr) {
var new_array = [], l = arr.length;
for(var i = 0; i < l; i++)
new_array[i] = arr[i].clone();
return new_array;
};
/**
* Fills numbers between array values
* @param {Numbers[]} arr
* @param {Integer} slices
*/
var arrayAddSlices = function (arr, slices) {
slices = slices || 20;
var retval = [];
var c, delta, e;
retval.push(arr[0]); //push the beginning
for(var i = 0; i < arr.length - 1; i++) {
c = arr[i];
delta = arr[i + 1] - c; //get the difference
e = delta / slices; //chop it up in the desired number of slices
for(var j = 0; j < slices; j++) {
c += e; //add the mesh to the last slice
retval.push(c);
}
}
return retval;
};
/**
* Gets nth roots of a number
* @param {Symbol} symbol
* @returns {Vector}
*/
var nroots = function (symbol) {
var a, b;
if(symbol.group === FN && symbol.fname === '') {
a = Symbol.unwrapPARENS(_.parse(symbol).toLinear());
b = _.parse(symbol.power);
}
else if(symbol.group === P) {
a = _.parse(symbol.value);
b = _.parse(symbol.power);
}
if(a && b && (a.group === N) && b.group === N && a.multiplier.isNegative()) {
var _roots = [];
var parts = Symbol.toPolarFormArray(evaluate(symbol));
var r = parts[0];
//var r = _.parse(a).abs().toString();
//https://en.wikipedia.org/wiki/De_Moivre%27s_formula
var x = _.arg(a);
var n = b.multiplier.den.toString();
var p = b.multiplier.num.toString();
var formula = '(({0})^({1})*(cos({3})+({2})*sin({3})))^({4})';
for(var i = 0; i < n; i++) {
var t = evaluate(_.parse(format("(({0})+2*pi*({1}))/({2})", x, i, n))).multiplier.toDecimal();
_roots.push(evaluate(_.parse(format(formula, r, n, Settings.IMAGINARY, t, p))));
}
return Vector.fromArray(_roots);
}
else if(symbol.isConstant(true, true)) {
var sign = symbol.sign();
var x = evaluate(symbol.abs());
var root = _.sqrt(x);
var _roots = [root.clone(), root.negate()];
if(sign < 0)
_roots = _roots.map(function (x) {
return _.multiply(x, Symbol.imaginary());
});
}
else {
_roots = [_.parse(symbol)];
}
return Vector.fromArray(_roots);
};
/**
* Sorts and array given 2 parameters
* @param {String} a
* @param {String} b
*/
var comboSort = function (a, b) {
var l = a.length,
combined = []; //the linker
for(var i = 0; i < a.length; i++) {
combined.push([a[i], b[i]]); //create the map
}
combined.sort(function (x, y) {
return x[0] - y[0];
});
var na = [], nb = [];
for(i = 0; i < l; i++) {
na.push(combined[i][0]);
nb.push(combined[i][1]);
}
return [na, nb];
};
/**
* TODO: Pick a more descriptive name and better description
* Breaks a function down into it's parts wrt to a variable, mainly coefficients
* Example a*x^2+b wrt x
* @param {Symbol} fn
* @param {String} wrt
* @param {bool} as_obj
*/
var decompose_fn = function (fn, wrt, as_obj) {
wrt = String(wrt); //convert to string
var ax, a, x, b;
if(fn.group === CP) {
var t = _.expand(fn.clone()).stripVar(wrt);
ax = _.subtract(fn.clone(), t.clone());
b = t;
}
else
ax = fn.clone();
a = ax.stripVar(wrt);
x = _.divide(ax.clone(), a.clone());
b = b || new Symbol(0);
if(as_obj)
return {
a: a,
x: x,
ax: ax,
b: b
};
return [a, x, ax, b];
};
/**
* Rounds a number up to x decimal places
* @param {Number} x
* @param {Number} s
*/
var nround = function (x, s) {
if(isInt(x)) {
if(x >= Number.MAX_VALUE)
return x.toString();
return Number(x);
}
s = typeof s === 'undefined' ? 14 : s;
return Math.round(x * Math.pow(10, s)) / Math.pow(10, s);
};
/**
* Is used for u-substitution. Gets a suitable u for substitution. If for
* instance a is used in the symbol then it keeps going down the line until
* one is found that's not in use. If all letters are taken then it
* starts appending numbers.
* IMPORTANT! It assumes that the substitution will be undone
* beore the user gets to interact with the object again.
* @param {Symbol} symbol
*/
var getU = function (symbol) {
//start with u
var u = 'u', //start with u
v = u, //init with u
c = 0, //postfix number
vars = variables(symbol);
//make sure this variable isn't reserved and isn't in the variable list
while(!(RESERVED.indexOf(v) === - 1 && vars.indexOf(v) === - 1))
v = u + c++;
//get an empty slot. It seems easier to just push but the
//problem is that we may have some which are created by clearU
for(var i = 0, l = RESERVED.length; i <= l; i++)
//reserved cannot equals false or 0 so we can safely check for a falsy type
if(!RESERVED[i]) {
RESERVED[i] = v; //reserve the variable
break;
}
return v;
};
/**
* Clears the u variable so it's no longer reserved
* @param {String} u
*/
var clearU = function (u) {
var indx = RESERVED.indexOf(u);
if(indx !== -1)
RESERVED[indx] = undefined;
};
/**
* Loops through each item in object and calls function with item as param
* @param {Object|Array} obj
* @param {Function} fn
*/
var each = function (obj, fn) {
if(isArray(obj)) {
var l = obj.length;
for(var i = 0; i < l; i++)
fn.call(obj, i);
}
else {
for(var x in obj)
if(obj.hasOwnProperty(x))
fn.call(obj, x);
}
};
/**
* Checks to see if a number is an even number
* @param {Number} num
* @returns {boolean}
*/
var even = function (num) {
return num % 2 === 0;
};
/**
* Checks to see if a fraction is divisible by 2
* @param {Number} num
* @returns {boolean}
*/
var evenFraction = function (num) {
return 1 / (num % 1) % 2 === 0;
};
/**
* Strips duplicates out of an array
* @param {Array} arr
*/
var arrayUnique = function (arr) {
var l = arr.length, a = [];
for(var i = 0; i < l; i++) {
var item = arr[i];
if(a.indexOf(item) === -1)
a.push(item);
}
return a;
};
/**
* Gets all the variables in an array of Symbols
* @param {Symbol[]} arr
*/
var arrayGetVariables = function (arr) {
var vars = variables(arr[0], null, null, true);
//get all variables
for(var i = 1, l = arr.length; i < l; i++)
vars = vars.concat(variables(arr[i]));
//remove duplicates
vars = arrayUnique(vars).sort();