pimath 0.0.60 → 0.0.63

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.
Files changed (58) hide show
  1. package/.eslintrc.js +23 -23
  2. package/.idea/misc.xml +5 -0
  3. package/.idea/php.xml +1 -1
  4. package/.idea/shelf/Uncommitted_changes_before_Update_at_17_04_2022_12_40_[Changes]/shelved.patch +21 -0
  5. package/.idea/shelf/Uncommitted_changes_before_Update_at_17_04_2022_12_40__Changes_.xml +4 -0
  6. package/dist/pi.js +638 -351
  7. package/dist/pi.js.map +1 -1
  8. package/dist/pi.min.js +1 -1
  9. package/dist/pi.min.js.map +1 -1
  10. package/docs/assets/highlight.css +78 -78
  11. package/docs/assets/main.js +52 -52
  12. package/docs/assets/style.css +1413 -1413
  13. package/docs/classes/Logicalset.Logicalset-1.html +4 -4
  14. package/docs/classes/Polynom.Rational.html +3 -3
  15. package/docs/classes/algebra_equation.Equation.html +25 -25
  16. package/docs/classes/algebra_monom.Monom.html +113 -113
  17. package/docs/classes/algebra_polynom.Polynom.html +29 -29
  18. package/docs/classes/coefficients_fraction.Fraction.html +18 -18
  19. package/docs/classes/coefficients_nthroot.NthRoot.html +2 -2
  20. package/docs/classes/geometry_circle.Circle.html +2 -2
  21. package/docs/classes/geometry_line.Line.html +2 -2
  22. package/docs/classes/geometry_triangle.Triangle.html +16 -16
  23. package/docs/classes/numeric.Numeric.html +13 -13
  24. package/docs/classes/shutingyard.Shutingyard.html +17 -17
  25. package/docs/index.html +10 -10
  26. package/docs/interfaces/algebra_equation.ISolution.html +2 -2
  27. package/docs/modules/Logicalset.html +2 -2
  28. package/docs/modules/Polynom.html +2 -2
  29. package/docs/modules/Vector.html +2 -2
  30. package/esm/maths/algebra/linearSystem.js +0 -1
  31. package/esm/maths/algebra/linearSystem.js.map +1 -1
  32. package/esm/maths/algebra/monom.d.ts +1 -0
  33. package/esm/maths/algebra/monom.js +18 -0
  34. package/esm/maths/algebra/monom.js.map +1 -1
  35. package/esm/maths/algebra/polynom.d.ts +30 -11
  36. package/esm/maths/algebra/polynom.js +144 -258
  37. package/esm/maths/algebra/polynom.js.map +1 -1
  38. package/esm/maths/algebra/rational.d.ts +12 -15
  39. package/esm/maths/algebra/rational.js +14 -103
  40. package/esm/maths/algebra/rational.js.map +1 -1
  41. package/esm/maths/algebra/study/rationalStudy.d.ts +33 -0
  42. package/esm/maths/algebra/study/rationalStudy.js +174 -0
  43. package/esm/maths/algebra/study/rationalStudy.js.map +1 -0
  44. package/esm/maths/algebra/study.d.ts +133 -0
  45. package/esm/maths/algebra/study.js +275 -0
  46. package/esm/maths/algebra/study.js.map +1 -0
  47. package/package.json +1 -1
  48. package/src/maths/algebra/linearSystem.ts +0 -1
  49. package/src/maths/algebra/monom.ts +19 -0
  50. package/src/maths/algebra/polynom.ts +185 -278
  51. package/src/maths/algebra/rational.ts +24 -132
  52. package/src/maths/algebra/study/rationalStudy.ts +208 -0
  53. package/src/maths/algebra/study.ts +384 -0
  54. package/tests/algebra/monom.test.ts +2 -5
  55. package/tests/algebra/polynom.test.ts +2 -3
  56. package/tests/algebra/rationnal.test.ts +0 -43
  57. package/tests/algebra/study.test.ts +18 -0
  58. package/tests/numexp.test.ts +1 -1
@@ -22,6 +22,11 @@ class Polynom {
22
22
  * @param values
23
23
  */
24
24
  constructor(polynomString, ...values) {
25
+ this.mark_as_dirty = () => {
26
+ this.dirty_factors = true;
27
+ this.dirty_zeroes = true;
28
+ this.euclidianCache = {};
29
+ };
25
30
  this.addToken = (stack, element) => {
26
31
  switch (element.tokenType) {
27
32
  case shutingyard_1.ShutingyardType.COEFFICIENT:
@@ -109,6 +114,7 @@ class Polynom {
109
114
  // Reset the main variables.
110
115
  this._monoms = [];
111
116
  this._factors = [];
117
+ this.mark_as_dirty();
112
118
  if (typeof inputStr === 'string') {
113
119
  return this._parseString(inputStr, ...values);
114
120
  }
@@ -149,88 +155,32 @@ class Polynom {
149
155
  this._monoms = [];
150
156
  this._monoms.push(new monom_1.Monom().zero());
151
157
  this._rawString = '0';
158
+ this.mark_as_dirty();
152
159
  return this;
153
160
  };
154
161
  this.one = () => {
155
162
  this._monoms = [];
156
163
  this._monoms.push(new monom_1.Monom().one());
157
164
  this._rawString = '1';
165
+ this.mark_as_dirty();
158
166
  return this;
159
167
  };
160
168
  this.empty = () => {
161
169
  this._monoms = [];
162
170
  this._rawString = '';
171
+ this.mark_as_dirty();
163
172
  return this;
164
173
  };
165
174
  // ------------------------------------------
166
175
  this.opposed = () => {
167
176
  this._monoms = this._monoms.map(m => m.opposed());
177
+ this.mark_as_dirty();
168
178
  return this;
169
179
  };
170
- // // -----------------------------------------------
171
- // // Polynom generators and randomizers
172
- // // -----------------------------------------------
173
- // random(config?: randomPolynomConfig) {
174
- // return Random.polynom(config);
175
- // }
176
- //
177
- // private _randomizeDefaults: { [key: string]: number | string | boolean } = {
178
- // degree: 2,
179
- // unit: true,
180
- // fractions: false,
181
- // factorable: false,
182
- // letters: 'x',
183
- // allowNullMonom: false,
184
- // numberOfMonoms: false
185
- // };
186
- // get randomizeDefaults(): { [key: string]: number | string | boolean } {
187
- // return this._randomizeDefaults;
188
- // }
189
- //
190
- // set randomizeDefaults(value) {
191
- // this._randomizeDefaults = value;
192
- // }
193
- //
194
- // randomize = (config: { [key: string]: number | string | boolean }): Polynom => {
195
- // let P = new Polynom();
196
- //
197
- // // Check the config file and use the default values.
198
- // if (config === undefined) {
199
- // config = {};
200
- // }
201
- // for (let k in this._randomizeDefaults) {
202
- // if (config[k] === undefined) {
203
- // config[k] = this._randomizeDefaults[k];
204
- // }
205
- // }
206
- //
207
- // // TODO: Build a more robust randomize function
208
- // return P;
209
- // }
210
- //
211
- // rndFactorable = (degree: number = 2, unit: boolean | number = false, letters: string = 'x'): Polynom => {
212
- // // TODO: Make rndFactorable polynom generator more user friendly
213
- // this._factors = [];
214
- // for (let i = 0; i < degree; i++) {
215
- // let factorUnit = unit === true || i >= unit,
216
- // p = Random.polynom({
217
- // degree: 1,
218
- // unit: factorUnit,
219
- // fraction: false,
220
- // letters
221
- // });
222
- // this._factors.push(p);
223
- // }
224
- //
225
- // this.empty().monoms = this._factors[0].monoms;
226
- // for (let i = 1; i < this._factors.length; i++) {
227
- // this.multiply(this._factors[i]);
228
- // }
229
- // return this;
230
- // };
231
180
  // ------------------------------------------
232
181
  // Mathematical operations
233
182
  this.add = (...values) => {
183
+ this.mark_as_dirty();
234
184
  for (let value of values) {
235
185
  if (value instanceof Polynom) {
236
186
  this._monoms = this._monoms.concat(value.monoms);
@@ -248,6 +198,7 @@ class Polynom {
248
198
  return this.reduce();
249
199
  };
250
200
  this.subtract = (...values) => {
201
+ this.mark_as_dirty();
251
202
  for (let value of values) {
252
203
  if (value instanceof Polynom) {
253
204
  this._monoms = this._monoms.concat(value.clone().opposed().monoms);
@@ -265,6 +216,7 @@ class Polynom {
265
216
  return this.reduce();
266
217
  };
267
218
  this.multiply = (value) => {
219
+ this.mark_as_dirty();
268
220
  if (value instanceof Polynom) {
269
221
  return this.multiplyByPolynom(value);
270
222
  }
@@ -286,6 +238,9 @@ class Polynom {
286
238
  * returns {quotient: Polynom, reminder: Polynom}
287
239
  */
288
240
  this.euclidian = (P) => {
241
+ if (this.euclidianCache[P.tex] !== undefined) {
242
+ return this.euclidianCache[P.tex];
243
+ }
289
244
  const letter = P.variables[0];
290
245
  const quotient = new Polynom().zero();
291
246
  const reminder = this.clone().reorder(letter);
@@ -319,6 +274,7 @@ class Polynom {
319
274
  return { quotient, reminder };
320
275
  };
321
276
  this.divide = (value) => {
277
+ this.mark_as_dirty();
322
278
  if (value instanceof fraction_1.Fraction) {
323
279
  return this.divideByFraction(value);
324
280
  }
@@ -332,6 +288,7 @@ class Polynom {
332
288
  }
333
289
  };
334
290
  this.pow = (nb) => {
291
+ this.mark_as_dirty();
335
292
  if (!Number.isSafeInteger(nb)) {
336
293
  return this.zero();
337
294
  }
@@ -532,6 +489,7 @@ class Polynom {
532
489
  * @param P
533
490
  */
534
491
  this.replaceBy = (letter, P) => {
492
+ this.mark_as_dirty();
535
493
  let pow;
536
494
  const resultPolynom = new Polynom().zero();
537
495
  for (const m of this.monoms) {
@@ -591,146 +549,109 @@ class Polynom {
591
549
  * @param maxValue Defines the greatest value to search to (default is 20).
592
550
  */
593
551
  this.factorize = (letter) => {
594
- let factors = [];
595
- // Extract the common monom
596
- let P = this.clone().reorder(), M = P.commonMonom(), tempPolynom;
597
- // It has a common monom.
598
- if (!M.isOne()) {
599
- tempPolynom = new Polynom(M);
600
- factors = [tempPolynom.clone()];
601
- P = P.euclidian(tempPolynom).quotient;
602
- }
603
- let securityLoop = P.degree().clone().multiply(2).value;
604
- let result;
605
- // securityLoop = 0
606
- while (securityLoop >= 0) {
607
- securityLoop--;
608
- if (P.monoms.length < 2) {
609
- if (!P.isOne()) {
552
+ if (this.dirty_factors) {
553
+ let factors = [];
554
+ let P = this.clone().reorder();
555
+ // Extract the common monom
556
+ // 2x^3+6x^2 => 2x^2
557
+ let M = P.commonMonom();
558
+ if (!M.isOne()) {
559
+ let tempPolynom = new Polynom(M);
560
+ factors = [tempPolynom.clone()];
561
+ P = P.euclidian(tempPolynom).quotient;
562
+ }
563
+ // Main loop
564
+ let securityLoop = P.degree().clone().multiply(2).value, maxDegree = 1;
565
+ while (securityLoop >= 0) {
566
+ securityLoop--;
567
+ if (P.monoms.length < 2) {
568
+ // The polynom has only one monom => 7x^2
569
+ // No need to continue.
570
+ if (!P.isOne()) {
571
+ factors.push(P.clone());
572
+ P.one();
573
+ }
574
+ break;
575
+ }
576
+ else if (P.degree(letter).isOne()) {
577
+ // The polynom is a first degree polynom => 3x-5
578
+ // No need to continue
610
579
  factors.push(P.clone());
611
580
  P.one();
581
+ break;
582
+ }
583
+ else {
584
+ // Create the list of all "potential" polynom dividers.
585
+ let allDividers = this._getAllPotentialFactors(P, maxDegree, letter);
586
+ maxDegree = P.degree(letter).value;
587
+ // Actually: 100ms
588
+ while (allDividers.length > 0) {
589
+ let div = allDividers[0];
590
+ if (!P.isDividableBy(div)) {
591
+ // Not dividable. Remove it from the list
592
+ allDividers.shift();
593
+ }
594
+ else {
595
+ // It's dividable - so make the division
596
+ let result = P.euclidian(div);
597
+ // Add the factor
598
+ factors.push(div);
599
+ // As it's dividable, get the quotient.
600
+ P = result.quotient.clone();
601
+ // filter all dividers that are no more suitable.
602
+ allDividers = allDividers.filter(x => {
603
+ let pX = P.monoms[0], pC = P.monoms[P.monoms.length - 1], dX = x.monoms[0],
604
+ dC = x.monoms[x.monoms.length - 1];
605
+ // Check last item (degree zero)
606
+ if (!pC.isDivisible(dC)) {
607
+ return false;
608
+ }
609
+ // Check the first item (degree max)
610
+ if (!pX.isDivisible(dX)) {
611
+ return false;
612
+ }
613
+ return true;
614
+ });
615
+ }
616
+ }
612
617
  }
613
- break;
614
618
  }
615
- else if (P.degree(letter).isOne()) {
619
+ // Maybe there is still something in the Polynom (not everything was possible to factorize)
620
+ if (!P.isOne()) {
616
621
  factors.push(P.clone());
617
- P.one();
618
- break;
622
+ }
623
+ // Save the factors
624
+ this._factors = factors;
625
+ // The factors list is no more dirty
626
+ this.dirty_factors = false;
627
+ }
628
+ return this._factors;
629
+ };
630
+ this.isDividableBy = (div) => {
631
+ // Quick evaluation.
632
+ if (div.degree().isOne()) {
633
+ let zero = div.getZeroes()[0];
634
+ if (zero.exact instanceof fraction_1.Fraction) {
635
+ return this.evaluate(zero.exact).isZero();
619
636
  }
620
637
  else {
621
- // Get the first and last monom and build all their dividers.
622
- // let m1 = P.monoms[0].dividers,
623
- // m2 = P.monoms[P.monoms.length - 1].dividers
624
- // Create the list of all "potential" polynom dividers.
625
- let allDividers = this._getAllPotentialFactors(P, letter);
626
- allDividers.every(div => {
627
- result = P.euclidian(div);
628
- if (result.reminder.isZero()) {
629
- P = result.quotient.clone();
630
- factors.push(div);
631
- return false;
632
- }
633
- return true;
634
- });
638
+ return false;
635
639
  }
636
640
  }
637
- if (!P.isOne()) {
638
- factors.push(P.clone());
641
+ else {
642
+ this.euclidianCache[div.tex] = this.euclidian(div);
643
+ return this.euclidianCache[div.tex].reminder.isZero();
639
644
  }
640
- this.factors = factors;
641
- return factors;
642
645
  };
643
646
  // TODO: get zeroes for more than first degree and for more than natural degrees
644
647
  this.getZeroes = () => {
645
- let equ = new equation_1.Equation(this.clone(), 0);
646
- equ.solve();
647
- return equ.solutions;
648
- //
649
- // const Z: Fraction[] = [];
650
- //
651
- // // ISolution: {tex: string, value: number, exact: boolean|Fraction|...}
652
- //
653
- // switch (this.degree().value) {
654
- // case 0:
655
- // if (this._monoms[0].coefficient.value === 0) {
656
- // return [{
657
- // tex: '\\mathbb{R}',
658
- // value: NaN,
659
- // exact: false
660
- // }];
661
- // } else {
662
- // return [{
663
- // tex: '\\varnothing',
664
- // value: NaN,
665
- // exact: false
666
- // }];
667
- // }
668
- // case 1:
669
- // // There is only one monoms,
670
- // if (this._monoms.length === 1) {
671
- // return [{
672
- // tex: '0',
673
- // value: 0,
674
- // exact: new Fraction().zero()
675
- // }];
676
- // } else {
677
- // const P = this.clone().reduce().reorder();
678
- // const coeff = P.monoms[1].coefficient.opposed().divide(P.monoms[0].coefficient)
679
- // return [{
680
- // tex: coeff.tex,
681
- // value: coeff.value,
682
- // exact: coeff
683
- // }];
684
- // }
685
- // // TODO: Determine the zeros of an equation of second degree.
686
- // //case 2:
687
- // default:
688
- // // Make sure the polynom is factorized.
689
- // if (this._factors.length === 0) {
690
- // this.factorize()
691
- // }
692
- //
693
- // let zeroes:Fraction[] = [], zeroesAsTex = [];
694
- // for (let P of this._factors) {
695
- // if (P.degree().greater(2)) {
696
- // // TODO: get zeroes of polynom with a degree greater than 2.
697
- //
698
- // } else if (P.degree().value === 2) {
699
- // let A = P.monomByDegree(2).coefficient,
700
- // B = P.monomByDegree(1).coefficient,
701
- // C = P.monomByDegree(0).coefficient,
702
- // D = B.clone().pow(2).subtract(A.clone().multiply(C).multiply(4));
703
- //
704
- // if (D.value > 0) {
705
- // /*console.log('Two zeroes for ', P.tex); */
706
- // let x1 = (-(B.value) + Math.sqrt(D.value)) / (2 * A.value),
707
- // x2 = (-(B.value) - Math.sqrt(D.value)) / (2 * A.value);
708
- //
709
- // zeroes.push(new Fraction(x1.toFixed(3)).reduce());
710
- // zeroes.push(new Fraction(x2.toFixed(3)).reduce());
711
- // } else if (D.value === 0) {
712
- // /*console.log('One zero for ', P.tex); */
713
- // } else {
714
- // console.log('No zero for ', P.tex);
715
- // }
716
- // } else {
717
- // for (let z of P.getZeroes()) {
718
- // // Check if the zero is already in the list.
719
- // // if (z === false || z === true) {
720
- // // continue;
721
- // // }
722
- // if (zeroesAsTex.indexOf(z.frac) === -1) {
723
- // zeroes.push(z);
724
- // zeroesAsTex.push(z.frac);
725
- // }
726
- // }
727
- // }
728
- // }
729
- //
730
- //
731
- // return zeroes;
732
- // }
733
- // return Z;
648
+ if (this.dirty_zeroes) {
649
+ let equ = new equation_1.Equation(this.clone(), 0);
650
+ equ.solve();
651
+ this._zeroes = equ.solutions;
652
+ this.dirty_zeroes = false;
653
+ }
654
+ return this._zeroes;
734
655
  };
735
656
  // TODO: analyse the next functions to determine if they are useful or not...
736
657
  this.monomByDegree = (degree, letter) => {
@@ -845,16 +766,19 @@ class Polynom {
845
766
  // Any other cases
846
767
  return (new fraction_1.Fraction()).zero();
847
768
  };
848
- this._getAllPotentialFactors = (P, letter) => {
769
+ this._getAllPotentialFactors = (P, maxDegree, letter) => {
849
770
  let m1 = P.monoms[0].dividers, m2 = P.monoms[P.monoms.length - 1].dividers;
850
771
  let allDividers = [];
851
772
  m1.forEach(m1d => {
852
- m2.forEach(m2d => {
853
- if (m1d.degree(letter).isNotEqual(m2d.degree(letter))) {
854
- allDividers.push(new Polynom(m1d, m2d));
855
- allDividers.push(new Polynom(m1d, m2d.clone().opposed()));
856
- }
857
- });
773
+ // Get only polynom that has a degree less than a specific value
774
+ if (m1d.degree(letter).leq(maxDegree)) {
775
+ m2.forEach(m2d => {
776
+ if (m1d.degree(letter).isNotEqual(m2d.degree(letter))) {
777
+ allDividers.push(new Polynom(m1d, m2d));
778
+ allDividers.push(new Polynom(m1d, m2d.clone().opposed()));
779
+ }
780
+ });
781
+ }
858
782
  });
859
783
  return allDividers;
860
784
  };
@@ -906,41 +830,6 @@ class Polynom {
906
830
  this.add(stack[0]);
907
831
  }
908
832
  return this;
909
- /**
910
- let m1: Polynom;
911
- let m2: Polynom;
912
-
913
- let stack: Polynom[] = [],
914
- previousToken: string = null,
915
- tempPolynom
916
-
917
- for (const element of rpn) {
918
- if (element.tokenType === 'coefficient' || element.tokenType === 'variable') {
919
- tempPolynom = new Polynom().zero();
920
- tempPolynom.monoms = [new Monom(element.token)]
921
- stack.push(tempPolynom.clone())
922
- } else if (element.tokenType === 'operation') {
923
- m2 = (stack.pop()) || new Polynom().zero();
924
- m1 = (stack.pop()) || new Polynom().zero();
925
- switch (element.token) {
926
- case '+':
927
- stack.push(m1.add(m2))
928
- break;
929
- case '-':
930
- stack.push(m1.subtract(m2))
931
- break;
932
- case '*':
933
- stack.push(m1.multiply(m2))
934
- break;
935
- case '^':
936
- stack.push(m1.pow(+previousToken))
937
- }
938
- }
939
- previousToken = element.token;
940
- }
941
-
942
- this._monoms = stack[0].monoms;
943
- return this;*/
944
833
  };
945
834
  this.multiplyByPolynom = (P) => {
946
835
  const M = [];
@@ -1065,33 +954,6 @@ class Polynom {
1065
954
  }
1066
955
  }
1067
956
  return [this.clone()];
1068
- //
1069
- // console.log(a.tex, b.tex, c.tex)
1070
- // if (a.isSquare() && c.isSquare()) {
1071
- // console.log('A C squares')
1072
- // if (a.clone().sqrt().multiply(c.clone().sqrt()).multiplyByNumber(2).isSameAs(b)) {
1073
- // console.log('HERE')
1074
- // if (a.coefficient.sign() === b.coefficient.sign()) {
1075
- // return []
1076
- // }else{
1077
- // return []
1078
- // }
1079
- // }
1080
- // } else if(a.isLiteralSquare() && c.isLiteralSquare()) {
1081
- // console.log('A C litteral SQUARES')
1082
- // // Check that the middle element is the product of a and c.
1083
- //
1084
- // if(b.clone().pow(2).isSameAs(a.clone().multiply(c))){
1085
- // console.log('SAME')
1086
- //
1087
- // }else{
1088
- // console.log('NOT SAME')
1089
- // }
1090
- //
1091
- // return [this.clone()]
1092
- // } else {
1093
- // console.log('NOT SQUARES AT ALL !!!!')
1094
- // }
1095
957
  }
1096
958
  };
1097
959
  this._factorizeByGroups = () => {
@@ -1100,11 +962,31 @@ class Polynom {
1100
962
  };
1101
963
  this._monoms = [];
1102
964
  this._factors = [];
965
+ this.mark_as_dirty();
1103
966
  if (polynomString !== undefined) {
1104
967
  this.parse(polynomString, ...values);
1105
968
  }
1106
969
  return this;
1107
970
  }
971
+ get euclidianCache() {
972
+ return this._euclidianCache;
973
+ }
974
+ set euclidianCache(value) {
975
+ this._euclidianCache = value;
976
+ }
977
+ get dirty_zeroes() {
978
+ return this._dirty_zeroes;
979
+ }
980
+ set dirty_zeroes(value) {
981
+ this._dirty_zeroes = value;
982
+ }
983
+ // ------------------------------------------
984
+ get dirty_factors() {
985
+ return this._dirty_factors;
986
+ }
987
+ set dirty_factors(value) {
988
+ this._dirty_factors = value;
989
+ }
1108
990
  // ------------------------------------------
1109
991
  get monoms() {
1110
992
  return this._monoms;
@@ -1112,10 +994,14 @@ class Polynom {
1112
994
  set monoms(M) {
1113
995
  this._monoms = M;
1114
996
  }
997
+ get zeroes() {
998
+ return this.getZeroes();
999
+ }
1115
1000
  get factors() {
1116
- return this._factors;
1001
+ return this.factorize();
1117
1002
  }
1118
1003
  set factors(value) {
1004
+ this.mark_as_dirty();
1119
1005
  this._factors = value;
1120
1006
  }
1121
1007
  get texString() {