pimath 0.0.120 → 0.0.121

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 (171) hide show
  1. package/.eslintrc.js +23 -23
  2. package/.idea/PI.iml +7 -1
  3. package/dev/pimath.js +7929 -0
  4. package/dev/pimath.js.map +1 -0
  5. package/dist/{pi.js → pimath.js} +7612 -7840
  6. package/dist/pimath.js.map +1 -0
  7. package/dist/pimath.min.js +2 -0
  8. package/dist/pimath.min.js.map +1 -0
  9. package/docs/assets/main.js +3 -3
  10. package/docs/assets/search.js +1 -1
  11. package/docs/assets/style.css +450 -363
  12. package/docs/classes/Logicalset.Logicalset.html +119 -110
  13. package/docs/classes/Polynom.Rational.html +230 -227
  14. package/docs/classes/Vector-1.Vector.html +319 -273
  15. package/docs/classes/Vector.Point.html +189 -190
  16. package/docs/classes/algebra_equation.Equation.html +489 -446
  17. package/docs/classes/algebra_linearSystem.LinearSystem.html +228 -217
  18. package/docs/classes/algebra_monom.Monom.html +564 -507
  19. package/docs/classes/algebra_polynom.Polynom.html +774 -753
  20. package/docs/classes/coefficients_fraction.Fraction.html +573 -565
  21. package/docs/classes/geometry_circle.Circle.html +299 -299
  22. package/docs/classes/geometry_line.Line.html +511 -451
  23. package/docs/classes/geometry_triangle.Triangle.html +273 -264
  24. package/docs/classes/numeric.Numeric.html +138 -132
  25. package/docs/classes/shutingyard.Shutingyard.html +144 -133
  26. package/docs/enums/algebra_equation.PARTICULAR_SOLUTION.html +47 -46
  27. package/docs/enums/geometry_line.LinePropriety.html +58 -58
  28. package/docs/enums/shutingyard.ShutingyardMode.html +62 -58
  29. package/docs/enums/shutingyard.ShutingyardType.html +74 -70
  30. package/docs/index.html +31 -33
  31. package/docs/interfaces/algebra_equation.ISolution.html +61 -59
  32. package/docs/interfaces/algebra_polynom.IEuclidian.html +47 -46
  33. package/docs/interfaces/geometry_triangle.remarquableLines.html +74 -74
  34. package/docs/modules/Logicalset.html +33 -38
  35. package/docs/modules/Polynom.html +33 -38
  36. package/docs/modules/Vector-1.html +33 -38
  37. package/docs/modules/Vector.html +33 -38
  38. package/docs/modules/algebra_equation.html +35 -41
  39. package/docs/modules/algebra_linearSystem.html +31 -37
  40. package/docs/modules/algebra_monom.html +33 -39
  41. package/docs/modules/algebra_polynom.html +35 -41
  42. package/docs/modules/coefficients_fraction.html +33 -39
  43. package/docs/modules/geometry_circle.html +31 -37
  44. package/docs/modules/geometry_line.html +33 -39
  45. package/docs/modules/geometry_triangle.html +33 -39
  46. package/docs/modules/numeric.html +31 -37
  47. package/docs/modules/shutingyard.html +41 -47
  48. package/docs/types/algebra_monom.literalType.html +33 -37
  49. package/docs/types/algebra_polynom.PolynomParsingType.html +33 -37
  50. package/docs/types/coefficients_fraction.FractionParsingType.html +32 -36
  51. package/docs/types/shutingyard.Token.html +38 -42
  52. package/docs/types/shutingyard.tokenType.html +40 -44
  53. package/docs/variables/shutingyard.tokenConstant.html +37 -41
  54. package/esm/index.d.ts +38 -41
  55. package/esm/index.js +43 -46
  56. package/esm/index.js.map +1 -1
  57. package/esm/maths/algebra/equation.d.ts +119 -117
  58. package/esm/maths/algebra/equation.js +796 -785
  59. package/esm/maths/algebra/equation.js.map +1 -1
  60. package/esm/maths/algebra/linearSystem.d.ts +39 -38
  61. package/esm/maths/algebra/linearSystem.js +278 -262
  62. package/esm/maths/algebra/linearSystem.js.map +1 -1
  63. package/esm/maths/algebra/logicalset.d.ts +28 -28
  64. package/esm/maths/algebra/logicalset.js +157 -157
  65. package/esm/maths/algebra/monom.d.ts +206 -206
  66. package/esm/maths/algebra/monom.js +908 -908
  67. package/esm/maths/algebra/monom.js.map +1 -1
  68. package/esm/maths/algebra/polynom.d.ts +157 -157
  69. package/esm/maths/algebra/polynom.js +1277 -1277
  70. package/esm/maths/algebra/rational.d.ts +45 -45
  71. package/esm/maths/algebra/rational.js +183 -183
  72. package/esm/maths/algebra/study/rationalStudy.d.ts +28 -28
  73. package/esm/maths/algebra/study/rationalStudy.js +243 -243
  74. package/esm/maths/algebra/study.d.ts +142 -142
  75. package/esm/maths/algebra/study.js +377 -377
  76. package/esm/maths/algebra/study.js.map +1 -1
  77. package/esm/maths/coefficients/fraction.d.ts +90 -90
  78. package/esm/maths/coefficients/fraction.js +516 -516
  79. package/esm/maths/coefficients/fraction.js.map +1 -1
  80. package/esm/maths/coefficients/nthRoot.d.ts +23 -23
  81. package/esm/maths/coefficients/nthRoot.js +136 -136
  82. package/esm/maths/geometry/circle.d.ts +45 -45
  83. package/esm/maths/geometry/circle.js +323 -323
  84. package/esm/maths/geometry/line.d.ts +99 -99
  85. package/esm/maths/geometry/line.js +481 -481
  86. package/esm/maths/geometry/line.js.map +1 -1
  87. package/esm/maths/geometry/point.d.ts +34 -34
  88. package/esm/maths/geometry/point.js +166 -166
  89. package/esm/maths/geometry/point.js.map +1 -1
  90. package/esm/maths/geometry/triangle.d.ts +85 -85
  91. package/esm/maths/geometry/triangle.js +268 -268
  92. package/esm/maths/geometry/vector.d.ts +41 -41
  93. package/esm/maths/geometry/vector.js +197 -197
  94. package/esm/maths/geometry/vector.js.map +1 -1
  95. package/esm/maths/numeric.d.ts +28 -28
  96. package/esm/maths/numeric.js +180 -180
  97. package/esm/maths/numexp.d.ts +19 -0
  98. package/esm/maths/numexp.js +186 -0
  99. package/esm/maths/numexp.js.map +1 -0
  100. package/esm/maths/randomization/random.d.ts +23 -23
  101. package/esm/maths/randomization/random.js +78 -78
  102. package/esm/maths/randomization/random.js.map +1 -1
  103. package/esm/maths/randomization/randomCore.d.ts +7 -7
  104. package/esm/maths/randomization/randomCore.js +21 -21
  105. package/esm/maths/randomization/rndFraction.d.ts +12 -12
  106. package/esm/maths/randomization/rndFraction.js +43 -43
  107. package/esm/maths/randomization/rndGeometryLine.d.ts +12 -12
  108. package/esm/maths/randomization/rndGeometryLine.js +45 -45
  109. package/esm/maths/randomization/rndGeometryPoint.d.ts +12 -12
  110. package/esm/maths/randomization/rndGeometryPoint.js +60 -60
  111. package/esm/maths/randomization/rndHelpers.d.ts +23 -23
  112. package/esm/maths/randomization/rndHelpers.js +76 -76
  113. package/esm/maths/randomization/rndMonom.d.ts +12 -12
  114. package/esm/maths/randomization/rndMonom.js +52 -52
  115. package/esm/maths/randomization/rndPolynom.d.ts +13 -13
  116. package/esm/maths/randomization/rndPolynom.js +74 -74
  117. package/esm/maths/randomization/rndTypes.d.ts +34 -34
  118. package/esm/maths/randomization/rndTypes.js +2 -2
  119. package/esm/maths/shutingyard.d.ts +59 -59
  120. package/esm/maths/shutingyard.js +442 -442
  121. package/esm/maths/shutingyard.js.map +1 -1
  122. package/package.json +11 -11
  123. package/public/index.html +50 -81
  124. package/public/playground.html +7 -8
  125. package/src/index.ts +2 -5
  126. package/src/maths/algebra/equation.ts +16 -0
  127. package/src/maths/algebra/linearSystem.ts +20 -0
  128. package/src/maths/algebra/study.ts +1 -1
  129. package/src/maths/{expressions/numexp.ts → numexp.ts} +2 -2
  130. package/tests/algebra/equation.test.ts +19 -5
  131. package/tests/algebra/linear.test.ts +3 -11
  132. package/tests/algebra/polynom.test.ts +7 -8
  133. package/tests/algebra/rationnal.test.ts +1 -1
  134. package/tests/algebra/study.test.ts +2 -9
  135. package/tests/coefficients/fraction.test.ts +8 -8
  136. package/tests/custom.test.ts +33 -37
  137. package/tests/numeric.test.ts +1 -2
  138. package/tests/numexp.test.ts +1 -5
  139. package/tests/shutingyard.test.ts +3 -3
  140. package/webpack-production-min.config.js +1 -1
  141. package/webpack-production.config.js +1 -1
  142. package/webpack.config.js +1 -1
  143. package/dist/pi.js.map +0 -1
  144. package/dist/pi.min.js +0 -2
  145. package/dist/pi.min.js.map +0 -1
  146. package/docs/classes/expressions_numexp.NumExp.html +0 -236
  147. package/docs/classes/expressions_polynomexp.PolynomExpFactor.html +0 -317
  148. package/docs/classes/expressions_polynomexp.PolynomExpProduct.html +0 -285
  149. package/docs/modules/expressions_numexp.html +0 -71
  150. package/docs/modules/expressions_polynomexp.html +0 -73
  151. package/docs/modules.html +0 -76
  152. package/graph.svg +0 -1033
  153. package/src/maths/expressions/ExpressionTree.ts +0 -172
  154. package/src/maths/expressions/expression.ts +0 -286
  155. package/src/maths/expressions/expressionFactor.ts +0 -190
  156. package/src/maths/expressions/expressionMember.ts +0 -233
  157. package/src/maths/expressions/expressionOperators.ts +0 -49
  158. package/src/maths/expressions/expressionParser.ts +0 -295
  159. package/src/maths/expressions/factors/ExpFactor.ts +0 -39
  160. package/src/maths/expressions/factors/ExpFactorConstant.ts +0 -60
  161. package/src/maths/expressions/factors/ExpFactorExponential.ts +0 -26
  162. package/src/maths/expressions/factors/ExpFactorNumber.ts +0 -72
  163. package/src/maths/expressions/factors/ExpFactorPower.ts +0 -42
  164. package/src/maths/expressions/factors/ExpFactorTrigo.ts +0 -53
  165. package/src/maths/expressions/factors/ExpFactorVariable.ts +0 -45
  166. package/src/maths/expressions/internals.ts +0 -14
  167. package/src/maths/expressions/polynomexp.bkp.ts +0 -221
  168. package/src/maths/expressions/polynomexp.ts +0 -310
  169. package/tests/expressions/expressions.test.ts +0 -145
  170. package/tests/expressions/expressiontree.test.ts +0 -11
  171. package/tests/polynomexp.test.ts +0 -12
@@ -1,517 +1,517 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Fraction = void 0;
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
- */
10
- class Fraction {
11
- constructor(value, denominatorOrPeriodic) {
12
- this.isApproximative = () => {
13
- return this._numerator.toString().length >= 15 && this._denominator.toString().length >= 15;
14
- };
15
- this.isExact = () => {
16
- return !this.isApproximative();
17
- };
18
- // ------------------------------------------
19
- /**
20
- * Parse the value to get the numerator and denominator
21
- * @param value : number or string to parse to get the fraction
22
- * @param denominatorOrPeriodic (optional|number) : length of the periodic part: 2.333333 => 1 or denominator value
23
- */
24
- this.parse = (value, denominatorOrPeriodic) => {
25
- let S;
26
- // A null value means a zero fraction.
27
- if (value === null || value === "") {
28
- this._numerator = 0;
29
- this._denominator = 1;
30
- return this;
31
- }
32
- switch (typeof value) {
33
- case "string":
34
- // Split the string value in two parts: Numerator/Denominator
35
- S = value.split('/');
36
- // Security checks
37
- if (S.length > 2)
38
- throw value + " has too many divide signs";
39
- if (S.map(x => x === '' || isNaN(Number(x))).includes(true))
40
- throw value + " is not a valid number";
41
- if (S.length === 1) {
42
- // No divide sign - it's a number
43
- return this.parse(+S[0]);
44
- }
45
- else if (S.length === 2) {
46
- // One divide signe
47
- // We check if the denominator is zero
48
- if (S[1] === '0') {
49
- this._numerator = NaN;
50
- this._denominator = 1;
51
- }
52
- else {
53
- this._numerator = +S[0];
54
- this._denominator = +S[1];
55
- }
56
- }
57
- else {
58
- // More than one divide sign ?
59
- // This is impossible
60
- this._numerator = NaN;
61
- this._denominator = 1;
62
- }
63
- break;
64
- case "number":
65
- if (Number.isSafeInteger(value)) {
66
- // The given value is an integer
67
- this._numerator = +value;
68
- if (denominatorOrPeriodic === undefined || !Number.isSafeInteger(denominatorOrPeriodic)) {
69
- this._denominator = 1;
70
- }
71
- else {
72
- this._denominator = +denominatorOrPeriodic;
73
- }
74
- }
75
- else {
76
- // The given value is a float number
77
- // Get the number of decimals after the float sign
78
- let [unit, decimal] = (value.toString()).split('.');
79
- let p = decimal ? decimal.length : 0;
80
- // Detect if the decimal part is periodic or not...
81
- // Transform the float number in two integer
82
- if (denominatorOrPeriodic === undefined) {
83
- this._numerator = value * Math.pow(10, p);
84
- this._denominator = Math.pow(10, p);
85
- }
86
- else if (Number.isSafeInteger(denominatorOrPeriodic)) {
87
- this._numerator = value * Math.pow(10, p) - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic));
88
- this.denominator = Math.pow(10, p) - Math.pow(10, p - denominatorOrPeriodic);
89
- }
90
- this.reduce();
91
- }
92
- break;
93
- case "object":
94
- if (value instanceof Fraction) {
95
- this._numerator = +value.numerator;
96
- this._denominator = +value.denominator;
97
- }
98
- break;
99
- }
100
- return this;
101
- };
102
- // ------------------------------------------
103
- // Mathematical operations
104
- this.clone = () => {
105
- let F = new Fraction();
106
- F.numerator = +this._numerator;
107
- F.denominator = +this._denominator;
108
- return F;
109
- };
110
- this.zero = () => {
111
- this._numerator = 0;
112
- this._denominator = 1;
113
- return this;
114
- };
115
- this.one = () => {
116
- this._numerator = 1;
117
- this._denominator = 1;
118
- return this;
119
- };
120
- this.infinite = () => {
121
- this._numerator = Infinity;
122
- this._denominator = 1;
123
- return this;
124
- };
125
- this.invalid = () => {
126
- this._numerator = NaN;
127
- this._denominator = 1;
128
- return this;
129
- };
130
- // ------------------------------------------
131
- this.opposed = () => {
132
- this._numerator = -this._numerator;
133
- return this;
134
- };
135
- this.add = (F) => {
136
- if (F instanceof Fraction) {
137
- let N = this._numerator, D = this._denominator;
138
- this._numerator = N * F.denominator + F.numerator * D;
139
- this._denominator = D * F.denominator;
140
- }
141
- else {
142
- return this.add(new Fraction(F));
143
- }
144
- return this.reduce();
145
- };
146
- this.subtract = (F) => {
147
- if (F instanceof Fraction) {
148
- return this.add(F.clone().opposed());
149
- }
150
- else {
151
- return this.add(-F);
152
- }
153
- };
154
- this.multiply = (F) => {
155
- // Parse the value.
156
- // If it's a fraction, return a clone of it
157
- // If it's an integer, return the fraction F/1
158
- let Q = new Fraction(F);
159
- this._numerator = this._numerator * Q.numerator;
160
- this._denominator = this._denominator * Q.denominator;
161
- return this.reduce();
162
- };
163
- this.xMultiply = (...values) => {
164
- // Parse the value.
165
- // If it's a fraction, return a clone of it
166
- // If it's an integer, return the fraction F/1
167
- for (let value of values) {
168
- let F = new Fraction(value);
169
- this._numerator = this._numerator * F.numerator;
170
- this._denominator = this._denominator * F.denominator;
171
- }
172
- return this;
173
- };
174
- this.divide = (F) => {
175
- let Q = new Fraction(F);
176
- if (Q.numerator === 0) {
177
- return new Fraction().infinite();
178
- }
179
- let N = +this._numerator, D = +this._denominator;
180
- this._numerator = N * Q.denominator;
181
- this._denominator = D * Q.numerator;
182
- return this.reduce();
183
- };
184
- this.invert = () => {
185
- let n = +this._numerator, d = +this._denominator;
186
- this._numerator = d;
187
- this._denominator = n;
188
- return this;
189
- };
190
- this.pow = (p) => {
191
- // TODO: Fraction.pow with a value different than a safe integer !
192
- if (p instanceof Fraction) {
193
- return this.pow(p.value);
194
- }
195
- this.reduce();
196
- if (p < 0) {
197
- this.invert();
198
- }
199
- // Check if numerator and denominator are roots of...
200
- // othervise, convert to numeric.
201
- let controlNumerator = Math.floor(Math.pow(this._numerator, Math.abs(p))), controlDenominator = Math.floor(Math.pow(this._denominator, Math.abs(p)));
202
- if (controlNumerator ** Math.abs(p) === this._numerator
203
- &&
204
- controlDenominator ** Math.abs(p) === this._denominator) {
205
- this._numerator = this._numerator ** Math.abs(p);
206
- this._denominator = this._denominator ** Math.abs(p);
207
- }
208
- else {
209
- this._numerator = this._numerator ** Math.abs(p);
210
- this._denominator = this._denominator ** Math.abs(p);
211
- }
212
- return this;
213
- };
214
- this.root = (p) => {
215
- // TODO: nth - root of a fraction => this will return another type of coefficient.
216
- // Check if they are perfect roots..
217
- if (p === 0) {
218
- return this;
219
- }
220
- // If negative, invert the fraction
221
- if (p < 0) {
222
- this.invert();
223
- }
224
- let n = Math.pow(this._numerator, Math.abs(1 / p)), d = Math.pow(this._denominator, Math.abs(1 / p));
225
- this._numerator = Math.pow(this._numerator, Math.abs(1 / p));
226
- this._denominator = Math.pow(this._denominator, Math.abs(1 / p));
227
- return this;
228
- };
229
- this.sqrt = () => {
230
- return this.root(2);
231
- };
232
- this.abs = () => {
233
- this._numerator = Math.abs(this._numerator);
234
- this._denominator = Math.abs(this._denominator);
235
- return this;
236
- };
237
- // ------------------------------------------
238
- // Mathematical operations specific to fractions
239
- // ------------------------------------------
240
- this.reduce = () => {
241
- let g = numeric_1.Numeric.gcd(this._numerator, this._denominator);
242
- this._numerator = this._numerator / g;
243
- this._denominator = this._denominator / g;
244
- if (this._denominator < 0) {
245
- this._denominator = -this._denominator;
246
- this._numerator = -this._numerator;
247
- }
248
- return this;
249
- };
250
- this.amplify = (k) => {
251
- if (Number.isSafeInteger(k)) {
252
- this._numerator *= k;
253
- this._denominator *= k;
254
- }
255
- return this;
256
- };
257
- // ------------------------------------------
258
- // Compare functions
259
- // ------------------------------------------
260
- /**
261
- * Compare the current coefficient with another coefficient
262
- * @param F (Coefficient) The coefficient to compare
263
- * @param sign (string| default is =): authorized values: =, <, <=, >, >= with some variations.
264
- */
265
- this.compare = (F, sign) => {
266
- if (sign === undefined) {
267
- sign = '=';
268
- }
269
- let compareFraction;
270
- if (F instanceof Fraction) {
271
- compareFraction = F.clone();
272
- }
273
- else {
274
- compareFraction = new Fraction(F);
275
- }
276
- switch (sign) {
277
- case '>':
278
- return this.value > compareFraction.value;
279
- case ">=" || "=>" || "geq":
280
- return this.value >= compareFraction.value;
281
- case "<":
282
- return this.value < compareFraction.value;
283
- case "<=" || "=>" || "leq":
284
- return this.value <= compareFraction.value;
285
- case "=":
286
- // let F2: Fraction = compareFraction.clone().reduce(),
287
- // F1: Fraction = this.clone().reduce();
288
- // return (F1.numerator === F2.numerator && F1.denominator === F2.denominator);
289
- return this.value === compareFraction.value;
290
- case "<>":
291
- return this.value !== compareFraction.value;
292
- default:
293
- return false;
294
- }
295
- };
296
- /* Compare shortcuts */
297
- this.lesser = (than) => {
298
- return this.compare(than, '<');
299
- };
300
- this.leq = (than) => {
301
- return this.compare(than, '<=');
302
- };
303
- this.greater = (than) => {
304
- return this.compare(than, '>');
305
- };
306
- this.geq = (than) => {
307
- return this.compare(than, '>=');
308
- };
309
- this.isEqual = (than) => {
310
- return this.compare(than, '=');
311
- };
312
- this.isNotEqual = (than) => {
313
- return this.compare(than, '<>');
314
- };
315
- this.isOpposed = (p) => {
316
- return this.isEqual(p.clone().opposed());
317
- };
318
- this.isInverted = (p) => {
319
- return this.isEqual(new Fraction().one().divide(p.clone()));
320
- };
321
- this.isZero = () => {
322
- return this._numerator === 0;
323
- };
324
- this.isNotZero = () => {
325
- return this._numerator !== 0;
326
- };
327
- this.isOne = () => {
328
- return this._numerator === 1 && this._denominator === 1;
329
- };
330
- this.isNegativeOne = () => {
331
- return this._numerator === -1 && this._denominator === 1;
332
- };
333
- this.isPositive = () => {
334
- return this.sign() === 1;
335
- };
336
- this.isNegative = () => {
337
- return this.sign() === -1;
338
- };
339
- this.isStrictlyPositive = () => {
340
- return this.value > 0;
341
- };
342
- this.isStrictlyNegative = () => {
343
- return this.value < 0;
344
- };
345
- this.isNaN = () => {
346
- return isNaN(this._numerator);
347
- };
348
- this.isInfinity = () => {
349
- return Math.abs(this._numerator) === Infinity;
350
- };
351
- this.isFinite = () => {
352
- return !this.isInfinity() && !this.isNaN();
353
- };
354
- this.isSquare = () => {
355
- return Math.sqrt(this._numerator) % 1 === 0 && Math.sqrt(this._denominator) % 1 === 0;
356
- };
357
- this.isReduced = () => {
358
- return Math.abs(numeric_1.Numeric.gcd(this._numerator, this._denominator)) === 1;
359
- };
360
- this.isNatural = () => {
361
- return this.isRelative() && this.isPositive();
362
- };
363
- this.isRelative = () => {
364
- return this.clone().reduce().denominator === 1;
365
- };
366
- this.isRational = () => {
367
- return !this.isRelative();
368
- };
369
- this.isEven = () => {
370
- return this.isRelative() && this.value % 2 === 0;
371
- };
372
- this.isOdd = () => {
373
- return this.isRelative() && this.value % 2 === 1;
374
- };
375
- this.sign = () => {
376
- return (this._numerator * this._denominator >= 0) ? 1 : -1;
377
- };
378
- // TODO: The rest of the functions are not used or unnecessary ?
379
- /**
380
- * Simple function to determine if it's a fraction
381
- */
382
- this.areEquals = (...F) => {
383
- for (let i = 0; i < F.length; i++) {
384
- if (!this.isEqual(F[i])) {
385
- return false;
386
- }
387
- }
388
- return true;
389
- };
390
- this._numerator = 1;
391
- this._denominator = 1;
392
- if (value !== undefined) {
393
- this.parse(value, denominatorOrPeriodic);
394
- }
395
- return this;
396
- }
397
- // ------------------------------------------
398
- // Getter and setter
399
- // ------------------------------------------
400
- get numerator() {
401
- return this._numerator;
402
- }
403
- set numerator(value) {
404
- this._numerator = value;
405
- }
406
- get denominator() {
407
- return this._denominator;
408
- }
409
- set denominator(value) {
410
- this._denominator = value;
411
- }
412
- get value() {
413
- return this._numerator / this._denominator;
414
- }
415
- // Display getter
416
- get tex() {
417
- if (this.isInfinity()) {
418
- return `${this.sign() === 1 ? '+' : '-'}\\infty`;
419
- }
420
- if (this.isExact()) {
421
- if (this._denominator === 1) {
422
- return `${this._numerator}`;
423
- }
424
- else if (this._numerator < 0) {
425
- return `-\\frac{ ${-this._numerator} }{ ${this._denominator} }`;
426
- }
427
- else {
428
- return `\\frac{ ${this._numerator} }{ ${this._denominator} }`;
429
- }
430
- }
431
- else {
432
- return this.value.toFixed(3);
433
- }
434
- }
435
- get texWithSign() {
436
- return this.isPositive() ? `+${this.tex}` : this.tex;
437
- }
438
- get display() {
439
- if (this.isExact()) {
440
- if (this._denominator === 1) {
441
- return `${this._numerator}`;
442
- }
443
- else {
444
- return `${this._numerator}/${this._denominator}`;
445
- }
446
- }
447
- else {
448
- return this.value.toFixed(3);
449
- }
450
- }
451
- // Helper function to display fractions
452
- get frac() {
453
- return this.tex;
454
- }
455
- get dfrac() {
456
- return this.tex.replace('\\frac', '\\dfrac');
457
- }
458
- get tfrac() {
459
- return this.tex.replace('\\frac', '\\tfrac');
460
- }
461
- }
462
- exports.Fraction = Fraction;
463
- Fraction.max = (...fractions) => {
464
- let M = new Fraction(fractions[0]);
465
- for (let m of fractions) {
466
- let compare = new Fraction(m);
467
- if (compare.greater(M)) {
468
- M = compare.clone();
469
- }
470
- }
471
- return M;
472
- };
473
- Fraction.min = (...fractions) => {
474
- let M = new Fraction(fractions[0]);
475
- for (let m of fractions) {
476
- let compare = new Fraction(m);
477
- if (compare.lesser(M)) {
478
- M = compare.clone();
479
- }
480
- }
481
- return M;
482
- };
483
- // ------------------------------------------
484
- // Creation / parsing functions
485
- Fraction.average = (...fractions) => {
486
- let M = new Fraction().zero();
487
- for (let f of fractions) {
488
- M.add(f);
489
- }
490
- M.divide(fractions.length);
491
- return M;
492
- };
493
- Fraction.unique = (fractions, sorted) => {
494
- // TODO: make sure it's wokring -> test !
495
- let unique = {}, distinct = [];
496
- fractions.forEach(x => {
497
- if (!unique[x.clone().reduce().tex]) {
498
- distinct.push(x.clone());
499
- unique[x.tex] = true;
500
- }
501
- });
502
- if (sorted) {
503
- return Fraction.sort(distinct);
504
- }
505
- else {
506
- return distinct;
507
- }
508
- };
509
- Fraction.sort = (fractions, reverse) => {
510
- // Todo make sure it's the correct order, not reverse -> make a test
511
- let sorted = fractions.sort((a, b) => a.value - b.value);
512
- if (reverse) {
513
- sorted.reverse();
514
- }
515
- return sorted;
516
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Fraction = void 0;
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
+ */
10
+ class Fraction {
11
+ constructor(value, denominatorOrPeriodic) {
12
+ this.isApproximative = () => {
13
+ return this._numerator.toString().length >= 15 && this._denominator.toString().length >= 15;
14
+ };
15
+ this.isExact = () => {
16
+ return !this.isApproximative();
17
+ };
18
+ // ------------------------------------------
19
+ /**
20
+ * Parse the value to get the numerator and denominator
21
+ * @param value : number or string to parse to get the fraction
22
+ * @param denominatorOrPeriodic (optional|number) : length of the periodic part: 2.333333 => 1 or denominator value
23
+ */
24
+ this.parse = (value, denominatorOrPeriodic) => {
25
+ let S;
26
+ // A null value means a zero fraction.
27
+ if (value === null || value === "") {
28
+ this._numerator = 0;
29
+ this._denominator = 1;
30
+ return this;
31
+ }
32
+ switch (typeof value) {
33
+ case "string":
34
+ // Split the string value in two parts: Numerator/Denominator
35
+ S = value.split('/');
36
+ // Security checks
37
+ if (S.length > 2)
38
+ throw value + " has too many divide signs";
39
+ if (S.map(x => x === '' || isNaN(Number(x))).includes(true))
40
+ throw value + " is not a valid number";
41
+ if (S.length === 1) {
42
+ // No divide sign - it's a number
43
+ return this.parse(+S[0]);
44
+ }
45
+ else if (S.length === 2) {
46
+ // One divide signe
47
+ // We check if the denominator is zero
48
+ if (S[1] === '0') {
49
+ this._numerator = NaN;
50
+ this._denominator = 1;
51
+ }
52
+ else {
53
+ this._numerator = +S[0];
54
+ this._denominator = +S[1];
55
+ }
56
+ }
57
+ else {
58
+ // More than one divide sign ?
59
+ // This is impossible
60
+ this._numerator = NaN;
61
+ this._denominator = 1;
62
+ }
63
+ break;
64
+ case "number":
65
+ if (Number.isSafeInteger(value)) {
66
+ // The given value is an integer
67
+ this._numerator = +value;
68
+ if (denominatorOrPeriodic === undefined || !Number.isSafeInteger(denominatorOrPeriodic)) {
69
+ this._denominator = 1;
70
+ }
71
+ else {
72
+ this._denominator = +denominatorOrPeriodic;
73
+ }
74
+ }
75
+ else {
76
+ // The given value is a float number
77
+ // Get the number of decimals after the float sign
78
+ let [unit, decimal] = (value.toString()).split('.');
79
+ let p = decimal ? decimal.length : 0;
80
+ // Detect if the decimal part is periodic or not...
81
+ // Transform the float number in two integer
82
+ if (denominatorOrPeriodic === undefined) {
83
+ this._numerator = value * Math.pow(10, p);
84
+ this._denominator = Math.pow(10, p);
85
+ }
86
+ else if (Number.isSafeInteger(denominatorOrPeriodic)) {
87
+ this._numerator = value * Math.pow(10, p) - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic));
88
+ this.denominator = Math.pow(10, p) - Math.pow(10, p - denominatorOrPeriodic);
89
+ }
90
+ this.reduce();
91
+ }
92
+ break;
93
+ case "object":
94
+ if (value instanceof Fraction) {
95
+ this._numerator = +value.numerator;
96
+ this._denominator = +value.denominator;
97
+ }
98
+ break;
99
+ }
100
+ return this;
101
+ };
102
+ // ------------------------------------------
103
+ // Mathematical operations
104
+ this.clone = () => {
105
+ let F = new Fraction();
106
+ F.numerator = +this._numerator;
107
+ F.denominator = +this._denominator;
108
+ return F;
109
+ };
110
+ this.zero = () => {
111
+ this._numerator = 0;
112
+ this._denominator = 1;
113
+ return this;
114
+ };
115
+ this.one = () => {
116
+ this._numerator = 1;
117
+ this._denominator = 1;
118
+ return this;
119
+ };
120
+ this.infinite = () => {
121
+ this._numerator = Infinity;
122
+ this._denominator = 1;
123
+ return this;
124
+ };
125
+ this.invalid = () => {
126
+ this._numerator = NaN;
127
+ this._denominator = 1;
128
+ return this;
129
+ };
130
+ // ------------------------------------------
131
+ this.opposed = () => {
132
+ this._numerator = -this._numerator;
133
+ return this;
134
+ };
135
+ this.add = (F) => {
136
+ if (F instanceof Fraction) {
137
+ let N = this._numerator, D = this._denominator;
138
+ this._numerator = N * F.denominator + F.numerator * D;
139
+ this._denominator = D * F.denominator;
140
+ }
141
+ else {
142
+ return this.add(new Fraction(F));
143
+ }
144
+ return this.reduce();
145
+ };
146
+ this.subtract = (F) => {
147
+ if (F instanceof Fraction) {
148
+ return this.add(F.clone().opposed());
149
+ }
150
+ else {
151
+ return this.add(-F);
152
+ }
153
+ };
154
+ this.multiply = (F) => {
155
+ // Parse the value.
156
+ // If it's a fraction, return a clone of it
157
+ // If it's an integer, return the fraction F/1
158
+ let Q = new Fraction(F);
159
+ this._numerator = this._numerator * Q.numerator;
160
+ this._denominator = this._denominator * Q.denominator;
161
+ return this.reduce();
162
+ };
163
+ this.xMultiply = (...values) => {
164
+ // Parse the value.
165
+ // If it's a fraction, return a clone of it
166
+ // If it's an integer, return the fraction F/1
167
+ for (let value of values) {
168
+ let F = new Fraction(value);
169
+ this._numerator = this._numerator * F.numerator;
170
+ this._denominator = this._denominator * F.denominator;
171
+ }
172
+ return this;
173
+ };
174
+ this.divide = (F) => {
175
+ let Q = new Fraction(F);
176
+ if (Q.numerator === 0) {
177
+ return new Fraction().infinite();
178
+ }
179
+ let N = +this._numerator, D = +this._denominator;
180
+ this._numerator = N * Q.denominator;
181
+ this._denominator = D * Q.numerator;
182
+ return this.reduce();
183
+ };
184
+ this.invert = () => {
185
+ let n = +this._numerator, d = +this._denominator;
186
+ this._numerator = d;
187
+ this._denominator = n;
188
+ return this;
189
+ };
190
+ this.pow = (p) => {
191
+ // TODO: Fraction.pow with a value different than a safe integer !
192
+ if (p instanceof Fraction) {
193
+ return this.pow(p.value);
194
+ }
195
+ this.reduce();
196
+ if (p < 0) {
197
+ this.invert();
198
+ }
199
+ // Check if numerator and denominator are roots of...
200
+ // othervise, convert to numeric.
201
+ let controlNumerator = Math.floor(Math.pow(this._numerator, Math.abs(p))), controlDenominator = Math.floor(Math.pow(this._denominator, Math.abs(p)));
202
+ if (controlNumerator ** Math.abs(p) === this._numerator
203
+ &&
204
+ controlDenominator ** Math.abs(p) === this._denominator) {
205
+ this._numerator = this._numerator ** Math.abs(p);
206
+ this._denominator = this._denominator ** Math.abs(p);
207
+ }
208
+ else {
209
+ this._numerator = this._numerator ** Math.abs(p);
210
+ this._denominator = this._denominator ** Math.abs(p);
211
+ }
212
+ return this;
213
+ };
214
+ this.root = (p) => {
215
+ // TODO: nth - root of a fraction => this will return another type of coefficient.
216
+ // Check if they are perfect roots..
217
+ if (p === 0) {
218
+ return this;
219
+ }
220
+ // If negative, invert the fraction
221
+ if (p < 0) {
222
+ this.invert();
223
+ }
224
+ let n = Math.pow(this._numerator, Math.abs(1 / p)), d = Math.pow(this._denominator, Math.abs(1 / p));
225
+ this._numerator = Math.pow(this._numerator, Math.abs(1 / p));
226
+ this._denominator = Math.pow(this._denominator, Math.abs(1 / p));
227
+ return this;
228
+ };
229
+ this.sqrt = () => {
230
+ return this.root(2);
231
+ };
232
+ this.abs = () => {
233
+ this._numerator = Math.abs(this._numerator);
234
+ this._denominator = Math.abs(this._denominator);
235
+ return this;
236
+ };
237
+ // ------------------------------------------
238
+ // Mathematical operations specific to fractions
239
+ // ------------------------------------------
240
+ this.reduce = () => {
241
+ let g = numeric_1.Numeric.gcd(this._numerator, this._denominator);
242
+ this._numerator = this._numerator / g;
243
+ this._denominator = this._denominator / g;
244
+ if (this._denominator < 0) {
245
+ this._denominator = -this._denominator;
246
+ this._numerator = -this._numerator;
247
+ }
248
+ return this;
249
+ };
250
+ this.amplify = (k) => {
251
+ if (Number.isSafeInteger(k)) {
252
+ this._numerator *= k;
253
+ this._denominator *= k;
254
+ }
255
+ return this;
256
+ };
257
+ // ------------------------------------------
258
+ // Compare functions
259
+ // ------------------------------------------
260
+ /**
261
+ * Compare the current coefficient with another coefficient
262
+ * @param F (Coefficient) The coefficient to compare
263
+ * @param sign (string| default is =): authorized values: =, <, <=, >, >= with some variations.
264
+ */
265
+ this.compare = (F, sign) => {
266
+ if (sign === undefined) {
267
+ sign = '=';
268
+ }
269
+ let compareFraction;
270
+ if (F instanceof Fraction) {
271
+ compareFraction = F.clone();
272
+ }
273
+ else {
274
+ compareFraction = new Fraction(F);
275
+ }
276
+ switch (sign) {
277
+ case '>':
278
+ return this.value > compareFraction.value;
279
+ case ">=" || "=>" || "geq":
280
+ return this.value >= compareFraction.value;
281
+ case "<":
282
+ return this.value < compareFraction.value;
283
+ case "<=" || "=>" || "leq":
284
+ return this.value <= compareFraction.value;
285
+ case "=":
286
+ // let F2: Fraction = compareFraction.clone().reduce(),
287
+ // F1: Fraction = this.clone().reduce();
288
+ // return (F1.numerator === F2.numerator && F1.denominator === F2.denominator);
289
+ return this.value === compareFraction.value;
290
+ case "<>":
291
+ return this.value !== compareFraction.value;
292
+ default:
293
+ return false;
294
+ }
295
+ };
296
+ /* Compare shortcuts */
297
+ this.lesser = (than) => {
298
+ return this.compare(than, '<');
299
+ };
300
+ this.leq = (than) => {
301
+ return this.compare(than, '<=');
302
+ };
303
+ this.greater = (than) => {
304
+ return this.compare(than, '>');
305
+ };
306
+ this.geq = (than) => {
307
+ return this.compare(than, '>=');
308
+ };
309
+ this.isEqual = (than) => {
310
+ return this.compare(than, '=');
311
+ };
312
+ this.isNotEqual = (than) => {
313
+ return this.compare(than, '<>');
314
+ };
315
+ this.isOpposed = (p) => {
316
+ return this.isEqual(p.clone().opposed());
317
+ };
318
+ this.isInverted = (p) => {
319
+ return this.isEqual(new Fraction().one().divide(p.clone()));
320
+ };
321
+ this.isZero = () => {
322
+ return this._numerator === 0;
323
+ };
324
+ this.isNotZero = () => {
325
+ return this._numerator !== 0;
326
+ };
327
+ this.isOne = () => {
328
+ return this._numerator === 1 && this._denominator === 1;
329
+ };
330
+ this.isNegativeOne = () => {
331
+ return this._numerator === -1 && this._denominator === 1;
332
+ };
333
+ this.isPositive = () => {
334
+ return this.sign() === 1;
335
+ };
336
+ this.isNegative = () => {
337
+ return this.sign() === -1;
338
+ };
339
+ this.isStrictlyPositive = () => {
340
+ return this.value > 0;
341
+ };
342
+ this.isStrictlyNegative = () => {
343
+ return this.value < 0;
344
+ };
345
+ this.isNaN = () => {
346
+ return isNaN(this._numerator);
347
+ };
348
+ this.isInfinity = () => {
349
+ return Math.abs(this._numerator) === Infinity;
350
+ };
351
+ this.isFinite = () => {
352
+ return !this.isInfinity() && !this.isNaN();
353
+ };
354
+ this.isSquare = () => {
355
+ return Math.sqrt(this._numerator) % 1 === 0 && Math.sqrt(this._denominator) % 1 === 0;
356
+ };
357
+ this.isReduced = () => {
358
+ return Math.abs(numeric_1.Numeric.gcd(this._numerator, this._denominator)) === 1;
359
+ };
360
+ this.isNatural = () => {
361
+ return this.isRelative() && this.isPositive();
362
+ };
363
+ this.isRelative = () => {
364
+ return this.clone().reduce().denominator === 1;
365
+ };
366
+ this.isRational = () => {
367
+ return !this.isRelative();
368
+ };
369
+ this.isEven = () => {
370
+ return this.isRelative() && this.value % 2 === 0;
371
+ };
372
+ this.isOdd = () => {
373
+ return this.isRelative() && this.value % 2 === 1;
374
+ };
375
+ this.sign = () => {
376
+ return (this._numerator * this._denominator >= 0) ? 1 : -1;
377
+ };
378
+ // TODO: The rest of the functions are not used or unnecessary ?
379
+ /**
380
+ * Simple function to determine if it's a fraction
381
+ */
382
+ this.areEquals = (...F) => {
383
+ for (let i = 0; i < F.length; i++) {
384
+ if (!this.isEqual(F[i])) {
385
+ return false;
386
+ }
387
+ }
388
+ return true;
389
+ };
390
+ this._numerator = 1;
391
+ this._denominator = 1;
392
+ if (value !== undefined) {
393
+ this.parse(value, denominatorOrPeriodic);
394
+ }
395
+ return this;
396
+ }
397
+ // ------------------------------------------
398
+ // Getter and setter
399
+ // ------------------------------------------
400
+ get numerator() {
401
+ return this._numerator;
402
+ }
403
+ set numerator(value) {
404
+ this._numerator = value;
405
+ }
406
+ get denominator() {
407
+ return this._denominator;
408
+ }
409
+ set denominator(value) {
410
+ this._denominator = value;
411
+ }
412
+ get value() {
413
+ return this._numerator / this._denominator;
414
+ }
415
+ // Display getter
416
+ get tex() {
417
+ if (this.isInfinity()) {
418
+ return `${this.sign() === 1 ? '+' : '-'}\\infty`;
419
+ }
420
+ if (this.isExact()) {
421
+ if (this._denominator === 1) {
422
+ return `${this._numerator}`;
423
+ }
424
+ else if (this._numerator < 0) {
425
+ return `-\\frac{ ${-this._numerator} }{ ${this._denominator} }`;
426
+ }
427
+ else {
428
+ return `\\frac{ ${this._numerator} }{ ${this._denominator} }`;
429
+ }
430
+ }
431
+ else {
432
+ return this.value.toFixed(3);
433
+ }
434
+ }
435
+ get texWithSign() {
436
+ return this.isPositive() ? `+${this.tex}` : this.tex;
437
+ }
438
+ get display() {
439
+ if (this.isExact()) {
440
+ if (this._denominator === 1) {
441
+ return `${this._numerator}`;
442
+ }
443
+ else {
444
+ return `${this._numerator}/${this._denominator}`;
445
+ }
446
+ }
447
+ else {
448
+ return this.value.toFixed(3);
449
+ }
450
+ }
451
+ // Helper function to display fractions
452
+ get frac() {
453
+ return this.tex;
454
+ }
455
+ get dfrac() {
456
+ return this.tex.replace('\\frac', '\\dfrac');
457
+ }
458
+ get tfrac() {
459
+ return this.tex.replace('\\frac', '\\tfrac');
460
+ }
461
+ }
462
+ exports.Fraction = Fraction;
463
+ Fraction.max = (...fractions) => {
464
+ let M = new Fraction(fractions[0]);
465
+ for (let m of fractions) {
466
+ let compare = new Fraction(m);
467
+ if (compare.greater(M)) {
468
+ M = compare.clone();
469
+ }
470
+ }
471
+ return M;
472
+ };
473
+ Fraction.min = (...fractions) => {
474
+ let M = new Fraction(fractions[0]);
475
+ for (let m of fractions) {
476
+ let compare = new Fraction(m);
477
+ if (compare.lesser(M)) {
478
+ M = compare.clone();
479
+ }
480
+ }
481
+ return M;
482
+ };
483
+ // ------------------------------------------
484
+ // Creation / parsing functions
485
+ Fraction.average = (...fractions) => {
486
+ let M = new Fraction().zero();
487
+ for (let f of fractions) {
488
+ M.add(f);
489
+ }
490
+ M.divide(fractions.length);
491
+ return M;
492
+ };
493
+ Fraction.unique = (fractions, sorted) => {
494
+ // TODO: make sure it's wokring -> test !
495
+ let unique = {}, distinct = [];
496
+ fractions.forEach(x => {
497
+ if (!unique[x.clone().reduce().tex]) {
498
+ distinct.push(x.clone());
499
+ unique[x.tex] = true;
500
+ }
501
+ });
502
+ if (sorted) {
503
+ return Fraction.sort(distinct);
504
+ }
505
+ else {
506
+ return distinct;
507
+ }
508
+ };
509
+ Fraction.sort = (fractions, reverse) => {
510
+ // Todo make sure it's the correct order, not reverse -> make a test
511
+ let sorted = fractions.sort((a, b) => a.value - b.value);
512
+ if (reverse) {
513
+ sorted.reverse();
514
+ }
515
+ return sorted;
516
+ };
517
517
  //# sourceMappingURL=fraction.js.map