pimath 0.0.126 → 0.0.128

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 (121) hide show
  1. package/.idea/jsLibraryMappings.xml +6 -0
  2. package/{public/index.html → dist/demo/exercises.html} +2 -2
  3. package/dist/demo/matrices.html +39 -0
  4. package/dist/demo/playground.html +20 -0
  5. package/dist/demo.css +3 -0
  6. package/dist/pimath.js +4239 -7819
  7. package/dist/pimath.umd.cjs +15 -0
  8. package/index.html +15 -0
  9. package/package.json +16 -8
  10. package/public/demo/exercises.html +283 -0
  11. package/public/demo/matrices.html +39 -0
  12. package/public/demo/playground.html +20 -0
  13. package/src/demo/exercises.ts +0 -0
  14. package/src/demo/matrices.ts +61 -0
  15. package/src/demo/playground.ts +153 -0
  16. package/src/{index.ts → main.ts} +1 -1
  17. package/src/maths/algebra/polynom.ts +9 -16
  18. package/src/maths/algebra/rational.ts +8 -4
  19. package/src/maths/geometry/line.ts +22 -25
  20. package/src/maths/geometry/point.ts +43 -29
  21. package/src/maths/randomization/random.ts +7 -0
  22. package/src/maths/randomization/rndGeometryCircle.ts +50 -0
  23. package/src/maths/randomization/rndTypes.ts +10 -4
  24. package/tests/algebra/polynom.test.ts +10 -0
  25. package/tests/geometry/circle.test.ts +222 -1
  26. package/tests/numeric.test.ts +19 -3
  27. package/tsconfig.json +15 -4
  28. package/vite.config.js +23 -0
  29. package/webpack-production.config.js +22 -22
  30. package/webpack.config.js +22 -22
  31. package/dev/pimath.js +0 -7945
  32. package/dev/pimath.js.map +0 -1
  33. package/dist/pimath.js.map +0 -1
  34. package/dist/pimath.min.js +0 -2
  35. package/dist/pimath.min.js.map +0 -1
  36. package/esm/index.d.ts +0 -38
  37. package/esm/index.js +0 -44
  38. package/esm/index.js.map +0 -1
  39. package/esm/maths/algebra/equation.d.ts +0 -119
  40. package/esm/maths/algebra/equation.js +0 -797
  41. package/esm/maths/algebra/equation.js.map +0 -1
  42. package/esm/maths/algebra/linearSystem.d.ts +0 -39
  43. package/esm/maths/algebra/linearSystem.js +0 -279
  44. package/esm/maths/algebra/linearSystem.js.map +0 -1
  45. package/esm/maths/algebra/logicalset.d.ts +0 -28
  46. package/esm/maths/algebra/logicalset.js +0 -158
  47. package/esm/maths/algebra/logicalset.js.map +0 -1
  48. package/esm/maths/algebra/monom.d.ts +0 -206
  49. package/esm/maths/algebra/monom.js +0 -909
  50. package/esm/maths/algebra/monom.js.map +0 -1
  51. package/esm/maths/algebra/polynom.d.ts +0 -157
  52. package/esm/maths/algebra/polynom.js +0 -1305
  53. package/esm/maths/algebra/polynom.js.map +0 -1
  54. package/esm/maths/algebra/rational.d.ts +0 -46
  55. package/esm/maths/algebra/rational.js +0 -195
  56. package/esm/maths/algebra/rational.js.map +0 -1
  57. package/esm/maths/algebra/study/rationalStudy.d.ts +0 -28
  58. package/esm/maths/algebra/study/rationalStudy.js +0 -244
  59. package/esm/maths/algebra/study/rationalStudy.js.map +0 -1
  60. package/esm/maths/algebra/study.d.ts +0 -143
  61. package/esm/maths/algebra/study.js +0 -380
  62. package/esm/maths/algebra/study.js.map +0 -1
  63. package/esm/maths/coefficients/fraction.d.ts +0 -90
  64. package/esm/maths/coefficients/fraction.js +0 -517
  65. package/esm/maths/coefficients/fraction.js.map +0 -1
  66. package/esm/maths/coefficients/nthRoot.d.ts +0 -23
  67. package/esm/maths/coefficients/nthRoot.js +0 -137
  68. package/esm/maths/coefficients/nthRoot.js.map +0 -1
  69. package/esm/maths/geometry/circle.d.ts +0 -45
  70. package/esm/maths/geometry/circle.js +0 -324
  71. package/esm/maths/geometry/circle.js.map +0 -1
  72. package/esm/maths/geometry/line.d.ts +0 -99
  73. package/esm/maths/geometry/line.js +0 -485
  74. package/esm/maths/geometry/line.js.map +0 -1
  75. package/esm/maths/geometry/point.d.ts +0 -34
  76. package/esm/maths/geometry/point.js +0 -167
  77. package/esm/maths/geometry/point.js.map +0 -1
  78. package/esm/maths/geometry/triangle.d.ts +0 -91
  79. package/esm/maths/geometry/triangle.js +0 -276
  80. package/esm/maths/geometry/triangle.js.map +0 -1
  81. package/esm/maths/geometry/vector.d.ts +0 -41
  82. package/esm/maths/geometry/vector.js +0 -198
  83. package/esm/maths/geometry/vector.js.map +0 -1
  84. package/esm/maths/numeric.d.ts +0 -28
  85. package/esm/maths/numeric.js +0 -136
  86. package/esm/maths/numeric.js.map +0 -1
  87. package/esm/maths/numexp.d.ts +0 -19
  88. package/esm/maths/numexp.js +0 -186
  89. package/esm/maths/numexp.js.map +0 -1
  90. package/esm/maths/randomization/random.d.ts +0 -23
  91. package/esm/maths/randomization/random.js +0 -79
  92. package/esm/maths/randomization/random.js.map +0 -1
  93. package/esm/maths/randomization/randomCore.d.ts +0 -7
  94. package/esm/maths/randomization/randomCore.js +0 -22
  95. package/esm/maths/randomization/randomCore.js.map +0 -1
  96. package/esm/maths/randomization/rndFraction.d.ts +0 -12
  97. package/esm/maths/randomization/rndFraction.js +0 -44
  98. package/esm/maths/randomization/rndFraction.js.map +0 -1
  99. package/esm/maths/randomization/rndGeometryLine.d.ts +0 -12
  100. package/esm/maths/randomization/rndGeometryLine.js +0 -46
  101. package/esm/maths/randomization/rndGeometryLine.js.map +0 -1
  102. package/esm/maths/randomization/rndGeometryPoint.d.ts +0 -12
  103. package/esm/maths/randomization/rndGeometryPoint.js +0 -61
  104. package/esm/maths/randomization/rndGeometryPoint.js.map +0 -1
  105. package/esm/maths/randomization/rndHelpers.d.ts +0 -23
  106. package/esm/maths/randomization/rndHelpers.js +0 -98
  107. package/esm/maths/randomization/rndHelpers.js.map +0 -1
  108. package/esm/maths/randomization/rndMonom.d.ts +0 -12
  109. package/esm/maths/randomization/rndMonom.js +0 -53
  110. package/esm/maths/randomization/rndMonom.js.map +0 -1
  111. package/esm/maths/randomization/rndPolynom.d.ts +0 -13
  112. package/esm/maths/randomization/rndPolynom.js +0 -75
  113. package/esm/maths/randomization/rndPolynom.js.map +0 -1
  114. package/esm/maths/randomization/rndTypes.d.ts +0 -34
  115. package/esm/maths/randomization/rndTypes.js +0 -3
  116. package/esm/maths/randomization/rndTypes.js.map +0 -1
  117. package/esm/maths/shutingyard.d.ts +0 -59
  118. package/esm/maths/shutingyard.js +0 -443
  119. package/esm/maths/shutingyard.js.map +0 -1
  120. package/public/matrices.html +0 -100
  121. package/public/playground.html +0 -168
@@ -1,909 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Monom = void 0;
4
- /***
5
- * Monom class
6
- */
7
- const numeric_1 = require("../numeric");
8
- const shutingyard_1 = require("../shutingyard");
9
- const fraction_1 = require("../coefficients/fraction");
10
- class Monom {
11
- /**
12
- * Create a Monom
13
- * Defined as \\(k \\cdot x^{n}\\), where \\( k,n \in \\mathbb{Q}\\).
14
- * Examples: \\(3x^2\\) or \\(3/5x^2\\)
15
- * @param value (optional) string The value that should be parse. Can be a Monom, a Fraction, a string or a number. If nothing is provided, it will return the trivial monom (0).
16
- */
17
- constructor(value) {
18
- // -----------------------------------------
19
- /**
20
- * Parse a string to a monom. The string may include fraction.
21
- * @param inputStr
22
- */
23
- this.parse = (inputStr) => {
24
- if (typeof inputStr === 'string') {
25
- this._shutingYardToReducedMonom(inputStr);
26
- }
27
- else if (typeof inputStr === 'number') {
28
- this._coefficient = new fraction_1.Fraction(inputStr);
29
- this._literal = {};
30
- }
31
- else if (inputStr instanceof fraction_1.Fraction) {
32
- this._coefficient = inputStr.clone();
33
- this._literal = {};
34
- }
35
- else if (inputStr instanceof Monom) {
36
- this._coefficient = inputStr._coefficient.clone();
37
- this._literal = this.copyLiterals(inputStr.literal);
38
- }
39
- return this;
40
- };
41
- this.addToken = (stack, element) => {
42
- let q1, q2, m, letter, pow;
43
- if (element.tokenType === shutingyard_1.ShutingyardType.COEFFICIENT) {
44
- stack.push(new Monom(new fraction_1.Fraction(element.token)));
45
- }
46
- else if (element.tokenType === shutingyard_1.ShutingyardType.VARIABLE) {
47
- let M = new Monom().one();
48
- M.setLetter(element.token, 1);
49
- stack.push(M.clone());
50
- }
51
- else if (element.tokenType === shutingyard_1.ShutingyardType.OPERATION) {
52
- switch (element.token) {
53
- case '-':
54
- // this should only happen for negative powers or for negative coefficient.
55
- q2 = (stack.pop()) || new Monom().zero();
56
- q1 = (stack.pop()) || new Monom().zero();
57
- stack.push(q1.subtract(q2));
58
- break;
59
- case '*':
60
- // Get the last element in the stack
61
- q2 = (stack.pop()) || new Monom().one();
62
- q1 = (stack.pop()) || new Monom().one();
63
- stack.push(q1.multiply(q2));
64
- break;
65
- case '/':
66
- // Get the last element in the stack
67
- q2 = (stack.pop()) || new Monom().one();
68
- q1 = (stack.pop()) || new Monom().one();
69
- stack.push(q1.divide(q2));
70
- break;
71
- case '^':
72
- // get the two last elements in the stack
73
- pow = (stack.pop().coefficient) || new fraction_1.Fraction().one();
74
- m = (stack.pop()) || new Monom().one();
75
- letter = m.variables[0];
76
- if (letter !== undefined) {
77
- m.setLetter(letter, pow);
78
- }
79
- stack.push(m);
80
- // this.multiply(m.clone())
81
- break;
82
- }
83
- }
84
- };
85
- /**
86
- * Clone the current Monom.
87
- */
88
- this.clone = () => {
89
- let F = new Monom();
90
- F.coefficient = this._coefficient.clone();
91
- // Copy the literal parts.
92
- for (let k in this._literal) {
93
- F.setLetter(k, this._literal[k].clone());
94
- }
95
- return F;
96
- };
97
- this.copyLiterals = (literal) => {
98
- let L = {};
99
- for (let k in literal) {
100
- L[k] = literal[k].clone();
101
- }
102
- return L;
103
- };
104
- this.makeSame = (M) => {
105
- // Copy the literal parts.
106
- for (let k in M._literal) {
107
- this.setLetter(k, M._literal[k].clone());
108
- }
109
- return this;
110
- };
111
- /**
112
- * Create a zero value monom
113
- */
114
- this.zero = () => {
115
- this._coefficient = new fraction_1.Fraction().zero();
116
- this._literal = {};
117
- return this;
118
- };
119
- /**
120
- * Create a one value monom
121
- */
122
- this.one = () => {
123
- this._coefficient = new fraction_1.Fraction().one();
124
- this._literal = {};
125
- return this;
126
- };
127
- /**
128
- * Clean the monom by removing each letters with a power of zero.
129
- */
130
- this.clean = () => {
131
- for (let letter in this._literal) {
132
- if (this._literal[letter].isZero()) {
133
- delete this._literal[letter];
134
- }
135
- }
136
- return this;
137
- };
138
- this.reduce = () => {
139
- this.clean();
140
- this.coefficient.reduce();
141
- return this;
142
- };
143
- // ------------------------------------------
144
- // Mathematical operations
145
- // ------------------------------------------
146
- /**
147
- * Get the opposed
148
- * Returns a monom.
149
- */
150
- this.opposed = () => {
151
- this._coefficient.opposed();
152
- return this;
153
- };
154
- /**
155
- * Add all similar monoms. If they aren't similar, they are simply skipped.
156
- * @param M (Monom[]) The monoms to add.
157
- */
158
- this.add = (...M) => {
159
- for (let m of M) {
160
- if (this.isSameAs(m)) {
161
- if (this.isZero()) {
162
- this.makeSame(m);
163
- }
164
- this._coefficient.add(m.coefficient);
165
- }
166
- else {
167
- console.log('Add monom: ' + this.display + ' is not similar with ', m.display);
168
- }
169
- }
170
- return this;
171
- };
172
- /**
173
- * Subtract multiple monoms
174
- * @param M (Monom[]) The monoms to subtract
175
- */
176
- this.subtract = (...M) => {
177
- for (let m of M) {
178
- if (this.isSameAs(m)) {
179
- if (this.isZero()) {
180
- this.makeSame(m);
181
- }
182
- this._coefficient.add(m.clone().coefficient.opposed());
183
- }
184
- else {
185
- console.log('Subtract: Is not similar: ', m.display);
186
- }
187
- }
188
- return this;
189
- };
190
- /**
191
- * Multiple multiple monoms to the current monom
192
- * @param M (Monom[]) The monoms to multiply to.
193
- */
194
- this.multiply = (...M) => {
195
- for (let m of M) {
196
- // Multiply the coefficient.
197
- this._coefficient.multiply(m.coefficient);
198
- // Multiply the literal parts.
199
- for (let letter in m.literal) {
200
- if (this._literal[letter] === undefined) {
201
- this._literal[letter] = m.literal[letter].clone();
202
- }
203
- else {
204
- this._literal[letter].add(m.literal[letter]);
205
- }
206
- }
207
- }
208
- return this;
209
- };
210
- this.multiplyByNumber = (F) => {
211
- this._coefficient.multiply(F);
212
- return this;
213
- };
214
- /**
215
- * Divide the current monoms by multiple monoms
216
- * @param M (Monom[])
217
- */
218
- this.divide = (...M) => {
219
- // Depending on the given value, choose the current item
220
- for (let v of M) {
221
- // Divide the coefficient
222
- this._coefficient.divide(v.coefficient);
223
- // Subtract the power values
224
- for (let letter in v.literal) {
225
- this._literal[letter] = (this._literal[letter] === undefined) ? v.literal[letter].clone().opposed() : this._literal[letter].subtract(v.literal[letter]);
226
- // If the power of a particular setLetter is zero, delete it from the literal part..
227
- if (this._literal[letter].isZero()) {
228
- delete this._literal[letter];
229
- }
230
- }
231
- }
232
- return this;
233
- };
234
- /**
235
- * Get the pow of a monom.
236
- * @param nb (number) : Mathematical pow
237
- */
238
- this.pow = (nb) => {
239
- this._coefficient.pow(nb);
240
- for (let letter in this._literal) {
241
- this._literal[letter].multiply(nb);
242
- }
243
- return this;
244
- };
245
- /**
246
- * Get the nth-root of the monom
247
- * @param p
248
- */
249
- this.root = (p) => {
250
- // TODO: determiner the nth root of a monom
251
- return this;
252
- };
253
- /**
254
- * Return the square root of a monom
255
- */
256
- this.sqrt = () => {
257
- if (this.isSquare()) {
258
- this._coefficient.sqrt();
259
- for (let letter in this._literal) {
260
- this._literal[letter].clone().divide(2);
261
- }
262
- }
263
- return this.root(2);
264
- };
265
- // ------------------------------------------
266
- // Compare functions
267
- // ------------------------------------------
268
- this.compare = (M, sign) => {
269
- // TODO: Build the compare systems.
270
- if (sign === undefined) {
271
- sign = '=';
272
- }
273
- switch (sign) {
274
- case '=':
275
- // To be equal, they must be the isSame
276
- if (!this.compare(M, 'same')) {
277
- return false;
278
- }
279
- // The literal parts are the isSame. The coefficient must be equal
280
- return this._coefficient.isEqual(M.coefficient);
281
- case 'same':
282
- // Get the list of all variables from both monoms.
283
- let M1 = this.variables, M2 = M.variables, K = M1.concat(M2.filter((item) => M1.indexOf(item) < 0));
284
- if (M1.length === 0 && M2.length === 0) {
285
- return true;
286
- }
287
- // To compare, both must be different than zero.
288
- if (!this.isZero() && !M.isZero()) {
289
- for (let key of K) {
290
- // The setLetter is not available in one of the monom
291
- if (this._literal[key] === undefined || M.literal[key] === undefined) {
292
- return false;
293
- }
294
- // The setLetter does not have the isSame power in each monoms.
295
- if (!this._literal[key].isEqual(M.literal[key])) {
296
- return false;
297
- }
298
- }
299
- }
300
- // All are positive check - the monoms are the sames.
301
- return true;
302
- default:
303
- return false;
304
- }
305
- };
306
- /**
307
- * Determine if two monoms are equals
308
- * @param M
309
- */
310
- this.isEqual = (M) => {
311
- return this.compare(M, '=');
312
- };
313
- /**
314
- * Determine if two monoms are similar
315
- * @param M
316
- */
317
- this.isSameAs = (M) => {
318
- return this.compare(M, 'same');
319
- };
320
- this.isSquare = () => {
321
- if (!this.coefficient.isSquare()) {
322
- return false;
323
- }
324
- return this.isLiteralSquare();
325
- };
326
- this.isLiteralSquare = () => {
327
- for (let letter in this.literal) {
328
- // A literal square must have a natural power
329
- if (this.literal[letter].isRational()) {
330
- return false;
331
- }
332
- // The natural power must be be even
333
- if (this.literal[letter].isEven()) {
334
- return false;
335
- }
336
- }
337
- return true;
338
- };
339
- this.hasFractionCoefficient = () => {
340
- for (let letter in this._literal) {
341
- if (this._literal[letter].isRational()) {
342
- return true;
343
- }
344
- }
345
- return false;
346
- };
347
- // ------------------------------------------
348
- // Misc monoms functions
349
- // -------------------------------------
350
- /**
351
- * Determine if a monom contains a setLetter in it's literal part
352
- * @param letter
353
- */
354
- this.hasLetter = (letter) => {
355
- // The letter was not found
356
- if (this._literal[letter === undefined ? 'x' : letter] === undefined) {
357
- return false;
358
- }
359
- // The letter is found and is not zero !
360
- return this._literal[letter === undefined ? 'x' : letter].isNotZero();
361
- };
362
- /**
363
- * Set the power of a particular setLetter
364
- * @param letter (string) Letter to change
365
- * @param pow (number) Power of the setLetter (must be positive integer.
366
- */
367
- this.setLetter = (letter, pow) => {
368
- if (pow instanceof fraction_1.Fraction) {
369
- // Set the power of the letter to zero => remove it
370
- if (this.hasLetter(letter) && pow.isZero()) {
371
- delete this._literal[letter];
372
- }
373
- this._literal[letter] = pow.clone();
374
- }
375
- else {
376
- this.setLetter(letter, new fraction_1.Fraction(pow));
377
- }
378
- };
379
- /**
380
- * Get the degree of a monom. If no setLetter is given, the result will be the global degree.
381
- * @param letter (string) Letter to get to degree (power)
382
- */
383
- this.degree = (letter) => {
384
- if (this.variables.length === 0) {
385
- return new fraction_1.Fraction().zero();
386
- }
387
- if (letter === undefined) {
388
- // Not setLetter given -> we get the global monom degree (sum of all the letters).
389
- return Object.values(this._literal).reduce((t, n) => t.clone().add(n));
390
- }
391
- else {
392
- // A setLetter is given -> get the corresponding power.
393
- return this._literal[letter] === undefined ? new fraction_1.Fraction().zero() : this._literal[letter].clone();
394
- }
395
- };
396
- /**
397
- * Evaluate a monom. Each setLetter must be assigned to a Fraction.
398
- * @param values Dictionary of <setLetter: Fraction>
399
- */
400
- this.evaluate = (values) => {
401
- let r = this.coefficient.clone();
402
- if (typeof values === 'number' || values instanceof fraction_1.Fraction) {
403
- let tmpValues = {};
404
- tmpValues[this.variables[0]] = new fraction_1.Fraction(values);
405
- return this.evaluate(tmpValues);
406
- }
407
- if (typeof values === 'object') {
408
- if (this.variables.length === 0) {
409
- return this.coefficient;
410
- }
411
- for (let L in this._literal) {
412
- if (values[L] === undefined) {
413
- return new fraction_1.Fraction().zero();
414
- }
415
- let value = new fraction_1.Fraction(values[L]);
416
- r.multiply(value.pow(this._literal[L]));
417
- }
418
- }
419
- return r;
420
- };
421
- this.evaluateAsNumeric = (values) => {
422
- let r = this.coefficient.value;
423
- if (typeof values === 'number') {
424
- let tmpValues = {};
425
- tmpValues[this.variables[0]] = values;
426
- return this.evaluateAsNumeric(tmpValues);
427
- }
428
- if (typeof values === 'object') {
429
- if (this.variables.length === 0) {
430
- return this.coefficient.value;
431
- }
432
- for (let L in this._literal) {
433
- if (values[L] === undefined) {
434
- return 0;
435
- }
436
- r *= values[L] ** (this._literal[L].value);
437
- }
438
- }
439
- return r;
440
- };
441
- /**
442
- * Derivative the monom
443
- * @param letter
444
- */
445
- this.derivative = (letter) => {
446
- // No setLetter given - assume it's the setLetter 'x'
447
- if (letter === undefined) {
448
- letter = 'x';
449
- }
450
- if (this.hasLetter(letter)) {
451
- let d = this._literal[letter].clone(), dM = this.clone();
452
- // Subtract one to the degree.
453
- dM._literal[letter].subtract(1);
454
- // Multiply the coefficient by the previous degree
455
- dM._coefficient.multiply(new fraction_1.Fraction(d.clone()));
456
- return dM;
457
- }
458
- else {
459
- return new Monom().zero();
460
- }
461
- };
462
- this.primitive = (letter) => {
463
- // TODO: derivative including the ln value => implies creating different monom system ?
464
- if (letter === undefined) {
465
- letter = 'x';
466
- }
467
- // Zero monom
468
- let M = this.clone(), degree;
469
- if (M.hasLetter(letter)) {
470
- degree = M.degree(letter).clone().add(1);
471
- M.coefficient = M.coefficient.clone().divide(degree);
472
- M.setLetter(letter, degree);
473
- }
474
- else {
475
- // There is no letter.
476
- // The coefficient might be zero (=> x) or a number a (=> ax)
477
- if (M.coefficient.isZero()) {
478
- M.coefficient = new fraction_1.Fraction().one();
479
- }
480
- M.setLetter(letter, 1);
481
- }
482
- return M;
483
- };
484
- // ----------------------------------------
485
- // Static functions
486
- // ----------------------------------------
487
- // TODO: The rest of the functions are not used or unnecessary ?
488
- /**
489
- * Determine if multiple monoms are similar
490
- * @param M
491
- */
492
- this.areSameAs = (...M) => {
493
- let result = true;
494
- // Check all monoms if they are the isSame as the "this" one.
495
- for (let i = 0; i < M.length; i++) {
496
- if (!this.isSameAs(M[i])) {
497
- return false;
498
- }
499
- }
500
- // All check passed -> all the monoms are similar.
501
- return result;
502
- };
503
- /**
504
- * Determine if multiple monoms are equals
505
- * @param M
506
- */
507
- this.areEquals = (...M) => {
508
- // They are not similar.
509
- if (!this.areSameAs(...M)) {
510
- return false;
511
- }
512
- // Check all coefficient. They must be equals.
513
- for (let m of M) {
514
- if (!this._coefficient.isEqual(m.coefficient)) {
515
- return false;
516
- }
517
- }
518
- // All checks passed.
519
- return true;
520
- };
521
- this.isDivisible = (div) => {
522
- // For all variables (letters), the current monom must have a degree higher than the divider
523
- if (div.degree().isStrictlyPositive()) {
524
- for (let letter of div.variables) {
525
- if (!this.degree(letter).geq(div.degree(letter))) {
526
- return false;
527
- }
528
- }
529
- }
530
- // If the coefficient is rational, we suppose we don't need to check the division by the coefficient.
531
- if (this.coefficient.isRational() || div.coefficient.isRational()) {
532
- return true;
533
- }
534
- return this.coefficient.clone().divide(div.coefficient).isRelative();
535
- };
536
- this._shutingYardToReducedMonom = (inputStr) => {
537
- // Get the RPN array of the current expression
538
- const SY = new shutingyard_1.Shutingyard().parse(inputStr);
539
- const rpn = SY.rpn;
540
- let stack = [], m, pow, letter, q1, q2;
541
- if (rpn.length === 0) {
542
- this.zero();
543
- return this;
544
- }
545
- else if (rpn.length === 1) {
546
- const element = rpn[0];
547
- this.one();
548
- if (element.tokenType === 'coefficient') {
549
- this.coefficient = new fraction_1.Fraction(element.token);
550
- }
551
- else if (element.tokenType === 'variable') {
552
- this.setLetter(element.token, 1);
553
- }
554
- return this;
555
- }
556
- else {
557
- // Reset the monom
558
- for (const element of rpn) {
559
- this.addToken(stack, element);
560
- }
561
- }
562
- this.one();
563
- this.multiply(stack[0]);
564
- return this;
565
- };
566
- this.zero();
567
- if (value !== undefined) {
568
- // A string is given - try to parse the value.
569
- this.parse(value);
570
- }
571
- return this;
572
- }
573
- // ------------------------------------------
574
- /**
575
- * Get the coefficient \\(k\\) of the Monom \\(k\\cdot x^{n}\\)
576
- * @returns {Fraction}
577
- */
578
- get coefficient() {
579
- return this._coefficient;
580
- }
581
- // ------------------------------------------
582
- // Getter and setter
583
- /**
584
- * Set the coefficient \\(k\\) value of the monom
585
- * @param {Fraction | number | string} F
586
- */
587
- set coefficient(F) {
588
- this._coefficient = new fraction_1.Fraction(F);
589
- }
590
- /**
591
- * Get the literal part of \\(x^{n_1}y^{n_2}\\) as dictionary \\[\\begin{array}{ll}x&=n_1\\\\y&=n_2\\end{array}\\]
592
- * @returns {literalType}
593
- */
594
- get literal() {
595
- return this._literal;
596
- }
597
- /**
598
- * Set the literal part of the monom. Must be a dictionary {x: Fraction, y: Fraction, ...}
599
- * @param {literalType} L
600
- */
601
- set literal(L) {
602
- this._literal = L;
603
- }
604
- /**
605
- * Get the literal square roots of the Monom.
606
- * TODO: remove this getter ? Is it used and is it correct ?
607
- * @returns {literalType}
608
- */
609
- get literalSqrt() {
610
- if (this.isLiteralSquare()) {
611
- let L = {};
612
- for (let key in this._literal) {
613
- L[key] = this._literal[key].clone().sqrt();
614
- }
615
- return L;
616
- }
617
- else {
618
- return this._literal;
619
- }
620
- }
621
- /**
622
- * Set the literal part of the monom from a string
623
- * @param inputStr String like x^2y^3
624
- */
625
- set literalStr(inputStr) {
626
- // TODO : parse using shutingyard tree !
627
- // Match all x^n
628
- for (const v of [...inputStr.matchAll(/([a-z])\^([+-]?[0-9]+)/g)]) {
629
- // Create the default letter entry if necessary.
630
- if (!(v[1] in this._literal)) {
631
- this._literal[v[1]] = new fraction_1.Fraction().zero();
632
- }
633
- // Add the new value.
634
- // TODO: actually, it adds only numeric value
635
- this._literal[v[1]].add(+v[2]);
636
- }
637
- // Match all x
638
- for (const v of [...inputStr.matchAll(/([a-z](?!\^))/g)]) {
639
- // Match all single letters
640
- if (!(v[1] in this._literal)) {
641
- this._literal[v[1]] = new fraction_1.Fraction().zero();
642
- }
643
- // Add one to the value.
644
- this._literal[v[1]].add(1);
645
- }
646
- }
647
- // Getter helpers.
648
- /**
649
- * Get the variables letters
650
- */
651
- get variables() {
652
- let M = this.clone().clean();
653
- return Object.keys(M.literal);
654
- }
655
- // Display getter
656
- /**
657
- * This display getter is to be used in the polynom display getter
658
- */
659
- get display() {
660
- let L = '', letters = Object.keys(this._literal).sort();
661
- for (let letter of letters) {
662
- if (this._literal[letter].isNotZero()) {
663
- L += `${letter}`;
664
- if (this._literal[letter].isNotEqual(1)) {
665
- L += `^(${this._literal[letter].display})`;
666
- }
667
- }
668
- }
669
- if (L === '') {
670
- // No setLetter - means it's only a number !
671
- if (this._coefficient.value != 0) {
672
- return `${this._coefficient.display}`;
673
- }
674
- else {
675
- return '';
676
- }
677
- }
678
- else {
679
- if (this._coefficient.value === 1) {
680
- return L;
681
- }
682
- else if (this._coefficient.value === -1) {
683
- return `-${L}`;
684
- }
685
- else if (this._coefficient.value === 0) {
686
- return '0';
687
- }
688
- else {
689
- return `${this._coefficient.display}${L}`;
690
- }
691
- }
692
- }
693
- get dividers() {
694
- // Decompose only if the coefficient is a natural number
695
- if (!this.coefficient.isRelative()) {
696
- return [this.clone()];
697
- }
698
- // Decompose only if the power values are natural numbers.
699
- if (this.hasFractionCoefficient()) {
700
- return [this.clone()];
701
- }
702
- // Security : do not do this if greater than 10000
703
- if (this.coefficient.numerator > 1000000) {
704
- return [this.clone()];
705
- }
706
- const dividers = numeric_1.Numeric.dividers(Math.abs(this.coefficient.numerator));
707
- // Decompose the literals parts.
708
- let literals = [];
709
- for (let L in this.literal) {
710
- // L is the letter.
711
- literals = this._getLiteralDividers(literals, L);
712
- }
713
- const monomDividers = [];
714
- if (literals.length > 0 && dividers.length > 0) {
715
- for (let N of dividers) {
716
- for (let L of literals) {
717
- let M = new Monom();
718
- M.coefficient = new fraction_1.Fraction(N);
719
- M.literal = L;
720
- monomDividers.push(M);
721
- }
722
- }
723
- }
724
- else if (dividers.length === 0) {
725
- for (let L of literals) {
726
- let M = new Monom();
727
- M.coefficient = new fraction_1.Fraction().one();
728
- M.literal = L;
729
- monomDividers.push(M);
730
- }
731
- }
732
- else {
733
- for (let N of dividers) {
734
- let M = new Monom();
735
- M.coefficient = new fraction_1.Fraction(N);
736
- monomDividers.push(M);
737
- }
738
- }
739
- return monomDividers.length === 0 ? [new Monom().one()] : monomDividers;
740
- }
741
- /**
742
- * Display the monom, forcing the '+' sign to appear
743
- */
744
- get displayWithSign() {
745
- let d = this.display;
746
- return (d[0] !== '-' ? '+' : '') + d;
747
- }
748
- get texWithSign() {
749
- if (this.coefficient.isStrictlyPositive()) {
750
- return '+' + this.tex;
751
- }
752
- return this.tex;
753
- }
754
- get plotFunction() {
755
- let L = '', letters = Object.keys(this._literal).sort();
756
- for (let letter of letters) {
757
- if (this._literal[letter].isNotZero()) {
758
- L += (L === '' ? "" : "*") + `${letter}`;
759
- if (this._literal[letter].isNotEqual(1)) {
760
- L += `^(${this._literal[letter].display})`;
761
- }
762
- }
763
- }
764
- // No literal part
765
- if (L === '') {
766
- // No setLetter - means it's only a number !
767
- if (this._coefficient.value != 0) {
768
- return `${this._coefficient.display}`;
769
- }
770
- else {
771
- return '';
772
- }
773
- }
774
- else {
775
- if (this._coefficient.value === 1) {
776
- return L;
777
- }
778
- else if (this._coefficient.value === -1) {
779
- return `-${L}`;
780
- }
781
- else if (this._coefficient.value === 0) {
782
- return '0';
783
- }
784
- else {
785
- return `${this._coefficient.display}*${L}`;
786
- }
787
- }
788
- }
789
- /**
790
- * Get the tex output of the monom
791
- */
792
- get tex() {
793
- // TODO: display with square root !
794
- let L = '', letters = Object.keys(this._literal).sort();
795
- for (let letter of letters) {
796
- if (this._literal[letter].isNotZero()) {
797
- L += `${letter}`;
798
- if (this._literal[letter].isNotEqual(1)) {
799
- L += `^{${this._literal[letter].tfrac}}`;
800
- }
801
- }
802
- }
803
- if (L === '') {
804
- // No setLetter - means it's only a number !
805
- if (this._coefficient.value != 0) {
806
- return `${this._coefficient.frac}`;
807
- }
808
- else {
809
- return '0';
810
- }
811
- }
812
- else {
813
- if (this._coefficient.value === 1) {
814
- return L;
815
- }
816
- else if (this._coefficient.value === -1) {
817
- return `-${L}`;
818
- }
819
- else if (this._coefficient.value === 0) {
820
- return '0';
821
- }
822
- else {
823
- return `${this._coefficient.frac}${L}`;
824
- }
825
- }
826
- }
827
- /**
828
- * Determine if the monom is null
829
- */
830
- isZero() {
831
- return this._coefficient.value === 0;
832
- }
833
- /**
834
- * Determine if the monom is one
835
- */
836
- isOne() {
837
- return this._coefficient.value === 1 && this.variables.length === 0;
838
- }
839
- _getLiteralDividers(arr, letter) {
840
- let tmpList = [];
841
- // Be default, this.literal[letter] should be a rational number.
842
- for (let d = 0; d <= this.literal[letter].value; d++) {
843
- if (arr.length === 0) {
844
- let litt = {};
845
- litt[letter] = new fraction_1.Fraction(d);
846
- tmpList.push(litt);
847
- }
848
- else {
849
- for (let item of arr) {
850
- let litt = {};
851
- for (let currentLetter in item) {
852
- litt[currentLetter] = item[currentLetter];
853
- }
854
- litt[letter] = new fraction_1.Fraction(d);
855
- tmpList.push(litt);
856
- }
857
- }
858
- }
859
- return tmpList;
860
- }
861
- }
862
- exports.Monom = Monom;
863
- /**
864
- * Get the least common multiple of monoms
865
- * @param monoms Array of monoms
866
- */
867
- Monom.lcm = (...monoms) => {
868
- // All the monoms must be with natural powers...
869
- for (let m of monoms) {
870
- if (m.hasFractionCoefficient()) {
871
- return new Monom().zero();
872
- }
873
- }
874
- let M = new Monom(), coeffN = monoms.map(value => value.coefficient.numerator), coeffD = monoms.map(value => value.coefficient.denominator), n = numeric_1.Numeric.gcd(...coeffN), d = numeric_1.Numeric.lcm(...coeffD);
875
- // Get the coefficient.
876
- M.coefficient = new fraction_1.Fraction(n, d).reduce();
877
- // Set the literal parts - go through each monoms literal parts and get only the lowest degree of each letters.
878
- for (let m of monoms) {
879
- // Remove the inexistant letters from the resulting monom
880
- for (let letter in M.literal) {
881
- if (!(letter in m.literal)) {
882
- M.literal[letter].zero();
883
- }
884
- }
885
- for (let letter in m.literal) {
886
- if (M.literal[letter] === undefined && m.literal[letter].isStrictlyPositive()) {
887
- M.literal[letter] = m.literal[letter].clone();
888
- }
889
- else {
890
- M.literal[letter] = new fraction_1.Fraction(Math.min(m.literal[letter].value, M.literal[letter].value));
891
- }
892
- }
893
- }
894
- return M;
895
- };
896
- // ------------------------------------------
897
- // Creation / parsing functions
898
- /**
899
- * Multiply two monoms and return a NEW monom.
900
- * @param monoms
901
- */
902
- Monom.xmultiply = (...monoms) => {
903
- let M = new Monom().one();
904
- for (let m of monoms) {
905
- M.multiply(m);
906
- }
907
- return M;
908
- };
909
- //# sourceMappingURL=monom.js.map