pimath 0.1.39 → 0.1.40

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.
Files changed (65) hide show
  1. package/dist/pimath.js +188 -159
  2. package/dist/pimath.js.map +1 -1
  3. package/package.json +4 -2
  4. package/src/algebra/equation.ts +556 -0
  5. package/src/algebra/equationSolver.ts +539 -0
  6. package/src/algebra/factor.ts +339 -0
  7. package/src/algebra/index.ts +11 -0
  8. package/src/algebra/linearSystem.ts +388 -0
  9. package/src/algebra/logicalset.ts +256 -0
  10. package/src/algebra/matrix.ts +474 -0
  11. package/src/algebra/monom.ts +1015 -0
  12. package/src/algebra/operations.ts +24 -0
  13. package/src/algebra/polyFactor.ts +668 -0
  14. package/src/algebra/polynom.ts +1394 -0
  15. package/src/analyze/solution.ts +115 -0
  16. package/src/analyze/tableOfSigns.ts +30 -0
  17. package/src/coefficients/fraction.ts +678 -0
  18. package/src/coefficients/index.ts +4 -0
  19. package/src/coefficients/nthRoot.ts +149 -0
  20. package/src/coefficients/root.ts +299 -0
  21. package/src/geometry/circle.ts +386 -0
  22. package/src/geometry/geomMath.ts +70 -0
  23. package/src/geometry/index.ts +10 -0
  24. package/src/geometry/line.ts +677 -0
  25. package/src/geometry/line3.ts +206 -0
  26. package/src/geometry/plane3.ts +170 -0
  27. package/src/geometry/point.ts +66 -0
  28. package/src/geometry/sphere3.ts +214 -0
  29. package/src/geometry/triangle.ts +354 -0
  30. package/src/geometry/vector.ts +341 -0
  31. package/src/helpers.ts +35 -0
  32. package/src/index.ts +60 -0
  33. package/src/numeric.ts +199 -0
  34. package/src/pimath.interface.ts +160 -0
  35. package/src/randomization/algebra/rndEquation.ts +41 -0
  36. package/src/randomization/algebra/rndMonom.ts +39 -0
  37. package/src/randomization/algebra/rndPolynom.ts +86 -0
  38. package/src/randomization/coefficient/rndFraction.ts +38 -0
  39. package/src/randomization/geometry/rndCircle.ts +27 -0
  40. package/src/randomization/geometry/rndLine.ts +37 -0
  41. package/src/randomization/geometry/rndLine3.ts +27 -0
  42. package/src/randomization/geometry/rndVector.ts +63 -0
  43. package/src/randomization/random.ts +91 -0
  44. package/src/randomization/rndHelpers.ts +102 -0
  45. package/src/randomization/rndTypes.ts +63 -0
  46. package/types/algebra/equationSolver.d.ts +3 -0
  47. package/types/algebra/equationSolver.d.ts.map +1 -1
  48. package/types/algebra/polyFactor.d.ts +5 -0
  49. package/types/algebra/polyFactor.d.ts.map +1 -1
  50. package/types/analyze/solution.d.ts +21 -0
  51. package/types/analyze/solution.d.ts.map +1 -0
  52. package/types/analyze/tableOfSigns.d.ts +9 -0
  53. package/types/analyze/tableOfSigns.d.ts.map +1 -0
  54. package/types/coefficients/root.d.ts +38 -0
  55. package/types/coefficients/root.d.ts.map +1 -0
  56. package/types/geometry/point.d.ts +1 -1
  57. package/types/geometry/point.d.ts.map +1 -1
  58. package/types/helpers.d.ts +1 -0
  59. package/types/helpers.d.ts.map +1 -1
  60. package/types/index.d.ts +1 -0
  61. package/types/index.d.ts.map +1 -1
  62. package/types/numeric.d.ts +2 -0
  63. package/types/numeric.d.ts.map +1 -1
  64. package/types/pimath.interface.d.ts +26 -26
  65. package/types/pimath.interface.d.ts.map +1 -1
@@ -0,0 +1,339 @@
1
+ import type {
2
+ IAlgebra,
3
+ IExpression,
4
+ InputAlgebra,
5
+ InputValue,
6
+ IPiMathObject,
7
+ ISolution,
8
+ literalType,
9
+ TABLE_OF_SIGNS
10
+ } from "../pimath.interface"
11
+ import {Fraction} from "../coefficients/fraction"
12
+ import {Polynom} from "./polynom"
13
+ import {replace_in_array, wrapParenthesis} from "../helpers"
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
+
26
+ if (value instanceof Factor) {
27
+ this.#polynom = value.polynom.clone()
28
+ this.#power = value.power.clone()
29
+
30
+ if(power !== undefined){
31
+ this.#power.multiply(new Fraction(power))
32
+ }
33
+ } else if(value !== undefined) {
34
+ this.#polynom = new Polynom(value)
35
+ this.#power = new Fraction(power ?? 1)
36
+ }else{
37
+ this.#polynom = new Polynom()
38
+ this.#power = new Fraction(1)
39
+ }
40
+
41
+ this.#displayMode = FACTOR_DISPLAY.POWER
42
+
43
+ return this
44
+ }
45
+
46
+ public parse(/*value: InputValue<Factor>*/): Factor {
47
+ throw new Error("Method not implemented.")
48
+ }
49
+
50
+ public clone(): Factor {
51
+ return new Factor(this)
52
+ }
53
+
54
+ public fromPolynom(polynom: InputValue<Polynom>): this {
55
+ this.#polynom = new Polynom(polynom)
56
+ this.#power = new Fraction(1)
57
+ return this
58
+ }
59
+ public get tex(): string {
60
+ const num = this.power.numerator
61
+ const den = this.power.denominator
62
+
63
+ let base: string
64
+ let power: string
65
+
66
+ if (this.#displayMode === FACTOR_DISPLAY.ROOT && den > 1) {
67
+ base = `\\sqrt${den === 2 ? '' : `[ ${den} ]`}{ ${this.polynom.tex} }`
68
+ power = num === 1 ? '' : `^{ ${num} }`
69
+ } else {
70
+ base = this.#singleMode && this.power.isOne() ? this.polynom.tex : wrapParenthesis(this.polynom.tex)
71
+ power = (den === 1 && num === 1) ? '' : `^{ ${this.power.tex} }`
72
+ }
73
+
74
+ // Add the power if it's not 1 or -1
75
+ base = `${base}${power}`
76
+
77
+ // If the power is negative, make it as a fraction.
78
+ if (this.#displayMode === FACTOR_DISPLAY.ROOT && num < 0) {
79
+ base = `\\frac{ 1 }{ ${base} }`
80
+ }
81
+
82
+
83
+ return base
84
+ }
85
+
86
+ public get display(): string {
87
+ const num = this.power.numerator
88
+ const den = this.power.denominator
89
+
90
+ let base: string
91
+ let power: string
92
+
93
+ if (this.#displayMode === FACTOR_DISPLAY.ROOT && den > 1) {
94
+ base = `${den === 2 ? 'sqrt' : `root(${den})`}(${this.polynom.display})`
95
+ power = num === 1 ? '' : `^(${num})`
96
+ } else {
97
+ base = this.#singleMode && this.power.isOne() ? this.polynom.display : wrapParenthesis(this.polynom.display, false)
98
+ power = (den === 1 && num === 1) ? '' : `^(${this.power.display})`
99
+ }
100
+
101
+ // Add the power if it's not 1 or -1
102
+ base = `${base}${power}`
103
+
104
+ // If the power is negative, make it as a fraction.
105
+ if (this.#displayMode === FACTOR_DISPLAY.ROOT && num < 0) {
106
+ base = `1/(${base})`
107
+ }
108
+
109
+
110
+ return base
111
+ }
112
+
113
+ public add(): Factor {
114
+ throw new Error("Adding two factors is not possible")
115
+ }
116
+
117
+ public get asSingle(): this {
118
+ this.#singleMode = true
119
+ return this
120
+ }
121
+
122
+ public degree(letter?: string): Fraction {
123
+ return this.polynom.degree(letter).multiply(this.power)
124
+ }
125
+
126
+ public derivative(): Factor[] {
127
+ // The power is zero, the derivative is zero
128
+ if (this.power.isZero()) {
129
+ return [new Factor('0')]
130
+ }
131
+
132
+ // The power is one, the derivative is the derivative of the polynom
133
+ if (this.power.isOne()) {
134
+ return [new Factor(this.polynom.clone().derivative())]
135
+ }
136
+
137
+ // In any other case, the derivative consist of three Factors:
138
+ // the derivative of the polynom, the power and the polynom
139
+ return [
140
+ new Factor(this.power.clone()),
141
+ new Factor(this.polynom.clone().derivative()),
142
+ new Factor(this.polynom.clone(), this.power.clone().subtract(1))
143
+ ]
144
+ }
145
+
146
+ public develop(): Polynom {
147
+ if (this.power.isNatural()) {
148
+ return this.polynom.clone().pow(this.power.value)
149
+ }
150
+
151
+ throw new Error("The power must be a natural number")
152
+ }
153
+
154
+ public divide(value: InputAlgebra<Factor | Polynom>): this {
155
+ if (value instanceof Factor) {
156
+ if (this.isSameAs(value)) {
157
+ this.power.subtract(value.power)
158
+ return this
159
+ }
160
+ }
161
+
162
+
163
+ const P = new Polynom(value as Polynom)
164
+ if (this.isSameAs(P)) {
165
+ this.power.subtract(1)
166
+ return this
167
+ }
168
+
169
+ throw new Error("The two factors must be the same")
170
+ }
171
+
172
+ public evaluate(values: InputValue<Fraction> | literalType<number | Fraction>, asNumeric?: boolean): number | Fraction {
173
+ if (asNumeric) {
174
+ return (this.polynom.evaluate(values, true) as number) ** this.power.value
175
+ }
176
+
177
+
178
+ return (this.polynom.evaluate(values) as Fraction).pow(this.power)
179
+ }
180
+
181
+ public hasVariable(letter: string): boolean {
182
+ return this.polynom.hasVariable(letter)
183
+ }
184
+
185
+ public inverse(): this {
186
+ this.power.opposite()
187
+ return this
188
+ }
189
+
190
+ public isEqual(value: Factor): boolean {
191
+ // Must have the same polynom and the same reduce power
192
+
193
+ return this.isSameAs(value) &&
194
+ this.power.isEqual(value.power)
195
+ }
196
+
197
+ public isOne(): boolean {
198
+ return this.polynom.isOne() || this.power.isZero()
199
+ }
200
+
201
+ public isSameAs(value: InputAlgebra<Factor | Polynom>) {
202
+ let P: Polynom
203
+ if (value instanceof Factor) {
204
+ P = value.polynom
205
+ } else if (value instanceof Polynom) {
206
+ P = value
207
+ } else {
208
+ P = new Polynom(value)
209
+ }
210
+
211
+
212
+ return this.polynom.isEqual(P)
213
+ }
214
+
215
+ public isZero(): boolean {
216
+ return this.polynom.isZero()
217
+ }
218
+
219
+ public multiply(value: InputAlgebra<Factor | Polynom>): this {
220
+ if (value instanceof Factor) {
221
+ if (this.isSameAs(value)) {
222
+ this.power.add(value.power)
223
+ return this
224
+ }
225
+ }
226
+
227
+
228
+ const P = new Polynom(value as Polynom)
229
+ if (this.isSameAs(P)) {
230
+ this.power.add(1)
231
+ return this
232
+ }
233
+
234
+ throw new Error("The two factors must be the same")
235
+ }
236
+
237
+ public one(): this {
238
+ this.#polynom.one()
239
+ this.#power.one()
240
+ return this
241
+ }
242
+
243
+ public opposite(): Factor {
244
+ throw new Error("Method not implemented.")
245
+ }
246
+
247
+ public get polynom(): Polynom {
248
+ return this.#polynom
249
+ }
250
+
251
+ public set polynom(value: Polynom) {
252
+ this.#polynom = value
253
+ }
254
+
255
+ public pow(value: number | Fraction): this {
256
+ this.power.multiply(value)
257
+ return this
258
+ }
259
+
260
+ public get power(): Fraction {
261
+ return this.#power
262
+ }
263
+
264
+ public set power(value: InputValue<Fraction>) {
265
+ this.#power = new Fraction(value)
266
+ }
267
+
268
+ public primitive(): Factor {
269
+ throw new Error("Method not implemented.")
270
+ }
271
+
272
+ public reduce(): Factor {
273
+ throw new Error("Method not implemented.")
274
+ }
275
+
276
+ public root(value: number): this {
277
+ this.power.divide(value)
278
+ return this
279
+ }
280
+
281
+ public sqrt(): this {
282
+ return this.root(2)
283
+ }
284
+
285
+ public subtract(): Factor {
286
+ throw new Error("Subtracting two factors is not possible")
287
+ }
288
+
289
+ public tableOfSigns(): TABLE_OF_SIGNS {
290
+ const pow = this.power.clone().reduce()
291
+ const tos = this.polynom.tableOfSigns()
292
+
293
+ // The zero roots becomes defence (d) if the power is negative
294
+ if (pow.isStrictlyNegative()) {
295
+ tos.signs = replace_in_array(tos.signs, 'z', 'd')
296
+ }
297
+
298
+ // The - sign becomes
299
+ // + (plus) if the power num is even and the power den is odd
300
+ // i (invalid) if the power denominator is even
301
+ if (pow.denominator % 2 === 0) {
302
+ // it's an even roots : no negative values!
303
+ tos.signs = replace_in_array(tos.signs, '-', 'h')
304
+ } else if (pow.numerator % 2 === 0) {
305
+ // it's an even power : negative values becomes positive !
306
+ tos.signs = replace_in_array(tos.signs, '-', '+')
307
+ }
308
+
309
+
310
+ return {roots: tos.roots, signs: tos.signs}
311
+ }
312
+
313
+ public get variables(): string[] {
314
+ return this.polynom.variables
315
+ }
316
+
317
+ public get withPower(): this {
318
+ this.#displayMode = FACTOR_DISPLAY.POWER
319
+ return this
320
+ }
321
+
322
+ public get withRoot(): this {
323
+ this.#displayMode = FACTOR_DISPLAY.ROOT
324
+ return this
325
+ }
326
+
327
+ public zero(): this {
328
+ this.#polynom.zero()
329
+ this.#power.one()
330
+ return this
331
+ }
332
+
333
+ }
334
+
335
+ export enum FACTOR_DISPLAY {
336
+ ROOT,
337
+ POWER
338
+ }
339
+
@@ -0,0 +1,11 @@
1
+ // Exports every files from the algebra folder
2
+
3
+ export * from './equation'
4
+ export * from './equationSolver'
5
+ export * from './factor'
6
+ export * from './linearSystem'
7
+ export * from './logicalset'
8
+ export * from './monom'
9
+ export * from './polyFactor'
10
+ export * from './polynom'
11
+ export * from './matrix'