@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.
@@ -0,0 +1,635 @@
1
+ interface ToStringParam {
2
+ isAfterOperator?: boolean;
3
+ }
4
+ export declare enum MathNodeType {
5
+ Add = "Add",
6
+ Constant = "Constant",
7
+ Divide = "Divide",
8
+ Equal = "Equal",
9
+ Fraction = "Fraction",
10
+ Exponent = "Exponent",
11
+ Multiply = "Multiply",
12
+ Negative = "Negative",
13
+ Parenthesis = "Parenthesis",
14
+ Subtract = "Subtract",
15
+ Variable = "Variable"
16
+ }
17
+ /**
18
+ * Base class of every math node.
19
+ */
20
+ declare abstract class MathNode {
21
+ /**
22
+ * Represent the value of a constant as a number.
23
+ * If the node is not a constant, then value is always null.
24
+ */
25
+ value: number | null;
26
+ override?: (type: MathNodeType) => (((...args: any) => MathNode) | undefined);
27
+ /**
28
+ * The type of the node
29
+ * Check MathNodeType for all available nodes.
30
+ */
31
+ abstract type: MathNodeType;
32
+ /**
33
+ * Tell if an expression can be simplified or not.
34
+ *
35
+ * If isAtomic === true, the expression can't be simplified.
36
+ * For example a constant is atomic
37
+ * Calling the next() function will return the same instance.
38
+ *
39
+ * If isAtomic === false, the expression can be simplified.
40
+ * For example "4 + 3" is not atomic and can be simplified in "7"
41
+ * Calling the next() function will simplify the expression, or return the next step.
42
+ */
43
+ abstract isAtomic: boolean;
44
+ /**
45
+ * The next function will return the node of the next step.
46
+ *
47
+ * - Each operation should implement its own logic to get the next step.
48
+ * - The next operation should simplify the deepest node from the binary tree.
49
+ * - Eg: 'new Add(new Constant(2) , new Constant(3)).next()' will become 'new Constant(5)'.
50
+ */
51
+ abstract next(args?: {
52
+ isNegative?: boolean;
53
+ }): MathNode;
54
+ /**
55
+ * Return a JSON representation of the node.
56
+ *
57
+ * @returns {JSON}
58
+ */
59
+ abstract toJson(): Object;
60
+ /**
61
+ * Return the string of the node.
62
+ * The returned value will produce the same node if passed to the parser.
63
+ *
64
+ * @returns {string}
65
+ */
66
+ abstract toString(data?: ToStringParam): string;
67
+ /**
68
+ * Return a valid latex string of the current node.
69
+ * Tested with Mathjax & Katex.
70
+ *
71
+ * @returns {string} Valid latex as string.
72
+ */
73
+ abstract toTex(data?: ToStringParam): string;
74
+ /**
75
+ * Do a deep equal.
76
+ *
77
+ * @param {MathNode} mathNode A math node.
78
+ * @returns {boolean} Tell if is the nodes are equals.
79
+ */
80
+ abstract isEqual(mathNode: MathNode): boolean;
81
+ /**
82
+ * This function aims to solve the node step by step.
83
+ * It calls the next() function until the node is atomic.
84
+ *
85
+ * @returns {MathNode[]} - Each step.
86
+ */
87
+ solveAll(): MathNode[];
88
+ /**
89
+ * This function aims to solve the node step by step.
90
+ * It calls the next() function until the node is atomic.
91
+ *
92
+ * @returns {object[]} - Each step as a json.
93
+ */
94
+ solveAllToJson(): Object[];
95
+ /**
96
+ * This function aims to solve the node step by step.
97
+ * It calls the next() function until the node is atomic.
98
+ *
99
+ * @returns {string[]} - Each step as a string.
100
+ */
101
+ solveAllToString(): string[];
102
+ /**
103
+ * This function aims to solve the node step by step.
104
+ * It calls the next() function until the node is atomic.
105
+ *
106
+ * @returns {string[]} - Each step as valid latex string.
107
+ */
108
+ solveAllToTex(): string[];
109
+ /**
110
+ * Solve the node until it is atomic.
111
+ *
112
+ * @returns {MathNode} - The last node.
113
+ */
114
+ solve<D extends MathNode>(): D;
115
+ /**
116
+ * Solve the node until it is atomic and return its value.
117
+ *
118
+ * @returns {number | NaN} The value of the node.
119
+ */
120
+ evaluate(): number;
121
+ }
122
+ /**
123
+ * The base class for [addition, subtraction, multiplication, division].
124
+ */
125
+ declare abstract class BaseOperation extends MathNode {
126
+ type: MathNodeType;
127
+ isAtomic: boolean;
128
+ left: MathNode;
129
+ right: MathNode;
130
+ private readonly symbol;
131
+ private readonly texSymbol;
132
+ /**
133
+ * @param {MathNode} left - The left node.
134
+ * @param {MathNode} right - The right node.
135
+ * @param {MathNodeType} type - The node type.
136
+ * @param {string} symbol - The symbol of the operator.
137
+ * @param {string} texSymbol - The LaTeX symbol of the operator, defaults to `symbol`.
138
+ */
139
+ constructor(left: MathNode, right: MathNode, type: MathNodeType, symbol: string, texSymbol?: string);
140
+ /**
141
+ * @inheritDoc
142
+ */
143
+ toJson(): {
144
+ type: MathNodeType;
145
+ left: Object;
146
+ right: Object;
147
+ };
148
+ /**
149
+ * @inheritDoc
150
+ */
151
+ toString(data?: ToStringParam): string;
152
+ /**
153
+ * @inheritDoc
154
+ */
155
+ toTex(data?: ToStringParam): string;
156
+ /**
157
+ * @inheritDoc
158
+ */
159
+ isEqual(mathNode: MathNode): boolean;
160
+ /**
161
+ * A shared logic for all base operations.
162
+ *
163
+ * @returns {MathNode} The next step.
164
+ */
165
+ next(): MathNode;
166
+ /**
167
+ * Check if the left and right operands are constants.
168
+ * If the operands are parenthesis containing a constant then it returns the constants from the parenthesis.
169
+ *
170
+ * @returns {MathNode[]} An array of operands [leftNode, rightNode].
171
+ */
172
+ protected getConstantsFromParenthesis(): MathNode[];
173
+ /**
174
+ * A more concrete implementation of the next method
175
+ * that should be defined in concrete classes.
176
+ *
177
+ * @protected
178
+ */
179
+ protected abstract concreteNext(): MathNode;
180
+ /**
181
+ * Each subclass should implement its own operation.
182
+ *
183
+ * @param {number} left
184
+ * @param {number} right
185
+ */
186
+ abstract operation(left: number, right: number): number;
187
+ }
188
+ /**
189
+ * Represent the Addition operation as a math node.
190
+ */
191
+ declare class Add extends BaseOperation {
192
+ /**
193
+ * @param {MathNode} left - The left node.
194
+ * @param {MathNode} right - The right node.
195
+ */
196
+ constructor(left: MathNode, right: MathNode);
197
+ /**
198
+ * @inheritDoc
199
+ */
200
+ concreteNext(): MathNode;
201
+ /**
202
+ * Return the addition of left and right.
203
+ *
204
+ * @param {number} left The left operand.
205
+ * @param {number} right The right operand.
206
+ * @returns {number} The addition result.
207
+ */
208
+ operation(left: number, right: number): number;
209
+ }
210
+ interface ToFraction {
211
+ toFraction: () => Fraction;
212
+ }
213
+ /**
214
+ * Represent fractions as math node.
215
+ */
216
+ declare class Fraction extends MathNode implements ToFraction {
217
+ type: MathNodeType;
218
+ /**
219
+ * A fraction is considered as atomic if it is irreducible and not a whole number eg: 5 / 3.
220
+ */
221
+ isAtomic: boolean;
222
+ private readonly n;
223
+ private readonly d;
224
+ /**
225
+ * Override an operator.
226
+ *
227
+ * @param {MathNodeType} type The type of the operation.
228
+ * @returns {undefined | (type: MathNodeType) => MathNode} A function that override the operator.
229
+ */
230
+ override: (type: MathNodeType) => {
231
+ (constant: Constant): MathNode;
232
+ (fraction: Fraction): MathNode;
233
+ } | {
234
+ (constant: Constant): MathNode;
235
+ (fraction: Fraction): MathNode;
236
+ } | {
237
+ (constant: Constant): MathNode;
238
+ (fraction: Fraction): MathNode;
239
+ } | {
240
+ (constant: Constant): MathNode;
241
+ (fraction: Fraction): MathNode;
242
+ } | undefined;
243
+ /**
244
+ * @param {MathNode} n The numerator, (the number at the top).
245
+ * @param {MathNode} d The denominator, (the number on the bottom).
246
+ */
247
+ constructor(n: MathNode, d: MathNode);
248
+ /**
249
+ * Aim to solve a fraction with constants numerator and denominator.
250
+ *
251
+ * @param {MathNode} n The numerator.
252
+ * @param {MathNode} d The denominator.
253
+ * @returns {MathNode} The value as a math not.
254
+ */
255
+ private solveForConstant;
256
+ /**
257
+ * @inheritDoc
258
+ */
259
+ next(): MathNode;
260
+ /**
261
+ * The functions find a common denominator for 2 fractions.
262
+ * Useful when you want to add 2 fractions with different denominators.
263
+ *
264
+ * @param {Fraction} fractionA A fraction.
265
+ * @param {Fraction} fractionB A fraction.
266
+ * @returns {Fraction[]} An array of both fractions with new denominators.
267
+ */
268
+ private solveDenominatorForConstants;
269
+ /**
270
+ * The next operands when the operation can't be applied to this fraction yet.
271
+ * The other operand is solved first, this fraction is only stepped (eg: reduced) once the other operand is atomic.
272
+ *
273
+ * @param {MathNode} argument The other operand.
274
+ * @returns {MathNode[]} The next [left, right] operands.
275
+ */
276
+ private nextOperands;
277
+ add(constant: Constant): MathNode;
278
+ add(fraction: Fraction): MathNode;
279
+ subtract(constant: Constant): MathNode;
280
+ subtract(fraction: Fraction): MathNode;
281
+ multiply(constant: Constant): MathNode;
282
+ multiply(fraction: Fraction): MathNode;
283
+ divide(constant: Constant): MathNode;
284
+ divide(fraction: Fraction): MathNode;
285
+ /**
286
+ * Return the fraction numerator.
287
+ *
288
+ * @returns {MathNode} The numerator.
289
+ */
290
+ getNumerator(): MathNode;
291
+ /**
292
+ * Return the fraction denominator.
293
+ *
294
+ * @returns {MathNode} The denominator.
295
+ */
296
+ getDenominator(): MathNode;
297
+ /**
298
+ * @inheritDoc
299
+ */
300
+ toFraction(): this;
301
+ /**
302
+ * @inheritDoc
303
+ */
304
+ toJson(): {
305
+ type: MathNodeType;
306
+ numerator: Object;
307
+ denominator: Object;
308
+ };
309
+ /**
310
+ * @inheritDoc
311
+ */
312
+ toString(): string;
313
+ /**
314
+ * @inheritDoc
315
+ */
316
+ toTex(): string;
317
+ /**
318
+ * @inheritDoc
319
+ */
320
+ isEqual(mathNode: MathNode): boolean;
321
+ }
322
+ /**
323
+ * Math node for representing a constant value.
324
+ */
325
+ declare class Constant extends MathNode implements ToFraction {
326
+ type: MathNodeType;
327
+ isAtomic: boolean;
328
+ value: number;
329
+ /**
330
+ * @param {number} value The value of the constant.
331
+ */
332
+ constructor(value: number);
333
+ /**
334
+ * @inheritDoc
335
+ */
336
+ next(): this;
337
+ /**
338
+ * @inheritDoc
339
+ */
340
+ toJson(): {
341
+ type: MathNodeType;
342
+ value: number;
343
+ };
344
+ /**
345
+ * @inheritDoc
346
+ */
347
+ toString(data?: ToStringParam): string;
348
+ /**
349
+ * @inheritDoc
350
+ */
351
+ toTex(data?: ToStringParam): string;
352
+ /**
353
+ * @inheritDoc
354
+ */
355
+ toFraction(): Fraction;
356
+ /**
357
+ * @inheritDoc
358
+ */
359
+ isEqual(mathNode: MathNode): boolean;
360
+ }
361
+ /**
362
+ * Represent the division operation as a math node.
363
+ */
364
+ declare class Divide extends BaseOperation {
365
+ /**
366
+ * @param {MathNode} left The left node.
367
+ * @param {MathNode} right The right node.
368
+ */
369
+ constructor(left: MathNode, right: MathNode);
370
+ /**
371
+ * @inheritDoc
372
+ */
373
+ concreteNext(): MathNode;
374
+ /**
375
+ * Return the division between left and right.
376
+ *
377
+ * @param {number} left The left operand.
378
+ * @param {number} right The right operand.
379
+ * @returns {number} The division result.
380
+ */
381
+ operation(left: number, right: number): number;
382
+ }
383
+ /**
384
+ * Represent the exponentiation operation as a math node.
385
+ */
386
+ declare class Exponent extends MathNode {
387
+ type: MathNodeType;
388
+ base: MathNode;
389
+ exponent: MathNode;
390
+ isAtomic: boolean;
391
+ /**
392
+ * @param {MathNode} base The base.
393
+ * @param {MathNode} exponent The exponent.
394
+ */
395
+ constructor(base: MathNode, exponent: MathNode);
396
+ /**
397
+ * @inheritDoc
398
+ */
399
+ next(): Constant | Exponent;
400
+ /**
401
+ * @inheritDoc
402
+ */
403
+ toJson(): {
404
+ type: MathNodeType;
405
+ base: Object;
406
+ exponent: Object;
407
+ };
408
+ /**
409
+ * @inheritDoc
410
+ */
411
+ toString(data?: ToStringParam): string;
412
+ /**
413
+ * @inheritDoc
414
+ */
415
+ toTex(data?: ToStringParam): string;
416
+ /**
417
+ * @inheritDoc
418
+ */
419
+ isEqual(mathNode: MathNode): boolean;
420
+ }
421
+ /**
422
+ * Represent fractions as math node.
423
+ */
424
+ declare class Equal extends MathNode {
425
+ left: MathNode;
426
+ right: MathNode;
427
+ type: MathNodeType;
428
+ /**
429
+ * A fraction is considered as atomic if its results is a Floating number eg: 5 / 3.
430
+ */
431
+ isAtomic: boolean;
432
+ /**
433
+ * @param {MathNode} left The left operands.
434
+ * @param {MathNode} right The right operands.
435
+ */
436
+ constructor(left: MathNode, right: MathNode);
437
+ /**
438
+ * @inheritDoc
439
+ */
440
+ next(): MathNode;
441
+ /**
442
+ * @inheritDoc
443
+ */
444
+ toFraction(): this;
445
+ /**
446
+ * @inheritDoc
447
+ */
448
+ toJson(): {
449
+ type: MathNodeType;
450
+ left: Object;
451
+ right: Object;
452
+ };
453
+ /**
454
+ * @inheritDoc
455
+ */
456
+ toString(): string;
457
+ /**
458
+ * @inheritDoc
459
+ */
460
+ toTex(): string;
461
+ /**
462
+ * @inheritDoc
463
+ */
464
+ isEqual(mathNode: MathNode): boolean;
465
+ }
466
+ /**
467
+ * Represent the multiplication operation as a math node.
468
+ */
469
+ declare class Multiply extends BaseOperation {
470
+ /**
471
+ * @param {MathNode} left The left node.
472
+ * @param {MathNode} right The right node.
473
+ */
474
+ constructor(left: MathNode, right: MathNode);
475
+ /**
476
+ * @inheritDoc
477
+ */
478
+ concreteNext(): MathNode;
479
+ /**
480
+ * Return the multiplication of left and right.
481
+ *
482
+ * @param {number} left The left operand.
483
+ * @param {number} right The right operand.
484
+ * @returns {number} The multiplication result.
485
+ */
486
+ operation(left: number, right: number): number;
487
+ }
488
+ /**
489
+ * Represent the Negative operation as a math node.
490
+ */
491
+ declare class Negative extends MathNode {
492
+ type: MathNodeType;
493
+ isAtomic: boolean;
494
+ content: MathNode;
495
+ /**
496
+ * @param {MathNode} content - The negative node.
497
+ */
498
+ constructor(content: MathNode);
499
+ /**
500
+ * @inheritDoc
501
+ */
502
+ next(): Constant | Negative;
503
+ /**
504
+ * @inheritDoc
505
+ */
506
+ toJson(): {
507
+ type: MathNodeType;
508
+ content: Object;
509
+ };
510
+ /**
511
+ * @inheritDoc
512
+ */
513
+ toString(): string;
514
+ /**
515
+ * @inheritDoc
516
+ */
517
+ toTex(): string;
518
+ /**
519
+ * @inheritDoc
520
+ */
521
+ isEqual(mathNode: MathNode): boolean;
522
+ }
523
+ /**
524
+ * Represent the parenthesis as a math node.
525
+ */
526
+ declare class Parenthesis extends MathNode {
527
+ type: MathNodeType;
528
+ isAtomic: boolean;
529
+ readonly content: MathNode;
530
+ /**
531
+ * @param {MathNode} content The content of the parenthesis.
532
+ */
533
+ constructor(content: MathNode);
534
+ /**
535
+ * @inheritDoc
536
+ */
537
+ next(args?: any): MathNode;
538
+ /**
539
+ * @inheritDoc
540
+ */
541
+ toJson(): {
542
+ type: MathNodeType;
543
+ content: Object;
544
+ };
545
+ /**
546
+ * @inheritDoc
547
+ */
548
+ toString(): string;
549
+ /**
550
+ * @inheritDoc
551
+ */
552
+ toTex(): string;
553
+ /**
554
+ * @inheritDoc
555
+ */
556
+ isEqual(mathNode: MathNode): boolean;
557
+ }
558
+ /**
559
+ * Represent the subtraction operation as a math node.
560
+ */
561
+ declare class Subtract extends BaseOperation {
562
+ /**
563
+ * @param {MathNode} left The left node.
564
+ * @param {MathNode} right The right node.
565
+ */
566
+ constructor(left: MathNode, right: MathNode);
567
+ /**
568
+ * @inheritDoc
569
+ */
570
+ concreteNext(): MathNode;
571
+ /**
572
+ * Return the subtraction of left and right.
573
+ *
574
+ * @param {number} left The left operand.
575
+ * @param {number} right The right operand.
576
+ * @returns {number} The difference result
577
+ * .
578
+ */
579
+ operation(left: number, right: number): number;
580
+ }
581
+ /**
582
+ * Represent fractions as math node.
583
+ */
584
+ declare class Variable extends MathNode {
585
+ name: string;
586
+ type: MathNodeType;
587
+ isAtomic: boolean;
588
+ /**
589
+ * @param {string} name The variable name.
590
+ */
591
+ constructor(name: string);
592
+ /**
593
+ * @inheritDoc
594
+ */
595
+ next(): MathNode;
596
+ /**
597
+ * @inheritDoc
598
+ */
599
+ toFraction(): Fraction;
600
+ /**
601
+ * @inheritDoc
602
+ */
603
+ toJson(): {
604
+ type: MathNodeType;
605
+ name: string;
606
+ };
607
+ /**
608
+ * @inheritDoc
609
+ */
610
+ toString(): string;
611
+ /**
612
+ * @inheritDoc
613
+ */
614
+ toTex(): string;
615
+ /**
616
+ * @inheritDoc
617
+ */
618
+ isEqual(mathNode: MathNode): boolean;
619
+ }
620
+ /**
621
+ * Parse a given mathematical expression and return a MathNode.
622
+ *
623
+ * Eg: parse("2 + 3 * 4") will produce the following object:
624
+ *
625
+ * new Add(new Constant(2), new Multiply(new Constant(3), new Constant(4))
626
+ *
627
+ * @see MathNode
628
+ *
629
+ * @param {string} expression A mathematical expression
630
+ * @return {MathNode} a math node that represent the expression from the input
631
+ */
632
+ export declare const parse: (expression: string) => MathNode;
633
+ declare const solve: (expression: string) => MathNode;
634
+ declare const evaluate: (expression: string) => number;
635
+ export { Add, Constant, Divide, Equal, Exponent, Fraction, MathNode, Multiply, Negative, Parenthesis, Subtract, Variable, evaluate, solve };