pimath 0.0.13 → 0.0.17
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/dev/index.html +9 -3
- package/dev/pi.js +364 -229
- package/dev/pi.js.map +1 -1
- package/dist/pi.js +1 -1
- package/dist/pi.js.map +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/algebra.Algebra.Equation.html +26 -0
- package/docs/classes/algebra.Algebra.LinearSystem.html +1 -0
- package/docs/classes/algebra.Algebra.LogicalSet.html +3 -0
- package/docs/classes/algebra.Algebra.Monom.html +36 -36
- package/docs/classes/algebra.Algebra.Polynom.html +11 -11
- package/docs/classes/algebra.Algebra.Rational.html +2 -0
- package/docs/classes/algebra.Equation.html +9 -9
- package/docs/classes/algebra.LinearSystem.html +1 -1
- package/docs/classes/algebra.Logicalset.html +2 -2
- package/docs/classes/algebra.Monom.html +36 -36
- package/docs/classes/algebra.Polynom.html +12 -12
- package/docs/classes/algebra.Rational.html +2 -2
- package/docs/classes/coefficients.Fraction.html +4 -4
- package/docs/classes/coefficients.Nthroot.html +1 -1
- package/docs/classes/geometry.Circle.html +1 -1
- package/docs/classes/geometry.Geometry.Circle.html +1 -0
- package/docs/classes/geometry.Geometry.Line.html +1 -0
- package/docs/classes/geometry.Geometry.Point.html +1 -0
- package/docs/classes/geometry.Geometry.Triangle.html +9 -0
- package/docs/classes/geometry.Geometry.Vector.html +1 -0
- package/docs/classes/geometry.Line.html +1 -1
- package/docs/classes/geometry.Point.html +1 -1
- package/docs/classes/geometry.Triangle.html +6 -6
- package/docs/classes/geometry.Vector.html +1 -1
- package/docs/classes/numeric.Numeric.html +5 -5
- package/docs/classes/shutingyard.Shutingyard.html +5 -5
- package/docs/interfaces/geometry.remarquableLines.html +1 -1
- package/docs/modules/algebra.Algebra.html +1 -1
- package/docs/modules/geometry.Geometry.html +1 -0
- package/docs/modules/geometry.html +1 -1
- package/docs/modules/random.Random.html +1 -1
- package/docs/modules/random.html +1 -1
- package/esm/main.js +24 -22
- package/esm/main.js.map +1 -1
- package/esm/maths/algebra/equation.d.ts +2 -1
- package/esm/maths/algebra/equation.js +29 -25
- package/esm/maths/algebra/equation.js.map +1 -1
- package/esm/maths/algebra/index.d.ts +0 -8
- package/esm/maths/algebra/index.js +18 -17
- package/esm/maths/algebra/index.js.map +1 -1
- package/esm/maths/algebra/linearSystem.d.ts +1 -1
- package/esm/maths/algebra/linearSystem.js +19 -15
- package/esm/maths/algebra/linearSystem.js.map +1 -1
- package/esm/maths/algebra/logicalset.js +7 -3
- package/esm/maths/algebra/logicalset.js.map +1 -1
- package/esm/maths/algebra/monom.d.ts +14 -17
- package/esm/maths/algebra/monom.js +164 -75
- package/esm/maths/algebra/monom.js.map +1 -1
- package/esm/maths/algebra/polynom.d.ts +6 -24
- package/esm/maths/algebra/polynom.js +57 -206
- package/esm/maths/algebra/polynom.js.map +1 -1
- package/esm/maths/algebra/rational.js +15 -10
- package/esm/maths/algebra/rational.js.map +1 -1
- package/esm/maths/coefficients/fraction.d.ts +20 -10
- package/esm/maths/coefficients/fraction.js +83 -15
- package/esm/maths/coefficients/fraction.js.map +1 -1
- package/esm/maths/coefficients/index.js +14 -2
- package/esm/maths/coefficients/index.js.map +1 -1
- package/esm/maths/coefficients/nthroot.js +5 -1
- package/esm/maths/coefficients/nthroot.js.map +1 -1
- package/esm/maths/geometry/circle.js +12 -8
- package/esm/maths/geometry/circle.js.map +1 -1
- package/esm/maths/geometry/index.js +17 -5
- package/esm/maths/geometry/index.js.map +1 -1
- package/esm/maths/geometry/line.js +26 -22
- package/esm/maths/geometry/line.js.map +1 -1
- package/esm/maths/geometry/point.js +16 -12
- package/esm/maths/geometry/point.js.map +1 -1
- package/esm/maths/geometry/triangle.js +31 -27
- package/esm/maths/geometry/triangle.js.map +1 -1
- package/esm/maths/geometry/vector.js +21 -17
- package/esm/maths/geometry/vector.js.map +1 -1
- package/esm/maths/numeric.js +5 -1
- package/esm/maths/numeric.js.map +1 -1
- package/esm/maths/random/index.d.ts +3 -1
- package/esm/maths/random/index.js +32 -14
- package/esm/maths/random/index.js.map +1 -1
- package/esm/maths/random/randomCore.js +5 -1
- package/esm/maths/random/randomCore.js.map +1 -1
- package/esm/maths/random/rndFraction.d.ts +9 -0
- package/esm/maths/random/rndFraction.js +30 -0
- package/esm/maths/random/rndFraction.js.map +1 -0
- package/esm/maths/random/rndHelpers.js +5 -1
- package/esm/maths/random/rndHelpers.js.map +1 -1
- package/esm/maths/random/rndMonom.d.ts +2 -2
- package/esm/maths/random/rndMonom.js +15 -8
- package/esm/maths/random/rndMonom.js.map +1 -1
- package/esm/maths/random/rndPolynom.d.ts +1 -1
- package/esm/maths/random/rndPolynom.js +22 -10
- package/esm/maths/random/rndPolynom.js.map +1 -1
- package/esm/maths/random/rndTypes.d.ts +5 -0
- package/esm/maths/random/rndTypes.js +2 -1
- package/esm/maths/shutingyard.js +5 -1
- package/esm/maths/shutingyard.js.map +1 -1
- package/package.json +5 -5
- package/src/maths/algebra/equation.ts +6 -5
- package/src/maths/algebra/index.ts +1 -9
- package/src/maths/algebra/linearSystem.ts +3 -3
- package/src/maths/algebra/monom.ts +759 -622
- package/src/maths/algebra/monom_bck.backup +746 -0
- package/src/maths/algebra/polynom.ts +980 -1170
- package/src/maths/algebra/rational.ts +6 -6
- package/src/maths/coefficients/fraction.ts +98 -27
- package/src/maths/geometry/index.ts +1 -1
- package/src/maths/geometry/line.ts +1 -1
- package/src/maths/random/index.ts +9 -3
- package/src/maths/random/rndFraction.ts +37 -0
- package/src/maths/random/rndMonom.ts +6 -3
- package/src/maths/random/rndPolynom.ts +15 -5
- package/src/maths/random/rndTypes.ts +5 -0
- package/src/maths/shutingyard.ts +2 -0
- package/tests/algebra/monom.test.ts +29 -8
- package/tests/algebra/polynom.test.ts +13 -22
- package/tests/coefficients/fraction.test.ts +35 -38
- package/tests/shutingyard.test.ts +0 -1
- package/tsconfig.json +1 -1
- package/tsconfig.testing.json +28 -0
|
@@ -131,17 +131,17 @@ import {Fraction} from "../coefficients/fraction";
|
|
|
131
131
|
|
|
132
132
|
N.divide(D)
|
|
133
133
|
|
|
134
|
-
if (N.degree(letter)
|
|
135
|
-
return N.coefficient.sign() * (Math.pow((value > 0 ? 1 : -1), N.degree(letter) % 2)) === 1 ? Infinity : -Infinity
|
|
134
|
+
if (N.degree(letter).isStrictlyPositive()) {
|
|
135
|
+
return N.coefficient.sign() * (Math.pow((value > 0 ? 1 : -1), N.degree(letter).value % 2)) === 1 ? Infinity : -Infinity
|
|
136
136
|
}
|
|
137
|
-
if (N.degree(letter)
|
|
137
|
+
if (N.degree(letter).isZero()) {
|
|
138
138
|
return N.coefficient
|
|
139
139
|
}
|
|
140
|
-
if (N.degree(letter)
|
|
141
|
-
return N.coefficient.sign() * (Math.pow(-1, N.degree(letter) % 2)) === 1 ? 0 : -0
|
|
140
|
+
if (N.degree(letter).isStrictlyPositive()) {
|
|
141
|
+
return N.coefficient.sign() * (Math.pow(-1, N.degree(letter).value % 2)) === 1 ? 0 : -0
|
|
142
142
|
}
|
|
143
143
|
} else {
|
|
144
|
-
return this._numerator.evaluate({letter: value}).divide(this._denominator.evaluate({letter: value}))
|
|
144
|
+
return this._numerator.evaluate({letter: new Fraction(value)}).divide(this._denominator.evaluate({letter: new Fraction(value)}))
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Numeric} from "../numeric";
|
|
2
|
+
import {Random} from "../random";
|
|
2
3
|
|
|
3
4
|
export class Fraction {
|
|
4
5
|
private _numerator: number;
|
|
@@ -95,8 +96,8 @@ export class Fraction {
|
|
|
95
96
|
S = value.split('/');
|
|
96
97
|
|
|
97
98
|
// Security checks
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
if (S.length > 2) throw "Two many divide signs";
|
|
100
|
+
if (S.map(x => x === '' || isNaN(Number(x))).includes(true)) throw "Not a number"
|
|
100
101
|
|
|
101
102
|
|
|
102
103
|
if (S.length === 1) {
|
|
@@ -193,18 +194,26 @@ export class Fraction {
|
|
|
193
194
|
return this;
|
|
194
195
|
};
|
|
195
196
|
|
|
196
|
-
add = (F: Fraction): Fraction => {
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
add = (F: Fraction | number): Fraction => {
|
|
198
|
+
if (F instanceof Fraction) {
|
|
199
|
+
let N: number = this._numerator,
|
|
200
|
+
D: number = this._denominator;
|
|
199
201
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
this._numerator = N * F.denominator + F.numerator * D;
|
|
203
|
+
this._denominator = D * F.denominator;
|
|
204
|
+
}else{
|
|
205
|
+
return this.add(new Fraction(F))
|
|
206
|
+
}
|
|
202
207
|
|
|
203
208
|
return this.reduce();
|
|
204
209
|
};
|
|
205
210
|
|
|
206
|
-
subtract = (F: Fraction): Fraction => {
|
|
207
|
-
|
|
211
|
+
subtract = (F: Fraction | number): Fraction => {
|
|
212
|
+
if (F instanceof Fraction) {
|
|
213
|
+
return this.add(F.clone().opposed());
|
|
214
|
+
} else {
|
|
215
|
+
return this.add(-F)
|
|
216
|
+
}
|
|
208
217
|
};
|
|
209
218
|
|
|
210
219
|
multiply = (F: Fraction | number): Fraction => {
|
|
@@ -241,10 +250,16 @@ export class Fraction {
|
|
|
241
250
|
|
|
242
251
|
return this;
|
|
243
252
|
}
|
|
244
|
-
pow = (p: number): Fraction => {
|
|
253
|
+
pow = (p: number | Fraction): Fraction => {
|
|
254
|
+
// TODO: Fraction.pow with a value different than a safe integer ?
|
|
255
|
+
if (p instanceof Fraction) {
|
|
256
|
+
return this.pow(p.value)
|
|
257
|
+
}
|
|
258
|
+
|
|
245
259
|
if (!Number.isSafeInteger(p)) {
|
|
246
260
|
return this.invalid();
|
|
247
261
|
}
|
|
262
|
+
|
|
248
263
|
this.reduce();
|
|
249
264
|
|
|
250
265
|
if (p < 0) {
|
|
@@ -287,6 +302,32 @@ export class Fraction {
|
|
|
287
302
|
return this;
|
|
288
303
|
};
|
|
289
304
|
|
|
305
|
+
|
|
306
|
+
static max = (...fractions: (Fraction|number)[]): Fraction => {
|
|
307
|
+
let M = new Fraction(fractions[0])
|
|
308
|
+
|
|
309
|
+
for (let m of fractions) {
|
|
310
|
+
let compare = new Fraction(m)
|
|
311
|
+
if (compare.greater(M)) {
|
|
312
|
+
M = compare.clone()
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return M
|
|
317
|
+
}
|
|
318
|
+
static min = (...fractions: (Fraction|number)[]): Fraction => {
|
|
319
|
+
let M = new Fraction(fractions[0])
|
|
320
|
+
|
|
321
|
+
for (let m of fractions) {
|
|
322
|
+
let compare = new Fraction(m)
|
|
323
|
+
if (compare.lesser(M)) {
|
|
324
|
+
M = compare.clone()
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return M
|
|
329
|
+
}
|
|
330
|
+
|
|
290
331
|
// ------------------------------------------
|
|
291
332
|
// Mathematical operations specific to fractions
|
|
292
333
|
// ------------------------------------------
|
|
@@ -319,49 +360,55 @@ export class Fraction {
|
|
|
319
360
|
* @param F (Coefficient) The coefficient to compare
|
|
320
361
|
* @param sign (string| default is =): authorized values: =, <, <=, >, >= with some variations.
|
|
321
362
|
*/
|
|
322
|
-
compare = (F:
|
|
363
|
+
compare = (F: unknown, sign?: string): boolean => {
|
|
323
364
|
if (sign === undefined) {
|
|
324
365
|
sign = '=';
|
|
325
366
|
}
|
|
326
367
|
|
|
368
|
+
let compareFraction: Fraction
|
|
369
|
+
if (F instanceof Fraction) {
|
|
370
|
+
compareFraction = F.clone()
|
|
371
|
+
} else {
|
|
372
|
+
compareFraction = new Fraction(F)
|
|
373
|
+
}
|
|
327
374
|
|
|
328
375
|
switch (sign) {
|
|
329
376
|
case '>':
|
|
330
|
-
return this.value >
|
|
377
|
+
return this.value > compareFraction.value;
|
|
331
378
|
case ">=" || "=>" || "geq":
|
|
332
|
-
return this.value >=
|
|
379
|
+
return this.value >= compareFraction.value;
|
|
333
380
|
case "<":
|
|
334
|
-
return this.value <
|
|
381
|
+
return this.value < compareFraction.value;
|
|
335
382
|
case "<=" || "=>" || "leq":
|
|
336
|
-
return this.value <=
|
|
383
|
+
return this.value <= compareFraction.value;
|
|
337
384
|
case "=":
|
|
338
|
-
// let F2: Fraction =
|
|
385
|
+
// let F2: Fraction = compareFraction.clone().reduce(),
|
|
339
386
|
// F1: Fraction = this.clone().reduce();
|
|
340
387
|
// return (F1.numerator === F2.numerator && F1.denominator === F2.denominator);
|
|
341
|
-
return this.value ===
|
|
388
|
+
return this.value === compareFraction.value;
|
|
342
389
|
case "<>":
|
|
343
|
-
return this.value !==
|
|
390
|
+
return this.value !== compareFraction.value;
|
|
344
391
|
default:
|
|
345
392
|
return false;
|
|
346
393
|
}
|
|
347
394
|
};
|
|
348
395
|
/* Compare shortcuts */
|
|
349
|
-
lesser = (than: Fraction): Boolean => {
|
|
396
|
+
lesser = (than: Fraction | number): Boolean => {
|
|
350
397
|
return this.compare(than, '<');
|
|
351
398
|
};
|
|
352
|
-
leq = (than: Fraction): Boolean => {
|
|
399
|
+
leq = (than: Fraction | number): Boolean => {
|
|
353
400
|
return this.compare(than, '<=');
|
|
354
401
|
};
|
|
355
|
-
greater = (than: Fraction): Boolean => {
|
|
402
|
+
greater = (than: Fraction | number): Boolean => {
|
|
356
403
|
return this.compare(than, '>');
|
|
357
404
|
};
|
|
358
|
-
geq = (than: Fraction): Boolean => {
|
|
405
|
+
geq = (than: Fraction | number): Boolean => {
|
|
359
406
|
return this.compare(than, '>=');
|
|
360
407
|
};
|
|
361
|
-
isEqual = (than: Fraction): boolean => {
|
|
408
|
+
isEqual = (than: Fraction | number): boolean => {
|
|
362
409
|
return this.compare(than, '=');
|
|
363
410
|
}
|
|
364
|
-
|
|
411
|
+
isNotEqual = (than: Fraction | number): boolean => {
|
|
365
412
|
return this.compare(than, '<>');
|
|
366
413
|
}
|
|
367
414
|
isOpposed = (p: Fraction): boolean => {
|
|
@@ -373,14 +420,26 @@ export class Fraction {
|
|
|
373
420
|
isZero = (): boolean => {
|
|
374
421
|
return this._numerator === 0;
|
|
375
422
|
}
|
|
423
|
+
isNotZero = (): boolean => {
|
|
424
|
+
return this._numerator !== 0;
|
|
425
|
+
}
|
|
376
426
|
isOne = (): boolean => {
|
|
377
427
|
return this._numerator === 1 && this._denominator === 1;
|
|
378
428
|
}
|
|
429
|
+
isNegativeOne = (): boolean => {
|
|
430
|
+
return this._numerator === -1 && this._denominator === 1;
|
|
431
|
+
}
|
|
379
432
|
isPositive = (): boolean => {
|
|
380
|
-
return this.sign()===1;
|
|
433
|
+
return this.sign() === 1;
|
|
381
434
|
}
|
|
382
435
|
isNegative = (): boolean => {
|
|
383
|
-
return this.sign()
|
|
436
|
+
return this.sign() === -1;
|
|
437
|
+
}
|
|
438
|
+
isStrictlyPositive = (): boolean => {
|
|
439
|
+
return this.value > 0
|
|
440
|
+
}
|
|
441
|
+
isStrictlyNegative = (): Boolean => {
|
|
442
|
+
return this.value < 0
|
|
384
443
|
}
|
|
385
444
|
isNaN = (): boolean => {
|
|
386
445
|
return isNaN(this._numerator);
|
|
@@ -395,7 +454,19 @@ export class Fraction {
|
|
|
395
454
|
return Math.sqrt(this._numerator) % 1 === 0 && Math.sqrt(this._denominator) % 1 === 0
|
|
396
455
|
}
|
|
397
456
|
isReduced = (): boolean => {
|
|
398
|
-
return Math.abs(Numeric.gcd(this._numerator, this._denominator))===1
|
|
457
|
+
return Math.abs(Numeric.gcd(this._numerator, this._denominator)) === 1
|
|
458
|
+
}
|
|
459
|
+
isNatural = (): boolean => {
|
|
460
|
+
return this.clone().reduce().denominator === 1
|
|
461
|
+
}
|
|
462
|
+
isRational = (): boolean => {
|
|
463
|
+
return !this.isNatural()
|
|
464
|
+
}
|
|
465
|
+
isEven = (): boolean => {
|
|
466
|
+
return this.isNatural() && this.value % 2 === 0
|
|
467
|
+
}
|
|
468
|
+
isOdd = (): boolean => {
|
|
469
|
+
return this.isNatural() && this.value % 2 === 1
|
|
399
470
|
}
|
|
400
471
|
sign = (): number => {
|
|
401
472
|
return (this._numerator * this._denominator >= 0) ? 1 : -1;
|
|
@@ -221,7 +221,7 @@ export class Line {
|
|
|
221
221
|
// ------------------------------------------
|
|
222
222
|
isParellelTo = (line: Line): Boolean => {
|
|
223
223
|
// Do they have the isSame direction ?
|
|
224
|
-
return this.slope.isEqual(line.slope) && this.height.
|
|
224
|
+
return this.slope.isEqual(line.slope) && this.height.isNotEqual(line.height);
|
|
225
225
|
}
|
|
226
226
|
isSameAs = (line: Line): Boolean => {
|
|
227
227
|
return this.slope.isEqual(line.slope) && this.height.isEqual(line.height);
|
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
import {rndPolynom} from "./rndPolynom";
|
|
2
2
|
import {rndMonom} from "./rndMonom";
|
|
3
3
|
import {rndHelpers} from "./rndHelpers";
|
|
4
|
-
import {randomMonomConfig, randomPolynomConfig} from "./rndTypes";
|
|
4
|
+
import {randomCoefficientConfig, randomMonomConfig, randomPolynomConfig} from "./rndTypes";
|
|
5
5
|
import {Monom, Polynom} from "../algebra";
|
|
6
|
+
import {Fraction} from "../coefficients";
|
|
7
|
+
import {rndFraction} from "./rndFraction";
|
|
6
8
|
|
|
7
9
|
export * from "./rndTypes"
|
|
8
10
|
export namespace Random {
|
|
9
11
|
export function polynom(config?: randomPolynomConfig): Polynom {
|
|
10
|
-
return new rndPolynom(config).generate()
|
|
12
|
+
return (new rndPolynom(config)).generate()
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export function monom(config?: randomMonomConfig): Monom {
|
|
14
|
-
return new rndMonom(config).generate()
|
|
16
|
+
return (new rndMonom(config)).generate()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function fraction(config?: randomCoefficientConfig): Fraction {
|
|
20
|
+
return (new rndFraction(config)).generate()
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
export function number(from:number, to:number):number { return rndHelpers.randomInt(from,to)}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {randomCore} from "./randomCore";
|
|
2
|
+
import {randomCoefficientConfig, randomMonomConfig, randomPolynomConfig} from "./rndTypes";
|
|
3
|
+
import {Fraction} from "../coefficients";
|
|
4
|
+
import {Random} from "./index";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a random monom based on a based configuration
|
|
8
|
+
*/
|
|
9
|
+
export class rndFraction extends randomCore {
|
|
10
|
+
declare protected _config: randomCoefficientConfig
|
|
11
|
+
declare protected _defaultConfig: randomCoefficientConfig
|
|
12
|
+
|
|
13
|
+
constructor(userConfig?: randomCoefficientConfig) {
|
|
14
|
+
super();
|
|
15
|
+
|
|
16
|
+
this._defaultConfig = {
|
|
17
|
+
negative: true,
|
|
18
|
+
reduced: true,
|
|
19
|
+
zero: true
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
this._config = this.mergeConfig(userConfig, this._defaultConfig)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
generate = (): Fraction => {
|
|
26
|
+
let Q = new Fraction()
|
|
27
|
+
|
|
28
|
+
if(this._config.negative){
|
|
29
|
+
Q.numerator = Random.numberSym(10, this._config.zero)
|
|
30
|
+
}else {
|
|
31
|
+
Q.numerator = Random.number(this._config.zero ? 0 : 1, 10)
|
|
32
|
+
}
|
|
33
|
+
Q.denominator = Random.number(1, 10)
|
|
34
|
+
|
|
35
|
+
return this._config.reduced?Q.reduce():Q
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -8,7 +8,7 @@ import {Monom} from "../algebra/monom";
|
|
|
8
8
|
*/
|
|
9
9
|
export class rndMonom extends randomCore {
|
|
10
10
|
declare protected _config: randomMonomConfig
|
|
11
|
-
declare protected _defaultConfig:
|
|
11
|
+
declare protected _defaultConfig: randomMonomConfig
|
|
12
12
|
|
|
13
13
|
constructor(userConfig?: randomMonomConfig) {
|
|
14
14
|
super();
|
|
@@ -28,7 +28,10 @@ import {Monom} from "../algebra/monom";
|
|
|
28
28
|
let M = new Monom()
|
|
29
29
|
|
|
30
30
|
// Generate the coefficient
|
|
31
|
-
M.coefficient
|
|
31
|
+
M.coefficient = Random.fraction({
|
|
32
|
+
zero: this._config.zero,
|
|
33
|
+
reduced: true
|
|
34
|
+
})
|
|
32
35
|
|
|
33
36
|
// Calculate the degree of the monom
|
|
34
37
|
if (this._config.letters.length > 1) {
|
|
@@ -38,7 +41,7 @@ import {Monom} from "../algebra/monom";
|
|
|
38
41
|
}
|
|
39
42
|
for (let i = 0; i < this._config.degree; i++) {
|
|
40
43
|
const L = Random.item(this._config.letters.split(""))
|
|
41
|
-
M.setLetter(L, M.degree(L)
|
|
44
|
+
M.setLetter(L, M.degree(L).clone().add(1))
|
|
42
45
|
}
|
|
43
46
|
} else {
|
|
44
47
|
M.setLetter(this._config.letters, this._config.degree)
|
|
@@ -2,8 +2,8 @@ import {randomCore} from "./randomCore";
|
|
|
2
2
|
import {randomPolynomConfig} from "./rndTypes";
|
|
3
3
|
import {rndMonom} from "./rndMonom";
|
|
4
4
|
import {Random} from "./index";
|
|
5
|
-
import {Polynom} from "../algebra
|
|
6
|
-
import {Monom} from "../algebra
|
|
5
|
+
import {Polynom} from "../algebra";
|
|
6
|
+
import {Monom} from "../algebra";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Random polynoms
|
|
@@ -32,10 +32,13 @@ export class rndPolynom extends randomCore {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
generate = (): Polynom => {
|
|
35
|
+
if(this._config.factorable && this._config.degree>1){
|
|
36
|
+
return this.factorable()
|
|
37
|
+
}
|
|
38
|
+
|
|
35
39
|
// Create the polynom
|
|
36
40
|
let P = new Polynom().empty(),
|
|
37
41
|
M: Monom
|
|
38
|
-
|
|
39
42
|
for (let i = this._config.degree; i >= 0; i--) {
|
|
40
43
|
// Create monom of corresponding degree.
|
|
41
44
|
M = new rndMonom({
|
|
@@ -56,14 +59,21 @@ export class rndPolynom extends randomCore {
|
|
|
56
59
|
|
|
57
60
|
// If the number of monoms is greater than the allowed value, remove some of them...
|
|
58
61
|
if (this._config.numberOfMonoms > 0 && this._config.numberOfMonoms < P.length) {
|
|
59
|
-
console.log(P.monoms.length)
|
|
60
62
|
P.monoms = Random.array(P.monoms, this._config.numberOfMonoms)
|
|
61
63
|
}
|
|
62
64
|
return P
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
factorable = (): Polynom => {
|
|
66
|
-
let P = new Polynom()
|
|
68
|
+
let P = new Polynom().one()
|
|
69
|
+
|
|
70
|
+
let _factorableConfig = {...this._config}
|
|
71
|
+
_factorableConfig.degree = 1
|
|
72
|
+
_factorableConfig.factorable = false
|
|
73
|
+
|
|
74
|
+
for(let i=0; i<this._config.degree;i++){
|
|
75
|
+
P.multiply(Random.polynom(_factorableConfig))
|
|
76
|
+
}
|
|
67
77
|
|
|
68
78
|
return P
|
|
69
79
|
}
|
package/src/maths/shutingyard.ts
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
import {expect} from 'chai';
|
|
2
|
-
import {Monom} from "../../src/maths/algebra
|
|
2
|
+
import {Monom} from "../../src/maths/algebra";
|
|
3
3
|
import {Random} from "../../src/maths/random";
|
|
4
|
+
import exp = require("constants");
|
|
4
5
|
|
|
5
|
-
describe('Monom
|
|
6
|
-
it('
|
|
6
|
+
describe('Monom tests', ()=> {
|
|
7
|
+
it('parsing', ()=>{
|
|
8
|
+
const M0a = new Monom('3');
|
|
9
|
+
const M0b = new Monom('x');
|
|
10
|
+
const M1 = new Monom('3x^5');
|
|
11
|
+
const M2 = new Monom('2/3x^2yz^3y^4')
|
|
12
|
+
const M3 = new Monom('-3x^(-2)')
|
|
13
|
+
const M4 = new Monom('3x^(2/3)')
|
|
14
|
+
const M5 = new Monom('-3x^(-2/3)y^(-5)8x^3')
|
|
15
|
+
|
|
16
|
+
expect(M0a.tex).to.be.equal('3')
|
|
17
|
+
expect(M0b.tex).to.be.equal('x')
|
|
18
|
+
expect(M1.tex).to.be.equal('3x^{5}')
|
|
19
|
+
expect(M2.display).to.be.equal('2/3x^2y^5z^3')
|
|
20
|
+
expect(M3.tex).to.be.equal('-3x^{-2}')
|
|
21
|
+
expect(M4.tex).to.be.equal('3x^{2/3}')
|
|
22
|
+
expect(M5.tex).to.be.equal('-24x^{7/3}y^{-5}')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('derivative', () => { // the single test
|
|
7
26
|
const options = new Monom('7x^3'); // this will be your class
|
|
8
|
-
|
|
27
|
+
|
|
28
|
+
expect(options.tex).to.be.equal('7x^{3}')
|
|
29
|
+
expect(options.derivative().tex).to.be.equal('21x^{2}');
|
|
9
30
|
});
|
|
10
31
|
|
|
11
32
|
it('integrate', () => { // the single test
|
|
@@ -13,15 +34,15 @@ describe('Monom derivate and integrate', () => { // the tests container
|
|
|
13
34
|
expect(options.primitive().display).to.be.equal('7/4x^4');
|
|
14
35
|
});
|
|
15
36
|
|
|
16
|
-
it('
|
|
37
|
+
it('randomize', function () {
|
|
17
38
|
const M = Random.monom({
|
|
18
39
|
letters: 'xyz',
|
|
19
40
|
degree: 5,
|
|
20
41
|
fraction: false,
|
|
21
|
-
|
|
42
|
+
zero: false
|
|
22
43
|
});
|
|
23
44
|
|
|
24
45
|
expect(M.coefficient.isZero()).to.be.false
|
|
25
|
-
expect(M.degree()).to.be.greaterThan(0)
|
|
46
|
+
expect(M.degree().value).to.be.greaterThan(0)
|
|
26
47
|
});
|
|
27
|
-
})
|
|
48
|
+
})
|
|
@@ -3,54 +3,45 @@ import {Fraction} from "../../src/maths/coefficients";
|
|
|
3
3
|
import {Polynom} from "../../src/maths/algebra";
|
|
4
4
|
import {Random} from "../../src/maths/random";
|
|
5
5
|
|
|
6
|
-
describe('Polynom
|
|
7
|
-
it('
|
|
8
|
-
const options = new Polynom('x^2-
|
|
9
|
-
|
|
6
|
+
describe('Polynom tests', () => {
|
|
7
|
+
it('Parse polynom', () => {
|
|
8
|
+
const options = new Polynom('2x(x+3)^2(x-1)');
|
|
9
|
+
options.reorder().reduce();
|
|
10
|
+
expect(options.tex).to.be.equal('2x^{4}+10x^{3}+6x^{2}-18x');
|
|
10
11
|
});
|
|
11
12
|
|
|
12
|
-
it('
|
|
13
|
-
const options = new Polynom('
|
|
14
|
-
options.
|
|
15
|
-
expect(options.tex).to.be.equal('2x^4+10x^3+6x^2-18x');
|
|
13
|
+
it('Tex display', () => {
|
|
14
|
+
const options = new Polynom('x^2-2x+1');
|
|
15
|
+
expect(options.tex).to.be.equal('x^{2}-2x+1');
|
|
16
16
|
});
|
|
17
17
|
|
|
18
|
-
it('
|
|
18
|
+
it('Evaluate a polynom', function () {
|
|
19
19
|
const P = new Polynom('2x-3')
|
|
20
20
|
|
|
21
21
|
expect(P.evaluate(5).value).to.be.equal(7)
|
|
22
22
|
expect(P.evaluate(new Fraction('5/3')).display).to.be.equal('1/3')
|
|
23
23
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
describe('Polynom operation', () => {
|
|
27
|
-
it('Polynom are equals', () => {
|
|
24
|
+
it('Compare: equals', () => {
|
|
28
25
|
let F = new Polynom('x+3'),
|
|
29
26
|
Q = new Polynom('3+x'),
|
|
30
27
|
P = new Polynom('x-3');
|
|
31
28
|
expect(F.isEqual(Q)).to.be.true;
|
|
32
29
|
expect(F.isEqual(P)).to.be.false;
|
|
33
30
|
})
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
describe('Polynom integration', () => {
|
|
37
|
-
it('Polynom integrate', () => {
|
|
31
|
+
it('Integrate', () => {
|
|
38
32
|
let F = new Polynom('2x^3-3x^2+x-3'),
|
|
39
33
|
G = new Polynom('3/5x^2+4')
|
|
40
34
|
|
|
41
35
|
expect(F.integrate(0, 2).value).to.be.equal(-4)
|
|
42
36
|
expect(G.integrate(-3, 3).display).to.be.equal('174/5')
|
|
43
37
|
})
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
describe('Polynom random creation', ()=>{
|
|
47
|
-
it('should create a random Polynom of degree 5', function () {
|
|
38
|
+
it('Random Polynom of degree 5', function () {
|
|
48
39
|
let P = Random.polynom({
|
|
49
40
|
degree: 6,
|
|
50
41
|
numberOfMonoms: 3
|
|
51
42
|
})
|
|
52
43
|
|
|
53
44
|
expect(P.length).to.be.equal(3)
|
|
54
|
-
expect(P.degree()).to.be.lessThanOrEqual(6)
|
|
45
|
+
expect(P.degree().value).to.be.lessThanOrEqual(6)
|
|
55
46
|
});
|
|
56
47
|
})
|
|
@@ -1,39 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {Fraction} from "../../src/maths/coefficients
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
expect(F.isReduced()).to.be.true
|
|
37
|
-
expect(Q.isReduced()).to.be.false
|
|
38
|
-
})
|
|
1
|
+
import {expect} from "chai";
|
|
2
|
+
import {Fraction} from "../../src/maths/coefficients";
|
|
3
|
+
|
|
4
|
+
describe('Fraction tests', () => { // the tests container
|
|
5
|
+
|
|
6
|
+
it('Tex display', () => { // the single test
|
|
7
|
+
const options = new Fraction(2, 5); // this will be your class
|
|
8
|
+
expect(options.frac).to.be.equal('\\frac{ 2 }{ 5 }');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('Compare: equals', () => {
|
|
12
|
+
let F = new Fraction(1, 3),
|
|
13
|
+
Q = new Fraction(2, 6),
|
|
14
|
+
P = new Fraction(2, 5);
|
|
15
|
+
expect(F.isEqual(Q)).to.be.true;
|
|
16
|
+
expect(F.isEqual(P)).to.be.false;
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('Operation: sum of two fraction', () => {
|
|
20
|
+
let F = new Fraction(1, 3),
|
|
21
|
+
Q = new Fraction(2, 7);
|
|
22
|
+
|
|
23
|
+
F.add(Q);
|
|
24
|
+
|
|
25
|
+
expect(F.numerator).to.be.equal(13);
|
|
26
|
+
expect(F.denominator).to.be.equal(21);
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('Reduced', () => {
|
|
30
|
+
let F = new Fraction(2, 5),
|
|
31
|
+
Q = new Fraction(2, 6)
|
|
32
|
+
|
|
33
|
+
expect(F.isReduced()).to.be.true
|
|
34
|
+
expect(Q.isReduced()).to.be.false
|
|
35
|
+
})
|
|
39
36
|
})
|
|
@@ -19,7 +19,6 @@ describe('Shuting yard', () => { // the tests container
|
|
|
19
19
|
expect(SY3b.rpn.map(x=>x.token)).to.have.all.members(['3/2', 'x', '5', '+', '*'])
|
|
20
20
|
expect(SY4.rpn.map(x=>x.token)).to.have.all.members(['3/2', 'x', '2', '^', '*', '5', 'x', '*', 'y', '*', '-', '12', '-'])
|
|
21
21
|
expect(SY5.rpn.map(x=>x.token)).to.have.all.members(['3/2', 'x', '0', '3', '-', '^', '*', '5', 'x', '*', 'y', '*', '-', '12', '-'])
|
|
22
|
-
|
|
23
22
|
expect(SY6.rpn.map(x=>x.token)).to.have.all.members(['x', '3', '^', 'y', '2', '^', '*', 'z', '*'])
|
|
24
23
|
})
|
|
25
24
|
|