pimath 0.0.132 → 0.0.134

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