pimath 0.1.40 → 0.2.1
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 +3106 -2873
- package/dist/pimath.js.map +1 -1
- package/package.json +13 -11
- package/src/algebra/equation.ts +113 -111
- package/src/algebra/equationSolver.ts +69 -120
- package/src/algebra/factor.ts +6 -7
- package/src/algebra/linearSystem.ts +97 -46
- package/src/algebra/logicalset.ts +51 -52
- package/src/algebra/monom.ts +23 -61
- package/src/algebra/operations.ts +0 -1
- package/src/algebra/polyFactor.ts +5 -5
- package/src/algebra/polynom.ts +69 -216
- package/src/analyze/index.ts +4 -0
- package/src/analyze/solution.ts +92 -29
- package/src/analyze/tableOfSigns.ts +1 -1
- package/src/coefficients/fraction.ts +189 -149
- package/src/coefficients/index.ts +1 -1
- package/src/coefficients/root.ts +66 -19
- package/src/geometry/TupleN.ts +128 -0
- package/src/geometry/circle.ts +308 -238
- package/src/geometry/geomMath.ts +4 -3
- package/src/geometry/index.ts +1 -0
- package/src/geometry/line.ts +221 -245
- package/src/geometry/line3.ts +78 -73
- package/src/geometry/plane3.ts +64 -55
- package/src/geometry/point.ts +57 -19
- package/src/geometry/triangle.ts +376 -248
- package/src/geometry/vector.ts +113 -229
- package/src/index.ts +13 -12
- package/src/numeric.ts +6 -9
- package/src/pimath.interface.ts +30 -28
- package/src/randomization/algebra/rndPolynom.ts +29 -15
- package/src/randomization/coefficient/rndFraction.ts +3 -3
- package/src/randomization/geometry/rndLine.ts +8 -10
- package/src/randomization/random.ts +11 -13
- package/src/randomization/rndTypes.ts +16 -12
- package/types/algebra/equation.d.ts +18 -17
- package/types/algebra/equation.d.ts.map +1 -1
- package/types/algebra/equationSolver.d.ts +5 -4
- package/types/algebra/equationSolver.d.ts.map +1 -1
- package/types/algebra/factor.d.ts +1 -1
- package/types/algebra/factor.d.ts.map +1 -1
- package/types/algebra/linearSystem.d.ts +23 -6
- package/types/algebra/linearSystem.d.ts.map +1 -1
- package/types/algebra/logicalset.d.ts +1 -1
- package/types/algebra/logicalset.d.ts.map +1 -1
- package/types/algebra/monom.d.ts +1 -6
- package/types/algebra/monom.d.ts.map +1 -1
- package/types/algebra/operations.d.ts.map +1 -1
- package/types/algebra/polyFactor.d.ts +4 -4
- package/types/algebra/polyFactor.d.ts.map +1 -1
- package/types/algebra/polynom.d.ts +10 -7
- package/types/algebra/polynom.d.ts.map +1 -1
- package/types/analyze/index.d.ts +2 -0
- package/types/analyze/index.d.ts.map +1 -0
- package/types/analyze/solution.d.ts +14 -8
- package/types/analyze/solution.d.ts.map +1 -1
- package/types/coefficients/fraction.d.ts +14 -12
- package/types/coefficients/fraction.d.ts.map +1 -1
- package/types/coefficients/index.d.ts +1 -1
- package/types/coefficients/index.d.ts.map +1 -1
- package/types/coefficients/root.d.ts +3 -0
- package/types/coefficients/root.d.ts.map +1 -1
- package/types/geometry/TupleAbstract.d.ts +22 -0
- package/types/geometry/TupleAbstract.d.ts.map +1 -0
- package/types/geometry/TupleN.d.ts +24 -0
- package/types/geometry/TupleN.d.ts.map +1 -0
- package/types/geometry/circle.d.ts +26 -17
- package/types/geometry/circle.d.ts.map +1 -1
- package/types/geometry/geomMath.d.ts +2 -1
- package/types/geometry/geomMath.d.ts.map +1 -1
- package/types/geometry/index.d.ts.map +1 -1
- package/types/geometry/line.d.ts +21 -30
- package/types/geometry/line.d.ts.map +1 -1
- package/types/geometry/line3.d.ts +19 -19
- package/types/geometry/line3.d.ts.map +1 -1
- package/types/geometry/matrix.d.ts +11 -11
- package/types/geometry/plane3.d.ts +10 -10
- package/types/geometry/plane3.d.ts.map +1 -1
- package/types/geometry/point.d.ts +11 -6
- package/types/geometry/point.d.ts.map +1 -1
- package/types/geometry/triangle.d.ts +68 -23
- package/types/geometry/triangle.d.ts.map +1 -1
- package/types/geometry/vector.d.ts +24 -44
- package/types/geometry/vector.d.ts.map +1 -1
- package/types/index.d.ts +5 -4
- package/types/index.d.ts.map +1 -1
- package/types/numeric.d.ts.map +1 -1
- package/types/pimath.interface.d.ts +18 -24
- package/types/pimath.interface.d.ts.map +1 -1
- package/types/randomization/algebra/rndPolynom.d.ts.map +1 -1
- package/types/randomization/coefficient/rndFraction.d.ts +1 -1
- package/types/randomization/coefficient/rndFraction.d.ts.map +1 -1
- package/types/randomization/geometry/rndLine.d.ts.map +1 -1
- package/types/randomization/random.d.ts +3 -2
- package/types/randomization/random.d.ts.map +1 -1
- package/types/randomization/rndTypes.d.ts +15 -10
- package/types/randomization/rndTypes.d.ts.map +1 -1
- package/src/coefficients/nthRoot.ts +0 -149
|
@@ -13,19 +13,20 @@ export enum FRAC_TYPE {
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction> {
|
|
16
|
-
#approximative = false
|
|
17
16
|
#denominator = 1
|
|
17
|
+
#digits = 3
|
|
18
|
+
#exact = true
|
|
18
19
|
#numerator = 1
|
|
19
20
|
#type: FRAC_TYPE = FRAC_TYPE.frac
|
|
21
|
+
#withSign = false
|
|
20
22
|
|
|
21
23
|
constructor()
|
|
22
24
|
constructor(value: InputValue<Fraction>)
|
|
23
25
|
constructor(numerator: number, denominator: number)
|
|
24
|
-
constructor(
|
|
25
|
-
constructor(value?: InputValue<Fraction>, denominatorOrPeriodic?: number) {
|
|
26
|
+
constructor(value?: InputValue<Fraction>, denominator?: number) {
|
|
26
27
|
|
|
27
28
|
if (value !== undefined) {
|
|
28
|
-
this.parse(value,
|
|
29
|
+
this.parse(value, denominator)
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
return this
|
|
@@ -35,10 +36,8 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
35
36
|
/**
|
|
36
37
|
* Parse the value to get the numerator and denominator
|
|
37
38
|
* @param value : number or string to parse to get the fraction
|
|
38
|
-
* @param denominatorOrPeriodic (optional|number) : length of the periodic part: 2.333333 => 1 or denominator value
|
|
39
39
|
*/
|
|
40
|
-
public parse = (value: InputValue<Fraction>,
|
|
41
|
-
let S: string[]
|
|
40
|
+
public parse = (value: InputValue<Fraction>, denominator?: number): this => {
|
|
42
41
|
|
|
43
42
|
// A null value means a zero fraction.
|
|
44
43
|
if (value === "") {
|
|
@@ -47,121 +46,76 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
47
46
|
return this
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// Split the string value in two parts: Numerator/Denominator
|
|
53
|
-
S = value.split('/')
|
|
54
|
-
|
|
55
|
-
// Security checks
|
|
56
|
-
if (S.length > 2) {
|
|
57
|
-
this.#numerator = NaN
|
|
58
|
-
// throw new Error(`The given value is not a valid fraction (${value})`)
|
|
59
|
-
}
|
|
60
|
-
if (S.map(x => x === '' || isNaN(Number(x))).includes(true)) {
|
|
61
|
-
this.#numerator = NaN
|
|
62
|
-
// throw new Error(`The given value is not a valid fraction (${value})`)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (S.length === 1) {
|
|
66
|
-
// No divide sign - it's a number
|
|
67
|
-
return this.parse(+S[0])
|
|
68
|
-
} else if (S.length === 2) {
|
|
69
|
-
// One divide signe
|
|
70
|
-
// We check if the denominator is zero
|
|
71
|
-
if (S[1] === '0') {
|
|
72
|
-
this.#numerator = NaN
|
|
73
|
-
this.#denominator = 1
|
|
74
|
-
} else {
|
|
75
|
-
this.#numerator = +S[0]
|
|
76
|
-
this.#denominator = +S[1]
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
79
|
-
// More than one divide sign ?
|
|
80
|
-
// This is not a fraction
|
|
81
|
-
this.#numerator = NaN
|
|
82
|
-
this.#denominator = 1
|
|
83
|
-
}
|
|
84
|
-
break
|
|
85
|
-
case "number":
|
|
86
|
-
if (Number.isSafeInteger(value)) {
|
|
87
|
-
// The given value is an integer
|
|
88
|
-
this.#numerator = +value
|
|
89
|
-
|
|
90
|
-
if (denominatorOrPeriodic === undefined || !Number.isSafeInteger(denominatorOrPeriodic)) {
|
|
91
|
-
this.#denominator = 1
|
|
92
|
-
} else {
|
|
93
|
-
this.#denominator = +denominatorOrPeriodic
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
// The given value is a float number
|
|
97
|
-
// Get the number of decimals after the float sign
|
|
98
|
-
const [, decimal] = (value.toString()).split('.')
|
|
99
|
-
const p: number = decimal ? decimal.length : 0
|
|
100
|
-
const power = Math.pow(10, p)
|
|
101
|
-
|
|
102
|
-
// Detect if the decimal part is periodic or not...
|
|
103
|
-
if (denominatorOrPeriodic === undefined) {
|
|
104
|
-
// Transform the float number in two integer
|
|
105
|
-
// 0.123 = 0.123*10^3 / 10^3
|
|
106
|
-
this.#numerator = value * power
|
|
107
|
-
this.#denominator = power
|
|
108
|
-
} else if (Number.isSafeInteger(denominatorOrPeriodic)) {
|
|
109
|
-
this.#numerator = value * power - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic))
|
|
110
|
-
this.denominator = power - Math.pow(10, p - denominatorOrPeriodic)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
this.#numerator = Numeric.numberCorrection(this.#numerator)
|
|
114
|
-
this.#denominator = Numeric.numberCorrection(this.#denominator)
|
|
115
|
-
|
|
116
|
-
this.reduce()
|
|
117
|
-
}
|
|
118
|
-
break
|
|
119
|
-
case "object":
|
|
120
|
-
if (value instanceof Fraction) {
|
|
121
|
-
this.#numerator = +value.numerator
|
|
122
|
-
this.#denominator = +value.denominator
|
|
123
|
-
}
|
|
124
|
-
break
|
|
49
|
+
if (typeof value === "string") {
|
|
50
|
+
return this.fromString(value)
|
|
125
51
|
}
|
|
52
|
+
|
|
53
|
+
if (typeof value === "number" && denominator===undefined) {
|
|
54
|
+
return this.fromNumber(value)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (typeof value === "number" && typeof denominator==="number") {
|
|
58
|
+
return this.fromNumbers(value, denominator)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (value instanceof Fraction) {
|
|
62
|
+
return this.copy(value)
|
|
63
|
+
}
|
|
64
|
+
|
|
126
65
|
return this
|
|
127
66
|
}
|
|
128
67
|
|
|
129
68
|
public clone = (): Fraction => {
|
|
130
69
|
const F = new Fraction()
|
|
131
|
-
F.numerator =
|
|
132
|
-
F.denominator =
|
|
70
|
+
F.numerator = this.#numerator
|
|
71
|
+
F.denominator = this.#denominator
|
|
72
|
+
F.exact = this.exact
|
|
133
73
|
return F
|
|
134
74
|
}
|
|
135
75
|
|
|
136
|
-
|
|
76
|
+
public copy(value: Fraction): this {
|
|
77
|
+
this.#numerator = value.numerator
|
|
78
|
+
this.#denominator = value.denominator
|
|
79
|
+
this.#exact = value.exact
|
|
80
|
+
|
|
81
|
+
return this
|
|
82
|
+
}
|
|
83
|
+
|
|
137
84
|
public get tex(): string {
|
|
138
85
|
if (this.isInfinity()) {
|
|
139
86
|
return `${this.sign() === 1 ? '+' : '-'}\\infty`
|
|
140
87
|
}
|
|
141
88
|
|
|
89
|
+
const plus = this.#withSign && this.isPositive() ? '+' : ''
|
|
142
90
|
|
|
143
|
-
if (this.
|
|
91
|
+
if (this.exact) {
|
|
144
92
|
if (this.#denominator === 1) {
|
|
145
|
-
return `${this.#numerator}`
|
|
93
|
+
return `${plus}${this.#numerator}`
|
|
146
94
|
} else if (this.#numerator < 0) {
|
|
147
95
|
return `-\\${this.#type}{ ${-this.#numerator} }{ ${this.#denominator} }`
|
|
148
96
|
} else {
|
|
149
|
-
return
|
|
97
|
+
return `${plus}\\${this.#type}{ ${this.#numerator} }{ ${this.#denominator} }`
|
|
150
98
|
}
|
|
151
99
|
} else {
|
|
152
|
-
return this.value.toFixed(
|
|
100
|
+
return plus + this.value.toFixed(this.#digits)
|
|
153
101
|
}
|
|
154
102
|
}
|
|
155
103
|
|
|
156
104
|
public get display(): string {
|
|
157
|
-
if (this.
|
|
105
|
+
if (this.isInfinity()) {
|
|
106
|
+
return `${this.sign() === 1 ? '+' : '-'}oo`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const plus = this.#withSign && this.isPositive() ? '+' : ''
|
|
110
|
+
|
|
111
|
+
if (this.exact) {
|
|
158
112
|
if (this.#denominator === 1) {
|
|
159
|
-
return `${this.#numerator}`
|
|
113
|
+
return `${plus}${this.#numerator}`
|
|
160
114
|
} else {
|
|
161
|
-
return `${this.#numerator}/${this.#denominator}`
|
|
115
|
+
return `${plus}${this.#numerator}/${this.#denominator}`
|
|
162
116
|
}
|
|
163
117
|
} else {
|
|
164
|
-
return this.value.toFixed(
|
|
118
|
+
return plus + this.value.toFixed(this.#digits)
|
|
165
119
|
}
|
|
166
120
|
}
|
|
167
121
|
|
|
@@ -179,12 +133,12 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
179
133
|
|
|
180
134
|
public static isFraction(value: InputValue<Fraction>) {
|
|
181
135
|
if (value instanceof Fraction ||
|
|
182
|
-
(typeof value === "number" && !isNaN(
|
|
136
|
+
(typeof value === "number" && !isNaN(value))
|
|
183
137
|
) {
|
|
184
138
|
return true
|
|
185
139
|
}
|
|
186
140
|
|
|
187
|
-
if(typeof value === "string"){
|
|
141
|
+
if (typeof value === "string") {
|
|
188
142
|
const [num, den] = value.split('/')
|
|
189
143
|
|
|
190
144
|
return !isNaN(+num) && !isNaN(+den)
|
|
@@ -207,9 +161,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
207
161
|
return M
|
|
208
162
|
}
|
|
209
163
|
|
|
210
|
-
// ------------------------------------------
|
|
211
|
-
// Compare functions
|
|
212
|
-
|
|
213
164
|
public static min = (...fractions: (InputValue<Fraction>)[]): Fraction => {
|
|
214
165
|
let M = new Fraction(fractions[0])
|
|
215
166
|
|
|
@@ -235,6 +186,15 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
235
186
|
return sorted
|
|
236
187
|
}
|
|
237
188
|
|
|
189
|
+
public static toSameDenominateur(...fractions: InputValue<Fraction>[]): Fraction[] {
|
|
190
|
+
const F = fractions.map(x => new Fraction(x))
|
|
191
|
+
const lcm = Numeric.lcm(...F.map(x => x.denominator))
|
|
192
|
+
|
|
193
|
+
F.forEach(x => x.amplify(lcm / x.denominator))
|
|
194
|
+
|
|
195
|
+
return F
|
|
196
|
+
}
|
|
197
|
+
|
|
238
198
|
public static unique = (fractions: (InputValue<Fraction>)[]): Fraction[] => {
|
|
239
199
|
const unique: Record<string, boolean> = {},
|
|
240
200
|
distinct: Fraction[] = []
|
|
@@ -255,15 +215,8 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
255
215
|
|
|
256
216
|
public static xMultiply = (...values: (InputValue<Fraction>)[]): Fraction => {
|
|
257
217
|
const R = new Fraction()
|
|
258
|
-
// Parse the value.
|
|
259
|
-
// If it's a fraction, return a clone of it
|
|
260
|
-
// If it's an integer, return the fraction F/1
|
|
261
|
-
for (const value of values) {
|
|
262
218
|
|
|
263
|
-
|
|
264
|
-
R.numerator = R.numerator * F.numerator
|
|
265
|
-
R.denominator = R.denominator * F.denominator
|
|
266
|
-
}
|
|
219
|
+
values.forEach(f=> R.multiply(f, false))
|
|
267
220
|
|
|
268
221
|
return R
|
|
269
222
|
}
|
|
@@ -281,6 +234,7 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
281
234
|
|
|
282
235
|
this.#numerator = N * F.denominator + F.numerator * D
|
|
283
236
|
this.#denominator = D * F.denominator
|
|
237
|
+
this.exact = this.exact && F.exact
|
|
284
238
|
} else {
|
|
285
239
|
return this.add(new Fraction(F))
|
|
286
240
|
}
|
|
@@ -296,14 +250,10 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
296
250
|
return this
|
|
297
251
|
}
|
|
298
252
|
|
|
299
|
-
/**
|
|
300
|
-
* Simple function to determine if it's a fraction
|
|
301
|
-
*/
|
|
302
253
|
public areEquals = (...F: Fraction[]): boolean => {
|
|
303
254
|
return F.every(f => f.isEqual(F[0]))
|
|
304
255
|
}
|
|
305
256
|
|
|
306
|
-
// ------------------------------------------
|
|
307
257
|
/**
|
|
308
258
|
* Compare the current coefficient with another coefficient
|
|
309
259
|
* @param F (Coefficient) The coefficient to _compare
|
|
@@ -354,6 +304,11 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
354
304
|
return this
|
|
355
305
|
}
|
|
356
306
|
|
|
307
|
+
digits(value: number): this {
|
|
308
|
+
this.#digits = value
|
|
309
|
+
return this
|
|
310
|
+
}
|
|
311
|
+
|
|
357
312
|
public divide = (F: Fraction | number): Fraction => {
|
|
358
313
|
const Q = new Fraction(F)
|
|
359
314
|
|
|
@@ -366,23 +321,124 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
366
321
|
|
|
367
322
|
this.#numerator = N * Q.denominator
|
|
368
323
|
this.#denominator = D * Q.numerator
|
|
324
|
+
|
|
325
|
+
this.exact = this.exact && Q.exact
|
|
369
326
|
return this.reduce()
|
|
370
327
|
}
|
|
371
328
|
|
|
329
|
+
get exact(): boolean {
|
|
330
|
+
return this.#exact
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
set exact(value: boolean) {
|
|
334
|
+
this.#exact = value
|
|
335
|
+
}
|
|
336
|
+
|
|
372
337
|
public get frac(): this {
|
|
373
338
|
this.#type = FRAC_TYPE.frac
|
|
374
339
|
return this
|
|
375
340
|
}
|
|
376
341
|
|
|
342
|
+
public fromNumber(value: number): this {
|
|
343
|
+
if (Number.isSafeInteger(value)) {
|
|
344
|
+
// The given value is an integer
|
|
345
|
+
this.#numerator = value
|
|
346
|
+
this.#denominator = 1
|
|
347
|
+
this.#exact = true
|
|
348
|
+
return this
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// The given value is a float number
|
|
352
|
+
// Get the number of decimals after the float sign
|
|
353
|
+
const [, decimal] = (value.toString()).split('.')
|
|
354
|
+
const p: number = decimal ? decimal.length : 0
|
|
355
|
+
const power = Math.pow(10, p)
|
|
356
|
+
|
|
357
|
+
this.#numerator = value * power
|
|
358
|
+
this.#denominator = power
|
|
359
|
+
|
|
360
|
+
this.#numerator = Numeric.numberCorrection(this.#numerator)
|
|
361
|
+
this.#denominator = Numeric.numberCorrection(this.#denominator)
|
|
362
|
+
|
|
363
|
+
this.reduce()
|
|
364
|
+
|
|
365
|
+
// assume it's not exact if the decimal part is more than 10 decimals
|
|
366
|
+
this.#exact = p < 10
|
|
367
|
+
|
|
368
|
+
return this
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
public fromNumbers(numerator: number, denominator: number): this {
|
|
372
|
+
if (Number.isSafeInteger(numerator) && Number.isSafeInteger(denominator)) {
|
|
373
|
+
// The given value is an integer
|
|
374
|
+
this.#numerator = numerator
|
|
375
|
+
this.#denominator = denominator
|
|
376
|
+
this.#exact = true
|
|
377
|
+
return this
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return this.fromNumber(numerator / denominator)
|
|
381
|
+
}
|
|
382
|
+
public fromPeriodic(value: string | number, length: number): this {
|
|
383
|
+
const [, decimal] = (value.toString()).split(/[.,]/)
|
|
384
|
+
const p: number = decimal ? decimal.length : 0
|
|
385
|
+
const power = Math.pow(10, p)
|
|
386
|
+
|
|
387
|
+
this.#numerator = (+value) * power - Math.floor((+value) * Math.pow(10, p - length))
|
|
388
|
+
this.#denominator = power - Math.pow(10, p - length)
|
|
389
|
+
this.#exact = true
|
|
390
|
+
|
|
391
|
+
return this
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
public fromString(value: string): this {
|
|
395
|
+
// Split the string value in two parts: Numerator/Denominator
|
|
396
|
+
const S = value.split('/').map(Number)
|
|
397
|
+
|
|
398
|
+
this.#exact = true
|
|
399
|
+
|
|
400
|
+
// Only one divide sign allowed
|
|
401
|
+
if (S.length > 2) {
|
|
402
|
+
this.#numerator = NaN
|
|
403
|
+
return this
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Each parts must be a number
|
|
407
|
+
if (S.some(x => isNaN(x))) {
|
|
408
|
+
this.#numerator = NaN
|
|
409
|
+
return this
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (S.length === 1) {
|
|
413
|
+
// No divide sign - it's a number
|
|
414
|
+
return this.fromNumber(+value)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// One divide signe
|
|
418
|
+
// We check if the denominator is zero
|
|
419
|
+
if (S[1] === 0) {
|
|
420
|
+
this.#numerator = NaN
|
|
421
|
+
this.#denominator = 1
|
|
422
|
+
return this
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
this.#numerator = S[0]
|
|
426
|
+
this.#denominator = S[1]
|
|
427
|
+
|
|
428
|
+
return this
|
|
429
|
+
}
|
|
430
|
+
|
|
377
431
|
public infinite = (): this => {
|
|
378
432
|
this.#numerator = Infinity
|
|
379
433
|
this.#denominator = 1
|
|
434
|
+
this.exact = true
|
|
380
435
|
return this
|
|
381
436
|
}
|
|
382
437
|
|
|
383
438
|
public invalid = (): this => {
|
|
384
439
|
this.#numerator = NaN
|
|
385
440
|
this.#denominator = 1
|
|
441
|
+
this.exact = true
|
|
386
442
|
return this
|
|
387
443
|
}
|
|
388
444
|
|
|
@@ -395,11 +451,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
395
451
|
return this
|
|
396
452
|
}
|
|
397
453
|
|
|
398
|
-
public isApproximative = (): boolean => {
|
|
399
|
-
return this.#approximative ||
|
|
400
|
-
this.#numerator.toString().length >= 15 && this.#denominator.toString().length >= 15
|
|
401
|
-
}
|
|
402
|
-
|
|
403
454
|
public isEqual = (than: Fraction | number): boolean => {
|
|
404
455
|
return this.compare(than, '=')
|
|
405
456
|
}
|
|
@@ -408,10 +459,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
408
459
|
return this.isRelative() && this.value % 2 === 0
|
|
409
460
|
}
|
|
410
461
|
|
|
411
|
-
public isExact = (): boolean => {
|
|
412
|
-
return !this.isApproximative()
|
|
413
|
-
}
|
|
414
|
-
|
|
415
462
|
public isFinite = (): boolean => {
|
|
416
463
|
return !this.isInfinity() && !this.isNaN()
|
|
417
464
|
}
|
|
@@ -428,15 +475,10 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
428
475
|
return Math.abs(this.#numerator) === Infinity
|
|
429
476
|
}
|
|
430
477
|
|
|
431
|
-
public isInverted = (p: Fraction): boolean => {
|
|
432
|
-
return this.isEqual(new Fraction().one().divide(p.clone()))
|
|
433
|
-
}
|
|
434
|
-
|
|
435
478
|
public isLeq = (than: Fraction | number): boolean => {
|
|
436
479
|
return this.compare(than, '<=')
|
|
437
480
|
}
|
|
438
481
|
|
|
439
|
-
/* Compare shortcuts */
|
|
440
482
|
public isLesser = (than: Fraction | number): boolean => {
|
|
441
483
|
return this.compare(than, '<')
|
|
442
484
|
}
|
|
@@ -453,12 +495,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
453
495
|
return this.sign() === -1
|
|
454
496
|
}
|
|
455
497
|
|
|
456
|
-
public isNegativeOne = (): boolean => {
|
|
457
|
-
return this.#numerator === -1 && this.#denominator === 1
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
// ------------------------------------------
|
|
461
|
-
|
|
462
498
|
public isNotEqual = (than: Fraction | number): boolean => {
|
|
463
499
|
return this.compare(than, '<>')
|
|
464
500
|
}
|
|
@@ -475,16 +511,12 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
475
511
|
return this.#numerator === 1 && this.#denominator === 1
|
|
476
512
|
}
|
|
477
513
|
|
|
478
|
-
public isOpposite = (p: Fraction): boolean => {
|
|
479
|
-
return this.isEqual(p.clone().opposite())
|
|
480
|
-
}
|
|
481
|
-
|
|
482
514
|
public isPositive = (): boolean => {
|
|
483
515
|
return this.sign() === 1
|
|
484
516
|
}
|
|
485
517
|
|
|
486
518
|
public isRational = (): boolean => {
|
|
487
|
-
return !this.isRelative()
|
|
519
|
+
return this.exact && !this.isRelative()
|
|
488
520
|
}
|
|
489
521
|
|
|
490
522
|
public isReduced = (): boolean => {
|
|
@@ -492,7 +524,7 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
492
524
|
}
|
|
493
525
|
|
|
494
526
|
public isRelative = (): boolean => {
|
|
495
|
-
return this.clone().reduce().denominator === 1
|
|
527
|
+
return this.exact && this.clone().reduce().denominator === 1
|
|
496
528
|
}
|
|
497
529
|
|
|
498
530
|
public isSquare = (): boolean => {
|
|
@@ -507,12 +539,16 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
507
539
|
return this.value > 0
|
|
508
540
|
}
|
|
509
541
|
|
|
542
|
+
public isUnit(): boolean {
|
|
543
|
+
return Math.abs(this.#numerator) === 1 && this.#denominator === 1
|
|
544
|
+
}
|
|
545
|
+
|
|
510
546
|
// Mathematical operations specific to fractions
|
|
511
547
|
public isZero = (): boolean => {
|
|
512
548
|
return this.#numerator === 0
|
|
513
549
|
}
|
|
514
550
|
|
|
515
|
-
public multiply = (F: Fraction
|
|
551
|
+
public multiply = (F: InputValue<Fraction>, reduce = true): this => {
|
|
516
552
|
// Parse the value.
|
|
517
553
|
// If it's a fraction, return a clone of it
|
|
518
554
|
// If it's an integer, return the fraction F/1
|
|
@@ -521,7 +557,9 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
521
557
|
this.#numerator = this.#numerator * Q.numerator
|
|
522
558
|
this.#denominator = this.#denominator * Q.denominator
|
|
523
559
|
|
|
524
|
-
|
|
560
|
+
this.exact = this.exact && Q.exact
|
|
561
|
+
|
|
562
|
+
return reduce ? this.reduce() : this
|
|
525
563
|
}
|
|
526
564
|
|
|
527
565
|
// ------------------------------------------
|
|
@@ -534,9 +572,7 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
534
572
|
}
|
|
535
573
|
|
|
536
574
|
public one = (): this => {
|
|
537
|
-
this
|
|
538
|
-
this.#denominator = 1
|
|
539
|
-
return this
|
|
575
|
+
return this.fromNumber(1)
|
|
540
576
|
}
|
|
541
577
|
|
|
542
578
|
public opposite = (): this => {
|
|
@@ -572,7 +608,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
572
608
|
return this
|
|
573
609
|
}
|
|
574
610
|
|
|
575
|
-
// ------------------------------------------
|
|
576
611
|
public reduce = (): this => {
|
|
577
612
|
const g = Numeric.gcd(this.#numerator, this.#denominator)
|
|
578
613
|
this.#numerator = this.#numerator / g
|
|
@@ -585,9 +620,6 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
585
620
|
return this
|
|
586
621
|
}
|
|
587
622
|
|
|
588
|
-
// ------------------------------------------
|
|
589
|
-
// Getter and setter
|
|
590
|
-
|
|
591
623
|
public root = (p: number): this => {
|
|
592
624
|
|
|
593
625
|
// Check if they are perfect roots..
|
|
@@ -631,7 +663,7 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
631
663
|
// The fraction is not a perfect root - make it approximative
|
|
632
664
|
this.#numerator = this.#numerator / this.#denominator
|
|
633
665
|
this.#denominator = 1
|
|
634
|
-
this
|
|
666
|
+
this.exact = false
|
|
635
667
|
}
|
|
636
668
|
|
|
637
669
|
// Restore the sign
|
|
@@ -670,9 +702,17 @@ export class Fraction implements IPiMathObject<Fraction>, IExpression<Fraction>
|
|
|
670
702
|
return result === 0 ? 0 : result
|
|
671
703
|
}
|
|
672
704
|
|
|
673
|
-
|
|
674
|
-
this.#
|
|
675
|
-
this.#denominator = 1
|
|
705
|
+
get withSign(): this {
|
|
706
|
+
this.#withSign = true
|
|
676
707
|
return this
|
|
677
708
|
}
|
|
709
|
+
|
|
710
|
+
get withoutSign(): this {
|
|
711
|
+
this.#withSign = false
|
|
712
|
+
return this
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
public zero = (): this => {
|
|
716
|
+
return this.fromNumber(0)
|
|
717
|
+
}
|
|
678
718
|
}
|