@math-x-ts/expressions 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +145 -2
- package/lib/index.cjs +1142 -0
- package/lib/index.d.cts +635 -0
- package/lib/index.d.mts +635 -0
- package/lib/index.mjs +1126 -0
- package/package.json +52 -4
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,1126 @@
|
|
|
1
|
+
// src/math-node/MathNode.ts
|
|
2
|
+
let MathNodeType = /* @__PURE__ */ function(MathNodeType) {
|
|
3
|
+
MathNodeType["Add"] = "Add";
|
|
4
|
+
MathNodeType["Constant"] = "Constant";
|
|
5
|
+
MathNodeType["Divide"] = "Divide";
|
|
6
|
+
MathNodeType["Equal"] = "Equal";
|
|
7
|
+
MathNodeType["Fraction"] = "Fraction";
|
|
8
|
+
MathNodeType["Exponent"] = "Exponent";
|
|
9
|
+
MathNodeType["Multiply"] = "Multiply";
|
|
10
|
+
MathNodeType["Negative"] = "Negative";
|
|
11
|
+
MathNodeType["Parenthesis"] = "Parenthesis";
|
|
12
|
+
MathNodeType["Subtract"] = "Subtract";
|
|
13
|
+
MathNodeType["Variable"] = "Variable";
|
|
14
|
+
return MathNodeType;
|
|
15
|
+
}({});
|
|
16
|
+
/**
|
|
17
|
+
* Base class of every math node.
|
|
18
|
+
*/
|
|
19
|
+
var MathNode = class {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.value = null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* This function aims to solve the node step by step.
|
|
25
|
+
* It calls the next() function until the node is atomic.
|
|
26
|
+
*
|
|
27
|
+
* @returns {MathNode[]} - Each step.
|
|
28
|
+
*/
|
|
29
|
+
solveAll() {
|
|
30
|
+
const steps = [this];
|
|
31
|
+
while (!steps[steps.length - 1].isAtomic) {
|
|
32
|
+
const expression = steps[steps.length - 1].next();
|
|
33
|
+
steps.push(expression);
|
|
34
|
+
}
|
|
35
|
+
return steps;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* This function aims to solve the node step by step.
|
|
39
|
+
* It calls the next() function until the node is atomic.
|
|
40
|
+
*
|
|
41
|
+
* @returns {object[]} - Each step as a json.
|
|
42
|
+
*/
|
|
43
|
+
solveAllToJson() {
|
|
44
|
+
return this.solveAll().map((x) => x.toJson());
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* This function aims to solve the node step by step.
|
|
48
|
+
* It calls the next() function until the node is atomic.
|
|
49
|
+
*
|
|
50
|
+
* @returns {string[]} - Each step as a string.
|
|
51
|
+
*/
|
|
52
|
+
solveAllToString() {
|
|
53
|
+
return this.solveAll().map((x) => x.toString());
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* This function aims to solve the node step by step.
|
|
57
|
+
* It calls the next() function until the node is atomic.
|
|
58
|
+
*
|
|
59
|
+
* @returns {string[]} - Each step as valid latex string.
|
|
60
|
+
*/
|
|
61
|
+
solveAllToTex() {
|
|
62
|
+
return this.solveAll().map((x) => x.toTex());
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Solve the node until it is atomic.
|
|
66
|
+
*
|
|
67
|
+
* @returns {MathNode} - The last node.
|
|
68
|
+
*/
|
|
69
|
+
solve() {
|
|
70
|
+
const steps = this.solveAll();
|
|
71
|
+
return steps[steps.length - 1];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Solve the node until it is atomic and return its value.
|
|
75
|
+
*
|
|
76
|
+
* @returns {number | NaN} The value of the node.
|
|
77
|
+
*/
|
|
78
|
+
evaluate() {
|
|
79
|
+
return this.solve().value ?? NaN;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
// src/math-node/BaseOperation.ts
|
|
83
|
+
/**
|
|
84
|
+
* The base class for [addition, subtraction, multiplication, division].
|
|
85
|
+
*/
|
|
86
|
+
var BaseOperation = class BaseOperation extends MathNode {
|
|
87
|
+
/**
|
|
88
|
+
* @param {MathNode} left - The left node.
|
|
89
|
+
* @param {MathNode} right - The right node.
|
|
90
|
+
* @param {MathNodeType} type - The node type.
|
|
91
|
+
* @param {string} symbol - The symbol of the operator.
|
|
92
|
+
* @param {string} texSymbol - The LaTeX symbol of the operator, defaults to `symbol`.
|
|
93
|
+
*/
|
|
94
|
+
constructor(left, right, type, symbol, texSymbol = symbol) {
|
|
95
|
+
super();
|
|
96
|
+
this.isAtomic = false;
|
|
97
|
+
this.left = left;
|
|
98
|
+
this.right = right;
|
|
99
|
+
this.type = type;
|
|
100
|
+
this.symbol = symbol;
|
|
101
|
+
this.texSymbol = texSymbol;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @inheritDoc
|
|
105
|
+
*/
|
|
106
|
+
toJson() {
|
|
107
|
+
return {
|
|
108
|
+
type: this.type,
|
|
109
|
+
left: this.left.toJson(),
|
|
110
|
+
right: this.right.toJson()
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @inheritDoc
|
|
115
|
+
*/
|
|
116
|
+
toString(data) {
|
|
117
|
+
const left = this.left.toString({ isAfterOperator: data?.isAfterOperator });
|
|
118
|
+
const right = this.right.toString({ isAfterOperator: true });
|
|
119
|
+
return `${left} ${this.symbol} ${right}`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @inheritDoc
|
|
123
|
+
*/
|
|
124
|
+
toTex(data) {
|
|
125
|
+
const left = this.left.toTex({ isAfterOperator: data?.isAfterOperator });
|
|
126
|
+
const right = this.right.toTex({ isAfterOperator: true });
|
|
127
|
+
return `${left} ${this.texSymbol} ${right}`;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @inheritDoc
|
|
131
|
+
*/
|
|
132
|
+
isEqual(mathNode) {
|
|
133
|
+
return mathNode instanceof BaseOperation && this.type === mathNode.type && this.right.isEqual(mathNode.right) && this.left.isEqual(mathNode.left);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* A shared logic for all base operations.
|
|
137
|
+
*
|
|
138
|
+
* @returns {MathNode} The next step.
|
|
139
|
+
*/
|
|
140
|
+
next() {
|
|
141
|
+
if (this.left instanceof Constant && this.right instanceof Constant) return new Constant(this.operation(this.left.value, this.right.value));
|
|
142
|
+
/**
|
|
143
|
+
* Check if one of the operands override the operator.
|
|
144
|
+
*/
|
|
145
|
+
const leftOverride = this.left.override && this.left.override(this.type);
|
|
146
|
+
const rightOverride = this.right.override && this.right.override(this.type);
|
|
147
|
+
if (typeof leftOverride === "function") return leftOverride(this.right);
|
|
148
|
+
if (typeof rightOverride === "function") {
|
|
149
|
+
const nextStep = rightOverride(this.left);
|
|
150
|
+
if (nextStep instanceof BaseOperation) {
|
|
151
|
+
const temp = nextStep.right;
|
|
152
|
+
nextStep.right = nextStep.left;
|
|
153
|
+
nextStep.left = temp;
|
|
154
|
+
}
|
|
155
|
+
return nextStep;
|
|
156
|
+
}
|
|
157
|
+
return this.concreteNext();
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Check if the left and right operands are constants.
|
|
161
|
+
* If the operands are parenthesis containing a constant then it returns the constants from the parenthesis.
|
|
162
|
+
*
|
|
163
|
+
* @returns {MathNode[]} An array of operands [leftNode, rightNode].
|
|
164
|
+
*/
|
|
165
|
+
getConstantsFromParenthesis() {
|
|
166
|
+
if (!this.left.isAtomic || !this.right.isAtomic) {
|
|
167
|
+
let leftNode = this.left;
|
|
168
|
+
let rightNode = this.right;
|
|
169
|
+
if (this.left instanceof Parenthesis) leftNode = this.left.content;
|
|
170
|
+
if (this.right instanceof Parenthesis) rightNode = this.right.content;
|
|
171
|
+
if (leftNode instanceof Constant && rightNode instanceof Constant) return [leftNode, rightNode];
|
|
172
|
+
}
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
// src/math-node/Multiply.ts
|
|
177
|
+
/**
|
|
178
|
+
* Represent the multiplication operation as a math node.
|
|
179
|
+
*/
|
|
180
|
+
var Multiply = class Multiply extends BaseOperation {
|
|
181
|
+
/**
|
|
182
|
+
* @param {MathNode} left The left node.
|
|
183
|
+
* @param {MathNode} right The right node.
|
|
184
|
+
*/
|
|
185
|
+
constructor(left, right) {
|
|
186
|
+
super(left, right, "Multiply", "*", "\\times");
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* @inheritDoc
|
|
190
|
+
*/
|
|
191
|
+
concreteNext() {
|
|
192
|
+
if (!this.left.isAtomic || !this.right.isAtomic) {
|
|
193
|
+
const [leftNode, rightNode] = super.getConstantsFromParenthesis();
|
|
194
|
+
if (leftNode instanceof Constant && rightNode instanceof Constant) return new Constant(leftNode.value * rightNode.value);
|
|
195
|
+
const nextLeft = this.left.next();
|
|
196
|
+
const nextRight = this.right.next();
|
|
197
|
+
if (!this.left.isEqual(nextLeft) || !this.right.isEqual(nextRight)) return new Multiply(nextLeft, nextRight);
|
|
198
|
+
}
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Return the multiplication of left and right.
|
|
203
|
+
*
|
|
204
|
+
* @param {number} left The left operand.
|
|
205
|
+
* @param {number} right The right operand.
|
|
206
|
+
* @returns {number} The multiplication result.
|
|
207
|
+
*/
|
|
208
|
+
operation(left, right) {
|
|
209
|
+
return left * right;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
// src/math-node/Subtract.ts
|
|
213
|
+
/**
|
|
214
|
+
* Represent the subtraction operation as a math node.
|
|
215
|
+
*/
|
|
216
|
+
var Subtract = class Subtract extends BaseOperation {
|
|
217
|
+
/**
|
|
218
|
+
* @param {MathNode} left The left node.
|
|
219
|
+
* @param {MathNode} right The right node.
|
|
220
|
+
*/
|
|
221
|
+
constructor(left, right) {
|
|
222
|
+
super(left, right, "Subtract", "-");
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @inheritDoc
|
|
226
|
+
*/
|
|
227
|
+
concreteNext() {
|
|
228
|
+
if (!this.left.isAtomic || !this.right.isAtomic) {
|
|
229
|
+
const [leftNode, rightNode] = super.getConstantsFromParenthesis();
|
|
230
|
+
if (leftNode instanceof Constant && rightNode instanceof Constant) {
|
|
231
|
+
if (rightNode.value < 0) return new Add(leftNode, new Constant(Math.abs(rightNode.value)));
|
|
232
|
+
return new Constant(leftNode.value - rightNode.value);
|
|
233
|
+
}
|
|
234
|
+
return new Subtract(this.left.next(), this.right.next());
|
|
235
|
+
}
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Return the subtraction of left and right.
|
|
240
|
+
*
|
|
241
|
+
* @param {number} left The left operand.
|
|
242
|
+
* @param {number} right The right operand.
|
|
243
|
+
* @returns {number} The difference result
|
|
244
|
+
* .
|
|
245
|
+
*/
|
|
246
|
+
operation(left, right) {
|
|
247
|
+
return left - right;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
// src/math-node/number-helpers.ts
|
|
251
|
+
/**
|
|
252
|
+
* Find the greatest common divisor, always positive.
|
|
253
|
+
*
|
|
254
|
+
* @example gcd(15, 20) = 5.
|
|
255
|
+
* @example gcd(-2, 30) = 2.
|
|
256
|
+
*
|
|
257
|
+
* @param {number} a A number.
|
|
258
|
+
* @param {number} b A number.
|
|
259
|
+
* @returns {number} The gcd.
|
|
260
|
+
*/
|
|
261
|
+
const gcd = (a, b) => {
|
|
262
|
+
a = Math.abs(a);
|
|
263
|
+
b = Math.abs(b);
|
|
264
|
+
if (a === 0) return b;
|
|
265
|
+
return gcd(b % a, a);
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* Find the least common multiple.
|
|
269
|
+
*
|
|
270
|
+
* @example lcm(3, 4) = 12.
|
|
271
|
+
* @example lcm(5, 10) = 10.
|
|
272
|
+
*
|
|
273
|
+
* @param {number} a A number.
|
|
274
|
+
* @param {number} b A number.
|
|
275
|
+
* @returns {number} The lcm.
|
|
276
|
+
*/
|
|
277
|
+
const lcm = (a, b) => {
|
|
278
|
+
return a * b / gcd(a, b);
|
|
279
|
+
};
|
|
280
|
+
// src/math-node/Divide.ts
|
|
281
|
+
/**
|
|
282
|
+
* Represent the division operation as a math node.
|
|
283
|
+
*/
|
|
284
|
+
var Divide = class Divide extends BaseOperation {
|
|
285
|
+
/**
|
|
286
|
+
* @param {MathNode} left The left node.
|
|
287
|
+
* @param {MathNode} right The right node.
|
|
288
|
+
*/
|
|
289
|
+
constructor(left, right) {
|
|
290
|
+
super(left, right, "Divide", ":");
|
|
291
|
+
this.left = left;
|
|
292
|
+
this.right = right;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* @inheritDoc
|
|
296
|
+
*/
|
|
297
|
+
concreteNext() {
|
|
298
|
+
if (!this.left.isAtomic || !this.right.isAtomic) {
|
|
299
|
+
const [leftNode, rightNode] = super.getConstantsFromParenthesis();
|
|
300
|
+
if (leftNode instanceof Constant && rightNode instanceof Constant) return new Constant(leftNode.value / rightNode.value);
|
|
301
|
+
return new Divide(this.left.next(), this.right.next());
|
|
302
|
+
}
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Return the division between left and right.
|
|
307
|
+
*
|
|
308
|
+
* @param {number} left The left operand.
|
|
309
|
+
* @param {number} right The right operand.
|
|
310
|
+
* @returns {number} The division result.
|
|
311
|
+
*/
|
|
312
|
+
operation(left, right) {
|
|
313
|
+
return left / right;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
// src/math-node/Fraction.ts
|
|
317
|
+
/**
|
|
318
|
+
* Represent fractions as math node.
|
|
319
|
+
*/
|
|
320
|
+
var Fraction = class Fraction extends MathNode {
|
|
321
|
+
/**
|
|
322
|
+
* @param {MathNode} n The numerator, (the number at the top).
|
|
323
|
+
* @param {MathNode} d The denominator, (the number on the bottom).
|
|
324
|
+
*/
|
|
325
|
+
constructor(n, d) {
|
|
326
|
+
super();
|
|
327
|
+
this.type = "Fraction";
|
|
328
|
+
this.isAtomic = false;
|
|
329
|
+
this.override = (type) => {
|
|
330
|
+
return {
|
|
331
|
+
["Add"]: this.add.bind(this),
|
|
332
|
+
["Subtract"]: this.subtract.bind(this),
|
|
333
|
+
["Multiply"]: this.multiply.bind(this),
|
|
334
|
+
["Divide"]: this.divide.bind(this)
|
|
335
|
+
}[type.toString()];
|
|
336
|
+
};
|
|
337
|
+
this.n = n;
|
|
338
|
+
this.d = d;
|
|
339
|
+
if (this.n instanceof Constant && this.d instanceof Constant) {
|
|
340
|
+
const NUMERATOR = this.n.value;
|
|
341
|
+
const DENOMINATOR = this.d.value;
|
|
342
|
+
const MODULO = NUMERATOR % DENOMINATOR;
|
|
343
|
+
this.isAtomic = MODULO !== 0 && gcd(NUMERATOR, DENOMINATOR) === 1;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Aim to solve a fraction with constants numerator and denominator.
|
|
348
|
+
*
|
|
349
|
+
* @param {MathNode} n The numerator.
|
|
350
|
+
* @param {MathNode} d The denominator.
|
|
351
|
+
* @returns {MathNode} The value as a math not.
|
|
352
|
+
*/
|
|
353
|
+
solveForConstant(n, d) {
|
|
354
|
+
const NUMERATOR = n.value;
|
|
355
|
+
const DENOMINATOR = d.value;
|
|
356
|
+
const QUOTIENT = NUMERATOR / DENOMINATOR;
|
|
357
|
+
if (QUOTIENT === Math.floor(QUOTIENT)) return new Constant(QUOTIENT);
|
|
358
|
+
const GCD = gcd(NUMERATOR, DENOMINATOR);
|
|
359
|
+
if (GCD > 1) return new Fraction(new Divide(n, new Constant(GCD)), new Divide(d, new Constant(GCD)));
|
|
360
|
+
return new Fraction(n, d);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* @inheritDoc
|
|
364
|
+
*/
|
|
365
|
+
next() {
|
|
366
|
+
if (this.n instanceof Constant && this.d instanceof Constant) return this.solveForConstant(this.n, this.d);
|
|
367
|
+
return new Fraction(this.n.next(), this.d.next());
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The functions find a common denominator for 2 fractions.
|
|
371
|
+
* Useful when you want to add 2 fractions with different denominators.
|
|
372
|
+
*
|
|
373
|
+
* @param {Fraction} fractionA A fraction.
|
|
374
|
+
* @param {Fraction} fractionB A fraction.
|
|
375
|
+
* @returns {Fraction[]} An array of both fractions with new denominators.
|
|
376
|
+
*/
|
|
377
|
+
solveDenominatorForConstants(fractionA, fractionB) {
|
|
378
|
+
if (fractionA.d instanceof Constant && fractionB.d instanceof Constant) {
|
|
379
|
+
const DENOMINATOR_A = fractionA.d.value;
|
|
380
|
+
const DENOMINATOR_B = fractionB.d.value;
|
|
381
|
+
const LCM = lcm(DENOMINATOR_A, DENOMINATOR_B);
|
|
382
|
+
const COMMON_A = LCM / DENOMINATOR_A;
|
|
383
|
+
const COMMON_B = LCM / DENOMINATOR_B;
|
|
384
|
+
const numeratorA = COMMON_A === 1 ? fractionA.n : new Multiply(fractionA.n, new Constant(COMMON_A));
|
|
385
|
+
const numeratorB = COMMON_B === 1 ? fractionB.n : new Multiply(fractionB.n, new Constant(COMMON_B));
|
|
386
|
+
const denominatorA = COMMON_A === 1 ? new Constant(DENOMINATOR_A) : new Multiply(new Constant(DENOMINATOR_A), new Constant(COMMON_A));
|
|
387
|
+
const denominatorB = COMMON_B === 1 ? new Constant(DENOMINATOR_B) : new Multiply(new Constant(DENOMINATOR_B), new Constant(COMMON_B));
|
|
388
|
+
return [new Fraction(numeratorA, denominatorA), new Fraction(numeratorB, denominatorB)];
|
|
389
|
+
}
|
|
390
|
+
return [fractionA, fractionB];
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* The next operands when the operation can't be applied to this fraction yet.
|
|
394
|
+
* The other operand is solved first, this fraction is only stepped (eg: reduced) once the other operand is atomic.
|
|
395
|
+
*
|
|
396
|
+
* @param {MathNode} argument The other operand.
|
|
397
|
+
* @returns {MathNode[]} The next [left, right] operands.
|
|
398
|
+
*/
|
|
399
|
+
nextOperands(argument) {
|
|
400
|
+
if (!argument.isAtomic) return [this, argument.next()];
|
|
401
|
+
return [this.next(), argument];
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* This function override the addition operator for fractions.
|
|
405
|
+
*
|
|
406
|
+
* @param {Constant | Fraction} argument A math node you want to add to the fraction.
|
|
407
|
+
* @returns {MathNode} A new math node.
|
|
408
|
+
*/
|
|
409
|
+
add(argument) {
|
|
410
|
+
if (!this.n.isAtomic || !this.d.isAtomic) return new Add(new Fraction(this.n.next(), this.d.next()), argument.isAtomic ? argument : argument.next());
|
|
411
|
+
if (argument instanceof Constant) {
|
|
412
|
+
if (this.n instanceof Constant && this.d instanceof Constant) {
|
|
413
|
+
const next = this.solveForConstant(this.n, this.d);
|
|
414
|
+
if (next instanceof Constant) return new Add(next, argument);
|
|
415
|
+
}
|
|
416
|
+
return new Add(this, argument.toFraction());
|
|
417
|
+
} else if (this.d instanceof Constant && argument.d instanceof Constant) {
|
|
418
|
+
if (this.d.value === argument.d.value) {
|
|
419
|
+
const addNumerator = new Add(this.n, argument.n);
|
|
420
|
+
return new Fraction(addNumerator, this.d);
|
|
421
|
+
} else {
|
|
422
|
+
const [fractionA, fractionB] = this.solveDenominatorForConstants(this, argument);
|
|
423
|
+
return new Add(fractionA, fractionB);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return new Add(...this.nextOperands(argument));
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* This function override the subtraction operator for fractions.
|
|
430
|
+
*
|
|
431
|
+
* @param {Constant | Fraction} argument A math node you want to subtract to the current fraction.
|
|
432
|
+
* @returns {MathNode} A new math node.
|
|
433
|
+
*/
|
|
434
|
+
subtract(argument) {
|
|
435
|
+
if (!this.n.isAtomic || !this.d.isAtomic) return new Subtract(new Fraction(this.n.next(), this.d.next()), argument.isAtomic ? argument : argument.next());
|
|
436
|
+
if (argument instanceof Constant) {
|
|
437
|
+
if (this.n instanceof Constant && this.d instanceof Constant) {
|
|
438
|
+
const next = this.solveForConstant(this.n, this.d);
|
|
439
|
+
if (next instanceof Constant) return new Subtract(next, argument);
|
|
440
|
+
}
|
|
441
|
+
return new Subtract(this, argument.toFraction());
|
|
442
|
+
} else if (this.d instanceof Constant && argument.d instanceof Constant) {
|
|
443
|
+
if (this.d.value === argument.d.value) {
|
|
444
|
+
const subtractNumerator = new Subtract(this.n, argument.n);
|
|
445
|
+
return new Fraction(subtractNumerator, this.d);
|
|
446
|
+
} else {
|
|
447
|
+
const [fractionA, fractionB] = this.solveDenominatorForConstants(this, argument);
|
|
448
|
+
return new Subtract(fractionA, fractionB);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return new Subtract(...this.nextOperands(argument));
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* This function override the multiplication for fractions.
|
|
455
|
+
*
|
|
456
|
+
* @param {Constant | Fraction} argument A math node you want to multiply by.
|
|
457
|
+
* @returns {MathNode} A new math node.
|
|
458
|
+
*/
|
|
459
|
+
multiply(argument) {
|
|
460
|
+
if (!this.n.isAtomic || !this.d.isAtomic) return new Multiply(new Fraction(this.n.next(), this.d.next()), argument.isAtomic ? argument : argument.next());
|
|
461
|
+
if (argument instanceof Constant) {
|
|
462
|
+
if (this.n instanceof Constant && this.d instanceof Constant) {
|
|
463
|
+
const next = this.solveForConstant(this.n, this.d);
|
|
464
|
+
if (next instanceof Constant) return new Multiply(next, argument);
|
|
465
|
+
}
|
|
466
|
+
return new Fraction(new Multiply(this.n, argument), new Multiply(this.d, argument));
|
|
467
|
+
}
|
|
468
|
+
if (this.d && argument && argument.d) return new Fraction(new Multiply(this.n, argument.n), new Multiply(this.d, argument.d));
|
|
469
|
+
return new Multiply(...this.nextOperands(argument));
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* This function override the division for fractions.
|
|
473
|
+
*
|
|
474
|
+
* @param {Constant | Fraction} argument A math node you want to divide by.
|
|
475
|
+
* @returns {MathNode} A new math node.
|
|
476
|
+
*/
|
|
477
|
+
divide(argument) {
|
|
478
|
+
if (!this.n.isAtomic || !this.d.isAtomic) return new Divide(new Fraction(this.n.next(), this.d.next()), argument.isAtomic ? argument : argument.next());
|
|
479
|
+
if (argument instanceof Constant) argument = argument.toFraction();
|
|
480
|
+
return new Multiply(this, new Fraction(argument.d, argument.n));
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Return the fraction numerator.
|
|
484
|
+
*
|
|
485
|
+
* @returns {MathNode} The numerator.
|
|
486
|
+
*/
|
|
487
|
+
getNumerator() {
|
|
488
|
+
return this.n;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Return the fraction denominator.
|
|
492
|
+
*
|
|
493
|
+
* @returns {MathNode} The denominator.
|
|
494
|
+
*/
|
|
495
|
+
getDenominator() {
|
|
496
|
+
return this.d;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* @inheritDoc
|
|
500
|
+
*/
|
|
501
|
+
toFraction() {
|
|
502
|
+
return this;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* @inheritDoc
|
|
506
|
+
*/
|
|
507
|
+
toJson() {
|
|
508
|
+
return {
|
|
509
|
+
type: this.type,
|
|
510
|
+
numerator: this.n.toJson(),
|
|
511
|
+
denominator: this.d.toJson()
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* @inheritDoc
|
|
516
|
+
*/
|
|
517
|
+
toString() {
|
|
518
|
+
return `{${this.n.toString()}} / {${this.d.toString()}}`;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* @inheritDoc
|
|
522
|
+
*/
|
|
523
|
+
toTex() {
|
|
524
|
+
return `\\frac{${this.n.toTex()}}{${this.d.toTex()}}`;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* @inheritDoc
|
|
528
|
+
*/
|
|
529
|
+
isEqual(mathNode) {
|
|
530
|
+
return mathNode instanceof Fraction && this.type === mathNode.type && this.n.isEqual(mathNode.n) && this.d.isEqual(mathNode.d);
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
// src/math-node/Constant.ts
|
|
534
|
+
/**
|
|
535
|
+
* Math node for representing a constant value.
|
|
536
|
+
*/
|
|
537
|
+
var Constant = class Constant extends MathNode {
|
|
538
|
+
/**
|
|
539
|
+
* @param {number} value The value of the constant.
|
|
540
|
+
*/
|
|
541
|
+
constructor(value) {
|
|
542
|
+
super();
|
|
543
|
+
this.type = "Constant";
|
|
544
|
+
this.isAtomic = true;
|
|
545
|
+
this.value = value;
|
|
546
|
+
/**
|
|
547
|
+
* When value === -0 set value to 0.
|
|
548
|
+
*/
|
|
549
|
+
if (value === 0) this.value = 0;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* @inheritDoc
|
|
553
|
+
*/
|
|
554
|
+
next() {
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* @inheritDoc
|
|
559
|
+
*/
|
|
560
|
+
toJson() {
|
|
561
|
+
return {
|
|
562
|
+
type: this.type,
|
|
563
|
+
value: this.value
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* @inheritDoc
|
|
568
|
+
*/
|
|
569
|
+
toString(data) {
|
|
570
|
+
if (data?.isAfterOperator && this.value < 0) return `(${this.value})`;
|
|
571
|
+
return `${this.value}`;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* @inheritDoc
|
|
575
|
+
*/
|
|
576
|
+
toTex(data) {
|
|
577
|
+
return this.toString(data);
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* @inheritDoc
|
|
581
|
+
*/
|
|
582
|
+
toFraction() {
|
|
583
|
+
return new Fraction(this, new Constant(1));
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* @inheritDoc
|
|
587
|
+
*/
|
|
588
|
+
isEqual(mathNode) {
|
|
589
|
+
return mathNode instanceof Constant && this.type === mathNode.type && this.value === mathNode.value;
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
// src/math-node/Add.ts
|
|
593
|
+
/**
|
|
594
|
+
* Represent the Addition operation as a math node.
|
|
595
|
+
*/
|
|
596
|
+
var Add = class Add extends BaseOperation {
|
|
597
|
+
/**
|
|
598
|
+
* @param {MathNode} left - The left node.
|
|
599
|
+
* @param {MathNode} right - The right node.
|
|
600
|
+
*/
|
|
601
|
+
constructor(left, right) {
|
|
602
|
+
super(left, right, "Add", "+");
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* @inheritDoc
|
|
606
|
+
*/
|
|
607
|
+
concreteNext() {
|
|
608
|
+
if (!this.left.isAtomic || !this.right.isAtomic) {
|
|
609
|
+
const [leftNode, rightNode] = super.getConstantsFromParenthesis();
|
|
610
|
+
if (leftNode instanceof Constant && rightNode instanceof Constant) {
|
|
611
|
+
if (rightNode.value < 0) return new Subtract(leftNode, new Constant(Math.abs(rightNode.value)));
|
|
612
|
+
return new Constant(leftNode.value + rightNode.value);
|
|
613
|
+
}
|
|
614
|
+
return new Add(this.left.next(), this.right.next());
|
|
615
|
+
}
|
|
616
|
+
return this;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Return the addition of left and right.
|
|
620
|
+
*
|
|
621
|
+
* @param {number} left The left operand.
|
|
622
|
+
* @param {number} right The right operand.
|
|
623
|
+
* @returns {number} The addition result.
|
|
624
|
+
*/
|
|
625
|
+
operation(left, right) {
|
|
626
|
+
return left + right;
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
// src/math-node/Exponent.ts
|
|
630
|
+
/**
|
|
631
|
+
* Represent the exponentiation operation as a math node.
|
|
632
|
+
*/
|
|
633
|
+
var Exponent = class Exponent extends MathNode {
|
|
634
|
+
/**
|
|
635
|
+
* @param {MathNode} base The base.
|
|
636
|
+
* @param {MathNode} exponent The exponent.
|
|
637
|
+
*/
|
|
638
|
+
constructor(base, exponent) {
|
|
639
|
+
super();
|
|
640
|
+
this.type = "Exponent";
|
|
641
|
+
this.isAtomic = false;
|
|
642
|
+
this.base = base;
|
|
643
|
+
this.exponent = exponent;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* @inheritDoc
|
|
647
|
+
*/
|
|
648
|
+
next() {
|
|
649
|
+
if (this.base instanceof Constant && this.exponent instanceof Constant) return new Constant(this.base.value ** this.exponent.value);
|
|
650
|
+
else if (!this.base.isAtomic || !this.exponent.isAtomic) return new Exponent(this.base.next(), this.exponent.next());
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* @inheritDoc
|
|
655
|
+
*/
|
|
656
|
+
toJson() {
|
|
657
|
+
return {
|
|
658
|
+
type: this.type,
|
|
659
|
+
base: this.base.toJson(),
|
|
660
|
+
exponent: this.exponent.toJson()
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* @inheritDoc
|
|
665
|
+
*/
|
|
666
|
+
toString(data) {
|
|
667
|
+
return `${this.base.toString({ isAfterOperator: data?.isAfterOperator })} ^ {${this.exponent.toString()}}`;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* @inheritDoc
|
|
671
|
+
*/
|
|
672
|
+
toTex(data) {
|
|
673
|
+
return `${this.base.toTex({ isAfterOperator: data?.isAfterOperator })}^{${this.exponent.toTex()}}`;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* @inheritDoc
|
|
677
|
+
*/
|
|
678
|
+
isEqual(mathNode) {
|
|
679
|
+
return mathNode instanceof Exponent && this.type === mathNode.type && this.base.isEqual(mathNode.base) && this.exponent.isEqual(mathNode.exponent);
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
// src/math-node/Equal.ts
|
|
683
|
+
/**
|
|
684
|
+
* Represent fractions as math node.
|
|
685
|
+
*/
|
|
686
|
+
var Equal = class Equal extends MathNode {
|
|
687
|
+
/**
|
|
688
|
+
* @param {MathNode} left The left operands.
|
|
689
|
+
* @param {MathNode} right The right operands.
|
|
690
|
+
*/
|
|
691
|
+
constructor(left, right) {
|
|
692
|
+
super();
|
|
693
|
+
this.left = left;
|
|
694
|
+
this.right = right;
|
|
695
|
+
this.type = "Equal";
|
|
696
|
+
this.isAtomic = false;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* @inheritDoc
|
|
700
|
+
*/
|
|
701
|
+
next() {
|
|
702
|
+
return this;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* @inheritDoc
|
|
706
|
+
*/
|
|
707
|
+
toFraction() {
|
|
708
|
+
return this;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* @inheritDoc
|
|
712
|
+
*/
|
|
713
|
+
toJson() {
|
|
714
|
+
return {
|
|
715
|
+
type: this.type,
|
|
716
|
+
left: this.left.toJson(),
|
|
717
|
+
right: this.right.toJson()
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* @inheritDoc
|
|
722
|
+
*/
|
|
723
|
+
toString() {
|
|
724
|
+
return `${this.left.toString()} = ${this.right.toString()}`;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* @inheritDoc
|
|
728
|
+
*/
|
|
729
|
+
toTex() {
|
|
730
|
+
return `${this.left.toTex()} = ${this.right.toTex()}`;
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* @inheritDoc
|
|
734
|
+
*/
|
|
735
|
+
isEqual(mathNode) {
|
|
736
|
+
return mathNode instanceof Equal && this.type === mathNode.type && this.left.isEqual(mathNode.left) && this.right.isEqual(mathNode.right);
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
// src/math-node/Negative.ts
|
|
740
|
+
/**
|
|
741
|
+
* Represent the Negative operation as a math node.
|
|
742
|
+
*/
|
|
743
|
+
var Negative = class Negative extends MathNode {
|
|
744
|
+
/**
|
|
745
|
+
* @param {MathNode} content - The negative node.
|
|
746
|
+
*/
|
|
747
|
+
constructor(content) {
|
|
748
|
+
super();
|
|
749
|
+
this.type = "Negative";
|
|
750
|
+
this.isAtomic = false;
|
|
751
|
+
this.content = content;
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* @inheritDoc
|
|
755
|
+
*/
|
|
756
|
+
next() {
|
|
757
|
+
if (this.content instanceof Constant) return new Constant(-this.content.value);
|
|
758
|
+
const next = this.content.next({ isNegative: true });
|
|
759
|
+
if (next instanceof Constant) return new Constant(-next.value);
|
|
760
|
+
return new Negative(next);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* @inheritDoc
|
|
764
|
+
*/
|
|
765
|
+
toJson() {
|
|
766
|
+
return {
|
|
767
|
+
type: this.type,
|
|
768
|
+
content: this.content.toJson()
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* @inheritDoc
|
|
773
|
+
*/
|
|
774
|
+
toString() {
|
|
775
|
+
return `-${this.content.toString()}`;
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* @inheritDoc
|
|
779
|
+
*/
|
|
780
|
+
toTex() {
|
|
781
|
+
return `-${this.content.toTex()}`;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* @inheritDoc
|
|
785
|
+
*/
|
|
786
|
+
isEqual(mathNode) {
|
|
787
|
+
return mathNode instanceof Negative && this.type === mathNode.type && this.content.isEqual(mathNode.content);
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
// src/math-node/Parenthesis.ts
|
|
791
|
+
/**
|
|
792
|
+
* Represent the parenthesis as a math node.
|
|
793
|
+
*/
|
|
794
|
+
var Parenthesis = class Parenthesis extends MathNode {
|
|
795
|
+
/**
|
|
796
|
+
* @param {MathNode} content The content of the parenthesis.
|
|
797
|
+
*/
|
|
798
|
+
constructor(content) {
|
|
799
|
+
super();
|
|
800
|
+
this.type = "Parenthesis";
|
|
801
|
+
this.isAtomic = false;
|
|
802
|
+
this.content = content;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* @inheritDoc
|
|
806
|
+
*/
|
|
807
|
+
next(args) {
|
|
808
|
+
const isNegative = args?.isNegative ?? false;
|
|
809
|
+
if (this.content.type === "Constant") return this.content;
|
|
810
|
+
else {
|
|
811
|
+
const solvedParenthesis = this.content.next();
|
|
812
|
+
if (solvedParenthesis instanceof Constant && !isNegative || solvedParenthesis instanceof Fraction) return solvedParenthesis;
|
|
813
|
+
return new Parenthesis(solvedParenthesis);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* @inheritDoc
|
|
818
|
+
*/
|
|
819
|
+
toJson() {
|
|
820
|
+
return {
|
|
821
|
+
type: this.type,
|
|
822
|
+
content: this.content.toJson()
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* @inheritDoc
|
|
827
|
+
*/
|
|
828
|
+
toString() {
|
|
829
|
+
return `(${this.content.toString()})`;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* @inheritDoc
|
|
833
|
+
*/
|
|
834
|
+
toTex() {
|
|
835
|
+
return `(${this.content.toTex()})`;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* @inheritDoc
|
|
839
|
+
*/
|
|
840
|
+
isEqual(mathNode) {
|
|
841
|
+
return mathNode instanceof Parenthesis && this.type === mathNode.type && this.content.isEqual(mathNode.content);
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
// src/math-node/Variable.ts
|
|
845
|
+
/**
|
|
846
|
+
* Represent fractions as math node.
|
|
847
|
+
*/
|
|
848
|
+
var Variable = class Variable extends MathNode {
|
|
849
|
+
/**
|
|
850
|
+
* @param {string} name The variable name.
|
|
851
|
+
*/
|
|
852
|
+
constructor(name) {
|
|
853
|
+
super();
|
|
854
|
+
this.name = name;
|
|
855
|
+
this.type = "Variable";
|
|
856
|
+
this.isAtomic = false;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* @inheritDoc
|
|
860
|
+
*/
|
|
861
|
+
next() {
|
|
862
|
+
return this;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* @inheritDoc
|
|
866
|
+
*/
|
|
867
|
+
toFraction() {
|
|
868
|
+
return new Fraction(new Constant(1), this);
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* @inheritDoc
|
|
872
|
+
*/
|
|
873
|
+
toJson() {
|
|
874
|
+
return {
|
|
875
|
+
type: this.type,
|
|
876
|
+
name: this.name
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* @inheritDoc
|
|
881
|
+
*/
|
|
882
|
+
toString() {
|
|
883
|
+
return `${this.name}`;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* @inheritDoc
|
|
887
|
+
*/
|
|
888
|
+
toTex() {
|
|
889
|
+
return `${this.name}`;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* @inheritDoc
|
|
893
|
+
*/
|
|
894
|
+
isEqual(mathNode) {
|
|
895
|
+
return mathNode instanceof Variable && this.type === mathNode.type && this.name === mathNode.name;
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
// src/parser/utils.ts
|
|
899
|
+
const isNumber = (n) => !isNaN(Number(n));
|
|
900
|
+
const isOperator = (c) => {
|
|
901
|
+
return [
|
|
902
|
+
"+",
|
|
903
|
+
"-",
|
|
904
|
+
"*",
|
|
905
|
+
":",
|
|
906
|
+
"/",
|
|
907
|
+
"^",
|
|
908
|
+
"="
|
|
909
|
+
].indexOf(c) !== -1;
|
|
910
|
+
};
|
|
911
|
+
const isInParenthesis = (symbols) => {
|
|
912
|
+
const first = symbols[0];
|
|
913
|
+
const last = symbols[symbols.length - 1];
|
|
914
|
+
const HAS_PARENTHESIS = first === "(" && last === ")";
|
|
915
|
+
let numberOfParenthesis = 0;
|
|
916
|
+
let depth = 0;
|
|
917
|
+
for (let i = 0; i < symbols.length && HAS_PARENTHESIS; i++) {
|
|
918
|
+
let char = symbols[i];
|
|
919
|
+
if (char === "(") {
|
|
920
|
+
if (depth === 0) numberOfParenthesis++;
|
|
921
|
+
depth++;
|
|
922
|
+
}
|
|
923
|
+
if (char === ")") depth--;
|
|
924
|
+
}
|
|
925
|
+
return numberOfParenthesis === 1;
|
|
926
|
+
};
|
|
927
|
+
const isInBracket = (symbols) => {
|
|
928
|
+
const first = symbols[0];
|
|
929
|
+
const last = symbols[symbols.length - 1];
|
|
930
|
+
const HAS_BRACKET = first === "{" && last === "}";
|
|
931
|
+
let numberOfBracket = 0;
|
|
932
|
+
let depth = 0;
|
|
933
|
+
for (let i = 0; i < symbols.length && HAS_BRACKET; i++) {
|
|
934
|
+
let char = symbols[i];
|
|
935
|
+
if (char === "{") {
|
|
936
|
+
if (depth === 0) numberOfBracket++;
|
|
937
|
+
depth++;
|
|
938
|
+
}
|
|
939
|
+
if (char === "}") depth--;
|
|
940
|
+
}
|
|
941
|
+
return numberOfBracket === 1;
|
|
942
|
+
};
|
|
943
|
+
const getParenthesisContent = (symbols) => {
|
|
944
|
+
return symbols.slice(1, symbols.length - 1);
|
|
945
|
+
};
|
|
946
|
+
const splitStringExpressionToSymbols = (expression) => {
|
|
947
|
+
expression = expression.split("").filter((c) => c !== " ").join("");
|
|
948
|
+
let symbols = [];
|
|
949
|
+
for (let i = 0; i < expression.length; i++) {
|
|
950
|
+
const symbolN1 = symbols[symbols.length - 1];
|
|
951
|
+
const char = expression[i];
|
|
952
|
+
if (symbolN1 && isNumber(symbolN1) && isNumber(char)) symbols[symbols.length - 1] = symbolN1 + char;
|
|
953
|
+
else symbols.push(expression[i]);
|
|
954
|
+
}
|
|
955
|
+
return symbols;
|
|
956
|
+
};
|
|
957
|
+
// src/parser/normalisation.ts
|
|
958
|
+
const findNextNumberIndex = (symbols, startIndex = 0) => {
|
|
959
|
+
let nextNumberIndex = -1;
|
|
960
|
+
for (let i = startIndex; i < symbols.length && nextNumberIndex === -1; i++) if (isNumber(symbols[i])) nextNumberIndex = i;
|
|
961
|
+
return nextNumberIndex;
|
|
962
|
+
};
|
|
963
|
+
/**
|
|
964
|
+
* Surround with bracket negative numbers
|
|
965
|
+
* If at the beginning of the expression ex: "-2 +3" becomes "{-2} + 3"
|
|
966
|
+
* If after operator ex: "2 * -3" becomes "2 * {-3}"
|
|
967
|
+
* Except in front of parenthesis
|
|
968
|
+
* @param {string[]} arr An array of symbols
|
|
969
|
+
*/
|
|
970
|
+
const normalizeNegativeNumbers = (arr) => {
|
|
971
|
+
let symbols = [...arr];
|
|
972
|
+
for (let i = 0; i < symbols.length; i++) {
|
|
973
|
+
const symbol = symbols[i];
|
|
974
|
+
const lastSymbol = symbols[i - 1];
|
|
975
|
+
const nextSymbol = symbols[i + 1];
|
|
976
|
+
if (symbol === "-") {
|
|
977
|
+
if (isOperator(lastSymbol) && !(nextSymbol && nextSymbol === "(")) {
|
|
978
|
+
const left = symbols.slice(0, i);
|
|
979
|
+
const NEXT_NUMBER_INDEX = findNextNumberIndex(symbols, i);
|
|
980
|
+
let mid = symbols.slice(i, NEXT_NUMBER_INDEX + 1);
|
|
981
|
+
mid = normalizeNegativeNumbers(mid);
|
|
982
|
+
if (mid.length === 2 && mid[0] === "-" && isNumber(mid[1])) mid = [mid.join("")];
|
|
983
|
+
const right = symbols.slice(NEXT_NUMBER_INDEX + 1, symbols.length);
|
|
984
|
+
symbols = [
|
|
985
|
+
...left,
|
|
986
|
+
"(",
|
|
987
|
+
...mid,
|
|
988
|
+
")",
|
|
989
|
+
...right
|
|
990
|
+
];
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return symbols;
|
|
995
|
+
};
|
|
996
|
+
/**
|
|
997
|
+
* Given the following expression
|
|
998
|
+
* 1 + 2 / 3 + 4
|
|
999
|
+
* It should return 1 + {2} / {3} + 4
|
|
1000
|
+
* @param expression
|
|
1001
|
+
*/
|
|
1002
|
+
const normalizeFractions = (symbols) => {
|
|
1003
|
+
return symbols;
|
|
1004
|
+
};
|
|
1005
|
+
/**
|
|
1006
|
+
* Given the following expression
|
|
1007
|
+
* 1 + 2 ^ 3 + 4
|
|
1008
|
+
* It should return 1 + 2 ^ {3} + 4
|
|
1009
|
+
* @param expression
|
|
1010
|
+
*/
|
|
1011
|
+
const normalizeExponent = (symbols) => {
|
|
1012
|
+
return symbols;
|
|
1013
|
+
};
|
|
1014
|
+
const normalize = (symbols) => {
|
|
1015
|
+
return normalizeExponent(normalizeFractions(normalizeNegativeNumbers(symbols)));
|
|
1016
|
+
};
|
|
1017
|
+
// src/parser/parser.ts
|
|
1018
|
+
const alphaRegex = /* @__PURE__ */ new RegExp(/^[a-zA-Z]+$/);
|
|
1019
|
+
const getOperatorPriority = (operator) => {
|
|
1020
|
+
if (operator === "=") return 0;
|
|
1021
|
+
else if (operator === "+" || operator === "-") return 1;
|
|
1022
|
+
else if (operator === "*" || operator === ":") return 2;
|
|
1023
|
+
else if (operator === "^" || operator === "/") return 3;
|
|
1024
|
+
return 0;
|
|
1025
|
+
};
|
|
1026
|
+
const parseParenthesisAndBracket = (symbols) => {
|
|
1027
|
+
if (!symbols.length) return new Constant(NaN);
|
|
1028
|
+
if (isInParenthesis(symbols)) {
|
|
1029
|
+
const content = getParenthesisContent(symbols);
|
|
1030
|
+
return new Parenthesis(buildMathNode(content));
|
|
1031
|
+
}
|
|
1032
|
+
if (isInBracket(symbols)) {
|
|
1033
|
+
const content = getParenthesisContent(symbols);
|
|
1034
|
+
return buildMathNode(content);
|
|
1035
|
+
}
|
|
1036
|
+
return buildMathNode(symbols);
|
|
1037
|
+
};
|
|
1038
|
+
/**
|
|
1039
|
+
* Find the last operator with the lowest priority and return its index.
|
|
1040
|
+
*
|
|
1041
|
+
* - The function read each character from left to right;
|
|
1042
|
+
* - The function ignore parenthesis and bracket since they're parsed elsewhere;
|
|
1043
|
+
* - The function ignore negative numbers;
|
|
1044
|
+
*
|
|
1045
|
+
* Ex. ["2", "+", "6", "-", "3", "*" "2"]
|
|
1046
|
+
* The last lowest priority operator is "-" so the function will return 3
|
|
1047
|
+
*
|
|
1048
|
+
* @param {string[]} symbols a list of symbols
|
|
1049
|
+
* @return {number} the index of the operator with the lowest priority
|
|
1050
|
+
*/
|
|
1051
|
+
const findLastLowestPriorityOperator = (symbols) => {
|
|
1052
|
+
let lowestPriority = 3;
|
|
1053
|
+
let lastLowestPriorityIndex = -1;
|
|
1054
|
+
let depth = 0;
|
|
1055
|
+
for (let i = 0; i < symbols.length; i++) {
|
|
1056
|
+
const char = symbols[i];
|
|
1057
|
+
if (char === "(" || char === "{") depth++;
|
|
1058
|
+
if (char === ")" || char === "}") depth--;
|
|
1059
|
+
/**
|
|
1060
|
+
* Ignore the operators in the parenthesis since they are parsed later
|
|
1061
|
+
*/
|
|
1062
|
+
if (depth > 0) continue;
|
|
1063
|
+
if (isOperator(char)) {
|
|
1064
|
+
const OPERATOR_PRIORITY = getOperatorPriority(char);
|
|
1065
|
+
if (OPERATOR_PRIORITY <= lowestPriority) {
|
|
1066
|
+
lowestPriority = OPERATOR_PRIORITY;
|
|
1067
|
+
lastLowestPriorityIndex = i;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
return lastLowestPriorityIndex;
|
|
1072
|
+
};
|
|
1073
|
+
const buildMathNode = (symbols) => {
|
|
1074
|
+
if (symbols.length === 1) {
|
|
1075
|
+
if (isNumber(symbols[0])) return new Constant(parseFloat(symbols[0]));
|
|
1076
|
+
if (alphaRegex.test(symbols[0])) return new Variable(symbols[0]);
|
|
1077
|
+
}
|
|
1078
|
+
if (symbols.length === 2 && symbols[0] === "-" && isNumber(symbols[1])) return new Constant(parseFloat(`-${symbols[1]}`));
|
|
1079
|
+
if (isInParenthesis(symbols) || isInBracket(symbols)) return parseParenthesisAndBracket(symbols);
|
|
1080
|
+
const OPERATOR_INDEX = findLastLowestPriorityOperator(symbols);
|
|
1081
|
+
const operator = symbols[OPERATOR_INDEX];
|
|
1082
|
+
const leftString = symbols.slice(0, OPERATOR_INDEX);
|
|
1083
|
+
const rightString = symbols.slice(OPERATOR_INDEX + 1, symbols.length);
|
|
1084
|
+
const left = parseParenthesisAndBracket(leftString);
|
|
1085
|
+
const right = parseParenthesisAndBracket(rightString);
|
|
1086
|
+
return mathNodeFactory(operator, left, right);
|
|
1087
|
+
};
|
|
1088
|
+
/**
|
|
1089
|
+
* Parse a given mathematical expression and return a MathNode.
|
|
1090
|
+
*
|
|
1091
|
+
* Eg: parse("2 + 3 * 4") will produce the following object:
|
|
1092
|
+
*
|
|
1093
|
+
* new Add(new Constant(2), new Multiply(new Constant(3), new Constant(4))
|
|
1094
|
+
*
|
|
1095
|
+
* @see MathNode
|
|
1096
|
+
*
|
|
1097
|
+
* @param {string} expression A mathematical expression
|
|
1098
|
+
* @return {MathNode} a math node that represent the expression from the input
|
|
1099
|
+
*/
|
|
1100
|
+
const parse = (expression) => {
|
|
1101
|
+
const symbols = splitStringExpressionToSymbols(expression);
|
|
1102
|
+
if (isOperator(symbols[symbols.length - 1]) && isOperator(symbols[symbols.length - 2])) throw Error("Parse error");
|
|
1103
|
+
const normalized = normalize(symbols);
|
|
1104
|
+
return buildMathNode(normalized);
|
|
1105
|
+
};
|
|
1106
|
+
const mathNodeFactory = (operator, left, right) => {
|
|
1107
|
+
if (left.value !== left.value) {
|
|
1108
|
+
if (operator === "-") return new Negative(right);
|
|
1109
|
+
}
|
|
1110
|
+
if (operator === "+") return new Add(left, right);
|
|
1111
|
+
else if (operator === "-") return new Subtract(left, right);
|
|
1112
|
+
else if (operator === "*") return new Multiply(left, right);
|
|
1113
|
+
else if (operator === ":") return new Divide(left, right);
|
|
1114
|
+
else if (operator === "/") return new Fraction(left, right);
|
|
1115
|
+
else if (operator === "^") return new Exponent(left, right);
|
|
1116
|
+
else if (operator === "=") return new Equal(left, right);
|
|
1117
|
+
return new Constant(NaN);
|
|
1118
|
+
};
|
|
1119
|
+
// src/parser/index.ts
|
|
1120
|
+
const solve = (expression) => {
|
|
1121
|
+
return parse(expression).solve();
|
|
1122
|
+
};
|
|
1123
|
+
const evaluate = (expression) => {
|
|
1124
|
+
return parse(expression).evaluate();
|
|
1125
|
+
};
|
|
1126
|
+
export { Add, Constant, Divide, Equal, Exponent, Fraction, MathNode, MathNodeType, Multiply, Negative, Parenthesis, Subtract, Variable, evaluate, parse, solve };
|