pimath 0.0.105 → 0.0.106
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 +78 -64
- package/dist/pi.js.map +1 -1
- package/dist/pi.min.js +1 -1
- package/dist/pi.min.js.map +1 -1
- package/esm/maths/expressions/numexp.js +1 -1
- package/esm/maths/expressions/numexp.js.map +1 -1
- package/esm/maths/shutingyard.d.ts +2 -8
- package/esm/maths/shutingyard.js +77 -63
- package/esm/maths/shutingyard.js.map +1 -1
- package/package.json +1 -1
- package/src/maths/expressions/numexp.ts +1 -1
- package/src/maths/shutingyard.ts +77 -80
- package/tests/numexp.test.ts +6 -2
package/dist/pi.js
CHANGED
|
@@ -4990,7 +4990,7 @@ const fraction_1 = __webpack_require__(506);
|
|
|
4990
4990
|
class NumExp {
|
|
4991
4991
|
constructor(value, uniformize) {
|
|
4992
4992
|
this._expression = value;
|
|
4993
|
-
this._rpn = new shutingyard_1.Shutingyard(shutingyard_1.ShutingyardMode.NUMERIC).parse(value).rpn;
|
|
4993
|
+
this._rpn = new shutingyard_1.Shutingyard(shutingyard_1.ShutingyardMode.NUMERIC).parse(value, uniformize).rpn;
|
|
4994
4994
|
}
|
|
4995
4995
|
get rpn() {
|
|
4996
4996
|
return this._rpn;
|
|
@@ -7608,78 +7608,92 @@ class Shutingyard {
|
|
|
7608
7608
|
// add the last token
|
|
7609
7609
|
return normalizedExpr + nextToken;
|
|
7610
7610
|
}
|
|
7611
|
-
/**
|
|
7612
|
-
|
|
7613
|
-
|
|
7614
|
-
|
|
7615
|
-
|
|
7616
|
-
Uniformizer(expr) {
|
|
7617
|
-
|
|
7618
|
-
|
|
7619
|
-
|
|
7620
|
-
|
|
7621
|
-
|
|
7622
|
-
|
|
7623
|
-
|
|
7624
|
-
|
|
7625
|
-
|
|
7626
|
-
|
|
7627
|
-
|
|
7628
|
-
|
|
7629
|
-
|
|
7630
|
-
|
|
7631
|
-
|
|
7632
|
-
|
|
7633
|
-
|
|
7634
|
-
|
|
7635
|
-
|
|
7636
|
-
|
|
7637
|
-
|
|
7638
|
-
|
|
7639
|
-
|
|
7640
|
-
|
|
7641
|
-
|
|
7642
|
-
|
|
7643
|
-
|
|
7644
|
-
|
|
7645
|
-
|
|
7646
|
-
|
|
7647
|
-
|
|
7648
|
-
|
|
7649
|
-
|
|
7650
|
-
|
|
7651
|
-
|
|
7652
|
-
|
|
7653
|
-
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
|
|
7659
|
-
|
|
7660
|
-
|
|
7661
|
-
|
|
7662
|
-
|
|
7663
|
-
|
|
7664
|
-
|
|
7665
|
-
|
|
7666
|
-
|
|
7667
|
-
|
|
7668
|
-
|
|
7669
|
-
|
|
7670
|
-
|
|
7611
|
+
// /**
|
|
7612
|
+
// * Sanitize an expression by adding missing common operation (multiplication between parentheseses)
|
|
7613
|
+
// * @param expr
|
|
7614
|
+
// * @constructor
|
|
7615
|
+
// */
|
|
7616
|
+
// Uniformizer(expr: string): string {
|
|
7617
|
+
// // TODO: Delete this old version
|
|
7618
|
+
// // Prefere "normalize", much more robust !
|
|
7619
|
+
// // Determiner if need to be uniformized
|
|
7620
|
+
// if (!this._uniformize) {
|
|
7621
|
+
// return expr
|
|
7622
|
+
// }
|
|
7623
|
+
//
|
|
7624
|
+
// // Generate the list of function token.
|
|
7625
|
+
// let fnToken: string[] = []
|
|
7626
|
+
// for (let token in this._tokenConfig) {
|
|
7627
|
+
// if (this._tokenConfig[token].type === ShutingyardType.FUNCTION) {
|
|
7628
|
+
// fnToken.push(token)
|
|
7629
|
+
// }
|
|
7630
|
+
// }
|
|
7631
|
+
// // sort if from the lengthy to the smallest function
|
|
7632
|
+
// fnToken.sort((a, b) => b.length - a.length)
|
|
7633
|
+
// let tokenRegExp = new RegExp(`(${fnToken.join('|')})`, 'g')
|
|
7634
|
+
// let functionTokenOrder = Array.from(expr.matchAll(tokenRegExp))
|
|
7635
|
+
//
|
|
7636
|
+
//
|
|
7637
|
+
// let expr2;
|
|
7638
|
+
//
|
|
7639
|
+
// // Replace all function by @
|
|
7640
|
+
// expr2 = expr.replace(tokenRegExp, '@')
|
|
7641
|
+
// // Add * before @ (functionn)
|
|
7642
|
+
// expr2 = expr2.replace(/([\da-zA-Z])(@)/g, "$1*$2");
|
|
7643
|
+
//
|
|
7644
|
+
// // Replace missing multiplication between two parenthese
|
|
7645
|
+
// expr2 = expr2.replace(/\)\(/g, ')*(');
|
|
7646
|
+
//
|
|
7647
|
+
// // Replace missing multiplication between number or setLetter and parenthese.
|
|
7648
|
+
//
|
|
7649
|
+
// // 3x(x-4) => 3x*(x-4)
|
|
7650
|
+
// expr2 = expr2.replace(/([\da-zA-Z])(\()/g, "$1*$2");
|
|
7651
|
+
//
|
|
7652
|
+
// // (x-4)3x => (x-4)*3x
|
|
7653
|
+
// expr2 = expr2.replace(/(\))([\da-zA-Z])/g, "$1*$2");
|
|
7654
|
+
//
|
|
7655
|
+
// // Add multiplication between number and letters.
|
|
7656
|
+
// // 3x => 3*x
|
|
7657
|
+
// expr2 = expr2.replace(/([0-9])([a-zA-Z])/g, "$1*$2");
|
|
7658
|
+
// expr2 = expr2.replace(/([a-zA-Z])([0-9])/g, "$1*$2");
|
|
7659
|
+
//
|
|
7660
|
+
// // Remove letter between function token and it's parenthese.
|
|
7661
|
+
// // for (let token of fnToken) {
|
|
7662
|
+
// // // Remove
|
|
7663
|
+
// // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
|
|
7664
|
+
// // }
|
|
7665
|
+
// // Add multiplication between letters ?
|
|
7666
|
+
// expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
|
|
7667
|
+
// expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
|
|
7668
|
+
//
|
|
7669
|
+
// // Restore operation auto formatting (prevent adding the multiplication star)
|
|
7670
|
+
// let exprAsArray = expr2.split('@')
|
|
7671
|
+
//
|
|
7672
|
+
// if (exprAsArray.length > 0) {
|
|
7673
|
+
// expr2 = ""
|
|
7674
|
+
// for (let idx in exprAsArray) {
|
|
7675
|
+
// }
|
|
7676
|
+
// for (let token of fnToken) {
|
|
7677
|
+
// // Remove
|
|
7678
|
+
//
|
|
7679
|
+
// // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
|
|
7680
|
+
// }
|
|
7681
|
+
// }
|
|
7682
|
+
//
|
|
7683
|
+
// return expr2;
|
|
7684
|
+
// }
|
|
7671
7685
|
/**
|
|
7672
7686
|
* Parse an expression using the shutting yard tree algorithms
|
|
7673
7687
|
* @param expr (string) Expression to analyse
|
|
7674
7688
|
* Returns a RPN list of items.
|
|
7675
|
-
* @param
|
|
7689
|
+
* @param uniformize
|
|
7676
7690
|
*/
|
|
7677
|
-
parse(expr,
|
|
7691
|
+
parse(expr, uniformize) {
|
|
7678
7692
|
let outQueue = [], // Output queue
|
|
7679
7693
|
opStack = [], // Operation queue
|
|
7680
7694
|
token = '', tokenPos = 0, tokenType = '', previousOpStatckLength = 0;
|
|
7681
7695
|
// Normalize the input if required.
|
|
7682
|
-
if (this._uniformize)
|
|
7696
|
+
if (uniformize || this._uniformize)
|
|
7683
7697
|
expr = this.normalize(expr);
|
|
7684
7698
|
let securityLoopLvl1 = 50, securityLoopLvl2_default = 50, securityLoopLvl2;
|
|
7685
7699
|
while (tokenPos < expr.length) {
|