pimath 0.0.127 → 0.0.129

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