pimath 0.0.52 → 0.0.55

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 (37) hide show
  1. package/dist/pi.js +153 -144
  2. package/dist/pi.js.map +1 -1
  3. package/dist/pi.min.js +1 -1
  4. package/dist/pi.min.js.map +1 -1
  5. package/esm/maths/algebra/equation.js +10 -10
  6. package/esm/maths/algebra/equation.js.map +10 -1
  7. package/esm/maths/algebra/linearSystem.js +1 -1
  8. package/esm/maths/algebra/linearSystem.js.map +10 -1
  9. package/esm/maths/algebra/monom.d.ts +4 -3
  10. package/esm/maths/algebra/monom.js +49 -55
  11. package/esm/maths/algebra/monom.js.map +10 -1
  12. package/esm/maths/algebra/polynom.d.ts +6 -5
  13. package/esm/maths/algebra/polynom.js +77 -81
  14. package/esm/maths/algebra/polynom.js.map +10 -1
  15. package/esm/maths/algebra/rational.js +3 -5
  16. package/esm/maths/algebra/rational.js.map +10 -1
  17. package/esm/maths/expressions/polynomexp.bkp.js +2 -3
  18. package/esm/maths/expressions/polynomexp.bkp.js.map +10 -1
  19. package/esm/maths/expressions/polynomexp.js +3 -3
  20. package/esm/maths/expressions/polynomexp.js.map +10 -1
  21. package/esm/maths/geometry/point.js +1 -1
  22. package/esm/maths/geometry/point.js.map +10 -1
  23. package/esm/maths/randomization/random.js +9 -1
  24. package/esm/maths/randomization/random.js.map +10 -1
  25. package/package.json +1 -1
  26. package/src/maths/algebra/equation.ts +7 -7
  27. package/src/maths/algebra/linearSystem.ts +1 -1
  28. package/src/maths/algebra/monom.ts +5 -6
  29. package/src/maths/algebra/polynom.ts +16 -10
  30. package/src/maths/algebra/rational.ts +4 -3
  31. package/src/maths/expressions/polynomexp.bkp.ts +1 -2
  32. package/src/maths/expressions/polynomexp.ts +1 -1
  33. package/src/maths/geometry/point.ts +1 -1
  34. package/tests/algebra/equation.test.ts +2 -4
  35. package/tests/algebra/monom.test.ts +1 -1
  36. package/tests/algebra/polynom.test.ts +7 -3
  37. package/tests/algebra/rationnal.test.ts +34 -1
package/dist/pi.js CHANGED
@@ -497,12 +497,12 @@ class Equation {
497
497
  else {
498
498
  this._solutions = [
499
499
  {
500
- tex: `\\dfrac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
500
+ tex: `\\frac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
501
501
  value: realX1,
502
502
  exact: false
503
503
  },
504
504
  {
505
- tex: `\\dfrac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
505
+ tex: `\\frac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
506
506
  value: realX2,
507
507
  exact: false
508
508
  },
@@ -527,12 +527,12 @@ class Equation {
527
527
  else {
528
528
  this._solutions = [
529
529
  {
530
- tex: `\\dfrac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
530
+ tex: `\\frac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
531
531
  value: realX1,
532
532
  exact: false
533
533
  },
534
534
  {
535
- tex: `\\dfrac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
535
+ tex: `\\frac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
536
536
  value: realX2,
537
537
  exact: false
538
538
  },
@@ -545,12 +545,12 @@ class Equation {
545
545
  const S1 = new fraction_1.Fraction(-b - nthDelta.coefficient, 2 * a).reduce(), S2 = new fraction_1.Fraction(-b + nthDelta.coefficient, 2 * a).reduce();
546
546
  this._solutions = [
547
547
  {
548
- tex: S1.dfrac,
548
+ tex: S1.frac,
549
549
  value: realX1,
550
550
  exact: S1
551
551
  },
552
552
  {
553
- tex: S2.dfrac,
553
+ tex: S2.frac,
554
554
  value: realX2,
555
555
  exact: S2
556
556
  }
@@ -561,7 +561,7 @@ class Equation {
561
561
  else if (delta === 0) {
562
562
  const sol = new fraction_1.Fraction(-b, 2 * a).reduce();
563
563
  this._solutions = [{
564
- tex: sol.dfrac,
564
+ tex: sol.frac,
565
565
  value: sol.value,
566
566
  exact: sol
567
567
  }];
@@ -1017,7 +1017,7 @@ class LinearSystem {
1017
1017
  console.log(`Undefined (letter ${letter})`);
1018
1018
  return;
1019
1019
  }
1020
- tex.push(this._solutions[letter].value.dfrac);
1020
+ tex.push(this._solutions[letter].value.frac);
1021
1021
  }
1022
1022
  return `(${tex.join(';')})`;
1023
1023
  }
@@ -1276,6 +1276,50 @@ class Monom {
1276
1276
  }
1277
1277
  return this;
1278
1278
  };
1279
+ this.addToken = (stack, element) => {
1280
+ let q1, q2, m, letter, pow;
1281
+ if (element.tokenType === shutingyard_1.ShutingyardType.COEFFICIENT) {
1282
+ stack.push(new Monom(new fraction_1.Fraction(element.token)));
1283
+ }
1284
+ else if (element.tokenType === shutingyard_1.ShutingyardType.VARIABLE) {
1285
+ let M = new Monom().one();
1286
+ M.setLetter(element.token, 1);
1287
+ stack.push(M.clone());
1288
+ }
1289
+ else if (element.tokenType === shutingyard_1.ShutingyardType.OPERATION) {
1290
+ switch (element.token) {
1291
+ case '-':
1292
+ // this should only happen for negative powers or for negative coefficient.
1293
+ q2 = (stack.pop()) || new Monom().zero();
1294
+ q1 = (stack.pop()) || new Monom().zero();
1295
+ stack.push(q1.subtract(q2));
1296
+ break;
1297
+ case '*':
1298
+ // Get the last element in the stack
1299
+ q2 = (stack.pop()) || new Monom().one();
1300
+ q1 = (stack.pop()) || new Monom().one();
1301
+ stack.push(q1.multiply(q2));
1302
+ break;
1303
+ case '/':
1304
+ // Get the last element in the stack
1305
+ q2 = (stack.pop()) || new Monom().one();
1306
+ q1 = (stack.pop()) || new Monom().one();
1307
+ stack.push(q1.divide(q2));
1308
+ break;
1309
+ case '^':
1310
+ // get the two last elements in the stack
1311
+ pow = (stack.pop().coefficient) || new fraction_1.Fraction().one();
1312
+ m = (stack.pop()) || new Monom().one();
1313
+ letter = m.variables[0];
1314
+ if (letter !== undefined) {
1315
+ m.setLetter(letter, pow);
1316
+ }
1317
+ stack.push(m);
1318
+ // this.multiply(m.clone())
1319
+ break;
1320
+ }
1321
+ }
1322
+ };
1279
1323
  this._shutingYardToReducedMonom = (inputStr) => {
1280
1324
  // Get the RPN array of the current expression
1281
1325
  const SY = new shutingyard_1.Shutingyard().parse(inputStr);
@@ -1299,7 +1343,7 @@ class Monom {
1299
1343
  else {
1300
1344
  // Reset the monom
1301
1345
  for (const element of rpn) {
1302
- Monom.addToken(stack, element);
1346
+ this.addToken(stack, element);
1303
1347
  }
1304
1348
  }
1305
1349
  this.one();
@@ -1943,7 +1987,7 @@ class Monom {
1943
1987
  if (L === '') {
1944
1988
  // No setLetter - means it's only a number !
1945
1989
  if (this._coefficient.value != 0) {
1946
- return `${this._coefficient.dfrac}`;
1990
+ return `${this._coefficient.frac}`;
1947
1991
  }
1948
1992
  else {
1949
1993
  return '0';
@@ -1960,7 +2004,7 @@ class Monom {
1960
2004
  return '0';
1961
2005
  }
1962
2006
  else {
1963
- return `${this._coefficient.dfrac}${L}`;
2007
+ return `${this._coefficient.frac}${L}`;
1964
2008
  }
1965
2009
  }
1966
2010
  }
@@ -1978,50 +2022,6 @@ class Monom {
1978
2022
  }
1979
2023
  }
1980
2024
  exports.Monom = Monom;
1981
- Monom.addToken = (stack, element) => {
1982
- let q1, q2, m, letter, pow;
1983
- if (element.tokenType === shutingyard_1.ShutingyardType.COEFFICIENT) {
1984
- stack.push(new Monom(new fraction_1.Fraction(element.token)));
1985
- }
1986
- else if (element.tokenType === shutingyard_1.ShutingyardType.VARIABLE) {
1987
- let M = new Monom().one();
1988
- M.setLetter(element.token, 1);
1989
- stack.push(M.clone());
1990
- }
1991
- else if (element.tokenType === shutingyard_1.ShutingyardType.OPERATION) {
1992
- switch (element.token) {
1993
- case '-':
1994
- // this should only happen for negative powers or for negative coefficient.
1995
- q2 = (stack.pop()) || new Monom().zero();
1996
- q1 = (stack.pop()) || new Monom().zero();
1997
- stack.push(q1.subtract(q2));
1998
- break;
1999
- case '*':
2000
- // Get the last element in the stack
2001
- q2 = (stack.pop()) || new Monom().one();
2002
- q1 = (stack.pop()) || new Monom().one();
2003
- stack.push(q1.multiply(q2));
2004
- break;
2005
- case '/':
2006
- // Get the last element in the stack
2007
- q2 = (stack.pop()) || new Monom().one();
2008
- q1 = (stack.pop()) || new Monom().one();
2009
- stack.push(q1.divide(q2));
2010
- break;
2011
- case '^':
2012
- // get the two last elements in the stack
2013
- pow = (stack.pop().coefficient) || new fraction_1.Fraction().one();
2014
- m = (stack.pop()) || new Monom().one();
2015
- letter = m.variables[0];
2016
- if (letter !== undefined) {
2017
- m.setLetter(letter, pow);
2018
- }
2019
- stack.push(m);
2020
- // this.multiply(m.clone())
2021
- break;
2022
- }
2023
- }
2024
- };
2025
2025
  // ----------------------------------------
2026
2026
  // Static functions
2027
2027
  // ----------------------------------------
@@ -2100,6 +2100,81 @@ class Polynom {
2100
2100
  * @param values
2101
2101
  */
2102
2102
  constructor(polynomString, ...values) {
2103
+ this.addToken = (stack, element) => {
2104
+ switch (element.tokenType) {
2105
+ case shutingyard_1.ShutingyardType.COEFFICIENT:
2106
+ stack.push(new Polynom(element.token));
2107
+ break;
2108
+ case shutingyard_1.ShutingyardType.VARIABLE:
2109
+ stack.push(new Polynom().add(new monom_1.Monom(element.token)));
2110
+ break;
2111
+ case shutingyard_1.ShutingyardType.CONSTANT:
2112
+ // TODO: add constant support to Polynom parsing.
2113
+ console.log('Actually, not supported - will be added later !');
2114
+ break;
2115
+ case shutingyard_1.ShutingyardType.OPERATION:
2116
+ if (stack.length >= 2) {
2117
+ const b = stack.pop(), a = stack.pop();
2118
+ if (element.token === '+') {
2119
+ stack.push(a.add(b));
2120
+ }
2121
+ else if (element.token === '-') {
2122
+ stack.push(a.subtract(b));
2123
+ }
2124
+ else if (element.token === '*') {
2125
+ stack.push(a.multiply(b));
2126
+ }
2127
+ else if (element.token === '/') {
2128
+ if (b.degree().isStrictlyPositive()) {
2129
+ console.log('divide by a polynom -> should create a rational polynom !');
2130
+ }
2131
+ else {
2132
+ stack.push(a.divide(b.monoms[0].coefficient));
2133
+ }
2134
+ }
2135
+ else if (element.token === '^') {
2136
+ if (b.degree().isStrictlyPositive()) {
2137
+ console.error('Cannot elevate a polynom with another polynom !');
2138
+ }
2139
+ else {
2140
+ if (b.monoms[0].coefficient.isRelative()) {
2141
+ // Integer power
2142
+ stack.push(a.pow(b.monoms[0].coefficient.value));
2143
+ }
2144
+ else {
2145
+ // Only allow power if the previous polynom is only a monom, without coefficient.
2146
+ if (a.monoms.length === 1 && a.monoms[0].coefficient.isOne()) {
2147
+ for (let letter in a.monoms[0].literal) {
2148
+ a.monoms[0].literal[letter].multiply(b.monoms[0].coefficient);
2149
+ }
2150
+ stack.push(a);
2151
+ }
2152
+ else {
2153
+ console.error('Cannot have power with fraction');
2154
+ }
2155
+ }
2156
+ }
2157
+ }
2158
+ }
2159
+ else {
2160
+ if (element.token === '-') {
2161
+ stack.push(stack.pop().opposed());
2162
+ }
2163
+ else {
2164
+ throw "Error parsing the polynom " + this._rawString;
2165
+ }
2166
+ }
2167
+ break;
2168
+ case shutingyard_1.ShutingyardType.MONOM:
2169
+ // Should never appear.
2170
+ console.error('The monom token should not appear here');
2171
+ break;
2172
+ case shutingyard_1.ShutingyardType.FUNCTION:
2173
+ // Should never appear.
2174
+ console.error('The function token should not appear here - might be introduced later.');
2175
+ break;
2176
+ }
2177
+ };
2103
2178
  // ------------------------------------------
2104
2179
  /**
2105
2180
  * Parse a string to a polynom.
@@ -2292,9 +2367,13 @@ class Polynom {
2292
2367
  const letter = P.variables[0];
2293
2368
  const quotient = new Polynom().zero();
2294
2369
  const reminder = this.clone().reorder(letter);
2295
- // There is no variable !
2370
+ // There is no variable - means it's a number
2296
2371
  if (P.variables.length === 0) {
2297
- return { quotient, reminder };
2372
+ let q = this.clone().divide(P);
2373
+ return {
2374
+ quotient: this.clone().divide(P),
2375
+ reminder: new Polynom().zero()
2376
+ };
2298
2377
  }
2299
2378
  // Get at least a letter
2300
2379
  const maxMP = P.monomByDegree(undefined, letter);
@@ -2324,6 +2403,11 @@ class Polynom {
2324
2403
  else if (typeof value === 'number' && Number.isSafeInteger(value)) {
2325
2404
  return this.divideByInteger(value);
2326
2405
  }
2406
+ else if (value instanceof Polynom) {
2407
+ if (value.monoms.length === 1 && value.variables.length === 0) {
2408
+ return this.divideByFraction(value.monoms[0].coefficient);
2409
+ }
2410
+ }
2327
2411
  };
2328
2412
  this.pow = (nb) => {
2329
2413
  if (!Number.isSafeInteger(nb)) {
@@ -2590,8 +2674,7 @@ class Polynom {
2590
2674
  let P = this.clone().reorder(), M = P.commonMonom(), tempPolynom;
2591
2675
  // It has a common monom.
2592
2676
  if (!M.isOne()) {
2593
- tempPolynom = new Polynom();
2594
- tempPolynom.monoms = [M];
2677
+ tempPolynom = new Polynom(M);
2595
2678
  factors = [tempPolynom.clone()];
2596
2679
  P = P.euclidian(tempPolynom).quotient;
2597
2680
  }
@@ -2886,7 +2969,7 @@ class Polynom {
2886
2969
  let stack = [], monom = new monom_1.Monom();
2887
2970
  // Loop through the
2888
2971
  for (const element of rpn) {
2889
- Polynom.addToken(stack, element);
2972
+ this.addToken(stack, element);
2890
2973
  }
2891
2974
  if (stack.length === 1) {
2892
2975
  this.add(stack[0]);
@@ -3215,82 +3298,6 @@ class Polynom {
3215
3298
  }
3216
3299
  }
3217
3300
  exports.Polynom = Polynom;
3218
- Polynom.addToken = (stack, element) => {
3219
- switch (element.tokenType) {
3220
- case shutingyard_1.ShutingyardType.COEFFICIENT:
3221
- stack.push(new Polynom(element.token));
3222
- break;
3223
- case shutingyard_1.ShutingyardType.VARIABLE:
3224
- stack.push(new Polynom().add(new monom_1.Monom(element.token)));
3225
- break;
3226
- case shutingyard_1.ShutingyardType.CONSTANT:
3227
- // TODO: add constant support to Polynom parsing.
3228
- console.log('Actually, not supported - will be added later !');
3229
- break;
3230
- case shutingyard_1.ShutingyardType.OPERATION:
3231
- if (stack.length >= 2) {
3232
- const b = stack.pop(), a = stack.pop();
3233
- if (element.token === '+') {
3234
- stack.push(a.add(b));
3235
- }
3236
- else if (element.token === '-') {
3237
- stack.push(a.subtract(b));
3238
- }
3239
- else if (element.token === '*') {
3240
- stack.push(a.multiply(b));
3241
- }
3242
- else if (element.token === '/') {
3243
- if (b.degree().isStrictlyPositive()) {
3244
- console.log('divide by a polynom -> should create a rational polynom !');
3245
- }
3246
- else {
3247
- stack.push(a.divide(b.monoms[0].coefficient));
3248
- }
3249
- }
3250
- else if (element.token === '^') {
3251
- if (b.degree().isStrictlyPositive()) {
3252
- console.error('Cannot elevate a polynom with another polynom !');
3253
- }
3254
- else {
3255
- if (b.monoms[0].coefficient.isRelative()) {
3256
- // Integer power
3257
- stack.push(a.pow(b.monoms[0].coefficient.value));
3258
- }
3259
- else {
3260
- // Only allow power if the previous polynom is only a monom, without coefficient.
3261
- if (a.monoms.length === 1 && a.monoms[0].coefficient.isOne()) {
3262
- for (let letter in a.monoms[0].literal) {
3263
- a.monoms[0].literal[letter].multiply(b.monoms[0].coefficient);
3264
- }
3265
- stack.push(a);
3266
- }
3267
- else {
3268
- console.error('Cannot have power with fraction');
3269
- }
3270
- }
3271
- }
3272
- }
3273
- }
3274
- else {
3275
- console.log('Stack size: ', stack.length);
3276
- if (element.token === '-') {
3277
- stack.push(stack.pop().opposed());
3278
- }
3279
- else {
3280
- console.log('While parsing, cannot apply ', element.token, 'to', stack[0].tex);
3281
- }
3282
- }
3283
- break;
3284
- case shutingyard_1.ShutingyardType.MONOM:
3285
- // Should never appear.
3286
- console.error('The monom token should not appear here');
3287
- break;
3288
- case shutingyard_1.ShutingyardType.FUNCTION:
3289
- // Should never appear.
3290
- console.log('The function token should not appear here - might be introduced later.');
3291
- break;
3292
- }
3293
- };
3294
3301
 
3295
3302
 
3296
3303
  /***/ }),
@@ -3495,9 +3502,7 @@ class Rational {
3495
3502
  return tex;
3496
3503
  };
3497
3504
  this._makeOneLineOfTableOfSigns = (factor, zeroes, zeroSign) => {
3498
- let oneLine = [],
3499
- // TODO : check if there is no zero ?
3500
- currentZero = factor.getZeroes().map(x => x.tex);
3505
+ let oneLine = [], currentZero = factor.getZeroes().map(x => x.tex);
3501
3506
  // First +/- sign, before the first zero
3502
3507
  oneLine.push('');
3503
3508
  oneLine.push(factor.evaluate(zeroes[0].value - 1).sign() === 1 ? '+' : '-');
@@ -3525,10 +3530,10 @@ class Rational {
3525
3530
  return this._denominator;
3526
3531
  }
3527
3532
  get tex() {
3528
- return `\\dfrac{ ${this._numerator.tex} }{ ${this._denominator.tex} }`;
3533
+ return `\\frac{ ${this._numerator.tex} }{ ${this._denominator.tex} }`;
3529
3534
  }
3530
3535
  get texFactors() {
3531
- return `\\dfrac{ ${this._numerator.texFactors} }{ ${this._denominator.texFactors} }`;
3536
+ return `\\frac{ ${this._numerator.texFactors} }{ ${this._denominator.texFactors} }`;
3532
3537
  }
3533
3538
  }
3534
3539
  exports.Rational = Rational;
@@ -4468,7 +4473,7 @@ class PolynomExpProduct {
4468
4473
  }
4469
4474
  // restore all degrees to negative again.
4470
4475
  denominators.map(x => x.degree.opposed());
4471
- tex = `\\dfrac{ ${numeratorsAsTex.join(' \\cdot ')} }{ ${denominatorsAsTex.join(' \\cdot ')} }`;
4476
+ tex = `\\frac{ ${numeratorsAsTex.join(' \\cdot ')} }{ ${denominatorsAsTex.join(' \\cdot ')} }`;
4472
4477
  }
4473
4478
  }
4474
4479
  // Apply the modification
@@ -5384,7 +5389,7 @@ class Point {
5384
5389
  let V = new vector_1.Vector(this, item);
5385
5390
  value = V.norm;
5386
5391
  fraction = V.normSquare.sqrt();
5387
- tex = V.normSquare.isSquare() ? fraction.tex : `\\sqrt{\\dfrac{ ${V.normSquare.numerator} }{ ${V.normSquare.denominator} }}`;
5392
+ tex = V.normSquare.isSquare() ? fraction.tex : `\\sqrt{\\frac{ ${V.normSquare.numerator} }{ ${V.normSquare.denominator} }}`;
5388
5393
  }
5389
5394
  return { value, fraction, tex };
5390
5395
  };
@@ -6030,7 +6035,11 @@ exports.Numeric = Numeric;
6030
6035
 
6031
6036
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6032
6037
  if (k2 === undefined) k2 = k;
6033
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
6038
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6039
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6040
+ desc = { enumerable: true, get: function() { return m[k]; } };
6041
+ }
6042
+ Object.defineProperty(o, k2, desc);
6034
6043
  }) : (function(o, m, k, k2) {
6035
6044
  if (k2 === undefined) k2 = k;
6036
6045
  o[k2] = m[k];