pimath 0.0.132 → 0.0.134
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.
- package/dist/pimath.js +2444 -3566
- package/package.json +37 -20
- package/types/algebra/equation.d.ts +109 -0
- package/types/algebra/equation.d.ts.map +1 -0
- package/types/algebra/equationSolver.d.ts +16 -0
- package/types/algebra/equationSolver.d.ts.map +1 -0
- package/types/algebra/factor.d.ts +47 -0
- package/types/algebra/factor.d.ts.map +1 -0
- package/{dist/maths → types}/algebra/monom.d.ts +98 -131
- package/types/algebra/monom.d.ts.map +1 -0
- package/types/algebra/polyFactor.d.ts +41 -0
- package/types/algebra/polyFactor.d.ts.map +1 -0
- package/types/algebra/polynom.d.ts +126 -0
- package/types/algebra/polynom.d.ts.map +1 -0
- package/types/algebra/rational.d.ts +41 -0
- package/types/algebra/rational.d.ts.map +1 -0
- package/{dist/maths → types}/coefficients/fraction.d.ts +69 -66
- package/types/coefficients/fraction.d.ts.map +1 -0
- package/{dist/maths → types}/coefficients/nthRoot.d.ts +3 -3
- package/{dist/maths → types}/geometry/circle.d.ts +10 -15
- package/types/geometry/circle.d.ts.map +1 -0
- package/types/geometry/geomMath.d.ts +10 -0
- package/types/geometry/geomMath.d.ts.map +1 -0
- package/{dist/maths → types}/geometry/line.d.ts +25 -35
- package/types/geometry/line.d.ts.map +1 -0
- package/types/geometry/line3.d.ts +50 -0
- package/types/geometry/line3.d.ts.map +1 -0
- package/types/geometry/plane3.d.ts +29 -0
- package/types/geometry/plane3.d.ts.map +1 -0
- package/types/geometry/vector.d.ts +70 -0
- package/types/geometry/vector.d.ts.map +1 -0
- package/types/geometry/vector3d.d.ts +37 -0
- package/types/geometry/vector3d.d.ts.map +1 -0
- package/types/index.d.ts +57 -0
- package/types/index.d.ts.map +1 -0
- package/types/numeric.d.ts +39 -0
- package/types/pimath.interface.d.ts +57 -0
- package/types/pimath.interface.d.ts.map +1 -0
- package/types/randomization/algebra/rndEquation.d.ts +4 -0
- package/types/randomization/algebra/rndEquation.d.ts.map +1 -0
- package/types/randomization/algebra/rndMonom.d.ts +4 -0
- package/types/randomization/algebra/rndMonom.d.ts.map +1 -0
- package/types/randomization/algebra/rndPolynom.d.ts +5 -0
- package/types/randomization/algebra/rndPolynom.d.ts.map +1 -0
- package/types/randomization/coefficient/rndFraction.d.ts +4 -0
- package/types/randomization/coefficient/rndFraction.d.ts.map +1 -0
- package/types/randomization/geometry/rndCircle.d.ts +4 -0
- package/types/randomization/geometry/rndCircle.d.ts.map +1 -0
- package/types/randomization/geometry/rndLine.d.ts +4 -0
- package/types/randomization/geometry/rndLine.d.ts.map +1 -0
- package/types/randomization/geometry/rndLine3.d.ts +4 -0
- package/types/randomization/geometry/rndLine3.d.ts.map +1 -0
- package/types/randomization/geometry/rndPoint.d.ts +4 -0
- package/types/randomization/geometry/rndPoint.d.ts.map +1 -0
- package/types/randomization/random.d.ts +20 -0
- package/types/randomization/random.d.ts.map +1 -0
- package/types/randomization/rndHelpers.d.ts +21 -0
- package/types/randomization/rndTypes.d.ts +64 -0
- package/types/randomization/rndTypes.d.ts.map +1 -0
- package/dist/main.d.ts +0 -1
- package/dist/maths/algebra/equation.d.ts +0 -120
- package/dist/maths/algebra/linearSystem.d.ts +0 -40
- package/dist/maths/algebra/logicalset.d.ts +0 -28
- package/dist/maths/algebra/polynom.d.ts +0 -155
- package/dist/maths/algebra/rational.d.ts +0 -44
- package/dist/maths/algebra/study/rationalStudy.d.ts +0 -14
- package/dist/maths/algebra/study.d.ts +0 -140
- package/dist/maths/geometry/point.d.ts +0 -36
- package/dist/maths/geometry/triangle.d.ts +0 -92
- package/dist/maths/geometry/vector.d.ts +0 -38
- package/dist/maths/numeric.d.ts +0 -28
- package/dist/maths/numexp.d.ts +0 -19
- package/dist/maths/randomization/random.d.ts +0 -26
- package/dist/maths/randomization/randomCore.d.ts +0 -7
- package/dist/maths/randomization/rndFraction.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryCircle.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryLine.d.ts +0 -13
- package/dist/maths/randomization/rndGeometryPoint.d.ts +0 -13
- package/dist/maths/randomization/rndHelpers.d.ts +0 -23
- package/dist/maths/randomization/rndMonom.d.ts +0 -13
- package/dist/maths/randomization/rndPolynom.d.ts +0 -14
- package/dist/maths/randomization/rndTypes.d.ts +0 -40
- package/dist/maths/shutingyard.d.ts +0 -59
- package/dist/pimath.d.ts +0 -39
- package/lib/main.ts +0 -1
- package/lib/maths/algebra/equation.ts +0 -891
- package/lib/maths/algebra/linearSystem.ts +0 -369
- package/lib/maths/algebra/logicalset.ts +0 -183
- package/lib/maths/algebra/monom.ts +0 -1027
- package/lib/maths/algebra/polynom.ts +0 -1537
- package/lib/maths/algebra/rational.ts +0 -244
- package/lib/maths/algebra/study/rationalStudy.ts +0 -287
- package/lib/maths/algebra/study.ts +0 -506
- package/lib/maths/coefficients/fraction.ts +0 -593
- package/lib/maths/coefficients/nthRoot.ts +0 -148
- package/lib/maths/geometry/circle.ts +0 -379
- package/lib/maths/geometry/line.ts +0 -604
- package/lib/maths/geometry/point.ts +0 -215
- package/lib/maths/geometry/triangle.ts +0 -368
- package/lib/maths/geometry/vector.ts +0 -243
- package/lib/maths/numeric.ts +0 -162
- package/lib/maths/numexp.ts +0 -198
- package/lib/maths/randomization/random.ts +0 -80
- package/lib/maths/randomization/randomCore.ts +0 -19
- package/lib/maths/randomization/rndFraction.ts +0 -47
- package/lib/maths/randomization/rndGeometryCircle.ts +0 -50
- package/lib/maths/randomization/rndGeometryLine.ts +0 -53
- package/lib/maths/randomization/rndGeometryPoint.ts +0 -69
- package/lib/maths/randomization/rndHelpers.ts +0 -107
- package/lib/maths/randomization/rndMonom.ts +0 -57
- package/lib/maths/randomization/rndPolynom.ts +0 -90
- package/lib/maths/randomization/rndTypes.ts +0 -43
- package/lib/maths/shutingyard.ts +0 -496
- package/lib/pimath.ts +0 -40
|
@@ -1,593 +0,0 @@
|
|
|
1
|
-
import {Numeric} from "../numeric.ts";
|
|
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
|
-
}
|