nerdamer 1.1.12 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gulpfile.js CHANGED
@@ -1,16 +1,16 @@
1
- var gulp = require('gulp');
2
- var concat = require('gulp-concat');
3
- var uglify = require('gulp-uglify');
4
-
5
- var devFiles = ['./nerdamer.core.js','./Algebra.js', './Calculus.js', './Solve.js', './Extra.js'];
6
-
7
- gulp.task('concat_all', function() {
8
- return gulp.src(devFiles)
9
- .pipe(concat('all.min.js'))
10
- .pipe(uglify())
11
- .pipe(gulp.dest('./'));
12
- });
13
-
14
- gulp.task('watch', function() {
15
- gulp.watch(devFiles, gulp.series('concat_all'));
1
+ var gulp = require('gulp');
2
+ var concat = require('gulp-concat');
3
+ var uglify = require('gulp-uglify');
4
+
5
+ var devFiles = ['./nerdamer.core.js','./Algebra.js', './Calculus.js', './Solve.js', './Extra.js'];
6
+
7
+ gulp.task('concat_all', function() {
8
+ return gulp.src(devFiles)
9
+ .pipe(concat('all.min.js'))
10
+ .pipe(uglify())
11
+ .pipe(gulp.dest('./'));
12
+ });
13
+
14
+ gulp.task('watch', function() {
15
+ gulp.watch(devFiles, gulp.series('concat_all'));
16
16
  });
package/index.d.ts CHANGED
@@ -68,6 +68,12 @@ declare namespace nerdamer {
68
68
  */
69
69
  export function convertToLaTeX(expression: string): string
70
70
 
71
+ /**
72
+ * Attempts to import a LaTeX string.
73
+ * @param TeX The expression being converted.
74
+ */
75
+ export function convertFromLaTeX(TeX: string): Expression
76
+
71
77
  /**
72
78
  * Each time an expression is parsed nerdamer stores the result. Use this method to get back stored expressions.
73
79
  * @param asObject Pass in true to get expressions as numbered object with 1 as starting index
package/nerdamer.core.js CHANGED
@@ -13,10 +13,10 @@
13
13
  //var nerdamerBigDecimal = typeof nerdamerBigDecimal !== 'undefined' ? nerdamerBigDecimal : require('big.js');
14
14
 
15
15
  var nerdamer = (function (imports) {
16
- "use strict";
16
+ "use strict";
17
17
 
18
18
  //version ======================================================================
19
- var version = '1.1.12';
19
+ var version = '1.1.13';
20
20
 
21
21
  //inits ========================================================================
22
22
  var _ = new Parser(); //nerdamer's parser
@@ -92,8 +92,12 @@ var nerdamer = (function (imports) {
92
92
  CACHE: {},
93
93
  //Print out warnings or not
94
94
  SILENCE_WARNINGS: false,
95
- //Precision
95
+ // Precision
96
96
  PRECISION: 21,
97
+ // The Expression defaults to this value for decimal places
98
+ EXPRESSION_DECP: 19,
99
+ // The text function defaults to this value for decimal places
100
+ DEFAULT_DECP: 16,
97
101
  //function mappings
98
102
  VECTOR: 'vector',
99
103
  PARENTHESIS: 'parens',
@@ -2223,7 +2227,7 @@ var nerdamer = (function (imports) {
2223
2227
  * @param {int} useGroup
2224
2228
  * @returns {String}
2225
2229
  */
2226
- function text(obj, option, useGroup, decp) {
2230
+ function text(obj, option, useGroup, decp) {
2227
2231
  var asHash = option === 'hash',
2228
2232
  //whether to wrap numbers in brackets
2229
2233
  wrapCondition = undefined,
@@ -2231,7 +2235,7 @@ var nerdamer = (function (imports) {
2231
2235
  asDecimal = opt === 'decimal' || opt === 'decimals';
2232
2236
 
2233
2237
  if(asDecimal && typeof decp === 'undefined')
2234
- decp = 16;
2238
+ decp = Settings.DEFAULT_DECP;
2235
2239
 
2236
2240
  function toString(obj) {
2237
2241
  switch(option)
@@ -2313,7 +2317,7 @@ var nerdamer = (function (imports) {
2313
2317
  case 'scientific':
2314
2318
  wrapCondition = wrapCondition || function (str) {
2315
2319
  return false;
2316
- }
2320
+ };
2317
2321
  return new Scientific(obj.valueOf()).toString(Settings.SCIENTIFIC_MAX_DECIMAL_PLACES);
2318
2322
  default:
2319
2323
  wrapCondition = wrapCondition || function (str) {
@@ -2577,7 +2581,7 @@ var nerdamer = (function (imports) {
2577
2581
  * @returns {String}
2578
2582
  */
2579
2583
  text: function (opt, n) {
2580
- n = n || 19;
2584
+ n = n || Settings.EXPRESSION_DECP;
2581
2585
  opt = opt || 'decimals';
2582
2586
  if(this.symbol.text_)
2583
2587
  return this.symbol.text_(opt);
@@ -2792,135 +2796,7 @@ var nerdamer = (function (imports) {
2792
2796
  //Aliases
2793
2797
  Expression.prototype.toTeX = Expression.prototype.latex;
2794
2798
 
2795
- //Scientific ===================================================================
2796
- function Scientific(num) {
2797
- if(!(this instanceof Scientific))
2798
- return new Scientific(num);
2799
-
2800
- num = String(typeof num === 'undefined' ? 0 : num); //convert to a string
2801
-
2802
- //remove the sign
2803
- if(num.startsWith('-')) {
2804
- this.sign = -1;
2805
- //remove the sign
2806
- num = num.substr(1, num.length);
2807
- }
2808
- else {
2809
- this.sign = 1;
2810
- }
2811
-
2812
- if(Scientific.isScientific(num)) {
2813
- this.fromScientific(num);
2814
- }
2815
- else {
2816
- this.convert(num);
2817
- }
2818
- return this;
2819
- }
2820
-
2821
- Scientific.prototype = {
2822
- fromScientific: function (num) {
2823
- var parts = String(num).toLowerCase().split('e');
2824
- this.coeff = parts[0];
2825
- this.exponent = parts[1];
2826
-
2827
- return this;
2828
- },
2829
- convert: function (num) {
2830
- //get wholes and decimals
2831
- var parts = num.split('.');
2832
- //make zero go away
2833
- var w = parts[0] || '';
2834
- var d = parts[1] || '';
2835
- //convert zero to blank strings
2836
- w = Scientific.removeLeadingZeroes(w);
2837
- d = Scientific.removeTrailingZeroes(d);
2838
- //find the location of the decimal place which is right after the wholes
2839
- var dot_location = w.length;
2840
- //add them together so we can move the dot
2841
- var n = w + d;
2842
- //find the next number
2843
- var zeroes = Scientific.leadingZeroes(n).length;
2844
- //set the exponent
2845
- this.exponent = dot_location - (zeroes + 1);
2846
- //set the coeff but first remove leading zeroes
2847
- var coeff = Scientific.removeLeadingZeroes(n);
2848
- this.coeff = coeff.charAt(0) + '.' + (coeff.substr(1, coeff.length) || '0');
2849
-
2850
- return this;
2851
- },
2852
- round: function (num) {
2853
- var n = this.copy();
2854
-
2855
- num = Number(num); //cast to number for safety
2856
- //since we know it guaranteed to be in the format {digit}{optional dot}{optional digits}
2857
- //we can round based on this
2858
- if(num === 0)
2859
- n.coeff = n.coeff.charAt(0);
2860
- else {
2861
- //get up to n-1 digits
2862
- var rounded = this.coeff.substring(0, num + 1);
2863
- //get the next two
2864
- var next_two = this.coeff.substring(num + 1, num + 3);
2865
- //the extra digit
2866
- var ed = next_two.charAt(0);
2867
-
2868
- if(next_two.charAt(1) > 4)
2869
- ed++;
2870
-
2871
- n.coeff = rounded + ed;
2872
- }
2873
-
2874
- return n;
2875
- },
2876
- copy: function () {
2877
- var n = new Scientific(0);
2878
- n.coeff = this.coeff;
2879
- n.exponent = this.exponent;
2880
- n.sign = this.sign;
2881
- return n;
2882
- },
2883
- toString: function (n) {
2884
- var coeff = typeof n === 'undefined' ? this.coeff : Scientific.round(this.coeff, n);
2885
-
2886
- var c;
2887
- if(this.exponent === 0 && Settings.SCIENTIFIC_IGNORE_INTS) {
2888
- c = this.coeff;
2889
- }
2890
- else {
2891
- c = coeff + 'e' + this.exponent;
2892
- }
2893
- return (this.sign === -1 ? '-' : '') + c;
2894
- }
2895
- };
2896
-
2897
- Scientific.isScientific = function (num) {
2898
- return /\d+\.?\d*e[\+\-]*\d+/i.test(num);
2899
- };
2900
- Scientific.leadingZeroes = function (num) {
2901
- var match = num.match(/^(0*).*$/);
2902
- return match ? match[1] : '';
2903
- };
2904
- Scientific.removeLeadingZeroes = function (num) {
2905
- var match = num.match(/^0*(.*)$/);
2906
- return match ? match[1] : '';
2907
- };
2908
-
2909
- Scientific.removeTrailingZeroes = function (num) {
2910
- var match = num.match(/0*$/);
2911
- return match ? num.substring(0, num.length - match[0].length) : '';
2912
- };
2913
- Scientific.round = function (c, n) {
2914
- var coeff = nround(c, n);
2915
- var m = String(coeff).split('.').pop();
2916
- var d = n - m.length;
2917
- //if we're asking for more significant figures
2918
- if(d > 0) {
2919
- coeff = coeff + (new Array(d + 1).join(0));
2920
- }
2921
- return coeff;
2922
- };
2923
-
2799
+
2924
2800
  //Scientific ===================================================================
2925
2801
  /*
2926
2802
  * Javascript has the toExponential method but this allows you to work with string and therefore any number of digits of your choosing
@@ -3058,6 +2934,16 @@ var nerdamer = (function (imports) {
3058
2934
  return match ? num.substring(0, num.length - match[0].length) : '';
3059
2935
  };
3060
2936
 
2937
+ Scientific.round = function (c, n) {
2938
+ var coeff = nround(c, n);
2939
+ var m = String(coeff).split('.').pop();
2940
+ var d = n - m.length;
2941
+ //if we're asking for more significant figures
2942
+ if(d > 0) {
2943
+ coeff = coeff + (new Array(d + 1).join(0));
2944
+ }
2945
+ return coeff;
2946
+ };
3061
2947
 
3062
2948
  //Frac =========================================================================
3063
2949
  function Frac(n) {
@@ -3221,13 +3107,14 @@ var nerdamer = (function (imports) {
3221
3107
  var dec = whole.toString() + '.' + narr.join('');
3222
3108
  return sign + dec;
3223
3109
  },
3224
- toDecimal: function (prec) {
3110
+ toDecimal: function (prec) {
3225
3111
  prec = prec || Settings.PRECISION;
3226
3112
  if(prec) {
3227
3113
  return this.decimal(prec);
3228
3114
  }
3229
- else
3115
+ else {
3230
3116
  return this.num / this.den;
3117
+ }
3231
3118
  },
3232
3119
  qcompare: function (n) {
3233
3120
  return [this.num.multiply(n.den), n.num.multiply(this.den)];
@@ -3307,7 +3194,8 @@ var nerdamer = (function (imports) {
3307
3194
  // if(this.num == 24) throw new Error(999)
3308
3195
  if(Settings.USE_BIG)
3309
3196
  return new bigDec(this.num.toString()).div(new bigDec(this.den.toString()));
3310
- return this.num / this.den;
3197
+ var retval = this.num / this.den;
3198
+ return retval;
3311
3199
  },
3312
3200
  isNegative: function () {
3313
3201
  return this.toDecimal() < 0;
@@ -4680,7 +4568,7 @@ var nerdamer = (function (imports) {
4680
4568
  symbol = _.expand(symbol);
4681
4569
 
4682
4570
  //if the symbol already is the denominator... DONE!!!
4683
- if(symbol.power.lessThan(0)) {
4571
+ if(symbol.power.lessThan(0) || symbol.group === EX && symbol.power.multiplier.lessThan(0)) {
4684
4572
  var d = _.parse(symbol.multiplier.den);
4685
4573
  retval = symbol.toUnitMultiplier();
4686
4574
  retval.power.negate();
@@ -4688,12 +4576,15 @@ var nerdamer = (function (imports) {
4688
4576
  }
4689
4577
  else if(symbol.group === CB) {
4690
4578
  retval = _.parse(symbol.multiplier.den);
4691
- for(var x in symbol.symbols)
4692
- if(symbol.symbols[x].power < 0)
4579
+ for(var x in symbol.symbols) {
4580
+ var s = symbol.symbols[x];
4581
+ if(s.power < 0 || s.group === EX && s.power.multiplier.lessThan(0))
4693
4582
  retval = _.multiply(retval, symbol.symbols[x].clone().invert());
4583
+ }
4694
4584
  }
4695
- else
4585
+ else {
4696
4586
  retval = _.parse(symbol.multiplier.den);
4587
+ }
4697
4588
  return retval;
4698
4589
  },
4699
4590
  getNum: function () {
@@ -4703,7 +4594,7 @@ var nerdamer = (function (imports) {
4703
4594
  if(symbol.group === CB && symbol.power.lessThan(0))
4704
4595
  symbol = _.expand(symbol);
4705
4596
  //if the symbol already is the denominator... DONE!!!
4706
- if(symbol.power.greaterThan(0) && symbol.group !== CB) {
4597
+ if(symbol.power.greaterThan(0) && symbol.group !== CB || symbol.group === EX && symbol.power.multiplier.greaterThan(0)) {
4707
4598
  retval = _.multiply(_.parse(symbol.multiplier.num), symbol.toUnitMultiplier());
4708
4599
  }
4709
4600
  else if(symbol.group === CB) {
@@ -4714,6 +4605,9 @@ var nerdamer = (function (imports) {
4714
4605
  }
4715
4606
  });
4716
4607
  }
4608
+ // else if(symbol.group === EX && this.previousGroup === S) {
4609
+ // retval = _.multiply(_.parse(symbol.multiplier.num), symbol.toUnitMultiplier());
4610
+ // }
4717
4611
  else {
4718
4612
  retval = _.parse(symbol.multiplier.num);
4719
4613
  }
@@ -7225,12 +7119,15 @@ var nerdamer = (function (imports) {
7225
7119
  if(e.group === FN) {
7226
7120
  var fname = e.fname, f;
7227
7121
 
7228
- if(fname === SQRT)
7122
+ if(fname === SQRT) {
7229
7123
  f = '\\sqrt' + LaTeX.braces(this.toTeX(e.args));
7230
- else if(fname === ABS)
7124
+ }
7125
+ else if(fname === ABS) {
7231
7126
  f = LaTeX.brackets(this.toTeX(e.args), 'abs');
7232
- else if(fname === PARENTHESIS)
7127
+ }
7128
+ else if(fname === PARENTHESIS) {
7233
7129
  f = LaTeX.brackets(this.toTeX(e.args), 'parens');
7130
+ }
7234
7131
  else if(fname === Settings.LOG10) {
7235
7132
  f = '\\' + Settings.LOG10_LATEX + '\\left( ' + this.toTeX(e.args) + '\\right)';
7236
7133
  }
@@ -7299,10 +7196,10 @@ var nerdamer = (function (imports) {
7299
7196
  });
7300
7197
  f = '\\lim_' + LaTeX.braces(args[1] + '\\to ' + args[2]) + ' ' + LaTeX.braces(args[0]);
7301
7198
  }
7302
- else if(fname === FACTORIAL || fname === DOUBLEFACTORIAL)
7199
+ else if(fname === FACTORIAL || fname === DOUBLEFACTORIAL) {
7303
7200
  f = this.toTeX(e.args) + (fname === FACTORIAL ? '!' : '!!');
7201
+ }
7304
7202
  else {
7305
-
7306
7203
  f = LaTeX.latex(e, decimals);
7307
7204
  //f = '\\mathrm'+LaTeX.braces(fname.replace(/_/g, '\\_')) + LaTeX.brackets(this.toTeX(e.args), 'parens');
7308
7205
  }
@@ -12518,9 +12415,9 @@ var nerdamer = (function (imports) {
12518
12415
 
12519
12416
  /**
12520
12417
  * This functions makes internal functions available externally
12521
- * @param {bool} override Override the functions when calling api if it exists
12418
+ * @param {bool} override Override the functions when calling updateAPI if it exists
12522
12419
  */
12523
- libExports.api = function (override) {
12420
+ libExports.updateAPI = function (override) {
12524
12421
  //Map internal functions to external ones
12525
12422
  var linker = function (fname) {
12526
12423
  return function () {
@@ -12587,7 +12484,7 @@ var nerdamer = (function (imports) {
12587
12484
  });
12588
12485
  };
12589
12486
 
12590
- libExports.api();
12487
+ libExports.updateAPI();
12591
12488
 
12592
12489
  return libExports; //Done
12593
12490
  //imports ======================================================================
@@ -12610,4 +12507,4 @@ var nerdamer = (function (imports) {
12610
12507
 
12611
12508
  if((typeof module) !== 'undefined') {
12612
12509
  module.exports = nerdamer;
12613
- };
12510
+ };
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "javascript light-weight symbolic math expression evaluator",
7
7
  "name": "nerdamer",
8
8
  "license": "MIT",
9
- "version": "1.1.12",
9
+ "version": "1.1.13",
10
10
  "homepage": "http://nerdamer.com/",
11
11
  "directory": {
12
12
  "lib": "./"