pimath 0.0.16 → 0.0.20
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 +39 -9
- package/dev/pi.js +883 -806
- 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 +3 -1
- package/esm/maths/algebra/equation.js +32 -25
- package/esm/maths/algebra/equation.js.map +1 -1
- package/esm/maths/algebra/index.js +18 -6
- 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 +5 -26
- package/esm/maths/algebra/polynom.js +64 -220
- 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 +16 -1
- package/esm/maths/geometry/circle.js +95 -14
- 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.d.ts +20 -4
- package/esm/maths/geometry/line.js +123 -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.js +14 -11
- 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 +13 -7
- 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 -1174
- package/src/maths/algebra/rational.ts +6 -6
- package/src/maths/coefficients/fraction.ts +98 -27
- package/src/maths/geometry/circle.ts +133 -21
- package/src/maths/geometry/line.ts +162 -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 +0 -1
- 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
|
@@ -3,744 +3,896 @@
|
|
|
3
3
|
* Defined as coefficient * literal
|
|
4
4
|
* Examples: 3x^2, 3/5x^2, ...
|
|
5
5
|
*/
|
|
6
|
-
import {Fraction} from "../coefficients
|
|
6
|
+
import {Fraction} from "../coefficients";
|
|
7
7
|
import {Numeric} from "../numeric";
|
|
8
|
+
import {Shutingyard} from "../shutingyard";
|
|
9
|
+
|
|
10
|
+
export type literalType = {
|
|
11
|
+
[Key: string]: Fraction
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class Monom {
|
|
15
|
+
private _coefficient: Fraction;
|
|
16
|
+
private _literal: literalType;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create the monom object.
|
|
20
|
+
* @param value (optional) string
|
|
21
|
+
*/
|
|
22
|
+
constructor(value?: string) {
|
|
23
|
+
this.zero();
|
|
24
|
+
|
|
25
|
+
if (value !== undefined) {
|
|
26
|
+
// A string is given - try to parse the value.
|
|
27
|
+
this.parse(value);
|
|
28
|
+
}
|
|
8
29
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
private _literal: { [Key: string]: number };
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Create the monom object.
|
|
15
|
-
* @param value (optional) string
|
|
16
|
-
*/
|
|
17
|
-
constructor(value?: string) {
|
|
18
|
-
this.zero();
|
|
19
|
-
|
|
20
|
-
if (value !== undefined) {
|
|
21
|
-
// A string is given - try to parse the value.
|
|
22
|
-
this.parse(value);
|
|
23
|
-
}
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
24
32
|
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
// ------------------------------------------
|
|
34
|
+
// Getter and setter
|
|
35
|
+
// ------------------------------------------
|
|
36
|
+
/**
|
|
37
|
+
* Get the coefficient as fraction
|
|
38
|
+
*/
|
|
39
|
+
get coefficient(): Fraction {
|
|
40
|
+
return this._coefficient;
|
|
41
|
+
}
|
|
27
42
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Set the coefficient value of the monom
|
|
45
|
+
* @param F Fraction
|
|
46
|
+
*/
|
|
47
|
+
set coefficient(F: Fraction) {
|
|
48
|
+
this._coefficient = F;
|
|
49
|
+
}
|
|
34
50
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Get the literal part, as dictionary
|
|
53
|
+
*/
|
|
54
|
+
get literal(): literalType {
|
|
55
|
+
return this._literal;
|
|
56
|
+
}
|
|
41
57
|
|
|
42
|
-
|
|
43
|
-
* Set the coefficient value of the monom
|
|
44
|
-
* @param F Fraction
|
|
45
|
-
*/
|
|
46
|
-
set coefficient(F: Fraction) {
|
|
47
|
-
this._coefficient = F;
|
|
48
|
-
}
|
|
58
|
+
get literalSqrt(): literalType {
|
|
49
59
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
if (this.isLiteralSquare()) {
|
|
61
|
+
let L: literalType = {}
|
|
62
|
+
for (let key in this._literal) {
|
|
63
|
+
L[key] = this._literal[key].clone().sqrt()
|
|
64
|
+
}
|
|
65
|
+
return L;
|
|
66
|
+
} else {
|
|
54
67
|
return this._literal;
|
|
55
68
|
}
|
|
69
|
+
}
|
|
56
70
|
|
|
57
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Set the literal part of the monom
|
|
73
|
+
* @param L Literal part as dictionary: <setLetter: exposant>
|
|
74
|
+
*/
|
|
75
|
+
set literal(L: literalType) {
|
|
76
|
+
this._literal = L;
|
|
77
|
+
}
|
|
58
78
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Set the literal part of the monom from a string
|
|
81
|
+
* @param inputStr String like x^2y^3
|
|
82
|
+
*/
|
|
83
|
+
set literalStr(inputStr: string) {
|
|
84
|
+
// TODO : parse using shutingyard tree !
|
|
85
|
+
|
|
86
|
+
// Match all x^n
|
|
87
|
+
for (const v of [...inputStr.matchAll(/([a-z])\^([+-]?[0-9]+)/g)]) {
|
|
88
|
+
// Create the default letter entry if necessary.
|
|
89
|
+
if (!(v[1] in this._literal)) {
|
|
90
|
+
this._literal[v[1]] = new Fraction().zero();
|
|
67
91
|
}
|
|
68
|
-
}
|
|
69
92
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*/
|
|
74
|
-
set literal(L: { [Key: string]: number }) {
|
|
75
|
-
this._literal = L;
|
|
93
|
+
// Add the new value.
|
|
94
|
+
// TODO: actually, it adds only numeric value
|
|
95
|
+
this._literal[v[1]].add(+v[2]);
|
|
76
96
|
}
|
|
77
97
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// Match all x^n
|
|
84
|
-
for (const v of [...inputStr.matchAll(/([a-z])\^([+-]?[0-9]+)/g)]) {
|
|
85
|
-
if (!(v[1] in this._literal)) {
|
|
86
|
-
this._literal[v[1]] = 0;
|
|
87
|
-
}
|
|
88
|
-
this._literal[v[1]] += +v[2];
|
|
89
|
-
}
|
|
90
|
-
for (const v of [...inputStr.matchAll(/([a-z](?!\^))/g)]) {
|
|
91
|
-
// Match all single letters
|
|
92
|
-
if (!(v[1] in this._literal)) {
|
|
93
|
-
this._literal[v[1]] = 0;
|
|
94
|
-
}
|
|
95
|
-
this._literal[v[1]] += 1;
|
|
98
|
+
// Match all x
|
|
99
|
+
for (const v of [...inputStr.matchAll(/([a-z](?!\^))/g)]) {
|
|
100
|
+
// Match all single letters
|
|
101
|
+
if (!(v[1] in this._literal)) {
|
|
102
|
+
this._literal[v[1]] = new Fraction().zero();
|
|
96
103
|
}
|
|
97
|
-
}
|
|
98
104
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
* Get the variables letters
|
|
102
|
-
*/
|
|
103
|
-
get variables(): string[] {
|
|
104
|
-
this.clone().clean();
|
|
105
|
-
return Object.keys(this._literal)
|
|
105
|
+
// Add one to the value.
|
|
106
|
+
this._literal[v[1]].add(1)
|
|
106
107
|
}
|
|
108
|
+
}
|
|
107
109
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
110
|
+
// Getter helpers.
|
|
111
|
+
/**
|
|
112
|
+
* Get the variables letters
|
|
113
|
+
*/
|
|
114
|
+
get variables(): string[] {
|
|
115
|
+
this.clone().clean();
|
|
116
|
+
return Object.keys(this._literal)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Display getter
|
|
120
|
+
/**
|
|
121
|
+
* This display getter is to be used in the polynom display getter
|
|
122
|
+
*/
|
|
123
|
+
get display(): string {
|
|
124
|
+
let L: string = '',
|
|
125
|
+
letters = Object.keys(this._literal).sort()
|
|
126
|
+
for (let letter of letters) {
|
|
127
|
+
if (this._literal[letter].isNotZero()) {
|
|
128
|
+
L += `${letter}`;
|
|
129
|
+
if (this._literal[letter].isNotEqual(1)) {
|
|
130
|
+
L += `^${this._literal[letter].display}`;
|
|
120
131
|
}
|
|
121
132
|
}
|
|
133
|
+
}
|
|
122
134
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
} else {
|
|
128
|
-
return '';
|
|
129
|
-
}
|
|
135
|
+
if (L === '') {
|
|
136
|
+
// No setLetter - means it's only a number !
|
|
137
|
+
if (this._coefficient.value != 0) {
|
|
138
|
+
return `${this._coefficient.display}`;
|
|
130
139
|
} else {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
+
return '';
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
if (this._coefficient.value === 1) {
|
|
144
|
+
return L;
|
|
145
|
+
} else if (this._coefficient.value === -1) {
|
|
146
|
+
return `-${L}`;
|
|
147
|
+
} else if (this._coefficient.value === 0) {
|
|
148
|
+
return '0';
|
|
149
|
+
} else {
|
|
150
|
+
return `${this._coefficient.display}${L}`;
|
|
140
151
|
}
|
|
141
152
|
}
|
|
153
|
+
}
|
|
142
154
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
155
|
+
get dividers(): Monom[] {
|
|
156
|
+
// Decompose only if the coefficient is a natural number
|
|
157
|
+
if (this.coefficient.denominator !== 1) {
|
|
158
|
+
return [this.clone()]
|
|
159
|
+
}
|
|
160
|
+
// Decompose only if the power values are natural numbers.
|
|
161
|
+
if (this.hasFractionCoefficient) {
|
|
162
|
+
return [this.clone()]
|
|
163
|
+
}
|
|
148
164
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
165
|
+
// Security : do not do this if greater than 10000
|
|
166
|
+
if (this.coefficient.numerator > 10000) {
|
|
167
|
+
return [this.clone()]
|
|
168
|
+
}
|
|
169
|
+
const dividers = Numeric.dividers(Math.abs(this.coefficient.numerator))
|
|
154
170
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
171
|
+
// Decompose the literals parts.
|
|
172
|
+
let literals: literalType[] = [];
|
|
173
|
+
for (let L in this.literal) {
|
|
174
|
+
// L is the letter.
|
|
175
|
+
literals = this._getLiteralDividers(literals, L)
|
|
176
|
+
}
|
|
161
177
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
let M = new Monom();
|
|
167
|
-
M.coefficient = new Fraction(N)
|
|
168
|
-
M.literal = L
|
|
169
|
-
monomDividers.push(M)
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
} else if (dividers.length === 0) {
|
|
173
|
-
for (let L of litterals) {
|
|
174
|
-
let M = new Monom();
|
|
175
|
-
M.coefficient = new Fraction().one()
|
|
176
|
-
M.literal = L
|
|
177
|
-
monomDividers.push(M)
|
|
178
|
-
}
|
|
179
|
-
} else {
|
|
180
|
-
for (let N of dividers) {
|
|
178
|
+
const monomDividers: Monom[] = [];
|
|
179
|
+
if (literals.length > 0 && dividers.length > 0) {
|
|
180
|
+
for (let N of dividers) {
|
|
181
|
+
for (let L of literals) {
|
|
181
182
|
let M = new Monom();
|
|
182
183
|
M.coefficient = new Fraction(N)
|
|
184
|
+
M.literal = L
|
|
183
185
|
monomDividers.push(M)
|
|
184
186
|
}
|
|
185
187
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
} else if (dividers.length === 0) {
|
|
189
|
+
for (let L of literals) {
|
|
190
|
+
let M = new Monom();
|
|
191
|
+
M.coefficient = new Fraction().one()
|
|
192
|
+
M.literal = L
|
|
193
|
+
monomDividers.push(M)
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
for (let N of dividers) {
|
|
197
|
+
let M = new Monom();
|
|
198
|
+
M.coefficient = new Fraction(N)
|
|
199
|
+
monomDividers.push(M)
|
|
200
|
+
}
|
|
188
201
|
}
|
|
189
202
|
|
|
190
|
-
|
|
191
|
-
|
|
203
|
+
return monomDividers.length === 0 ? [new Monom().one()] : monomDividers;
|
|
204
|
+
}
|
|
192
205
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
+
private _getLiteralDividers(arr: literalType[], letter: string): literalType[] {
|
|
207
|
+
let tmpList: { [key: string]: Fraction }[] = [];
|
|
208
|
+
|
|
209
|
+
// Be default, this.literal[letter] should be a rational number.
|
|
210
|
+
for (let d = 0; d <= this.literal[letter].value; d++) {
|
|
211
|
+
if (arr.length === 0) {
|
|
212
|
+
let litt: literalType = {}
|
|
213
|
+
litt[letter] = new Fraction(d)
|
|
214
|
+
tmpList.push(litt)
|
|
215
|
+
} else {
|
|
216
|
+
for (let item of arr) {
|
|
217
|
+
let litt: literalType = {}
|
|
218
|
+
for (let currentLetter in item) {
|
|
219
|
+
litt[currentLetter] = item[currentLetter]
|
|
206
220
|
}
|
|
221
|
+
litt[letter] = new Fraction(d)
|
|
222
|
+
tmpList.push(litt)
|
|
207
223
|
}
|
|
208
224
|
}
|
|
209
|
-
return tmpList;
|
|
210
225
|
}
|
|
226
|
+
return tmpList;
|
|
227
|
+
}
|
|
211
228
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Display the monom, forcing the '+' sign to appear
|
|
231
|
+
*/
|
|
232
|
+
get displayWithSign(): string {
|
|
233
|
+
// TODO: Rename or remove this getter ?
|
|
234
|
+
let d: String = this.display;
|
|
235
|
+
return (d[0] !== '-' ? '+' : '') + d;
|
|
236
|
+
}
|
|
220
237
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Get the tex output of the monom
|
|
240
|
+
*/
|
|
241
|
+
get tex(): string {
|
|
242
|
+
// TODO: display with square root !
|
|
243
|
+
let L: string = '',
|
|
244
|
+
letters = Object.keys(this._literal).sort()
|
|
245
|
+
|
|
246
|
+
for (let letter of letters) {
|
|
247
|
+
if (this._literal[letter].isNotZero()) {
|
|
248
|
+
L += `${letter}`;
|
|
249
|
+
if (this._literal[letter].isNotEqual(1)) {
|
|
250
|
+
L += `^{${this._literal[letter].display}}`;
|
|
232
251
|
}
|
|
233
252
|
}
|
|
253
|
+
}
|
|
234
254
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
} else {
|
|
240
|
-
return '0';
|
|
241
|
-
}
|
|
255
|
+
if (L === '') {
|
|
256
|
+
// No setLetter - means it's only a number !
|
|
257
|
+
if (this._coefficient.value != 0) {
|
|
258
|
+
return `${this._coefficient.dfrac}`;
|
|
242
259
|
} else {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
260
|
+
return '0';
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
if (this._coefficient.value === 1) {
|
|
264
|
+
return L;
|
|
265
|
+
} else if (this._coefficient.value === -1) {
|
|
266
|
+
return `-${L}`;
|
|
267
|
+
} else if (this._coefficient.value === 0) {
|
|
268
|
+
return '0';
|
|
269
|
+
} else {
|
|
270
|
+
return `${this._coefficient.dfrac}${L}`;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ------------------------------------------
|
|
276
|
+
// Creation / parsing functions
|
|
277
|
+
// -----------------------------------------
|
|
278
|
+
/**
|
|
279
|
+
* Parse a string to a monom. The string may include fraction.
|
|
280
|
+
* @param inputStr
|
|
281
|
+
*/
|
|
282
|
+
parse = (inputStr: string): Monom => {
|
|
283
|
+
this._shutingYardToReducedMonom(inputStr)
|
|
284
|
+
//
|
|
285
|
+
// // Set the literal part.
|
|
286
|
+
// this.literalStr = inputStr;
|
|
287
|
+
//
|
|
288
|
+
// // Get the coefficient
|
|
289
|
+
// this._coefficient = new Fraction();
|
|
290
|
+
// for (const v of [...inputStr.replace(/([a-z])|(\^[+-]?[0-9]+)/g, ',').split(',')]) {
|
|
291
|
+
// // The value is empty.
|
|
292
|
+
// if (v.trim() === '') {
|
|
293
|
+
// continue;
|
|
294
|
+
// }
|
|
295
|
+
//
|
|
296
|
+
// // Multiply the current coefficient by the new found value.
|
|
297
|
+
// this._coefficient.multiply(new Fraction(v.trim()));
|
|
298
|
+
// }
|
|
299
|
+
|
|
300
|
+
return this;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
private _shutingYardToReducedMonom = (inputStr: string): Monom => {
|
|
304
|
+
// Get the RPN array of the current expression
|
|
305
|
+
const SY: Shutingyard = new Shutingyard().parse(inputStr);
|
|
306
|
+
const rpn: { token: string, tokenType: string }[] = SY.rpn;
|
|
307
|
+
|
|
308
|
+
let stack: Monom[] = [], m, pow, letter, q1, q2
|
|
309
|
+
|
|
310
|
+
if (rpn.length === 0) {
|
|
311
|
+
this.zero()
|
|
312
|
+
return this
|
|
313
|
+
} else if (rpn.length === 1) {
|
|
314
|
+
const element = rpn[0]
|
|
315
|
+
|
|
316
|
+
this.one()
|
|
317
|
+
if (element.tokenType === 'coefficient') {
|
|
318
|
+
this.coefficient = new Fraction(element.token)
|
|
319
|
+
} else if (element.tokenType === 'variable') {
|
|
320
|
+
this.setLetter(element.token, 1)
|
|
321
|
+
}
|
|
322
|
+
return this
|
|
323
|
+
} else {
|
|
324
|
+
// Reset the monom
|
|
325
|
+
for (const element of rpn) {
|
|
326
|
+
if (element.tokenType === 'coefficient') {
|
|
327
|
+
let M = new Monom().one()
|
|
328
|
+
M.coefficient = new Fraction(element.token)
|
|
329
|
+
stack.push(M.clone())
|
|
330
|
+
} else if (element.tokenType === 'variable') {
|
|
331
|
+
let M = new Monom().one()
|
|
332
|
+
M.setLetter(element.token, 1)
|
|
333
|
+
stack.push(M.clone())
|
|
334
|
+
} else if (element.tokenType === 'operation') {
|
|
335
|
+
switch (element.token) {
|
|
336
|
+
case '-':
|
|
337
|
+
// this should only happen for negative powers or for negative coefficient.
|
|
338
|
+
q2 = (stack.pop()) || new Monom().zero()
|
|
339
|
+
q1 = (stack.pop()) || new Monom().zero()
|
|
340
|
+
|
|
341
|
+
stack.push(q1.subtract(q2))
|
|
342
|
+
|
|
343
|
+
break;
|
|
344
|
+
case '*':
|
|
345
|
+
// Get the last element in the stack
|
|
346
|
+
q2 = (stack.pop()) || new Monom().one()
|
|
347
|
+
q1 = (stack.pop()) || new Monom().one()
|
|
348
|
+
|
|
349
|
+
stack.push(q1.multiply(q2))
|
|
350
|
+
break
|
|
351
|
+
case '^':
|
|
352
|
+
// get the two last elements in the stack
|
|
353
|
+
pow = (stack.pop().coefficient) || new Fraction().one()
|
|
354
|
+
m = (stack.pop()) || new Monom().one()
|
|
355
|
+
|
|
356
|
+
letter = m.variables[0]
|
|
357
|
+
|
|
358
|
+
if (letter !== undefined) {
|
|
359
|
+
m.setLetter(letter, pow)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
stack.push(m)
|
|
363
|
+
// this.multiply(m.clone())
|
|
364
|
+
break
|
|
365
|
+
}
|
|
251
366
|
}
|
|
252
367
|
}
|
|
253
368
|
}
|
|
254
369
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
this.literalStr = inputStr;
|
|
370
|
+
this.one()
|
|
371
|
+
this.multiply(stack[0])
|
|
372
|
+
return this
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Clone the current Monom.
|
|
376
|
+
*/
|
|
377
|
+
clone = (): Monom => {
|
|
378
|
+
let F: Monom = new Monom();
|
|
265
379
|
|
|
266
|
-
|
|
267
|
-
this._coefficient = new Fraction();
|
|
380
|
+
F.coefficient = this._coefficient.clone();
|
|
268
381
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
382
|
+
// Copy the literal parts.
|
|
383
|
+
for (let k in this._literal) {
|
|
384
|
+
F.setLetter(k, this._literal[k].clone());
|
|
385
|
+
}
|
|
386
|
+
return F;
|
|
387
|
+
};
|
|
274
388
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
*/
|
|
309
|
-
one = (): Monom => {
|
|
310
|
-
this._coefficient = new Fraction().one();
|
|
311
|
-
this._literal = {};
|
|
312
|
-
return this;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Clean the monom by removing each letters with a power of zero.
|
|
317
|
-
*/
|
|
318
|
-
clean = (): Monom => {
|
|
319
|
-
for (let letter in this._literal) {
|
|
320
|
-
if (this._literal[letter] === 0) {
|
|
321
|
-
delete this._literal[letter];
|
|
322
|
-
}
|
|
389
|
+
makeSame = (M: Monom):Monom => {
|
|
390
|
+
// Copy the literal parts.
|
|
391
|
+
for (let k in M._literal) {
|
|
392
|
+
this.setLetter(k, M._literal[k].clone());
|
|
393
|
+
}
|
|
394
|
+
return this
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Create a zero value monom
|
|
399
|
+
*/
|
|
400
|
+
zero = (): Monom => {
|
|
401
|
+
this._coefficient = new Fraction().zero();
|
|
402
|
+
this._literal = {};
|
|
403
|
+
return this;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Create a one value monom
|
|
408
|
+
*/
|
|
409
|
+
one = (): Monom => {
|
|
410
|
+
this._coefficient = new Fraction().one();
|
|
411
|
+
this._literal = {};
|
|
412
|
+
return this;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Clean the monom by removing each letters with a power of zero.
|
|
417
|
+
*/
|
|
418
|
+
clean = (): Monom => {
|
|
419
|
+
for (let letter in this._literal) {
|
|
420
|
+
if (this._literal[letter].isZero()) {
|
|
421
|
+
delete this._literal[letter];
|
|
323
422
|
}
|
|
423
|
+
}
|
|
324
424
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
console.log('Add: Is not similar: ', m.display);
|
|
425
|
+
return this;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// ------------------------------------------
|
|
429
|
+
// Mathematical operations
|
|
430
|
+
// ------------------------------------------
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Get the opposed
|
|
434
|
+
* Returns a monom.
|
|
435
|
+
*/
|
|
436
|
+
opposed = (): Monom => {
|
|
437
|
+
this._coefficient.opposed();
|
|
438
|
+
return this;
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Add all similar monoms. If they aren't similar, they are simply skipped.
|
|
443
|
+
* @param M (Monom[]) The monoms to add.
|
|
444
|
+
*/
|
|
445
|
+
add = (...M: Monom[]): Monom => {
|
|
446
|
+
for (let m of M) {
|
|
447
|
+
if (this.isSameAs(m)) {
|
|
448
|
+
if(this.isZero()){
|
|
449
|
+
this.makeSame(m)
|
|
351
450
|
}
|
|
451
|
+
this._coefficient.add(m.coefficient);
|
|
452
|
+
} else {
|
|
453
|
+
console.log('Add: Is not similar: ', m.display);
|
|
352
454
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
455
|
+
}
|
|
456
|
+
return this;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Subtract multiple monoms
|
|
461
|
+
* @param M (Monom[]) The monoms to subtract
|
|
462
|
+
*/
|
|
463
|
+
subtract = (...M: Monom[]): Monom => {
|
|
464
|
+
for (let m of M) {
|
|
465
|
+
if (this.isSameAs(m)) {
|
|
466
|
+
if(this.isZero()){
|
|
467
|
+
this.makeSame(m)
|
|
366
468
|
}
|
|
469
|
+
this._coefficient.add(m.clone().coefficient.opposed());
|
|
470
|
+
} else {
|
|
471
|
+
console.log('Subtract: Is not similar: ', m.display);
|
|
367
472
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
473
|
+
}
|
|
474
|
+
return this;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Multiple multiple monoms to the current monom
|
|
479
|
+
* @param M (Monom[]) The monoms to multiply to.
|
|
480
|
+
*/
|
|
481
|
+
multiply = (...M: Monom[]): Monom => {
|
|
482
|
+
for (let m of M) {
|
|
483
|
+
// Multiply the coefficient.
|
|
484
|
+
this._coefficient.multiply(m.coefficient);
|
|
485
|
+
|
|
486
|
+
// Multiply the literal parts.
|
|
487
|
+
for (let letter in m.literal) {
|
|
488
|
+
if (this._literal[letter] === undefined) {
|
|
489
|
+
this._literal[letter] = m.literal[letter].clone()
|
|
490
|
+
} else {
|
|
491
|
+
this._literal[letter].add(m.literal[letter])
|
|
382
492
|
}
|
|
383
|
-
}
|
|
384
|
-
return this;
|
|
385
|
-
};
|
|
386
493
|
|
|
387
|
-
|
|
388
|
-
this._coefficient.multiply(F);
|
|
389
|
-
return this;
|
|
494
|
+
}
|
|
390
495
|
}
|
|
496
|
+
return this;
|
|
497
|
+
};
|
|
391
498
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
divide = (...M: Monom[]): Monom => {
|
|
397
|
-
// Depending on the given value, choose the current item
|
|
398
|
-
for (let v of M) {
|
|
399
|
-
// Divide the coefficient
|
|
400
|
-
this._coefficient.divide(v.coefficient);
|
|
401
|
-
|
|
402
|
-
// Subtract the power values
|
|
403
|
-
for (let letter in v.literal) {
|
|
404
|
-
this._literal[letter] = (this._literal[letter] === undefined) ? -v.literal[letter] : this._literal[letter] - v.literal[letter];
|
|
499
|
+
multiplyByNumber = (F: Fraction | number): Monom => {
|
|
500
|
+
this._coefficient.multiply(F);
|
|
501
|
+
return this;
|
|
502
|
+
}
|
|
405
503
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
504
|
+
/**
|
|
505
|
+
* Divide the current monoms by multiple monoms
|
|
506
|
+
* @param M (Monom[])
|
|
507
|
+
*/
|
|
508
|
+
divide = (...M: Monom[]): Monom => {
|
|
509
|
+
// Depending on the given value, choose the current item
|
|
510
|
+
for (let v of M) {
|
|
511
|
+
// Divide the coefficient
|
|
512
|
+
this._coefficient.divide(v.coefficient);
|
|
513
|
+
|
|
514
|
+
// Subtract the power values
|
|
515
|
+
for (let letter in v.literal) {
|
|
516
|
+
this._literal[letter] = (this._literal[letter] === undefined) ? v.literal[letter].clone().opposed() : this._literal[letter].subtract(v.literal[letter])
|
|
517
|
+
|
|
518
|
+
// If the power of a particular setLetter is zero, delete it from the literal part..
|
|
519
|
+
if (this._literal[letter].isZero()) {
|
|
520
|
+
delete this._literal[letter];
|
|
410
521
|
}
|
|
411
522
|
}
|
|
412
|
-
|
|
413
|
-
|
|
523
|
+
}
|
|
524
|
+
return this;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Get the pow of a monom.
|
|
529
|
+
* @param nb (number) : Mathematical pow
|
|
530
|
+
*/
|
|
531
|
+
pow = (nb: number): Monom => {
|
|
532
|
+
this._coefficient.pow(nb);
|
|
533
|
+
for (let letter in this._literal) {
|
|
534
|
+
this._literal[letter].pow(nb)
|
|
535
|
+
}
|
|
536
|
+
return this;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Get the nth-root of the monom
|
|
541
|
+
* @param p
|
|
542
|
+
*/
|
|
543
|
+
root = (p: number): Monom => {
|
|
544
|
+
// TODO: determiner the nth root of a monom
|
|
545
|
+
return this;
|
|
546
|
+
}
|
|
414
547
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
this._coefficient.
|
|
548
|
+
/**
|
|
549
|
+
* Return the square root of a monom
|
|
550
|
+
*/
|
|
551
|
+
sqrt = (): Monom => {
|
|
552
|
+
if (this.isSquare()) {
|
|
553
|
+
this._coefficient.sqrt();
|
|
421
554
|
for (let letter in this._literal) {
|
|
422
|
-
this._literal[letter]
|
|
423
|
-
}
|
|
424
|
-
return this;
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* Get the nth-root of the monom
|
|
429
|
-
* @param p
|
|
430
|
-
*/
|
|
431
|
-
root = (p: number): Monom => {
|
|
432
|
-
// TODO: determiner the nth root of a monom
|
|
433
|
-
return this;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Return the square root of a monom
|
|
438
|
-
*/
|
|
439
|
-
sqrt = (): Monom => {
|
|
440
|
-
if (this.isSquare()) {
|
|
441
|
-
this._coefficient.sqrt();
|
|
442
|
-
for (let letter in this._literal) {
|
|
443
|
-
this._literal[letter] /= 2;
|
|
444
|
-
}
|
|
555
|
+
this._literal[letter].clone().divide(2)
|
|
445
556
|
}
|
|
446
|
-
return this.root(2);
|
|
447
557
|
}
|
|
558
|
+
return this.root(2);
|
|
559
|
+
}
|
|
448
560
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
561
|
+
// ------------------------------------------
|
|
562
|
+
// Compare functions
|
|
563
|
+
// ------------------------------------------
|
|
564
|
+
compare = (M: Monom, sign?: string): boolean => {
|
|
565
|
+
// TODO: Build the compare systems.
|
|
566
|
+
if (sign === undefined) {
|
|
567
|
+
sign = '=';
|
|
568
|
+
}
|
|
457
569
|
|
|
458
570
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
571
|
+
switch (sign) {
|
|
572
|
+
case '=':
|
|
573
|
+
// To be equal, they must be the isSame
|
|
574
|
+
if (!this.compare(M, 'same')) {
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
465
577
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
578
|
+
// The literal parts are the isSame. The coefficient must be equal
|
|
579
|
+
return this._coefficient.isEqual(M.coefficient);
|
|
580
|
+
case 'same':
|
|
581
|
+
// Get the list of all variables from both monoms.
|
|
582
|
+
let M1: string[] = this.variables,
|
|
583
|
+
M2: string[] = M.variables,
|
|
584
|
+
K: string[] = M1.concat(M2.filter((item) => M1.indexOf(item) < 0));
|
|
473
585
|
|
|
586
|
+
// To compare, both must be different than zero.
|
|
587
|
+
if (!this.isZero() && !M.isZero()) {
|
|
474
588
|
for (let key of K) {
|
|
475
589
|
// The setLetter is not available in one of the monom
|
|
476
590
|
if (this._literal[key] === undefined || M.literal[key] === undefined) {
|
|
477
591
|
return false;
|
|
478
592
|
}
|
|
479
593
|
// The setLetter does not have the isSame power in each monoms.
|
|
480
|
-
if (this._literal[key]
|
|
594
|
+
if (!this._literal[key].isEqual(M.literal[key])) {
|
|
481
595
|
return false;
|
|
482
596
|
}
|
|
483
597
|
}
|
|
598
|
+
}
|
|
484
599
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
600
|
+
// All are positive check - the monoms are the sames.
|
|
601
|
+
return true;
|
|
602
|
+
default:
|
|
603
|
+
return false;
|
|
490
604
|
}
|
|
605
|
+
}
|
|
491
606
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
607
|
+
/**
|
|
608
|
+
* Determine if the monom is null
|
|
609
|
+
*/
|
|
610
|
+
isZero(): boolean {
|
|
611
|
+
return this._coefficient.value === 0;
|
|
612
|
+
}
|
|
498
613
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
614
|
+
/**
|
|
615
|
+
* Determine if the monom is one
|
|
616
|
+
*/
|
|
617
|
+
isOne(): boolean {
|
|
618
|
+
return this._coefficient.value === 1 && this.variables.length === 0;
|
|
619
|
+
}
|
|
505
620
|
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
621
|
+
/**
|
|
622
|
+
* Determine if two monoms are equals
|
|
623
|
+
* @param M
|
|
624
|
+
*/
|
|
625
|
+
isEqual = (M: Monom): boolean => {
|
|
626
|
+
return this.compare(M, '=');
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Determine if two monoms are similar
|
|
631
|
+
* @param M
|
|
632
|
+
*/
|
|
633
|
+
isSameAs = (M: Monom): boolean => {
|
|
634
|
+
return this.compare(M, 'same');
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
isSquare = (): boolean => {
|
|
638
|
+
if (!this.coefficient.isSquare()) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
return this.isLiteralSquare();
|
|
642
|
+
}
|
|
513
643
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
};
|
|
644
|
+
isLiteralSquare = (): boolean => {
|
|
645
|
+
for (let letter in this.literal) {
|
|
646
|
+
// A literal square must have a natural power
|
|
647
|
+
if (this.literal[letter].isRational()) {
|
|
648
|
+
return false
|
|
649
|
+
}
|
|
521
650
|
|
|
522
|
-
|
|
523
|
-
if (
|
|
651
|
+
// The natural power must be be even
|
|
652
|
+
if (this.literal[letter].isEven()) {
|
|
524
653
|
return false;
|
|
525
654
|
}
|
|
526
|
-
return this.isLitteralSquare();
|
|
527
655
|
}
|
|
528
656
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
if (this.literal[letter] % 2 !== 0) {
|
|
532
|
-
return false;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
return true;
|
|
536
|
-
}
|
|
537
|
-
// ------------------------------------------
|
|
538
|
-
// Misc monoms functions
|
|
539
|
-
// -------------------------------------
|
|
540
|
-
/**
|
|
541
|
-
* Determine if a monom contains a setLetter in it's literal part
|
|
542
|
-
* @param letter
|
|
543
|
-
*/
|
|
544
|
-
hasLetter = (letter?: string): boolean => {
|
|
545
|
-
return this._literal[letter === undefined ? 'x' : letter] > 0;
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
/**
|
|
549
|
-
* Set the power of a particular setLetter
|
|
550
|
-
* @param letter (string) Letter to change
|
|
551
|
-
* @param pow (number) Power of the setLetter (must be positive integer.
|
|
552
|
-
*/
|
|
553
|
-
setLetter = (letter: string, pow: number): void => {
|
|
554
|
-
// If the power is not legal or is zero, remove the setLetter from the dict
|
|
555
|
-
if (pow <= 0 || !Number.isSafeInteger(pow)) {
|
|
556
|
-
if (this._literal[letter] !== undefined) {
|
|
557
|
-
delete this._literal[letter];
|
|
558
|
-
}
|
|
559
|
-
} else {
|
|
560
|
-
this._literal[letter] = pow;
|
|
561
|
-
}
|
|
562
|
-
};
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
563
659
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
degree = (letter?: string): number => {
|
|
569
|
-
if (this.variables.length === 0) {
|
|
570
|
-
return 0;
|
|
571
|
-
}
|
|
572
|
-
if (letter === undefined) {
|
|
573
|
-
// Not setLetter given -> we get the global monom degree (sum of all the letters).
|
|
574
|
-
return Object.values(this._literal).reduce((t, n) => t + n);
|
|
575
|
-
} else {
|
|
576
|
-
// A setLetter is given -> get the corresponding power.
|
|
577
|
-
return this._literal[letter] === undefined ? 0 : this._literal[letter];
|
|
660
|
+
hasFractionCoefficient = (): boolean => {
|
|
661
|
+
for (let letter in this._literal) {
|
|
662
|
+
if (this._literal[letter].isRational()) {
|
|
663
|
+
return true
|
|
578
664
|
}
|
|
579
|
-
}
|
|
665
|
+
}
|
|
580
666
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
667
|
+
return false
|
|
668
|
+
}
|
|
669
|
+
// ------------------------------------------
|
|
670
|
+
// Misc monoms functions
|
|
671
|
+
// -------------------------------------
|
|
672
|
+
/**
|
|
673
|
+
* Determine if a monom contains a setLetter in it's literal part
|
|
674
|
+
* @param letter
|
|
675
|
+
*/
|
|
676
|
+
hasLetter = (letter?: string): boolean => {
|
|
677
|
+
// The letter was not found
|
|
678
|
+
if (this._literal[letter === undefined ? 'x' : letter] === undefined) {
|
|
679
|
+
return false
|
|
680
|
+
}
|
|
587
681
|
|
|
588
|
-
|
|
589
|
-
|
|
682
|
+
// The letter is found and is not zero !
|
|
683
|
+
return this._literal[letter === undefined ? 'x' : letter].isNotZero();
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Set the power of a particular setLetter
|
|
688
|
+
* @param letter (string) Letter to change
|
|
689
|
+
* @param pow (number) Power of the setLetter (must be positive integer.
|
|
690
|
+
*/
|
|
691
|
+
setLetter = (letter: string, pow: Fraction | number): void => {
|
|
692
|
+
if (pow instanceof Fraction) {
|
|
693
|
+
// Set the power of the letter to zero => remove it
|
|
694
|
+
if (this.hasLetter(letter) && pow.isZero()) {
|
|
695
|
+
delete this._literal[letter]
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
this._literal[letter] = pow.clone()
|
|
699
|
+
} else {
|
|
700
|
+
this.setLetter(letter, new Fraction(pow))
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Get the degree of a monom. If no setLetter is given, the result will be the global degree.
|
|
706
|
+
* @param letter (string) Letter to get to degree (power)
|
|
707
|
+
*/
|
|
708
|
+
degree = (letter?: string): Fraction => {
|
|
709
|
+
if (this.variables.length === 0) {
|
|
710
|
+
return new Fraction().zero();
|
|
711
|
+
}
|
|
712
|
+
if (letter === undefined) {
|
|
713
|
+
// Not setLetter given -> we get the global monom degree (sum of all the letters).
|
|
714
|
+
return Object.values(this._literal).reduce((t, n) => t.clone().add(n));
|
|
715
|
+
} else {
|
|
716
|
+
// A setLetter is given -> get the corresponding power.
|
|
717
|
+
return this._literal[letter] === undefined ? new Fraction().zero() : this._literal[letter].clone();
|
|
718
|
+
}
|
|
719
|
+
};
|
|
590
720
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
721
|
+
/**
|
|
722
|
+
* Evaluate a monom. Each setLetter must be assigned to a Fraction.
|
|
723
|
+
* @param values Dictionary of <setLetter: Fraction>
|
|
724
|
+
*/
|
|
725
|
+
evaluate = (values: literalType | Fraction | number): Fraction => {
|
|
726
|
+
let r = this.coefficient.clone();
|
|
594
727
|
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
728
|
+
if (typeof values === 'number' || values instanceof Fraction) {
|
|
729
|
+
let tmpValues: literalType = {}
|
|
730
|
+
|
|
731
|
+
tmpValues[this.variables[0]] = new Fraction(values)
|
|
732
|
+
return this.evaluate(tmpValues);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (typeof values === 'object') {
|
|
736
|
+
for (let L in this._literal) {
|
|
737
|
+
if (values[L] === undefined) {
|
|
738
|
+
return new Fraction().zero();
|
|
602
739
|
}
|
|
603
|
-
}
|
|
604
|
-
return r;
|
|
605
|
-
};
|
|
606
740
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
* @param letter
|
|
610
|
-
*/
|
|
611
|
-
derivative = (letter?: string): Monom => {
|
|
612
|
-
// No setLetter given - assume it's the setLetter 'x'
|
|
613
|
-
if (letter === undefined) {
|
|
614
|
-
letter = 'x';
|
|
741
|
+
let value = new Fraction(values[L])
|
|
742
|
+
r.multiply(value.pow(this._literal[L]))
|
|
615
743
|
}
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
744
|
+
}
|
|
745
|
+
return r;
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
/**
|
|
749
|
+
* Derivative the monom
|
|
750
|
+
* @param letter
|
|
751
|
+
*/
|
|
752
|
+
derivative = (letter?: string): Monom => {
|
|
753
|
+
// No setLetter given - assume it's the setLetter 'x'
|
|
754
|
+
if (letter === undefined) {
|
|
755
|
+
letter = 'x';
|
|
756
|
+
}
|
|
619
757
|
|
|
620
|
-
|
|
621
|
-
|
|
758
|
+
if (this.hasLetter(letter)) {
|
|
759
|
+
let d = this._literal[letter].clone(),
|
|
760
|
+
dM = this.clone();
|
|
622
761
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
}
|
|
762
|
+
// Subtract one to the degree.
|
|
763
|
+
dM._literal[letter].subtract(1)
|
|
764
|
+
|
|
765
|
+
// Multiply the coefficient by the previous degree
|
|
766
|
+
dM._coefficient.multiply(new Fraction(d.clone()));
|
|
767
|
+
return dM;
|
|
768
|
+
} else {
|
|
769
|
+
return new Monom().zero();
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
primitive = (letter?: string): Monom => {
|
|
774
|
+
// TODO: derivative including the ln value => implies creating different monom system ?
|
|
775
|
+
if (letter === undefined) {
|
|
776
|
+
letter = 'x'
|
|
777
|
+
}
|
|
630
778
|
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
779
|
+
// Zero monom
|
|
780
|
+
let M = this.clone(), degree
|
|
781
|
+
|
|
782
|
+
if (M.hasLetter(letter)) {
|
|
783
|
+
degree = M.degree(letter).clone().add(1)
|
|
784
|
+
M.coefficient = M.coefficient.clone().divide(degree)
|
|
785
|
+
M.setLetter(letter, degree)
|
|
786
|
+
} else {
|
|
787
|
+
// There is no letter.
|
|
788
|
+
|
|
789
|
+
// The coefficient might be zero (=> x) or a number a (=> ax)
|
|
790
|
+
if (M.coefficient.isZero()) {
|
|
791
|
+
M.coefficient = new Fraction().one()
|
|
792
|
+
}
|
|
793
|
+
M.setLetter(letter, 1)
|
|
794
|
+
}
|
|
795
|
+
return M
|
|
796
|
+
}
|
|
797
|
+
// ----------------------------------------
|
|
798
|
+
// Static functions
|
|
799
|
+
// ----------------------------------------
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Get the least common multiple of monoms
|
|
803
|
+
* @param monoms Array of monoms
|
|
804
|
+
*/
|
|
805
|
+
static lcm = (...monoms: Monom[]): Monom => {
|
|
806
|
+
// All the monoms must be with natural powers...
|
|
807
|
+
for (let m of monoms) {
|
|
808
|
+
if (m.hasFractionCoefficient()) {
|
|
809
|
+
return new Monom().zero()
|
|
634
810
|
}
|
|
811
|
+
}
|
|
635
812
|
|
|
636
|
-
// Zero monom
|
|
637
|
-
let M = this.clone()
|
|
638
813
|
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
814
|
+
let M = new Monom(),
|
|
815
|
+
coeffN: number[] = monoms.map(value => value.coefficient.numerator),
|
|
816
|
+
coeffD: number[] = monoms.map(value => value.coefficient.denominator),
|
|
817
|
+
n = Numeric.gcd(...coeffN),
|
|
818
|
+
d = Numeric.lcm(...coeffD);
|
|
644
819
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
// Static functions
|
|
655
|
-
// ----------------------------------------
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* Get the least common multiple of monoms
|
|
659
|
-
* @param monoms Array of monoms
|
|
660
|
-
*/
|
|
661
|
-
static lcm = (...monoms: Monom[]): Monom => {
|
|
662
|
-
let M = new Monom(),
|
|
663
|
-
coeffN: number[] = monoms.map(value => value.coefficient.numerator),
|
|
664
|
-
coeffD: number[] = monoms.map(value => value.coefficient.denominator),
|
|
665
|
-
n = Numeric.gcd(...coeffN),
|
|
666
|
-
d = Numeric.lcm(...coeffD);
|
|
667
|
-
|
|
668
|
-
// Get the coefficient.
|
|
669
|
-
M.coefficient = new Fraction(n, d).reduce();
|
|
670
|
-
|
|
671
|
-
// Set the literal parts - go through each monoms literal parts and get only the lowest degree of each letters.
|
|
672
|
-
for (let m of monoms) {
|
|
673
|
-
// Remove the inexistant letters from the resulting monom
|
|
674
|
-
for (let letter in M.literal) {
|
|
675
|
-
if (!(letter in m.literal)) {
|
|
676
|
-
M.literal[letter] = 0;
|
|
677
|
-
}
|
|
820
|
+
// Get the coefficient.
|
|
821
|
+
M.coefficient = new Fraction(n, d).reduce();
|
|
822
|
+
|
|
823
|
+
// Set the literal parts - go through each monoms literal parts and get only the lowest degree of each letters.
|
|
824
|
+
for (let m of monoms) {
|
|
825
|
+
// Remove the inexistant letters from the resulting monom
|
|
826
|
+
for (let letter in M.literal) {
|
|
827
|
+
if (!(letter in m.literal)) {
|
|
828
|
+
M.literal[letter].zero();
|
|
678
829
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
830
|
+
}
|
|
831
|
+
for (let letter in m.literal) {
|
|
832
|
+
if (M.literal[letter] === undefined && m.literal[letter].isStrictlyPositive()) {
|
|
833
|
+
M.literal[letter] = m.literal[letter].clone();
|
|
834
|
+
} else {
|
|
835
|
+
M.literal[letter] = new Fraction(Math.min(m.literal[letter].value, M.literal[letter].value))
|
|
685
836
|
}
|
|
686
837
|
}
|
|
838
|
+
}
|
|
687
839
|
|
|
688
|
-
|
|
689
|
-
|
|
840
|
+
return M;
|
|
841
|
+
};
|
|
690
842
|
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
843
|
+
/**
|
|
844
|
+
* Multiply two monoms and return a NEW monom.
|
|
845
|
+
* @param monoms
|
|
846
|
+
*/
|
|
847
|
+
static xmultiply = (...monoms: Monom[]): Monom => {
|
|
848
|
+
let M = new Monom().one();
|
|
697
849
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
850
|
+
for (let m of monoms) {
|
|
851
|
+
M.multiply(m);
|
|
852
|
+
}
|
|
701
853
|
|
|
702
|
-
|
|
703
|
-
|
|
854
|
+
return M;
|
|
855
|
+
};
|
|
704
856
|
|
|
705
857
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
858
|
+
// TODO: The rest of the functions are not used or unnecessary ?
|
|
859
|
+
/**
|
|
860
|
+
* Determine if multiple monoms are similar
|
|
861
|
+
* @param M
|
|
862
|
+
*/
|
|
863
|
+
areSameAs = (...M: Monom[]): boolean => {
|
|
864
|
+
let result: boolean = true;
|
|
713
865
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
}
|
|
866
|
+
// Check all monoms if they are the isSame as the "this" one.
|
|
867
|
+
for (let i = 0; i < M.length; i++) {
|
|
868
|
+
if (!this.isSameAs(M[i])) {
|
|
869
|
+
return false;
|
|
719
870
|
}
|
|
871
|
+
}
|
|
720
872
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
873
|
+
// All check passed -> all the monoms are similar.
|
|
874
|
+
return result;
|
|
875
|
+
};
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Determine if multiple monoms are equals
|
|
879
|
+
* @param M
|
|
880
|
+
*/
|
|
881
|
+
areEquals = (...M: Monom[]): boolean => {
|
|
882
|
+
// They are not similar.
|
|
883
|
+
if (!this.areSameAs(...M)) {
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
724
886
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
*/
|
|
729
|
-
areEquals = (...M: Monom[]): boolean => {
|
|
730
|
-
// They are not similar.
|
|
731
|
-
if (!this.areSameAs(...M)) {
|
|
887
|
+
// Check all coefficient. They must be equals.
|
|
888
|
+
for (let m of M) {
|
|
889
|
+
if (!this._coefficient.isEqual(m.coefficient)) {
|
|
732
890
|
return false;
|
|
733
891
|
}
|
|
892
|
+
}
|
|
734
893
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
return false;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
// All checks passed.
|
|
743
|
-
return true;
|
|
744
|
-
};
|
|
894
|
+
// All checks passed.
|
|
895
|
+
return true;
|
|
896
|
+
};
|
|
745
897
|
|
|
746
|
-
|
|
898
|
+
}
|