math-exercises 1.2.10 → 1.3.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.
Files changed (49) hide show
  1. package/README.md +63 -0
  2. package/lib/exercises/calcul/addAndSub.js +8 -9
  3. package/lib/exercises/calcul/fractions/fractionAndIntegerDivision.js +7 -8
  4. package/lib/exercises/calcul/fractions/fractionAndIntegerProduct.js +9 -9
  5. package/lib/exercises/calcul/fractions/fractionAndIntegerSum.js +7 -8
  6. package/lib/exercises/calcul/fractions/fractionsDivision.js +8 -9
  7. package/lib/exercises/calcul/fractions/fractionsProduct.js +8 -9
  8. package/lib/exercises/calcul/fractions/fractionsSum.js +8 -9
  9. package/lib/exercises/calcul/fractions/simplifyFraction.js +2 -3
  10. package/lib/exercises/calcul/operations/operationsPriorities.js +25 -15
  11. package/lib/exercises/calcul/rounding/rounding.js +33 -34
  12. package/lib/exercises/calculLitteral/distributivity/doubleDistributivity.js +9 -10
  13. package/lib/exercises/calculLitteral/distributivity/firstIdentity.js +9 -10
  14. package/lib/exercises/calculLitteral/distributivity/secondIdentity.js +10 -11
  15. package/lib/exercises/calculLitteral/distributivity/simpleDistributivity.js +9 -10
  16. package/lib/exercises/calculLitteral/distributivity/thirdIdentity.js +9 -10
  17. package/lib/exercises/calculLitteral/equation/equationType1Exercise.js +9 -10
  18. package/lib/exercises/calculLitteral/equation/equationType2Exercise.js +11 -12
  19. package/lib/exercises/calculLitteral/equation/equationType3Exercise.js +11 -12
  20. package/lib/exercises/calculLitteral/equation/equationType4Exercise.js +12 -13
  21. package/lib/exercises/calculLitteral/factorisation/factoType1Exercise.js +11 -12
  22. package/lib/exercises/geometry/cartesian/midpoint.js +1 -1
  23. package/lib/exercises/geometry/vectors/scalarProductViaCoords.js +9 -10
  24. package/lib/exercises/powers/powersDivision.js +14 -15
  25. package/lib/exercises/powers/powersOfTenToDecimal.js +6 -7
  26. package/lib/exercises/powers/powersPower.js +12 -13
  27. package/lib/exercises/powers/powersProduct.js +14 -15
  28. package/lib/exercises/powers/scientificToDecimal.js +8 -9
  29. package/lib/exercises/squareRoots/simpifySquareRoot.js +8 -9
  30. package/lib/exercises/utils/getDistinctQuestions.js +1 -2
  31. package/lib/geometry/point.js +4 -4
  32. package/lib/geometry/vector.js +3 -3
  33. package/lib/index.js +4 -5
  34. package/lib/tree/nodes/functions/oppositeNode.js +16 -3
  35. package/lib/tree/nodes/functions/sqrtNode.js +6 -3
  36. package/lib/tree/nodes/numbers/numberNode.js +1 -1
  37. package/lib/tree/nodes/operators/addNode.js +7 -3
  38. package/lib/tree/nodes/operators/divideNode.js +20 -3
  39. package/lib/tree/nodes/operators/equalNode.js +6 -3
  40. package/lib/tree/nodes/operators/fractionNode.js +6 -3
  41. package/lib/tree/nodes/operators/multiplyNode.js +26 -3
  42. package/lib/tree/nodes/operators/operatorNode.js +3 -3
  43. package/lib/tree/nodes/operators/powerNode.js +23 -3
  44. package/lib/tree/nodes/operators/substractNode.js +14 -3
  45. package/lib/tree/nodes/variables/variableNode.js +9 -6
  46. package/lib/tree/parsers/derivateParser.js +61 -59
  47. package/lib/tree/parsers/latexParser.js +109 -116
  48. package/lib/tree/parsers/simplify.js +51 -0
  49. package/package.json +5 -2
@@ -1,117 +1,110 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.latexParser = void 0;
4
- const functionNode_1 = require("../nodes/functions/functionNode");
5
- const node_1 = require("../nodes/node");
6
- const operatorNode_1 = require("../nodes/operators/operatorNode");
7
- function latexParser(node) {
8
- if (!node) {
9
- console.log("parsing a null node ???");
10
- return "";
11
- }
12
- switch (node.type) {
13
- case node_1.NodeType.variable:
14
- return node.tex;
15
- case node_1.NodeType.number:
16
- return node.tex;
17
- case node_1.NodeType.operator:
18
- const operatorNode = node;
19
- let rightTex = latexParser(operatorNode.rightChild);
20
- let leftTex = latexParser(operatorNode.leftChild);
21
- const { leftChild, rightChild } = operatorNode;
22
- switch (operatorNode.id) {
23
- case operatorNode_1.OperatorIds.add:
24
- return `${leftTex} ${rightTex[0] === "-" ? "" : "+ "}${rightTex}`;
25
- case operatorNode_1.OperatorIds.substract: {
26
- const needBrackets = (rightChild.type === node_1.NodeType.operator &&
27
- [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(rightChild.id)) ||
28
- rightTex[0] === "-";
29
- if (needBrackets)
30
- rightTex = `(${rightTex})`;
31
- return `${leftTex} - ${rightTex}`;
32
- }
33
- case operatorNode_1.OperatorIds.multiply: {
34
- if (leftChild.type === node_1.NodeType.operator) {
35
- if ([operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract, operatorNode_1.OperatorIds.divide].includes(leftChild.id))
36
- leftTex = `(${leftTex})`;
37
- }
38
- let needBrackets = rightTex[0] === "-";
39
- if (rightChild.type === node_1.NodeType.operator) {
40
- const operatorRightChild = rightChild;
41
- needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorRightChild.id));
42
- }
43
- if (needBrackets)
44
- rightTex = `(${rightTex})`;
45
- // permet de gérer le cas 3*2^x
46
- let showTimesSign = !isNaN(+rightTex[0]) || rightChild.type === node_1.NodeType.number;
47
- if (rightChild.type === node_1.NodeType.operator) {
48
- const operatorRightChild = rightChild;
49
- showTimesSign || (showTimesSign = [operatorNode_1.OperatorIds.fraction].includes(operatorRightChild.id));
50
- }
51
- return `${leftTex}${showTimesSign ? "\\times " : ""}${rightTex}`;
52
- }
53
- case operatorNode_1.OperatorIds.divide: {
54
- if (leftChild.type === node_1.NodeType.operator) {
55
- if ([operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract, operatorNode_1.OperatorIds.multiply].includes(leftChild.id))
56
- leftTex = `(${leftTex})`;
57
- }
58
- let needBrackets = rightTex[0] === "-";
59
- if (rightChild.type === node_1.NodeType.operator) {
60
- const operatorRightChild = rightChild;
61
- needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorRightChild.id));
62
- }
63
- if (needBrackets)
64
- rightTex = `(${rightTex})`;
65
- return `${leftTex} \\div ${rightTex}`;
66
- }
67
- case operatorNode_1.OperatorIds.fraction: {
68
- return `\\frac{${leftTex}}{${rightTex}}`;
69
- }
70
- case operatorNode_1.OperatorIds.power: {
71
- let needBrackets = leftTex[0] === "-";
72
- if (leftChild.type === node_1.NodeType.operator) {
73
- const childOperator = leftChild;
74
- needBrackets || (needBrackets = [
75
- operatorNode_1.OperatorIds.add,
76
- operatorNode_1.OperatorIds.substract,
77
- operatorNode_1.OperatorIds.multiply,
78
- operatorNode_1.OperatorIds.divide,
79
- operatorNode_1.OperatorIds.fraction,
80
- operatorNode_1.OperatorIds.power,
81
- ].includes(childOperator.id));
82
- }
83
- if (needBrackets)
84
- leftTex = `(${leftTex})`;
85
- return `${leftTex}^{${rightTex}}`;
86
- }
87
- case operatorNode_1.OperatorIds.equal: {
88
- return `${leftTex} = ${rightTex}`;
89
- }
90
- default:
91
- return node.tex;
92
- }
93
- case node_1.NodeType.function: {
94
- const functionNode = node;
95
- const child = functionNode.child;
96
- let childTex = latexParser(functionNode.child);
97
- switch (functionNode.id) {
98
- case functionNode_1.FunctionsIds.sqrt: {
99
- return `\\sqrt{${childTex}}`;
100
- }
101
- case functionNode_1.FunctionsIds.opposite: {
102
- let needBrackets = childTex[0] === "-";
103
- if (child.type === node_1.NodeType.operator) {
104
- const operatorChild = child;
105
- needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorChild.id));
106
- }
107
- if (needBrackets)
108
- childTex = `(${childTex})`;
109
- return `-${childTex}`;
110
- }
111
- }
112
- }
113
- default:
114
- return "";
115
- }
116
- }
117
- exports.latexParser = latexParser;
2
+ // import { FunctionNode, FunctionsIds } from "../nodes/functions/functionNode";
3
+ // import { Node, NodeType } from "../nodes/node";
4
+ // import { OperatorIds, OperatorNode } from "../nodes/operators/operatorNode";
5
+ // export function latexParser(node: Node): string {
6
+ // if (!node) {
7
+ // console.log("parsing a null node ???");
8
+ // return "";
9
+ // }
10
+ // switch (node.type) {
11
+ // case NodeType.variable:
12
+ // return node.tex;
13
+ // case NodeType.number:
14
+ // return node.tex;
15
+ // case NodeType.operator:
16
+ // const operatorNode = node as OperatorNode;
17
+ // let rightTex = latexParser(operatorNode.rightChild);
18
+ // let leftTex = latexParser(operatorNode.leftChild);
19
+ // const { leftChild, rightChild } = operatorNode;
20
+ // switch (operatorNode.id) {
21
+ // case OperatorIds.add:
22
+ // return `${leftTex} ${rightTex[0] === "-" ? "" : "+ "}${rightTex}`;
23
+ // case OperatorIds.substract: {
24
+ // const needBrackets =
25
+ // (rightChild.type === NodeType.operator &&
26
+ // [OperatorIds.add, OperatorIds.substract].includes((rightChild as OperatorNode).id)) ||
27
+ // rightTex[0] === "-";
28
+ // if (needBrackets) rightTex = `(${rightTex})`;
29
+ // return `${leftTex} - ${rightTex}`;
30
+ // }
31
+ // case OperatorIds.multiply: {
32
+ // if (leftChild.type === NodeType.operator) {
33
+ // if ([OperatorIds.add, OperatorIds.substract, OperatorIds.divide].includes((leftChild as OperatorNode).id))
34
+ // leftTex = `(${leftTex})`;
35
+ // }
36
+ // let needBrackets = rightTex[0] === "-";
37
+ // if (rightChild.type === NodeType.operator) {
38
+ // const operatorRightChild = rightChild as OperatorNode;
39
+ // needBrackets ||= [OperatorIds.add, OperatorIds.substract].includes(operatorRightChild.id);
40
+ // }
41
+ // if (needBrackets) rightTex = `(${rightTex})`;
42
+ // // permet de gérer le cas 3*2^x
43
+ // let showTimesSign = !isNaN(+rightTex[0]) || rightChild.type === NodeType.number;
44
+ // if (rightChild.type === NodeType.operator) {
45
+ // const operatorRightChild = rightChild as OperatorNode;
46
+ // showTimesSign ||= [OperatorIds.fraction].includes(operatorRightChild.id);
47
+ // }
48
+ // return `${leftTex}${showTimesSign ? "\\times " : ""}${rightTex}`;
49
+ // }
50
+ // case OperatorIds.divide: {
51
+ // if (leftChild.type === NodeType.operator) {
52
+ // if ([OperatorIds.add, OperatorIds.substract, OperatorIds.multiply].includes((leftChild as OperatorNode).id))
53
+ // leftTex = `(${leftTex})`;
54
+ // }
55
+ // let needBrackets = rightTex[0] === "-";
56
+ // if (rightChild.type === NodeType.operator) {
57
+ // const operatorRightChild = rightChild as OperatorNode;
58
+ // needBrackets ||= [OperatorIds.add, OperatorIds.substract].includes(operatorRightChild.id);
59
+ // }
60
+ // if (needBrackets) rightTex = `(${rightTex})`;
61
+ // return `${leftTex} \\div ${rightTex}`;
62
+ // }
63
+ // case OperatorIds.fraction: {
64
+ // return `\\frac{${leftTex}}{${rightTex}}`;
65
+ // }
66
+ // case OperatorIds.power: {
67
+ // let needBrackets = leftTex[0] === "-";
68
+ // if (leftChild.type === NodeType.operator) {
69
+ // const childOperator = leftChild as OperatorNode;
70
+ // needBrackets ||= [
71
+ // OperatorIds.add,
72
+ // OperatorIds.substract,
73
+ // OperatorIds.multiply,
74
+ // OperatorIds.divide,
75
+ // OperatorIds.fraction,
76
+ // OperatorIds.power,
77
+ // ].includes(childOperator.id);
78
+ // }
79
+ // if (needBrackets) leftTex = `(${leftTex})`;
80
+ // return `${leftTex}^{${rightTex}}`;
81
+ // }
82
+ // case OperatorIds.equal: {
83
+ // return `${leftTex} = ${rightTex}`;
84
+ // }
85
+ // default:
86
+ // return node.tex;
87
+ // }
88
+ // case NodeType.function: {
89
+ // const functionNode = node as FunctionNode;
90
+ // const child = functionNode.child;
91
+ // let childTex = latexParser(functionNode.child);
92
+ // switch (functionNode.id) {
93
+ // case FunctionsIds.sqrt: {
94
+ // return `\\sqrt{${childTex}}`;
95
+ // }
96
+ // case FunctionsIds.opposite: {
97
+ // let needBrackets = childTex[0] === "-";
98
+ // if (child.type === NodeType.operator) {
99
+ // const operatorChild = child as OperatorNode;
100
+ // needBrackets ||= [OperatorIds.add, OperatorIds.substract].includes(operatorChild.id);
101
+ // }
102
+ // if (needBrackets) childTex = `(${childTex})`;
103
+ // return `-${childTex}`;
104
+ // }
105
+ // }
106
+ // }
107
+ // default:
108
+ // return "";
109
+ // }
110
+ // }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.simplifyNode = void 0;
4
+ const mathjs_1 = require("mathjs");
5
+ const oppositeNode_1 = require("../nodes/functions/oppositeNode");
6
+ const sqrtNode_1 = require("../nodes/functions/sqrtNode");
7
+ const numberNode_1 = require("../nodes/numbers/numberNode");
8
+ const addNode_1 = require("../nodes/operators/addNode");
9
+ const fractionNode_1 = require("../nodes/operators/fractionNode");
10
+ const multiplyNode_1 = require("../nodes/operators/multiplyNode");
11
+ const powerNode_1 = require("../nodes/operators/powerNode");
12
+ const substractNode_1 = require("../nodes/operators/substractNode");
13
+ const variableNode_1 = require("../nodes/variables/variableNode");
14
+ const simplifyNode = (node) => {
15
+ console.log(node.toMathString());
16
+ return mathjsNodeToNode((0, mathjs_1.simplify)(node.toMathString()));
17
+ };
18
+ exports.simplifyNode = simplifyNode;
19
+ const mathjsNodeToNode = (mathjsNode) => {
20
+ console.log(mathjsNode);
21
+ if (mathjsNode.isSymbolNode) {
22
+ return new variableNode_1.VariableNode(mathjsNode.name);
23
+ }
24
+ else if (mathjsNode.isConstantNode) {
25
+ return new numberNode_1.NumberNode(mathjsNode.value);
26
+ }
27
+ else if (mathjsNode.isOperatorNode) {
28
+ switch (mathjsNode.fn) {
29
+ case 'add':
30
+ return new addNode_1.AddNode(mathjsNodeToNode(mathjsNode.args[0]), mathjsNodeToNode(mathjsNode.args[1]));
31
+ case 'substract':
32
+ return new substractNode_1.SubstractNode(mathjsNodeToNode(mathjsNode.args[0]), mathjsNodeToNode(mathjsNode.args[1]));
33
+ case 'unaryMinus':
34
+ return new oppositeNode_1.OppositeNode(mathjsNodeToNode(mathjsNode.args[0]));
35
+ case 'divide':
36
+ return new fractionNode_1.FractionNode(mathjsNodeToNode(mathjsNode.args[0]), mathjsNodeToNode(mathjsNode.args[1]));
37
+ case 'multiply':
38
+ return new multiplyNode_1.MultiplyNode(mathjsNodeToNode(mathjsNode.args[0]), mathjsNodeToNode(mathjsNode.args[1]));
39
+ case 'pow':
40
+ return new powerNode_1.PowerNode(mathjsNodeToNode(mathjsNode.args[0]), mathjsNodeToNode(mathjsNode.args[1]));
41
+ }
42
+ }
43
+ else if (mathjsNode.isFunctionNode) {
44
+ const fn = mathjsNode.fn;
45
+ switch (fn.name) {
46
+ case 'sqrt':
47
+ return new sqrtNode_1.SqrtNode(mathjsNodeToNode(mathjsNode.args[0]));
48
+ }
49
+ }
50
+ throw Error('unrecognized mathjs node');
51
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "math-exercises",
3
- "version": "1.2.10",
3
+ "version": "1.3.0",
4
4
  "description": "Générateurs d'exercices et questions mathématiques en latex",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -31,5 +31,8 @@
31
31
  "bugs": {
32
32
  "url": "https://github.com/krirkrirk/math-exercices/issues"
33
33
  },
34
- "homepage": "https://github.com/krirkrirk/math-exercices#readme"
34
+ "homepage": "https://github.com/krirkrirk/math-exercices#readme",
35
+ "dependencies": {
36
+ "mathjs": "^11.5.1"
37
+ }
35
38
  }