pimath 0.0.33 → 0.0.36

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 (165) hide show
  1. package/dist/pi.js +6468 -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 +41 -1
  34. package/esm/main.js +34 -20
  35. package/esm/main.js.map +1 -1
  36. package/esm/maths/algebra/equation.d.ts +63 -18
  37. package/esm/maths/algebra/equation.js +599 -503
  38. package/esm/maths/algebra/equation.js.map +1 -1
  39. package/esm/maths/algebra/linearSystem.d.ts +1 -1
  40. package/esm/maths/algebra/linearSystem.js +157 -104
  41. package/esm/maths/algebra/linearSystem.js.map +1 -1
  42. package/esm/maths/algebra/logicalset.d.ts +11 -0
  43. package/esm/maths/algebra/logicalset.js +18 -6
  44. package/esm/maths/algebra/logicalset.js.map +1 -1
  45. package/esm/maths/algebra/monom.d.ts +142 -1
  46. package/esm/maths/algebra/monom.js +633 -405
  47. package/esm/maths/algebra/monom.js.map +1 -1
  48. package/esm/maths/algebra/polynom.d.ts +52 -3
  49. package/esm/maths/algebra/polynom.js +1003 -720
  50. package/esm/maths/algebra/polynom.js.map +1 -1
  51. package/esm/maths/algebra/rational.d.ts +13 -1
  52. package/esm/maths/algebra/rational.js +98 -83
  53. package/esm/maths/algebra/rational.js.map +1 -1
  54. package/esm/maths/coefficients/fraction.d.ts +18 -0
  55. package/esm/maths/coefficients/fraction.js +390 -332
  56. package/esm/maths/coefficients/fraction.js.map +1 -1
  57. package/esm/maths/coefficients/nthroot.d.ts +3 -0
  58. package/esm/maths/coefficients/nthroot.js +48 -33
  59. package/esm/maths/coefficients/nthroot.js.map +1 -1
  60. package/esm/maths/expressions/numexp.js +13 -5
  61. package/esm/maths/expressions/numexp.js.map +1 -1
  62. package/esm/maths/expressions/polynomexp.bkp.d.ts +2 -2
  63. package/esm/maths/expressions/polynomexp.bkp.js +95 -95
  64. package/esm/maths/expressions/polynomexp.bkp.js.map +1 -1
  65. package/esm/maths/expressions/polynomexp.d.ts +2 -2
  66. package/esm/maths/expressions/polynomexp.js +27 -14
  67. package/esm/maths/expressions/polynomexp.js.map +1 -1
  68. package/esm/maths/geometry/circle.d.ts +20 -8
  69. package/esm/maths/geometry/circle.js +148 -50
  70. package/esm/maths/geometry/circle.js.map +1 -1
  71. package/esm/maths/geometry/line.d.ts +11 -4
  72. package/esm/maths/geometry/line.js +251 -193
  73. package/esm/maths/geometry/line.js.map +1 -1
  74. package/esm/maths/geometry/point.d.ts +13 -1
  75. package/esm/maths/geometry/point.js +124 -76
  76. package/esm/maths/geometry/point.js.map +1 -1
  77. package/esm/maths/geometry/triangle.d.ts +23 -1
  78. package/esm/maths/geometry/triangle.js +197 -158
  79. package/esm/maths/geometry/triangle.js.map +1 -1
  80. package/esm/maths/geometry/vector.d.ts +5 -1
  81. package/esm/maths/geometry/vector.js +139 -115
  82. package/esm/maths/geometry/vector.js.map +1 -1
  83. package/esm/maths/numeric.d.ts +17 -0
  84. package/esm/maths/numeric.js +40 -0
  85. package/esm/maths/numeric.js.map +1 -1
  86. package/esm/maths/{random/index.d.ts → randomization/random.d.ts} +3 -2
  87. package/esm/maths/{random/index.js → randomization/random.js} +6 -2
  88. package/esm/maths/randomization/random.js.map +1 -0
  89. package/esm/maths/{random → randomization}/randomCore.d.ts +0 -0
  90. package/esm/maths/randomization/randomCore.js +22 -0
  91. package/esm/maths/randomization/randomCore.js.map +1 -0
  92. package/esm/maths/{random → randomization}/rndFraction.d.ts +4 -1
  93. package/esm/maths/randomization/rndFraction.js +40 -0
  94. package/esm/maths/randomization/rndFraction.js.map +1 -0
  95. package/esm/maths/randomization/rndHelpers.d.ts +25 -0
  96. package/esm/maths/{random → randomization}/rndHelpers.js +20 -0
  97. package/esm/maths/randomization/rndHelpers.js.map +1 -0
  98. package/esm/maths/{random → randomization}/rndMonom.d.ts +4 -1
  99. package/esm/maths/randomization/rndMonom.js +53 -0
  100. package/esm/maths/randomization/rndMonom.js.map +1 -0
  101. package/esm/maths/{random → randomization}/rndPolynom.d.ts +4 -1
  102. package/esm/maths/randomization/rndPolynom.js +75 -0
  103. package/esm/maths/randomization/rndPolynom.js.map +1 -0
  104. package/esm/maths/{random → randomization}/rndTypes.d.ts +0 -0
  105. package/esm/maths/{random → randomization}/rndTypes.js +0 -0
  106. package/esm/maths/randomization/rndTypes.js.map +1 -0
  107. package/esm/maths/shutingyard.d.ts +21 -0
  108. package/esm/maths/shutingyard.js +86 -9
  109. package/esm/maths/shutingyard.js.map +1 -1
  110. package/package.json +2 -2
  111. package/public/index.html +47 -0
  112. package/src/main.ts +17 -15
  113. package/src/maths/algebra/equation.ts +144 -129
  114. package/src/maths/algebra/linearSystem.ts +2 -2
  115. package/src/maths/algebra/monom.ts +1 -1
  116. package/src/maths/algebra/polynom.ts +136 -136
  117. package/src/maths/algebra/rational.ts +1 -1
  118. package/src/maths/expressions/numexp.ts +1 -1
  119. package/src/maths/expressions/polynomexp.bkp.ts +2 -2
  120. package/src/maths/expressions/polynomexp.ts +2 -2
  121. package/src/maths/geometry/circle.ts +172 -77
  122. package/src/maths/geometry/line.ts +4 -3
  123. package/src/maths/geometry/point.ts +26 -3
  124. package/src/maths/geometry/triangle.ts +1 -1
  125. package/src/maths/geometry/vector.ts +1 -1
  126. package/src/maths/numeric.ts +15 -0
  127. package/src/maths/{random/index.ts → randomization/random.ts} +3 -2
  128. package/src/maths/{random → randomization}/randomCore.ts +0 -0
  129. package/src/maths/{random → randomization}/rndFraction.ts +2 -2
  130. package/src/maths/{random → randomization}/rndHelpers.ts +0 -0
  131. package/src/maths/{random → randomization}/rndMonom.ts +2 -2
  132. package/src/maths/{random → randomization}/rndPolynom.ts +3 -3
  133. package/src/maths/{random → randomization}/rndTypes.ts +0 -0
  134. package/tests/algebra/monom.test.ts +1 -1
  135. package/tests/algebra/polynom.test.ts +8 -9
  136. package/tests/geometry/circle.test.ts +33 -0
  137. package/tsconfig.json +2 -2
  138. package/webpack-production-min.config.js +26 -0
  139. package/webpack-production.config.js +1 -1
  140. package/dev/pi.js +0 -5392
  141. package/dev/pi.js.map +0 -1
  142. package/esm/maths/algebra/index.d.ts +0 -7
  143. package/esm/maths/algebra/index.js +0 -20
  144. package/esm/maths/algebra/index.js.map +0 -1
  145. package/esm/maths/coefficients/index.d.ts +0 -2
  146. package/esm/maths/coefficients/index.js +0 -15
  147. package/esm/maths/coefficients/index.js.map +0 -1
  148. package/esm/maths/geometry/index.d.ts +0 -5
  149. package/esm/maths/geometry/index.js +0 -18
  150. package/esm/maths/geometry/index.js.map +0 -1
  151. package/esm/maths/random/index.js.map +0 -1
  152. package/esm/maths/random/randomCore.js +0 -22
  153. package/esm/maths/random/randomCore.js.map +0 -1
  154. package/esm/maths/random/rndFraction.js +0 -37
  155. package/esm/maths/random/rndFraction.js.map +0 -1
  156. package/esm/maths/random/rndHelpers.d.ts +0 -8
  157. package/esm/maths/random/rndHelpers.js.map +0 -1
  158. package/esm/maths/random/rndMonom.js +0 -46
  159. package/esm/maths/random/rndMonom.js.map +0 -1
  160. package/esm/maths/random/rndPolynom.js +0 -63
  161. package/esm/maths/random/rndPolynom.js.map +0 -1
  162. package/esm/maths/random/rndTypes.js.map +0 -1
  163. package/src/maths/algebra/index.ts +0 -7
  164. package/src/maths/coefficients/index.ts +0 -2
  165. package/src/maths/geometry/index.ts +0 -5
@@ -2,10 +2,373 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Fraction = void 0;
4
4
  const numeric_1 = require("../numeric");
5
+ /**
6
+ * The fraction class make possible to handle
7
+ * TODO: Write the documentation correctly.
8
+ * \\(\frac{a}{b}\\) or \\[\frac{a}{b}\\] values.
9
+ */
5
10
  class Fraction {
6
- _numerator;
7
- _denominator;
8
11
  constructor(value, denominatorOrPeriodic) {
12
+ // ------------------------------------------
13
+ // Creation / parsing functions
14
+ // ------------------------------------------
15
+ /**
16
+ * Parse the value to get the numerator and denominator
17
+ * @param value : number or string to parse to get the fraction
18
+ * @param denominatorOrPeriodic (optional|number) : length of the periodic part: 2.333333 => 1 or denominator value
19
+ */
20
+ this.parse = (value, denominatorOrPeriodic) => {
21
+ let S;
22
+ // A null value means a zero fraction.
23
+ if (value === null || value === "") {
24
+ this._numerator = 0;
25
+ this._denominator = 1;
26
+ return this;
27
+ }
28
+ switch (typeof value) {
29
+ case "string":
30
+ // Split the sting value in two parts: Numerator/Denominator
31
+ S = value.split('/');
32
+ // Security checks
33
+ if (S.length > 2)
34
+ throw "Two many divide signs";
35
+ if (S.map(x => x === '' || isNaN(Number(x))).includes(true))
36
+ throw "Not a number";
37
+ if (S.length === 1) {
38
+ // No divide sign
39
+ return this.parse(+S[0]);
40
+ }
41
+ else if (S.length === 2) {
42
+ // One divide signe
43
+ // We check if the denominator is zero
44
+ if (S[1] === '0') {
45
+ this._numerator = NaN;
46
+ this._denominator = 1;
47
+ }
48
+ else {
49
+ this._numerator = +S[0];
50
+ this._denominator = +S[1];
51
+ }
52
+ }
53
+ else {
54
+ // More than one divide sign ?
55
+ this._numerator = NaN;
56
+ this._denominator = 1;
57
+ }
58
+ break;
59
+ case "number":
60
+ if (Number.isSafeInteger(value)) {
61
+ // The given value is an integer
62
+ this._numerator = +value;
63
+ if (denominatorOrPeriodic === undefined || !Number.isSafeInteger(denominatorOrPeriodic)) {
64
+ this._denominator = 1;
65
+ }
66
+ else {
67
+ this._denominator = +denominatorOrPeriodic;
68
+ }
69
+ }
70
+ else {
71
+ // The given value is a float number
72
+ // Get the number of decimals after the float sign
73
+ let p = (value.toString()).split('.')[1].length;
74
+ // Transform the float number in two integer
75
+ if (denominatorOrPeriodic === undefined) {
76
+ this._numerator = value * Math.pow(10, p);
77
+ this._denominator = Math.pow(10, p);
78
+ }
79
+ else if (Number.isSafeInteger(denominatorOrPeriodic)) {
80
+ this._numerator = value * Math.pow(10, p) - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic));
81
+ this.denominator = Math.pow(10, p) - Math.pow(10, p - denominatorOrPeriodic);
82
+ }
83
+ this.reduce();
84
+ }
85
+ break;
86
+ case "object":
87
+ if (value instanceof Fraction) {
88
+ this._numerator = +value.numerator;
89
+ this._denominator = +value.denominator;
90
+ }
91
+ break;
92
+ }
93
+ return this;
94
+ };
95
+ this.clone = () => {
96
+ let F = new Fraction();
97
+ F.numerator = +this._numerator;
98
+ F.denominator = +this._denominator;
99
+ return F;
100
+ };
101
+ this.zero = () => {
102
+ this._numerator = 0;
103
+ this._denominator = 1;
104
+ return this;
105
+ };
106
+ this.one = () => {
107
+ this._numerator = 1;
108
+ this._denominator = 1;
109
+ return this;
110
+ };
111
+ this.infinite = () => {
112
+ this._numerator = Infinity;
113
+ this._denominator = 1;
114
+ return this;
115
+ };
116
+ this.invalid = () => {
117
+ this._numerator = NaN;
118
+ this._denominator = 1;
119
+ return this;
120
+ };
121
+ // ------------------------------------------
122
+ // Mathematical operations
123
+ // ------------------------------------------
124
+ this.opposed = () => {
125
+ this._numerator = -this._numerator;
126
+ return this;
127
+ };
128
+ this.add = (F) => {
129
+ if (F instanceof Fraction) {
130
+ let N = this._numerator, D = this._denominator;
131
+ this._numerator = N * F.denominator + F.numerator * D;
132
+ this._denominator = D * F.denominator;
133
+ }
134
+ else {
135
+ return this.add(new Fraction(F));
136
+ }
137
+ return this.reduce();
138
+ };
139
+ this.subtract = (F) => {
140
+ if (F instanceof Fraction) {
141
+ return this.add(F.clone().opposed());
142
+ }
143
+ else {
144
+ return this.add(-F);
145
+ }
146
+ };
147
+ this.multiply = (F) => {
148
+ // Parse the value.
149
+ // If it's a fraction, return a clone of it
150
+ // If it's an integer, return the fraction F/1
151
+ let Q = new Fraction(F);
152
+ this._numerator = this._numerator * Q.numerator;
153
+ this._denominator = this._denominator * Q.denominator;
154
+ return this.reduce();
155
+ };
156
+ this.divide = (F) => {
157
+ let Q = new Fraction(F);
158
+ if (Q.numerator === 0) {
159
+ return new Fraction().infinite();
160
+ }
161
+ let N = +this._numerator, D = +this._denominator;
162
+ this._numerator = N * Q.denominator;
163
+ this._denominator = D * Q.numerator;
164
+ return this.reduce();
165
+ };
166
+ this.invert = () => {
167
+ let n = +this._numerator, d = +this._denominator;
168
+ this._numerator = d;
169
+ this._denominator = n;
170
+ return this;
171
+ };
172
+ this.pow = (p) => {
173
+ // TODO: Fraction.pow with a value different than a safe integer !
174
+ if (p instanceof Fraction) {
175
+ return this.pow(p.value);
176
+ }
177
+ this.reduce();
178
+ if (p < 0) {
179
+ this.invert();
180
+ }
181
+ // Check if numerator and denominator are roots of...
182
+ // othervise, convert to numeric.
183
+ let controlNumerator = Math.floor(Math.pow(this._numerator, Math.abs(p))), controlDenominator = Math.floor(Math.pow(this._denominator, Math.abs(p)));
184
+ if (controlNumerator ** Math.abs(p) === this._numerator
185
+ &&
186
+ controlDenominator ** Math.abs(p) === this._denominator) {
187
+ this._numerator = this._numerator ** Math.abs(p);
188
+ this._denominator = this._denominator ** Math.abs(p);
189
+ }
190
+ else {
191
+ this._numerator = this._numerator ** Math.abs(p);
192
+ this._denominator = this._denominator ** Math.abs(p);
193
+ }
194
+ return this;
195
+ };
196
+ this.root = (p) => {
197
+ // TODO: nth - root of a fraction => this will return another type of coefficient.
198
+ // Check if they are perfect roots..
199
+ if (p === 0) {
200
+ return this;
201
+ }
202
+ // If negative, invert the fraction
203
+ if (p < 0) {
204
+ this.invert();
205
+ }
206
+ let n = Math.pow(this._numerator, Math.abs(1 / p)), d = Math.pow(this._denominator, Math.abs(1 / p));
207
+ this._numerator = Math.pow(this._numerator, Math.abs(1 / p));
208
+ this._denominator = Math.pow(this._denominator, Math.abs(1 / p));
209
+ return this;
210
+ };
211
+ this.sqrt = () => {
212
+ return this.root(2);
213
+ };
214
+ this.abs = () => {
215
+ this._numerator = Math.abs(this._numerator);
216
+ this._denominator = Math.abs(this._denominator);
217
+ return this;
218
+ };
219
+ // ------------------------------------------
220
+ // Mathematical operations specific to fractions
221
+ // ------------------------------------------
222
+ this.reduce = () => {
223
+ let g = numeric_1.Numeric.gcd(this._numerator, this._denominator);
224
+ this._numerator = this._numerator / g;
225
+ this._denominator = this._denominator / g;
226
+ if (this._denominator < 0) {
227
+ this._denominator = -this._denominator;
228
+ this._numerator = -this._numerator;
229
+ }
230
+ return this;
231
+ };
232
+ this.amplify = (k) => {
233
+ if (Number.isSafeInteger(k)) {
234
+ this._numerator *= k;
235
+ this._denominator *= k;
236
+ }
237
+ return this;
238
+ };
239
+ // ------------------------------------------
240
+ // Compare functions
241
+ // ------------------------------------------
242
+ /**
243
+ * Compare the current coefficient with another coefficient
244
+ * @param F (Coefficient) The coefficient to compare
245
+ * @param sign (string| default is =): authorized values: =, <, <=, >, >= with some variations.
246
+ */
247
+ this.compare = (F, sign) => {
248
+ if (sign === undefined) {
249
+ sign = '=';
250
+ }
251
+ let compareFraction;
252
+ if (F instanceof Fraction) {
253
+ compareFraction = F.clone();
254
+ }
255
+ else {
256
+ compareFraction = new Fraction(F);
257
+ }
258
+ switch (sign) {
259
+ case '>':
260
+ return this.value > compareFraction.value;
261
+ case ">=" || "=>" || "geq":
262
+ return this.value >= compareFraction.value;
263
+ case "<":
264
+ return this.value < compareFraction.value;
265
+ case "<=" || "=>" || "leq":
266
+ return this.value <= compareFraction.value;
267
+ case "=":
268
+ // let F2: Fraction = compareFraction.clone().reduce(),
269
+ // F1: Fraction = this.clone().reduce();
270
+ // return (F1.numerator === F2.numerator && F1.denominator === F2.denominator);
271
+ return this.value === compareFraction.value;
272
+ case "<>":
273
+ return this.value !== compareFraction.value;
274
+ default:
275
+ return false;
276
+ }
277
+ };
278
+ /* Compare shortcuts */
279
+ this.lesser = (than) => {
280
+ return this.compare(than, '<');
281
+ };
282
+ this.leq = (than) => {
283
+ return this.compare(than, '<=');
284
+ };
285
+ this.greater = (than) => {
286
+ return this.compare(than, '>');
287
+ };
288
+ this.geq = (than) => {
289
+ return this.compare(than, '>=');
290
+ };
291
+ this.isEqual = (than) => {
292
+ return this.compare(than, '=');
293
+ };
294
+ this.isNotEqual = (than) => {
295
+ return this.compare(than, '<>');
296
+ };
297
+ this.isOpposed = (p) => {
298
+ return this.isEqual(p.clone().opposed());
299
+ };
300
+ this.isInverted = (p) => {
301
+ return this.isEqual(new Fraction().one().divide(p.clone()));
302
+ };
303
+ this.isZero = () => {
304
+ return this._numerator === 0;
305
+ };
306
+ this.isNotZero = () => {
307
+ return this._numerator !== 0;
308
+ };
309
+ this.isOne = () => {
310
+ return this._numerator === 1 && this._denominator === 1;
311
+ };
312
+ this.isNegativeOne = () => {
313
+ return this._numerator === -1 && this._denominator === 1;
314
+ };
315
+ this.isPositive = () => {
316
+ return this.sign() === 1;
317
+ };
318
+ this.isNegative = () => {
319
+ return this.sign() === -1;
320
+ };
321
+ this.isStrictlyPositive = () => {
322
+ return this.value > 0;
323
+ };
324
+ this.isStrictlyNegative = () => {
325
+ return this.value < 0;
326
+ };
327
+ this.isNaN = () => {
328
+ return isNaN(this._numerator);
329
+ };
330
+ this.isInfinity = () => {
331
+ return this._numerator === Infinity;
332
+ };
333
+ this.isFinite = () => {
334
+ return !this.isInfinity();
335
+ };
336
+ this.isSquare = () => {
337
+ return Math.sqrt(this._numerator) % 1 === 0 && Math.sqrt(this._denominator) % 1 === 0;
338
+ };
339
+ this.isReduced = () => {
340
+ return Math.abs(numeric_1.Numeric.gcd(this._numerator, this._denominator)) === 1;
341
+ };
342
+ this.isNatural = () => {
343
+ return this.isRelative() && this.isPositive();
344
+ };
345
+ this.isRelative = () => {
346
+ return this.clone().reduce().denominator === 1;
347
+ };
348
+ this.isRational = () => {
349
+ return !this.isRelative();
350
+ };
351
+ this.isEven = () => {
352
+ return this.isRelative() && this.value % 2 === 0;
353
+ };
354
+ this.isOdd = () => {
355
+ return this.isRelative() && this.value % 2 === 1;
356
+ };
357
+ this.sign = () => {
358
+ return (this._numerator * this._denominator >= 0) ? 1 : -1;
359
+ };
360
+ // TODO: The rest of the functions are not used or unnecessary ?
361
+ /**
362
+ * Simple function to determine if it's a fraction
363
+ */
364
+ this.areEquals = (...F) => {
365
+ for (let i = 0; i < F.length; i++) {
366
+ if (!this.isEqual(F[i])) {
367
+ return false;
368
+ }
369
+ }
370
+ return true;
371
+ };
9
372
  this._numerator = 1;
10
373
  this._denominator = 1;
11
374
  if (value !== undefined) {
@@ -16,6 +379,9 @@ class Fraction {
16
379
  get isFraction() {
17
380
  return true;
18
381
  }
382
+ // ------------------------------------------
383
+ // Getter and setter
384
+ // ------------------------------------------
19
385
  get numerator() {
20
386
  return this._numerator;
21
387
  }
@@ -31,6 +397,7 @@ class Fraction {
31
397
  get value() {
32
398
  return this._numerator / this._denominator;
33
399
  }
400
+ // Display getter
34
401
  get tex() {
35
402
  if (this._denominator === 1) {
36
403
  return `${this._numerator}`;
@@ -50,6 +417,7 @@ class Fraction {
50
417
  return `${this._numerator}/${this._denominator}`;
51
418
  }
52
419
  }
420
+ // Helper function to display fractions
53
421
  get frac() {
54
422
  return this.tex;
55
423
  }
@@ -59,336 +427,26 @@ class Fraction {
59
427
  get tfrac() {
60
428
  return this.tex.replace('\\frac', '\\tfrac');
61
429
  }
62
- parse = (value, denominatorOrPeriodic) => {
63
- let S;
64
- if (value === null || value === "") {
65
- this._numerator = 0;
66
- this._denominator = 1;
67
- return this;
68
- }
69
- switch (typeof value) {
70
- case "string":
71
- S = value.split('/');
72
- if (S.length > 2)
73
- throw "Two many divide signs";
74
- if (S.map(x => x === '' || isNaN(Number(x))).includes(true))
75
- throw "Not a number";
76
- if (S.length === 1) {
77
- return this.parse(+S[0]);
78
- }
79
- else if (S.length === 2) {
80
- if (S[1] === '0') {
81
- this._numerator = NaN;
82
- this._denominator = 1;
83
- }
84
- else {
85
- this._numerator = +S[0];
86
- this._denominator = +S[1];
87
- }
88
- }
89
- else {
90
- this._numerator = NaN;
91
- this._denominator = 1;
92
- }
93
- break;
94
- case "number":
95
- if (Number.isSafeInteger(value)) {
96
- this._numerator = +value;
97
- if (denominatorOrPeriodic === undefined || !Number.isSafeInteger(denominatorOrPeriodic)) {
98
- this._denominator = 1;
99
- }
100
- else {
101
- this._denominator = +denominatorOrPeriodic;
102
- }
103
- }
104
- else {
105
- let p = (value.toString()).split('.')[1].length;
106
- if (denominatorOrPeriodic === undefined) {
107
- this._numerator = value * Math.pow(10, p);
108
- this._denominator = Math.pow(10, p);
109
- }
110
- else if (Number.isSafeInteger(denominatorOrPeriodic)) {
111
- this._numerator = value * Math.pow(10, p) - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic));
112
- this.denominator = Math.pow(10, p) - Math.pow(10, p - denominatorOrPeriodic);
113
- }
114
- this.reduce();
115
- }
116
- break;
117
- case "object":
118
- if (value instanceof Fraction) {
119
- this._numerator = +value.numerator;
120
- this._denominator = +value.denominator;
121
- }
122
- break;
123
- }
124
- return this;
125
- };
126
- clone = () => {
127
- let F = new Fraction();
128
- F.numerator = +this._numerator;
129
- F.denominator = +this._denominator;
130
- return F;
131
- };
132
- zero = () => {
133
- this._numerator = 0;
134
- this._denominator = 1;
135
- return this;
136
- };
137
- one = () => {
138
- this._numerator = 1;
139
- this._denominator = 1;
140
- return this;
141
- };
142
- infinite = () => {
143
- this._numerator = Infinity;
144
- this._denominator = 1;
145
- return this;
146
- };
147
- invalid = () => {
148
- this._numerator = NaN;
149
- this._denominator = 1;
150
- return this;
151
- };
152
- opposed = () => {
153
- this._numerator = -this._numerator;
154
- return this;
155
- };
156
- add = (F) => {
157
- if (F instanceof Fraction) {
158
- let N = this._numerator, D = this._denominator;
159
- this._numerator = N * F.denominator + F.numerator * D;
160
- this._denominator = D * F.denominator;
161
- }
162
- else {
163
- return this.add(new Fraction(F));
164
- }
165
- return this.reduce();
166
- };
167
- subtract = (F) => {
168
- if (F instanceof Fraction) {
169
- return this.add(F.clone().opposed());
170
- }
171
- else {
172
- return this.add(-F);
173
- }
174
- };
175
- multiply = (F) => {
176
- let Q = new Fraction(F);
177
- this._numerator = this._numerator * Q.numerator;
178
- this._denominator = this._denominator * Q.denominator;
179
- return this.reduce();
180
- };
181
- divide = (F) => {
182
- let Q = new Fraction(F);
183
- if (Q.numerator === 0) {
184
- return new Fraction().infinite();
185
- }
186
- let N = +this._numerator, D = +this._denominator;
187
- this._numerator = N * Q.denominator;
188
- this._denominator = D * Q.numerator;
189
- return this.reduce();
190
- };
191
- invert = () => {
192
- let n = +this._numerator, d = +this._denominator;
193
- this._numerator = d;
194
- this._denominator = n;
195
- return this;
196
- };
197
- pow = (p) => {
198
- if (p instanceof Fraction) {
199
- return this.pow(p.value);
200
- }
201
- this.reduce();
202
- if (p < 0) {
203
- this.invert();
204
- }
205
- let controlNumerator = Math.floor(Math.pow(this._numerator, Math.abs(p))), controlDenominator = Math.floor(Math.pow(this._denominator, Math.abs(p)));
206
- if (controlNumerator ** Math.abs(p) === this._numerator
207
- &&
208
- controlDenominator ** Math.abs(p) === this._denominator) {
209
- this._numerator = this._numerator ** Math.abs(p);
210
- this._denominator = this._denominator ** Math.abs(p);
211
- }
212
- else {
213
- this._numerator = this._numerator ** Math.abs(p);
214
- this._denominator = this._denominator ** Math.abs(p);
215
- }
216
- return this;
217
- };
218
- root = (p) => {
219
- if (p === 0) {
220
- return this;
221
- }
222
- if (p < 0) {
223
- this.invert();
224
- }
225
- let n = Math.pow(this._numerator, Math.abs(1 / p)), d = Math.pow(this._denominator, Math.abs(1 / p));
226
- this._numerator = Math.pow(this._numerator, Math.abs(1 / p));
227
- this._denominator = Math.pow(this._denominator, Math.abs(1 / p));
228
- return this;
229
- };
230
- sqrt = () => {
231
- return this.root(2);
232
- };
233
- abs = () => {
234
- this._numerator = Math.abs(this._numerator);
235
- this._denominator = Math.abs(this._denominator);
236
- return this;
237
- };
238
- static max = (...fractions) => {
239
- let M = new Fraction(fractions[0]);
240
- for (let m of fractions) {
241
- let compare = new Fraction(m);
242
- if (compare.greater(M)) {
243
- M = compare.clone();
244
- }
245
- }
246
- return M;
247
- };
248
- static min = (...fractions) => {
249
- let M = new Fraction(fractions[0]);
250
- for (let m of fractions) {
251
- let compare = new Fraction(m);
252
- if (compare.lesser(M)) {
253
- M = compare.clone();
254
- }
255
- }
256
- return M;
257
- };
258
- reduce = () => {
259
- let g = numeric_1.Numeric.gcd(this._numerator, this._denominator);
260
- this._numerator = this._numerator / g;
261
- this._denominator = this._denominator / g;
262
- if (this._denominator < 0) {
263
- this._denominator = -this._denominator;
264
- this._numerator = -this._numerator;
265
- }
266
- return this;
267
- };
268
- amplify = (k) => {
269
- if (Number.isSafeInteger(k)) {
270
- this._numerator *= k;
271
- this._denominator *= k;
272
- }
273
- return this;
274
- };
275
- compare = (F, sign) => {
276
- if (sign === undefined) {
277
- sign = '=';
278
- }
279
- let compareFraction;
280
- if (F instanceof Fraction) {
281
- compareFraction = F.clone();
282
- }
283
- else {
284
- compareFraction = new Fraction(F);
285
- }
286
- switch (sign) {
287
- case '>':
288
- return this.value > compareFraction.value;
289
- case ">=" || "=>" || "geq":
290
- return this.value >= compareFraction.value;
291
- case "<":
292
- return this.value < compareFraction.value;
293
- case "<=" || "=>" || "leq":
294
- return this.value <= compareFraction.value;
295
- case "=":
296
- return this.value === compareFraction.value;
297
- case "<>":
298
- return this.value !== compareFraction.value;
299
- default:
300
- return false;
301
- }
302
- };
303
- lesser = (than) => {
304
- return this.compare(than, '<');
305
- };
306
- leq = (than) => {
307
- return this.compare(than, '<=');
308
- };
309
- greater = (than) => {
310
- return this.compare(than, '>');
311
- };
312
- geq = (than) => {
313
- return this.compare(than, '>=');
314
- };
315
- isEqual = (than) => {
316
- return this.compare(than, '=');
317
- };
318
- isNotEqual = (than) => {
319
- return this.compare(than, '<>');
320
- };
321
- isOpposed = (p) => {
322
- return this.isEqual(p.clone().opposed());
323
- };
324
- isInverted = (p) => {
325
- return this.isEqual(new Fraction().one().divide(p.clone()));
326
- };
327
- isZero = () => {
328
- return this._numerator === 0;
329
- };
330
- isNotZero = () => {
331
- return this._numerator !== 0;
332
- };
333
- isOne = () => {
334
- return this._numerator === 1 && this._denominator === 1;
335
- };
336
- isNegativeOne = () => {
337
- return this._numerator === -1 && this._denominator === 1;
338
- };
339
- isPositive = () => {
340
- return this.sign() === 1;
341
- };
342
- isNegative = () => {
343
- return this.sign() === -1;
344
- };
345
- isStrictlyPositive = () => {
346
- return this.value > 0;
347
- };
348
- isStrictlyNegative = () => {
349
- return this.value < 0;
350
- };
351
- isNaN = () => {
352
- return isNaN(this._numerator);
353
- };
354
- isInfinity = () => {
355
- return this._numerator === Infinity;
356
- };
357
- isFinite = () => {
358
- return !this.isInfinity();
359
- };
360
- isSquare = () => {
361
- return Math.sqrt(this._numerator) % 1 === 0 && Math.sqrt(this._denominator) % 1 === 0;
362
- };
363
- isReduced = () => {
364
- return Math.abs(numeric_1.Numeric.gcd(this._numerator, this._denominator)) === 1;
365
- };
366
- isNatural = () => {
367
- return this.isRelative() && this.isPositive();
368
- };
369
- isRelative = () => {
370
- return this.clone().reduce().denominator === 1;
371
- };
372
- isRational = () => {
373
- return !this.isRelative();
374
- };
375
- isEven = () => {
376
- return this.isRelative() && this.value % 2 === 0;
377
- };
378
- isOdd = () => {
379
- return this.isRelative() && this.value % 2 === 1;
380
- };
381
- sign = () => {
382
- return (this._numerator * this._denominator >= 0) ? 1 : -1;
383
- };
384
- areEquals = (...F) => {
385
- for (let i = 0; i < F.length; i++) {
386
- if (!this.isEqual(F[i])) {
387
- return false;
388
- }
389
- }
390
- return true;
391
- };
392
430
  }
393
431
  exports.Fraction = Fraction;
432
+ Fraction.max = (...fractions) => {
433
+ let M = new Fraction(fractions[0]);
434
+ for (let m of fractions) {
435
+ let compare = new Fraction(m);
436
+ if (compare.greater(M)) {
437
+ M = compare.clone();
438
+ }
439
+ }
440
+ return M;
441
+ };
442
+ Fraction.min = (...fractions) => {
443
+ let M = new Fraction(fractions[0]);
444
+ for (let m of fractions) {
445
+ let compare = new Fraction(m);
446
+ if (compare.lesser(M)) {
447
+ M = compare.clone();
448
+ }
449
+ }
450
+ return M;
451
+ };
394
452
  //# sourceMappingURL=fraction.js.map