pimath 0.0.32 → 0.0.35

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