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
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMainRemarkableValues = exports.mainRemarkableValuesExercise = void 0;
4
+ const remarkableValue_1 = require("src/math/trigonometry/remarkableValue");
5
+ const coinFlip_1 = require("src/utils/coinFlip");
6
+ const getDistinctQuestions_1 = require("../utils/getDistinctQuestions");
7
+ exports.mainRemarkableValuesExercise = {
8
+ id: 'mainRemarkableValues',
9
+ connector: '=',
10
+ instruction: 'Donner la valeur exacte :',
11
+ label: 'Valeurs remarquables de cos et sin sur [-\\pi, \\pi]',
12
+ levels: ['1', '0'],
13
+ isSingleStep: false,
14
+ section: 'Trigonométrie',
15
+ generator: (nb) => (0, getDistinctQuestions_1.getDistinctQuestions)(getMainRemarkableValues, nb),
16
+ };
17
+ function getMainRemarkableValues() {
18
+ const isCos = (0, coinFlip_1.coinFlip)();
19
+ const remarkableValue = remarkableValue_1.RemarkableValueConstructor.mainInterval();
20
+ const question = {
21
+ startStatement: isCos
22
+ ? `\\cos\\left(${remarkableValue.angle.toTex()}\\right)`
23
+ : `\\sin\\left(${remarkableValue.angle.toTex()}\\right)`,
24
+ answer: isCos ? remarkableValue.cos.toTex() : remarkableValue.sin.toTex(),
25
+ };
26
+ return question;
27
+ }
28
+ exports.getMainRemarkableValues = getMainRemarkableValues;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRemarkableValues = exports.remarkableValuesExercise = void 0;
4
+ const remarkableValue_1 = require("src/math/trigonometry/remarkableValue");
5
+ const coinFlip_1 = require("src/utils/coinFlip");
6
+ const getDistinctQuestions_1 = require("../utils/getDistinctQuestions");
7
+ exports.remarkableValuesExercise = {
8
+ id: 'remarkableValues',
9
+ connector: '=',
10
+ instruction: 'Donner la valeur exacte :',
11
+ label: 'Valeurs remarquables de cos et sin',
12
+ levels: ['1', '0'],
13
+ isSingleStep: false,
14
+ section: 'Trigonométrie',
15
+ generator: (nb) => (0, getDistinctQuestions_1.getDistinctQuestions)(getRemarkableValues, nb),
16
+ };
17
+ function getRemarkableValues() {
18
+ const isCos = (0, coinFlip_1.coinFlip)();
19
+ const remarkableValue = remarkableValue_1.RemarkableValueConstructor.simplifiable();
20
+ const question = {
21
+ startStatement: isCos
22
+ ? `\\cos\\left(${remarkableValue.angle.toTex()}\\right)`
23
+ : `\\sin\\left(${remarkableValue.angle.toTex()}\\right)`,
24
+ answer: isCos ? remarkableValue.cos.toTex() : remarkableValue.sin.toTex(),
25
+ };
26
+ return question;
27
+ }
28
+ exports.getRemarkableValues = getRemarkableValues;
@@ -7,7 +7,7 @@ const getDistinctQuestions = (generator, nb) => {
7
7
  let question;
8
8
  do {
9
9
  question = generator();
10
- } while (res.some((q) => q.startStatement === question.startStatement));
10
+ } while (res.some((q) => q.instruction === question.instruction && q.startStatement === question.startStatement));
11
11
  res.push(question);
12
12
  }
13
13
  return res;
@@ -1 +1,27 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Point = exports.PointConstructor = void 0;
4
+ const numberNode_1 = require("../tree/nodes/numbers/numberNode");
5
+ const addNode_1 = require("../tree/nodes/operators/addNode");
6
+ const fractionNode_1 = require("../tree/nodes/operators/fractionNode");
7
+ const simplify_1 = require("../tree/parsers/simplify");
8
+ class PointConstructor {
9
+ }
10
+ exports.PointConstructor = PointConstructor;
11
+ class Point {
12
+ constructor(name, x, y) {
13
+ this.name = name;
14
+ this.x = x;
15
+ this.y = y;
16
+ }
17
+ toTex() {
18
+ return `${this.name}`;
19
+ }
20
+ toTexWithCoords() {
21
+ return `${this.name}\\left(${this.x.toTex()}; ${this.y.toTex()}\\right)`;
22
+ }
23
+ midpoint(B, name = 'I') {
24
+ return new Point(name, (0, simplify_1.simplifyNode)(new fractionNode_1.FractionNode(new addNode_1.AddNode(this.x, B.x), new numberNode_1.NumberNode(2))), (0, simplify_1.simplifyNode)(new fractionNode_1.FractionNode(new addNode_1.AddNode(this.y, B.y), new numberNode_1.NumberNode(2))));
25
+ }
26
+ }
27
+ exports.Point = Point;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Vector = exports.VectorConstructor = void 0;
4
+ const addNode_1 = require("../tree/nodes/operators/addNode");
5
+ const multiplyNode_1 = require("../tree/nodes/operators/multiplyNode");
6
+ const substractNode_1 = require("../tree/nodes/operators/substractNode");
7
+ const simplify_1 = require("../tree/parsers/simplify");
8
+ class VectorConstructor {
9
+ static fromPoints(origin, end) {
10
+ return new Vector(`${origin.name}${end.name}`, new substractNode_1.SubstractNode(end.x, origin.x), new substractNode_1.SubstractNode(end.y, origin.y));
11
+ }
12
+ }
13
+ exports.VectorConstructor = VectorConstructor;
14
+ class Vector {
15
+ constructor(name, x, y) {
16
+ this.name = name;
17
+ this.tex = `\\overrightarrow{${name}}`;
18
+ this.x = x;
19
+ this.y = y;
20
+ }
21
+ toTex() {
22
+ return `\\overrightarrow{${this.name}}`;
23
+ }
24
+ toTexWithCoords() {
25
+ return `\\overrightarrow{${this.name}}\\begin{pmatrix}${this.x.toTex()} \\\\ ${this.y.toTex()} \\end{pmatrix}`;
26
+ }
27
+ scalarProduct(v) {
28
+ return (0, simplify_1.simplifyNode)(new addNode_1.AddNode(new multiplyNode_1.MultiplyNode(this.x, v.x), new multiplyNode_1.MultiplyNode(this.y, v.y)));
29
+ }
30
+ }
31
+ exports.Vector = Vector;
package/lib/index.js CHANGED
@@ -3,3 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.exercises = void 0;
4
4
  const exercises_1 = require("./exercises/exercises");
5
5
  Object.defineProperty(exports, "exercises", { enumerable: true, get: function () { return exercises_1.exercises; } });
6
+ exercises_1.exercises.forEach((exo) => console.log(exo.generator(10)));
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Point = exports.PointConstructor = void 0;
4
+ const numberNode_1 = require("src/tree/nodes/numbers/numberNode");
5
+ const addNode_1 = require("src/tree/nodes/operators/addNode");
6
+ const fractionNode_1 = require("src/tree/nodes/operators/fractionNode");
7
+ const simplify_1 = require("src/tree/parsers/simplify");
8
+ class PointConstructor {
9
+ }
10
+ exports.PointConstructor = PointConstructor;
11
+ class Point {
12
+ constructor(name, x, y) {
13
+ this.name = name;
14
+ this.x = x;
15
+ this.y = y;
16
+ }
17
+ toTex() {
18
+ return `${this.name}`;
19
+ }
20
+ toTexWithCoords() {
21
+ return `${this.name}\\left(${this.x.toTex()}; ${this.y.toTex()}\\right)`;
22
+ }
23
+ midpoint(B, name = 'I') {
24
+ return new Point(name, (0, simplify_1.simplifyNode)(new fractionNode_1.FractionNode(new addNode_1.AddNode(this.x, B.x), new numberNode_1.NumberNode(2))), (0, simplify_1.simplifyNode)(new fractionNode_1.FractionNode(new addNode_1.AddNode(this.y, B.y), new numberNode_1.NumberNode(2))));
25
+ }
26
+ }
27
+ exports.Point = Point;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Vector = exports.VectorConstructor = void 0;
4
+ const addNode_1 = require("../tree/nodes/operators/addNode");
5
+ const multiplyNode_1 = require("../tree/nodes/operators/multiplyNode");
6
+ const substractNode_1 = require("../tree/nodes/operators/substractNode");
7
+ const simplify_1 = require("../tree/parsers/simplify");
8
+ class VectorConstructor {
9
+ static fromPoints(origin, end) {
10
+ return new Vector(`${origin.name}${end.name}`, new substractNode_1.SubstractNode(end.x, origin.x), new substractNode_1.SubstractNode(end.y, origin.y));
11
+ }
12
+ }
13
+ exports.VectorConstructor = VectorConstructor;
14
+ class Vector {
15
+ constructor(name, x, y) {
16
+ this.name = name;
17
+ this.tex = `\\overrightarrow{${name}}`;
18
+ this.x = x;
19
+ this.y = y;
20
+ }
21
+ toTex() {
22
+ return `\\overrightarrow{${this.name}}`;
23
+ }
24
+ toTexWithCoords() {
25
+ return `\\overrightarrow{${this.name}}\\begin{pmatrix}${this.x.toTex()} \\\\ ${this.y.toTex()} \\end{pmatrix}`;
26
+ }
27
+ scalarProduct(v) {
28
+ return (0, simplify_1.simplifyNode)(new addNode_1.AddNode(new multiplyNode_1.MultiplyNode(this.x, v.x), new multiplyNode_1.MultiplyNode(this.y, v.y)));
29
+ }
30
+ }
31
+ exports.Vector = Vector;
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Decimal = exports.DecimalConstructor = void 0;
4
+ const randint_1 = require("../../mathutils/random/randint");
5
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
6
+ const integer_1 = require("../integer/integer");
7
+ const nombre_1 = require("../nombre");
8
+ class DecimalConstructor {
9
+ static randomFracPart(precision) {
10
+ let decimals = "";
11
+ for (let i = 0; i < precision; i++) {
12
+ decimals += (0, randint_1.randint)(i === precision - 1 ? 1 : 0, 10);
13
+ }
14
+ return decimals;
15
+ }
16
+ static random(min, max, precision) {
17
+ const int = (0, randint_1.randint)(min, max) + "";
18
+ const decimals = DecimalConstructor.randomFracPart(precision);
19
+ return DecimalConstructor.fromParts(int, decimals);
20
+ }
21
+ static fromParts(intPart, decimalPart) {
22
+ return new Decimal(Number("" + intPart + "." + decimalPart));
23
+ }
24
+ }
25
+ exports.DecimalConstructor = DecimalConstructor;
26
+ class Decimal {
27
+ constructor(value) {
28
+ this.type = nombre_1.NumberType.Decimal;
29
+ this.value = value;
30
+ this.tex = value + "";
31
+ let [intPartString, decimalPartString] = (value + "").split(".");
32
+ this.intPart = Number(intPartString);
33
+ this.decimalPart = decimalPartString || "";
34
+ this.precision = this.decimalPart.length;
35
+ }
36
+ /**
37
+ *
38
+ * @param precision 0 = unité, 1 = dixieme, ... , -1 : dizaine
39
+ * @returns
40
+ */
41
+ round(precision) {
42
+ const intPartString = this.intPart + "";
43
+ if (precision < 0) {
44
+ if (precision < -intPartString.length)
45
+ throw Error("can't round to higher precision");
46
+ return new integer_1.Integer(this.intPart).round(-precision);
47
+ }
48
+ if (precision > this.precision)
49
+ throw Error("can't round to higher precision");
50
+ if (precision === this.precision)
51
+ return this;
52
+ let newFracPart = "", newIntPart = "";
53
+ const shouldRoundUp = Number(this.decimalPart[precision]) > 4;
54
+ if (shouldRoundUp) {
55
+ let retenue = true;
56
+ let i = precision - 1;
57
+ while (retenue) {
58
+ if (i > -1) {
59
+ const nb = (Number(this.decimalPart[i]) + 1) % 10;
60
+ if (nb || newFracPart) {
61
+ newFracPart = nb.toString() + newFracPart;
62
+ }
63
+ if (nb !== 0) {
64
+ retenue = false;
65
+ for (let j = i - 1; j > -1; j--) {
66
+ newFracPart = this.decimalPart[j] + newFracPart;
67
+ }
68
+ newIntPart = intPartString;
69
+ }
70
+ else
71
+ i--;
72
+ }
73
+ else {
74
+ const nb = (Number(intPartString[i + intPartString.length]) + 1) % 10;
75
+ newIntPart = nb + "" + newIntPart;
76
+ if (nb !== 0) {
77
+ retenue = false;
78
+ for (let j = i + intPartString.length - 1; j > -1; j--) {
79
+ newIntPart = intPartString[j] + newIntPart;
80
+ }
81
+ }
82
+ else
83
+ i--;
84
+ }
85
+ }
86
+ }
87
+ else {
88
+ let retenue = true;
89
+ let i = precision - 1;
90
+ while (retenue) {
91
+ if (i > -1) {
92
+ const nb = Number(this.decimalPart[i]);
93
+ if (nb || newFracPart) {
94
+ newFracPart = nb.toString() + newFracPart;
95
+ }
96
+ if (nb !== 0) {
97
+ retenue = false;
98
+ for (let j = i - 1; j > -1; j--) {
99
+ newFracPart = this.decimalPart[j] + newFracPart;
100
+ }
101
+ newIntPart = intPartString;
102
+ }
103
+ else
104
+ i--;
105
+ }
106
+ else {
107
+ newIntPart = intPartString;
108
+ retenue = false;
109
+ }
110
+ }
111
+ }
112
+ return DecimalConstructor.fromParts(newIntPart, newFracPart);
113
+ }
114
+ multiplyByPowerOfTen(power) {
115
+ let newIntPart = "", newFracPart = "";
116
+ if (power > -1) {
117
+ newIntPart = this.intPart + "";
118
+ for (let i = 0; i < power; i++) {
119
+ newIntPart += i > this.decimalPart.length - 1 ? "0" : this.decimalPart[i];
120
+ }
121
+ newFracPart = this.decimalPart.slice(power);
122
+ }
123
+ else {
124
+ const intPartString = this.intPart + "";
125
+ newFracPart = this.decimalPart;
126
+ for (let i = intPartString.length - 1; i > intPartString.length - 1 + power; i--) {
127
+ newFracPart = (i < 0 ? "0" : intPartString[i]) + newFracPart;
128
+ }
129
+ if (power + intPartString.length < 1)
130
+ newIntPart = "0";
131
+ else
132
+ newIntPart = intPartString.slice(0, power + intPartString.length);
133
+ }
134
+ return DecimalConstructor.fromParts(newIntPart, newFracPart);
135
+ }
136
+ toTree() {
137
+ return new numberNode_1.NumberNode(this.value);
138
+ }
139
+ }
140
+ exports.Decimal = Decimal;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EPSILON = void 0;
4
+ function getEpsilon() {
5
+ var e = 1.0;
6
+ while (1.0 + 0.5 * e !== 1.0)
7
+ e *= 0.5;
8
+ return e;
9
+ }
10
+ exports.EPSILON = getEpsilon();
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Integer = exports.IntegerConstructor = void 0;
4
+ const randint_1 = require("../../mathutils/random/randint");
5
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
6
+ const nombre_1 = require("../nombre");
7
+ const rational_1 = require("../rationals/rational");
8
+ class IntegerConstructor {
9
+ static random(nbOfDigits) {
10
+ return (0, randint_1.randint)(0, 10 ** nbOfDigits);
11
+ }
12
+ }
13
+ exports.IntegerConstructor = IntegerConstructor;
14
+ class Integer {
15
+ constructor(value, tex) {
16
+ this.value = value;
17
+ this.tex = tex || value + "";
18
+ this.type = nombre_1.NumberType.Integer;
19
+ }
20
+ toTree() {
21
+ return new numberNode_1.NumberNode(this.value, this.tex);
22
+ }
23
+ round(precision) {
24
+ const intString = this.value + "";
25
+ if (precision >= intString.length || precision < 1)
26
+ throw Error("can't round to higher precision");
27
+ let newInt = "";
28
+ const shouldRoundUp = Number(intString[intString.length - precision]) > 4;
29
+ if (shouldRoundUp) {
30
+ for (let i = 0; i < precision; i++) {
31
+ newInt += "0";
32
+ }
33
+ let retenue = true;
34
+ let i = intString.length - precision - 1;
35
+ while (retenue) {
36
+ const nb = (Number(intString[i]) + 1) % 10;
37
+ newInt = "" + nb + newInt;
38
+ if (nb === 0) {
39
+ i--;
40
+ }
41
+ else {
42
+ retenue = false;
43
+ for (let j = i - 1; j > -1; j--) {
44
+ newInt = intString[j] + newInt;
45
+ }
46
+ }
47
+ }
48
+ }
49
+ else {
50
+ for (let i = 0; i < intString.length; i++) {
51
+ newInt += i < intString.length - precision ? intString[i] : "0";
52
+ }
53
+ }
54
+ return new Integer(Number(newInt));
55
+ }
56
+ divide(nb) {
57
+ switch (nb.type) {
58
+ case nombre_1.NumberType.Integer:
59
+ return new rational_1.Rational(this.value, nb.value).simplify();
60
+ case nombre_1.NumberType.Rational:
61
+ const rational = nb;
62
+ return new rational_1.Rational(this.value * rational.denum, rational.num).simplify();
63
+ default:
64
+ throw Error("not implemented");
65
+ }
66
+ }
67
+ }
68
+ exports.Integer = Integer;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Power = void 0;
4
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
5
+ const powerNode_1 = require("../../tree/nodes/operators/powerNode");
6
+ const nombre_1 = require("../nombre");
7
+ const integer_1 = require("./integer");
8
+ class Power {
9
+ constructor(a, b) {
10
+ this.operand = a;
11
+ this.power = b;
12
+ this.value = Math.pow(a, b);
13
+ this.tex = `${a}^{${b}}`;
14
+ this.type = b < 0 ? nombre_1.NumberType.Rational : nombre_1.NumberType.Integer;
15
+ }
16
+ simplify() {
17
+ if (this.power === 0)
18
+ return new numberNode_1.NumberNode(1);
19
+ if (this.power === 1)
20
+ return new numberNode_1.NumberNode(this.operand);
21
+ if (this.operand === 1)
22
+ return new numberNode_1.NumberNode(1);
23
+ if (this.operand === 0)
24
+ return new numberNode_1.NumberNode(0);
25
+ if (this.operand === -1)
26
+ return new numberNode_1.NumberNode(this.power % 2 === 0 ? 1 : -1);
27
+ return this.toTree();
28
+ }
29
+ toDecimalWriting() {
30
+ if (this.operand !== 10)
31
+ throw Error("only implemented for powers of ten so far");
32
+ let s = "";
33
+ if (this.power > -1) {
34
+ s += "1";
35
+ for (let i = 0; i < this.power; i++) {
36
+ s += 0;
37
+ }
38
+ }
39
+ else {
40
+ s += "1";
41
+ for (let i = 1; i < Math.abs(this.power); i++) {
42
+ s = "0" + s;
43
+ }
44
+ s = "0." + s;
45
+ }
46
+ return new integer_1.Integer(Number(s), s);
47
+ }
48
+ toTree() {
49
+ return new powerNode_1.PowerNode(new numberNode_1.NumberNode(this.operand), new numberNode_1.NumberNode(this.power));
50
+ }
51
+ }
52
+ exports.Power = Power;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NumberType = void 0;
4
+ var NumberType;
5
+ (function (NumberType) {
6
+ NumberType[NumberType["Integer"] = 0] = "Integer";
7
+ NumberType[NumberType["Decimal"] = 1] = "Decimal";
8
+ NumberType[NumberType["Rational"] = 2] = "Rational";
9
+ NumberType[NumberType["Real"] = 3] = "Real";
10
+ })(NumberType = exports.NumberType || (exports.NumberType = {}));
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Rational = exports.RationalConstructor = void 0;
4
+ const coprimesOf_1 = require("../../mathutils/arithmetic/coprimesOf");
5
+ const gcd_1 = require("../../mathutils/arithmetic/gcd");
6
+ const lcd_1 = require("../../mathutils/arithmetic/lcd");
7
+ const randint_1 = require("../../mathutils/random/randint");
8
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
9
+ const fractionNode_1 = require("../../tree/nodes/operators/fractionNode");
10
+ const random_1 = require("../../utils/random");
11
+ const shuffle_1 = require("../../utils/shuffle");
12
+ const integer_1 = require("../integer/integer");
13
+ const nombre_1 = require("../nombre");
14
+ class RationalConstructor {
15
+ /**
16
+ * @param maxGcd max number by which the fraction is simplifiable
17
+ */
18
+ static randomSimplifiable(maxGcd = 10) {
19
+ const gcd = (0, randint_1.randint)(2, maxGcd);
20
+ const max = (0, randint_1.randint)(3, 11);
21
+ const min = (0, random_1.random)((0, coprimesOf_1.coprimesOf)(max));
22
+ let [num, denum] = (0, shuffle_1.shuffle)([gcd * min, gcd * max]);
23
+ if (denum === gcd) {
24
+ //si 10/2 on transforme en 2/10
25
+ return new Rational(denum, num);
26
+ }
27
+ return new Rational(num, denum);
28
+ }
29
+ static randomIrreductible(max = 11) {
30
+ const a = (0, randint_1.randint)(2, max);
31
+ const b = (0, random_1.random)([...(0, coprimesOf_1.coprimesOf)(a), 1]);
32
+ if (b === 1)
33
+ return new Rational(b, a);
34
+ const [num, denum] = (0, shuffle_1.shuffle)([a, b]);
35
+ return new Rational(num, denum);
36
+ }
37
+ }
38
+ exports.RationalConstructor = RationalConstructor;
39
+ class Rational {
40
+ constructor(numerator, denumerator) {
41
+ if (denumerator === 0)
42
+ throw Error("division by zero");
43
+ this.num = numerator;
44
+ this.denum = denumerator;
45
+ this.value = numerator / denumerator;
46
+ this.isSimplified = Math.abs((0, gcd_1.gcd)(numerator, denumerator)) === 1;
47
+ this.tex = `\\frac{${this.num}}{${this.denum}}`;
48
+ this.type = nombre_1.NumberType.Rational;
49
+ }
50
+ toTex() {
51
+ return `\\frac{${this.num}}{${this.denum}}`;
52
+ }
53
+ add(nb) {
54
+ switch (nb.type) {
55
+ case nombre_1.NumberType.Integer: {
56
+ const num = this.num + this.denum * nb.value;
57
+ return new Rational(num, this.denum).simplify();
58
+ }
59
+ case nombre_1.NumberType.Rational: {
60
+ const rational = nb;
61
+ const ppcm = (0, lcd_1.lcd)(rational.denum, this.denum);
62
+ const num = this.num * (ppcm / this.denum) + rational.num * (ppcm / rational.denum);
63
+ return new Rational(num, ppcm).simplify();
64
+ }
65
+ }
66
+ throw Error("not implemented yet");
67
+ }
68
+ multiply(nb) {
69
+ switch (nb.type) {
70
+ case nombre_1.NumberType.Integer: {
71
+ const num = this.num * nb.value;
72
+ const denum = this.denum;
73
+ return new Rational(num, denum).simplify();
74
+ }
75
+ case nombre_1.NumberType.Rational: {
76
+ const rational = nb;
77
+ const num = this.num * rational.num;
78
+ const denum = this.denum * rational.denum;
79
+ return new Rational(num, denum).simplify();
80
+ }
81
+ }
82
+ throw Error("not implemented yet");
83
+ }
84
+ divide(nb) {
85
+ switch (nb.type) {
86
+ case nombre_1.NumberType.Integer: {
87
+ const denum = this.denum * nb.value;
88
+ return new Rational(this.num, denum).simplify();
89
+ }
90
+ case nombre_1.NumberType.Rational: {
91
+ const rational = nb;
92
+ const num = this.num * rational.denum;
93
+ const denum = this.denum * rational.num;
94
+ return new Rational(num, denum).simplify();
95
+ }
96
+ }
97
+ throw Error("not implemented yet");
98
+ }
99
+ opposite() {
100
+ return new Rational(-this.num, this.denum);
101
+ }
102
+ toTree() {
103
+ return new fractionNode_1.FractionNode(new numberNode_1.NumberNode(this.num), new numberNode_1.NumberNode(this.denum));
104
+ }
105
+ simplify() {
106
+ const sign = (this.num > 0 && this.denum > 0) || (this.num < 0 && this.denum < 0) ? 1 : -1;
107
+ const div = Math.abs((0, gcd_1.gcd)(this.num, this.denum));
108
+ if (Math.abs(this.denum) === div)
109
+ return new integer_1.Integer(this.num / this.denum);
110
+ return new Rational((sign * Math.abs(this.num)) / div, Math.abs(this.denum) / div);
111
+ }
112
+ }
113
+ exports.Rational = Rational;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Real = void 0;
4
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
5
+ const nombre_1 = require("../nombre");
6
+ class Real {
7
+ constructor(value, tex) {
8
+ this.value = value;
9
+ this.tex = tex;
10
+ this.type = nombre_1.NumberType.Real;
11
+ }
12
+ toTree() {
13
+ return new numberNode_1.NumberNode(this.value);
14
+ }
15
+ }
16
+ exports.Real = Real;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SquareRoot = exports.SquareRootConstructor = void 0;
4
+ const isSquare_1 = require("../../mathutils/arithmetic/isSquare");
5
+ const primeFactors_1 = require("../../mathutils/arithmetic/primeFactors");
6
+ const randint_1 = require("../../mathutils/random/randint");
7
+ const numberNode_1 = require("../../tree/nodes/numbers/numberNode");
8
+ const multiplyNode_1 = require("../../tree/nodes/operators/multiplyNode");
9
+ const real_1 = require("./real");
10
+ const sqrtNode_1 = require("../../tree/nodes/functions/sqrtNode");
11
+ class SquareRootConstructor {
12
+ /**
13
+ * @returns simplifiable square root type sqrt(c)=a*sqrt(b)
14
+ */
15
+ static randomSimplifiable({ allowPerfectSquare = false, maxSquare = 11 }) {
16
+ const a = (0, randint_1.randint)(2, maxSquare);
17
+ let b;
18
+ let bMin = allowPerfectSquare ? 1 : 2;
19
+ do {
20
+ b = (0, randint_1.randint)(bMin, maxSquare);
21
+ } while (b % (a * a) === 0 || (0, isSquare_1.isSquare)(b));
22
+ return new SquareRoot(a * a * b);
23
+ }
24
+ }
25
+ exports.SquareRootConstructor = SquareRootConstructor;
26
+ class SquareRoot extends real_1.Real {
27
+ constructor(operand) {
28
+ super(Math.sqrt(operand), `\\sqrt{${operand}}`);
29
+ this.operand = operand;
30
+ }
31
+ simplify() {
32
+ const factors = (0, primeFactors_1.primeFactors)(this.operand);
33
+ // finds primes with even exponents
34
+ const multiples = [1];
35
+ for (let i = 0; i < factors.length - 1; i++) {
36
+ if (factors[i] === factors[i + 1]) {
37
+ multiples.push(factors[i]);
38
+ factors.splice(i, 2);
39
+ i--;
40
+ }
41
+ }
42
+ const outsideSqrt = multiples.reduce((x, y) => x * y);
43
+ const insideSqrt = factors.length === 0 ? 1 : factors.reduce((x, y) => x * y);
44
+ const simplified = insideSqrt !== 1
45
+ ? new real_1.Real(outsideSqrt * Math.sqrt(insideSqrt), `${outsideSqrt === 1 ? "" : `${outsideSqrt}`}\\sqrt{${insideSqrt}}`)
46
+ : new real_1.Real(outsideSqrt, outsideSqrt + "");
47
+ simplified.toTree = () => {
48
+ return insideSqrt !== 1
49
+ ? outsideSqrt === 1
50
+ ? new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(insideSqrt))
51
+ : new multiplyNode_1.MultiplyNode(new numberNode_1.NumberNode(outsideSqrt), new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(insideSqrt)))
52
+ : new numberNode_1.NumberNode(outsideSqrt);
53
+ };
54
+ return simplified;
55
+ }
56
+ toTex() {
57
+ return this.tex;
58
+ }
59
+ toTree() {
60
+ return new sqrtNode_1.SqrtNode(new numberNode_1.NumberNode(this.operand));
61
+ }
62
+ }
63
+ exports.SquareRoot = SquareRoot;