pimath 0.0.53 → 0.0.54

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
@@ -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();
@@ -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,82 @@ 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
+ console.error('While parsing, cannot apply ', element.token, 'to', stack[0].tex);
2165
+ throw "Error parsing the polynom " + this._rawString;
2166
+ }
2167
+ }
2168
+ break;
2169
+ case shutingyard_1.ShutingyardType.MONOM:
2170
+ // Should never appear.
2171
+ console.error('The monom token should not appear here');
2172
+ break;
2173
+ case shutingyard_1.ShutingyardType.FUNCTION:
2174
+ // Should never appear.
2175
+ console.error('The function token should not appear here - might be introduced later.');
2176
+ break;
2177
+ }
2178
+ };
2103
2179
  // ------------------------------------------
2104
2180
  /**
2105
2181
  * Parse a string to a polynom.
@@ -2886,7 +2962,7 @@ class Polynom {
2886
2962
  let stack = [], monom = new monom_1.Monom();
2887
2963
  // Loop through the
2888
2964
  for (const element of rpn) {
2889
- Polynom.addToken(stack, element);
2965
+ this.addToken(stack, element);
2890
2966
  }
2891
2967
  if (stack.length === 1) {
2892
2968
  this.add(stack[0]);
@@ -3215,82 +3291,6 @@ class Polynom {
3215
3291
  }
3216
3292
  }
3217
3293
  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
3294
 
3295
3295
 
3296
3296
  /***/ }),