pimath 0.0.122 → 0.0.124

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.
@@ -8,6 +8,7 @@ import {Numeric} from '../numeric';
8
8
  import {Fraction} from "../coefficients/fraction";
9
9
  import {Equation, ISolution} from "./equation";
10
10
  import {Random} from "../randomization/random";
11
+ import {loadHighlighter} from "typedoc/dist/lib/utils/highlighter";
11
12
 
12
13
  export type PolynomParsingType = string | Polynom | number | Fraction | Monom
13
14
 
@@ -747,8 +748,11 @@ export class Polynom {
747
748
  isDeveloped = (polynomString: string): Boolean => {
748
749
  let P: Polynom;
749
750
 
751
+ // Start by removing the parenthis after a "power"
752
+ let pString = polynomString.replaceAll(/\^\(([-0-9/]+)\)/g, '$1')
753
+
750
754
  // There is at least one parenthese - it is not developed.
751
- if (polynomString.split('(').length + polynomString.split(')').length > 0) {
755
+ if (pString.includes('(') || pString.includes(')')) {
752
756
  return false
753
757
  }
754
758
 
@@ -766,16 +770,17 @@ export class Polynom {
766
770
  }
767
771
 
768
772
  // Check that everything is completely developed. Actually, there are no parentheses... so it is fully developed
769
-
770
- // maybe it wasn't reduced and not ordered...
771
- // compare polynom string.
772
-
773
- // normalize the string
774
- let polynomStringNormalized = polynomString.replaceAll('[*\s]', '')
775
-
776
- // Determine if it's the exact same string.
777
- // TODO: Maybe it's enough to just make this test !
778
- return polynomStringNormalized === P.reduce().reorder().display
773
+ return true
774
+
775
+ // // maybe it wasn't reduced and not ordered...
776
+ // // compare polynom string.
777
+ //
778
+ // // normalize the string
779
+ // let polynomStringNormalized = polynomString.replaceAll('[*\s]', '')
780
+ //
781
+ // // Determine if it's the exact same string.
782
+ // // TODO: Maybe it's enough to just make this test !a
783
+ // return polynomStringNormalized === P.reduce().reorder().display
779
784
  }
780
785
 
781
786
  // -------------------------------------
@@ -54,6 +54,10 @@ export class Rational {
54
54
  return `\\frac{ ${this._numerator.tex} }{ ${this._denominator.tex} }`;
55
55
  }
56
56
 
57
+ get display(): string {
58
+ return `(${this._numerator.display})/(${this._denominator.display})`;
59
+ }
60
+
57
61
  get texFactors(): string {
58
62
  return `\\frac{ ${this._numerator.texFactors} }{ ${this._denominator.texFactors} }`
59
63
  }
@@ -126,7 +130,15 @@ export class Rational {
126
130
  reduce = (): Rational => {
127
131
  this._numerator.factorize();
128
132
  for (let f of this._numerator.factors) {
129
- this.simplify(f);
133
+
134
+ if(f.degree().isZero()){
135
+ // Do the simplify only if the factor can divide the denominator
136
+ if(this._denominator.commonMonom().coefficient.clone().divide(f.monomByDegree().coefficient).isNatural()){
137
+ this.simplify(f);
138
+ }
139
+ }else {
140
+ this.simplify(f);
141
+ }
130
142
  }
131
143
 
132
144
  return this;
@@ -28,6 +28,12 @@ export interface remarquableLines {
28
28
  'B': Line,
29
29
  'C': Line,
30
30
  'intersection': Point
31
+ },
32
+ externalBisectors: {
33
+ 'A': Line,
34
+ 'B': Line,
35
+ 'C': Line,
36
+ 'intersection': Point
31
37
  }
32
38
  }
33
39
 
@@ -281,6 +287,10 @@ export class Triangle {
281
287
  }
282
288
 
283
289
  private _calculateRemarquableLines = (): remarquableLines => {
290
+ const bA= this._calculateBisectors('A'),
291
+ bB= this._calculateBisectors('B'),
292
+ bC= this._calculateBisectors('C')
293
+
284
294
  let remarquables: remarquableLines = {
285
295
  'medians': {
286
296
  'A': new Line(this._A, this._middles.BC),
@@ -301,9 +311,15 @@ export class Triangle {
301
311
  'intersection': null
302
312
  },
303
313
  'bisectors': {
304
- 'A': this._calculateBisectors('A'),
305
- 'B': this._calculateBisectors('B'),
306
- 'C': this._calculateBisectors('C'),
314
+ 'A': bA.internal,
315
+ 'B': bB.internal,
316
+ 'C': bB.internal,
317
+ 'intersection': null
318
+ },
319
+ externalBisectors: {
320
+ 'A': bA.external,
321
+ 'B': bB.external,
322
+ 'C': bC.external,
307
323
  'intersection': null
308
324
  }
309
325
  }
@@ -318,7 +334,7 @@ export class Triangle {
318
334
  return remarquables;
319
335
  }
320
336
 
321
- private _calculateBisectors = (pt: string): Line => {
337
+ private _calculateBisectors = (pt: string): { internal: Line, external: Line } => {
322
338
  let tlines = this.lines, d1, d2;
323
339
 
324
340
  if(pt==='A'){
@@ -337,16 +353,16 @@ export class Triangle {
337
353
 
338
354
  // Must determine which bisectors is in the triangle
339
355
  if(pt==='A'){
340
- return b1.hitSegment(this.B, this.C)?b1:b2;
356
+ return b1.hitSegment(this.B, this.C)?{internal:b1, external: b2}:{internal:b2, external: b1};
341
357
  }
342
358
  if(pt==='B'){
343
- return b1.hitSegment(this.A, this.C)?b1:b2;
359
+ return b1.hitSegment(this.A, this.C)?{internal:b1, external: b2}:{internal:b2, external: b1};
344
360
  }
345
361
  if(pt==='C'){
346
- return b1.hitSegment(this.B, this.A)?b1:b2;
362
+ return b1.hitSegment(this.B, this.A)?{internal:b1, external: b2}:{internal:b2, external: b1};
347
363
  }
348
364
 
349
365
  // Default returns the first bisector
350
- return b1;
366
+ return {internal:b1, external: b2}
351
367
  }
352
368
  }
@@ -97,7 +97,7 @@ export class NumExp {
97
97
  stack.push(this._numberCorrection(value))
98
98
  }
99
99
 
100
- evaluate(values: { [Key: string]: number }): number {
100
+ evaluate(values?: { [Key: string]: number }): number {
101
101
  const stack: number[] = []
102
102
 
103
103
  if(this._rpn===null){
@@ -254,22 +254,23 @@ export class Shutingyard {
254
254
  crtToken = expr[i]
255
255
  nextToken = expr[i + 1]
256
256
  normalizedExpr += crtToken
257
+
257
258
  if (crtToken.match(/[a-zA-Z]/g)) {
258
259
  // Current element is a letter.
259
260
  // if the next element is a letter, a number or an opening parentheses, add the multiplication sign.
260
- if (nextToken.match(/[a-zA-Z\d(]/)) {
261
+ if (nextToken?.match(/[a-zA-Z\d(]/)) {
261
262
  normalizedExpr += '*'
262
263
  }
263
264
  } else if (crtToken.match(/\d/)) {
264
265
  // Current element is a number.
265
266
  // if the next element is a letter or a parentheses, add the multiplication sign.
266
- if (nextToken.match(/[a-zA-Z(]/)) {
267
+ if (nextToken?.match(/[a-zA-Z(]/)) {
267
268
  normalizedExpr += '*'
268
269
  }
269
270
  } else if (crtToken === ')') {
270
271
  // Current element is a closing parentheses.
271
272
  // if the next element is a letter, a number or an opening parentheses, add the multiplication sign
272
- if (nextToken.match(/[a-zA-Z\d(]/)) {
273
+ if (nextToken?.match(/[a-zA-Z\d(]/)) {
273
274
  normalizedExpr += '*'
274
275
  }
275
276
  }
@@ -279,7 +280,7 @@ export class Shutingyard {
279
280
  }
280
281
 
281
282
  // add the last token
282
- return normalizedExpr + nextToken
283
+ return normalizedExpr + (nextToken===undefined?'':nextToken)
283
284
  }
284
285
 
285
286
  // /**
@@ -374,6 +375,7 @@ export class Shutingyard {
374
375
  // Normalize the input if required.
375
376
  if (uniformize || this._uniformize) expr = this.normalize(expr)
376
377
 
378
+
377
379
  let securityLoopLvl1 = 50,
378
380
  securityLoopLvl2_default = 50,
379
381
  securityLoopLvl2;
@@ -139,6 +139,14 @@ describe('Polynom tests', () => {
139
139
  expect(Q.isFactorized('4(2x+5)(x-6)')).to.be.true
140
140
  expect(Q.isFactorized('(8x+20)(x-6)')).to.be.false
141
141
  expect(Q.isFactorized('(8x+20)(x-6)', true)).to.be.true
142
+ });
143
+
144
+ it('should check if a polynom is developed', function () {
145
+ let P = new Polynom('x(x+1)')
146
+
147
+ expect(P.isDeveloped('x^(2)+x')).true
148
+ expect(P.isDeveloped('x^2+x')).true
149
+ expect(P.isDeveloped('x(x+1)')).false
142
150
 
143
151
  });
144
152
 
@@ -56,4 +56,9 @@ describe('Rational tests', () => {
56
56
 
57
57
  expect(P.plotFunction).to.be.equal("(245*x-490)/(6*x^(2)-19*x+15)")
58
58
  });
59
+
60
+ it('should reduce withouth creating fraction', function () {
61
+ let P = new Rational('4(x+1)', '(x+1)(x-3)')
62
+ expect(P.reduce().display).to.be.equal('(4)/(x-3)')
63
+ });
59
64
  })
@@ -65,12 +65,17 @@ describe('Numerical expression', () => { // the tests container
65
65
  it('should work with constant', function () {
66
66
 
67
67
  let k = new NumExp('2pi*x')
68
- expect(k.evaluate({x: 1})).to.be.equal(6.283186)
68
+ expect(+k.evaluate({x: 1}).toFixed(6)).to.be.equal(6.283185)
69
69
  });
70
70
 
71
71
  it('should work with constant but without variables', function () {
72
72
 
73
73
  let k = new NumExp('2pi')
74
- expect(k.evaluate()).to.be.equal(6.283186)
74
+ expect(+k.evaluate().toFixed(6)).to.be.equal(6.283185)
75
+ });
76
+
77
+ it('should parse with ln or log', function () {
78
+ let k = new NumExp('ln(3)')
79
+ expect(+k.evaluate().toFixed(6)).to.be.equal(1.098612)
75
80
  });
76
81
  });
@@ -1,107 +0,0 @@
1
- Index: src/index.ts
2
- IDEA additional info:
3
- Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
4
- <+>import {Numeric} from \"./maths/numeric\";\r\nimport {NumExp} from \"./maths/expressions/numexp\";\r\nimport {Shutingyard} from \"./maths/shutingyard\";\r\nimport {Random} from \"./maths/randomization/random\";\r\nimport {Fraction} from \"./maths/coefficients/fraction\";\r\nimport {NthRoot} from \"./maths/coefficients/nthRoot\";\r\nimport {Monom} from \"./maths/algebra/monom\";\r\nimport {Polynom} from \"./maths/algebra/polynom\";\r\nimport {Equation} from \"./maths/algebra/equation\";\r\nimport {LinearSystem} from \"./maths/algebra/linearSystem\";\r\nimport {Rational} from \"./maths/algebra/rational\";\r\nimport {Logicalset} from \"./maths/algebra/logicalset\";\r\nimport {PolynomExpFactor, PolynomExpProduct} from \"./maths/expressions/polynomexp\";\r\nimport {Vector} from \"./maths/geometry/vector\";\r\nimport {Line} from \"./maths/geometry/line\";\r\nimport {Triangle} from \"./maths/geometry/triangle\";\r\nimport {Circle} from \"./maths/geometry/circle\";\r\nimport {Point} from \"./maths/geometry/point\";\r\n\r\n// Expose as global\r\nexport const PiMath = {\r\n ShutingYard: Shutingyard,\r\n Numeric: Numeric,\r\n NumExp: NumExp,\r\n Fraction: Fraction,\r\n Root: NthRoot,\r\n Monom: Monom,\r\n Polynom: Polynom,\r\n Equation: Equation,\r\n LinearSystem: LinearSystem,\r\n Rational: Rational,\r\n Logicalset: Logicalset,\r\n Random: Random,\r\n PolynomExpFactor: PolynomExpFactor,\r\n PolynomExpProduct: PolynomExpProduct,\r\n Geometry: {\r\n Vector: Vector,\r\n Point: Point,\r\n Line: Line,\r\n Triangle: Triangle,\r\n Circle: Circle\r\n }\r\n};\r\n\r\n(<any>window).Pi = PiMath\r\n
5
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
6
- <+>UTF-8
7
- ===================================================================
8
- diff --git a/src/index.ts b/src/index.ts
9
- --- a/src/index.ts
10
- +++ b/src/index.ts
11
- @@ -42,4 +42,5 @@
12
- }
13
- };
14
-
15
- -(<any>window).Pi = PiMath
16
- +// rename window.Pi to window.PiMath
17
- +(<any>window).PiMath = PiMath
18
- Index: tests/numexp.test.ts
19
- IDEA additional info:
20
- Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
21
- <+>import {expect} from 'chai';\r\nimport {NumExp} from \"../src/maths/expressions/numexp\";\r\nimport {Numeric} from \"../src/maths/numeric\";\r\n\r\ndescribe('Numerical expression', () => { // the tests container\r\n it('RPN for numerical expression', () => {\r\n const RPN = new NumExp('3*x+5').rpn\r\n expect(RPN.map(x => x.token)).to.have.all.members(['3', 'x', '*', '5', '+'])\r\n\r\n const RPN2 = new NumExp('-3*x^2-5').rpn\r\n expect(RPN2.map(x => x.token)).to.have.all.members(['3', 'x', '2', '^', '*', '-', '5', '-'])\r\n })\r\n\r\n it('Evaluate for numerical expression', () => {\r\n const expr = new NumExp('3*x+5')\r\n expect(expr.evaluate({x: 5})).to.be.equal(20)\r\n\r\n const expr2 = new NumExp('-3*x^2-5')\r\n expect(expr2.evaluate({x: -2})).to.be.equal(-17)\r\n })\r\n\r\n it('Evaluation simple mathematical functions', () => {\r\n const expr = new NumExp('sqrt(x)')\r\n expect(expr.evaluate({x: 9})).to.be.equal(3)\r\n })\r\n\r\n it('should detect invalid rpn parsing', function () {\r\n const exprValid = new NumExp('3*sin(x)'),\r\n exprInvalid = new NumExp('3*sin')\r\n\r\n expect(exprValid.isValid).to.be.true\r\n expect(exprInvalid.isValid).to.be.false\r\n });\r\n\r\n it('souldd detect invalid expression withouth crahsing', function() {\r\n const exprPourrie = new NumExp('3xsi'),\r\n exprOk = new NumExp('3xsin(x)')\r\n\r\n expect(exprPourrie.isValid).to.be.false\r\n expect(exprOk.isValid).to.be.true\r\n })\r\n\r\n it('should parse without mult sign', function () {\r\n\r\n let a = 1 / 5\r\n\r\n const expr = new NumExp('3x-5', true)\r\n expect(expr.isValid).to.be.true\r\n expect(expr.evaluate({x: 2})).to.be.equal(1)\r\n\r\n const expr2 = new NumExp('3*x-5', true)\r\n expect(expr2.isValid).to.be.true\r\n expect(expr2.evaluate({x: 2})).to.be.equal(1)\r\n });\r\n\r\n it('should calculate sqrt from exp', function(){\r\n // let a = new NumExp('x^(1/3)')\r\n // console.log(a.evaluate({x: 8}))\r\n\r\n let k = new NumExp('nthrt(x,3)')\r\n expect(k.evaluate({x: -8})).to.be.equal(-2)\r\n expect(k.evaluate({x: 27})).to.be.equal(3)\r\n\r\n let p = new NumExp('nthrt(x,4)')\r\n expect(p.evaluate({x: 16})).to.be.equal(2)\r\n expect(p.evaluate({x: -16})).to.be.NaN\r\n })\r\n});\r\n
22
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
23
- <+>UTF-8
24
- ===================================================================
25
- diff --git a/tests/numexp.test.ts b/tests/numexp.test.ts
26
- --- a/tests/numexp.test.ts
27
- +++ b/tests/numexp.test.ts
28
- @@ -65,4 +65,16 @@
29
- expect(p.evaluate({x: 16})).to.be.equal(2)
30
- expect(p.evaluate({x: -16})).to.be.NaN
31
- })
32
- +
33
- + it('should work with constant', function () {
34
- +
35
- + let k = new NumExp('2pi*x')
36
- + expect(k.evaluate({x: 1})).to.be.equal(6.283186)
37
- + });
38
- +
39
- + it('should work with constant but without variables', function () {
40
- +
41
- + let k = new NumExp('2pi')
42
- + expect(k.evaluate()).to.be.equal(6.283186)
43
- + });
44
- });
45
- Index: tests/algebra/linear.test.ts
46
- IDEA additional info:
47
- Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
48
- <+>import {describe} from \"mocha\";\r\nimport {expect} from \"chai\";\r\nimport {LinearSystem} from \"../../src/maths/algebra/linearSystem\";\r\nimport {Polynom} from \"../../src/maths/algebra/polynom\";\r\nimport exp = require(\"constants\");\r\n\r\ndescribe('Linear systems tests', () => {\r\n it('should solve a 2x2 equations', () => {\r\n let LS = new LinearSystem(\r\n '4x+5y=11',\r\n '7y-24=3x'\r\n )\r\n LS.solve(true)\r\n\r\n expect(LS.solution).to.be.equal('\\\\left(-1;3\\\\right)')\r\n })\r\n it('should solve a 3x3 equations', () => {\r\n let LS = new LinearSystem(\r\n '2x+7y-z=-3',\r\n '-3x+2y+3z=12',\r\n '-5x-3y+2z=5'\r\n )\r\n\r\n LS.solve()\r\n console.log(LS.solution)\r\n })\r\n\r\n it('should solve a 3x3 equations II ', () => {\r\n let LS = new LinearSystem(\r\n '-x+y-z=-6',\r\n '3x+2y+z=14',\r\n '5x+y+3z=7'\r\n )\r\n\r\n LS.solve()\r\n console.log(LS.solution)\r\n })\r\n\r\n it('should calculate the reduction', function () {\r\n let E1 = new Polynom('6x+21y-3z'),\r\n E2 = new Polynom('-6x+21y-3z')\r\n\r\n // Start from hre\r\n console.log('------------')\r\n console.log(E1.tex, E2.tex)\r\n\r\n console.log(E1.monoms.map(x => x.tex))\r\n console.log(E2.monoms.map(x => x.tex))\r\n E1.add(E2);\r\n\r\n console.log(E1.tex)\r\n });\r\n\r\n it('should use a reduced linear reducation', function () {\r\n let LS = new LinearSystem(\r\n '3x-6y+3=0',\r\n 'x+12y-6=0'\r\n )\r\n LS.solve(true)\r\n\r\n const tex = LS.stepTex('x')\r\n\r\n expect(+LS.resolutionSteps['x'][0].operations[0][0]).to.be.equal(2)\r\n expect(+LS.resolutionSteps['x'][0].operations[1][0]).to.be.equal(1)\r\n });\r\n})\r\n
49
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
50
- <+>UTF-8
51
- ===================================================================
52
- diff --git a/tests/algebra/linear.test.ts b/tests/algebra/linear.test.ts
53
- --- a/tests/algebra/linear.test.ts
54
- +++ b/tests/algebra/linear.test.ts
55
- @@ -63,4 +63,34 @@
56
- expect(+LS.resolutionSteps['x'][0].operations[0][0]).to.be.equal(2)
57
- expect(+LS.resolutionSteps['x'][0].operations[1][0]).to.be.equal(1)
58
- });
59
- +
60
- + it('should calculate what i need', function (){
61
- + let LS = new LinearSystem(
62
- + '15x-8y+16=0',
63
- + '5x+12y-24=0'
64
- + )
65
- + LS.solve()
66
- + console.log(LS.solution)
67
- +
68
- + let LS2 = new LinearSystem(
69
- + '15x-8y+16=0',
70
- + '3x-4y-6=0'
71
- + )
72
- + LS2.solve()
73
- + console.log(LS2.solution)
74
- +
75
- + let LS3 = new LinearSystem(
76
- + '5x+12y-24=0',
77
- + '3x-4y-6=0'
78
- + )
79
- + LS3.solve()
80
- + console.log(LS3.solution)
81
- +
82
- + let LS4 = new LinearSystem(
83
- + '70x+25y-50=0',
84
- + '63x-54y-11=0'
85
- + )
86
- + LS4.solve()
87
- + console.log(LS4.solution)
88
- + })
89
- })
90
- Index: LICENSE.md
91
- IDEA additional info:
92
- Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
93
- <+>MIT License\r\n\r\nCopyright (c) 2020 Basil Gass\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\nSOFTWARE.\r\n
94
- Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
95
- <+>UTF-8
96
- ===================================================================
97
- diff --git a/LICENSE.md b/LICENSE.md
98
- --- a/LICENSE.md
99
- +++ b/LICENSE.md
100
- @@ -1,6 +1,6 @@
101
- MIT License
102
-
103
- -Copyright (c) 2020 Basil Gass
104
- +Copyright (c) 2023 Basil Gass
105
-
106
- Permission is hereby granted, free of charge, to any person obtaining a copy
107
- of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +0,0 @@
1
- <changelist name="Uncommitted_changes_before_Update_at_24_07_2023_15_31_[Default_Changelist]1" date="1690205596619" recycled="true" deleted="true">
2
- <option name="PATH" value="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Update_at_24_07_2023_15_31_[Default_Changelist]1/shelved.patch" />
3
- <option name="DESCRIPTION" value="Uncommitted changes before Update at 24.07.2023 15:31 [Default Changelist]" />
4
- </changelist>