pimath 0.2.13 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pimath.js +856 -1882
- package/dist/pimath.js.map +1 -1
- package/package.json +2 -2
- package/src/algebra/equation.ts +562 -558
- package/src/algebra/equationSolver.ts +489 -489
- package/src/algebra/factor.ts +396 -392
- package/src/algebra/linearSystem.ts +446 -442
- package/src/algebra/matrix.ts +515 -511
- package/src/algebra/monom.ts +932 -928
- package/src/algebra/polyFactor.ts +724 -720
- package/src/algebra/polynom.ts +1233 -1229
- package/src/coefficients/fraction.ts +907 -748
- package/src/coefficients/root.ts +500 -359
- package/src/geometry/TupleN.ts +129 -129
- package/src/geometry/circle.ts +455 -455
- package/src/geometry/line.ts +658 -654
- package/src/geometry/point.ts +105 -105
- package/src/geometry/sphere3.ts +2 -2
- package/src/geometry/triangle.ts +488 -488
- package/src/geometry/vector.ts +230 -226
- package/src/numeric.ts +136 -51
- package/src/pimath.interface.ts +2 -0
- package/types/algebra/equation.d.ts +1 -0
- package/types/algebra/equation.d.ts.map +1 -1
- package/types/algebra/equationSolver.d.ts.map +1 -1
- package/types/algebra/factor.d.ts +1 -0
- package/types/algebra/factor.d.ts.map +1 -1
- package/types/algebra/linearSystem.d.ts +1 -0
- package/types/algebra/linearSystem.d.ts.map +1 -1
- package/types/algebra/matrix.d.ts +6 -5
- package/types/algebra/matrix.d.ts.map +1 -1
- package/types/algebra/monom.d.ts +1 -0
- package/types/algebra/monom.d.ts.map +1 -1
- package/types/algebra/polyFactor.d.ts +1 -0
- package/types/algebra/polyFactor.d.ts.map +1 -1
- package/types/algebra/polynom.d.ts +1 -0
- package/types/algebra/polynom.d.ts.map +1 -1
- package/types/coefficients/fraction.d.ts +62 -53
- package/types/coefficients/fraction.d.ts.map +1 -1
- package/types/coefficients/root.d.ts +3 -0
- package/types/coefficients/root.d.ts.map +1 -1
- package/types/geometry/TupleN.d.ts +1 -1
- package/types/geometry/TupleN.d.ts.map +1 -1
- package/types/geometry/circle.d.ts.map +1 -1
- package/types/geometry/line.d.ts +1 -0
- package/types/geometry/line.d.ts.map +1 -1
- package/types/geometry/point.d.ts.map +1 -1
- package/types/geometry/sphere3.d.ts +2 -2
- package/types/geometry/sphere3.d.ts.map +1 -1
- package/types/geometry/triangle.d.ts.map +1 -1
- package/types/geometry/vector.d.ts +1 -0
- package/types/geometry/vector.d.ts.map +1 -1
- package/types/index.d.ts +3 -3
- package/types/numeric.d.ts +4 -4
- package/types/numeric.d.ts.map +1 -1
- package/types/pimath.interface.d.ts +1 -0
- package/types/pimath.interface.d.ts.map +1 -1
- package/types/primes.data.d.ts +2 -0
- package/types/primes.data.d.ts.map +1 -0
package/src/algebra/factor.ts
CHANGED
|
@@ -1,392 +1,396 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
IAlgebra,
|
|
3
|
-
IExpression,
|
|
4
|
-
InputAlgebra,
|
|
5
|
-
InputValue,
|
|
6
|
-
IPiMathObject,
|
|
7
|
-
literalType,
|
|
8
|
-
TABLE_OF_SIGNS
|
|
9
|
-
} from "../pimath.interface"
|
|
10
|
-
import {Fraction} from "../coefficients/fraction"
|
|
11
|
-
import {Polynom} from "./polynom"
|
|
12
|
-
import {replace_in_array, splitIfOutsideParentheses, wrapParenthesis} from "../helpers"
|
|
13
|
-
import {MathError, NotImplementedError} from "../errors"
|
|
14
|
-
|
|
15
|
-
export class Factor implements IPiMathObject<Factor>,
|
|
16
|
-
IExpression<Factor>,
|
|
17
|
-
IAlgebra<Factor> {
|
|
18
|
-
#displayMode: FACTOR_DISPLAY
|
|
19
|
-
#polynom: Polynom
|
|
20
|
-
#power: Fraction
|
|
21
|
-
#singleMode = false
|
|
22
|
-
|
|
23
|
-
constructor(value?: InputAlgebra<Polynom> | Factor, power?: InputValue<Fraction>) {
|
|
24
|
-
|
|
25
|
-
if (value instanceof Factor) {
|
|
26
|
-
this.#polynom = value.polynom.clone()
|
|
27
|
-
this.#power = value.power.clone()
|
|
28
|
-
|
|
29
|
-
if (power !== undefined) {
|
|
30
|
-
this.#power.multiply(new Fraction(power))
|
|
31
|
-
}
|
|
32
|
-
} else if (value !== undefined) {
|
|
33
|
-
this.#polynom = new Polynom(value)
|
|
34
|
-
this.#power = new Fraction(power ?? 1)
|
|
35
|
-
} else {
|
|
36
|
-
this.#polynom = new Polynom()
|
|
37
|
-
this.#power = new Fraction(1)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.#displayMode = FACTOR_DISPLAY.POWER
|
|
41
|
-
|
|
42
|
-
return this
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
throw new NotImplementedError("Method not implemented.")
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return this
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
this
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return this
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this.
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
P =
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
return this.polynom.
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
this.#polynom
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
this
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return this
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
this.#power
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
throw new NotImplementedError("Method not implemented.")
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
return this
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
return
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
this
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
this
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
if(
|
|
351
|
-
if(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
1
|
+
import type {
|
|
2
|
+
IAlgebra,
|
|
3
|
+
IExpression,
|
|
4
|
+
InputAlgebra,
|
|
5
|
+
InputValue,
|
|
6
|
+
IPiMathObject,
|
|
7
|
+
literalType,
|
|
8
|
+
TABLE_OF_SIGNS
|
|
9
|
+
} from "../pimath.interface"
|
|
10
|
+
import {Fraction} from "../coefficients/fraction"
|
|
11
|
+
import {Polynom} from "./polynom"
|
|
12
|
+
import {replace_in_array, splitIfOutsideParentheses, wrapParenthesis} from "../helpers"
|
|
13
|
+
import {MathError, NotImplementedError} from "../errors"
|
|
14
|
+
|
|
15
|
+
export class Factor implements IPiMathObject<Factor>,
|
|
16
|
+
IExpression<Factor>,
|
|
17
|
+
IAlgebra<Factor> {
|
|
18
|
+
#displayMode: FACTOR_DISPLAY
|
|
19
|
+
#polynom: Polynom
|
|
20
|
+
#power: Fraction
|
|
21
|
+
#singleMode = false
|
|
22
|
+
|
|
23
|
+
constructor(value?: InputAlgebra<Polynom> | Factor, power?: InputValue<Fraction>) {
|
|
24
|
+
|
|
25
|
+
if (value instanceof Factor) {
|
|
26
|
+
this.#polynom = value.polynom.clone()
|
|
27
|
+
this.#power = value.power.clone()
|
|
28
|
+
|
|
29
|
+
if (power !== undefined) {
|
|
30
|
+
this.#power.multiply(new Fraction(power))
|
|
31
|
+
}
|
|
32
|
+
} else if (value !== undefined) {
|
|
33
|
+
this.#polynom = new Polynom(value)
|
|
34
|
+
this.#power = new Fraction(power ?? 1)
|
|
35
|
+
} else {
|
|
36
|
+
this.#polynom = new Polynom()
|
|
37
|
+
this.#power = new Fraction(1)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.#displayMode = FACTOR_DISPLAY.POWER
|
|
41
|
+
|
|
42
|
+
return this
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
parse(/*value: InputValue<Factor>*/): Factor {
|
|
46
|
+
throw new NotImplementedError("Method not implemented.")
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
copy(): this {
|
|
50
|
+
throw new NotImplementedError("copy() is not yet implemented for Factor")
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
clone(): Factor {
|
|
54
|
+
return new Factor(this)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get tex(): string {
|
|
58
|
+
const num = this.power.numerator
|
|
59
|
+
const den = this.power.denominator
|
|
60
|
+
|
|
61
|
+
let base: string
|
|
62
|
+
let power: string
|
|
63
|
+
|
|
64
|
+
if (this.#displayMode === FACTOR_DISPLAY.ROOT && den > 1) {
|
|
65
|
+
base = `\\sqrt${den === 2 ? '' : `[ ${den} ]`}{ ${this.polynom.tex} }`
|
|
66
|
+
power = num === 1 ? '' : `^{ ${num} }`
|
|
67
|
+
} else {
|
|
68
|
+
base = this.#singleMode && this.power.isOne() ? this.polynom.tex : wrapParenthesis(this.polynom.tex)
|
|
69
|
+
power = (den === 1 && num === 1) ? '' : `^{ ${this.power.tex} }`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Add the power if it's not 1 or -1
|
|
73
|
+
base = `${base}${power}`
|
|
74
|
+
|
|
75
|
+
// If the power is negative, make it as a fraction.
|
|
76
|
+
if (this.#displayMode === FACTOR_DISPLAY.ROOT && num < 0) {
|
|
77
|
+
base = `\\frac{ 1 }{ ${base} }`
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
return base
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get display(): string {
|
|
85
|
+
const num = this.power.numerator
|
|
86
|
+
const den = this.power.denominator
|
|
87
|
+
|
|
88
|
+
let base: string
|
|
89
|
+
let power: string
|
|
90
|
+
|
|
91
|
+
if (this.#displayMode === FACTOR_DISPLAY.ROOT && den > 1) {
|
|
92
|
+
base = `${den === 2 ? 'sqrt' : `root(${den})`}(${this.polynom.display})`
|
|
93
|
+
power = num === 1 ? '' : `^(${num})`
|
|
94
|
+
} else {
|
|
95
|
+
base = this.#singleMode && this.power.isOne() ? this.polynom.display : wrapParenthesis(this.polynom.display, false)
|
|
96
|
+
power = (den === 1 && num === 1) ? '' : `^(${this.power.display})`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Add the power if it's not 1 or -1
|
|
100
|
+
base = `${base}${power}`
|
|
101
|
+
|
|
102
|
+
// If the power is negative, make it as a fraction.
|
|
103
|
+
if (this.#displayMode === FACTOR_DISPLAY.ROOT && num < 0) {
|
|
104
|
+
base = `1/(${base})`
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
return base
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
add(): Factor {
|
|
112
|
+
throw new MathError("Adding two factors is not possible")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
get asSingle(): this {
|
|
116
|
+
this.#singleMode = true
|
|
117
|
+
return this
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
degree(letter?: string): Fraction {
|
|
121
|
+
return this.polynom.degree(letter).multiply(this.power)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
derivative(): Factor[] {
|
|
125
|
+
// The power is zero, the derivative is zero
|
|
126
|
+
if (this.power.isZero()) {
|
|
127
|
+
return [new Factor('0')]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// The power is one, the derivative is the derivative of the polynom
|
|
131
|
+
if (this.power.isOne()) {
|
|
132
|
+
return [new Factor(this.polynom.clone().derivative())]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// In any other case, the derivative consist of three Factors:
|
|
136
|
+
// the derivative of the polynom, the power and the polynom
|
|
137
|
+
return [
|
|
138
|
+
new Factor(this.power.clone()),
|
|
139
|
+
new Factor(this.polynom.clone().derivative()),
|
|
140
|
+
new Factor(this.polynom.clone(), this.power.clone().subtract(1))
|
|
141
|
+
]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
develop(): Polynom {
|
|
145
|
+
if (this.power.isNatural()) {
|
|
146
|
+
return this.polynom.clone().pow(this.power.value)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
throw new MathError("The power must be a natural number")
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
divide(value: InputAlgebra<Factor | Polynom>): this {
|
|
153
|
+
if (value instanceof Factor) {
|
|
154
|
+
if (this.isSameAs(value)) {
|
|
155
|
+
this.power.subtract(value.power)
|
|
156
|
+
return this
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
const P = new Polynom(value as Polynom)
|
|
162
|
+
if (this.isSameAs(P)) {
|
|
163
|
+
this.power.subtract(1)
|
|
164
|
+
return this
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
throw new MathError("The two factors must be the same")
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
evaluate(values: InputValue<Fraction> | literalType<number | Fraction>, asNumeric?: boolean): number | Fraction {
|
|
171
|
+
if (asNumeric) {
|
|
172
|
+
return (this.polynom.evaluate(values, true) as number) ** this.power.value
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
return (this.polynom.evaluate(values) as Fraction).pow(this.power)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fromPolynom(polynom: InputValue<Polynom>): this {
|
|
180
|
+
this.#polynom = new Polynom(polynom)
|
|
181
|
+
this.#power = new Fraction(1)
|
|
182
|
+
return this
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
hasVariable(letter: string): boolean {
|
|
186
|
+
return this.polynom.hasVariable(letter)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
inverse(): this {
|
|
190
|
+
this.power.opposite()
|
|
191
|
+
return this
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
isEqual(value: Factor): boolean {
|
|
195
|
+
// Must have the same polynom and the same reduce power
|
|
196
|
+
|
|
197
|
+
return this.isSameAs(value) &&
|
|
198
|
+
this.power.isEqual(value.power)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
isOne(): boolean {
|
|
202
|
+
return this.polynom.isOne() || this.power.isZero()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
isSameAs(value: InputAlgebra<Factor | Polynom>) {
|
|
206
|
+
let P: Polynom
|
|
207
|
+
if (value instanceof Factor) {
|
|
208
|
+
P = value.polynom
|
|
209
|
+
} else if (value instanceof Polynom) {
|
|
210
|
+
P = value
|
|
211
|
+
} else {
|
|
212
|
+
P = new Polynom(value)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
return this.polynom.isEqual(P)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
isZero(): boolean {
|
|
220
|
+
return this.polynom.isZero()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
multiply(value: InputAlgebra<Factor | Polynom>): this {
|
|
224
|
+
const F = new Factor(value)
|
|
225
|
+
|
|
226
|
+
// Case if this and value are monoms of degree one.
|
|
227
|
+
if(this.polynom.monoms.length===1 && this.power.isOne()
|
|
228
|
+
&&
|
|
229
|
+
F.polynom.monoms.length===1 && this.power.isOne()
|
|
230
|
+
){
|
|
231
|
+
this.polynom.multiply(F.polynom)
|
|
232
|
+
return this
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (value instanceof Factor) {
|
|
236
|
+
if (this.isSameAs(value)) {
|
|
237
|
+
this.power.add(value.power)
|
|
238
|
+
return this
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
const P = new Polynom(value as Polynom)
|
|
244
|
+
if (this.isSameAs(P)) {
|
|
245
|
+
this.power.add(1)
|
|
246
|
+
return this
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
throw new MathError("The two factors must be the same")
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
one(): this {
|
|
253
|
+
this.#polynom.one()
|
|
254
|
+
this.#power.one()
|
|
255
|
+
return this
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
opposite(): Factor {
|
|
259
|
+
throw new NotImplementedError("Method not implemented.")
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
get polynom(): Polynom {
|
|
263
|
+
return this.#polynom
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
set polynom(value: Polynom) {
|
|
267
|
+
this.#polynom = value
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
pow(value: number | Fraction): this {
|
|
271
|
+
this.power.multiply(value)
|
|
272
|
+
return this
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
get power(): Fraction {
|
|
276
|
+
return this.#power
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
set power(value: InputValue<Fraction>) {
|
|
280
|
+
this.#power = new Fraction(value)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
primitive(): Factor {
|
|
284
|
+
throw new NotImplementedError("Method not implemented.")
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
reduce(): Factor {
|
|
288
|
+
throw new NotImplementedError("Method not implemented.")
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
root(value: number): this {
|
|
292
|
+
this.power.divide(value)
|
|
293
|
+
return this
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
sqrt(): this {
|
|
297
|
+
return this.root(2)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
subtract(): Factor {
|
|
301
|
+
throw new MathError("Subtracting two factors is not possible")
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
tableOfSigns(): TABLE_OF_SIGNS {
|
|
305
|
+
const pow = this.power.clone().reduce()
|
|
306
|
+
const tos = this.polynom.tableOfSigns()
|
|
307
|
+
|
|
308
|
+
// The zero roots becomes defence (d) if the power is negative
|
|
309
|
+
if (pow.isStrictlyNegative()) {
|
|
310
|
+
tos.signs = replace_in_array(tos.signs, 'z', 'd')
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// The - sign becomes
|
|
314
|
+
// + (plus) if the power num is even and the power den is odd
|
|
315
|
+
// i (invalid) if the power denominator is even
|
|
316
|
+
if (pow.denominator % 2 === 0) {
|
|
317
|
+
// it's an even roots : no negative values!
|
|
318
|
+
tos.signs = replace_in_array(tos.signs, '-', 'h')
|
|
319
|
+
} else if (pow.numerator % 2 === 0) {
|
|
320
|
+
// it's an even power : negative values becomes positive !
|
|
321
|
+
tos.signs = replace_in_array(tos.signs, '-', '+')
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
return {roots: tos.roots, signs: tos.signs}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
get variables(): string[] {
|
|
329
|
+
return this.polynom.variables
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
get withPower(): this {
|
|
333
|
+
this.#displayMode = FACTOR_DISPLAY.POWER
|
|
334
|
+
return this
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
get withRoot(): this {
|
|
338
|
+
this.#displayMode = FACTOR_DISPLAY.ROOT
|
|
339
|
+
return this
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
zero(): this {
|
|
343
|
+
this.#polynom.zero()
|
|
344
|
+
this.#power.one()
|
|
345
|
+
return this
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static checkParseAsFactors(str: string): boolean {
|
|
349
|
+
const splitPlus = splitIfOutsideParentheses(str, "+")
|
|
350
|
+
if(splitPlus[0]==='') splitPlus.shift()
|
|
351
|
+
if(splitPlus.length>1) return false
|
|
352
|
+
|
|
353
|
+
const splitMinus = splitIfOutsideParentheses(str, "-")
|
|
354
|
+
if(splitMinus[0]==='') splitMinus.shift()
|
|
355
|
+
if(splitMinus.length>1) return false
|
|
356
|
+
|
|
357
|
+
return true
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
static factorsFromString(value: string, isNumerator = true): Factor[] {
|
|
361
|
+
if (!Factor.checkParseAsFactors(value)) {
|
|
362
|
+
return [new Factor(value, isNumerator ? 1 : -1)]
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const sign = isNumerator ? 1 : -1
|
|
366
|
+
const factors: Factor[] = []
|
|
367
|
+
|
|
368
|
+
// Alternance : facteur parenthésé avec puissance optionnelle | séquence non-parenthésée
|
|
369
|
+
const regex =
|
|
370
|
+
/\(([^)]+)\)(?:\^(-?[0-9]+(?:\/[0-9]+)?|\(-?[0-9]+(?:\/[0-9]+)?\)))?|([^(]+)/g
|
|
371
|
+
|
|
372
|
+
for (const match of value.matchAll(regex)) {
|
|
373
|
+
if (match[1] !== undefined) {
|
|
374
|
+
// Cas : (expr)^puissance
|
|
375
|
+
const powStr = (match[2] ?? '1')
|
|
376
|
+
.replace(/[()]/g, '')
|
|
377
|
+
|
|
378
|
+
factors.push(new Factor(
|
|
379
|
+
new Polynom(match[1]),
|
|
380
|
+
new Fraction(powStr).multiply(sign))
|
|
381
|
+
)
|
|
382
|
+
} else if (match[3]?.trim()) {
|
|
383
|
+
// Cas : monôme ou constante hors parenthèses (ex: 3x^2, 3)
|
|
384
|
+
factors.push(new Factor(new Polynom(match[3].trim()), new Fraction(sign)))
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return factors
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export enum FACTOR_DISPLAY {
|
|
393
|
+
ROOT,
|
|
394
|
+
POWER
|
|
395
|
+
}
|
|
396
|
+
|