pimath 0.0.131 → 0.0.133

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