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/Algebra.js CHANGED
@@ -2394,6 +2394,7 @@ if((typeof module) !== 'undefined') {
2394
2394
  return symbol;
2395
2395
  },
2396
2396
  _factor: function (symbol, factors) {
2397
+ var g = symbol.group;
2397
2398
  //some items cannot be factored any further so return those right away
2398
2399
  if(symbol.group === FN) {
2399
2400
  var arg = symbol.args[0];
@@ -2575,11 +2576,20 @@ if((typeof module) !== 'undefined') {
2575
2576
 
2576
2577
  //last minute clean up
2577
2578
  symbol = _.parse(symbol, core.Utils.getFunctionsSubs(map));
2578
-
2579
+
2580
+ var addPower = factors.length === 1;
2581
+
2579
2582
  factors.add(_.pow(symbol, _.parse(p)));
2580
2583
 
2581
2584
  var retval = factors.toSymbol();
2582
-
2585
+
2586
+ // We may have only factored out the symbol itself so we end up with a factor of one
2587
+ // where the power needs to be placed back
2588
+ // e.g. factor((2*y+p)^2). Here we end up having a factor of 1 remaining and a p of 2.
2589
+ if(addPower && symbol.equals(1) && retval.isLinear()) {
2590
+ retval = _.pow(retval, _.parse(p));
2591
+ }
2592
+
2583
2593
  return retval;
2584
2594
  }
2585
2595
 
@@ -2910,11 +2920,17 @@ if((typeof module) !== 'undefined') {
2910
2920
 
2911
2921
  var div = __.div(symbol, d.clone()),
2912
2922
  is_factor = div[1].equals(0);
2913
-
2923
+
2924
+ // Break infinite loop for factoring e^t*x-1
2925
+ if((symbol.equals(div[0]) && div[1].equals(0))) {
2926
+ break;
2927
+ }
2928
+
2914
2929
  if(div[0].isConstant()) {
2915
2930
  factors.add(div[0]);
2916
2931
  break;
2917
2932
  }
2933
+
2918
2934
  }
2919
2935
  else
2920
2936
  is_factor = false;
@@ -4172,7 +4188,7 @@ if((typeof module) !== 'undefined') {
4172
4188
  var symbols = symbol.collectSymbols();
4173
4189
  //assumption 1.
4174
4190
  //since it's a composite, it has a length of at least 1
4175
- var retval, a, b, d1, d2, n1, n2, x, y, c, den, num;
4191
+ var retval, a, b, d1, d2, n1, n2, s, x, y, c, den, num;
4176
4192
  a = symbols.pop(); //grab the first symbol
4177
4193
  //loop through each term and make denominator common
4178
4194
  while(symbols.length) {
@@ -4184,11 +4200,11 @@ if((typeof module) !== 'undefined') {
4184
4200
  c = _.multiply(d1.clone(), d2.clone());
4185
4201
  x = _.multiply(n1, d2);
4186
4202
  y = _.multiply(n2, d1);
4187
- a = _.divide(_.add(x, y), c);
4203
+ s = _.add(x, y);
4204
+ a = _.divide(s, c);
4188
4205
  }
4189
4206
  den = _.expand(a.getDenom());
4190
4207
  num = _.expand(a.getNum());
4191
-
4192
4208
  //simplify imaginary
4193
4209
  if(num.isImaginary() && den.isImaginary()) {
4194
4210
  retval = __.Simplify.complexSimp(num, den);
@@ -4345,7 +4361,6 @@ if((typeof module) !== 'undefined') {
4345
4361
  symbol = sym_array.pop();
4346
4362
  //remove gcd from denominator
4347
4363
  symbol = __.Simplify.fracSimp(symbol);
4348
-
4349
4364
  //nothing more to do
4350
4365
  if(symbol.isConstant() || symbol.group === core.groups.S) {
4351
4366
  sym_array.push(symbol);
@@ -4549,5 +4564,5 @@ if((typeof module) !== 'undefined') {
4549
4564
  }
4550
4565
  }
4551
4566
  ]);
4552
- nerdamer.api();
4567
+ nerdamer.updateAPI();
4553
4568
  })();
package/Calculus.js CHANGED
@@ -1223,6 +1223,10 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
1223
1223
  __.integration.stop();
1224
1224
  }
1225
1225
  }
1226
+ else if(p === 1/2 && x.power.equals(2) && a.greaterThan(0)) {
1227
+ // TODO: Revisit
1228
+ __.integration.stop();
1229
+ }
1226
1230
  else {
1227
1231
  if(x.isLinear() && x.group !== PL)
1228
1232
  retval = _.divide(__.integration.poly_integrate(symbol), a);
@@ -2666,6 +2670,6 @@ if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
2666
2670
  }
2667
2671
  ]);
2668
2672
  //link registered functions externally
2669
- nerdamer.api();
2673
+ nerdamer.updateAPI();
2670
2674
 
2671
2675
  })();
package/Extra.js CHANGED
@@ -613,7 +613,7 @@ if((typeof module) !== 'undefined') {
613
613
  ]);
614
614
 
615
615
  //link registered functions externally
616
- nerdamer.api();
616
+ nerdamer.updateAPI();
617
617
  }());
618
618
 
619
619
  // Added for all.min.js
package/README.md CHANGED
@@ -20,6 +20,25 @@ Load the library in your html page
20
20
  <script src="Solve.js"></script>
21
21
  <script src="Extra.js"></script>
22
22
  ```
23
+ Or import everything
24
+ ```html
25
+ <script src="all.min.js"></script> <!-- assuming you've saved the file in the root -->
26
+ ```
27
+ If you're using node.js install it using `npm i nerdamer` and then
28
+
29
+ ```javascript
30
+ // const cannot be used since nerdamer gets modified when other modules are loaded
31
+ var nerdamer = require('nerdamer');
32
+ // Load additional modules. These are not required.
33
+ require('nerdamer/Algebra');
34
+ require('nerdamer/Calculus');
35
+ require('nerdamer/Solve');
36
+ require('nerdamer/Extra');
37
+ ```
38
+ Or do a single import to import everything
39
+ ```javascript
40
+ const nerdamer = require("nerdamer/all.min")
41
+ ```
23
42
 
24
43
  Some functions have dependencies from other add-ons.
25
44
 
@@ -29,7 +48,7 @@ For full documentation go to http://nerdamer.com/documentation
29
48
 
30
49
  All operations are done using the 'nerdamer' object.
31
50
 
32
- To add an expression just add it to the nerdamer object which will return a expression object.
51
+ To add an expression just add it to the nerdamer object which will return a `Expression` object.
33
52
 
34
53
  ```javascript
35
54
  var e = nerdamer('x^2+2*(cos(x)+x*x)');
@@ -38,6 +57,16 @@ console.log(e.text());
38
57
  //result:
39
58
  //2*cos(x)+3*x^2
40
59
  ```
60
+ It is also possible to use `nerdamer` functions directly within the need for string manipulation of the input. The input will be parsed and the output will of type `Expression`. For example:
61
+ ```javascript
62
+ var ans = nerdamer.expand('(x-1)^5');
63
+ console.log(ans.text());
64
+ // -1-10*x^2-5*x^4+10*x^3+5*x+x^5
65
+
66
+ var sol = nerdamer.solve('x^2-4', 'x');
67
+ console.log(sol.text())
68
+ // [2,-2]
69
+ ```
41
70
 
42
71
  You can also pass in an object with known values as the second parameter.
43
72
 
package/Solve.js CHANGED
@@ -97,7 +97,7 @@ if((typeof module) !== 'undefined') {
97
97
  * This is an equation that has a left hand side and a right hand side
98
98
  */
99
99
  function Equation(lhs, rhs) {
100
- if(rhs.isConstant() && lhs.isConstant() && !lhs.equals(rhs) || lhs.equals(core.Settings.IMAGINARY) || rhs.equals(core.Settings.IMAGINARY))
100
+ if(rhs.isConstant() && lhs.isConstant() && !lhs.equals(rhs) || lhs.equals(core.Settings.IMAGINARY) && rhs.isConstant(true) || rhs.equals(core.Settings.IMAGINARY) && lhs.isConstant(true))
101
101
  throw new core.exceptions.NerdamerValueError(lhs.toString() + ' does not equal ' + rhs.toString());
102
102
  this.LHS = lhs; //left hand side
103
103
  this.RHS = rhs; //right and side
@@ -124,12 +124,19 @@ if((typeof module) !== 'undefined') {
124
124
  }
125
125
  var a = eqn.LHS;
126
126
  var b = eqn.RHS;
127
+
127
128
  if(a.isConstant(true) && !b.isConstant(true)) {
128
129
  // Swap them to avoid confusing parser and cause an infinite loop
129
130
  [a, b] = [b, a];
130
131
  }
131
132
  var _t = _.subtract(a, b);
132
133
  var retval = expand ? _.expand(_t) : _t;
134
+
135
+ // Quick workaround for issue #636
136
+ // This basically borrows the removeDenom method from the Equation class.
137
+ // TODO: Make this function a stand-alone function
138
+ retval = new Equation(retval, new Symbol(0)).removeDenom().LHS;
139
+
133
140
  return retval;
134
141
  },
135
142
  removeDenom: function () {
@@ -605,7 +612,43 @@ if((typeof module) !== 'undefined') {
605
612
  }
606
613
 
607
614
  vars = core.Utils.arrayGetVariables(eqns);
615
+
616
+ // If the system only has one variable then we solve for the first one and
617
+ // then test the remaining equations with that solution. If any of the remaining
618
+ // equation fails then the system has no solution
619
+ if(vars.length === 1) {
620
+ var n = 0,
621
+ sol, e;
622
+ do {
623
+ var e = eqns[n].clone();
624
+
625
+ if(n > 0) {
626
+ e = e.sub(vars[0], sol[0]);
627
+ }
608
628
 
629
+ sol = solve(e, vars[0]);
630
+ // Skip the first one
631
+ if(n === 0)
632
+ continue;
633
+ }
634
+ while(++n < eqns.length)
635
+
636
+ // Format the output
637
+ var solutions;
638
+ if(Settings.SOLUTIONS_AS_OBJECT) {
639
+ solutions = {};
640
+ solutions[vars[0]] = sol;
641
+ }
642
+ else if(sol.length === 0) {
643
+ solutions = sol; // No solutions
644
+ }
645
+ else {
646
+ solutions = [vars[0], sol];
647
+ }
648
+
649
+ return solutions;
650
+ }
651
+
609
652
  // Deal with redundant equations as expressed in #562
610
653
  // The fix is to remove all but the number of equations equal to the number
611
654
  // of variables. We then solve those and then evaluate the remaining equations
@@ -748,6 +791,8 @@ if((typeof module) !== 'undefined') {
748
791
  */
749
792
  quad: function (c, b, a) {
750
793
  var discriminant = _.subtract(_.pow(b.clone(), Symbol(2)), _.multiply(_.multiply(a.clone(), c.clone()), Symbol(4)))/*b^2 - 4ac*/;
794
+ // Fix for #608
795
+ discriminant = _.expand(discriminant);
751
796
  var det = _.pow(discriminant, Symbol(0.5));
752
797
  var den = _.parse(_.multiply(new Symbol(2), a.clone()));
753
798
  var retval = [
@@ -1357,6 +1402,7 @@ if((typeof module) !== 'undefined') {
1357
1402
  return symbol;
1358
1403
  };
1359
1404
 
1405
+
1360
1406
  //separate the equation
1361
1407
  var separate = function (eq) {
1362
1408
  var lhs = new Symbol(0),
@@ -1561,8 +1607,11 @@ if((typeof module) !== 'undefined') {
1561
1607
  var separated = separate(eq);
1562
1608
  var lhs = separated[0],
1563
1609
  rhs = separated[1];
1610
+
1564
1611
  if(lhs.group === core.groups.EX) {
1565
- add_to_result(_.parse(core.Utils.format(core.Settings.LOG + '(({0})/({2}))/' + core.Settings.LOG + '({1})', rhs, lhs.value, lhs.multiplier)));
1612
+ var log = core.Settings.LOG;
1613
+ var expr_str = `${log}((${rhs})/(${lhs.multiplier}))/${log}(${lhs.value})/${lhs.power.multiplier}`;
1614
+ add_to_result(_.parse(expr_str));
1566
1615
  }
1567
1616
  break;
1568
1617
  case 1:
@@ -1730,5 +1779,5 @@ if((typeof module) !== 'undefined') {
1730
1779
  }
1731
1780
  }
1732
1781
  ]);
1733
- nerdamer.api();
1782
+ nerdamer.updateAPI();
1734
1783
  })();