pimath 0.0.101 → 0.0.102

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.
@@ -13,7 +13,7 @@ export class LinearSystem {
13
13
 
14
14
  constructor(...equationStrings: string[]) {
15
15
  this._equations = [];
16
- this._letters = 'xy'.split('');
16
+ this._letters = 'xyz'.split('');
17
17
 
18
18
  if (equationStrings !== undefined && equationStrings.length > 0) {
19
19
  this.parse(...equationStrings);
@@ -96,9 +96,7 @@ export class LinearSystem {
96
96
  equArray.push(equStr.join('&'));
97
97
  }
98
98
 
99
-
100
99
  return `\\left\\{\\begin{array}{${"r".repeat(letters.length)}cl}${equArray.join('\\\\\ ')}\\end{array}\\right.`;
101
- //return `\\left\\{\\begin{array}{rrrcl}${this._equations.map(equ => `${equ.tex}`).join('\\\\\ \n')}\\end{array}\\right.`;
102
100
  }
103
101
 
104
102
  get solution(): string {
@@ -118,9 +116,9 @@ export class LinearSystem {
118
116
  return;
119
117
  }
120
118
 
121
- tex.push(this._solutions[letter].value.frac);
119
+ tex.push(this._solutions[letter].value.tex);
122
120
  }
123
- return `(${tex.join(';')})`;
121
+ return `\\left(${tex.join(';')}\\right)`;
124
122
  }
125
123
 
126
124
  // ------------------------------------------
@@ -227,6 +225,7 @@ export class LinearSystem {
227
225
  let c1 = eq1.left.monomByDegree(1, letter).coefficient.clone(),
228
226
  c2 = eq2.left.monomByDegree(1, letter).coefficient.clone().opposed();
229
227
 
228
+ console.log('reduction: ', letter, eq1.tex, eq2.tex, c2.tex, c1.tex)
230
229
  return this.mergeEquations(eq1, eq2, c2, c1);
231
230
  }
232
231
 
@@ -236,10 +235,16 @@ export class LinearSystem {
236
235
  let eq1multiplied = eq1.clone().multiply(new Fraction(factor1)),
237
236
  eq2multiplied = eq2.clone().multiply(new Fraction(factor2));
238
237
 
238
+ // @ts-ignore
239
+ console.log(eq1.tex, eq1multiplied.tex, factor1.tex)
240
+ // @ts-ignore
241
+ console.log(eq2.tex, eq2multiplied.tex, factor2.tex)
242
+
239
243
  // Add both equations together.
240
244
  eq1multiplied.left.add(eq2multiplied.left);
241
245
  eq1multiplied.right.add(eq2multiplied.right);
242
246
 
247
+ console.log('resulting reduction', eq1multiplied.tex)
243
248
  return eq1multiplied;
244
249
  }
245
250
 
@@ -266,6 +271,7 @@ export class LinearSystem {
266
271
  let V = this.variables.sort();
267
272
 
268
273
  for (let letter of V) {
274
+ console.log('SOLVING FOR', letter)
269
275
  this._solutions[letter] = this._solveOneLetter(letter, V)
270
276
  }
271
277
 
@@ -278,6 +284,12 @@ export class LinearSystem {
278
284
  return true;
279
285
  };
280
286
 
287
+ /**
288
+ * Linear reduction of the equations to have only one letter
289
+ * @param letter letter to isolate
290
+ * @param V list of variables in the linear system.
291
+ * @private
292
+ */
281
293
  private _solveOneLetter(letter: string, V: string[]): { value: Fraction, isReal: boolean, isVarnothing: boolean } {
282
294
  // list of equations.
283
295
  let LE: Equation[] = this.clone().equations,
@@ -291,12 +303,15 @@ export class LinearSystem {
291
303
  continue;
292
304
  }
293
305
 
306
+ console.log('Removing the variable: ', L)
294
307
  // Linear reduction.
295
308
  // TODO: Search for better association
296
309
  for (let i = 0; i < LE.length - 1; i++) {
297
310
  reducedEquations.push(this._linearReduction(LE[i], LE[i + 1], L));
298
311
  }
299
312
 
313
+ console.log(reducedEquations.map(x=>x.tex))
314
+
300
315
  // Keep track of each steps.
301
316
  this._resolutionSteps.push(new LinearSystem().parse(...reducedEquations));
302
317
 
@@ -309,8 +324,8 @@ export class LinearSystem {
309
324
 
310
325
  // Solve the equations
311
326
  let E = this._resolutionSteps[this._resolutionSteps.length - 1].equations[0];
312
- E.solve();
313
-
327
+ E.solve()
328
+ console.log('Solutions for ', letter, ': ', E.solutions[0].tex)
314
329
  return {
315
330
  value: new Fraction(E.solutions[0].value),
316
331
  isReal: E.isReal,
@@ -525,7 +525,7 @@ export class Monom {
525
525
  }
526
526
  this._coefficient.add(m.coefficient);
527
527
  } else {
528
- console.log('Add: Is not similar: ', m.display);
528
+ console.log('Add monom: ' + this.display + ' is not similar with ', m.display);
529
529
  }
530
530
  }
531
531
  return this;
@@ -401,6 +401,7 @@ export class Polynom {
401
401
  add = (...values: unknown[]): Polynom => {
402
402
  this.mark_as_dirty()
403
403
 
404
+ // @ts-ignore
404
405
  for (let value of values) {
405
406
  if (value instanceof Polynom) {
406
407
  this._monoms = this._monoms.concat(value.monoms);
@@ -473,7 +474,6 @@ export class Polynom {
473
474
  quotient: this.clone().divide(P),
474
475
  reminder: new Polynom().zero()
475
476
  }
476
-
477
477
  }
478
478
 
479
479
  // Get at least a letter
@@ -634,7 +634,7 @@ export class Polynom {
634
634
 
635
635
  for (let x of polynomStringNormalized.matchAll(/\(([a-z0-9+\-]+)\)(\^[0-9]*)?/g)) {
636
636
  if (x[2] !== undefined) {
637
- for (let i = 0; i < +x[2].substr(1); i++) {
637
+ for (let i = 0; i < +x[2].substring(1); i++) {
638
638
  factors.push(x[1])
639
639
  }
640
640
  } else {
@@ -650,9 +650,19 @@ export class Polynom {
650
650
  // Factorize the current polynom.
651
651
  this.factorize();
652
652
 
653
+ // console.log('RESULT BEFORE COMPARE')
654
+ // console.log(polynomString, polynomStringNormalized)
655
+ // console.log(factors)
656
+ // console.log(this.factors.map(x=>x.display))
657
+
653
658
  // Compare the given factors with the generated factors
654
659
  let sign = 1;
655
660
  for (let f of this.factors) {
661
+ if(f.degree().isZero()){
662
+ if(f.monoms[0].coefficient.isNegativeOne()){
663
+ sign=-sign
664
+ }
665
+ }
656
666
  for (let i = 0; i < polyFactors.length; i++) {
657
667
  if (f.isEqual(polyFactors[i])) {
658
668
  polyFactors.splice(i, 1);
@@ -724,13 +734,38 @@ export class Polynom {
724
734
 
725
735
  // -------------------------------------
726
736
  reduce = (): Polynom => {
727
- for (let i = 0; i < this._monoms.length; i++) {
728
- for (let j = i + 1; j < this._monoms.length; j++) {
729
- if (this._monoms[i].isSameAs(this.monoms[j])) {
730
- this._monoms[i].add(this.monoms[j]);
731
- this._monoms.splice(j, 1);
737
+ // Reduce the polynom
738
+ let values = [...this._monoms],
739
+ vars = [...this.variables]
740
+
741
+ this._monoms = []
742
+
743
+ let coeffs = values.filter(x=>x.variables.length===0)
744
+
745
+ if(coeffs.length>0){
746
+ this._monoms.push(coeffs.reduce((a, b)=>a.add(b)))
747
+ }
748
+
749
+ // Build the new monoms
750
+ for(let letter of vars){
751
+ // Monom with same letters, but might be of different degrees
752
+ let M = values.filter(x=>x.hasLetter(letter))
753
+
754
+ while(M.length>0){
755
+ // Take the first element
756
+ const m = M.shift(), degree=m.degree(letter)
757
+
758
+ for(let a of M.filter(x=>x.degree(letter).isEqual(degree))){
759
+ m.add(a)
732
760
  }
761
+
762
+ this._monoms.push(m)
763
+
764
+ // Make the new array.
765
+ M = M.filter(x=>x.degree(letter).isNotEqual(degree))
733
766
  }
767
+ // reduce the monom
768
+
734
769
  }
735
770
 
736
771
  // Remove all null monoms
@@ -746,7 +781,7 @@ export class Polynom {
746
781
  if (this.length === 0) {
747
782
  return new Polynom().zero();
748
783
  }
749
- return this;
784
+ return this.reorder();
750
785
  };
751
786
 
752
787
  reorder = (letter: string = 'x'): Polynom => {
@@ -754,7 +789,7 @@ export class Polynom {
754
789
  this._monoms.sort(function (a, b) {
755
790
  return b.degree(letter).clone().subtract(a.degree(letter)).value
756
791
  });
757
- return this.reduce();
792
+ return this;
758
793
  };
759
794
 
760
795
  degree = (letter?: string): Fraction => {
@@ -871,7 +906,6 @@ export class Polynom {
871
906
  return this._factors
872
907
  }
873
908
 
874
-
875
909
  let factors: Polynom[] = [];
876
910
  let P = this.clone().reorder()
877
911
 
@@ -879,7 +913,7 @@ export class Polynom {
879
913
  // 2x^3+6x^2 => 2x^2
880
914
  let M = P.commonMonom()
881
915
  // If the polynom starts with a negative monom, factorize it.
882
- if(P.monomByDegree().coefficient.isStrictlyNegative() && M.coefficient.isStrictlyPositive()){
916
+ if(P.monomByDegree().coefficient.isStrictlyNegative() && M.coefficient.isStrictlyPositive() && !M.isOne()){
883
917
  M.opposed()
884
918
  }
885
919
 
@@ -1264,7 +1298,7 @@ export class Polynom {
1264
1298
  this.add(stack[0])
1265
1299
  }
1266
1300
 
1267
- return this
1301
+ return this.reorder()
1268
1302
  }
1269
1303
 
1270
1304
  private multiplyByPolynom = (P: Polynom): Polynom => {
@@ -0,0 +1,54 @@
1
+ import {describe} from "mocha";
2
+ import {expect} from "chai";
3
+ import {Equation} from "../../src/maths/algebra/equation";
4
+ import {PiMath} from "../../src";
5
+ import exp = require("constants");
6
+ import {LinearSystem} from "../../src/maths/algebra/linearSystem";
7
+ import {Polynom} from "../../src/maths/algebra/polynom";
8
+
9
+ describe('Linear systems tests', () => {
10
+ it('should solve a 2x2 equations', () => {
11
+ let LS = new LinearSystem(
12
+ '4x+5y=11',
13
+ '7y-24=3x'
14
+ )
15
+ LS.solve()
16
+ expect(LS.solution).to.be.equal('(-1;3)')
17
+ })
18
+ it('should solve a 3x3 equations', () => {
19
+ let LS = new LinearSystem(
20
+ '2x+7y-z=-3',
21
+ '-3x+2y+3z=12',
22
+ '-5x-3y+2z=5'
23
+ )
24
+
25
+ LS.solve()
26
+ console.log(LS.solution)
27
+ })
28
+
29
+ it('should solve a 3x3 equations II ', () => {
30
+ let LS = new LinearSystem(
31
+ '-x+y-z=-6',
32
+ '3x+2y+z=14',
33
+ '5x+y+3z=7'
34
+ )
35
+
36
+ LS.solve()
37
+ console.log(LS.solution)
38
+ })
39
+
40
+ it('should calculate the reduction', function () {
41
+ let E1 = new Polynom('6x+21y-3z'),
42
+ E2 = new Polynom('-6x+21y-3z')
43
+
44
+ // Start from hre
45
+ console.log('------------')
46
+ console.log(E1.tex, E2.tex)
47
+
48
+ console.log(E1.monoms.map(x=>x.tex))
49
+ console.log(E2.monoms.map(x=>x.tex))
50
+ E1.add(E2);
51
+
52
+ console.log(E1.tex)
53
+ });
54
+ })
@@ -9,7 +9,7 @@ import exp = require("constants");
9
9
  describe('Polynom tests', () => {
10
10
  it('Parse polynom', () => {
11
11
  const options = new Polynom('2x(x+3)^2(x-1)');
12
- options.reorder().reduce();
12
+ options.reorder();
13
13
  expect(options.tex).to.be.equal('2x^{4}+10x^{3}+6x^{2}-18x');
14
14
  });
15
15
 
@@ -102,14 +102,20 @@ describe('Polynom tests', () => {
102
102
 
103
103
  it('should detect if a polynom is factorized', function (){
104
104
  let P = new Polynom('x-1')
105
-
106
105
  expect(P.isFactorized('x-1')).to.be.true
107
106
  expect(P.isFactorized('x-2')).to.be.false
108
107
 
109
- let Q = new Polynom('(x-1)(x+2)')
108
+ let R = new Polynom('(x+2)^2')
109
+ expect(R.isFactorized('(x+2)^2')).to.be.true
110
110
 
111
+ let Q = new Polynom('(x-1)(x+2)')
111
112
  expect(Q.isFactorized('(x+2)(x-1)')).to.be.true
112
113
  expect(Q.isFactorized('x^2+x-2')).to.be.false
114
+
115
+ let T = new Polynom('(x-3)(1-x)')
116
+ expect(T.isFactorized('(x-1)(3-x)')).to.be.true
117
+ expect(T.isFactorized('(x-1)(x-3)')).to.be.false
118
+ expect(T.isFactorized('-1(x-1)(x-3)')).to.be.false
113
119
  })
114
120
  })
115
121