math-exercises 1.0.1 → 1.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/nodemon.json +1 -2
- package/package.json +1 -1
- package/src/exercises/calcul/addAndSub.js +34 -0
- package/src/exercises/calcul/addAndSub.ts +32 -0
- package/src/exercises/calcul/priority.ts +33 -0
- package/src/exercises/calculLitteral/distributivity/allIdentities.js +25 -0
- package/src/exercises/calculLitteral/distributivity/allIdentities.ts +24 -0
- package/src/exercises/calculLitteral/distributivity/doubleDistributivity.js +31 -0
- package/src/exercises/calculLitteral/distributivity/doubleDistributivity.ts +31 -0
- package/src/exercises/calculLitteral/distributivity/firstIdentity.js +30 -0
- package/src/exercises/calculLitteral/distributivity/firstIdentity.ts +31 -0
- package/src/exercises/calculLitteral/distributivity/secondIdentity.js +33 -0
- package/src/exercises/calculLitteral/distributivity/secondIdentity.ts +33 -0
- package/src/exercises/calculLitteral/distributivity/simpleDistributivity.js +33 -0
- package/src/exercises/calculLitteral/distributivity/simpleDistributivity.ts +31 -0
- package/src/exercises/calculLitteral/distributivity/thirdIdentity.js +30 -0
- package/src/exercises/calculLitteral/distributivity/thirdIdentity.ts +32 -0
- package/src/exercises/calculLitteral/equation/equationType1Exercise.js +38 -0
- package/src/exercises/calculLitteral/equation/equationType1Exercise.ts +39 -0
- package/src/exercises/calculLitteral/equation/equationType2Exercise.js +41 -0
- package/src/exercises/calculLitteral/equation/equationType2Exercise.ts +41 -0
- package/src/exercises/calculLitteral/equation/equationType3Exercise.js +42 -0
- package/src/exercises/calculLitteral/equation/equationType3Exercise.ts +43 -0
- package/src/exercises/calculLitteral/equation/equationType4Exercise.js +44 -0
- package/src/exercises/calculLitteral/equation/equationType4Exercise.ts +51 -0
- package/src/exercises/calculLitteral/factorisation/factoType1Exercise.js +28 -23
- package/src/exercises/calculLitteral/factorisation/factoType1Exercise.ts +36 -22
- package/src/exercises/exercise.js +5 -0
- package/src/exercises/exercise.ts +9 -1
- package/src/exercises/exercises.ts +27 -0
- package/src/exercises/squareRoots/simpifySquareRoot.js +29 -0
- package/src/exercises/squareRoots/simpifySquareRoot.ts +25 -0
- package/src/expression/expression.js +3 -1
- package/src/expression/expression.ts +4 -0
- package/src/index.js +8 -6
- package/src/index.ts +18 -6
- package/src/mathutils/arithmetic/isSquare.js +7 -0
- package/src/mathutils/arithmetic/primeFactors.js +22 -0
- package/src/numbers/integer/integer.js +15 -0
- package/src/numbers/integer/integer.ts +20 -2
- package/src/numbers/nombre.js +9 -0
- package/src/numbers/nombre.ts +13 -0
- package/src/numbers/rationals/rational.js +23 -1
- package/src/numbers/rationals/rational.ts +37 -3
- package/src/numbers/reals/real.js +15 -0
- package/src/numbers/reals/real.ts +15 -1
- package/src/numbers/reals/squareRoot.js +85 -0
- package/src/numbers/reals/squareRoot.ts +44 -21
- package/src/polynomials/affine.js +5 -4
- package/src/polynomials/affine.ts +16 -21
- package/src/polynomials/polynomial.js +47 -11
- package/src/polynomials/polynomial.ts +57 -17
- package/src/sets/discreteSet.js +4 -4
- package/src/sets/discreteSet.ts +10 -9
- package/src/sets/emptySet.ts +5 -5
- package/src/sets/intervals/intervals.js +33 -9
- package/src/sets/intervals/intervals.ts +41 -12
- package/src/sets/mathSet.ts +5 -3
- package/src/sets/mathSetInterface.ts +4 -2
- package/src/tree/latexParser/latexParse.js +76 -0
- package/src/tree/latexParser/latexParse.ts +82 -0
- package/src/tree/nodes/functions/sqrtNode.js +18 -0
- package/src/tree/nodes/functions/sqrtNode.ts +16 -0
- package/src/tree/nodes/node.js +10 -0
- package/src/tree/nodes/node.ts +14 -0
- package/src/tree/nodes/numbers/numberNode.js +19 -0
- package/src/tree/nodes/numbers/numberNode.ts +19 -0
- package/src/tree/nodes/operators/addNode.js +18 -0
- package/src/tree/nodes/operators/addNode.ts +17 -0
- package/src/tree/nodes/operators/divideNode.js +22 -0
- package/src/tree/nodes/operators/divideNode.ts +23 -0
- package/src/tree/nodes/operators/equalNode.js +18 -0
- package/src/tree/nodes/operators/equalNode.ts +17 -0
- package/src/tree/nodes/operators/multiplyNode.js +18 -0
- package/src/tree/nodes/operators/multiplyNode.ts +18 -0
- package/src/{operations/operation.js → tree/nodes/operators/operatorNode.js} +0 -0
- package/src/tree/nodes/operators/operatorNode.ts +3 -0
- package/src/tree/nodes/operators/oppositeNode.js +18 -0
- package/src/tree/nodes/operators/oppositeNode.ts +17 -0
- package/src/tree/nodes/operators/powerNode.js +18 -0
- package/src/tree/nodes/operators/powerNode.ts +17 -0
- package/src/tree/nodes/operators/substractNode.js +18 -0
- package/src/tree/nodes/operators/substractNode.ts +17 -0
- package/src/tree/nodes/variables/variableNode.js +20 -0
- package/src/tree/nodes/variables/variableNode.ts +18 -0
- package/src/equations/equations.ts +0 -16
- package/src/exercises/calculLitteral/factorisation/factorisation.js +0 -39
- package/src/latex/add.js +0 -7
- package/src/latex/latex.js +0 -37
- package/src/latex/latex.ts +0 -35
- package/src/latex/monome.ts +0 -0
- package/src/latex/multiply.ts +0 -5
- package/src/numbers/number.ts +0 -4
- package/src/operations/add.js +0 -9
- package/src/operations/add.ts +0 -9
- package/src/operations/operation.ts +0 -6
- package/src/operations/substract.js +0 -9
- package/src/operations/substract.ts +0 -9
package/src/sets/discreteSet.js
CHANGED
|
@@ -6,13 +6,13 @@ var DiscreteSet = /** @class */ (function () {
|
|
|
6
6
|
function DiscreteSet(elements) {
|
|
7
7
|
var _this = this;
|
|
8
8
|
this.elements = Array.from(new Set(elements));
|
|
9
|
-
var tex = "{";
|
|
9
|
+
var tex = "\\{";
|
|
10
10
|
this.elements.forEach(function (el, index) {
|
|
11
|
-
tex += el;
|
|
11
|
+
tex += el.tex;
|
|
12
12
|
if (index < _this.elements.length - 1)
|
|
13
13
|
tex += ";";
|
|
14
14
|
else
|
|
15
|
-
tex += "}";
|
|
15
|
+
tex += "\\}";
|
|
16
16
|
});
|
|
17
17
|
this.tex = tex;
|
|
18
18
|
}
|
|
@@ -20,7 +20,7 @@ var DiscreteSet = /** @class */ (function () {
|
|
|
20
20
|
return this.tex;
|
|
21
21
|
};
|
|
22
22
|
DiscreteSet.prototype.includes = function (el) {
|
|
23
|
-
return this.elements.
|
|
23
|
+
return this.elements.some(function (nb) { return nb.value === el.value && nb.tex === el.tex; });
|
|
24
24
|
};
|
|
25
25
|
DiscreteSet.prototype.getRandomElement = function () {
|
|
26
26
|
return (0, random_1.random)(this.elements);
|
package/src/sets/discreteSet.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { Nombre } from "../numbers/nombre";
|
|
1
2
|
import { random } from "../utils/random";
|
|
2
3
|
import { MathSetInterface } from "./mathSetInterface";
|
|
3
4
|
|
|
4
|
-
export class DiscreteSet
|
|
5
|
-
elements:
|
|
5
|
+
export class DiscreteSet implements MathSetInterface {
|
|
6
|
+
elements: Nombre[];
|
|
6
7
|
tex: string;
|
|
7
|
-
constructor(elements:
|
|
8
|
+
constructor(elements: Nombre[]) {
|
|
8
9
|
this.elements = Array.from(new Set(elements));
|
|
9
|
-
let tex = "{";
|
|
10
|
+
let tex = "\\{";
|
|
10
11
|
this.elements.forEach((el, index) => {
|
|
11
|
-
tex += el;
|
|
12
|
+
tex += el.tex;
|
|
12
13
|
if (index < this.elements.length - 1) tex += ";";
|
|
13
|
-
else tex += "}";
|
|
14
|
+
else tex += "\\}";
|
|
14
15
|
});
|
|
15
16
|
this.tex = tex;
|
|
16
17
|
}
|
|
@@ -19,11 +20,11 @@ export class DiscreteSet<T> implements MathSetInterface<T> {
|
|
|
19
20
|
return this.tex;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
includes(el:
|
|
23
|
-
return this.elements.
|
|
23
|
+
includes(el: Nombre): boolean {
|
|
24
|
+
return this.elements.some((nb) => nb.value === el.value && nb.tex === el.tex);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
getRandomElement():
|
|
27
|
+
getRandomElement(): Nombre {
|
|
27
28
|
return random(this.elements);
|
|
28
29
|
}
|
|
29
30
|
}
|
package/src/sets/emptySet.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { MathSetInterface } from "./mathSetInterface";
|
|
1
|
+
// import { MathSetInterface } from "./mathSetInterface";
|
|
2
2
|
|
|
3
|
-
export abstract class EmptySet implements MathSetInterface {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
// export abstract class EmptySet implements MathSetInterface {
|
|
4
|
+
// tex: string = "\\emptyset";
|
|
5
|
+
// getRandomElement = () => null;
|
|
6
|
+
// }
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
exports.__esModule = true;
|
|
3
3
|
exports.Interval = void 0;
|
|
4
4
|
var epsilon_1 = require("../../numbers/epsilon");
|
|
5
|
-
var
|
|
5
|
+
var nombre_1 = require("../../numbers/nombre");
|
|
6
6
|
var round_1 = require("../../mathutils/round");
|
|
7
7
|
var mathSet_1 = require("../mathSet");
|
|
8
|
+
var integer_1 = require("../../numbers/integer/integer");
|
|
9
|
+
var real_1 = require("../../numbers/reals/real");
|
|
8
10
|
var BoundType;
|
|
9
11
|
(function (BoundType) {
|
|
10
12
|
BoundType["OO"] = "]a;b[";
|
|
@@ -20,10 +22,12 @@ var Interval = /** @class */ (function () {
|
|
|
20
22
|
if (tex === void 0) { tex = "[-10; 10]"; }
|
|
21
23
|
this.tex = tex;
|
|
22
24
|
var isInt = tex[1] === "[" || tex[1] === "]";
|
|
23
|
-
this.type = isInt ?
|
|
25
|
+
this.type = isInt ? nombre_1.NumberType.Integer : nombre_1.NumberType.Real;
|
|
24
26
|
var left = tex[0];
|
|
25
27
|
var right = tex[tex.length - 1];
|
|
26
|
-
var _a = tex
|
|
28
|
+
var _a = tex
|
|
29
|
+
.slice(isInt ? 2 : 1, isInt ? tex.length - 2 : tex.length - 1)
|
|
30
|
+
.split(";"), a = _a[0], b = _a[1];
|
|
27
31
|
switch ("".concat(left, "a;b").concat(right)) {
|
|
28
32
|
case "[a;b]":
|
|
29
33
|
this.boundType = BoundType.FF;
|
|
@@ -38,7 +42,6 @@ var Interval = /** @class */ (function () {
|
|
|
38
42
|
this.boundType = BoundType.OF;
|
|
39
43
|
break;
|
|
40
44
|
default:
|
|
41
|
-
console.log("".concat(left, "a;b").concat(right));
|
|
42
45
|
throw console.error("wrong interval");
|
|
43
46
|
}
|
|
44
47
|
function getBound(bound) {
|
|
@@ -62,6 +65,17 @@ var Interval = /** @class */ (function () {
|
|
|
62
65
|
//res = a
|
|
63
66
|
// return new MathSet();
|
|
64
67
|
// }
|
|
68
|
+
Interval.prototype.exclude = function (nb) {
|
|
69
|
+
var _this = this;
|
|
70
|
+
var rand = function () {
|
|
71
|
+
var x;
|
|
72
|
+
do {
|
|
73
|
+
x = _this.getRandomElement();
|
|
74
|
+
} while (x.value === nb);
|
|
75
|
+
return x;
|
|
76
|
+
};
|
|
77
|
+
return new mathSet_1.MathSet(this.toTex() + "\\{".concat(nb, "\\}"), rand);
|
|
78
|
+
};
|
|
65
79
|
Interval.prototype.difference = function (set) {
|
|
66
80
|
var _this = this;
|
|
67
81
|
var rand = function () {
|
|
@@ -74,15 +88,25 @@ var Interval = /** @class */ (function () {
|
|
|
74
88
|
return new mathSet_1.MathSet(this.toTex() + "\\ ".concat(set.toTex()), rand);
|
|
75
89
|
};
|
|
76
90
|
Interval.prototype.toTex = function () {
|
|
77
|
-
return;
|
|
91
|
+
return this.tex;
|
|
78
92
|
};
|
|
79
93
|
Interval.prototype.getRandomElement = function (precision) {
|
|
80
|
-
if (precision === void 0) { precision = this.type ===
|
|
94
|
+
if (precision === void 0) { precision = this.type === nombre_1.NumberType.Integer ? 0 : 2; }
|
|
81
95
|
if (this.min === -Infinity || this.max === Infinity)
|
|
82
96
|
throw Error("Can't chose amongst infinity");
|
|
83
|
-
var min = this.boundType === BoundType.OO || this.boundType === BoundType.OF
|
|
84
|
-
|
|
85
|
-
|
|
97
|
+
var min = this.boundType === BoundType.OO || this.boundType === BoundType.OF
|
|
98
|
+
? this.min + epsilon_1.EPSILON
|
|
99
|
+
: this.min;
|
|
100
|
+
var max = this.boundType === BoundType.OO || this.boundType === BoundType.FO
|
|
101
|
+
? this.max - epsilon_1.EPSILON
|
|
102
|
+
: this.max;
|
|
103
|
+
var value = (0, round_1.round)(min + Math.random() * (max - this.min), precision);
|
|
104
|
+
switch (this.type) {
|
|
105
|
+
case nombre_1.NumberType.Integer:
|
|
106
|
+
return new integer_1.Integer(value);
|
|
107
|
+
default:
|
|
108
|
+
return new real_1.Real(value, value.toString());
|
|
109
|
+
}
|
|
86
110
|
};
|
|
87
111
|
return Interval;
|
|
88
112
|
}());
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { EPSILON } from "../../numbers/epsilon";
|
|
2
|
-
import { NumberType } from "../../numbers/
|
|
2
|
+
import { Nombre, NumberType } from "../../numbers/nombre";
|
|
3
3
|
import { round } from "../../mathutils/round";
|
|
4
4
|
import { MathSetInterface } from "../mathSetInterface";
|
|
5
5
|
import { DiscreteSet } from "../discreteSet";
|
|
6
6
|
import { MathSet } from "../mathSet";
|
|
7
|
+
import { Integer } from "../../numbers/integer/integer";
|
|
8
|
+
import { Real } from "../../numbers/reals/real";
|
|
7
9
|
|
|
8
10
|
enum BoundType {
|
|
9
11
|
OO = "]a;b[",
|
|
@@ -12,7 +14,7 @@ enum BoundType {
|
|
|
12
14
|
FF = "[a;b]",
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export class Interval implements MathSetInterface
|
|
17
|
+
export class Interval implements MathSetInterface {
|
|
16
18
|
min: number;
|
|
17
19
|
max: number;
|
|
18
20
|
boundType: BoundType;
|
|
@@ -27,7 +29,9 @@ export class Interval implements MathSetInterface<Number> {
|
|
|
27
29
|
this.type = isInt ? NumberType.Integer : NumberType.Real;
|
|
28
30
|
const left = tex[0];
|
|
29
31
|
const right = tex[tex.length - 1];
|
|
30
|
-
const [a, b] = tex
|
|
32
|
+
const [a, b] = tex
|
|
33
|
+
.slice(isInt ? 2 : 1, isInt ? tex.length - 2 : tex.length - 1)
|
|
34
|
+
.split(";");
|
|
31
35
|
|
|
32
36
|
switch (`${left}a;b${right}`) {
|
|
33
37
|
case "[a;b]":
|
|
@@ -43,7 +47,6 @@ export class Interval implements MathSetInterface<Number> {
|
|
|
43
47
|
this.boundType = BoundType.OF;
|
|
44
48
|
break;
|
|
45
49
|
default:
|
|
46
|
-
console.log(`${left}a;b${right}`);
|
|
47
50
|
throw console.error("wrong interval");
|
|
48
51
|
}
|
|
49
52
|
function getBound(bound: string) {
|
|
@@ -68,8 +71,18 @@ export class Interval implements MathSetInterface<Number> {
|
|
|
68
71
|
//res = a
|
|
69
72
|
// return new MathSet();
|
|
70
73
|
// }
|
|
74
|
+
exclude(nb: number) {
|
|
75
|
+
const rand = () => {
|
|
76
|
+
let x;
|
|
77
|
+
do {
|
|
78
|
+
x = this.getRandomElement();
|
|
79
|
+
} while (x.value === nb);
|
|
80
|
+
return x;
|
|
81
|
+
};
|
|
82
|
+
return new MathSet(this.toTex() + `\\{${nb}\\}`, rand);
|
|
83
|
+
}
|
|
71
84
|
|
|
72
|
-
difference(set: DiscreteSet
|
|
85
|
+
difference(set: DiscreteSet): MathSet {
|
|
73
86
|
const rand = () => {
|
|
74
87
|
let x;
|
|
75
88
|
do {
|
|
@@ -81,13 +94,29 @@ export class Interval implements MathSetInterface<Number> {
|
|
|
81
94
|
return new MathSet(this.toTex() + `\\ ${set.toTex()}`, rand);
|
|
82
95
|
}
|
|
83
96
|
|
|
84
|
-
toTex() {
|
|
85
|
-
return;
|
|
97
|
+
toTex(): string {
|
|
98
|
+
return this.tex;
|
|
86
99
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
|
|
101
|
+
getRandomElement(
|
|
102
|
+
precision: number = this.type === NumberType.Integer ? 0 : 2
|
|
103
|
+
): Nombre {
|
|
104
|
+
if (this.min === -Infinity || this.max === Infinity)
|
|
105
|
+
throw Error("Can't chose amongst infinity");
|
|
106
|
+
let min =
|
|
107
|
+
this.boundType === BoundType.OO || this.boundType === BoundType.OF
|
|
108
|
+
? this.min + EPSILON
|
|
109
|
+
: this.min;
|
|
110
|
+
let max =
|
|
111
|
+
this.boundType === BoundType.OO || this.boundType === BoundType.FO
|
|
112
|
+
? this.max - EPSILON
|
|
113
|
+
: this.max;
|
|
114
|
+
const value = round(min + Math.random() * (max - this.min), precision);
|
|
115
|
+
switch (this.type) {
|
|
116
|
+
case NumberType.Integer:
|
|
117
|
+
return new Integer(value);
|
|
118
|
+
default:
|
|
119
|
+
return new Real(value, value.toString());
|
|
120
|
+
}
|
|
92
121
|
}
|
|
93
122
|
}
|
package/src/sets/mathSet.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { Nombre } from "../numbers/nombre";
|
|
2
|
+
import { DiscreteSet } from "./discreteSet";
|
|
1
3
|
import { MathSetInterface } from "./mathSetInterface";
|
|
2
4
|
|
|
3
|
-
export class MathSet
|
|
5
|
+
export class MathSet implements MathSetInterface {
|
|
4
6
|
tex: string;
|
|
5
|
-
getRandomElement: () =>
|
|
6
|
-
constructor(tex: string, getRandomElement: () =>
|
|
7
|
+
getRandomElement: () => Nombre;
|
|
8
|
+
constructor(tex: string, getRandomElement: () => Nombre) {
|
|
7
9
|
this.tex = tex;
|
|
8
10
|
this.getRandomElement = getRandomElement;
|
|
9
11
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { Nombre } from "../numbers/nombre";
|
|
2
|
+
|
|
3
|
+
export interface MathSetInterface {
|
|
2
4
|
tex: string;
|
|
3
|
-
getRandomElement: () =>
|
|
5
|
+
getRandomElement: () => Nombre;
|
|
4
6
|
// constructor(tex: string, getRandomElement: () => number) {
|
|
5
7
|
// this.tex = tex;
|
|
6
8
|
// this.getRandomElement = getRandomElement;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.latexParse = void 0;
|
|
4
|
+
var node_1 = require("../nodes/node");
|
|
5
|
+
function latexParse(node) {
|
|
6
|
+
if (!node) {
|
|
7
|
+
console.log("parsing a null node ???");
|
|
8
|
+
return "";
|
|
9
|
+
}
|
|
10
|
+
switch (node.type) {
|
|
11
|
+
case node_1.NodeType.variable:
|
|
12
|
+
return node.tex;
|
|
13
|
+
case node_1.NodeType.number:
|
|
14
|
+
return node.tex;
|
|
15
|
+
case node_1.NodeType.operator:
|
|
16
|
+
var rightTex = latexParse(node.rightChild);
|
|
17
|
+
var leftTex = latexParse(node.leftChild);
|
|
18
|
+
var leftChild = node.leftChild, rightChild = node.rightChild;
|
|
19
|
+
switch (node.id) {
|
|
20
|
+
case "add":
|
|
21
|
+
return "".concat(leftTex, " ").concat(rightTex[0] === "-" ? "" : "+ ").concat(rightTex);
|
|
22
|
+
case "opposite":
|
|
23
|
+
var needBrackets = leftChild.id === "add" ||
|
|
24
|
+
leftChild.id === "substract" ||
|
|
25
|
+
leftTex[0] === "-";
|
|
26
|
+
if (needBrackets)
|
|
27
|
+
leftTex = "(".concat(leftTex, ")");
|
|
28
|
+
return "-".concat(leftTex);
|
|
29
|
+
case "substract": {
|
|
30
|
+
var needBrackets_1 = rightChild.id === "add" ||
|
|
31
|
+
rightChild.id === "substract" ||
|
|
32
|
+
rightTex[0] === "-";
|
|
33
|
+
if (needBrackets_1)
|
|
34
|
+
rightTex = "(".concat(rightTex, ")");
|
|
35
|
+
return "".concat(leftTex, " - ").concat(rightTex);
|
|
36
|
+
}
|
|
37
|
+
case "multiply": {
|
|
38
|
+
if (leftChild.id === "add" || leftChild.id === "substract")
|
|
39
|
+
leftTex = "(".concat(leftTex, ")");
|
|
40
|
+
var needBrackets_2 = rightChild.id === "add" ||
|
|
41
|
+
rightChild.id === "substract" ||
|
|
42
|
+
rightTex[0] === "-";
|
|
43
|
+
if (needBrackets_2)
|
|
44
|
+
rightTex = "(".concat(rightTex, ")");
|
|
45
|
+
var showTimesSign = !isNaN(+rightTex[0]); //permet de gérer le cas 3*2^x
|
|
46
|
+
return "".concat(leftTex).concat(showTimesSign ? "\\times " : "").concat(rightTex);
|
|
47
|
+
}
|
|
48
|
+
case "divide": {
|
|
49
|
+
return "\\frac{".concat(leftTex, "}{").concat(rightTex, "}");
|
|
50
|
+
}
|
|
51
|
+
case "power": {
|
|
52
|
+
if (leftChild.id === "add" ||
|
|
53
|
+
leftChild.id === "substract" ||
|
|
54
|
+
leftChild.id === "multiply")
|
|
55
|
+
leftTex = "(".concat(leftTex, ")");
|
|
56
|
+
return "".concat(leftTex, "^{").concat(rightTex, "}");
|
|
57
|
+
}
|
|
58
|
+
case "equal": {
|
|
59
|
+
return "".concat(leftTex, " = ").concat(rightTex);
|
|
60
|
+
}
|
|
61
|
+
default:
|
|
62
|
+
return node.tex;
|
|
63
|
+
}
|
|
64
|
+
case node_1.NodeType["function"]: {
|
|
65
|
+
var leftTex_1 = latexParse(node.leftChild);
|
|
66
|
+
switch (node.id) {
|
|
67
|
+
case "sqrt": {
|
|
68
|
+
return "\\sqrt{".concat(leftTex_1, "}");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
default:
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.latexParse = latexParse;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Node, NodeType } from "../nodes/node";
|
|
2
|
+
import { OperatorNode } from "../nodes/operators/operatorNode";
|
|
3
|
+
|
|
4
|
+
export function latexParse(node: Node | null): string {
|
|
5
|
+
if (!node) {
|
|
6
|
+
console.log("parsing a null node ???");
|
|
7
|
+
return "";
|
|
8
|
+
}
|
|
9
|
+
switch (node.type) {
|
|
10
|
+
case NodeType.variable:
|
|
11
|
+
return node.tex;
|
|
12
|
+
case NodeType.number:
|
|
13
|
+
return node.tex;
|
|
14
|
+
case NodeType.operator:
|
|
15
|
+
let rightTex = latexParse(node.rightChild);
|
|
16
|
+
let leftTex = latexParse(node.leftChild);
|
|
17
|
+
const { leftChild, rightChild } = node;
|
|
18
|
+
switch (node.id) {
|
|
19
|
+
case "add":
|
|
20
|
+
return `${leftTex} ${rightTex[0] === "-" ? "" : "+ "}${rightTex}`;
|
|
21
|
+
|
|
22
|
+
case "opposite":
|
|
23
|
+
const needBrackets =
|
|
24
|
+
leftChild!.id === "add" ||
|
|
25
|
+
leftChild!.id === "substract" ||
|
|
26
|
+
leftTex[0] === "-";
|
|
27
|
+
if (needBrackets) leftTex = `(${leftTex})`;
|
|
28
|
+
return `-${leftTex}`;
|
|
29
|
+
|
|
30
|
+
case "substract": {
|
|
31
|
+
const needBrackets =
|
|
32
|
+
rightChild!.id === "add" ||
|
|
33
|
+
rightChild!.id === "substract" ||
|
|
34
|
+
rightTex[0] === "-";
|
|
35
|
+
if (needBrackets) rightTex = `(${rightTex})`;
|
|
36
|
+
return `${leftTex} - ${rightTex}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
case "multiply": {
|
|
40
|
+
if (leftChild!.id === "add" || leftChild!.id === "substract")
|
|
41
|
+
leftTex = `(${leftTex})`;
|
|
42
|
+
const needBrackets =
|
|
43
|
+
rightChild!.id === "add" ||
|
|
44
|
+
rightChild!.id === "substract" ||
|
|
45
|
+
rightTex[0] === "-";
|
|
46
|
+
if (needBrackets) rightTex = `(${rightTex})`;
|
|
47
|
+
const showTimesSign = !isNaN(+rightTex[0]); //permet de gérer le cas 3*2^x
|
|
48
|
+
return `${leftTex}${showTimesSign ? "\\times " : ""}${rightTex}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
case "divide": {
|
|
52
|
+
return `\\frac{${leftTex}}{${rightTex}}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
case "power": {
|
|
56
|
+
if (
|
|
57
|
+
leftChild!.id === "add" ||
|
|
58
|
+
leftChild!.id === "substract" ||
|
|
59
|
+
leftChild!.id === "multiply"
|
|
60
|
+
)
|
|
61
|
+
leftTex = `(${leftTex})`;
|
|
62
|
+
return `${leftTex}^{${rightTex}}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
case "equal": {
|
|
66
|
+
return `${leftTex} = ${rightTex}`;
|
|
67
|
+
}
|
|
68
|
+
default:
|
|
69
|
+
return node.tex;
|
|
70
|
+
}
|
|
71
|
+
case NodeType.function: {
|
|
72
|
+
let leftTex = latexParse(node.leftChild);
|
|
73
|
+
switch (node.id) {
|
|
74
|
+
case "sqrt": {
|
|
75
|
+
return `\\sqrt{${leftTex}}`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
default:
|
|
80
|
+
return "";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.SqrtNode = void 0;
|
|
4
|
+
var node_1 = require("../node");
|
|
5
|
+
var SqrtNode = /** @class */ (function () {
|
|
6
|
+
function SqrtNode(leftChild) {
|
|
7
|
+
this.type = node_1.NodeType["function"];
|
|
8
|
+
this.id = "sqrt";
|
|
9
|
+
this.tex = "\\sqrt";
|
|
10
|
+
this.leftChild = leftChild;
|
|
11
|
+
this.rightChild = null;
|
|
12
|
+
}
|
|
13
|
+
SqrtNode.prototype.toString = function () {
|
|
14
|
+
return "sqrt(".concat(this.leftChild, ")");
|
|
15
|
+
};
|
|
16
|
+
return SqrtNode;
|
|
17
|
+
}());
|
|
18
|
+
exports.SqrtNode = SqrtNode;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Node, NodeType } from "../node";
|
|
2
|
+
|
|
3
|
+
export class SqrtNode implements Node {
|
|
4
|
+
leftChild: Node;
|
|
5
|
+
rightChild: null;
|
|
6
|
+
type: NodeType = NodeType.function;
|
|
7
|
+
id: string = "sqrt";
|
|
8
|
+
tex = "\\sqrt";
|
|
9
|
+
constructor(leftChild: Node) {
|
|
10
|
+
this.leftChild = leftChild;
|
|
11
|
+
this.rightChild = null;
|
|
12
|
+
}
|
|
13
|
+
toString(): string {
|
|
14
|
+
return `sqrt(${this.leftChild})`;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.NodeType = void 0;
|
|
4
|
+
var NodeType;
|
|
5
|
+
(function (NodeType) {
|
|
6
|
+
NodeType[NodeType["number"] = 0] = "number";
|
|
7
|
+
NodeType[NodeType["variable"] = 1] = "variable";
|
|
8
|
+
NodeType[NodeType["operator"] = 2] = "operator";
|
|
9
|
+
NodeType[NodeType["function"] = 3] = "function";
|
|
10
|
+
})(NodeType = exports.NodeType || (exports.NodeType = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.NumberNode = void 0;
|
|
4
|
+
var node_1 = require("../node");
|
|
5
|
+
var NumberNode = /** @class */ (function () {
|
|
6
|
+
function NumberNode(value) {
|
|
7
|
+
this.id = "number";
|
|
8
|
+
this.leftChild = null;
|
|
9
|
+
this.rightChild = null;
|
|
10
|
+
this.value = value;
|
|
11
|
+
this.tex = value + "";
|
|
12
|
+
this.type = node_1.NodeType.number;
|
|
13
|
+
}
|
|
14
|
+
NumberNode.prototype.toString = function () {
|
|
15
|
+
return "".concat(this.tex);
|
|
16
|
+
};
|
|
17
|
+
return NumberNode;
|
|
18
|
+
}());
|
|
19
|
+
exports.NumberNode = NumberNode;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Node, NodeType } from "../node";
|
|
2
|
+
|
|
3
|
+
export class NumberNode implements Node {
|
|
4
|
+
tex: string;
|
|
5
|
+
value: number;
|
|
6
|
+
type: NodeType;
|
|
7
|
+
id = "number";
|
|
8
|
+
leftChild = null;
|
|
9
|
+
rightChild = null;
|
|
10
|
+
constructor(value: number) {
|
|
11
|
+
this.value = value;
|
|
12
|
+
this.tex = value + "";
|
|
13
|
+
this.type = NodeType.number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
toString(): string {
|
|
17
|
+
return `${this.tex}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.AddNode = void 0;
|
|
4
|
+
var node_1 = require("../node");
|
|
5
|
+
var AddNode = /** @class */ (function () {
|
|
6
|
+
function AddNode(leftChild, rightChild) {
|
|
7
|
+
this.type = node_1.NodeType.operator;
|
|
8
|
+
this.id = "add";
|
|
9
|
+
this.tex = "+";
|
|
10
|
+
this.leftChild = leftChild;
|
|
11
|
+
this.rightChild = rightChild;
|
|
12
|
+
}
|
|
13
|
+
AddNode.prototype.toString = function () {
|
|
14
|
+
return "".concat(this.leftChild, " + ").concat(this.rightChild);
|
|
15
|
+
};
|
|
16
|
+
return AddNode;
|
|
17
|
+
}());
|
|
18
|
+
exports.AddNode = AddNode;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Node, NodeType } from "../node";
|
|
2
|
+
import { OperatorNode } from "./operatorNode";
|
|
3
|
+
|
|
4
|
+
export class AddNode implements Node, OperatorNode {
|
|
5
|
+
leftChild: Node;
|
|
6
|
+
rightChild: Node;
|
|
7
|
+
type: NodeType = NodeType.operator;
|
|
8
|
+
id: string = "add";
|
|
9
|
+
tex = "+";
|
|
10
|
+
constructor(leftChild: Node, rightChild: Node) {
|
|
11
|
+
this.leftChild = leftChild;
|
|
12
|
+
this.rightChild = rightChild;
|
|
13
|
+
}
|
|
14
|
+
toString(): string {
|
|
15
|
+
return `${this.leftChild} + ${this.rightChild}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.DivideNode = void 0;
|
|
4
|
+
var node_1 = require("../node");
|
|
5
|
+
var DivideNode = /** @class */ (function () {
|
|
6
|
+
/**
|
|
7
|
+
* @param leftChild num
|
|
8
|
+
* @param rightChild denum
|
|
9
|
+
*/
|
|
10
|
+
function DivideNode(leftChild, rightChild) {
|
|
11
|
+
this.type = node_1.NodeType.operator;
|
|
12
|
+
this.id = "divide";
|
|
13
|
+
this.tex = "\\frac";
|
|
14
|
+
this.leftChild = leftChild;
|
|
15
|
+
this.rightChild = rightChild;
|
|
16
|
+
}
|
|
17
|
+
DivideNode.prototype.toString = function () {
|
|
18
|
+
return "\\frac{".concat(this.leftChild, "}{").concat(this.rightChild, "}");
|
|
19
|
+
};
|
|
20
|
+
return DivideNode;
|
|
21
|
+
}());
|
|
22
|
+
exports.DivideNode = DivideNode;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Node, NodeType } from "../node";
|
|
2
|
+
import { OperatorNode } from "./operatorNode";
|
|
3
|
+
|
|
4
|
+
export class DivideNode implements Node, OperatorNode {
|
|
5
|
+
leftChild: Node;
|
|
6
|
+
rightChild: Node;
|
|
7
|
+
type: NodeType = NodeType.operator;
|
|
8
|
+
id: string = "divide";
|
|
9
|
+
tex = "\\frac";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param leftChild num
|
|
13
|
+
* @param rightChild denum
|
|
14
|
+
*/
|
|
15
|
+
constructor(leftChild: Node, rightChild: Node) {
|
|
16
|
+
this.leftChild = leftChild;
|
|
17
|
+
this.rightChild = rightChild;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
toString(): string {
|
|
21
|
+
return `\\frac{${this.leftChild}}{${this.rightChild}}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.EqualNode = void 0;
|
|
4
|
+
var node_1 = require("../node");
|
|
5
|
+
var EqualNode = /** @class */ (function () {
|
|
6
|
+
function EqualNode(leftChild, rightChild) {
|
|
7
|
+
this.type = node_1.NodeType.operator;
|
|
8
|
+
this.id = "equal";
|
|
9
|
+
this.tex = "=";
|
|
10
|
+
this.leftChild = leftChild;
|
|
11
|
+
this.rightChild = rightChild;
|
|
12
|
+
}
|
|
13
|
+
EqualNode.prototype.toString = function () {
|
|
14
|
+
return "".concat(this.leftChild, " = ").concat(this.rightChild);
|
|
15
|
+
};
|
|
16
|
+
return EqualNode;
|
|
17
|
+
}());
|
|
18
|
+
exports.EqualNode = EqualNode;
|