pimath 0.0.132 → 0.0.134
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/pimath.js +2444 -3566
- package/package.json +37 -20
- package/types/algebra/equation.d.ts +109 -0
- package/types/algebra/equation.d.ts.map +1 -0
- package/types/algebra/equationSolver.d.ts +16 -0
- package/types/algebra/equationSolver.d.ts.map +1 -0
- package/types/algebra/factor.d.ts +47 -0
- package/types/algebra/factor.d.ts.map +1 -0
- package/{dist/maths → types}/algebra/monom.d.ts +98 -131
- package/types/algebra/monom.d.ts.map +1 -0
- package/types/algebra/polyFactor.d.ts +41 -0
- package/types/algebra/polyFactor.d.ts.map +1 -0
- package/types/algebra/polynom.d.ts +126 -0
- package/types/algebra/polynom.d.ts.map +1 -0
- package/types/algebra/rational.d.ts +41 -0
- package/types/algebra/rational.d.ts.map +1 -0
- package/{dist/maths → types}/coefficients/fraction.d.ts +69 -66
- package/types/coefficients/fraction.d.ts.map +1 -0
- package/{dist/maths → types}/coefficients/nthRoot.d.ts +3 -3
- package/{dist/maths → types}/geometry/circle.d.ts +10 -15
- package/types/geometry/circle.d.ts.map +1 -0
- package/types/geometry/geomMath.d.ts +10 -0
- package/types/geometry/geomMath.d.ts.map +1 -0
- package/{dist/maths → types}/geometry/line.d.ts +25 -35
- package/types/geometry/line.d.ts.map +1 -0
- package/types/geometry/line3.d.ts +50 -0
- package/types/geometry/line3.d.ts.map +1 -0
- package/types/geometry/plane3.d.ts +29 -0
- package/types/geometry/plane3.d.ts.map +1 -0
- package/types/geometry/vector.d.ts +70 -0
- package/types/geometry/vector.d.ts.map +1 -0
- package/types/geometry/vector3d.d.ts +37 -0
- package/types/geometry/vector3d.d.ts.map +1 -0
- package/types/index.d.ts +57 -0
- package/types/index.d.ts.map +1 -0
- package/types/numeric.d.ts +39 -0
- package/types/pimath.interface.d.ts +57 -0
- package/types/pimath.interface.d.ts.map +1 -0
- package/types/randomization/algebra/rndEquation.d.ts +4 -0
- package/types/randomization/algebra/rndEquation.d.ts.map +1 -0
- package/types/randomization/algebra/rndMonom.d.ts +4 -0
- package/types/randomization/algebra/rndMonom.d.ts.map +1 -0
- package/types/randomization/algebra/rndPolynom.d.ts +5 -0
- package/types/randomization/algebra/rndPolynom.d.ts.map +1 -0
- package/types/randomization/coefficient/rndFraction.d.ts +4 -0
- package/types/randomization/coefficient/rndFraction.d.ts.map +1 -0
- package/types/randomization/geometry/rndCircle.d.ts +4 -0
- package/types/randomization/geometry/rndCircle.d.ts.map +1 -0
- package/types/randomization/geometry/rndLine.d.ts +4 -0
- package/types/randomization/geometry/rndLine.d.ts.map +1 -0
- package/types/randomization/geometry/rndLine3.d.ts +4 -0
- package/types/randomization/geometry/rndLine3.d.ts.map +1 -0
- package/types/randomization/geometry/rndPoint.d.ts +4 -0
- package/types/randomization/geometry/rndPoint.d.ts.map +1 -0
- package/types/randomization/random.d.ts +20 -0
- package/types/randomization/random.d.ts.map +1 -0
- package/types/randomization/rndHelpers.d.ts +21 -0
- package/types/randomization/rndTypes.d.ts +64 -0
- package/types/randomization/rndTypes.d.ts.map +1 -0
- package/dist/main.d.ts +0 -1
- package/dist/maths/algebra/equation.d.ts +0 -120
- package/dist/maths/algebra/linearSystem.d.ts +0 -40
- package/dist/maths/algebra/logicalset.d.ts +0 -28
- package/dist/maths/algebra/polynom.d.ts +0 -155
- package/dist/maths/algebra/rational.d.ts +0 -44
- package/dist/maths/algebra/study/rationalStudy.d.ts +0 -14
- package/dist/maths/algebra/study.d.ts +0 -140
- package/dist/maths/geometry/point.d.ts +0 -36
- package/dist/maths/geometry/triangle.d.ts +0 -92
- package/dist/maths/geometry/vector.d.ts +0 -38
- package/dist/maths/numeric.d.ts +0 -28
- package/dist/maths/numexp.d.ts +0 -19
- package/dist/maths/randomization/random.d.ts +0 -26
- package/dist/maths/randomization/randomCore.d.ts +0 -7
- package/dist/maths/randomization/rndFraction.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryCircle.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryLine.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryPoint.d.ts +0 -13
- package/dist/maths/randomization/rndHelpers.d.ts +0 -23
- package/dist/maths/randomization/rndMonom.d.ts +0 -13
- package/dist/maths/randomization/rndPolynom.d.ts +0 -14
- package/dist/maths/randomization/rndTypes.d.ts +0 -40
- package/dist/maths/shutingyard.d.ts +0 -59
- package/dist/pimath.d.ts +0 -39
- package/lib/main.ts +0 -1
- package/lib/maths/algebra/equation.ts +0 -891
- package/lib/maths/algebra/linearSystem.ts +0 -369
- package/lib/maths/algebra/logicalset.ts +0 -183
- package/lib/maths/algebra/monom.ts +0 -1027
- package/lib/maths/algebra/polynom.ts +0 -1537
- package/lib/maths/algebra/rational.ts +0 -244
- package/lib/maths/algebra/study/rationalStudy.ts +0 -287
- package/lib/maths/algebra/study.ts +0 -506
- package/lib/maths/coefficients/fraction.ts +0 -593
- package/lib/maths/coefficients/nthRoot.ts +0 -148
- package/lib/maths/geometry/circle.ts +0 -379
- package/lib/maths/geometry/line.ts +0 -604
- package/lib/maths/geometry/point.ts +0 -215
- package/lib/maths/geometry/triangle.ts +0 -368
- package/lib/maths/geometry/vector.ts +0 -243
- package/lib/maths/numeric.ts +0 -162
- package/lib/maths/numexp.ts +0 -198
- package/lib/maths/randomization/random.ts +0 -80
- package/lib/maths/randomization/randomCore.ts +0 -19
- package/lib/maths/randomization/rndFraction.ts +0 -47
- package/lib/maths/randomization/rndGeometryCircle.ts +0 -50
- package/lib/maths/randomization/rndGeometryLine.ts +0 -53
- package/lib/maths/randomization/rndGeometryPoint.ts +0 -69
- package/lib/maths/randomization/rndHelpers.ts +0 -107
- package/lib/maths/randomization/rndMonom.ts +0 -57
- package/lib/maths/randomization/rndPolynom.ts +0 -90
- package/lib/maths/randomization/rndTypes.ts +0 -43
- package/lib/maths/shutingyard.ts +0 -496
- package/lib/pimath.ts +0 -40
package/lib/maths/shutingyard.ts
DELETED
|
@@ -1,496 +0,0 @@
|
|
|
1
|
-
export type tokenType = {
|
|
2
|
-
[key: string]: {
|
|
3
|
-
precedence: number,
|
|
4
|
-
associative: string,
|
|
5
|
-
type: string
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const tokenConstant: { [Key: string]: number } = {
|
|
10
|
-
pi: Math.PI,
|
|
11
|
-
e: Math.exp(1)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export enum ShutingyardType {
|
|
15
|
-
VARIABLE = 'variable',
|
|
16
|
-
COEFFICIENT = 'coefficient',
|
|
17
|
-
OPERATION = 'operation',
|
|
18
|
-
CONSTANT = 'constant',
|
|
19
|
-
FUNCTION = 'function',
|
|
20
|
-
MONOM = 'monom'
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export enum ShutingyardMode {
|
|
24
|
-
EXPRESSION = 'expression',
|
|
25
|
-
POLYNOM = 'polynom',
|
|
26
|
-
SET = 'set',
|
|
27
|
-
NUMERIC = 'numeric'
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export type Token = { token: string, tokenType: string }
|
|
31
|
-
|
|
32
|
-
export class Shutingyard {
|
|
33
|
-
readonly _mode: ShutingyardMode;
|
|
34
|
-
private _rpn: Token[] = [];
|
|
35
|
-
private _tokenConfig: tokenType;
|
|
36
|
-
private _tokenConstant: { [Key: string]: number }
|
|
37
|
-
private _tokenKeys: string[]
|
|
38
|
-
private _uniformize: boolean;
|
|
39
|
-
|
|
40
|
-
constructor(mode?: ShutingyardMode) {
|
|
41
|
-
this._mode = typeof mode === 'undefined' ? ShutingyardMode.POLYNOM : mode;
|
|
42
|
-
this.tokenConfigInitialization()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Getter
|
|
46
|
-
get rpn() {
|
|
47
|
-
// console.log(this._rpn)
|
|
48
|
-
return this._rpn;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
get rpnToken() {
|
|
52
|
-
return this._rpn.map(x => x.token)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Determin if the token is a defined operation
|
|
57
|
-
* Defined operations: + - * / ^ sin cos tan
|
|
58
|
-
* @param token
|
|
59
|
-
*/
|
|
60
|
-
// isOperation(token: string): boolean {
|
|
61
|
-
// if (token[0].match(/[+\-*/^]/g)) {
|
|
62
|
-
// return true;
|
|
63
|
-
// }
|
|
64
|
-
// //
|
|
65
|
-
// // if (token.match(/^sin|cos|tan/g)) {
|
|
66
|
-
// // return true;
|
|
67
|
-
// // }
|
|
68
|
-
//
|
|
69
|
-
// return false;
|
|
70
|
-
// }
|
|
71
|
-
|
|
72
|
-
tokenConfigInitialization(): tokenType {
|
|
73
|
-
if (this._mode === ShutingyardMode.SET) {
|
|
74
|
-
this._tokenConfig = {
|
|
75
|
-
'&': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
76
|
-
'|': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
77
|
-
'!': {precedence: 4, associative: 'right', type: ShutingyardType.OPERATION},
|
|
78
|
-
'-': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION}
|
|
79
|
-
}
|
|
80
|
-
this._uniformize = false;
|
|
81
|
-
} else if (this._mode === ShutingyardMode.NUMERIC) {
|
|
82
|
-
this._tokenConfig = {
|
|
83
|
-
'^': {precedence: 4, associative: 'right', type: ShutingyardType.OPERATION},
|
|
84
|
-
'*': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
85
|
-
'/': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
86
|
-
'+': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
87
|
-
'-': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
88
|
-
'%': {precedence: 3, associative: 'right', type: ShutingyardType.OPERATION},
|
|
89
|
-
'sin': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
90
|
-
'cos': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
91
|
-
'tan': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
92
|
-
'sqrt': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
93
|
-
'nthrt': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
94
|
-
'ln': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
95
|
-
'log': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
96
|
-
}
|
|
97
|
-
this._uniformize = false
|
|
98
|
-
} else if (this._mode === ShutingyardMode.EXPRESSION) {
|
|
99
|
-
this._tokenConfig = {
|
|
100
|
-
'^': {precedence: 4, associative: 'right', type: ShutingyardType.OPERATION},
|
|
101
|
-
'*': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
102
|
-
'/': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
103
|
-
'+': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
104
|
-
'-': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
105
|
-
'%': {precedence: 3, associative: 'right', type: ShutingyardType.OPERATION},
|
|
106
|
-
'sin': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
107
|
-
'cos': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
108
|
-
'tan': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
109
|
-
'sqrt': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
110
|
-
'nthrt': {precedence: 4, associative: 'right', type: ShutingyardType.FUNCTION},
|
|
111
|
-
}
|
|
112
|
-
this._uniformize = true
|
|
113
|
-
} else {
|
|
114
|
-
this._tokenConfig = {
|
|
115
|
-
'^': {precedence: 4, associative: 'right', type: ShutingyardType.OPERATION},
|
|
116
|
-
'*': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
117
|
-
'/': {precedence: 3, associative: 'left', type: ShutingyardType.OPERATION},
|
|
118
|
-
'+': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
119
|
-
'-': {precedence: 2, associative: 'left', type: ShutingyardType.OPERATION},
|
|
120
|
-
}
|
|
121
|
-
this._uniformize = true
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
this._tokenKeys = Object.keys(this._tokenConfig).sort((a, b) => b.length - a.length)
|
|
125
|
-
return this._tokenConfig
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Get the next token to analyse.
|
|
130
|
-
* @param expr (string) Expression to analyse
|
|
131
|
-
* @param start (number) CUrrent position in the expr string.
|
|
132
|
-
*/
|
|
133
|
-
NextToken(expr: string, start: number): [string, number, string] {
|
|
134
|
-
let token: string, tokenType: string;
|
|
135
|
-
token = '';
|
|
136
|
-
tokenType = '';
|
|
137
|
-
// Case of parenthesis or comma (generic items)
|
|
138
|
-
if (expr[start] === '(') {
|
|
139
|
-
token = '(';
|
|
140
|
-
tokenType = '(';
|
|
141
|
-
}
|
|
142
|
-
// It's a closing parenthese
|
|
143
|
-
else if (expr[start] === ')') {
|
|
144
|
-
token = ')';
|
|
145
|
-
tokenType = ')';
|
|
146
|
-
}
|
|
147
|
-
// It's an argument separator for a function
|
|
148
|
-
else if (expr[start] === ',') {
|
|
149
|
-
token = ',';
|
|
150
|
-
tokenType = 'function-argument';
|
|
151
|
-
} else {
|
|
152
|
-
// Extract operation and function tokens
|
|
153
|
-
for (let key of this._tokenKeys) {
|
|
154
|
-
if (expr.substring(start, start + key.length) === key) {
|
|
155
|
-
token += key;
|
|
156
|
-
tokenType = this._tokenConfig[key].type
|
|
157
|
-
break
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Extract constant
|
|
162
|
-
for (let key in tokenConstant) {
|
|
163
|
-
if (expr.substring(start, start + key.length) === key) {
|
|
164
|
-
token += key;
|
|
165
|
-
tokenType = ShutingyardType.CONSTANT
|
|
166
|
-
break
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (token === '') {
|
|
171
|
-
// No function found ! Might be a coefficient !
|
|
172
|
-
if (expr[start].match(/[0-9]/)) {
|
|
173
|
-
if (this._mode === ShutingyardMode.POLYNOM && false) {
|
|
174
|
-
token = expr.substring(start).match(/^([0-9.,/]+)/)[0]
|
|
175
|
-
} else {
|
|
176
|
-
token = expr.substring(start).match(/^([0-9.]+)/)[0]
|
|
177
|
-
}
|
|
178
|
-
tokenType = ShutingyardType.COEFFICIENT
|
|
179
|
-
} else if (expr[start].match(/[a-zA-Z]/)) {
|
|
180
|
-
token = expr.substring(start).match(/^([a-zA-Z])/)[0]
|
|
181
|
-
tokenType = ShutingyardType.VARIABLE
|
|
182
|
-
} else {
|
|
183
|
-
console.log('Unidentified token', expr[start], expr, start)
|
|
184
|
-
token = expr[start]
|
|
185
|
-
tokenType = ShutingyardType.MONOM
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return [token, start + token.length, tokenType];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
normalize(expr: string): string {
|
|
195
|
-
if(expr.length===1){return expr}
|
|
196
|
-
|
|
197
|
-
// Get the list of function token.
|
|
198
|
-
let fnToken: string[] = [],
|
|
199
|
-
kToken: string[] = []
|
|
200
|
-
for (let token in this._tokenConfig) {
|
|
201
|
-
if (this._tokenConfig[token].type === ShutingyardType.FUNCTION) {
|
|
202
|
-
fnToken.push(token)
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
// sort if from the lengthy to the smallest function
|
|
206
|
-
fnToken.sort((a, b) => b.length - a.length)
|
|
207
|
-
|
|
208
|
-
for (let token in tokenConstant) {
|
|
209
|
-
kToken.push(token)
|
|
210
|
-
}
|
|
211
|
-
// sort if from the lengthy to the smallest function
|
|
212
|
-
kToken.sort((a, b) => b.length - a.length)
|
|
213
|
-
|
|
214
|
-
let normalizedExpr: string = "",
|
|
215
|
-
i = 0,
|
|
216
|
-
crtToken,
|
|
217
|
-
nextToken
|
|
218
|
-
|
|
219
|
-
while (i < expr.length - 1) {
|
|
220
|
-
// Check if we have a function token.
|
|
221
|
-
// The function MUST have an open parentheses
|
|
222
|
-
let tokenIdx = 0
|
|
223
|
-
while (tokenIdx < fnToken.length) {
|
|
224
|
-
let token = fnToken[tokenIdx]
|
|
225
|
-
if (expr.slice(i, i + token.length + 1) === token + '(') {
|
|
226
|
-
normalizedExpr += token + '('
|
|
227
|
-
i += token.length + 1
|
|
228
|
-
|
|
229
|
-
// Restart the scan for the function token
|
|
230
|
-
tokenIdx = 0
|
|
231
|
-
} else {
|
|
232
|
-
// scan for a next function token
|
|
233
|
-
tokenIdx++
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Check for a constant
|
|
238
|
-
tokenIdx = 0
|
|
239
|
-
while (tokenIdx < kToken.length) {
|
|
240
|
-
let token = kToken[tokenIdx]
|
|
241
|
-
if (expr.slice(i, i + token.length) === token) {
|
|
242
|
-
// We have found a constant.
|
|
243
|
-
// add it, but with remove the last letter
|
|
244
|
-
normalizedExpr += token.slice(0, -1)
|
|
245
|
-
i += token.length-1
|
|
246
|
-
|
|
247
|
-
// Exit the loop
|
|
248
|
-
break
|
|
249
|
-
}
|
|
250
|
-
tokenIdx++
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// The function token are solved.
|
|
254
|
-
crtToken = expr[i]
|
|
255
|
-
nextToken = expr[i + 1]
|
|
256
|
-
normalizedExpr += crtToken
|
|
257
|
-
|
|
258
|
-
if (crtToken.match(/[a-zA-Z]/g)) {
|
|
259
|
-
// Current element is a letter.
|
|
260
|
-
// if the next element is a letter, a number or an opening parentheses, add the multiplication sign.
|
|
261
|
-
if (nextToken?.match(/[a-zA-Z\d(]/)) {
|
|
262
|
-
normalizedExpr += '*'
|
|
263
|
-
}
|
|
264
|
-
} else if (crtToken.match(/\d/)) {
|
|
265
|
-
// Current element is a number.
|
|
266
|
-
// if the next element is a letter or a parentheses, add the multiplication sign.
|
|
267
|
-
if (nextToken?.match(/[a-zA-Z(]/)) {
|
|
268
|
-
normalizedExpr += '*'
|
|
269
|
-
}
|
|
270
|
-
} else if (crtToken === ')') {
|
|
271
|
-
// Current element is a closing parentheses.
|
|
272
|
-
// if the next element is a letter, a number or an opening parentheses, add the multiplication sign
|
|
273
|
-
if (nextToken?.match(/[a-zA-Z\d(]/)) {
|
|
274
|
-
normalizedExpr += '*'
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Go to next token
|
|
279
|
-
i++
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// add the last token
|
|
283
|
-
return normalizedExpr + (nextToken===undefined?'':nextToken)
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// /**
|
|
287
|
-
// * Sanitize an expression by adding missing common operation (multiplication between parentheseses)
|
|
288
|
-
// * @param expr
|
|
289
|
-
// * @constructor
|
|
290
|
-
// */
|
|
291
|
-
// Uniformizer(expr: string): string {
|
|
292
|
-
// // TODO: Delete this old version
|
|
293
|
-
// // Prefere "normalize", much more robust !
|
|
294
|
-
// // Determiner if need to be uniformized
|
|
295
|
-
// if (!this._uniformize) {
|
|
296
|
-
// return expr
|
|
297
|
-
// }
|
|
298
|
-
//
|
|
299
|
-
// // Generate the list of function token.
|
|
300
|
-
// let fnToken: string[] = []
|
|
301
|
-
// for (let token in this._tokenConfig) {
|
|
302
|
-
// if (this._tokenConfig[token].type === ShutingyardType.FUNCTION) {
|
|
303
|
-
// fnToken.push(token)
|
|
304
|
-
// }
|
|
305
|
-
// }
|
|
306
|
-
// // sort if from the lengthy to the smallest function
|
|
307
|
-
// fnToken.sort((a, b) => b.length - a.length)
|
|
308
|
-
// let tokenRegExp = new RegExp(`(${fnToken.join('|')})`, 'g')
|
|
309
|
-
// let functionTokenOrder = Array.from(expr.matchAll(tokenRegExp))
|
|
310
|
-
//
|
|
311
|
-
//
|
|
312
|
-
// let expr2;
|
|
313
|
-
//
|
|
314
|
-
// // Replace all function by @
|
|
315
|
-
// expr2 = expr.replace(tokenRegExp, '@')
|
|
316
|
-
// // Add * before @ (functionn)
|
|
317
|
-
// expr2 = expr2.replace(/([\da-zA-Z])(@)/g, "$1*$2");
|
|
318
|
-
//
|
|
319
|
-
// // Replace missing multiplication between two parenthese
|
|
320
|
-
// expr2 = expr2.replace(/\)\(/g, ')*(');
|
|
321
|
-
//
|
|
322
|
-
// // Replace missing multiplication between number or setLetter and parenthese.
|
|
323
|
-
//
|
|
324
|
-
// // 3x(x-4) => 3x*(x-4)
|
|
325
|
-
// expr2 = expr2.replace(/([\da-zA-Z])(\()/g, "$1*$2");
|
|
326
|
-
//
|
|
327
|
-
// // (x-4)3x => (x-4)*3x
|
|
328
|
-
// expr2 = expr2.replace(/(\))([\da-zA-Z])/g, "$1*$2");
|
|
329
|
-
//
|
|
330
|
-
// // Add multiplication between number and letters.
|
|
331
|
-
// // 3x => 3*x
|
|
332
|
-
// expr2 = expr2.replace(/([0-9])([a-zA-Z])/g, "$1*$2");
|
|
333
|
-
// expr2 = expr2.replace(/([a-zA-Z])([0-9])/g, "$1*$2");
|
|
334
|
-
//
|
|
335
|
-
// // Remove letter between function token and it's parenthese.
|
|
336
|
-
// // for (let token of fnToken) {
|
|
337
|
-
// // // Remove
|
|
338
|
-
// // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
|
|
339
|
-
// // }
|
|
340
|
-
// // Add multiplication between letters ?
|
|
341
|
-
// expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
|
|
342
|
-
// expr2 = expr2.replace(/([a-zA-Z])([a-zA-Z])/g, "$1*$2");
|
|
343
|
-
//
|
|
344
|
-
// // Restore operation auto formatting (prevent adding the multiplication star)
|
|
345
|
-
// let exprAsArray = expr2.split('@')
|
|
346
|
-
//
|
|
347
|
-
// if (exprAsArray.length > 0) {
|
|
348
|
-
// expr2 = ""
|
|
349
|
-
// for (let idx in exprAsArray) {
|
|
350
|
-
// }
|
|
351
|
-
// for (let token of fnToken) {
|
|
352
|
-
// // Remove
|
|
353
|
-
//
|
|
354
|
-
// // expr2 = expr2.replace(new RegExp(token + '\\*', 'g'), token);
|
|
355
|
-
// }
|
|
356
|
-
// }
|
|
357
|
-
//
|
|
358
|
-
// return expr2;
|
|
359
|
-
// }
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Parse an expression using the shutting yard tree algorithms
|
|
363
|
-
* @param expr (string) Expression to analyse
|
|
364
|
-
* Returns a RPN list of items.
|
|
365
|
-
* @param uniformize
|
|
366
|
-
*/
|
|
367
|
-
parse(expr: string, uniformize?: boolean): Shutingyard {
|
|
368
|
-
let outQueue: { token: string, tokenType: string }[] = [], // Output queue
|
|
369
|
-
opStack: { token: string, tokenType: string }[] = [], // Operation queue
|
|
370
|
-
token: string = '',
|
|
371
|
-
tokenPos: number = 0,
|
|
372
|
-
tokenType: string = '',
|
|
373
|
-
previousOpStatckLength = 0
|
|
374
|
-
|
|
375
|
-
// Normalize the input if required.
|
|
376
|
-
if (uniformize || this._uniformize) expr = this.normalize(expr)
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
let securityLoopLvl1 = 50,
|
|
380
|
-
securityLoopLvl2_default = 50,
|
|
381
|
-
securityLoopLvl2;
|
|
382
|
-
|
|
383
|
-
while (tokenPos < expr.length) {
|
|
384
|
-
securityLoopLvl1--;
|
|
385
|
-
if (securityLoopLvl1 === 0) {
|
|
386
|
-
console.log('SECURITY LEVEL 1 EXIT');
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// Get the next token and the corresponding new (ending) position
|
|
391
|
-
[token, tokenPos, tokenType] = this.NextToken(expr, tokenPos);
|
|
392
|
-
|
|
393
|
-
switch (tokenType) {
|
|
394
|
-
case 'monom':
|
|
395
|
-
case 'coefficient':
|
|
396
|
-
case 'variable':
|
|
397
|
-
case 'constant':
|
|
398
|
-
outQueue.push({
|
|
399
|
-
token,
|
|
400
|
-
tokenType
|
|
401
|
-
});
|
|
402
|
-
break;
|
|
403
|
-
case 'operation':
|
|
404
|
-
previousOpStatckLength = opStack.length;
|
|
405
|
-
//If the token is an operator, o1, then:
|
|
406
|
-
if (opStack.length > 0) {
|
|
407
|
-
let opTop = opStack[opStack.length - 1];
|
|
408
|
-
|
|
409
|
-
securityLoopLvl2 = +securityLoopLvl2_default;
|
|
410
|
-
|
|
411
|
-
//while there is an operator token o2, at the top of the operator stack and
|
|
412
|
-
while (opTop.token in this._tokenConfig && (
|
|
413
|
-
//either o1 is left-associative and its precedence is less than or equal to that of o2,
|
|
414
|
-
(this._tokenConfig[token].associative === 'left' && this._tokenConfig[token].precedence <= this._tokenConfig[opTop.token].precedence)
|
|
415
|
-
||
|
|
416
|
-
//or o1 is right associative, and has precedence less than that of o2,
|
|
417
|
-
(this._tokenConfig[token].associative === 'right' && this._tokenConfig[token].precedence < this._tokenConfig[opTop.token].precedence)
|
|
418
|
-
)
|
|
419
|
-
) {
|
|
420
|
-
|
|
421
|
-
/* Security exit ! */
|
|
422
|
-
securityLoopLvl2--;
|
|
423
|
-
if (securityLoopLvl2 === 0) {
|
|
424
|
-
console.log('SECURITY LEVEL 2 OPERATION EXIT');
|
|
425
|
-
break;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// Add the operation to the queue
|
|
429
|
-
outQueue.push((opStack.pop()) || {token: '', tokenType: 'operation'});
|
|
430
|
-
|
|
431
|
-
// Get the next operation on top of the Stack.
|
|
432
|
-
if (opStack.length === 0) {
|
|
433
|
-
break;
|
|
434
|
-
}
|
|
435
|
-
opTop = opStack[opStack.length - 1];
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
//at the end of iteration push o1 onto the operator stack
|
|
439
|
-
opStack.push({token, tokenType});
|
|
440
|
-
break;
|
|
441
|
-
case 'function-argument':
|
|
442
|
-
// TODO: check if the opStack exist.
|
|
443
|
-
securityLoopLvl2 = +securityLoopLvl2_default;
|
|
444
|
-
while (opStack[opStack.length - 1].token !== '(' && opStack.length > 0) {
|
|
445
|
-
securityLoopLvl2--;
|
|
446
|
-
if (securityLoopLvl2 === 0) {
|
|
447
|
-
console.log('SECURITY LEVEL 2 FUNCTION ARGUMENT EXIT');
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
outQueue.push((opStack.pop()) || {token, tokenType});
|
|
452
|
-
}
|
|
453
|
-
break;
|
|
454
|
-
case '(':
|
|
455
|
-
opStack.push({token, tokenType});
|
|
456
|
-
// Add an empty value if next element is negative.
|
|
457
|
-
if (expr[tokenPos] === '-') {
|
|
458
|
-
outQueue.push({token: '0', tokenType: 'coefficient'});
|
|
459
|
-
}
|
|
460
|
-
break;
|
|
461
|
-
case ')':
|
|
462
|
-
securityLoopLvl2 = +securityLoopLvl2_default;
|
|
463
|
-
//Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
|
|
464
|
-
while (opStack[opStack.length - 1].token !== '(' && opStack.length > 1 /*Maybe zero !? */) {
|
|
465
|
-
securityLoopLvl2--;
|
|
466
|
-
if (securityLoopLvl2 === 0) {
|
|
467
|
-
console.log('SECURITY LEVEL 2 CLOSING PARENTHESE EXIT');
|
|
468
|
-
break;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
outQueue.push((opStack.pop()) || {token, tokenType});
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
//Pop the left parenthesis from the stack, but not onto the output queue.
|
|
475
|
-
opStack.pop();
|
|
476
|
-
break;
|
|
477
|
-
case 'function':
|
|
478
|
-
opStack.push({token, tokenType});
|
|
479
|
-
break;
|
|
480
|
-
default:
|
|
481
|
-
// In theory, everything should be handled.
|
|
482
|
-
console.log(`SHUTING YARD: ${tokenType} : ${token} `);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// Output
|
|
486
|
-
// console.log(outQueue.concat(opStack.reverse()).join(" "));
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// console.log(outQueue.concat(opStack.reverse()));
|
|
490
|
-
this._rpn = outQueue.concat(opStack.reverse());
|
|
491
|
-
|
|
492
|
-
return this;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
}
|
package/lib/pimath.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// Expose as global
|
|
2
|
-
import {Shutingyard} from "./maths/shutingyard.ts";
|
|
3
|
-
import {Numeric} from "./maths/numeric.ts";
|
|
4
|
-
import {NumExp} from "./maths/numexp.ts";
|
|
5
|
-
import {Fraction} from "./maths/coefficients/fraction.ts";
|
|
6
|
-
import {NthRoot} from "./maths/coefficients/nthRoot.ts";
|
|
7
|
-
import {Monom} from "./maths/algebra/monom.ts";
|
|
8
|
-
import {Polynom} from "./maths/algebra/polynom.ts";
|
|
9
|
-
import {Equation} from "./maths/algebra/equation.ts";
|
|
10
|
-
import {LinearSystem} from "./maths/algebra/linearSystem.ts";
|
|
11
|
-
import {Rational} from "./maths/algebra/rational.ts";
|
|
12
|
-
import {Logicalset} from "./maths/algebra/logicalset.ts";
|
|
13
|
-
import {Random} from "./maths/randomization/random.ts";
|
|
14
|
-
import {Vector} from "./maths/geometry/vector.ts";
|
|
15
|
-
import {Point} from "./maths/geometry/point.ts";
|
|
16
|
-
import {Line} from "./maths/geometry/line.ts";
|
|
17
|
-
import {Triangle} from "./maths/geometry/triangle.ts";
|
|
18
|
-
import {Circle} from "./maths/geometry/circle.ts";
|
|
19
|
-
|
|
20
|
-
export const PiMath = {
|
|
21
|
-
ShutingYard: Shutingyard,
|
|
22
|
-
Numeric: Numeric,
|
|
23
|
-
NumExp: NumExp,
|
|
24
|
-
Fraction: Fraction,
|
|
25
|
-
Root: NthRoot,
|
|
26
|
-
Monom: Monom,
|
|
27
|
-
Polynom: Polynom,
|
|
28
|
-
Equation: Equation,
|
|
29
|
-
LinearSystem: LinearSystem,
|
|
30
|
-
Rational: Rational,
|
|
31
|
-
Logicalset: Logicalset,
|
|
32
|
-
Random: Random,
|
|
33
|
-
Geometry: {
|
|
34
|
-
Vector: Vector,
|
|
35
|
-
Point: Point,
|
|
36
|
-
Line: Line,
|
|
37
|
-
Triangle: Triangle,
|
|
38
|
-
Circle: Circle
|
|
39
|
-
}
|
|
40
|
-
};
|