math-exercises 1.3.4 → 1.3.5

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 (102) hide show
  1. package/lib/exercises/calcul/addAndSub.js +11 -12
  2. package/lib/exercises/calcul/fractions/fractionAndIntegerDivision.js +15 -15
  3. package/lib/exercises/calcul/fractions/fractionAndIntegerProduct.js +15 -14
  4. package/lib/exercises/calcul/fractions/fractionAndIntegerSum.js +13 -13
  5. package/lib/exercises/calcul/fractions/fractionsDivision.js +12 -12
  6. package/lib/exercises/calcul/fractions/fractionsProduct.js +11 -12
  7. package/lib/exercises/calcul/fractions/fractionsSum.js +11 -12
  8. package/lib/exercises/calcul/fractions/simplifyFraction.js +10 -11
  9. package/lib/exercises/calcul/operations/operationsPriorities.js +98 -0
  10. package/lib/exercises/calcul/operations/operationsPrioritiesWithoutRelative.js +111 -0
  11. package/lib/exercises/calcul/rounding/rounding.js +36 -37
  12. package/lib/exercises/calculLitteral/distributivity/allIdentities.js +8 -8
  13. package/lib/exercises/calculLitteral/distributivity/doubleDistributivity.js +15 -16
  14. package/lib/exercises/calculLitteral/distributivity/firstIdentity.js +10 -11
  15. package/lib/exercises/calculLitteral/distributivity/secondIdentity.js +17 -18
  16. package/lib/exercises/calculLitteral/distributivity/simpleDistributivity.js +16 -17
  17. package/lib/exercises/calculLitteral/distributivity/thirdIdentity.js +15 -16
  18. package/lib/exercises/calculLitteral/equation/equationType1Exercise.js +15 -16
  19. package/lib/exercises/calculLitteral/equation/equationType2Exercise.js +19 -20
  20. package/lib/exercises/calculLitteral/equation/equationType3Exercise.js +19 -20
  21. package/lib/exercises/calculLitteral/equation/equationType4Exercise.js +20 -21
  22. package/lib/exercises/calculLitteral/factorisation/factoType1Exercise.js +20 -21
  23. package/lib/exercises/exercises.js +11 -1
  24. package/lib/exercises/geometry/cartesian/midpoint.js +29 -0
  25. package/lib/exercises/geometry/vectors/scalarProductViaCoords.js +28 -0
  26. package/lib/exercises/geometry/vectors/scalarProductViaNorms.js +27 -0
  27. package/lib/exercises/powers/powersDivision.js +23 -24
  28. package/lib/exercises/powers/powersOfTenToDecimal.js +13 -14
  29. package/lib/exercises/powers/powersPower.js +19 -20
  30. package/lib/exercises/powers/powersProduct.js +22 -23
  31. package/lib/exercises/powers/scientificToDecimal.js +17 -18
  32. package/lib/exercises/sequences/geometric/geometricExplicitFormulaUsage.js +24 -0
  33. package/lib/exercises/sequences/geometric/geometricFindReason.js +23 -0
  34. package/lib/exercises/squareRoots/simpifySquareRoot.js +9 -10
  35. package/lib/exercises/trigonometry/mainRemarkableValues.js +28 -0
  36. package/lib/exercises/trigonometry/remarkableValues.js +28 -0
  37. package/lib/exercises/utils/getDistinctQuestions.js +1 -1
  38. package/lib/geometry/point.js +26 -0
  39. package/lib/geometry/vector.js +31 -0
  40. package/lib/index.js +1 -0
  41. package/lib/math/geometry/point.js +27 -0
  42. package/lib/math/geometry/vector.js +31 -0
  43. package/lib/math/numbers/decimals/decimal.js +140 -0
  44. package/lib/math/numbers/epsilon.js +10 -0
  45. package/lib/math/numbers/integer/integer.js +68 -0
  46. package/lib/math/numbers/integer/power.js +52 -0
  47. package/lib/math/numbers/nombre.js +10 -0
  48. package/lib/math/numbers/rationals/rational.js +113 -0
  49. package/lib/math/numbers/reals/real.js +16 -0
  50. package/lib/math/numbers/reals/squareRoot.js +63 -0
  51. package/lib/math/polynomials/affine.js +42 -0
  52. package/lib/math/polynomials/polynomial.js +125 -0
  53. package/lib/math/sequences/arithmeticSequence.js +1 -0
  54. package/lib/math/sequences/geometricSequence.js +8 -0
  55. package/lib/math/sequences/sequence.js +18 -0
  56. package/lib/math/sets/discreteSet.js +28 -0
  57. package/lib/math/sets/emptySet.js +6 -0
  58. package/lib/math/sets/intervals/intervals.js +104 -0
  59. package/lib/math/sets/mathSet.js +10 -0
  60. package/lib/math/sets/mathSetInterface.js +2 -0
  61. package/lib/math/trigonometry/remarkableValue.js +29 -0
  62. package/lib/math/trigonometry/remarkableValues.js +51 -0
  63. package/lib/math/utils/arithmetic/coprimesOf.js +13 -0
  64. package/lib/math/utils/arithmetic/dividersOf.js +12 -0
  65. package/lib/math/utils/arithmetic/gcd.js +7 -0
  66. package/lib/math/utils/arithmetic/isSquare.js +7 -0
  67. package/lib/math/utils/arithmetic/lcd.js +12 -0
  68. package/lib/math/utils/arithmetic/nonCoprimesOf.js +13 -0
  69. package/lib/math/utils/arithmetic/nonDividersOf.js +14 -0
  70. package/lib/math/utils/arithmetic/primeFactors.js +22 -0
  71. package/lib/math/utils/decimals/decimalPartLengthOf.js +14 -0
  72. package/lib/math/utils/random/randTupleInt.js +30 -0
  73. package/lib/math/utils/random/randint.js +18 -0
  74. package/lib/math/utils/round.js +8 -0
  75. package/lib/mathutils/random/randTupleInt.js +30 -0
  76. package/lib/sets/intervals/intervals.js +14 -18
  77. package/lib/tree/nodes/functions/cosNode.js +21 -0
  78. package/lib/tree/nodes/functions/functionNode.js +2 -0
  79. package/lib/tree/nodes/functions/oppositeNode.js +16 -3
  80. package/lib/tree/nodes/functions/sinNode.js +1 -0
  81. package/lib/tree/nodes/functions/sqrtNode.js +6 -3
  82. package/lib/tree/nodes/node.js +4 -3
  83. package/lib/tree/nodes/numbers/constantNode.js +18 -0
  84. package/lib/tree/nodes/numbers/numberNode.js +7 -3
  85. package/lib/tree/nodes/numbers/piNode.js +5 -0
  86. package/lib/tree/nodes/operators/addNode.js +7 -3
  87. package/lib/tree/nodes/operators/divideNode.js +20 -3
  88. package/lib/tree/nodes/operators/equalNode.js +6 -3
  89. package/lib/tree/nodes/operators/fractionNode.js +6 -3
  90. package/lib/tree/nodes/operators/multiplyNode.js +33 -3
  91. package/lib/tree/nodes/operators/operatorNode.js +5 -5
  92. package/lib/tree/nodes/operators/powerNode.js +23 -3
  93. package/lib/tree/nodes/operators/substractNode.js +14 -3
  94. package/lib/tree/nodes/variables/variableNode.js +9 -6
  95. package/lib/tree/parsers/derivateParser.js +61 -59
  96. package/lib/tree/parsers/latexParser.js +109 -116
  97. package/lib/tree/parsers/simplify.js +52 -0
  98. package/lib/trigonometry/remarkableValue.js +29 -0
  99. package/lib/trigonometry/remarkableValues.js +51 -0
  100. package/lib/utils/arrayEqual.js +13 -0
  101. package/lib/utils/coinFlip.js +7 -0
  102. package/package.json +1 -1
@@ -18,35 +18,35 @@ class Interval {
18
18
  /**
19
19
  * [[a; b]] pour un interval d'integer; [a;b] pour des réels
20
20
  */
21
- constructor(tex = "[-10; 10]") {
21
+ constructor(tex = '[-10; 10]') {
22
+ if (!tex.includes(';'))
23
+ throw Error('wrong interval format');
22
24
  this.tex = tex;
23
- const isInt = tex[1] === "[" || tex[1] === "]";
25
+ const isInt = tex[1] === '[' || tex[1] === ']';
24
26
  this.type = isInt ? nombre_1.NumberType.Integer : nombre_1.NumberType.Real;
25
27
  const left = tex[0];
26
28
  const right = tex[tex.length - 1];
27
- const [a, b] = tex
28
- .slice(isInt ? 2 : 1, isInt ? tex.length - 2 : tex.length - 1)
29
- .split(";");
29
+ const [a, b] = tex.slice(isInt ? 2 : 1, isInt ? tex.length - 2 : tex.length - 1).split(';');
30
30
  switch (`${left}a;b${right}`) {
31
- case "[a;b]":
31
+ case '[a;b]':
32
32
  this.boundType = BoundType.FF;
33
33
  break;
34
- case "]a;b[":
34
+ case ']a;b[':
35
35
  this.boundType = BoundType.OO;
36
36
  break;
37
- case "[a;b[":
37
+ case '[a;b[':
38
38
  this.boundType = BoundType.FO;
39
39
  break;
40
- case "]a;b]":
40
+ case ']a;b]':
41
41
  this.boundType = BoundType.OF;
42
42
  break;
43
43
  default:
44
- throw console.error("wrong interval");
44
+ throw console.error('wrong interval format');
45
45
  }
46
46
  function getBound(bound) {
47
- return bound === "-\\infty"
47
+ return bound === '-\\infty'
48
48
  ? Number.NEGATIVE_INFINITY
49
- : bound === "+\\infty"
49
+ : bound === '+\\infty'
50
50
  ? Number.POSITIVE_INFINITY
51
51
  : Number(bound);
52
52
  }
@@ -90,12 +90,8 @@ class Interval {
90
90
  getRandomElement(precision = this.type === nombre_1.NumberType.Integer ? 0 : 2) {
91
91
  if (this.min === -Infinity || this.max === Infinity)
92
92
  throw Error("Can't chose amongst infinity");
93
- let min = this.boundType === BoundType.OO || this.boundType === BoundType.OF
94
- ? this.min + epsilon_1.EPSILON
95
- : this.min;
96
- let max = this.boundType === BoundType.OO || this.boundType === BoundType.FO
97
- ? this.max - epsilon_1.EPSILON
98
- : this.max;
93
+ let min = this.boundType === BoundType.OO || this.boundType === BoundType.OF ? this.min + epsilon_1.EPSILON : this.min;
94
+ let max = this.boundType === BoundType.OO || this.boundType === BoundType.FO ? this.max - epsilon_1.EPSILON : this.max;
99
95
  const value = (0, round_1.round)(min + Math.random() * (max - this.min), precision);
100
96
  switch (this.type) {
101
97
  case nombre_1.NumberType.Integer:
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CosNode = void 0;
4
+ const node_1 = require("../node");
5
+ const functionNode_1 = require("./functionNode");
6
+ class CosNode extends functionNode_1.FunctionNode {
7
+ constructor(child) {
8
+ super(functionNode_1.FunctionsIds.cos, child, '\\cos');
9
+ this.type = node_1.NodeType.function;
10
+ }
11
+ toMathString() {
12
+ return `cos(${this.child.toMathString()})`;
13
+ }
14
+ toTex() {
15
+ return `\\cos\\left(${this.child.toTex()}\\right)`;
16
+ }
17
+ simplify() {
18
+ return this;
19
+ }
20
+ }
21
+ exports.CosNode = CosNode;
@@ -6,6 +6,8 @@ var FunctionsIds;
6
6
  (function (FunctionsIds) {
7
7
  FunctionsIds[FunctionsIds["opposite"] = 0] = "opposite";
8
8
  FunctionsIds[FunctionsIds["sqrt"] = 1] = "sqrt";
9
+ FunctionsIds[FunctionsIds["cos"] = 2] = "cos";
10
+ FunctionsIds[FunctionsIds["sin"] = 3] = "sin";
9
11
  })(FunctionsIds = exports.FunctionsIds || (exports.FunctionsIds = {}));
10
12
  class FunctionNode {
11
13
  constructor(id, child, tex) {
@@ -1,13 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OppositeNode = void 0;
4
+ const node_1 = require("../node");
5
+ const operatorNode_1 = require("../operators/operatorNode");
4
6
  const functionNode_1 = require("./functionNode");
5
7
  class OppositeNode extends functionNode_1.FunctionNode {
6
8
  constructor(child) {
7
- super(functionNode_1.FunctionsIds.opposite, child, "-");
9
+ super(functionNode_1.FunctionsIds.opposite, child, '-');
8
10
  }
9
- toString() {
10
- return `-(${this.child})`;
11
+ toMathString() {
12
+ return `-(${this.child.toMathString()})`;
13
+ }
14
+ toTex() {
15
+ let childTex = this.child.toTex();
16
+ let needBrackets = childTex[0] === '-';
17
+ if (this.child.type === node_1.NodeType.operator) {
18
+ const operatorChild = this.child;
19
+ needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorChild.id));
20
+ }
21
+ if (needBrackets)
22
+ childTex = `(${childTex})`;
23
+ return `-${childTex}`;
11
24
  }
12
25
  }
13
26
  exports.OppositeNode = OppositeNode;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -5,11 +5,14 @@ const node_1 = require("../node");
5
5
  const functionNode_1 = require("./functionNode");
6
6
  class SqrtNode extends functionNode_1.FunctionNode {
7
7
  constructor(child) {
8
- super(functionNode_1.FunctionsIds.sqrt, child, "\\sqrt");
8
+ super(functionNode_1.FunctionsIds.sqrt, child, '\\sqrt');
9
9
  this.type = node_1.NodeType.function;
10
10
  }
11
- toString() {
12
- return `sqrt(${this.child})`;
11
+ toMathString() {
12
+ return `sqrt(${this.child.toMathString()})`;
13
+ }
14
+ toTex() {
15
+ return `\\sqrt{${this.child.toTex()}}`;
13
16
  }
14
17
  }
15
18
  exports.SqrtNode = SqrtNode;
@@ -4,7 +4,8 @@ exports.NodeType = void 0;
4
4
  var NodeType;
5
5
  (function (NodeType) {
6
6
  NodeType[NodeType["number"] = 0] = "number";
7
- NodeType[NodeType["variable"] = 1] = "variable";
8
- NodeType[NodeType["operator"] = 2] = "operator";
9
- NodeType[NodeType["function"] = 3] = "function";
7
+ NodeType[NodeType["constant"] = 1] = "constant";
8
+ NodeType[NodeType["variable"] = 2] = "variable";
9
+ NodeType[NodeType["operator"] = 3] = "operator";
10
+ NodeType[NodeType["function"] = 4] = "function";
10
11
  })(NodeType = exports.NodeType || (exports.NodeType = {}));
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstantNode = void 0;
4
+ const node_1 = require("../node");
5
+ class ConstantNode {
6
+ constructor(tex, mathString) {
7
+ this.type = node_1.NodeType.constant;
8
+ this.tex = tex;
9
+ this.mathString = mathString;
10
+ }
11
+ toMathString() {
12
+ return `${this.mathString}`;
13
+ }
14
+ toTex() {
15
+ return `${this.tex}`;
16
+ }
17
+ }
18
+ exports.ConstantNode = ConstantNode;
@@ -3,12 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberNode = void 0;
4
4
  const node_1 = require("../node");
5
5
  class NumberNode {
6
- constructor(value, tex) {
6
+ constructor(value, tex, mathString) {
7
7
  this.type = node_1.NodeType.number;
8
8
  this.value = value;
9
- this.tex = tex || value + "";
9
+ this.tex = tex || value + '';
10
+ this.mathString = mathString || this.tex;
10
11
  }
11
- toString() {
12
+ toMathString() {
13
+ return `${this.mathString ? this.mathString : this.tex}`;
14
+ }
15
+ toTex() {
12
16
  return `${this.tex}`;
13
17
  }
14
18
  }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PiNode = void 0;
4
+ const constantNode_1 = require("./constantNode");
5
+ exports.PiNode = new constantNode_1.ConstantNode('\\pi', 'pi');
@@ -4,12 +4,16 @@ exports.AddNode = void 0;
4
4
  const operatorNode_1 = require("./operatorNode");
5
5
  class AddNode extends operatorNode_1.OperatorNode {
6
6
  constructor(leftChild, rightChild) {
7
- super(operatorNode_1.OperatorIds.add, leftChild, rightChild, true, "+");
7
+ super(operatorNode_1.OperatorIds.add, leftChild, rightChild, true, '+');
8
8
  this.leftChild = leftChild;
9
9
  this.rightChild = rightChild;
10
10
  }
11
- toString() {
12
- return `${this.leftChild} + ${this.rightChild}`;
11
+ toMathString() {
12
+ return `${this.leftChild.toMathString()} + (${this.rightChild.toMathString()})`;
13
+ }
14
+ toTex() {
15
+ const rightTex = this.rightChild.toTex();
16
+ return `${this.leftChild.toTex()} ${rightTex[0] === '-' ? '' : '+ '}${rightTex}`;
13
17
  }
14
18
  }
15
19
  exports.AddNode = AddNode;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DivideNode = void 0;
4
+ const node_1 = require("../node");
4
5
  const operatorNode_1 = require("./operatorNode");
5
6
  class DivideNode extends operatorNode_1.OperatorNode {
6
7
  /**
@@ -8,10 +9,26 @@ class DivideNode extends operatorNode_1.OperatorNode {
8
9
  * @param rightChild denum
9
10
  */
10
11
  constructor(leftChild, rightChild) {
11
- super(operatorNode_1.OperatorIds.divide, leftChild, rightChild, false, "\\div");
12
+ super(operatorNode_1.OperatorIds.divide, leftChild, rightChild, false, '\\div');
12
13
  }
13
- toString() {
14
- return `(${this.leftChild}) \\div (${this.rightChild})`;
14
+ toMathString() {
15
+ return `(${this.leftChild.toMathString()}) / (${this.rightChild.toMathString()})`;
16
+ }
17
+ toTex() {
18
+ let rightTex = this.rightChild.toTex();
19
+ let leftTex = this.leftChild.toTex();
20
+ if (this.leftChild.type === node_1.NodeType.operator) {
21
+ if ([operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract, operatorNode_1.OperatorIds.multiply].includes(this.leftChild.id))
22
+ leftTex = `(${leftTex})`;
23
+ }
24
+ let needBrackets = rightTex[0] === '-';
25
+ if (this.rightChild.type === node_1.NodeType.operator) {
26
+ const operatorRightChild = this.rightChild;
27
+ needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorRightChild.id));
28
+ }
29
+ if (needBrackets)
30
+ rightTex = `(${rightTex})`;
31
+ return `${leftTex} \\div ${rightTex}`;
15
32
  }
16
33
  }
17
34
  exports.DivideNode = DivideNode;
@@ -4,10 +4,13 @@ exports.EqualNode = void 0;
4
4
  const operatorNode_1 = require("./operatorNode");
5
5
  class EqualNode extends operatorNode_1.OperatorNode {
6
6
  constructor(leftChild, rightChild) {
7
- super(operatorNode_1.OperatorIds.equal, leftChild, rightChild, true, "=");
7
+ super(operatorNode_1.OperatorIds.equal, leftChild, rightChild, true, '=');
8
8
  }
9
- toString() {
10
- return `${this.leftChild} = ${this.rightChild}`;
9
+ toMathString() {
10
+ return `${this.leftChild.toMathString()} = ${this.rightChild.toMathString()}`;
11
+ }
12
+ toTex() {
13
+ return `${this.leftChild.toTex()} = ${this.rightChild.toTex()}`;
11
14
  }
12
15
  }
13
16
  exports.EqualNode = EqualNode;
@@ -8,10 +8,13 @@ class FractionNode extends operatorNode_1.OperatorNode {
8
8
  * @param rightChild denum
9
9
  */
10
10
  constructor(leftChild, rightChild) {
11
- super(operatorNode_1.OperatorIds.fraction, leftChild, rightChild, false, "\\frac");
11
+ super(operatorNode_1.OperatorIds.fraction, leftChild, rightChild, false, '\\frac');
12
12
  }
13
- toString() {
14
- return `\\frac{${this.leftChild}}{${this.rightChild}}`;
13
+ toMathString() {
14
+ return `(${this.leftChild.toMathString()}) / (${this.rightChild.toMathString()})`;
15
+ }
16
+ toTex() {
17
+ return `\\frac{${this.leftChild.toTex()}}{${this.rightChild.toTex()}}`;
15
18
  }
16
19
  }
17
20
  exports.FractionNode = FractionNode;
@@ -1,13 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MultiplyNode = void 0;
4
+ const functionNode_1 = require("../functions/functionNode");
5
+ const node_1 = require("../node");
4
6
  const operatorNode_1 = require("./operatorNode");
5
7
  class MultiplyNode extends operatorNode_1.OperatorNode {
6
8
  constructor(leftChild, rightChild) {
7
- super(operatorNode_1.OperatorIds.multiply, leftChild, rightChild, true, "\\times");
9
+ let [left, right] = [leftChild, rightChild];
10
+ const shouldSwitch = (rightChild.type === node_1.NodeType.function && rightChild.id === functionNode_1.FunctionsIds.opposite) ||
11
+ (leftChild.type === node_1.NodeType.constant && rightChild.type === node_1.NodeType.number);
12
+ if (shouldSwitch) {
13
+ [left, right] = [rightChild, leftChild];
14
+ }
15
+ super(operatorNode_1.OperatorIds.multiply, left, right, true, '\\times');
8
16
  }
9
- toString() {
10
- return `(${this.leftChild})*(${this.rightChild})`;
17
+ toMathString() {
18
+ return `(${this.leftChild.toMathString()})*(${this.rightChild.toMathString()})`;
19
+ }
20
+ toTex() {
21
+ let leftTex = this.leftChild.toTex();
22
+ let rightTex = this.rightChild.toTex();
23
+ if (this.leftChild.type === node_1.NodeType.operator) {
24
+ if ([operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract, operatorNode_1.OperatorIds.divide].includes(this.leftChild.id))
25
+ leftTex = `(${leftTex})`;
26
+ }
27
+ let needBrackets = rightTex[0] === '-';
28
+ if (this.rightChild.type === node_1.NodeType.operator) {
29
+ const operatorRightChild = this.rightChild;
30
+ needBrackets || (needBrackets = [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(operatorRightChild.id));
31
+ }
32
+ if (needBrackets)
33
+ rightTex = `(${rightTex})`;
34
+ // permet de gérer le cas 3*2^x par ex
35
+ let showTimesSign = !isNaN(+rightTex[0]) || this.rightChild.type === node_1.NodeType.number;
36
+ if (this.rightChild.type === node_1.NodeType.operator) {
37
+ const operatorRightChild = this.rightChild;
38
+ showTimesSign || (showTimesSign = [operatorNode_1.OperatorIds.fraction].includes(operatorRightChild.id));
39
+ }
40
+ return `${leftTex}${showTimesSign ? '\\times ' : ''}${rightTex}`;
11
41
  }
12
42
  }
13
43
  exports.MultiplyNode = MultiplyNode;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperatorNode = exports.OperatorIds = void 0;
4
- const coin_1 = require("../../../utils/coin");
4
+ const coinFlip_1 = require("../../../utils/coinFlip");
5
5
  const node_1 = require("../node");
6
6
  var OperatorIds;
7
7
  (function (OperatorIds) {
@@ -25,11 +25,11 @@ class OperatorNode {
25
25
  /**shuffles in place */
26
26
  shuffle() {
27
27
  if (!this.isCommutative)
28
- return this;
29
- if ((0, coin_1.coin)())
30
- return this;
28
+ return;
29
+ if ((0, coinFlip_1.coinFlip)())
30
+ return;
31
31
  [this.leftChild, this.rightChild] = [this.rightChild, this.leftChild];
32
- return this;
32
+ return;
33
33
  }
34
34
  }
35
35
  exports.OperatorNode = OperatorNode;
@@ -1,13 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PowerNode = void 0;
4
+ const node_1 = require("../node");
4
5
  const operatorNode_1 = require("./operatorNode");
5
6
  class PowerNode extends operatorNode_1.OperatorNode {
6
7
  constructor(leftChild, rightChild) {
7
- super(operatorNode_1.OperatorIds.power, leftChild, rightChild, false, "^");
8
+ super(operatorNode_1.OperatorIds.power, leftChild, rightChild, false, '^');
8
9
  }
9
- toString() {
10
- return `(${this.leftChild})^{${this.rightChild}}`;
10
+ toMathString() {
11
+ return `(${this.leftChild.toMathString()})^(${this.rightChild.toMathString()})`;
12
+ }
13
+ toTex() {
14
+ let rightTex = this.rightChild.toTex();
15
+ let leftTex = this.leftChild.toTex();
16
+ let needBrackets = leftTex[0] === '-';
17
+ if (this.leftChild.type === node_1.NodeType.operator) {
18
+ const childOperator = this.leftChild;
19
+ needBrackets || (needBrackets = [
20
+ operatorNode_1.OperatorIds.add,
21
+ operatorNode_1.OperatorIds.substract,
22
+ operatorNode_1.OperatorIds.multiply,
23
+ operatorNode_1.OperatorIds.divide,
24
+ operatorNode_1.OperatorIds.fraction,
25
+ operatorNode_1.OperatorIds.power,
26
+ ].includes(childOperator.id));
27
+ }
28
+ if (needBrackets)
29
+ leftTex = `(${leftTex})`;
30
+ return `${leftTex}^{${rightTex}}`;
11
31
  }
12
32
  }
13
33
  exports.PowerNode = PowerNode;
@@ -1,13 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SubstractNode = void 0;
4
+ const node_1 = require("../node");
4
5
  const operatorNode_1 = require("./operatorNode");
5
6
  class SubstractNode extends operatorNode_1.OperatorNode {
6
7
  constructor(leftChild, rightChild) {
7
- super(operatorNode_1.OperatorIds.substract, leftChild, rightChild, false, "-");
8
+ super(operatorNode_1.OperatorIds.substract, leftChild, rightChild, false, '-');
8
9
  }
9
- toString() {
10
- return `${this.leftChild}-(${this.rightChild})`;
10
+ toMathString() {
11
+ return `${this.leftChild.toMathString()}-(${this.rightChild.toMathString()})`;
12
+ }
13
+ toTex() {
14
+ let rightTex = this.rightChild.toTex();
15
+ let leftTex = this.leftChild.toTex();
16
+ const needBrackets = (this.rightChild.type === node_1.NodeType.operator &&
17
+ [operatorNode_1.OperatorIds.add, operatorNode_1.OperatorIds.substract].includes(this.rightChild.id)) ||
18
+ rightTex[0] === '-';
19
+ if (needBrackets)
20
+ rightTex = `(${rightTex})`;
21
+ return `${leftTex} - ${rightTex}`;
11
22
  }
12
23
  }
13
24
  exports.SubstractNode = SubstractNode;
@@ -3,14 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VariableNode = void 0;
4
4
  const node_1 = require("../node");
5
5
  class VariableNode {
6
- constructor(tex) {
6
+ constructor(name) {
7
7
  this.type = node_1.NodeType.variable;
8
- if (tex.length !== 1 || !tex.match("[a-zA-Z]"))
9
- throw Error("variable must be a letter");
10
- this.tex = tex;
8
+ if (name.length !== 1 || !name.match('[a-zA-Z]'))
9
+ throw Error('variable must be a letter');
10
+ this.name = name;
11
11
  }
12
- toString() {
13
- return `${this.tex}`;
12
+ toTex() {
13
+ return `${this.name}`;
14
+ }
15
+ toMathString() {
16
+ return `${this.name}`;
14
17
  }
15
18
  }
16
19
  exports.VariableNode = VariableNode;
@@ -1,60 +1,62 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.derivateParser = void 0;
4
- const node_1 = require("../nodes/node");
5
- const numberNode_1 = require("../nodes/numbers/numberNode");
6
- const powerNode_1 = require("../nodes/operators/powerNode");
7
- const addNode_1 = require("../nodes/operators/addNode");
8
- const fractionNode_1 = require("../nodes/operators/fractionNode");
9
- const multiplyNode_1 = require("../nodes/operators/multiplyNode");
10
- const operatorNode_1 = require("../nodes/operators/operatorNode");
11
- const substractNode_1 = require("../nodes/operators/substractNode");
12
- const functionNode_1 = require("../nodes/functions/functionNode");
13
- const sqrtNode_1 = require("../nodes/functions/sqrtNode");
14
- const oppositeNode_1 = require("../nodes/functions/oppositeNode");
15
- function derivateParser(node) {
16
- if (!node)
17
- throw Error("encountered a null node ??");
18
- switch (node.type) {
19
- case node_1.NodeType.variable:
20
- return new numberNode_1.NumberNode(1);
21
- case node_1.NodeType.number:
22
- return new numberNode_1.NumberNode(0);
23
- case node_1.NodeType.operator:
24
- const operatorNode = node;
25
- const u = operatorNode.leftChild;
26
- const v = operatorNode.rightChild;
27
- switch (operatorNode.id) {
28
- case operatorNode_1.OperatorIds.add:
29
- return new addNode_1.AddNode(derivateParser(u), derivateParser(v));
30
- case operatorNode_1.OperatorIds.substract: {
31
- return new substractNode_1.SubstractNode(derivateParser(u), derivateParser(v));
32
- }
33
- case operatorNode_1.OperatorIds.multiply: {
34
- return new addNode_1.AddNode(new multiplyNode_1.MultiplyNode(derivateParser(u), v), new multiplyNode_1.MultiplyNode(u, derivateParser(v)));
35
- }
36
- case operatorNode_1.OperatorIds.divide:
37
- case operatorNode_1.OperatorIds.fraction:
38
- return new fractionNode_1.FractionNode(new substractNode_1.SubstractNode(new multiplyNode_1.MultiplyNode(derivateParser(u), v), new multiplyNode_1.MultiplyNode(u, derivateParser(v))), new powerNode_1.PowerNode(v, new numberNode_1.NumberNode(2)));
39
- case operatorNode_1.OperatorIds.power: {
40
- const operatorNode = node;
41
- const n = operatorNode.rightChild;
42
- const u = operatorNode.leftChild;
43
- return new multiplyNode_1.MultiplyNode(n, new multiplyNode_1.MultiplyNode(derivateParser(u), new powerNode_1.PowerNode(u, new numberNode_1.NumberNode(n.value - 1))));
44
- }
45
- }
46
- case node_1.NodeType.function: {
47
- const functionNode = node;
48
- const child = functionNode.child;
49
- switch (functionNode.id) {
50
- case functionNode_1.FunctionsIds.sqrt: {
51
- return new fractionNode_1.FractionNode(derivateParser(child), new multiplyNode_1.MultiplyNode(new numberNode_1.NumberNode(2), new sqrtNode_1.SqrtNode(child)));
52
- }
53
- case functionNode_1.FunctionsIds.opposite: {
54
- return new oppositeNode_1.OppositeNode(derivateParser(child));
55
- }
56
- }
57
- }
58
- }
59
- }
60
- exports.derivateParser = derivateParser;
2
+ // import { Node, NodeType } from "../nodes/node";
3
+ // import { NumberNode } from "../nodes/numbers/numberNode";
4
+ // import { PowerNode } from "../nodes/operators/powerNode";
5
+ // import { AddNode } from "../nodes/operators/addNode";
6
+ // import { FractionNode } from "../nodes/operators/fractionNode";
7
+ // import { MultiplyNode } from "../nodes/operators/multiplyNode";
8
+ // import { OperatorIds, OperatorNode } from "../nodes/operators/operatorNode";
9
+ // import { SubstractNode } from "../nodes/operators/substractNode";
10
+ // import { FunctionNode, FunctionsIds } from "../nodes/functions/functionNode";
11
+ // import { SqrtNode } from "../nodes/functions/sqrtNode";
12
+ // import { OppositeNode } from "../nodes/functions/oppositeNode";
13
+ // export function derivateParser(node: Node): Node {
14
+ // if (!node) throw Error("encountered a null node ??");
15
+ // switch (node.type) {
16
+ // case NodeType.variable:
17
+ // return new NumberNode(1);
18
+ // case NodeType.number:
19
+ // return new NumberNode(0);
20
+ // case NodeType.operator:
21
+ // const operatorNode = node as OperatorNode;
22
+ // const u = operatorNode.leftChild;
23
+ // const v = operatorNode.rightChild;
24
+ // switch (operatorNode.id) {
25
+ // case OperatorIds.add:
26
+ // return new AddNode(derivateParser(u), derivateParser(v));
27
+ // case OperatorIds.substract: {
28
+ // return new SubstractNode(derivateParser(u), derivateParser(v));
29
+ // }
30
+ // case OperatorIds.multiply: {
31
+ // return new AddNode(new MultiplyNode(derivateParser(u), v), new MultiplyNode(u, derivateParser(v)));
32
+ // }
33
+ // case OperatorIds.divide:
34
+ // case OperatorIds.fraction:
35
+ // return new FractionNode(
36
+ // new SubstractNode(new MultiplyNode(derivateParser(u), v), new MultiplyNode(u, derivateParser(v))),
37
+ // new PowerNode(v, new NumberNode(2))
38
+ // );
39
+ // case OperatorIds.power: {
40
+ // const operatorNode = node as OperatorNode;
41
+ // const n = operatorNode.rightChild as NumberNode;
42
+ // const u = operatorNode.leftChild;
43
+ // return new MultiplyNode(
44
+ // n,
45
+ // new MultiplyNode(derivateParser(u), new PowerNode(u, new NumberNode(n.value - 1)))
46
+ // );
47
+ // }
48
+ // }
49
+ // case NodeType.function: {
50
+ // const functionNode = node as FunctionNode;
51
+ // const child = functionNode.child;
52
+ // switch (functionNode.id) {
53
+ // case FunctionsIds.sqrt: {
54
+ // return new FractionNode(derivateParser(child), new MultiplyNode(new NumberNode(2), new SqrtNode(child)));
55
+ // }
56
+ // case FunctionsIds.opposite: {
57
+ // return new OppositeNode(derivateParser(child));
58
+ // }
59
+ // }
60
+ // }
61
+ // }
62
+ // }