pimath 0.0.26 → 0.0.27
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/dev/index.html +8 -7
- package/dev/pi.js +225 -94
- package/dev/pi.js.map +1 -1
- package/dist/pi.js +1 -1
- package/dist/pi.js.map +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/algebra.Equation.html +9 -9
- package/docs/classes/algebra.LinearSystem.html +1 -1
- package/docs/classes/algebra.Logicalset.html +2 -2
- package/docs/classes/algebra.Monom.html +45 -44
- package/docs/classes/algebra.Polynom.html +9 -9
- package/docs/classes/algebra.Rational.html +2 -2
- package/docs/classes/coefficients.Fraction.html +6 -6
- package/docs/classes/coefficients.Nthroot.html +1 -1
- package/docs/classes/geometry.Circle.html +1 -1
- package/docs/classes/geometry.Line.html +2 -2
- package/docs/classes/geometry.Point.html +1 -1
- package/docs/classes/geometry.Triangle.html +5 -5
- package/docs/classes/geometry.Vector.html +1 -1
- package/docs/classes/numeric.Numeric.html +5 -5
- package/docs/classes/shutingyard.Shutingyard.html +5 -5
- package/docs/interfaces/geometry.remarquableLines.html +1 -1
- package/docs/modules/algebra.html +1 -1
- package/docs/modules/random.Random.html +1 -1
- package/docs/modules/random.html +1 -1
- package/esm/main.js +2 -0
- package/esm/main.js.map +1 -1
- package/esm/maths/algebra/logicalset.js +1 -1
- package/esm/maths/algebra/logicalset.js.map +1 -1
- package/esm/maths/algebra/monom.d.ts +2 -1
- package/esm/maths/algebra/monom.js +7 -1
- package/esm/maths/algebra/monom.js.map +1 -1
- package/esm/maths/algebra/rational.d.ts +1 -1
- package/esm/maths/algebra/rational.js +2 -2
- package/esm/maths/algebra/rational.js.map +1 -1
- package/esm/maths/numexp.d.ts +11 -0
- package/esm/maths/numexp.js +107 -0
- package/esm/maths/numexp.js.map +1 -0
- package/esm/maths/shutingyard.d.ts +21 -4
- package/esm/maths/shutingyard.js +72 -74
- package/esm/maths/shutingyard.js.map +1 -1
- package/package.json +1 -1
- package/src/main.ts +2 -0
- package/src/maths/algebra/logicalset.ts +2 -2
- package/src/maths/algebra/monom.ts +35 -22
- package/src/maths/algebra/rational.ts +1 -1
- package/src/maths/numexp.ts +127 -0
- package/src/maths/shutingyard.ts +144 -94
- package/tests/algebra/monom.test.ts +1 -1
- package/tests/shutingyard.test.ts +3 -3
package/esm/maths/shutingyard.js
CHANGED
|
@@ -1,45 +1,74 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Shutingyard = void 0;
|
|
3
|
+
exports.Shutingyard = exports.ShutingyardMode = exports.ShutingyardType = exports.tokenConstant = void 0;
|
|
4
|
+
exports.tokenConstant = {
|
|
5
|
+
pi: Math.PI,
|
|
6
|
+
e: Math.exp(1)
|
|
7
|
+
};
|
|
8
|
+
var ShutingyardType;
|
|
9
|
+
(function (ShutingyardType) {
|
|
10
|
+
ShutingyardType["VARIABLE"] = "variable";
|
|
11
|
+
ShutingyardType["COEFFICIENT"] = "coefficient";
|
|
12
|
+
ShutingyardType["OPERATION"] = "operation";
|
|
13
|
+
ShutingyardType["CONSTANT"] = "constant";
|
|
14
|
+
ShutingyardType["FUNCTION"] = "function";
|
|
15
|
+
ShutingyardType["MONOM"] = "monom";
|
|
16
|
+
})(ShutingyardType = exports.ShutingyardType || (exports.ShutingyardType = {}));
|
|
17
|
+
var ShutingyardMode;
|
|
18
|
+
(function (ShutingyardMode) {
|
|
19
|
+
ShutingyardMode["POLYNOM"] = "polynom";
|
|
20
|
+
ShutingyardMode["SET"] = "set";
|
|
21
|
+
ShutingyardMode["NUMERIC"] = "numeric";
|
|
22
|
+
})(ShutingyardMode = exports.ShutingyardMode || (exports.ShutingyardMode = {}));
|
|
4
23
|
class Shutingyard {
|
|
5
24
|
_rpn = [];
|
|
6
25
|
_mode;
|
|
7
26
|
_tokenConfig;
|
|
27
|
+
_tokenConstant;
|
|
8
28
|
_uniformize;
|
|
29
|
+
_tokenKeys;
|
|
9
30
|
constructor(mode) {
|
|
10
|
-
this._mode = typeof mode === 'undefined' ?
|
|
31
|
+
this._mode = typeof mode === 'undefined' ? ShutingyardMode.POLYNOM : mode;
|
|
11
32
|
this.tokenConfigInitialization();
|
|
12
33
|
}
|
|
13
|
-
isOperation(token) {
|
|
14
|
-
if (token[0].match(/[+\-*/^]/g)) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
34
|
tokenConfigInitialization() {
|
|
20
|
-
if (this._mode ===
|
|
35
|
+
if (this._mode === ShutingyardMode.SET) {
|
|
21
36
|
this._tokenConfig = {
|
|
22
|
-
'&': { precedence: 3, associative: 'left' },
|
|
23
|
-
'|': { precedence: 3, associative: 'left' },
|
|
24
|
-
'!': { precedence: 4, associative: 'right' },
|
|
25
|
-
'-': { precedence: 2, associative: 'left' }
|
|
37
|
+
'&': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
38
|
+
'|': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
39
|
+
'!': { precedence: 4, associative: 'right', type: ShutingyardType.OPERATION },
|
|
40
|
+
'-': { precedence: 2, associative: 'left', type: ShutingyardType.OPERATION }
|
|
26
41
|
};
|
|
27
42
|
this._uniformize = false;
|
|
28
43
|
}
|
|
44
|
+
else if (this._mode === ShutingyardMode.NUMERIC) {
|
|
45
|
+
this._tokenConfig = {
|
|
46
|
+
'^': { precedence: 4, associative: 'right', type: ShutingyardType.OPERATION },
|
|
47
|
+
'*': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
48
|
+
'/': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
49
|
+
'+': { precedence: 2, associative: 'left', type: ShutingyardType.OPERATION },
|
|
50
|
+
'-': { precedence: 2, associative: 'left', type: ShutingyardType.OPERATION },
|
|
51
|
+
'%': { precedence: 3, associative: 'right', type: ShutingyardType.OPERATION },
|
|
52
|
+
'sin': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
53
|
+
'cos': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
54
|
+
'tan': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
29
57
|
else {
|
|
30
58
|
this._tokenConfig = {
|
|
31
|
-
'^': { precedence: 4, associative: 'right' },
|
|
32
|
-
'*': { precedence: 3, associative: 'left' },
|
|
33
|
-
'/': { precedence: 3, associative: 'left' },
|
|
34
|
-
'+': { precedence: 2, associative: 'left' },
|
|
35
|
-
'-': { precedence: 2, associative: 'left' },
|
|
36
|
-
'%': { precedence: 3, associative: 'right' },
|
|
37
|
-
'sin': { precedence: 4, associative: 'right' },
|
|
38
|
-
'cos': { precedence: 4, associative: 'right' },
|
|
39
|
-
'
|
|
59
|
+
'^': { precedence: 4, associative: 'right', type: ShutingyardType.OPERATION },
|
|
60
|
+
'*': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
61
|
+
'/': { precedence: 3, associative: 'left', type: ShutingyardType.OPERATION },
|
|
62
|
+
'+': { precedence: 2, associative: 'left', type: ShutingyardType.OPERATION },
|
|
63
|
+
'-': { precedence: 2, associative: 'left', type: ShutingyardType.OPERATION },
|
|
64
|
+
'%': { precedence: 3, associative: 'right', type: ShutingyardType.OPERATION },
|
|
65
|
+
'sin': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
66
|
+
'cos': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
67
|
+
'tan': { precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION },
|
|
40
68
|
};
|
|
41
69
|
this._uniformize = true;
|
|
42
70
|
}
|
|
71
|
+
this._tokenKeys = Object.keys(this._tokenConfig).sort((a, b) => b.length - a.length);
|
|
43
72
|
return this._tokenConfig;
|
|
44
73
|
}
|
|
45
74
|
NextToken2(expr, start) {
|
|
@@ -59,75 +88,43 @@ class Shutingyard {
|
|
|
59
88
|
tokenType = 'function-argument';
|
|
60
89
|
}
|
|
61
90
|
else {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
91
|
+
for (let key of this._tokenKeys) {
|
|
92
|
+
if (expr.substring(start, start + key.length) === key) {
|
|
93
|
+
token += key;
|
|
94
|
+
tokenType = this._tokenConfig[key].type;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (let key in exports.tokenConstant) {
|
|
99
|
+
if (expr.substring(start, start + key.length) === key) {
|
|
65
100
|
token += key;
|
|
66
|
-
tokenType =
|
|
101
|
+
tokenType = ShutingyardType.CONSTANT;
|
|
67
102
|
break;
|
|
68
103
|
}
|
|
69
104
|
}
|
|
70
105
|
if (token === '') {
|
|
71
106
|
if (expr[start].match(/[0-9]/)) {
|
|
72
|
-
|
|
73
|
-
|
|
107
|
+
if (this._mode === ShutingyardMode.POLYNOM) {
|
|
108
|
+
token = expr.substring(start).match(/^([0-9.,/]+)/)[0];
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
token = expr.substring(start).match(/^([0-9.,]+)/)[0];
|
|
112
|
+
}
|
|
113
|
+
tokenType = ShutingyardType.COEFFICIENT;
|
|
74
114
|
}
|
|
75
115
|
else if (expr[start].match(/[a-zA-Z]/)) {
|
|
76
|
-
token = expr.
|
|
77
|
-
tokenType =
|
|
116
|
+
token = expr.substring(start).match(/^([a-zA-Z])/)[0];
|
|
117
|
+
tokenType = ShutingyardType.VARIABLE;
|
|
78
118
|
}
|
|
79
119
|
else {
|
|
80
120
|
console.log('Unidentified token', expr[start], expr, start);
|
|
81
121
|
token = expr[start];
|
|
82
|
-
tokenType =
|
|
122
|
+
tokenType = ShutingyardType.MONOM;
|
|
83
123
|
}
|
|
84
124
|
}
|
|
85
125
|
}
|
|
86
126
|
return [token, start + token.length, tokenType];
|
|
87
127
|
}
|
|
88
|
-
NextToken(expr, start) {
|
|
89
|
-
let tokenMatch, token, tokenType;
|
|
90
|
-
this.NextToken2(expr, start);
|
|
91
|
-
tokenMatch = (expr.substr(start).match(/^[0-9/a-zA-Z^]+/g)) || [];
|
|
92
|
-
if (expr.substr(start, start + 3).match(/^(sin|cos|tan)/g)) {
|
|
93
|
-
token = expr.substr(start, 3);
|
|
94
|
-
tokenType = 'function';
|
|
95
|
-
}
|
|
96
|
-
else if (tokenMatch.length > 0) {
|
|
97
|
-
token = tokenMatch[0];
|
|
98
|
-
tokenType = 'monom';
|
|
99
|
-
}
|
|
100
|
-
else if (expr[start].match(/[+\-*/^]/g)) {
|
|
101
|
-
token = expr[start];
|
|
102
|
-
tokenType = 'operation';
|
|
103
|
-
}
|
|
104
|
-
else if (expr[start].match(/[&|!]/g)) {
|
|
105
|
-
token = expr[start];
|
|
106
|
-
tokenType = 'operation';
|
|
107
|
-
}
|
|
108
|
-
else if (expr[start] === '(') {
|
|
109
|
-
token = '(';
|
|
110
|
-
tokenType = '(';
|
|
111
|
-
}
|
|
112
|
-
else if (expr[start] === ')') {
|
|
113
|
-
token = ')';
|
|
114
|
-
tokenType = ')';
|
|
115
|
-
}
|
|
116
|
-
else if (expr[start] === ',') {
|
|
117
|
-
token = ',';
|
|
118
|
-
tokenType = 'function-argument';
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
token = tokenMatch[0];
|
|
122
|
-
tokenType = 'monom';
|
|
123
|
-
if (token === '') {
|
|
124
|
-
token = expr[start];
|
|
125
|
-
tokenType = 'monom';
|
|
126
|
-
console.log('SHUTING YARD - NEXT TOKEN: error at ', start);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return [token, start + token.length, tokenType];
|
|
130
|
-
}
|
|
131
128
|
Uniformizer(expr) {
|
|
132
129
|
if (!this._uniformize) {
|
|
133
130
|
return expr;
|
|
@@ -160,6 +157,7 @@ class Shutingyard {
|
|
|
160
157
|
case 'monom':
|
|
161
158
|
case 'coefficient':
|
|
162
159
|
case 'variable':
|
|
160
|
+
case 'constant':
|
|
163
161
|
outQueue.push({
|
|
164
162
|
token,
|
|
165
163
|
tokenType
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shutingyard.js","sourceRoot":"","sources":["../../src/maths/shutingyard.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"shutingyard.js","sourceRoot":"","sources":["../../src/maths/shutingyard.ts"],"names":[],"mappings":";;;AAWa,QAAA,aAAa,GAAyB;IAC/C,EAAE,EAAE,IAAI,CAAC,EAAE;IACX,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;CACjB,CAAA;AACD,IAAY,eAOX;AAPD,WAAY,eAAe;IACvB,wCAAmB,CAAA;IACnB,8CAAyB,CAAA;IACzB,0CAAuB,CAAA;IACvB,wCAAqB,CAAA;IACrB,wCAAqB,CAAA;IACrB,kCAAe,CAAA;AACnB,CAAC,EAPW,eAAe,GAAf,uBAAe,KAAf,uBAAe,QAO1B;AACD,IAAY,eAIX;AAJD,WAAY,eAAe;IACvB,sCAAkB,CAAA;IAClB,8BAAW,CAAA;IACX,sCAAmB,CAAA;AACvB,CAAC,EAJW,eAAe,GAAf,uBAAe,KAAf,uBAAe,QAI1B;AAED,MAAa,WAAW;IACZ,IAAI,GAA2C,EAAE,CAAC;IACjD,KAAK,CAAkB;IACxB,YAAY,CAAY;IACxB,cAAc,CAAwB;IACtC,WAAW,CAAU;IACrB,UAAU,CAAU;IAE5B,YAAY,IAAsB;QAC9B,IAAI,CAAC,KAAK,GAAG,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;QAC1E,IAAI,CAAC,yBAAyB,EAAE,CAAA;IACpC,CAAC;IAmBD,yBAAyB;QACrB,IAAI,IAAI,CAAC,KAAK,KAAK,eAAe,CAAC,GAAG,EAAE;YACpC,IAAI,CAAC,YAAY,GAAG;gBAChB,GAAG,EAAE,EAAC,UAAU,EAAE,CAAC,EAAE,WAAW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KAAK,GAAG;oBACJ,gBAAgB,GAAG,CAAC,wBAAwB,CAAC;oBAE7C,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,KAAK,KAAK,GAAG,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAqB;wBACvF,gBAAgB,EAAE,CAAC;wBACnB,IAAI,gBAAgB,KAAK,CAAC,EAAE;4BACxB,OAAO,CAAC,GAAG,CAAC,0CAA0C,CAAC,CAAC;4BACxD,MAAM;yBACT;wBAED,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,EAAC,KAAK,EAAC,SAAS,EAAC,CAAC,CAAC;qBACvD;oBAGD,OAAO,CAAC,GAAG,EAAE,CAAC;oBACd,MAAM;gBACV,KAAK,UAAU;oBACX,OAAO,CAAC,IAAI,CAAC,EAAC,KAAK,EAAE,SAAS,EAAC,CAAC,CAAC;oBACjC,MAAM;gBACV;oBAEI,OAAO,CAAC,GAAG,CAAC,iBAAiB,SAAS,MAAM,KAAK,GAAG,CAAC,CAAC;aAC7D;SAIJ;QAGD,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QAE/C,OAAO,IAAI,CAAC;IAChB,CAAC;IAID,IAAI,GAAG;QAEH,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;CAGJ;AA1XD,kCA0XC"}
|
package/package.json
CHANGED
package/src/main.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Numeric} from "./maths/numeric";
|
|
2
|
+
import {NumExp} from "./maths/numexp";
|
|
2
3
|
import {Shutingyard} from "./maths/shutingyard";
|
|
3
4
|
import {Random} from "./maths/random";
|
|
4
5
|
import {Fraction, Nthroot} from "./maths/coefficients";
|
|
@@ -10,6 +11,7 @@ import {Line, Circle, Triangle, Point, Vector} from "./maths/geometry";
|
|
|
10
11
|
(<any>window).Pi = {
|
|
11
12
|
ShutingYard: Shutingyard,
|
|
12
13
|
Numeric: Numeric,
|
|
14
|
+
NumExp: NumExp,
|
|
13
15
|
Fraction: Fraction,
|
|
14
16
|
Root: Nthroot,
|
|
15
17
|
Monom: Monom,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @module Logicalset
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {Shutingyard} from '../shutingyard';
|
|
6
|
+
import {Shutingyard, ShutingyardMode} from '../shutingyard';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Polynom class can handle polynoms, reorder, resolve, ...
|
|
@@ -30,7 +30,7 @@ import {Shutingyard} from '../shutingyard';
|
|
|
30
30
|
// TODO: Must format the value string to convert some items...
|
|
31
31
|
|
|
32
32
|
// Parse the updated value to the shutingyard algorithm
|
|
33
|
-
this._rpn = new Shutingyard(
|
|
33
|
+
this._rpn = new Shutingyard(ShutingyardMode.SET).parse(value).rpn;
|
|
34
34
|
|
|
35
35
|
return this;
|
|
36
36
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/***
|
|
2
2
|
* Monom class
|
|
3
|
-
* Defined as coefficient * literal
|
|
4
|
-
* Examples: 3x^2, 3/5x^2, ...
|
|
5
3
|
*/
|
|
6
4
|
import {Fraction} from "../coefficients";
|
|
7
5
|
import {Numeric} from "../numeric";
|
|
@@ -16,8 +14,10 @@ export class Monom {
|
|
|
16
14
|
private _literal: literalType;
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
|
-
* Create
|
|
20
|
-
*
|
|
17
|
+
* Create a Monom
|
|
18
|
+
* Defined as \\(k \\cdot x^{n}\\), where \\( k,n \in \\mathbb{Q}\\).
|
|
19
|
+
* Examples: \\(3x^2\\) or \\(3/5x^2\\)
|
|
20
|
+
* @param value (optional) string The value that should be parse. Can be a Monom, a Fraction, a string or a number. If nothing is provided, it will return the trivial monom (0).
|
|
21
21
|
*/
|
|
22
22
|
constructor(value?: unknown) {
|
|
23
23
|
this.zero();
|
|
@@ -34,29 +34,35 @@ export class Monom {
|
|
|
34
34
|
// Getter and setter
|
|
35
35
|
// ------------------------------------------
|
|
36
36
|
/**
|
|
37
|
-
* Get the coefficient
|
|
37
|
+
* Get the coefficient \\(k\\) of the Monom \\(k\\cdot x^{n}\\)
|
|
38
|
+
* @returns {Fraction}
|
|
38
39
|
*/
|
|
39
40
|
get coefficient(): Fraction {
|
|
40
41
|
return this._coefficient;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
/**
|
|
44
|
-
* Set the coefficient value of the monom
|
|
45
|
-
* @param F
|
|
45
|
+
* Set the coefficient \\(k\\) value of the monom
|
|
46
|
+
* @param {Fraction | number | string} F
|
|
46
47
|
*/
|
|
47
|
-
set coefficient(F: Fraction) {
|
|
48
|
-
this._coefficient = F;
|
|
48
|
+
set coefficient(F: Fraction | number | string) {
|
|
49
|
+
this._coefficient = new Fraction(F);
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
/**
|
|
52
|
-
* Get the literal part
|
|
53
|
+
* Get the literal part of \\(x^{n_1}y^{n_2}\\) as dictionary \\[\\begin{array}{ll}x&=n_1\\\\y&=n_2\\end{array}\\]
|
|
54
|
+
* @returns {literalType}
|
|
53
55
|
*/
|
|
54
56
|
get literal(): literalType {
|
|
55
57
|
return this._literal;
|
|
56
58
|
}
|
|
57
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Get the literal square roots of the Monom.
|
|
62
|
+
* TODO: remove this getter ? Is it used and is it correct ?
|
|
63
|
+
* @returns {literalType}
|
|
64
|
+
*/
|
|
58
65
|
get literalSqrt(): literalType {
|
|
59
|
-
|
|
60
66
|
if (this.isLiteralSquare()) {
|
|
61
67
|
let L: literalType = {}
|
|
62
68
|
for (let key in this._literal) {
|
|
@@ -69,8 +75,8 @@ export class Monom {
|
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
/**
|
|
72
|
-
* Set the literal part of the monom
|
|
73
|
-
* @param L
|
|
78
|
+
* Set the literal part of the monom. Must be a dictionary {x: Fraction, y: Fraction, ...}
|
|
79
|
+
* @param {literalType} L
|
|
74
80
|
*/
|
|
75
81
|
set literal(L: literalType) {
|
|
76
82
|
this._literal = L;
|
|
@@ -230,11 +236,18 @@ export class Monom {
|
|
|
230
236
|
* Display the monom, forcing the '+' sign to appear
|
|
231
237
|
*/
|
|
232
238
|
get displayWithSign(): string {
|
|
233
|
-
// TODO: Rename or remove this getter ?
|
|
234
239
|
let d: String = this.display;
|
|
235
240
|
return (d[0] !== '-' ? '+' : '') + d;
|
|
236
241
|
}
|
|
237
242
|
|
|
243
|
+
get texWithSign(): string {
|
|
244
|
+
if (this.coefficient.isStrictlyPositive()) {
|
|
245
|
+
return '+' + this.tex
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return this.tex
|
|
249
|
+
}
|
|
250
|
+
|
|
238
251
|
/**
|
|
239
252
|
* Get the tex output of the monom
|
|
240
253
|
*/
|
|
@@ -281,15 +294,15 @@ export class Monom {
|
|
|
281
294
|
*/
|
|
282
295
|
parse = (inputStr: unknown): Monom => {
|
|
283
296
|
|
|
284
|
-
if(typeof inputStr === 'string') {
|
|
297
|
+
if (typeof inputStr === 'string') {
|
|
285
298
|
this._shutingYardToReducedMonom(inputStr)
|
|
286
|
-
}else if(typeof inputStr ==='number') {
|
|
299
|
+
} else if (typeof inputStr === 'number') {
|
|
287
300
|
this._coefficient = new Fraction(inputStr)
|
|
288
301
|
this._literal = {}
|
|
289
|
-
}else if(inputStr instanceof Fraction) {
|
|
302
|
+
} else if (inputStr instanceof Fraction) {
|
|
290
303
|
this._coefficient = inputStr.clone()
|
|
291
304
|
this._literal = {}
|
|
292
|
-
}else if(inputStr instanceof Monom){
|
|
305
|
+
} else if (inputStr instanceof Monom) {
|
|
293
306
|
this._coefficient = inputStr._coefficient.clone()
|
|
294
307
|
this._literal = this.copyLiterals(inputStr.literal)
|
|
295
308
|
}
|
|
@@ -384,7 +397,7 @@ export class Monom {
|
|
|
384
397
|
};
|
|
385
398
|
|
|
386
399
|
copyLiterals = (literal: literalType): literalType => {
|
|
387
|
-
let L:literalType = {}
|
|
400
|
+
let L: literalType = {}
|
|
388
401
|
|
|
389
402
|
for (let k in literal) {
|
|
390
403
|
L[k] = literal[k].clone()
|
|
@@ -392,7 +405,7 @@ export class Monom {
|
|
|
392
405
|
return L
|
|
393
406
|
}
|
|
394
407
|
|
|
395
|
-
makeSame = (M: Monom):Monom => {
|
|
408
|
+
makeSame = (M: Monom): Monom => {
|
|
396
409
|
// Copy the literal parts.
|
|
397
410
|
for (let k in M._literal) {
|
|
398
411
|
this.setLetter(k, M._literal[k].clone());
|
|
@@ -451,7 +464,7 @@ export class Monom {
|
|
|
451
464
|
add = (...M: Monom[]): Monom => {
|
|
452
465
|
for (let m of M) {
|
|
453
466
|
if (this.isSameAs(m)) {
|
|
454
|
-
if(this.isZero()){
|
|
467
|
+
if (this.isZero()) {
|
|
455
468
|
this.makeSame(m)
|
|
456
469
|
}
|
|
457
470
|
this._coefficient.add(m.coefficient);
|
|
@@ -469,7 +482,7 @@ export class Monom {
|
|
|
469
482
|
subtract = (...M: Monom[]): Monom => {
|
|
470
483
|
for (let m of M) {
|
|
471
484
|
if (this.isSameAs(m)) {
|
|
472
|
-
if(this.isZero()){
|
|
485
|
+
if (this.isZero()) {
|
|
473
486
|
this.makeSame(m)
|
|
474
487
|
}
|
|
475
488
|
this._coefficient.add(m.clone().coefficient.opposed());
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {Shutingyard, ShutingyardMode, ShutingyardType, tokenConstant} from "./shutingyard";
|
|
2
|
+
import {Fraction} from "./coefficients";
|
|
3
|
+
|
|
4
|
+
export class NumExp {
|
|
5
|
+
private _rpn: { token: string, tokenType: string }[]
|
|
6
|
+
private _expression: string
|
|
7
|
+
|
|
8
|
+
constructor(value: string) {
|
|
9
|
+
this._expression = value
|
|
10
|
+
this._rpn = new Shutingyard(ShutingyardMode.NUMERIC).parse(value).rpn
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private _extractDecimalPart(value: number): string {
|
|
14
|
+
let decimal = value.toString()
|
|
15
|
+
|
|
16
|
+
if (!decimal.includes('.')) {
|
|
17
|
+
return ''
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
decimal = decimal.split('.')[1]
|
|
21
|
+
|
|
22
|
+
return decimal.substring(0, decimal.length - 2)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private _numberCorrection(value: number): number {
|
|
26
|
+
// Must modify the number if it's like:
|
|
27
|
+
// a: 3.0000000000000003
|
|
28
|
+
// b: 3.9999999999999994
|
|
29
|
+
// remove the last character
|
|
30
|
+
// check if around n last characters are either 0 or 9
|
|
31
|
+
// if it is, 'round' the number.
|
|
32
|
+
|
|
33
|
+
const omega = 0.00000000000001,
|
|
34
|
+
number_of_digits = 6
|
|
35
|
+
|
|
36
|
+
let decimal = this._extractDecimalPart(value)
|
|
37
|
+
|
|
38
|
+
const n9 = decimal.match(/9+$/g)
|
|
39
|
+
const n0 = decimal.match(/0+$/g)
|
|
40
|
+
|
|
41
|
+
if (n9 && n9[0].length >= number_of_digits) {
|
|
42
|
+
// New tested values.
|
|
43
|
+
let mod = this._extractDecimalPart(value + omega),
|
|
44
|
+
mod0 = mod.match(/0+$/g)
|
|
45
|
+
|
|
46
|
+
if(mod0 && mod0[0].length>= number_of_digits){
|
|
47
|
+
// The value can be changed. Remove all zeros!
|
|
48
|
+
return +((value+omega).toString().split(mod0[0])[0])
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (n0 && n0[0].length >= number_of_digits) {
|
|
53
|
+
// New tested values.
|
|
54
|
+
let mod = this._extractDecimalPart(value - omega),
|
|
55
|
+
mod9 = mod.match(/9+$/g)
|
|
56
|
+
|
|
57
|
+
if(mod9 && mod9[0].length>= number_of_digits){
|
|
58
|
+
// The value can be changed. Remove all nines!
|
|
59
|
+
return +((value-omega).toString().split(mod9[0])[0])
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return value
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private _addToStack(stack:number[], value: number): void {
|
|
67
|
+
stack.push(this._numberCorrection(value))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
evaluate(values: { [Key: string]: number }): Number {
|
|
71
|
+
let stack: number[] = []
|
|
72
|
+
for (const element of this._rpn) {
|
|
73
|
+
if (element.tokenType === ShutingyardType.COEFFICIENT) {
|
|
74
|
+
// May be a numeric value or a Fraction.
|
|
75
|
+
if (!isNaN(+element.token)) {
|
|
76
|
+
this._addToStack(stack, +element.token)
|
|
77
|
+
} else {
|
|
78
|
+
this._addToStack(stack, new Fraction(element.token).value)
|
|
79
|
+
}
|
|
80
|
+
} else if (element.tokenType === ShutingyardType.VARIABLE) {
|
|
81
|
+
if (values[element.token] !== undefined) {
|
|
82
|
+
this._addToStack(stack, +values[element.token])
|
|
83
|
+
}
|
|
84
|
+
} else if (element.tokenType === ShutingyardType.CONSTANT) {
|
|
85
|
+
this._addToStack(stack, tokenConstant[element.token])
|
|
86
|
+
} else if (element.tokenType === ShutingyardType.OPERATION) {
|
|
87
|
+
if (element.token === '*') {
|
|
88
|
+
const b = +stack.pop(),
|
|
89
|
+
a = +stack.pop()
|
|
90
|
+
this._addToStack(stack, a * b)
|
|
91
|
+
} else if (element.token === '/') {
|
|
92
|
+
const b = +stack.pop(),
|
|
93
|
+
a = +stack.pop()
|
|
94
|
+
this._addToStack(stack, a / b)
|
|
95
|
+
} else if (element.token === '+') {
|
|
96
|
+
const b = +stack.pop(),
|
|
97
|
+
a = +stack.pop()
|
|
98
|
+
this._addToStack(stack, a + b)
|
|
99
|
+
} else if (element.token === '-') {
|
|
100
|
+
const b = +stack.pop(),
|
|
101
|
+
a = +stack.pop()
|
|
102
|
+
this._addToStack(stack, a - b)
|
|
103
|
+
} else if (element.token === '^') {
|
|
104
|
+
const b = +stack.pop(),
|
|
105
|
+
a = +stack.pop()
|
|
106
|
+
this._addToStack(stack, Math.pow(a, b))
|
|
107
|
+
}
|
|
108
|
+
} else if (element.tokenType === ShutingyardType.FUNCTION) {
|
|
109
|
+
const a = +stack.pop()
|
|
110
|
+
if (element.token === 'sin') {
|
|
111
|
+
this._addToStack(stack, Math.sin(a))
|
|
112
|
+
} else if (element.token === 'cos') {
|
|
113
|
+
this._addToStack(stack, Math.cos(a))
|
|
114
|
+
} else if (element.token === 'tan') {
|
|
115
|
+
this._addToStack(stack, Math.tan(a))
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (stack.length === 1) {
|
|
121
|
+
return stack[0]
|
|
122
|
+
} else {
|
|
123
|
+
console.error('There was a problem parsing', this._expression, '. The RPN array is', this._rpn)
|
|
124
|
+
return 0
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|