pimath 0.0.100 → 0.0.102

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/dist/pi.js CHANGED
@@ -941,9 +941,14 @@ class LinearSystem {
941
941
  this.mergeEquations = (eq1, eq2, factor1, factor2) => {
942
942
  // Set and clone the equations.
943
943
  let eq1multiplied = eq1.clone().multiply(new fraction_1.Fraction(factor1)), eq2multiplied = eq2.clone().multiply(new fraction_1.Fraction(factor2));
944
+ // @ts-ignore
945
+ console.log(eq1.tex, eq1multiplied.tex, factor1.tex);
946
+ // @ts-ignore
947
+ console.log(eq2.tex, eq2multiplied.tex, factor2.tex);
944
948
  // Add both equations together.
945
949
  eq1multiplied.left.add(eq2multiplied.left);
946
950
  eq1multiplied.right.add(eq2multiplied.right);
951
+ console.log('resulting reduction', eq1multiplied.tex);
947
952
  return eq1multiplied;
948
953
  };
949
954
  // ------------------------------------------
@@ -964,6 +969,7 @@ class LinearSystem {
964
969
  // Get all variables in the linear system
965
970
  let V = this.variables.sort();
966
971
  for (let letter of V) {
972
+ console.log('SOLVING FOR', letter);
967
973
  this._solutions[letter] = this._solveOneLetter(letter, V);
968
974
  }
969
975
  // TODO: LinearSystem - solve: optimization and handle undetermined and undefined systems.
@@ -983,7 +989,7 @@ class LinearSystem {
983
989
  return str;
984
990
  };
985
991
  this._equations = [];
986
- this._letters = 'xy'.split('');
992
+ this._letters = 'xyz'.split('');
987
993
  if (equationStrings !== undefined && equationStrings.length > 0) {
988
994
  this.parse(...equationStrings);
989
995
  }
@@ -1046,7 +1052,6 @@ class LinearSystem {
1046
1052
  equArray.push(equStr.join('&'));
1047
1053
  }
1048
1054
  return `\\left\\{\\begin{array}{${"r".repeat(letters.length)}cl}${equArray.join('\\\\\ ')}\\end{array}\\right.`;
1049
- //return `\\left\\{\\begin{array}{rrrcl}${this._equations.map(equ => `${equ.tex}`).join('\\\\\ \n')}\\end{array}\\right.`;
1050
1055
  }
1051
1056
  get solution() {
1052
1057
  let tex = [];
@@ -1062,9 +1067,9 @@ class LinearSystem {
1062
1067
  console.log(`Undefined (letter ${letter})`);
1063
1068
  return;
1064
1069
  }
1065
- tex.push(this._solutions[letter].value.frac);
1070
+ tex.push(this._solutions[letter].value.tex);
1066
1071
  }
1067
- return `(${tex.join(';')})`;
1072
+ return `\\left(${tex.join(';')}\\right)`;
1068
1073
  }
1069
1074
  // ------------------------------------------
1070
1075
  // Mathematical operations
@@ -1073,8 +1078,15 @@ class LinearSystem {
1073
1078
  // TODO: handle other signs for equations ?
1074
1079
  // Get the monom for the particular letter.
1075
1080
  let c1 = eq1.left.monomByDegree(1, letter).coefficient.clone(), c2 = eq2.left.monomByDegree(1, letter).coefficient.clone().opposed();
1081
+ console.log('reduction: ', letter, eq1.tex, eq2.tex, c2.tex, c1.tex);
1076
1082
  return this.mergeEquations(eq1, eq2, c2, c1);
1077
1083
  }
1084
+ /**
1085
+ * Linear reduction of the equations to have only one letter
1086
+ * @param letter letter to isolate
1087
+ * @param V list of variables in the linear system.
1088
+ * @private
1089
+ */
1078
1090
  _solveOneLetter(letter, V) {
1079
1091
  // list of equations.
1080
1092
  let LE = this.clone().equations, reducedEquations = [];
@@ -1085,11 +1097,13 @@ class LinearSystem {
1085
1097
  if (L === letter) {
1086
1098
  continue;
1087
1099
  }
1100
+ console.log('Removing the variable: ', L);
1088
1101
  // Linear reduction.
1089
1102
  // TODO: Search for better association
1090
1103
  for (let i = 0; i < LE.length - 1; i++) {
1091
1104
  reducedEquations.push(this._linearReduction(LE[i], LE[i + 1], L));
1092
1105
  }
1106
+ console.log(reducedEquations.map(x => x.tex));
1093
1107
  // Keep track of each steps.
1094
1108
  this._resolutionSteps.push(new LinearSystem().parse(...reducedEquations));
1095
1109
  // Set the list of equations to the new version.
@@ -1100,6 +1114,7 @@ class LinearSystem {
1100
1114
  // Solve the equations
1101
1115
  let E = this._resolutionSteps[this._resolutionSteps.length - 1].equations[0];
1102
1116
  E.solve();
1117
+ console.log('Solutions for ', letter, ': ', E.solutions[0].tex);
1103
1118
  return {
1104
1119
  value: new fraction_1.Fraction(E.solutions[0].value),
1105
1120
  isReal: E.isReal,
@@ -1477,7 +1492,7 @@ class Monom {
1477
1492
  this._coefficient.add(m.coefficient);
1478
1493
  }
1479
1494
  else {
1480
- console.log('Add: Is not similar: ', m.display);
1495
+ console.log('Add monom: ' + this.display + ' is not similar with ', m.display);
1481
1496
  }
1482
1497
  }
1483
1498
  return this;
@@ -2378,6 +2393,7 @@ class Polynom {
2378
2393
  // Mathematical operations
2379
2394
  this.add = (...values) => {
2380
2395
  this.mark_as_dirty();
2396
+ // @ts-ignore
2381
2397
  for (let value of values) {
2382
2398
  if (value instanceof Polynom) {
2383
2399
  this._monoms = this._monoms.concat(value.monoms);
@@ -2573,7 +2589,7 @@ class Polynom {
2573
2589
  let polynomStringNormalized = polynomString.replaceAll('*', ''), polynomStringReduced = '' + polynomStringNormalized, factors = [];
2574
2590
  for (let x of polynomStringNormalized.matchAll(/\(([a-z0-9+\-]+)\)(\^[0-9]*)?/g)) {
2575
2591
  if (x[2] !== undefined) {
2576
- for (let i = 0; i < +x[2].substr(1); i++) {
2592
+ for (let i = 0; i < +x[2].substring(1); i++) {
2577
2593
  factors.push(x[1]);
2578
2594
  }
2579
2595
  }
@@ -2588,9 +2604,18 @@ class Polynom {
2588
2604
  let polyFactors = factors.map(x => new Polynom(x));
2589
2605
  // Factorize the current polynom.
2590
2606
  this.factorize();
2607
+ // console.log('RESULT BEFORE COMPARE')
2608
+ // console.log(polynomString, polynomStringNormalized)
2609
+ // console.log(factors)
2610
+ // console.log(this.factors.map(x=>x.display))
2591
2611
  // Compare the given factors with the generated factors
2592
2612
  let sign = 1;
2593
2613
  for (let f of this.factors) {
2614
+ if (f.degree().isZero()) {
2615
+ if (f.monoms[0].coefficient.isNegativeOne()) {
2616
+ sign = -sign;
2617
+ }
2618
+ }
2594
2619
  for (let i = 0; i < polyFactors.length; i++) {
2595
2620
  if (f.isEqual(polyFactors[i])) {
2596
2621
  polyFactors.splice(i, 1);
@@ -2654,13 +2679,28 @@ class Polynom {
2654
2679
  };
2655
2680
  // -------------------------------------
2656
2681
  this.reduce = () => {
2657
- for (let i = 0; i < this._monoms.length; i++) {
2658
- for (let j = i + 1; j < this._monoms.length; j++) {
2659
- if (this._monoms[i].isSameAs(this.monoms[j])) {
2660
- this._monoms[i].add(this.monoms[j]);
2661
- this._monoms.splice(j, 1);
2682
+ // Reduce the polynom
2683
+ let values = [...this._monoms], vars = [...this.variables];
2684
+ this._monoms = [];
2685
+ let coeffs = values.filter(x => x.variables.length === 0);
2686
+ if (coeffs.length > 0) {
2687
+ this._monoms.push(coeffs.reduce((a, b) => a.add(b)));
2688
+ }
2689
+ // Build the new monoms
2690
+ for (let letter of vars) {
2691
+ // Monom with same letters, but might be of different degrees
2692
+ let M = values.filter(x => x.hasLetter(letter));
2693
+ while (M.length > 0) {
2694
+ // Take the first element
2695
+ const m = M.shift(), degree = m.degree(letter);
2696
+ for (let a of M.filter(x => x.degree(letter).isEqual(degree))) {
2697
+ m.add(a);
2662
2698
  }
2699
+ this._monoms.push(m);
2700
+ // Make the new array.
2701
+ M = M.filter(x => x.degree(letter).isNotEqual(degree));
2663
2702
  }
2703
+ // reduce the monom
2664
2704
  }
2665
2705
  // Remove all null monoms
2666
2706
  this._monoms = this._monoms.filter((m) => {
@@ -2673,14 +2713,14 @@ class Polynom {
2673
2713
  if (this.length === 0) {
2674
2714
  return new Polynom().zero();
2675
2715
  }
2676
- return this;
2716
+ return this.reorder();
2677
2717
  };
2678
2718
  this.reorder = (letter = 'x') => {
2679
2719
  // TODO: Must handle multiple setLetter reorder system
2680
2720
  this._monoms.sort(function (a, b) {
2681
2721
  return b.degree(letter).clone().subtract(a.degree(letter)).value;
2682
2722
  });
2683
- return this.reduce();
2723
+ return this;
2684
2724
  };
2685
2725
  this.degree = (letter) => {
2686
2726
  let d = new fraction_1.Fraction().zero();
@@ -2779,7 +2819,7 @@ class Polynom {
2779
2819
  // 2x^3+6x^2 => 2x^2
2780
2820
  let M = P.commonMonom();
2781
2821
  // If the polynom starts with a negative monom, factorize it.
2782
- if (P.monomByDegree().coefficient.isStrictlyNegative() && M.coefficient.isStrictlyPositive()) {
2822
+ if (P.monomByDegree().coefficient.isStrictlyNegative() && M.coefficient.isStrictlyPositive() && !M.isOne()) {
2783
2823
  M.opposed();
2784
2824
  }
2785
2825
  if (!M.isOne()) {
@@ -3054,7 +3094,7 @@ class Polynom {
3054
3094
  if (stack.length === 1) {
3055
3095
  this.add(stack[0]);
3056
3096
  }
3057
- return this;
3097
+ return this.reorder();
3058
3098
  };
3059
3099
  this.multiplyByPolynom = (P) => {
3060
3100
  const M = [];