pimath 0.0.89 → 0.0.91

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.
@@ -629,69 +629,93 @@ export class Equation {
629
629
  // -b +- coeff\sqrt{radical}
630
630
  // -------------------------
631
631
  // 2a
632
- let gcd = Numeric.gcd(b, 2 * a, nthDelta.coefficient);
632
+ let gcd = Numeric.gcd(b, 2 * a, nthDelta.coefficient),
633
+ am = a/gcd, bm = b/gcd
633
634
  nthDelta.coefficient = nthDelta.coefficient / gcd;
634
635
 
635
- // TODO: Can i delete the next line ?
636
- // let deltaC = nthDelta.coefficient, deltaR = nthDelta.radical;
637
- if (b !== 0) {
638
- if (2 * a / gcd === 1) {
639
- this._solutions = [
640
- {
641
- tex: `${-b / gcd} - ${nthDelta.tex}`,
642
- value: realX1,
643
- exact: false // TODO: implement exact value with nthroot
644
- },
645
- {
646
- tex: `${-b / gcd} + ${nthDelta.tex}`,
647
- value: realX2,
648
- exact: false
649
- },
650
-
651
- ]
652
- } else {
653
- this._solutions = [
654
- {
655
- tex: `\\frac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
656
- value: realX1,
657
- exact: false
658
- },
659
- {
660
- tex: `\\frac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
661
- value: realX2,
662
- exact: false
663
- },
664
- ]
665
- }
666
- } else {
667
- if (2 * a / gcd === 1) {
668
- this._solutions = [
669
- {
670
- tex: `- ${nthDelta.tex}`,
671
- value: realX1,
672
- exact: false
673
- },
674
- {
675
- tex: `${nthDelta.tex}`,
676
- value: realX2,
677
- exact: false
678
- },
679
- ]
680
- } else {
681
- this._solutions = [
682
- {
683
- tex: `\\frac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
684
- value: realX1,
685
- exact: false
686
- },
687
- {
688
- tex: `\\frac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
689
- value: realX2,
690
- exact: false
691
- },
692
- ]
693
- }
636
+ if(a<0){
637
+ am = -am
638
+ bm = -bm
639
+ }
640
+
641
+ let tex1 = "", tex2 = ""
642
+
643
+ tex1 = `${bm!==0?((-bm) + ' - '):''}${nthDelta.tex}`
644
+ tex2 = `${bm!==0?((-bm) + ' + '):''}${nthDelta.tex}`
645
+
646
+ if(am!==1){
647
+ tex1 = `\\frac{ ${tex1} }{ ${2*am} }`
648
+ tex2 = `\\frac{ ${tex2} }{ ${2*am} }`
694
649
  }
650
+
651
+ this._solutions = [
652
+ {
653
+ tex: tex1, value: realX1, exact: false
654
+ },
655
+ {
656
+ tex: tex2, value: realX2, exact: false
657
+ },
658
+ ]
659
+
660
+
661
+ // if (b !== 0) {
662
+ // if (2 * a / gcd === 1) {
663
+ // this._solutions = [
664
+ // {
665
+ // tex: `${-b / gcd} - ${nthDelta.tex}`,
666
+ // value: realX1,
667
+ // exact: false // TODO: implement exact value with nthroot
668
+ // },
669
+ // {
670
+ // tex: `${-b / gcd} + ${nthDelta.tex}`,
671
+ // value: realX2,
672
+ // exact: false
673
+ // },
674
+ //
675
+ // ]
676
+ // } else {
677
+ // this._solutions = [
678
+ // {
679
+ // tex: `\\frac{${-b / gcd} - ${nthDelta.tex} }{ ${2 * a / gcd} }`,
680
+ // value: realX1,
681
+ // exact: false
682
+ // },
683
+ // {
684
+ // tex: `\\frac{${-b / gcd} + ${nthDelta.tex} }{ ${2 * a / gcd} }`,
685
+ // value: realX2,
686
+ // exact: false
687
+ // },
688
+ // ]
689
+ // }
690
+ // } else {
691
+ // if (2 * a / gcd === 1) {
692
+ // this._solutions = [
693
+ // {
694
+ // tex: `- ${nthDelta.tex}`,
695
+ // value: realX1,
696
+ // exact: false
697
+ // },
698
+ // {
699
+ // tex: `${nthDelta.tex}`,
700
+ // value: realX2,
701
+ // exact: false
702
+ // },
703
+ // ]
704
+ // } else {
705
+ // this._solutions = [
706
+ // {
707
+ // tex: `\\frac{- ${nthDelta.tex} }{ ${2 * a / gcd} }`,
708
+ // value: realX1,
709
+ // exact: false
710
+ // },
711
+ // {
712
+ // tex: `\\frac{${nthDelta.tex} }{ ${2 * a / gcd} }`,
713
+ // value: realX2,
714
+ // exact: false
715
+ // },
716
+ // ]
717
+ // }
718
+ // }
695
719
  } else {
696
720
  // -b +- d / 2a
697
721
  const S1 = new Fraction(-b - nthDelta.coefficient, 2 * a).reduce(),
@@ -231,7 +231,7 @@ export class Fraction {
231
231
  // The given value is a float number
232
232
  // Get the number of decimals after the float sign
233
233
  let [unit, decimal] = (value.toString()).split('.')
234
- let p: number = decimal.length;
234
+ let p: number = decimal?decimal.length:0;
235
235
 
236
236
  // Detect if the decimal part is periodic or not...
237
237
 
@@ -73,6 +73,24 @@ export class Line {
73
73
  }
74
74
  }
75
75
 
76
+ get display(): { canonical: string, mxh: string, parametric: string } {
77
+ // canonical => ax + by + c = 0
78
+ // mxh => y = -a/b x - c/b
79
+ // parametric => (xy) = OA + k*d // not relevant in display mode.
80
+
81
+ let canonical = this.equation;
82
+ // Make sur the first item is positive.
83
+ if (this._a.isNegative()) {
84
+ canonical.multiply(-1);
85
+ }
86
+
87
+ return {
88
+ canonical: canonical.display,
89
+ mxh: this.slope.isInfinity() ? 'x=' + this.OA.x.display : 'y=' + new Polynom().parse('x', this.slope, this.height).display,
90
+ parametric: ""
91
+ }
92
+ }
93
+
76
94
  get a(): Fraction {
77
95
  return this._a;
78
96
  }
@@ -80,6 +80,11 @@ export class Numeric{
80
80
  return Math.abs(g);
81
81
  }
82
82
 
83
+ static divideNumbersByGCD(...values: number[]): number[]{
84
+ let gcd = Numeric.gcd(...values)
85
+
86
+ return values.map(x=>x/gcd)
87
+ }
83
88
  /**
84
89
  * Least Common Multiple
85
90
  * @param values: list of numbers
@@ -137,7 +142,6 @@ export class Numeric{
137
142
  mod0 = mod.match(/0+$/g)
138
143
 
139
144
  if(mod0 && mod0[0].length>= number_of_digits){
140
- // The value can be changed. Remove all zeros!
141
145
  return +((value+epsilon).toString().split(mod0[0])[0])
142
146
  }
143
147
  }
@@ -35,7 +35,11 @@ export class rndFraction extends randomCore {
35
35
  if(this._config.natural){
36
36
  Q.denominator = 1
37
37
  }else {
38
- Q.denominator = Random.number(1, this._config.max)
38
+ let securityCount = 0
39
+ while(Q.isRelative() && securityCount<10) {
40
+ Q.denominator = Random.number(1, this._config.max)
41
+ securityCount++
42
+ }
39
43
  }
40
44
 
41
45
  return this._config.reduced?Q.reduce():Q
@@ -1,6 +1,8 @@
1
1
  import {describe} from "mocha";
2
2
  import {expect} from "chai";
3
3
  import {Equation} from "../../src/maths/algebra/equation";
4
+ import {PiMath} from "../../src";
5
+ import exp = require("constants");
4
6
 
5
7
  describe('Equations tests', () => {
6
8
  it('should get the solutions', () => {
@@ -33,4 +35,16 @@ describe('Equations tests', () => {
33
35
  '\\frac{-7 + \\sqrt{669} }{ 10 }'
34
36
  ] )
35
37
  })
38
+
39
+ it('should resolve an equation of second degree', ()=>{
40
+ let P = new Equation('-10x^2-8x+8=0')
41
+ P.solve()
42
+ expect(P.solutions[0].tex).to.have.oneOf(['\\frac{ -2 - 2\\sqrt{6} }{ 5 }', '\\frac{ -2 + 2\\sqrt{6} }{ 5 }'])
43
+ })
44
+
45
+ it('should solve this equation please', function () {
46
+ let P = new Equation("2/7(3x+5)=9-(3-x)/7")
47
+ P.solve()
48
+ console.log(P.solutions.map(x=>x.tex))
49
+ });
36
50
  })
@@ -1,6 +1,8 @@
1
1
  import {expect} from "chai";
2
2
  import {Fraction} from "../../src/maths/coefficients/fraction";
3
3
  import {describe} from "mocha";
4
+ import {Random} from "../../src/maths/randomization/random";
5
+ import exp = require("constants");
4
6
 
5
7
  describe('Fraction tests', () => { // the tests container
6
8
 
@@ -111,4 +113,19 @@ describe("Evaluate fraction", () => {
111
113
  expect(G.isApproximative()).to.be.false
112
114
  expect(G.isExact()).to.be.true
113
115
  });
116
+ })
117
+
118
+ describe('Generate function', ()=>{
119
+ it('should generate a non natural fraction', function () {
120
+ let F, result = true
121
+
122
+ for(let i=0; i<100; i++){
123
+ F = Random.fraction()
124
+ if(!F.isRelative()){
125
+ result = false
126
+ break
127
+ }
128
+ }
129
+ expect(F.isNatural()).to.be.false;
130
+ });
114
131
  })
@@ -0,0 +1,31 @@
1
+ import {Random} from "../src/maths/randomization/random";
2
+ import {Fraction} from "../esm/maths/coefficients/fraction";
3
+ import {NthRoot} from "../esm/maths/coefficients/nthRoot";
4
+
5
+ describe('Custom test', () => { // the tests container
6
+ it('Build sqrt value', () => {
7
+
8
+ let a = 1, b = 27, c = 50
9
+
10
+ a = Random.number(2,20)
11
+ b = Random.number(2,20)
12
+ c = Random.number(2,20)
13
+
14
+ let nthB = new NthRoot(b).reduce(),
15
+ nthC = new NthRoot(c).reduce(),
16
+ nthD = new NthRoot(nthB.radical * nthC.radical).reduce(),
17
+ F = new Fraction(a*nthB.coefficient*nthD.coefficient, nthC.radical*nthC.coefficient).reduce(),
18
+ answer = ''
19
+
20
+ if(F.numerator>1){
21
+ answer = `${F.numerator}`
22
+ }
23
+ if(nthD.radical>1){
24
+ answer = `${answer}sqrt${nthD.radical}`
25
+ }
26
+ if(F.denominator>1){
27
+ answer = `${answer}/${F.denominator}`
28
+ }
29
+ console.log(`${a}sqrt${b}/sqrt${c}`, answer)
30
+ })
31
+ });
@@ -1,14 +1,21 @@
1
1
  import {Numeric} from "../src/maths/numeric";
2
2
  import {expect} from "chai";
3
- import {Random} from "../src/maths/randomization/random";
4
3
 
5
4
  describe('Numeric', () => { // the tests container
6
5
  it('Correct number', () => {
7
6
  const a = 0.1 + 0.2
8
- console.log(a)
7
+ expect(Numeric.numberCorrection(a)).to.be.equal(0.3)
8
+ const b = Math.pow(10, -5)
9
9
 
10
- console.log(Random.prime(20))
10
+ expect(Numeric.numberCorrection(b, 1,12)).to.be.equal(0.00001)
11
+ })
11
12
 
12
- expect(Numeric.numberCorrection(a)).to.be.equal(0.3)
13
+
14
+ it('should simplify numbers by the gcd', () => {
15
+ let a = 12, b = 15, c = 33, d = 303;
16
+
17
+ [a, b, c, d] = Numeric.divideNumbersByGCD(a, b, c, d)
18
+
19
+ expect([a, b, c, d]).to.have.all.members([4, 5, 11, 101])
13
20
  })
14
21
  });