pimath 0.0.131 → 0.0.133
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/main.d.ts +39 -1
- package/package.json +2 -3
- package/dist/maths/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/pimath.ts +0 -40
- 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
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Vector module contains everything necessary to handle 2d or 3d vectors.
|
|
3
|
-
* @module Vector
|
|
4
|
-
*/
|
|
5
|
-
import {Fraction} from "../coefficients/fraction";
|
|
6
|
-
import {Numeric} from "../numeric.ts";
|
|
7
|
-
import {Point} from "./point";
|
|
8
|
-
|
|
9
|
-
export class Vector {
|
|
10
|
-
private _x: Fraction; // 1st component
|
|
11
|
-
private _y: Fraction; // 2nd component
|
|
12
|
-
|
|
13
|
-
constructor(...values: unknown[]) {
|
|
14
|
-
this._x = new Fraction().zero();
|
|
15
|
-
this._y = new Fraction().zero();
|
|
16
|
-
|
|
17
|
-
if (values !== undefined) {
|
|
18
|
-
this.parse(...values);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
// ------------------------------------------
|
|
23
|
-
// Getter and setter
|
|
24
|
-
// ------------------------------------------
|
|
25
|
-
get x(): Fraction {
|
|
26
|
-
return this._x;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
set x(value: Fraction | number | string) {
|
|
30
|
-
this._x = new Fraction(value);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get y(): Fraction {
|
|
34
|
-
return this._y;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
set y(value: Fraction | number | string) {
|
|
38
|
-
this._y = new Fraction(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get normSquare(): Fraction {
|
|
42
|
-
return this._x.clone().pow(2).add(this._y.clone().pow(2));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get norm(): number {
|
|
46
|
-
return Math.sqrt(this.normSquare.value);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get tex(): string {
|
|
50
|
-
return `\\begin{pmatrix}${this._x.tex} \\\\\ ${this._y.tex} \\end{pmatrix}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get asPoint(): Point {
|
|
54
|
-
return new Point(this.x, this.y)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// ------------------------------------------
|
|
58
|
-
// Creation / parsing functions
|
|
59
|
-
// ------------------------------------------
|
|
60
|
-
|
|
61
|
-
get isNull(): boolean {
|
|
62
|
-
return this.x.isZero() && this.y.isZero()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static scalarProduct = (v1: Vector, v2: Vector): Fraction => {
|
|
66
|
-
return v1.x.clone().multiply(v2.x).add(v1.y.clone().multiply(v2.y));
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
static determinant = (v1: Vector, v2: Vector): Fraction => {
|
|
70
|
-
return v1.x.clone().multiply(v2.y).subtract(v1.y.clone().multiply(v2.x))
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
parse = (...values: any): Vector => {
|
|
74
|
-
// TODO: Must be more strict about what is given and limit to two dimensional vectors.p
|
|
75
|
-
// Maybe more than one value was given...
|
|
76
|
-
// Initialize the vector
|
|
77
|
-
this.zero();
|
|
78
|
-
|
|
79
|
-
if (values.length === 0) {
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (values.length === 1) {
|
|
84
|
-
if (values[0] instanceof Vector) {
|
|
85
|
-
return values[0].clone()
|
|
86
|
-
} else {
|
|
87
|
-
return this._parseString(values[0])
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (values.length >= 2) {
|
|
92
|
-
// Two points are given - skip the third value.
|
|
93
|
-
if (values[0] instanceof Point && values[1] instanceof Point) {
|
|
94
|
-
this._x = values[1].x.clone().subtract(values[0].x)
|
|
95
|
-
this._y = values[1].y.clone().subtract(values[0].y)
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Fractions or a number are give
|
|
100
|
-
if (values[0] instanceof Fraction || !isNaN(values[0])) {
|
|
101
|
-
this._x = new Fraction(values[0])
|
|
102
|
-
}
|
|
103
|
-
if (values[1] instanceof Fraction || !isNaN(values[1])) {
|
|
104
|
-
this._y = new Fraction(values[1])
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
(typeof values[0] === 'object' && !isNaN(values[0].x) && !isNaN(values[0].x)) &&
|
|
109
|
-
(typeof values[1] === 'object' && !isNaN(values[1].x) && !isNaN(values[1].x))
|
|
110
|
-
) {
|
|
111
|
-
this._x = new Fraction(+values[1].x - values[0].x)
|
|
112
|
-
this._y = new Fraction(+values[1].y - values[0].y)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return this;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
clone = (): Vector => {
|
|
120
|
-
let V = new Vector();
|
|
121
|
-
|
|
122
|
-
if (this._x !== null) {
|
|
123
|
-
V.x = this._x.clone();
|
|
124
|
-
}
|
|
125
|
-
if (this._y !== null) {
|
|
126
|
-
V.y = this._y.clone();
|
|
127
|
-
}
|
|
128
|
-
return V;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
reset = (): Vector => {
|
|
132
|
-
this._x = null;
|
|
133
|
-
this._y = null;
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// ------------------------------------------
|
|
138
|
-
// Mathematical operations
|
|
139
|
-
|
|
140
|
-
zero = (): Vector => {
|
|
141
|
-
this.reset();
|
|
142
|
-
this._x = new Fraction(null);
|
|
143
|
-
this._y = new Fraction(null);
|
|
144
|
-
return this;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
one = (): Vector => {
|
|
148
|
-
this._x = new Fraction();
|
|
149
|
-
this._y = new Fraction();
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// ------------------------------------------
|
|
154
|
-
opposed = (): Vector => {
|
|
155
|
-
this._x.opposed();
|
|
156
|
-
this._y.opposed();
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
add = (V: Vector): Vector => {
|
|
161
|
-
this._x.add(V.x);
|
|
162
|
-
this._y.add(V.y);
|
|
163
|
-
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
subtract = (V: Vector): Vector => {
|
|
168
|
-
return this.add(V.clone().opposed());
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
scalarProductWithVector = (V: Vector): Fraction => {
|
|
172
|
-
return Vector.scalarProduct(this, V)
|
|
173
|
-
// return this._x.clone().multiply(V.x).add(this._y.clone().multiply(V.y));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
determinantWithVector = (V: Vector): Fraction => {
|
|
177
|
-
return Vector.determinant(this, V)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
normal = (): Vector => {
|
|
181
|
-
let x = this.x.clone().opposed(), y = this.y.clone();
|
|
182
|
-
this._x = y;
|
|
183
|
-
this._y = x;
|
|
184
|
-
return this;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
isColinearTo = (v: Vector): boolean => {
|
|
188
|
-
return this.determinantWithVector(v).isZero()
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
isNormalTo = (v: Vector): boolean => {
|
|
192
|
-
return this.scalarProductWithVector(v).isZero()
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
multiplyByScalar = (k: any): Vector => {
|
|
196
|
-
let scalar = new Fraction(k);
|
|
197
|
-
this._x.multiply(scalar);
|
|
198
|
-
this._y.multiply(scalar);
|
|
199
|
-
return this;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
divideByScalar = (k: any): Vector => {
|
|
203
|
-
return this.multiplyByScalar(new Fraction(k).invert());
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
simplify = (): Vector => {
|
|
207
|
-
// Multiply by the lcm of denominators.
|
|
208
|
-
return this.multiplyByScalar(Numeric.lcm(this._x.denominator, this._y.denominator))
|
|
209
|
-
.divideByScalar(Numeric.gcd(this._x.numerator, this._y.numerator));
|
|
210
|
-
}
|
|
211
|
-
// ------------------------------------------
|
|
212
|
-
// Vector functions
|
|
213
|
-
// ------------------------------------------
|
|
214
|
-
|
|
215
|
-
simplifyDirection = (): Vector => {
|
|
216
|
-
let lcm = Numeric.lcm(this.x.denominator, this.y.denominator),
|
|
217
|
-
gcd = Numeric.gcd(this.x.numerator, this.y.numerator);
|
|
218
|
-
|
|
219
|
-
this.x.multiply(lcm).divide(gcd);
|
|
220
|
-
this.y.multiply(lcm).divide(gcd);
|
|
221
|
-
return this
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
angleWith = (V: Vector, sharp?: Boolean, radian?: Boolean): number => {
|
|
225
|
-
let scalar = this.scalarProductWithVector(V).value,
|
|
226
|
-
toDegree = radian ? 1 : 180 / Math.PI;
|
|
227
|
-
if (sharp) {
|
|
228
|
-
scalar = Math.abs(scalar);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return toDegree * Math.acos(scalar / (this.norm * V.norm));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private _parseString = (value: string): Vector => {
|
|
235
|
-
// Split comma, semi colon or single space.
|
|
236
|
-
let components = value.split(/[,;\s]/g);
|
|
237
|
-
|
|
238
|
-
// Validate the fraction values.
|
|
239
|
-
this.x = new Fraction(components[0] || null);
|
|
240
|
-
this.y = new Fraction(components[1] || null);
|
|
241
|
-
return this;
|
|
242
|
-
};
|
|
243
|
-
}
|
package/lib/maths/numeric.ts
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
export class Numeric {
|
|
2
|
-
static round(value: number, decimals: number = 2): number {
|
|
3
|
-
return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals);
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Get the list of the nth first prime numbers.
|
|
8
|
-
* @param nb : number of primes to choose from
|
|
9
|
-
*/
|
|
10
|
-
static primes(nb?: number): number[] {
|
|
11
|
-
let primesValues: number[] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973];
|
|
12
|
-
if (nb === undefined) {
|
|
13
|
-
return primesValues;
|
|
14
|
-
} else {
|
|
15
|
-
return primesValues.slice(0, Math.min(primesValues.length, nb));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Get the list of all dividers of a number.
|
|
21
|
-
* @param value
|
|
22
|
-
*/
|
|
23
|
-
static dividers(value: number): number[] {
|
|
24
|
-
let D: number[];
|
|
25
|
-
const maxV = Math.sqrt(Math.abs(value));
|
|
26
|
-
|
|
27
|
-
// Initialize the list of dividers.
|
|
28
|
-
D = [];
|
|
29
|
-
|
|
30
|
-
for (let i = 1; i <= maxV; i++) {
|
|
31
|
-
if (value % i === 0) {
|
|
32
|
-
D.push(i);
|
|
33
|
-
D.push(value / i);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Order numbers.
|
|
38
|
-
D.sort(function (a, b) {
|
|
39
|
-
return a - b;
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Make sure the array of value is unique.
|
|
43
|
-
return [...new Set(D)];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Great Common Divisor
|
|
48
|
-
* @param values : number values
|
|
49
|
-
*/
|
|
50
|
-
static gcd(...values: number[]): number {
|
|
51
|
-
// Define the gcd for two number
|
|
52
|
-
let gcd2 = function (a: number, b: number): number {
|
|
53
|
-
if (b === 0) {
|
|
54
|
-
return a;
|
|
55
|
-
}
|
|
56
|
-
return gcd2(b, a % b);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
let g: number = 1,
|
|
60
|
-
i: number = 2;
|
|
61
|
-
|
|
62
|
-
// Nothing is given
|
|
63
|
-
if (values.length === 0) {
|
|
64
|
-
return 1;
|
|
65
|
-
}
|
|
66
|
-
// Only one number is given
|
|
67
|
-
if (values.length === 1) {
|
|
68
|
-
// The first number is zero
|
|
69
|
-
if (values[0] === 0) {
|
|
70
|
-
return 1;
|
|
71
|
-
}
|
|
72
|
-
// Return the number
|
|
73
|
-
return values[0];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// We have at least 2 numbers.
|
|
77
|
-
g = gcd2(values[0], values[1]);
|
|
78
|
-
|
|
79
|
-
// The gcd of the two first value is one ? It's already finished.
|
|
80
|
-
if (g === 1) {
|
|
81
|
-
return 1;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// The current gcd isn't one. Continue with all next values.
|
|
85
|
-
for (i = 2; i < values.length; i++) {
|
|
86
|
-
g = gcd2(g, values[i]);
|
|
87
|
-
// Escape if gcd is already one.
|
|
88
|
-
if (g === 1) {
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return Math.abs(g);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
static divideNumbersByGCD(...values: number[]): number[] {
|
|
97
|
-
let gcd = Numeric.gcd(...values)
|
|
98
|
-
|
|
99
|
-
return values.map(x => x / gcd)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Least Common Multiple
|
|
104
|
-
* @param values: list of numbers
|
|
105
|
-
*/
|
|
106
|
-
static lcm(...values: number[]): number {
|
|
107
|
-
return values.reduce(function (a, b) {
|
|
108
|
-
return Math.abs(a * b / Numeric.gcd(a, b));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static pythagoricianTripletsWithTarget(target: number, targetIsSquare?: boolean): number[][] {
|
|
113
|
-
// méthode inverse, à partir du triplet.
|
|
114
|
-
const triplets = [],
|
|
115
|
-
targetValue = targetIsSquare === true ? +target : target ** 2
|
|
116
|
-
for (let u = 0; u <= target; u++) {
|
|
117
|
-
for (let v = 0; v <= target; v++) {
|
|
118
|
-
if (u ** 2 + v ** 2 === targetValue) {
|
|
119
|
-
triplets.push([u, v, target])
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return triplets
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
static numberCorrection(value: number, epsilonDigit: number = 1, epsilonNumberOfDigits: number = 10, number_of_digits: number = 8) {
|
|
128
|
-
return +value.toFixed(number_of_digits)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
static periodic(value: number): number {
|
|
132
|
-
if (Number.isSafeInteger(value)) {
|
|
133
|
-
return 0
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Assume it's with decimal.
|
|
137
|
-
let decimal = (value.toString()).split('.')[0]
|
|
138
|
-
|
|
139
|
-
// The decimal part is limited
|
|
140
|
-
if (decimal.length < 10) {
|
|
141
|
-
return 0
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Find the periodic if it exists.
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
static decompose(value: number): number[][] {
|
|
148
|
-
let dividers = Numeric.dividers(value),
|
|
149
|
-
limit = Math.sqrt(value),
|
|
150
|
-
arr = [],
|
|
151
|
-
u, v
|
|
152
|
-
|
|
153
|
-
while (dividers.length > 0) {
|
|
154
|
-
u = dividers.shift()
|
|
155
|
-
v = dividers.length > 0 ? dividers.pop() : +u
|
|
156
|
-
|
|
157
|
-
arr.push([u, v])
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return arr
|
|
161
|
-
}
|
|
162
|
-
}
|
package/lib/maths/numexp.ts
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import {Shutingyard, ShutingyardMode, ShutingyardType, tokenConstant} from "./shutingyard";
|
|
2
|
-
import {Fraction} from "./coefficients/fraction";
|
|
3
|
-
|
|
4
|
-
export class NumExp {
|
|
5
|
-
private _rpn: { token: string, tokenType: string }[]
|
|
6
|
-
private _expression: string
|
|
7
|
-
private _isValid: boolean
|
|
8
|
-
|
|
9
|
-
constructor(value: string, uniformize?: boolean) {
|
|
10
|
-
this._expression = value
|
|
11
|
-
try {
|
|
12
|
-
this._rpn = new Shutingyard(ShutingyardMode.NUMERIC).parse(value, uniformize || uniformize === undefined).rpn
|
|
13
|
-
} catch (e) {
|
|
14
|
-
this._rpn = null
|
|
15
|
-
this._isValid = false
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
get rpn(): { token: string; tokenType: string }[] {
|
|
20
|
-
return this._rpn;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get isValid(): boolean {
|
|
24
|
-
if (this._isValid === undefined) {
|
|
25
|
-
try {
|
|
26
|
-
const v = this.evaluate({x: 0})
|
|
27
|
-
} catch {
|
|
28
|
-
this._isValid = false
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return this._isValid
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set isValid(value: boolean) {
|
|
35
|
-
this._isValid = value
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get expression(): string {
|
|
39
|
-
return this._expression;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
evaluate(values?: { [Key: string]: number }): number {
|
|
43
|
-
const stack: number[] = []
|
|
44
|
-
|
|
45
|
-
if (this._rpn === null) {
|
|
46
|
-
this._isValid = false
|
|
47
|
-
return 0
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this.isValid = true
|
|
51
|
-
|
|
52
|
-
for (const element of this._rpn) {
|
|
53
|
-
if (element.tokenType === ShutingyardType.COEFFICIENT) {
|
|
54
|
-
// May be a numeric value or a Fraction.
|
|
55
|
-
if (!isNaN(+element.token)) {
|
|
56
|
-
this._addToStack(stack, +element.token)
|
|
57
|
-
} else {
|
|
58
|
-
this._addToStack(stack, new Fraction(element.token).value)
|
|
59
|
-
}
|
|
60
|
-
} else if (element.tokenType === ShutingyardType.VARIABLE) {
|
|
61
|
-
if (values[element.token] !== undefined) {
|
|
62
|
-
this._addToStack(stack, +values[element.token])
|
|
63
|
-
}
|
|
64
|
-
} else if (element.tokenType === ShutingyardType.CONSTANT) {
|
|
65
|
-
this._addToStack(stack, tokenConstant[element.token])
|
|
66
|
-
} else if (element.tokenType === ShutingyardType.OPERATION) {
|
|
67
|
-
if (element.token === '*') {
|
|
68
|
-
const b = stack.pop(),
|
|
69
|
-
a = stack.pop()
|
|
70
|
-
if (a === undefined || b === undefined) {
|
|
71
|
-
this.isValid = false
|
|
72
|
-
}
|
|
73
|
-
this._addToStack(stack, a * b)
|
|
74
|
-
} else if (element.token === '/') {
|
|
75
|
-
const b = stack.pop(),
|
|
76
|
-
a = stack.pop()
|
|
77
|
-
if (a === undefined || b === undefined) {
|
|
78
|
-
this.isValid = false
|
|
79
|
-
}
|
|
80
|
-
this._addToStack(stack, a / b)
|
|
81
|
-
} else if (element.token === '+') {
|
|
82
|
-
const b = stack.pop(),
|
|
83
|
-
a = stack.pop()
|
|
84
|
-
if (a === undefined || b === undefined) {
|
|
85
|
-
this.isValid = false
|
|
86
|
-
}
|
|
87
|
-
this._addToStack(stack, (+a) + (+b))
|
|
88
|
-
} else if (element.token === '-') {
|
|
89
|
-
const b = stack.pop(),
|
|
90
|
-
a = stack.pop() || 0
|
|
91
|
-
if (b === undefined) {
|
|
92
|
-
this.isValid = false
|
|
93
|
-
}
|
|
94
|
-
this._addToStack(stack, a - b)
|
|
95
|
-
} else if (element.token === '^') {
|
|
96
|
-
const b = stack.pop(),
|
|
97
|
-
a = stack.pop()
|
|
98
|
-
if (a === undefined || b === undefined) {
|
|
99
|
-
this.isValid = false
|
|
100
|
-
}
|
|
101
|
-
this._addToStack(stack, Math.pow(a, b))
|
|
102
|
-
}
|
|
103
|
-
} else if (element.tokenType === ShutingyardType.FUNCTION) {
|
|
104
|
-
const a = stack.pop()
|
|
105
|
-
if (a === undefined) {
|
|
106
|
-
this.isValid = false
|
|
107
|
-
}
|
|
108
|
-
if (element.token === 'sin') {
|
|
109
|
-
this._addToStack(stack, Math.sin(a))
|
|
110
|
-
} else if (element.token === 'cos') {
|
|
111
|
-
this._addToStack(stack, Math.cos(a))
|
|
112
|
-
} else if (element.token === 'tan') {
|
|
113
|
-
this._addToStack(stack, Math.tan(a))
|
|
114
|
-
} else if (element.token === 'sqrt') {
|
|
115
|
-
this._addToStack(stack, Math.sqrt(a))
|
|
116
|
-
} else if (element.token === 'nthrt') {
|
|
117
|
-
// TODO: support nthrt in num. exp.
|
|
118
|
-
let b = stack.pop()
|
|
119
|
-
if (a % 2 === 0 && b < 0) {
|
|
120
|
-
this._addToStack(stack, NaN)
|
|
121
|
-
} else {
|
|
122
|
-
this._addToStack(stack, (b < 0 ? -1 : 1) * Math.pow(Math.abs(b), 1 / a))
|
|
123
|
-
}
|
|
124
|
-
} else if (element.token === 'ln') {
|
|
125
|
-
this._addToStack(stack, Math.log(a))
|
|
126
|
-
} else if (element.token === 'log') {
|
|
127
|
-
this._addToStack(stack, Math.log10(a))
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (stack.length === 1) {
|
|
133
|
-
return stack[0]
|
|
134
|
-
} else {
|
|
135
|
-
throw `There was a problem parsing: ${this._expression}`
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private _extractDecimalPart(value: number): string {
|
|
140
|
-
let decimal = value.toString()
|
|
141
|
-
|
|
142
|
-
if (!decimal.includes('.')) {
|
|
143
|
-
return ''
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
decimal = decimal.split('.')[1]
|
|
147
|
-
|
|
148
|
-
return decimal.substring(0, decimal.length - 2)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
private _numberCorrection(value: number): number {
|
|
152
|
-
// Must modify the number if it's like:
|
|
153
|
-
// a: 3.0000000000000003
|
|
154
|
-
// b: 3.9999999999999994
|
|
155
|
-
// remove the last character
|
|
156
|
-
// check if around n last characters are either 0 or 9
|
|
157
|
-
// if it is, 'round' the number.
|
|
158
|
-
|
|
159
|
-
const epsilon = 0.00000000000001,
|
|
160
|
-
number_of_digits = 6
|
|
161
|
-
|
|
162
|
-
const decimal = this._extractDecimalPart(value)
|
|
163
|
-
if (decimal === '') {
|
|
164
|
-
return value
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const n9 = decimal.match(/9+$/g)
|
|
168
|
-
const n0 = decimal.match(/0+$/g)
|
|
169
|
-
|
|
170
|
-
if (n9 && n9[0].length >= number_of_digits) {
|
|
171
|
-
// New tested values.
|
|
172
|
-
const mod = this._extractDecimalPart(value + epsilon),
|
|
173
|
-
mod0 = mod.match(/0+$/g)
|
|
174
|
-
|
|
175
|
-
if (mod0 && mod0[0].length >= number_of_digits) {
|
|
176
|
-
// The value can be changed. Remove all zeros!
|
|
177
|
-
return +((value + epsilon).toString().split(mod0[0])[0])
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (n0 && n0[0].length >= number_of_digits) {
|
|
182
|
-
// New tested values.
|
|
183
|
-
const mod = this._extractDecimalPart(value - epsilon),
|
|
184
|
-
mod9 = mod.match(/9+$/g)
|
|
185
|
-
|
|
186
|
-
if (mod9 && mod9[0].length >= number_of_digits) {
|
|
187
|
-
// The value can be changed. Remove all nines!
|
|
188
|
-
return +(value.toString().split(n0[0])[0])
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return value
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private _addToStack(stack: number[], value: number): void {
|
|
196
|
-
stack.push(this._numberCorrection(value))
|
|
197
|
-
}
|
|
198
|
-
}
|
package/lib/maths/pimath.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// Expose as global
|
|
2
|
-
import {Shutingyard} from "./shutingyard.ts";
|
|
3
|
-
import {Numeric} from "./numeric.ts";
|
|
4
|
-
import {NumExp} from "./numexp.ts";
|
|
5
|
-
import {Fraction} from "./coefficients/fraction.ts";
|
|
6
|
-
import {NthRoot} from "./coefficients/nthRoot.ts";
|
|
7
|
-
import {Monom} from "./algebra/monom.ts";
|
|
8
|
-
import {Polynom} from "./algebra/polynom.ts";
|
|
9
|
-
import {Equation} from "./algebra/equation.ts";
|
|
10
|
-
import {LinearSystem} from "./algebra/linearSystem.ts";
|
|
11
|
-
import {Rational} from "./algebra/rational.ts";
|
|
12
|
-
import {Logicalset} from "./algebra/logicalset.ts";
|
|
13
|
-
import {Random} from "./randomization/random.ts";
|
|
14
|
-
import {Vector} from "./geometry/vector.ts";
|
|
15
|
-
import {Point} from "./geometry/point.ts";
|
|
16
|
-
import {Line} from "./geometry/line.ts";
|
|
17
|
-
import {Triangle} from "./geometry/triangle.ts";
|
|
18
|
-
import {Circle} from "./geometry/circle.ts";
|
|
19
|
-
|
|
20
|
-
export const PiMath = {
|
|
21
|
-
ShutingYard: Shutingyard,
|
|
22
|
-
Numeric: Numeric,
|
|
23
|
-
NumExp: NumExp,
|
|
24
|
-
Fraction: Fraction,
|
|
25
|
-
Root: NthRoot,
|
|
26
|
-
Monom: Monom,
|
|
27
|
-
Polynom: Polynom,
|
|
28
|
-
Equation: Equation,
|
|
29
|
-
LinearSystem: LinearSystem,
|
|
30
|
-
Rational: Rational,
|
|
31
|
-
Logicalset: Logicalset,
|
|
32
|
-
Random: Random,
|
|
33
|
-
Geometry: {
|
|
34
|
-
Vector: Vector,
|
|
35
|
-
Point: Point,
|
|
36
|
-
Line: Line,
|
|
37
|
-
Triangle: Triangle,
|
|
38
|
-
Circle: Circle
|
|
39
|
-
}
|
|
40
|
-
};
|