pimath 0.0.14 → 0.0.18
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 +37 -3
- package/dev/pi.js +901 -721
- package/dev/pi.js.map +1 -1
- package/dist/pi.js +1 -1
- package/dist/pi.js.map +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 -20
- package/esm/maths/algebra/index.js +18 -33
- 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 +15 -18
- package/esm/maths/algebra/monom.js +182 -84
- package/esm/maths/algebra/monom.js.map +1 -1
- package/esm/maths/algebra/polynom.d.ts +6 -27
- package/esm/maths/algebra/polynom.js +62 -218
- 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.d.ts +12 -0
- package/esm/maths/geometry/circle.js +79 -13
- package/esm/maths/geometry/circle.js.map +1 -1
- package/esm/maths/geometry/index.d.ts +0 -17
- package/esm/maths/geometry/index.js +17 -28
- package/esm/maths/geometry/index.js.map +1 -1
- package/esm/maths/geometry/line.d.ts +18 -4
- package/esm/maths/geometry/line.js +109 -49
- package/esm/maths/geometry/line.js.map +1 -1
- package/esm/maths/geometry/point.d.ts +1 -0
- package/esm/maths/geometry/point.js +22 -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.d.ts +0 -1
- package/esm/maths/geometry/vector.js +22 -21
- 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 +8 -7
- package/src/maths/algebra/index.ts +1 -17
- package/src/maths/algebra/linearSystem.ts +3 -3
- package/src/maths/algebra/monom.ts +764 -612
- package/src/maths/algebra/monom_bck.backup +746 -0
- package/src/maths/algebra/polynom.ts +977 -1176
- package/src/maths/algebra/rational.ts +6 -6
- package/src/maths/coefficients/fraction.ts +98 -27
- package/src/maths/geometry/circle.ts +106 -19
- package/src/maths/geometry/index.ts +0 -14
- package/src/maths/geometry/line.ts +145 -58
- package/src/maths/geometry/point.ts +9 -0
- package/src/maths/geometry/vector.ts +1 -5
- package/src/maths/random/index.ts +7 -1
- 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 +47 -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 +2 -2
- package/tsconfig.testing.json +28 -0
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
* This class works for 2d line in a plane.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {Fraction} from "../coefficients
|
|
5
|
+
import {Fraction} from "../coefficients";
|
|
6
6
|
import {Vector} from "./vector";
|
|
7
7
|
import {Point} from "./point";
|
|
8
|
-
import {Polynom} from "../algebra
|
|
8
|
+
import {Equation, Polynom} from "../algebra";
|
|
9
9
|
import {Numeric} from "../numeric";
|
|
10
|
-
|
|
10
|
+
|
|
11
|
+
enum LinePropriety {
|
|
12
|
+
None,
|
|
13
|
+
Parallel,
|
|
14
|
+
Perpendicular
|
|
15
|
+
}
|
|
11
16
|
|
|
12
17
|
export class Line {
|
|
13
18
|
// A line is defined as the canonical form
|
|
@@ -20,19 +25,30 @@ export class Line {
|
|
|
20
25
|
private _n: Vector;
|
|
21
26
|
private _exists: boolean
|
|
22
27
|
|
|
28
|
+
private _referencePropriety: LinePropriety
|
|
29
|
+
private _referenceLine: Line
|
|
30
|
+
|
|
31
|
+
static PERPENDICULAR = LinePropriety.Perpendicular
|
|
32
|
+
static PARALLEL = LinePropriety.Parallel
|
|
33
|
+
|
|
23
34
|
constructor(...values: any) {
|
|
24
35
|
|
|
25
36
|
this._exists = false;
|
|
26
37
|
|
|
27
|
-
if (values
|
|
38
|
+
if (values.length > 0) {
|
|
28
39
|
this.parse(...values);
|
|
29
40
|
}
|
|
30
41
|
|
|
31
42
|
return this;
|
|
32
43
|
}
|
|
33
44
|
|
|
34
|
-
get isLine():boolean {
|
|
35
|
-
|
|
45
|
+
get isLine(): boolean {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get exists(): boolean {
|
|
50
|
+
return this._exists;
|
|
51
|
+
}
|
|
36
52
|
|
|
37
53
|
// ------------------------------------------
|
|
38
54
|
// Getter and setter
|
|
@@ -40,6 +56,7 @@ export class Line {
|
|
|
40
56
|
get equation(): Equation {
|
|
41
57
|
return new Equation(new Polynom().parse('xy', this._a, this._b, this._c), new Polynom('0')).simplify();
|
|
42
58
|
}
|
|
59
|
+
|
|
43
60
|
get tex(): { canonical: string, mxh: string, parametric: string } {
|
|
44
61
|
// canonical => ax + by + c = 0
|
|
45
62
|
// mxh => y = -a/b x - c/b
|
|
@@ -47,7 +64,7 @@ export class Line {
|
|
|
47
64
|
|
|
48
65
|
let canonical = this.equation;
|
|
49
66
|
// Make sur the first item is positive.
|
|
50
|
-
if(this._a.isNegative()){
|
|
67
|
+
if (this._a.isNegative()) {
|
|
51
68
|
canonical.multiply(-1);
|
|
52
69
|
}
|
|
53
70
|
|
|
@@ -102,6 +119,10 @@ export class Line {
|
|
|
102
119
|
return new Vector(this._a, this._b);
|
|
103
120
|
}
|
|
104
121
|
|
|
122
|
+
get director(): Vector {
|
|
123
|
+
return this._d.clone()
|
|
124
|
+
}
|
|
125
|
+
|
|
105
126
|
set d(value: Vector) {
|
|
106
127
|
this._d = value;
|
|
107
128
|
}
|
|
@@ -117,61 +138,96 @@ export class Line {
|
|
|
117
138
|
// ------------------------------------------
|
|
118
139
|
// Creation / parsing functions
|
|
119
140
|
// ------------------------------------------
|
|
120
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Parse data to a line
|
|
143
|
+
* @param {any} values
|
|
144
|
+
* @returns {Line}
|
|
145
|
+
*/
|
|
146
|
+
parse = (...values: unknown[]): Line => {
|
|
121
147
|
this._exists = false;
|
|
122
148
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
149
|
+
// Nothing is given...
|
|
150
|
+
if (values.length === 0) {
|
|
151
|
+
return this
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// One value only: already a line (clone it), an Equation, a string (as Equation)
|
|
155
|
+
if (values.length === 1) {
|
|
156
|
+
if (values[0] instanceof Line) {
|
|
157
|
+
// Already a Line
|
|
158
|
+
return values[0].clone()
|
|
159
|
+
} else if (values[0] instanceof Equation) {
|
|
160
|
+
// It's an Equation
|
|
161
|
+
return this.parseEquation(values[0])
|
|
162
|
+
} else if (typeof values[0] === "string") {
|
|
163
|
+
// It's a string - create an Equation from it.
|
|
164
|
+
try {
|
|
165
|
+
let E = new Equation(values[0])
|
|
166
|
+
return this.parse(E)
|
|
167
|
+
} catch (e) {
|
|
168
|
+
return this
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (values.length === 2) {
|
|
174
|
+
if (values[0] instanceof Point && values[1] instanceof Vector) {
|
|
127
175
|
return this.parseByPointAndVector(values[0], values[1]);
|
|
128
|
-
} else if (values[0]
|
|
176
|
+
} else if (values[0] instanceof Point && values[1] instanceof Point) {
|
|
129
177
|
return this.parseByPointAndVector(values[0], new Vector(values[0], values[1]));
|
|
178
|
+
} else if (values[0] instanceof Vector && values[1] instanceof Point) {
|
|
179
|
+
return this.parseByPointAndNormal(values[1], values[0])
|
|
130
180
|
}
|
|
131
|
-
}
|
|
132
|
-
// It's a already a line - clone it !
|
|
133
|
-
if(values[0].isLine){
|
|
134
|
-
return values[0].clone();
|
|
135
|
-
}
|
|
181
|
+
}
|
|
136
182
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
183
|
+
if (values.length === 3) {
|
|
184
|
+
if (
|
|
185
|
+
(values[0] instanceof Fraction || typeof values[0] === 'number')
|
|
186
|
+
&&
|
|
187
|
+
(values[1] instanceof Fraction || typeof values[1] === 'number')
|
|
188
|
+
&&
|
|
189
|
+
(values[2] instanceof Fraction || typeof values[2] === 'number')
|
|
190
|
+
) {
|
|
191
|
+
return this.parseByCoefficient(values[0], values[1], values[2]);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
142
194
|
|
|
143
|
-
|
|
144
|
-
|
|
195
|
+
// TODO: Add the ability to create line from a normal vector
|
|
196
|
+
console.log('Someting wrong happend while creating the line')
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
145
199
|
|
|
146
|
-
|
|
147
|
-
|
|
200
|
+
parseEquation = (equ: Equation): Line => {
|
|
201
|
+
// Reorder the eequation
|
|
202
|
+
equ.reorder(true)
|
|
148
203
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if(letters.has(elem)){
|
|
152
|
-
letters.delete(elem)}
|
|
153
|
-
}
|
|
204
|
+
// It must contain either x, y or both.
|
|
205
|
+
let letters = new Set(equ.letters());
|
|
154
206
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
207
|
+
// No 'x', no 'y' in the equations
|
|
208
|
+
if (!(letters.has('x') || letters.has('y'))) {
|
|
209
|
+
return this
|
|
210
|
+
}
|
|
159
211
|
|
|
160
|
-
|
|
161
|
-
|
|
212
|
+
// Another letter in the equation ?
|
|
213
|
+
for (let elem of ['x', 'y']) {
|
|
214
|
+
if (letters.has(elem)) {
|
|
215
|
+
letters.delete(elem)
|
|
162
216
|
}
|
|
163
217
|
}
|
|
164
|
-
// TODO: Add the ability to create line from a normal vector
|
|
165
|
-
console.log('Someting wrong happend while creating the line')
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
218
|
|
|
169
|
-
|
|
219
|
+
if (letters.size > 0) {
|
|
220
|
+
return this
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Everything should be ok now...
|
|
224
|
+
return this.parseByCoefficient(equ.left.monomByLetter('x').coefficient, equ.left.monomByLetter('y').coefficient, equ.left.monomByDegree(0).coefficient)
|
|
225
|
+
}
|
|
226
|
+
parseByCoefficient = (a: Fraction | number, b: Fraction | number, c: Fraction | number): Line => {
|
|
170
227
|
this._a = new Fraction(a);
|
|
171
228
|
this._b = new Fraction(b);
|
|
172
229
|
this._c = new Fraction(c);
|
|
173
230
|
|
|
174
|
-
// TODO: initialize direction and reference point
|
|
175
231
|
this._d = new Vector(this._b.clone(), this._a.clone().opposed());
|
|
176
232
|
this._OA = new Point(new Fraction().zero(), this._c.clone());
|
|
177
233
|
this._n = this._d.clone().normal();
|
|
@@ -205,6 +261,31 @@ export class Line {
|
|
|
205
261
|
return this;
|
|
206
262
|
}
|
|
207
263
|
|
|
264
|
+
parseByPointAndNormal = (P: Point, n: Vector): Line => {
|
|
265
|
+
return this.parseByCoefficient(
|
|
266
|
+
n.x,
|
|
267
|
+
n.y,
|
|
268
|
+
P.x.clone().multiply(n.x)
|
|
269
|
+
.add(P.y.clone().multiply(n.y)).opposed()
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
parseByPointAndLine = (P: Point, L: Line, orientation?: LinePropriety): Line => {
|
|
274
|
+
|
|
275
|
+
if (orientation === undefined) {
|
|
276
|
+
orientation = LinePropriety.Parallel
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (orientation === LinePropriety.Parallel) {
|
|
280
|
+
return this.parseByPointAndNormal(P, L.normal)
|
|
281
|
+
} else if (orientation === LinePropriety.Perpendicular) {
|
|
282
|
+
return this.parseByPointAndNormal(P, L.director)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
this._exists = false
|
|
286
|
+
return this
|
|
287
|
+
}
|
|
288
|
+
|
|
208
289
|
clone = (): Line => {
|
|
209
290
|
this._a = this._a.clone();
|
|
210
291
|
this._b = this._b.clone();
|
|
@@ -214,6 +295,7 @@ export class Line {
|
|
|
214
295
|
this._OA = this._OA.clone();
|
|
215
296
|
this._n = this._n.clone();
|
|
216
297
|
|
|
298
|
+
this._exists = this.exists
|
|
217
299
|
return this;
|
|
218
300
|
}
|
|
219
301
|
// ------------------------------------------
|
|
@@ -221,7 +303,7 @@ export class Line {
|
|
|
221
303
|
// ------------------------------------------
|
|
222
304
|
isParellelTo = (line: Line): Boolean => {
|
|
223
305
|
// Do they have the isSame direction ?
|
|
224
|
-
return this.slope.isEqual(line.slope) && this.height.
|
|
306
|
+
return this.slope.isEqual(line.slope) && this.height.isNotEqual(line.height);
|
|
225
307
|
}
|
|
226
308
|
isSameAs = (line: Line): Boolean => {
|
|
227
309
|
return this.slope.isEqual(line.slope) && this.height.isEqual(line.height);
|
|
@@ -320,7 +402,7 @@ export class Line {
|
|
|
320
402
|
)
|
|
321
403
|
|
|
322
404
|
// There is an intersection point
|
|
323
|
-
if(iPt.hasIntersection) {
|
|
405
|
+
if (iPt.hasIntersection) {
|
|
324
406
|
return iPt.point.x.value >= Math.min(A.x.value, B.x.value)
|
|
325
407
|
&& iPt.point.x.value <= Math.max(A.x.value, B.x.value)
|
|
326
408
|
&& iPt.point.y.value >= Math.min(A.y.value, B.y.value)
|
|
@@ -328,36 +410,41 @@ export class Line {
|
|
|
328
410
|
}
|
|
329
411
|
return false;
|
|
330
412
|
}
|
|
413
|
+
|
|
331
414
|
// ------------------------------------------
|
|
332
415
|
// Special functions
|
|
333
416
|
// ------------------------------------------
|
|
334
|
-
canonicalAsFloatCoefficient(decimals: number): string{
|
|
335
|
-
if(decimals===undefined){
|
|
417
|
+
canonicalAsFloatCoefficient(decimals: number): string {
|
|
418
|
+
if (decimals === undefined) {
|
|
336
419
|
decimals = 2;
|
|
337
420
|
}
|
|
338
421
|
|
|
339
422
|
let ca = this._a.value,
|
|
340
423
|
cb = this._b.value,
|
|
341
|
-
cc= this._c.value,
|
|
424
|
+
cc = this._c.value,
|
|
342
425
|
canonical = '';
|
|
343
426
|
|
|
344
|
-
if(!this._a.isZero()){
|
|
345
|
-
if(this._a.isOne()){
|
|
427
|
+
if (!this._a.isZero()) {
|
|
428
|
+
if (this._a.isOne()) {
|
|
346
429
|
canonical = 'x'
|
|
347
|
-
}else if(this._a.clone().opposed().isOne()){
|
|
430
|
+
} else if (this._a.clone().opposed().isOne()) {
|
|
348
431
|
canonical = '-x'
|
|
349
|
-
}else{
|
|
350
|
-
canonical = this._a.value.toFixed(decimals)+'x'
|
|
432
|
+
} else {
|
|
433
|
+
canonical = this._a.value.toFixed(decimals) + 'x'
|
|
351
434
|
}
|
|
352
435
|
}
|
|
353
436
|
|
|
354
|
-
if(!this._b.isZero()){
|
|
355
|
-
if(this._b.isPositive()){
|
|
437
|
+
if (!this._b.isZero()) {
|
|
438
|
+
if (this._b.isPositive()) {
|
|
439
|
+
canonical += '+'
|
|
440
|
+
}
|
|
356
441
|
canonical += this._b.value.toFixed(decimals) + 'y'
|
|
357
442
|
}
|
|
358
443
|
|
|
359
|
-
if(!this._c.isZero()){
|
|
360
|
-
if(this._c.isPositive()){
|
|
444
|
+
if (!this._c.isZero()) {
|
|
445
|
+
if (this._c.isPositive()) {
|
|
446
|
+
canonical += '+'
|
|
447
|
+
}
|
|
361
448
|
canonical += this._c.value.toFixed(decimals)
|
|
362
449
|
}
|
|
363
450
|
|
|
@@ -53,6 +53,15 @@ export class Point {
|
|
|
53
53
|
return `\\left(${pts.join(';')}\\right)`
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
get display(): string {
|
|
57
|
+
let pts = [];
|
|
58
|
+
|
|
59
|
+
pts.push(this._x.tex);
|
|
60
|
+
pts.push(this._y.tex);
|
|
61
|
+
|
|
62
|
+
return `(${pts.join(';')})`
|
|
63
|
+
}
|
|
64
|
+
|
|
56
65
|
// ------------------------------------------
|
|
57
66
|
// Creation / parsing functions
|
|
58
67
|
// ------------------------------------------
|
|
@@ -18,10 +18,6 @@ export class Vector {
|
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
get isVector() {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
21
|
// ------------------------------------------
|
|
26
22
|
// Getter and setter
|
|
27
23
|
// ------------------------------------------
|
|
@@ -68,7 +64,7 @@ export class Vector {
|
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
if (values.length === 1) {
|
|
71
|
-
if (values[0]
|
|
67
|
+
if (values[0] instanceof Vector) {
|
|
72
68
|
return values[0].clone()
|
|
73
69
|
} else {
|
|
74
70
|
return this._parseString(values[0])
|
|
@@ -1,8 +1,10 @@
|
|
|
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 {
|
|
@@ -14,6 +16,10 @@ export namespace Random {
|
|
|
14
16
|
return (new rndMonom(config)).generate()
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
export function fraction(config?: randomCoefficientConfig): Fraction {
|
|
20
|
+
return (new rndFraction(config)).generate()
|
|
21
|
+
}
|
|
22
|
+
|
|
17
23
|
export function number(from:number, to:number):number { return rndHelpers.randomInt(from,to)}
|
|
18
24
|
export function numberSym(max:number, allowZero?:boolean):number { return rndHelpers.randomIntSym(max, allowZero)}
|
|
19
25
|
export function bool(percent?:number):boolean { return rndHelpers.randomBool(percent)}
|
|
@@ -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,50 @@
|
|
|
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
|
+
expect(M0a.tex).to.be.equal('3')
|
|
10
|
+
|
|
11
|
+
const M0b = new Monom('x');
|
|
12
|
+
expect(M0b.tex).to.be.equal('x')
|
|
13
|
+
|
|
14
|
+
const M0c = new Monom('3x');
|
|
15
|
+
expect(M0c.tex).to.be.equal('3x')
|
|
16
|
+
|
|
17
|
+
const M1 = new Monom('3x^5');
|
|
18
|
+
expect(M1.tex).to.be.equal('3x^{5}')
|
|
19
|
+
|
|
20
|
+
const M2 = new Monom('2/3x^2yz^3y^4')
|
|
21
|
+
expect(M2.display).to.be.equal('2/3x^2y^5z^3')
|
|
22
|
+
|
|
23
|
+
const M3 = new Monom('-3x^(-2)')
|
|
24
|
+
expect(M3.tex).to.be.equal('-3x^{-2}')
|
|
25
|
+
|
|
26
|
+
const M4 = new Monom('3x^(2/3)')
|
|
27
|
+
expect(M4.tex).to.be.equal('3x^{2/3}')
|
|
28
|
+
|
|
29
|
+
const M5 = new Monom('-3x^(-2/3)y^(-5)8x^3')
|
|
30
|
+
expect(M5.tex).to.be.equal('-24x^{7/3}y^{-5}')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('basic operations', ()=>{
|
|
34
|
+
const M1 = new Monom('3x'),
|
|
35
|
+
M2 = new Monom('2x')
|
|
36
|
+
|
|
37
|
+
expect(M1.clone().add(M2).isEqual(new Monom('5x'))).to.be.true
|
|
38
|
+
expect(M1.clone().subtract(M2).isEqual(new Monom('x'))).to.be.true
|
|
39
|
+
expect(M1.clone().multiply(M2).isEqual(new Monom('6x^2'))).to.be.true
|
|
40
|
+
expect(M1.clone().divide(M2).isEqual(new Monom('3/2'))).to.be.true
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('derivative', () => { // the single test
|
|
7
44
|
const options = new Monom('7x^3'); // this will be your class
|
|
8
|
-
|
|
45
|
+
|
|
46
|
+
expect(options.tex).to.be.equal('7x^{3}')
|
|
47
|
+
expect(options.derivative().tex).to.be.equal('21x^{2}');
|
|
9
48
|
});
|
|
10
49
|
|
|
11
50
|
it('integrate', () => { // the single test
|
|
@@ -13,15 +52,15 @@ describe('Monom derivate and integrate', () => { // the tests container
|
|
|
13
52
|
expect(options.primitive().display).to.be.equal('7/4x^4');
|
|
14
53
|
});
|
|
15
54
|
|
|
16
|
-
it('
|
|
55
|
+
it('randomize', function () {
|
|
17
56
|
const M = Random.monom({
|
|
18
57
|
letters: 'xyz',
|
|
19
58
|
degree: 5,
|
|
20
59
|
fraction: false,
|
|
21
|
-
|
|
60
|
+
zero: false
|
|
22
61
|
});
|
|
23
62
|
|
|
24
63
|
expect(M.coefficient.isZero()).to.be.false
|
|
25
|
-
expect(M.degree()).to.be.greaterThan(0)
|
|
64
|
+
expect(M.degree().value).to.be.greaterThan(0)
|
|
26
65
|
});
|
|
27
|
-
})
|
|
66
|
+
})
|
|
@@ -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
|
})
|