pimath 0.0.120 → 0.0.121

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 (171) hide show
  1. package/.eslintrc.js +23 -23
  2. package/.idea/PI.iml +7 -1
  3. package/dev/pimath.js +7929 -0
  4. package/dev/pimath.js.map +1 -0
  5. package/dist/{pi.js → pimath.js} +7612 -7840
  6. package/dist/pimath.js.map +1 -0
  7. package/dist/pimath.min.js +2 -0
  8. package/dist/pimath.min.js.map +1 -0
  9. package/docs/assets/main.js +3 -3
  10. package/docs/assets/search.js +1 -1
  11. package/docs/assets/style.css +450 -363
  12. package/docs/classes/Logicalset.Logicalset.html +119 -110
  13. package/docs/classes/Polynom.Rational.html +230 -227
  14. package/docs/classes/Vector-1.Vector.html +319 -273
  15. package/docs/classes/Vector.Point.html +189 -190
  16. package/docs/classes/algebra_equation.Equation.html +489 -446
  17. package/docs/classes/algebra_linearSystem.LinearSystem.html +228 -217
  18. package/docs/classes/algebra_monom.Monom.html +564 -507
  19. package/docs/classes/algebra_polynom.Polynom.html +774 -753
  20. package/docs/classes/coefficients_fraction.Fraction.html +573 -565
  21. package/docs/classes/geometry_circle.Circle.html +299 -299
  22. package/docs/classes/geometry_line.Line.html +511 -451
  23. package/docs/classes/geometry_triangle.Triangle.html +273 -264
  24. package/docs/classes/numeric.Numeric.html +138 -132
  25. package/docs/classes/shutingyard.Shutingyard.html +144 -133
  26. package/docs/enums/algebra_equation.PARTICULAR_SOLUTION.html +47 -46
  27. package/docs/enums/geometry_line.LinePropriety.html +58 -58
  28. package/docs/enums/shutingyard.ShutingyardMode.html +62 -58
  29. package/docs/enums/shutingyard.ShutingyardType.html +74 -70
  30. package/docs/index.html +31 -33
  31. package/docs/interfaces/algebra_equation.ISolution.html +61 -59
  32. package/docs/interfaces/algebra_polynom.IEuclidian.html +47 -46
  33. package/docs/interfaces/geometry_triangle.remarquableLines.html +74 -74
  34. package/docs/modules/Logicalset.html +33 -38
  35. package/docs/modules/Polynom.html +33 -38
  36. package/docs/modules/Vector-1.html +33 -38
  37. package/docs/modules/Vector.html +33 -38
  38. package/docs/modules/algebra_equation.html +35 -41
  39. package/docs/modules/algebra_linearSystem.html +31 -37
  40. package/docs/modules/algebra_monom.html +33 -39
  41. package/docs/modules/algebra_polynom.html +35 -41
  42. package/docs/modules/coefficients_fraction.html +33 -39
  43. package/docs/modules/geometry_circle.html +31 -37
  44. package/docs/modules/geometry_line.html +33 -39
  45. package/docs/modules/geometry_triangle.html +33 -39
  46. package/docs/modules/numeric.html +31 -37
  47. package/docs/modules/shutingyard.html +41 -47
  48. package/docs/types/algebra_monom.literalType.html +33 -37
  49. package/docs/types/algebra_polynom.PolynomParsingType.html +33 -37
  50. package/docs/types/coefficients_fraction.FractionParsingType.html +32 -36
  51. package/docs/types/shutingyard.Token.html +38 -42
  52. package/docs/types/shutingyard.tokenType.html +40 -44
  53. package/docs/variables/shutingyard.tokenConstant.html +37 -41
  54. package/esm/index.d.ts +38 -41
  55. package/esm/index.js +43 -46
  56. package/esm/index.js.map +1 -1
  57. package/esm/maths/algebra/equation.d.ts +119 -117
  58. package/esm/maths/algebra/equation.js +796 -785
  59. package/esm/maths/algebra/equation.js.map +1 -1
  60. package/esm/maths/algebra/linearSystem.d.ts +39 -38
  61. package/esm/maths/algebra/linearSystem.js +278 -262
  62. package/esm/maths/algebra/linearSystem.js.map +1 -1
  63. package/esm/maths/algebra/logicalset.d.ts +28 -28
  64. package/esm/maths/algebra/logicalset.js +157 -157
  65. package/esm/maths/algebra/monom.d.ts +206 -206
  66. package/esm/maths/algebra/monom.js +908 -908
  67. package/esm/maths/algebra/monom.js.map +1 -1
  68. package/esm/maths/algebra/polynom.d.ts +157 -157
  69. package/esm/maths/algebra/polynom.js +1277 -1277
  70. package/esm/maths/algebra/rational.d.ts +45 -45
  71. package/esm/maths/algebra/rational.js +183 -183
  72. package/esm/maths/algebra/study/rationalStudy.d.ts +28 -28
  73. package/esm/maths/algebra/study/rationalStudy.js +243 -243
  74. package/esm/maths/algebra/study.d.ts +142 -142
  75. package/esm/maths/algebra/study.js +377 -377
  76. package/esm/maths/algebra/study.js.map +1 -1
  77. package/esm/maths/coefficients/fraction.d.ts +90 -90
  78. package/esm/maths/coefficients/fraction.js +516 -516
  79. package/esm/maths/coefficients/fraction.js.map +1 -1
  80. package/esm/maths/coefficients/nthRoot.d.ts +23 -23
  81. package/esm/maths/coefficients/nthRoot.js +136 -136
  82. package/esm/maths/geometry/circle.d.ts +45 -45
  83. package/esm/maths/geometry/circle.js +323 -323
  84. package/esm/maths/geometry/line.d.ts +99 -99
  85. package/esm/maths/geometry/line.js +481 -481
  86. package/esm/maths/geometry/line.js.map +1 -1
  87. package/esm/maths/geometry/point.d.ts +34 -34
  88. package/esm/maths/geometry/point.js +166 -166
  89. package/esm/maths/geometry/point.js.map +1 -1
  90. package/esm/maths/geometry/triangle.d.ts +85 -85
  91. package/esm/maths/geometry/triangle.js +268 -268
  92. package/esm/maths/geometry/vector.d.ts +41 -41
  93. package/esm/maths/geometry/vector.js +197 -197
  94. package/esm/maths/geometry/vector.js.map +1 -1
  95. package/esm/maths/numeric.d.ts +28 -28
  96. package/esm/maths/numeric.js +180 -180
  97. package/esm/maths/numexp.d.ts +19 -0
  98. package/esm/maths/numexp.js +186 -0
  99. package/esm/maths/numexp.js.map +1 -0
  100. package/esm/maths/randomization/random.d.ts +23 -23
  101. package/esm/maths/randomization/random.js +78 -78
  102. package/esm/maths/randomization/random.js.map +1 -1
  103. package/esm/maths/randomization/randomCore.d.ts +7 -7
  104. package/esm/maths/randomization/randomCore.js +21 -21
  105. package/esm/maths/randomization/rndFraction.d.ts +12 -12
  106. package/esm/maths/randomization/rndFraction.js +43 -43
  107. package/esm/maths/randomization/rndGeometryLine.d.ts +12 -12
  108. package/esm/maths/randomization/rndGeometryLine.js +45 -45
  109. package/esm/maths/randomization/rndGeometryPoint.d.ts +12 -12
  110. package/esm/maths/randomization/rndGeometryPoint.js +60 -60
  111. package/esm/maths/randomization/rndHelpers.d.ts +23 -23
  112. package/esm/maths/randomization/rndHelpers.js +76 -76
  113. package/esm/maths/randomization/rndMonom.d.ts +12 -12
  114. package/esm/maths/randomization/rndMonom.js +52 -52
  115. package/esm/maths/randomization/rndPolynom.d.ts +13 -13
  116. package/esm/maths/randomization/rndPolynom.js +74 -74
  117. package/esm/maths/randomization/rndTypes.d.ts +34 -34
  118. package/esm/maths/randomization/rndTypes.js +2 -2
  119. package/esm/maths/shutingyard.d.ts +59 -59
  120. package/esm/maths/shutingyard.js +442 -442
  121. package/esm/maths/shutingyard.js.map +1 -1
  122. package/package.json +11 -11
  123. package/public/index.html +50 -81
  124. package/public/playground.html +7 -8
  125. package/src/index.ts +2 -5
  126. package/src/maths/algebra/equation.ts +16 -0
  127. package/src/maths/algebra/linearSystem.ts +20 -0
  128. package/src/maths/algebra/study.ts +1 -1
  129. package/src/maths/{expressions/numexp.ts → numexp.ts} +2 -2
  130. package/tests/algebra/equation.test.ts +19 -5
  131. package/tests/algebra/linear.test.ts +3 -11
  132. package/tests/algebra/polynom.test.ts +7 -8
  133. package/tests/algebra/rationnal.test.ts +1 -1
  134. package/tests/algebra/study.test.ts +2 -9
  135. package/tests/coefficients/fraction.test.ts +8 -8
  136. package/tests/custom.test.ts +33 -37
  137. package/tests/numeric.test.ts +1 -2
  138. package/tests/numexp.test.ts +1 -5
  139. package/tests/shutingyard.test.ts +3 -3
  140. package/webpack-production-min.config.js +1 -1
  141. package/webpack-production.config.js +1 -1
  142. package/webpack.config.js +1 -1
  143. package/dist/pi.js.map +0 -1
  144. package/dist/pi.min.js +0 -2
  145. package/dist/pi.min.js.map +0 -1
  146. package/docs/classes/expressions_numexp.NumExp.html +0 -236
  147. package/docs/classes/expressions_polynomexp.PolynomExpFactor.html +0 -317
  148. package/docs/classes/expressions_polynomexp.PolynomExpProduct.html +0 -285
  149. package/docs/modules/expressions_numexp.html +0 -71
  150. package/docs/modules/expressions_polynomexp.html +0 -73
  151. package/docs/modules.html +0 -76
  152. package/graph.svg +0 -1033
  153. package/src/maths/expressions/ExpressionTree.ts +0 -172
  154. package/src/maths/expressions/expression.ts +0 -286
  155. package/src/maths/expressions/expressionFactor.ts +0 -190
  156. package/src/maths/expressions/expressionMember.ts +0 -233
  157. package/src/maths/expressions/expressionOperators.ts +0 -49
  158. package/src/maths/expressions/expressionParser.ts +0 -295
  159. package/src/maths/expressions/factors/ExpFactor.ts +0 -39
  160. package/src/maths/expressions/factors/ExpFactorConstant.ts +0 -60
  161. package/src/maths/expressions/factors/ExpFactorExponential.ts +0 -26
  162. package/src/maths/expressions/factors/ExpFactorNumber.ts +0 -72
  163. package/src/maths/expressions/factors/ExpFactorPower.ts +0 -42
  164. package/src/maths/expressions/factors/ExpFactorTrigo.ts +0 -53
  165. package/src/maths/expressions/factors/ExpFactorVariable.ts +0 -45
  166. package/src/maths/expressions/internals.ts +0 -14
  167. package/src/maths/expressions/polynomexp.bkp.ts +0 -221
  168. package/src/maths/expressions/polynomexp.ts +0 -310
  169. package/tests/expressions/expressions.test.ts +0 -145
  170. package/tests/expressions/expressiontree.test.ts +0 -11
  171. package/tests/polynomexp.test.ts +0 -12
@@ -1,786 +1,797 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Equation = exports.PARTICULAR_SOLUTION = void 0;
4
- const polynom_1 = require("./polynom");
5
- const numeric_1 = require("../numeric");
6
- const fraction_1 = require("../coefficients/fraction");
7
- const nthRoot_1 = require("../coefficients/nthRoot");
8
- var PARTICULAR_SOLUTION;
9
- (function (PARTICULAR_SOLUTION) {
10
- PARTICULAR_SOLUTION["real"] = "\\mathbb{R}";
11
- PARTICULAR_SOLUTION["varnothing"] = "\\varnothing";
12
- })(PARTICULAR_SOLUTION = exports.PARTICULAR_SOLUTION || (exports.PARTICULAR_SOLUTION = {}));
13
- class Equation {
14
- /**
15
- * Create an Equation using two polynoms.
16
- * Markdown *support* is cool
17
- * @param equations
18
- */
19
- constructor(...equations) {
20
- // Undetermined texSolutions.
21
- this._varnothing = PARTICULAR_SOLUTION.varnothing;
22
- this._real = PARTICULAR_SOLUTION.real;
23
- this.hasVariable = (letter) => {
24
- return this.variables.includes(letter);
25
- };
26
- // ------------------------------------------
27
- // Creation / parsing functions
28
- // -----------------------------------------------
29
- this._randomizeDefaults = {
30
- degree: 2
31
- };
32
- // ------------------------------------------
33
- this.parse = (equationString) => {
34
- let pStr, strSign;
35
- // Find the string separator
36
- strSign = this._findSign(equationString);
37
- if (strSign === false) {
38
- console.error('The equation is not valid (no sign found)');
39
- return;
40
- }
41
- // The StrSign is found
42
- pStr = equationString.split(strSign);
43
- return this.create(new polynom_1.Polynom(pStr[0]), new polynom_1.Polynom(pStr[1]), this._formatSign(strSign));
44
- };
45
- this.create = (left, right, sign) => {
46
- this._left = left;
47
- this._right = right;
48
- this._sign = this._formatSign(sign);
49
- return this;
50
- };
51
- this.clone = () => {
52
- return new Equation().create(this._left.clone(), this._right.clone(), this._sign + '');
53
- };
54
- // -----------------------------------------------
55
- // Equations generators and randomizers
56
- this.randomize = (opts, sign) => {
57
- // TODO: Generate equations randomly, using config.
58
- return new Equation().create(new polynom_1.Polynom(), new polynom_1.Polynom(), sign);
59
- };
60
- // -----------------------------------------------
61
- /**
62
- * Reorder will move all monoms containing a letter on the left, all the other on the right.
63
- */
64
- this.moveLeft = () => {
65
- this._left = this._left.clone().subtract(this._right);
66
- this._right.zero();
67
- return this;
68
- };
69
- this.reorder = (allLeft) => {
70
- // Move all monoms of degree greater than 0 to the left.
71
- // and all zero degree monoms to the right.
72
- this._left.subtract(this._right);
73
- this._right.zero();
74
- this._left.reorder();
75
- // we eant all left (so equal zero) : it's done !
76
- if (allLeft)
77
- return this;
78
- // Fetch all zero degree monoms.
79
- this._left.monoms
80
- .filter(m => m.degree().isZero())
81
- .forEach(m => {
82
- const move = m.clone();
83
- this._left.subtract(move);
84
- this._right.subtract(move);
85
- });
86
- // Reorder the left and right polynoms
87
- this._left.reorder();
88
- this._right.reorder();
89
- return this;
90
- };
91
- /**
92
- * Multiply by the lcm denominator and divide by the gcm numerators.
93
- */
94
- this.simplify = () => {
95
- this.multiply(numeric_1.Numeric.lcm(...this._left.getDenominators(), ...this._right.getDenominators()));
96
- this.divide(numeric_1.Numeric.gcd(...this._left.getNumerators(), ...this._right.getNumerators()));
97
- return this;
98
- };
99
- // -----------------------------------------------
100
- // Equations operations
101
- /**
102
- * Reorder the polynom to have only one letter on the left, the rest on the right.
103
- * @param letter
104
- */
105
- this.isolate = (letter) => {
106
- // Determine if we can isolate the variables.
107
- // Both part of the equations must be of the first degree.
108
- //TODO: handle equations of degree two or more ?
109
- if (!this.degree(letter).isOne()) {
110
- return false;
111
- }
112
- // Modify the equation to isolate the asked variable.
113
- // TODO: must handle equations like 3xy+5y=4 => y = 4/(3x-5)
114
- if (this.isMultiVariable()) {
115
- return false;
116
- }
117
- // Isolate the letter.
118
- let mMove, cMove;
119
- // Start by moving everything to the left.
120
- this._left.subtract(this._right);
121
- this._right.zero();
122
- let values = [...this._left.monoms];
123
- for (let m of values) {
124
- if (!m.hasLetter(letter)) {
125
- mMove = m.clone();
126
- this._left.subtract(mMove);
127
- this._right.subtract(mMove);
128
- }
129
- }
130
- // In theory, we should have only one item on the left.
131
- if (this._left.length !== 1) {
132
- return false;
133
- }
134
- cMove = this._left.monoms[0].coefficient.clone();
135
- this._left.divide(cMove);
136
- this._right.divide(cMove);
137
- return this;
138
- };
139
- this.replaceBy = (letter, P) => {
140
- this._left.replaceBy(letter, P);
141
- this._right.replaceBy(letter, P);
142
- return this;
143
- };
144
- /**
145
- * Multiple an equation by a fraction value.
146
- * @param value
147
- */
148
- this.multiply = (value) => {
149
- // Make sure we have a fraction.
150
- let F = new fraction_1.Fraction(value);
151
- // Multiply each part of the equation by the fraction
152
- this._left.multiply(F);
153
- this._right.multiply(F);
154
- // The sign of the inequation must be changed.
155
- if (this._sign !== '=' && F.sign() === -1) {
156
- this._reverseSign();
157
- }
158
- return this;
159
- };
160
- /**
161
- * divide an equation by a given value (transformed as a fraction)
162
- *
163
- * ```
164
- * 8x+10=6x \vert 2
165
- * 4x+5=3x
166
- * ```
167
- *
168
- * |>Alternatively with $3x-4$ maybe it's working ?
169
- * $$\frac{3x}{5}$$
170
- *
171
- * @param value
172
- * @returns {Equation}
173
- */
174
- this.divide = (value) => {
175
- // Make sure we have a fraction.
176
- let F = new fraction_1.Fraction(value);
177
- if (F.isZero()) {
178
- return this;
179
- }
180
- else {
181
- return this.multiply(F.invert());
182
- }
183
- };
184
- /**
185
- * Get the degree of the equation
186
- * @param letter
187
- */
188
- this.degree = (letter) => {
189
- return fraction_1.Fraction.max(this._left.degree(letter), this._right.degree(letter));
190
- };
191
- /**
192
- * Determine if the equation contains more than one letter/variable.
193
- */
194
- this.isMultiVariable = () => {
195
- return this._left.isMultiVariable || this._right.isMultiVariable;
196
- };
197
- this.letters = () => {
198
- // @ts-ignore
199
- return [...new Set([...this._left.letters(), ...this._right.letters()])];
200
- };
201
- // -----------------------------------------------
202
- // Equations helpers
203
- // -----------------------------------------------
204
- // -----------------------------------------------
205
- this.solve = () => {
206
- // Initialise the variables:
207
- this._solutions = [];
208
- // TODO: consolidate solving equations (inequations vs equations)
209
- // TODO: work with not natural degrees ?
210
- this._polynom = this._left.clone().subtract(this._right);
211
- switch (this._polynom.degree().value) {
212
- case 0:
213
- case 1:
214
- this._solveDegree1();
215
- break;
216
- case 2:
217
- this._solveDegree2();
218
- break;
219
- default:
220
- this._solveDegree3plus();
221
- }
222
- // cleanup the solutions.
223
- this._solutions = Equation.makeSolutionsUnique(this._solutions);
224
- return this;
225
- };
226
- this.test = (values) => {
227
- return this.left.evaluate(values).isEqual(this.right.evaluate(values));
228
- };
229
- this._findSign = (equationString) => {
230
- let strSign = '';
231
- if (equationString.includes('geq')) {
232
- return (equationString.includes('\\geq')) ? '\\geq' : 'geq';
233
- }
234
- else if (equationString.includes('leq')) {
235
- return (equationString.includes('\\leq')) ? '\\leq' : 'leq';
236
- }
237
- else if (equationString.includes('>=')) {
238
- return '>=';
239
- }
240
- else if (equationString.includes('=>')) {
241
- return '=>';
242
- }
243
- else if (equationString.includes('>')) {
244
- return '>';
245
- }
246
- else if (equationString.includes('<=')) {
247
- return '<=';
248
- }
249
- else if (equationString.includes('=<')) {
250
- return '=<';
251
- }
252
- else if (equationString.includes('<')) {
253
- return '<';
254
- }
255
- else if (equationString.includes('=')) {
256
- return '=';
257
- }
258
- if (strSign === '') {
259
- console.log('Equation: parse string : sign not found');
260
- return false;
261
- }
262
- };
263
- // -----------------------------------------------
264
- // Equations solving algorithms
265
- this._formatSign = (signStr) => {
266
- if (signStr === undefined) {
267
- return '=';
268
- }
269
- if (signStr.includes('geq')) {
270
- return '>=';
271
- }
272
- else if (signStr.includes('>=')) {
273
- return '>=';
274
- }
275
- else if (signStr.includes('=>')) {
276
- return '>=';
277
- }
278
- else if (signStr.includes('>')) {
279
- return '>';
280
- }
281
- else if (signStr.includes('leq')) {
282
- return '<=';
283
- }
284
- else if (signStr.includes('<=')) {
285
- return '<=';
286
- }
287
- else if (signStr.includes('=<')) {
288
- return '<=';
289
- }
290
- else if (signStr.includes('<')) {
291
- return '<';
292
- }
293
- else {
294
- return '=';
295
- }
296
- };
297
- this._reverseSign = () => {
298
- if (this._sign === '=') {
299
- return this;
300
- }
301
- if (this._sign.includes('<')) {
302
- this._sign.replace('<', '>');
303
- return this;
304
- }
305
- if (this._sign.includes('>')) {
306
- this._sign.replace('>', '<');
307
- return this;
308
- }
309
- return this;
310
- };
311
- this.isGreater = () => {
312
- if (this._sign.indexOf('>') !== -1) {
313
- return true;
314
- }
315
- return this._sign.indexOf('geq') !== -1;
316
- };
317
- this.isStrictEqual = () => {
318
- return this._sign === '=';
319
- };
320
- this.isAlsoEqual = () => {
321
- if (this._sign.indexOf('=') !== -1) {
322
- return true;
323
- }
324
- if (this._sign.indexOf('geq') !== -1) {
325
- return true;
326
- }
327
- if (this._sign.indexOf('leq') !== -1) {
328
- return true;
329
- }
330
- };
331
- this._solveDegree1 = (letter) => {
332
- const m1 = this._polynom.monomByDegree(1, letter).coefficient, m0 = this._polynom.monomByDegree(0, letter).coefficient, v = m0.clone().opposed().divide(m1);
333
- let s, d;
334
- if (this.isStrictEqual()) {
335
- if (m1.value === 0) {
336
- // In this case, the coefficient of the x variable is zero.
337
- if (m0.value === 0) {
338
- this._solutions = [{
339
- tex: this._real,
340
- display: "RR",
341
- value: NaN,
342
- exact: false
343
- }];
344
- }
345
- else {
346
- this._solutions = [{
347
- tex: this._varnothing,
348
- display: "O/",
349
- value: NaN,
350
- exact: false
351
- }];
352
- }
353
- }
354
- else {
355
- this._solutions = [{
356
- tex: v.tex,
357
- display: v.display,
358
- value: v.value,
359
- exact: v
360
- }];
361
- }
362
- }
363
- else {
364
- if (m1.value === 0) {
365
- // In this case, the coefficient of the x variable is zero.
366
- if (m0.value === 0 && this.isAlsoEqual()) {
367
- s = '\\mathbb{R}';
368
- d = "RR";
369
- }
370
- else {
371
- if (m0.value > 0) {
372
- s = this.isGreater() ? this._real : this._varnothing;
373
- s = this.isGreater() ? "RR" : "O/";
374
- }
375
- else {
376
- s = !this.isGreater() ? this._real : this._varnothing;
377
- s = !this.isGreater() ? "RR" : "O/";
378
- }
379
- }
380
- }
381
- else {
382
- // Must handle the case if the m1 monom is negative.
383
- if ((this.isGreater() && m1.sign() === 1) || (!this.isGreater() && m1.sign() === -1)) {
384
- s = `\\left${this.isAlsoEqual() ? '[' : ']'}${v.tex};+\\infty\\right[`;
385
- d = `${this.isAlsoEqual() ? '[' : ']'}${v.tex};+oo[`;
386
- }
387
- else {
388
- s = `\\left]-\\infty;${v.tex} \\right${this.isAlsoEqual() ? ']' : '['}`;
389
- d = `]-oo;${v.tex}${this.isAlsoEqual() ? ']' : '['}`;
390
- }
391
- }
392
- this._solutions = [{
393
- tex: s,
394
- display: d,
395
- value: NaN,
396
- exact: false
397
- }];
398
- }
399
- return this._solutions;
400
- };
401
- this._solveDegree2 = (letter) => {
402
- let aF = this._polynom.monomByDegree(2, letter).coefficient, bF = this._polynom.monomByDegree(1, letter).coefficient, cF = this._polynom.monomByDegree(0, letter).coefficient, delta, nthDelta, lcm = numeric_1.Numeric.lcm(aF.denominator, bF.denominator, cF.denominator), a = aF.multiply(lcm).value, b = bF.multiply(lcm).value, c = cF.multiply(lcm).value, realX1, realX2, sX1, sX2;
403
- delta = b * b - 4 * a * c;
404
- if (delta > 0) {
405
- realX1 = (-b - Math.sqrt(delta)) / (2 * a);
406
- realX2 = (-b + Math.sqrt(delta)) / (2 * a);
407
- if (delta > 1.0e5) {
408
- // The delta is too big to be parsed !
409
- let v1 = ((-b - Math.sqrt(delta)) / (2 * a)).toFixed(5), v2 = ((-b + Math.sqrt(delta)) / (2 * a)).toFixed(5);
410
- this._solutions = [
411
- {
412
- tex: v1,
413
- display: v1,
414
- value: realX1,
415
- exact: false
416
- },
417
- {
418
- tex: v2,
419
- display: v2,
420
- value: realX2,
421
- exact: false
422
- }
423
- ];
424
- }
425
- else {
426
- nthDelta = new nthRoot_1.NthRoot(delta).reduce();
427
- if (nthDelta.hasRadical()) {
428
- // -b +- coeff\sqrt{radical}
429
- // -------------------------
430
- // 2a
431
- let gcd = numeric_1.Numeric.gcd(b, 2 * a, nthDelta.coefficient), am = a / gcd, bm = b / gcd;
432
- nthDelta.coefficient = nthDelta.coefficient / gcd;
433
- if (a < 0) {
434
- am = -am;
435
- bm = -bm;
436
- }
437
- let tex1 = "", tex2 = "", display1 = "", display2 = "";
438
- tex1 = `${bm !== 0 ? ((-bm) + ' - ') : ''}${nthDelta.tex}`;
439
- tex2 = `${bm !== 0 ? ((-bm) + ' + ') : ''}${nthDelta.tex}`;
440
- display1 = `${bm !== 0 ? ((-bm) + ' - ') : ''}${nthDelta.display}`;
441
- display2 = `${bm !== 0 ? ((-bm) + ' + ') : ''}${nthDelta.display}`;
442
- if (am !== 1) {
443
- tex1 = `\\frac{ ${tex1} }{ ${2 * am} }`;
444
- tex2 = `\\frac{ ${tex2} }{ ${2 * am} }`;
445
- }
446
- this._solutions = [
447
- {
448
- tex: tex1,
449
- display: tex1,
450
- value: realX1,
451
- exact: false
452
- },
453
- {
454
- tex: tex2,
455
- display: tex2,
456
- value: realX2,
457
- exact: false
458
- },
459
- ];
460
- // if (b !== 0) {
461
- // if (2 * a / gcd === 1) {
462
- // this._solutions = [
463
- // {
464
- // tex: `${-b / gcd} - ${nthDelta.tex}`,
465
- // value: realX1,
466
- // exact: false // TODO: implement exact value with nthroot
467
- // },
468
- // {
469
- // tex: `${-b / gcd} + ${nthDelta.tex}`,
470
- // value: realX2,
471
- // exact: false
472
- // },
473
- //
474
- // ]
475
- // } else {
476
- // this._solutions = [
477
- // {
478
- // tex: `\\frac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
479
- // value: realX1,
480
- // exact: false
481
- // },
482
- // {
483
- // tex: `\\frac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
484
- // value: realX2,
485
- // exact: false
486
- // },
487
- // ]
488
- // }
489
- // } else {
490
- // if (2 * a / gcd === 1) {
491
- // this._solutions = [
492
- // {
493
- // tex: `- ${nthDelta.tex}`,
494
- // value: realX1,
495
- // exact: false
496
- // },
497
- // {
498
- // tex: `${nthDelta.tex}`,
499
- // value: realX2,
500
- // exact: false
501
- // },
502
- // ]
503
- // } else {
504
- // this._solutions = [
505
- // {
506
- // tex: `\\frac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
507
- // value: realX1,
508
- // exact: false
509
- // },
510
- // {
511
- // tex: `\\frac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
512
- // value: realX2,
513
- // exact: false
514
- // },
515
- // ]
516
- // }
517
- // }
518
- }
519
- else {
520
- // -b +- d / 2a
521
- const S1 = new fraction_1.Fraction(-b - nthDelta.coefficient, 2 * a).reduce(), S2 = new fraction_1.Fraction(-b + nthDelta.coefficient, 2 * a).reduce();
522
- this._solutions = [
523
- {
524
- tex: S1.frac,
525
- display: S1.display,
526
- value: realX1,
527
- exact: S1
528
- },
529
- {
530
- tex: S2.frac,
531
- display: S2.display,
532
- value: realX2,
533
- exact: S2
534
- }
535
- ];
536
- }
537
- }
538
- }
539
- else if (delta === 0) {
540
- const sol = new fraction_1.Fraction(-b, 2 * a).reduce();
541
- this._solutions = [{
542
- tex: sol.frac,
543
- display: sol.display,
544
- value: sol.value,
545
- exact: sol
546
- }];
547
- }
548
- else {
549
- this._solutions = [{
550
- tex: this._varnothing,
551
- display: "O/",
552
- value: NaN,
553
- exact: false
554
- }];
555
- }
556
- // Handle now the inequations.
557
- if (!this.isStrictEqual()) {
558
- if (this._solutions.length === 2) {
559
- sX1 = (realX1 < realX2) ? this._solutions[0].tex : this._solutions[1].tex;
560
- sX2 = (realX1 < realX2) ? this._solutions[1].tex : this._solutions[0].tex;
561
- if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
562
- this._solutions = [{
563
- tex: `\\left]-\\infty ; ${sX1}\\right${this.isAlsoEqual() ? ']' : '['} \\cup \\left${this.isAlsoEqual() ? '[' : ']'}${sX2};+\\infty\\right[`,
564
- display: `]-oo;${sX1}${this.isAlsoEqual() ? ']' : '['}uu${this.isAlsoEqual() ? '[' : ']'}${sX2};+oo[`,
565
- value: NaN,
566
- exact: false
567
- }
568
- ];
569
- }
570
- else {
571
- this._solutions = [{
572
- tex: `\\left${this.isAlsoEqual() ? '[' : ']'}${sX1} ; ${sX2}\\right${this.isAlsoEqual() ? ']' : '['}`,
573
- display: `${this.isAlsoEqual() ? '[' : ']'}${sX1};${sX2}${this.isAlsoEqual() ? ']' : '['}`,
574
- value: NaN,
575
- exact: false
576
- }];
577
- }
578
- }
579
- else if (this._solutions.length === 1 && this._solutions[0].tex !== this._varnothing) {
580
- if (!this.isAlsoEqual()) {
581
- if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
582
- this._solutions = [{
583
- tex: `\\left]-\\infty ; ${this._solutions[0].tex}\\right[ \\cup \\left]${this._solutions[0].tex};+\\infty\\right[`,
584
- display: `]-oo;${this._solutions[0].tex}[uu]${this._solutions[0].tex};+oo[`,
585
- value: NaN,
586
- exact: false
587
- }
588
- ];
589
- }
590
- else {
591
- this._solutions = [{
592
- tex: this._varnothing,
593
- display: "O/",
594
- value: NaN,
595
- exact: false
596
- }];
597
- }
598
- }
599
- else {
600
- if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
601
- this._solutions = [{
602
- tex: this._real,
603
- display: "RR",
604
- value: NaN,
605
- exact: false
606
- }];
607
- }
608
- else {
609
- // this._texSolutions = [ this._texSolutions[0] ];
610
- }
611
- }
612
- }
613
- else {
614
- if (this.isGreater()) {
615
- this._solutions = [{
616
- tex: aF.sign() === 1 ? this._real : this._varnothing,
617
- display: aF.sign() === 1 ? "RR" : "O/",
618
- value: NaN,
619
- exact: false
620
- }];
621
- }
622
- else {
623
- this._solutions = [{
624
- tex: aF.sign() === -1 ? this._real : this._varnothing,
625
- display: aF.sign() === -1 ? "RR" : "O/",
626
- value: NaN,
627
- exact: false
628
- }];
629
- }
630
- }
631
- }
632
- return this._solutions;
633
- };
634
- this._solveDegree3plus = (letter) => {
635
- // Push everything to the left
636
- // factorize
637
- // solve each factors.
638
- let equ = this.clone().moveLeft();
639
- equ.left.factorize();
640
- this._solutions = [];
641
- equ.left.factors.forEach(factor => {
642
- if (factor.degree(letter).leq(2)) {
643
- let factorAsEquation = new Equation(factor, 0);
644
- factorAsEquation.solve();
645
- factorAsEquation.solutions.forEach(solution => {
646
- this._solutions.push(solution);
647
- });
648
- }
649
- else {
650
- console.log(factor.tex, ': cannot actually get the solution of this equation');
651
- }
652
- });
653
- // TODO: check equation resolution for more than degree 2
654
- // this._solutions = [{tex: 'solve x - not yet handled', value: NaN, exact: false}]; // ESLint remove system :(
655
- return this._solutions;
656
- };
657
- // Default equation
658
- this._left = new polynom_1.Polynom().zero();
659
- this._right = new polynom_1.Polynom().zero();
660
- this._sign = '=';
661
- if (equations.length === 1) {
662
- if (equations[0] instanceof Equation) {
663
- return equations[0].clone();
664
- }
665
- else if (typeof equations[0] === 'string') {
666
- this.parse(equations[0]);
667
- }
668
- }
669
- else if (equations.length === 2) {
670
- if (equations[0] instanceof polynom_1.Polynom) {
671
- this.left = equations[0].clone();
672
- }
673
- else if (typeof equations[0] === 'string') {
674
- this.left = new polynom_1.Polynom(equations[0]);
675
- }
676
- if (equations[1] instanceof polynom_1.Polynom) {
677
- this.right = equations[1].clone();
678
- }
679
- else if (typeof equations[1] === 'string') {
680
- this.right = new polynom_1.Polynom(equations[1]);
681
- }
682
- }
683
- else {
684
- // Return default empty equation
685
- return this;
686
- }
687
- return this;
688
- }
689
- get left() {
690
- return this._left;
691
- }
692
- set left(value) {
693
- this._left = value;
694
- }
695
- get right() {
696
- return this._right;
697
- }
698
- // ------------------------------------------
699
- // Getter and setter
700
- set right(value) {
701
- this._right = value;
702
- }
703
- get sign() {
704
- return this._sign;
705
- }
706
- set sign(value) {
707
- // Set the sign value as formatted.
708
- this._sign = this._formatSign(value);
709
- }
710
- // ------------------------------------------
711
- get solutions() {
712
- return this._solutions;
713
- }
714
- get isEquation() {
715
- return true;
716
- }
717
- get solution() {
718
- if (this._solutions.length === 1
719
- &&
720
- (this._solutions[0].tex === this._real
721
- || this._solutions[0].tex === this._varnothing
722
- || this._solutions[0].tex.includes('\\left'))) {
723
- return `S = ${this._solutions[0]}`;
724
- }
725
- return `S = \\left{ ${this._solutions.map(x => x.tex).join(';')} \\right}`;
726
- }
727
- get isReal() {
728
- if (this._solutions === undefined) {
729
- this.solve();
730
- }
731
- return this._solutions[0].tex === this._real;
732
- }
733
- get isVarnothing() {
734
- if (this._solutions === undefined) {
735
- this.solve();
736
- }
737
- return this._solutions[0].tex === this._varnothing;
738
- }
739
- get signAsTex() {
740
- if (this._sign === '>=' || this._sign === '=>' || this._sign === 'geq') {
741
- return '\\geq';
742
- }
743
- if (this._sign === '<=' || this._sign === '=<' || this._sign === 'leq') {
744
- return '\\leq';
745
- }
746
- return this._sign;
747
- }
748
- get tex() {
749
- return `${this._left.tex}${this.signAsTex}${this._right.tex}`;
750
- }
751
- get display() {
752
- return `${this._left.display}${this.signAsTex}${this._right.display}`;
753
- }
754
- get raw() {
755
- return `${this._left.raw}${this.signAsTex}${this._right.raw}`;
756
- }
757
- get variables() {
758
- return [...new Set(this._right.variables.concat(this._left.variables))];
759
- }
760
- get numberOfVars() {
761
- return this.variables.length;
762
- }
763
- get randomizeDefaults() {
764
- return this._randomizeDefaults;
765
- }
766
- set randomizeDefaults(value) {
767
- this._randomizeDefaults = value;
768
- }
769
- static makeSolutionsUnique(solutions, sorted) {
770
- let solutionAsTex = [], uniqueSolutions = solutions.filter(sol => {
771
- if (!solutionAsTex.includes(sol.tex)) {
772
- solutionAsTex.push(sol.tex);
773
- return true;
774
- }
775
- else {
776
- return false;
777
- }
778
- });
779
- if (sorted === true) {
780
- uniqueSolutions.sort((a, b) => a.value - b.value);
781
- }
782
- return uniqueSolutions;
783
- }
784
- }
785
- exports.Equation = Equation;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Equation = exports.PARTICULAR_SOLUTION = void 0;
4
+ const polynom_1 = require("./polynom");
5
+ const numeric_1 = require("../numeric");
6
+ const fraction_1 = require("../coefficients/fraction");
7
+ const nthRoot_1 = require("../coefficients/nthRoot");
8
+ var PARTICULAR_SOLUTION;
9
+ (function (PARTICULAR_SOLUTION) {
10
+ PARTICULAR_SOLUTION["real"] = "\\mathbb{R}";
11
+ PARTICULAR_SOLUTION["varnothing"] = "\\varnothing";
12
+ })(PARTICULAR_SOLUTION || (exports.PARTICULAR_SOLUTION = PARTICULAR_SOLUTION = {}));
13
+ class Equation {
14
+ /**
15
+ * Create an Equation using two polynoms.
16
+ * Markdown *support* is cool
17
+ * @param equations
18
+ */
19
+ constructor(...equations) {
20
+ // Undetermined texSolutions.
21
+ this._varnothing = PARTICULAR_SOLUTION.varnothing;
22
+ this._real = PARTICULAR_SOLUTION.real;
23
+ this.hasVariable = (letter) => {
24
+ return this.variables.includes(letter);
25
+ };
26
+ // ------------------------------------------
27
+ // Creation / parsing functions
28
+ // -----------------------------------------------
29
+ this._randomizeDefaults = {
30
+ degree: 2
31
+ };
32
+ // ------------------------------------------
33
+ this.parse = (equationString) => {
34
+ let pStr, strSign;
35
+ // Find the string separator
36
+ strSign = this._findSign(equationString);
37
+ if (strSign === false) {
38
+ console.error('The equation is not valid (no sign found)');
39
+ return;
40
+ }
41
+ // The StrSign is found
42
+ pStr = equationString.split(strSign);
43
+ return this.create(new polynom_1.Polynom(pStr[0]), new polynom_1.Polynom(pStr[1]), this._formatSign(strSign));
44
+ };
45
+ this.create = (left, right, sign) => {
46
+ this._left = left;
47
+ this._right = right;
48
+ this._sign = this._formatSign(sign);
49
+ return this;
50
+ };
51
+ this.clone = () => {
52
+ return new Equation().create(this._left.clone(), this._right.clone(), this._sign + '');
53
+ };
54
+ // -----------------------------------------------
55
+ // Equations generators and randomizers
56
+ this.randomize = (opts, sign) => {
57
+ // TODO: Generate equations randomly, using config.
58
+ return new Equation().create(new polynom_1.Polynom(), new polynom_1.Polynom(), sign);
59
+ };
60
+ // -----------------------------------------------
61
+ /**
62
+ * Reorder will move all monoms containing a letter on the left, all the other on the right.
63
+ */
64
+ this.moveLeft = () => {
65
+ this._left = this._left.clone().subtract(this._right);
66
+ this._right.zero();
67
+ return this;
68
+ };
69
+ this.reorder = (allLeft) => {
70
+ // Move all monoms of degree greater than 0 to the left.
71
+ // and all zero degree monoms to the right.
72
+ this._left.subtract(this._right);
73
+ this._right.zero();
74
+ this._left.reorder();
75
+ // we eant all left (so equal zero) : it's done !
76
+ if (allLeft)
77
+ return this;
78
+ // Fetch all zero degree monoms.
79
+ this._left.monoms
80
+ .filter(m => m.degree().isZero())
81
+ .forEach(m => {
82
+ const move = m.clone();
83
+ this._left.subtract(move);
84
+ this._right.subtract(move);
85
+ });
86
+ // Reorder the left and right polynoms
87
+ this._left.reorder();
88
+ this._right.reorder();
89
+ return this;
90
+ };
91
+ /**
92
+ * Multiply by the lcm denominator and divide by the gcm numerators.
93
+ */
94
+ this.simplify = () => {
95
+ this.multiply(numeric_1.Numeric.lcm(...this._left.getDenominators(), ...this._right.getDenominators()));
96
+ this.divide(numeric_1.Numeric.gcd(...this._left.getNumerators(), ...this._right.getNumerators()));
97
+ return this;
98
+ };
99
+ // -----------------------------------------------
100
+ // Equations operations
101
+ /**
102
+ * Reorder the polynom to have only one letter on the left, the rest on the right.
103
+ * @param letter
104
+ */
105
+ this.isolate = (letter) => {
106
+ // Determine if we can isolate the variables.
107
+ // Both part of the equations must be of the first degree.
108
+ //TODO: handle equations of degree two or more ?
109
+ if (!this.degree(letter).isOne()) {
110
+ return false;
111
+ }
112
+ // Modify the equation to isolate the asked variable.
113
+ // TODO: must handle equations like 3xy+5y=4 => y = 4/(3x-5)
114
+ if (this.isMultiVariable()) {
115
+ return false;
116
+ }
117
+ // Isolate the letter.
118
+ let mMove, cMove;
119
+ // Start by moving everything to the left.
120
+ this._left.subtract(this._right);
121
+ this._right.zero();
122
+ let values = [...this._left.monoms];
123
+ for (let m of values) {
124
+ if (!m.hasLetter(letter)) {
125
+ mMove = m.clone();
126
+ this._left.subtract(mMove);
127
+ this._right.subtract(mMove);
128
+ }
129
+ }
130
+ // In theory, we should have only one item on the left.
131
+ if (this._left.length !== 1) {
132
+ return false;
133
+ }
134
+ cMove = this._left.monoms[0].coefficient.clone();
135
+ this._left.divide(cMove);
136
+ this._right.divide(cMove);
137
+ return this;
138
+ };
139
+ this.replaceBy = (letter, P) => {
140
+ this._left.replaceBy(letter, P);
141
+ this._right.replaceBy(letter, P);
142
+ return this;
143
+ };
144
+ /**
145
+ * Multiple an equation by a fraction value.
146
+ * @param value
147
+ */
148
+ this.multiply = (value) => {
149
+ // Make sure we have a fraction.
150
+ let F = new fraction_1.Fraction(value);
151
+ // Multiply each part of the equation by the fraction
152
+ this._left.multiply(F);
153
+ this._right.multiply(F);
154
+ // The sign of the inequation must be changed.
155
+ if (this._sign !== '=' && F.sign() === -1) {
156
+ this._reverseSign();
157
+ }
158
+ return this;
159
+ };
160
+ /**
161
+ * divide an equation by a given value (transformed as a fraction)
162
+ *
163
+ * ```
164
+ * 8x+10=6x \vert 2
165
+ * 4x+5=3x
166
+ * ```
167
+ *
168
+ * |>Alternatively with $3x-4$ maybe it's working ?
169
+ * $$\frac{3x}{5}$$
170
+ *
171
+ * @param value
172
+ * @returns {Equation}
173
+ */
174
+ this.divide = (value) => {
175
+ // Make sure we have a fraction.
176
+ let F = new fraction_1.Fraction(value);
177
+ if (F.isZero()) {
178
+ return this;
179
+ }
180
+ else {
181
+ return this.multiply(F.invert());
182
+ }
183
+ };
184
+ /**
185
+ * Get the degree of the equation
186
+ * @param letter
187
+ */
188
+ this.degree = (letter) => {
189
+ return fraction_1.Fraction.max(this._left.degree(letter), this._right.degree(letter));
190
+ };
191
+ /**
192
+ * Determine if the equation contains more than one letter/variable.
193
+ */
194
+ this.isMultiVariable = () => {
195
+ return this._left.isMultiVariable || this._right.isMultiVariable;
196
+ };
197
+ this.letters = () => {
198
+ // @ts-ignore
199
+ return [...new Set([...this._left.letters(), ...this._right.letters()])];
200
+ };
201
+ // -----------------------------------------------
202
+ // Equations helpers
203
+ // -----------------------------------------------
204
+ // -----------------------------------------------
205
+ this.solve = () => {
206
+ // Initialise the variables:
207
+ this._solutions = [];
208
+ // TODO: consolidate solving equations (inequations vs equations)
209
+ // TODO: work with not natural degrees ?
210
+ this._polynom = this._left.clone().subtract(this._right);
211
+ switch (this._polynom.degree().value) {
212
+ case 0:
213
+ case 1:
214
+ this._solveDegree1();
215
+ break;
216
+ case 2:
217
+ this._solveDegree2();
218
+ break;
219
+ default:
220
+ this._solveDegree3plus();
221
+ }
222
+ // cleanup the solutions.
223
+ this._solutions = Equation.makeSolutionsUnique(this._solutions);
224
+ return this;
225
+ };
226
+ this.test = (values) => {
227
+ return this.left.evaluate(values).isEqual(this.right.evaluate(values));
228
+ };
229
+ this.isSameAs = (equ) => {
230
+ let p1 = equ.clone().moveLeft().left, p2 = this.clone().moveLeft().left;
231
+ // They are the same.
232
+ return p1.isEqual(p2) || p1.isOpposedAt(p2);
233
+ };
234
+ this.isLinearTo = (equ) => {
235
+ // Move all left.
236
+ let p1 = equ.clone().moveLeft().simplify().left, p2 = this.clone().moveLeft().simplify().left;
237
+ // They are the same.
238
+ return p1.isEqual(p2) || p1.isOpposedAt(p2);
239
+ };
240
+ this._findSign = (equationString) => {
241
+ let strSign = '';
242
+ if (equationString.includes('geq')) {
243
+ return (equationString.includes('\\geq')) ? '\\geq' : 'geq';
244
+ }
245
+ else if (equationString.includes('leq')) {
246
+ return (equationString.includes('\\leq')) ? '\\leq' : 'leq';
247
+ }
248
+ else if (equationString.includes('>=')) {
249
+ return '>=';
250
+ }
251
+ else if (equationString.includes('=>')) {
252
+ return '=>';
253
+ }
254
+ else if (equationString.includes('>')) {
255
+ return '>';
256
+ }
257
+ else if (equationString.includes('<=')) {
258
+ return '<=';
259
+ }
260
+ else if (equationString.includes('=<')) {
261
+ return '=<';
262
+ }
263
+ else if (equationString.includes('<')) {
264
+ return '<';
265
+ }
266
+ else if (equationString.includes('=')) {
267
+ return '=';
268
+ }
269
+ if (strSign === '') {
270
+ console.log('Equation: parse string : sign not found');
271
+ return false;
272
+ }
273
+ };
274
+ // -----------------------------------------------
275
+ // Equations solving algorithms
276
+ this._formatSign = (signStr) => {
277
+ if (signStr === undefined) {
278
+ return '=';
279
+ }
280
+ if (signStr.includes('geq')) {
281
+ return '>=';
282
+ }
283
+ else if (signStr.includes('>=')) {
284
+ return '>=';
285
+ }
286
+ else if (signStr.includes('=>')) {
287
+ return '>=';
288
+ }
289
+ else if (signStr.includes('>')) {
290
+ return '>';
291
+ }
292
+ else if (signStr.includes('leq')) {
293
+ return '<=';
294
+ }
295
+ else if (signStr.includes('<=')) {
296
+ return '<=';
297
+ }
298
+ else if (signStr.includes('=<')) {
299
+ return '<=';
300
+ }
301
+ else if (signStr.includes('<')) {
302
+ return '<';
303
+ }
304
+ else {
305
+ return '=';
306
+ }
307
+ };
308
+ this._reverseSign = () => {
309
+ if (this._sign === '=') {
310
+ return this;
311
+ }
312
+ if (this._sign.includes('<')) {
313
+ this._sign.replace('<', '>');
314
+ return this;
315
+ }
316
+ if (this._sign.includes('>')) {
317
+ this._sign.replace('>', '<');
318
+ return this;
319
+ }
320
+ return this;
321
+ };
322
+ this.isGreater = () => {
323
+ if (this._sign.indexOf('>') !== -1) {
324
+ return true;
325
+ }
326
+ return this._sign.indexOf('geq') !== -1;
327
+ };
328
+ this.isStrictEqual = () => {
329
+ return this._sign === '=';
330
+ };
331
+ this.isAlsoEqual = () => {
332
+ if (this._sign.indexOf('=') !== -1) {
333
+ return true;
334
+ }
335
+ if (this._sign.indexOf('geq') !== -1) {
336
+ return true;
337
+ }
338
+ if (this._sign.indexOf('leq') !== -1) {
339
+ return true;
340
+ }
341
+ };
342
+ this._solveDegree1 = (letter) => {
343
+ const m1 = this._polynom.monomByDegree(1, letter).coefficient, m0 = this._polynom.monomByDegree(0, letter).coefficient, v = m0.clone().opposed().divide(m1);
344
+ let s, d;
345
+ if (this.isStrictEqual()) {
346
+ if (m1.value === 0) {
347
+ // In this case, the coefficient of the x variable is zero.
348
+ if (m0.value === 0) {
349
+ this._solutions = [{
350
+ tex: this._real,
351
+ display: "RR",
352
+ value: NaN,
353
+ exact: false
354
+ }];
355
+ }
356
+ else {
357
+ this._solutions = [{
358
+ tex: this._varnothing,
359
+ display: "O/",
360
+ value: NaN,
361
+ exact: false
362
+ }];
363
+ }
364
+ }
365
+ else {
366
+ this._solutions = [{
367
+ tex: v.tex,
368
+ display: v.display,
369
+ value: v.value,
370
+ exact: v
371
+ }];
372
+ }
373
+ }
374
+ else {
375
+ if (m1.value === 0) {
376
+ // In this case, the coefficient of the x variable is zero.
377
+ if (m0.value === 0 && this.isAlsoEqual()) {
378
+ s = '\\mathbb{R}';
379
+ d = "RR";
380
+ }
381
+ else {
382
+ if (m0.value > 0) {
383
+ s = this.isGreater() ? this._real : this._varnothing;
384
+ s = this.isGreater() ? "RR" : "O/";
385
+ }
386
+ else {
387
+ s = !this.isGreater() ? this._real : this._varnothing;
388
+ s = !this.isGreater() ? "RR" : "O/";
389
+ }
390
+ }
391
+ }
392
+ else {
393
+ // Must handle the case if the m1 monom is negative.
394
+ if ((this.isGreater() && m1.sign() === 1) || (!this.isGreater() && m1.sign() === -1)) {
395
+ s = `\\left${this.isAlsoEqual() ? '[' : ']'}${v.tex};+\\infty\\right[`;
396
+ d = `${this.isAlsoEqual() ? '[' : ']'}${v.tex};+oo[`;
397
+ }
398
+ else {
399
+ s = `\\left]-\\infty;${v.tex} \\right${this.isAlsoEqual() ? ']' : '['}`;
400
+ d = `]-oo;${v.tex}${this.isAlsoEqual() ? ']' : '['}`;
401
+ }
402
+ }
403
+ this._solutions = [{
404
+ tex: s,
405
+ display: d,
406
+ value: NaN,
407
+ exact: false
408
+ }];
409
+ }
410
+ return this._solutions;
411
+ };
412
+ this._solveDegree2 = (letter) => {
413
+ let aF = this._polynom.monomByDegree(2, letter).coefficient, bF = this._polynom.monomByDegree(1, letter).coefficient, cF = this._polynom.monomByDegree(0, letter).coefficient, delta, nthDelta, lcm = numeric_1.Numeric.lcm(aF.denominator, bF.denominator, cF.denominator), a = aF.multiply(lcm).value, b = bF.multiply(lcm).value, c = cF.multiply(lcm).value, realX1, realX2, sX1, sX2;
414
+ delta = b * b - 4 * a * c;
415
+ if (delta > 0) {
416
+ realX1 = (-b - Math.sqrt(delta)) / (2 * a);
417
+ realX2 = (-b + Math.sqrt(delta)) / (2 * a);
418
+ if (delta > 1.0e5) {
419
+ // The delta is too big to be parsed !
420
+ let v1 = ((-b - Math.sqrt(delta)) / (2 * a)).toFixed(5), v2 = ((-b + Math.sqrt(delta)) / (2 * a)).toFixed(5);
421
+ this._solutions = [
422
+ {
423
+ tex: v1,
424
+ display: v1,
425
+ value: realX1,
426
+ exact: false
427
+ },
428
+ {
429
+ tex: v2,
430
+ display: v2,
431
+ value: realX2,
432
+ exact: false
433
+ }
434
+ ];
435
+ }
436
+ else {
437
+ nthDelta = new nthRoot_1.NthRoot(delta).reduce();
438
+ if (nthDelta.hasRadical()) {
439
+ // -b +- coeff\sqrt{radical}
440
+ // -------------------------
441
+ // 2a
442
+ let gcd = numeric_1.Numeric.gcd(b, 2 * a, nthDelta.coefficient), am = a / gcd, bm = b / gcd;
443
+ nthDelta.coefficient = nthDelta.coefficient / gcd;
444
+ if (a < 0) {
445
+ am = -am;
446
+ bm = -bm;
447
+ }
448
+ let tex1 = "", tex2 = "", display1 = "", display2 = "";
449
+ tex1 = `${bm !== 0 ? ((-bm) + ' - ') : ''}${nthDelta.tex}`;
450
+ tex2 = `${bm !== 0 ? ((-bm) + ' + ') : ''}${nthDelta.tex}`;
451
+ display1 = `${bm !== 0 ? ((-bm) + ' - ') : ''}${nthDelta.display}`;
452
+ display2 = `${bm !== 0 ? ((-bm) + ' + ') : ''}${nthDelta.display}`;
453
+ if (am !== 1) {
454
+ tex1 = `\\frac{ ${tex1} }{ ${2 * am} }`;
455
+ tex2 = `\\frac{ ${tex2} }{ ${2 * am} }`;
456
+ }
457
+ this._solutions = [
458
+ {
459
+ tex: tex1,
460
+ display: tex1,
461
+ value: realX1,
462
+ exact: false
463
+ },
464
+ {
465
+ tex: tex2,
466
+ display: tex2,
467
+ value: realX2,
468
+ exact: false
469
+ },
470
+ ];
471
+ // if (b !== 0) {
472
+ // if (2 * a / gcd === 1) {
473
+ // this._solutions = [
474
+ // {
475
+ // tex: `${-b / gcd} - ${nthDelta.tex}`,
476
+ // value: realX1,
477
+ // exact: false // TODO: implement exact value with nthroot
478
+ // },
479
+ // {
480
+ // tex: `${-b / gcd} + ${nthDelta.tex}`,
481
+ // value: realX2,
482
+ // exact: false
483
+ // },
484
+ //
485
+ // ]
486
+ // } else {
487
+ // this._solutions = [
488
+ // {
489
+ // tex: `\\frac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
490
+ // value: realX1,
491
+ // exact: false
492
+ // },
493
+ // {
494
+ // tex: `\\frac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
495
+ // value: realX2,
496
+ // exact: false
497
+ // },
498
+ // ]
499
+ // }
500
+ // } else {
501
+ // if (2 * a / gcd === 1) {
502
+ // this._solutions = [
503
+ // {
504
+ // tex: `- ${nthDelta.tex}`,
505
+ // value: realX1,
506
+ // exact: false
507
+ // },
508
+ // {
509
+ // tex: `${nthDelta.tex}`,
510
+ // value: realX2,
511
+ // exact: false
512
+ // },
513
+ // ]
514
+ // } else {
515
+ // this._solutions = [
516
+ // {
517
+ // tex: `\\frac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
518
+ // value: realX1,
519
+ // exact: false
520
+ // },
521
+ // {
522
+ // tex: `\\frac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
523
+ // value: realX2,
524
+ // exact: false
525
+ // },
526
+ // ]
527
+ // }
528
+ // }
529
+ }
530
+ else {
531
+ // -b +- d / 2a
532
+ const S1 = new fraction_1.Fraction(-b - nthDelta.coefficient, 2 * a).reduce(), S2 = new fraction_1.Fraction(-b + nthDelta.coefficient, 2 * a).reduce();
533
+ this._solutions = [
534
+ {
535
+ tex: S1.frac,
536
+ display: S1.display,
537
+ value: realX1,
538
+ exact: S1
539
+ },
540
+ {
541
+ tex: S2.frac,
542
+ display: S2.display,
543
+ value: realX2,
544
+ exact: S2
545
+ }
546
+ ];
547
+ }
548
+ }
549
+ }
550
+ else if (delta === 0) {
551
+ const sol = new fraction_1.Fraction(-b, 2 * a).reduce();
552
+ this._solutions = [{
553
+ tex: sol.frac,
554
+ display: sol.display,
555
+ value: sol.value,
556
+ exact: sol
557
+ }];
558
+ }
559
+ else {
560
+ this._solutions = [{
561
+ tex: this._varnothing,
562
+ display: "O/",
563
+ value: NaN,
564
+ exact: false
565
+ }];
566
+ }
567
+ // Handle now the inequations.
568
+ if (!this.isStrictEqual()) {
569
+ if (this._solutions.length === 2) {
570
+ sX1 = (realX1 < realX2) ? this._solutions[0].tex : this._solutions[1].tex;
571
+ sX2 = (realX1 < realX2) ? this._solutions[1].tex : this._solutions[0].tex;
572
+ if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
573
+ this._solutions = [{
574
+ tex: `\\left]-\\infty ; ${sX1}\\right${this.isAlsoEqual() ? ']' : '['} \\cup \\left${this.isAlsoEqual() ? '[' : ']'}${sX2};+\\infty\\right[`,
575
+ display: `]-oo;${sX1}${this.isAlsoEqual() ? ']' : '['}uu${this.isAlsoEqual() ? '[' : ']'}${sX2};+oo[`,
576
+ value: NaN,
577
+ exact: false
578
+ }
579
+ ];
580
+ }
581
+ else {
582
+ this._solutions = [{
583
+ tex: `\\left${this.isAlsoEqual() ? '[' : ']'}${sX1} ; ${sX2}\\right${this.isAlsoEqual() ? ']' : '['}`,
584
+ display: `${this.isAlsoEqual() ? '[' : ']'}${sX1};${sX2}${this.isAlsoEqual() ? ']' : '['}`,
585
+ value: NaN,
586
+ exact: false
587
+ }];
588
+ }
589
+ }
590
+ else if (this._solutions.length === 1 && this._solutions[0].tex !== this._varnothing) {
591
+ if (!this.isAlsoEqual()) {
592
+ if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
593
+ this._solutions = [{
594
+ tex: `\\left]-\\infty ; ${this._solutions[0].tex}\\right[ \\cup \\left]${this._solutions[0].tex};+\\infty\\right[`,
595
+ display: `]-oo;${this._solutions[0].tex}[uu]${this._solutions[0].tex};+oo[`,
596
+ value: NaN,
597
+ exact: false
598
+ }
599
+ ];
600
+ }
601
+ else {
602
+ this._solutions = [{
603
+ tex: this._varnothing,
604
+ display: "O/",
605
+ value: NaN,
606
+ exact: false
607
+ }];
608
+ }
609
+ }
610
+ else {
611
+ if ((this.isGreater() && aF.sign() === 1) || (!this.isGreater() && aF.sign() === -1)) {
612
+ this._solutions = [{
613
+ tex: this._real,
614
+ display: "RR",
615
+ value: NaN,
616
+ exact: false
617
+ }];
618
+ }
619
+ else {
620
+ // this._texSolutions = [ this._texSolutions[0] ];
621
+ }
622
+ }
623
+ }
624
+ else {
625
+ if (this.isGreater()) {
626
+ this._solutions = [{
627
+ tex: aF.sign() === 1 ? this._real : this._varnothing,
628
+ display: aF.sign() === 1 ? "RR" : "O/",
629
+ value: NaN,
630
+ exact: false
631
+ }];
632
+ }
633
+ else {
634
+ this._solutions = [{
635
+ tex: aF.sign() === -1 ? this._real : this._varnothing,
636
+ display: aF.sign() === -1 ? "RR" : "O/",
637
+ value: NaN,
638
+ exact: false
639
+ }];
640
+ }
641
+ }
642
+ }
643
+ return this._solutions;
644
+ };
645
+ this._solveDegree3plus = (letter) => {
646
+ // Push everything to the left
647
+ // factorize
648
+ // solve each factors.
649
+ let equ = this.clone().moveLeft();
650
+ equ.left.factorize();
651
+ this._solutions = [];
652
+ equ.left.factors.forEach(factor => {
653
+ if (factor.degree(letter).leq(2)) {
654
+ let factorAsEquation = new Equation(factor, 0);
655
+ factorAsEquation.solve();
656
+ factorAsEquation.solutions.forEach(solution => {
657
+ this._solutions.push(solution);
658
+ });
659
+ }
660
+ else {
661
+ console.log(factor.tex, ': cannot actually get the solution of this equation');
662
+ }
663
+ });
664
+ // TODO: check equation resolution for more than degree 2
665
+ // this._solutions = [{tex: 'solve x - not yet handled', value: NaN, exact: false}]; // ESLint remove system :(
666
+ return this._solutions;
667
+ };
668
+ // Default equation
669
+ this._left = new polynom_1.Polynom().zero();
670
+ this._right = new polynom_1.Polynom().zero();
671
+ this._sign = '=';
672
+ if (equations.length === 1) {
673
+ if (equations[0] instanceof Equation) {
674
+ return equations[0].clone();
675
+ }
676
+ else if (typeof equations[0] === 'string') {
677
+ this.parse(equations[0]);
678
+ }
679
+ }
680
+ else if (equations.length === 2) {
681
+ if (equations[0] instanceof polynom_1.Polynom) {
682
+ this.left = equations[0].clone();
683
+ }
684
+ else if (typeof equations[0] === 'string') {
685
+ this.left = new polynom_1.Polynom(equations[0]);
686
+ }
687
+ if (equations[1] instanceof polynom_1.Polynom) {
688
+ this.right = equations[1].clone();
689
+ }
690
+ else if (typeof equations[1] === 'string') {
691
+ this.right = new polynom_1.Polynom(equations[1]);
692
+ }
693
+ }
694
+ else {
695
+ // Return default empty equation
696
+ return this;
697
+ }
698
+ return this;
699
+ }
700
+ get left() {
701
+ return this._left;
702
+ }
703
+ set left(value) {
704
+ this._left = value;
705
+ }
706
+ get right() {
707
+ return this._right;
708
+ }
709
+ // ------------------------------------------
710
+ // Getter and setter
711
+ set right(value) {
712
+ this._right = value;
713
+ }
714
+ get sign() {
715
+ return this._sign;
716
+ }
717
+ set sign(value) {
718
+ // Set the sign value as formatted.
719
+ this._sign = this._formatSign(value);
720
+ }
721
+ // ------------------------------------------
722
+ get solutions() {
723
+ return this._solutions;
724
+ }
725
+ get isEquation() {
726
+ return true;
727
+ }
728
+ get solution() {
729
+ if (this._solutions.length === 1
730
+ &&
731
+ (this._solutions[0].tex === this._real
732
+ || this._solutions[0].tex === this._varnothing
733
+ || this._solutions[0].tex.includes('\\left'))) {
734
+ return `S = ${this._solutions[0]}`;
735
+ }
736
+ return `S = \\left{ ${this._solutions.map(x => x.tex).join(';')} \\right}`;
737
+ }
738
+ get isReal() {
739
+ if (this._solutions === undefined) {
740
+ this.solve();
741
+ }
742
+ return this._solutions[0].tex === this._real;
743
+ }
744
+ get isVarnothing() {
745
+ if (this._solutions === undefined) {
746
+ this.solve();
747
+ }
748
+ return this._solutions[0].tex === this._varnothing;
749
+ }
750
+ get signAsTex() {
751
+ if (this._sign === '>=' || this._sign === '=>' || this._sign === 'geq') {
752
+ return '\\geq';
753
+ }
754
+ if (this._sign === '<=' || this._sign === '=<' || this._sign === 'leq') {
755
+ return '\\leq';
756
+ }
757
+ return this._sign;
758
+ }
759
+ get tex() {
760
+ return `${this._left.tex}${this.signAsTex}${this._right.tex}`;
761
+ }
762
+ get display() {
763
+ return `${this._left.display}${this.signAsTex}${this._right.display}`;
764
+ }
765
+ get raw() {
766
+ return `${this._left.raw}${this.signAsTex}${this._right.raw}`;
767
+ }
768
+ get variables() {
769
+ return [...new Set(this._right.variables.concat(this._left.variables))];
770
+ }
771
+ get numberOfVars() {
772
+ return this.variables.length;
773
+ }
774
+ get randomizeDefaults() {
775
+ return this._randomizeDefaults;
776
+ }
777
+ set randomizeDefaults(value) {
778
+ this._randomizeDefaults = value;
779
+ }
780
+ static makeSolutionsUnique(solutions, sorted) {
781
+ let solutionAsTex = [], uniqueSolutions = solutions.filter(sol => {
782
+ if (!solutionAsTex.includes(sol.tex)) {
783
+ solutionAsTex.push(sol.tex);
784
+ return true;
785
+ }
786
+ else {
787
+ return false;
788
+ }
789
+ });
790
+ if (sorted === true) {
791
+ uniqueSolutions.sort((a, b) => a.value - b.value);
792
+ }
793
+ return uniqueSolutions;
794
+ }
795
+ }
796
+ exports.Equation = Equation;
786
797
  //# sourceMappingURL=equation.js.map