pimath 0.0.28 → 0.0.32

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 (96) hide show
  1. package/dev/pi.js +864 -487
  2. package/dev/pi.js.map +1 -1
  3. package/dist/pi.js +1 -1
  4. package/dist/pi.js.map +1 -1
  5. package/docs/assets/search.js +1 -1
  6. package/docs/classes/algebra.Equation.html +9 -9
  7. package/docs/classes/algebra.LinearSystem.html +1 -1
  8. package/docs/classes/algebra.Logicalset.html +2 -2
  9. package/docs/classes/algebra.Monom.html +42 -40
  10. package/docs/classes/algebra.Polynom.html +10 -10
  11. package/docs/classes/algebra.PolynomExpFactor.html +1 -0
  12. package/docs/classes/algebra.PolynomExpProduct.html +1 -0
  13. package/docs/classes/algebra.Rational.html +2 -2
  14. package/docs/classes/coefficients.Fraction.html +4 -4
  15. package/docs/classes/coefficients.Nthroot.html +1 -1
  16. package/docs/classes/geometry.Circle.html +2 -2
  17. package/docs/classes/geometry.Line.html +2 -2
  18. package/docs/classes/geometry.Point.html +1 -1
  19. package/docs/classes/geometry.Triangle.html +6 -6
  20. package/docs/classes/geometry.Vector.html +1 -1
  21. package/docs/classes/numeric.Numeric.html +5 -5
  22. package/docs/classes/shutingyard.Shutingyard.html +7 -8
  23. package/docs/enums/shutingyard.ShutingyardMode.html +1 -0
  24. package/docs/enums/shutingyard.ShutingyardType.html +1 -0
  25. package/docs/index.html +1 -1
  26. package/docs/interfaces/geometry.remarquableLines.html +1 -1
  27. package/docs/modules/algebra.html +1 -1
  28. package/docs/modules/coefficients.html +1 -1
  29. package/docs/modules/geometry.html +1 -1
  30. package/docs/modules/numeric.html +1 -1
  31. package/docs/modules/random.Random.html +1 -1
  32. package/docs/modules/random.html +1 -1
  33. package/docs/modules/shutingyard.html +1 -1
  34. package/docs/modules.html +1 -1
  35. package/esm/main.js +3 -1
  36. package/esm/main.js.map +1 -1
  37. package/esm/maths/algebra/equation.js +1 -1
  38. package/esm/maths/algebra/equation.js.map +1 -1
  39. package/esm/maths/algebra/index.d.ts +1 -0
  40. package/esm/maths/algebra/index.js +1 -0
  41. package/esm/maths/algebra/index.js.map +1 -1
  42. package/esm/maths/algebra/monom.d.ts +4 -1
  43. package/esm/maths/algebra/monom.js +52 -37
  44. package/esm/maths/algebra/monom.js.map +1 -1
  45. package/esm/maths/algebra/polynom.d.ts +19 -15
  46. package/esm/maths/algebra/polynom.js +242 -174
  47. package/esm/maths/algebra/polynom.js.map +1 -1
  48. package/esm/maths/coefficients/fraction.d.ts +3 -0
  49. package/esm/maths/coefficients/fraction.js +21 -8
  50. package/esm/maths/coefficients/fraction.js.map +1 -1
  51. package/esm/maths/{numexp.d.ts → expressions/numexp.d.ts} +3 -0
  52. package/esm/maths/{numexp.js → expressions/numexp.js} +46 -15
  53. package/esm/maths/expressions/numexp.js.map +1 -0
  54. package/esm/maths/expressions/polynomexp.bkp.d.ts +33 -0
  55. package/esm/maths/expressions/polynomexp.bkp.js +186 -0
  56. package/esm/maths/expressions/polynomexp.bkp.js.map +1 -0
  57. package/esm/maths/expressions/polynomexp.d.ts +52 -0
  58. package/esm/maths/expressions/polynomexp.js +233 -0
  59. package/esm/maths/expressions/polynomexp.js.map +1 -0
  60. package/esm/maths/geometry/line.d.ts +4 -2
  61. package/esm/maths/geometry/line.js +6 -2
  62. package/esm/maths/geometry/line.js.map +1 -1
  63. package/esm/maths/geometry/vector.js +7 -2
  64. package/esm/maths/geometry/vector.js.map +1 -1
  65. package/esm/maths/shutingyard.d.ts +7 -7
  66. package/esm/maths/shutingyard.js +5 -7
  67. package/esm/maths/shutingyard.js.map +1 -1
  68. package/package.json +1 -1
  69. package/{dev → public}/demo.css +0 -0
  70. package/{dev → public}/index.html +48 -14
  71. package/{dev → public}/playground.html +1 -1
  72. package/src/main.ts +13 -2
  73. package/src/maths/algebra/equation.ts +1 -1
  74. package/src/maths/algebra/index.ts +2 -1
  75. package/src/maths/algebra/monom.ts +71 -49
  76. package/src/maths/algebra/polynom.ts +432 -309
  77. package/src/maths/coefficients/fraction.ts +28 -11
  78. package/src/maths/{numexp.ts → expressions/numexp.ts} +42 -20
  79. package/src/maths/expressions/polynomexp.bkp.ts +223 -0
  80. package/src/maths/expressions/polynomexp.ts +309 -0
  81. package/src/maths/geometry/line.ts +7 -2
  82. package/src/maths/geometry/vector.ts +10 -2
  83. package/src/maths/shutingyard.ts +15 -64
  84. package/tests/algebra/monom.test.ts +12 -8
  85. package/tests/algebra/polynom.test.ts +28 -2
  86. package/tests/numexp.test.ts +34 -0
  87. package/tests/polynomexp.test.ts +15 -0
  88. package/tests/shutingyard.test.ts +4 -4
  89. package/tsconfig.json +0 -1
  90. package/esm/docs.d.ts +0 -6
  91. package/esm/docs.js +0 -7
  92. package/esm/docs.js.map +0 -1
  93. package/esm/maths/numexp.js.map +0 -1
  94. package/esm/maths/random/random.d.ts +0 -13
  95. package/esm/maths/random/random.js +0 -27
  96. package/esm/maths/random/random.js.map +0 -1
@@ -1,5 +1,6 @@
1
1
  import {Numeric} from "../numeric";
2
2
 
3
+ export type FractionParsingType = number|string|Fraction
3
4
  /**
4
5
  * The fraction class make possible to handle
5
6
  * TODO: Write the documentation correctly.
@@ -75,6 +76,9 @@ export class Fraction {
75
76
  get dfrac(): string {
76
77
  return this.tex.replace('\\frac', '\\dfrac');
77
78
  }
79
+ get tfrac(): string {
80
+ return this.tex.replace('\\frac', '\\tfrac')
81
+ }
78
82
 
79
83
  // ------------------------------------------
80
84
  // Creation / parsing functions
@@ -147,6 +151,7 @@ export class Fraction {
147
151
  this._numerator = value * Math.pow(10, p) - Math.floor(value * Math.pow(10, p - denominatorOrPeriodic));
148
152
  this.denominator = Math.pow(10, p) - Math.pow(10, p - denominatorOrPeriodic)
149
153
  }
154
+ this.reduce()
150
155
  }
151
156
  break;
152
157
  case "object":
@@ -255,23 +260,32 @@ export class Fraction {
255
260
  return this;
256
261
  }
257
262
  pow = (p: number | Fraction): Fraction => {
258
- // TODO: Fraction.pow with a value different than a safe integer ?
263
+ // TODO: Fraction.pow with a value different than a safe integer !
259
264
  if (p instanceof Fraction) {
260
265
  return this.pow(p.value)
261
266
  }
262
267
 
263
- if (!Number.isSafeInteger(p)) {
264
- return this.invalid();
265
- }
266
-
267
268
  this.reduce();
268
-
269
269
  if (p < 0) {
270
270
  this.invert()
271
271
  }
272
272
 
273
- this._numerator = this._numerator ** Math.abs(p);
274
- this._denominator = this._denominator ** Math.abs(p);
273
+ // Check if numerator and denominator are roots of...
274
+ // othervise, convert to numeric.
275
+ let controlNumerator = Math.floor(Math.pow(this._numerator, Math.abs(p))),
276
+ controlDenominator = Math.floor(Math.pow(this._denominator, Math.abs(p)))
277
+
278
+ if(controlNumerator ** Math.abs(p) === this._numerator
279
+ &&
280
+ controlDenominator ** Math.abs(p) === this._denominator){
281
+
282
+ this._numerator = this._numerator ** Math.abs(p);
283
+ this._denominator = this._denominator ** Math.abs(p);
284
+ }else{
285
+ this._numerator = this._numerator ** Math.abs(p);
286
+ this._denominator = this._denominator ** Math.abs(p);
287
+ }
288
+
275
289
  return this;
276
290
  };
277
291
 
@@ -461,16 +475,19 @@ export class Fraction {
461
475
  return Math.abs(Numeric.gcd(this._numerator, this._denominator)) === 1
462
476
  }
463
477
  isNatural = (): boolean => {
478
+ return this.isRelative() && this.isPositive()
479
+ }
480
+ isRelative = (): boolean => {
464
481
  return this.clone().reduce().denominator === 1
465
482
  }
466
483
  isRational = (): boolean => {
467
- return !this.isNatural()
484
+ return !this.isRelative()
468
485
  }
469
486
  isEven = (): boolean => {
470
- return this.isNatural() && this.value % 2 === 0
487
+ return this.isRelative() && this.value % 2 === 0
471
488
  }
472
489
  isOdd = (): boolean => {
473
- return this.isNatural() && this.value % 2 === 1
490
+ return this.isRelative() && this.value % 2 === 1
474
491
  }
475
492
  sign = (): number => {
476
493
  return (this._numerator * this._denominator >= 0) ? 1 : -1;
@@ -1,9 +1,10 @@
1
- import {Shutingyard, ShutingyardMode, ShutingyardType, tokenConstant} from "./shutingyard";
2
- import {Fraction} from "./coefficients";
1
+ import {Shutingyard, ShutingyardMode, ShutingyardType, tokenConstant} from "../shutingyard";
2
+ import {Fraction} from "../coefficients";
3
3
 
4
4
  export class NumExp {
5
5
  private _rpn: { token: string, tokenType: string }[]
6
6
  private _expression: string
7
+ private _isValid: boolean
7
8
 
8
9
  constructor(value: string) {
9
10
  this._expression = value
@@ -14,6 +15,17 @@ export class NumExp {
14
15
  return this._rpn;
15
16
  }
16
17
 
18
+ get isValid(): boolean {
19
+ if(this._isValid===undefined){
20
+ this.evaluate({x: 0})
21
+ }
22
+ return this._isValid
23
+ }
24
+
25
+ set isValid(value: boolean){
26
+ this._isValid = value
27
+ }
28
+
17
29
  get expression(): string {
18
30
  return this._expression;
19
31
  }
@@ -41,7 +53,7 @@ export class NumExp {
41
53
  const epsilon = 0.00000000000001,
42
54
  number_of_digits = 6
43
55
 
44
- let decimal = this._extractDecimalPart(value)
56
+ const decimal = this._extractDecimalPart(value)
45
57
  if(decimal===''){return value}
46
58
 
47
59
  const n9 = decimal.match(/9+$/g)
@@ -49,7 +61,7 @@ export class NumExp {
49
61
 
50
62
  if (n9 && n9[0].length >= number_of_digits) {
51
63
  // New tested values.
52
- let mod = this._extractDecimalPart(value + epsilon),
64
+ const mod = this._extractDecimalPart(value + epsilon),
53
65
  mod0 = mod.match(/0+$/g)
54
66
 
55
67
  if(mod0 && mod0[0].length>= number_of_digits){
@@ -60,7 +72,7 @@ export class NumExp {
60
72
 
61
73
  if (n0 && n0[0].length >= number_of_digits) {
62
74
  // New tested values.
63
- let mod = this._extractDecimalPart(value - epsilon),
75
+ const mod = this._extractDecimalPart(value - epsilon),
64
76
  mod9 = mod.match(/9+$/g)
65
77
 
66
78
  if(mod9 && mod9[0].length>= number_of_digits){
@@ -77,7 +89,10 @@ export class NumExp {
77
89
  }
78
90
 
79
91
  evaluate(values: { [Key: string]: number }): number {
80
- let stack: number[] = []
92
+ const stack: number[] = []
93
+
94
+ this.isValid = true
95
+
81
96
  for (const element of this._rpn) {
82
97
  if (element.tokenType === ShutingyardType.COEFFICIENT) {
83
98
  // May be a numeric value or a Fraction.
@@ -94,34 +109,42 @@ export class NumExp {
94
109
  this._addToStack(stack, tokenConstant[element.token])
95
110
  } else if (element.tokenType === ShutingyardType.OPERATION) {
96
111
  if (element.token === '*') {
97
- const b = +stack.pop(),
98
- a = +stack.pop()
112
+ const b = stack.pop(),
113
+ a = stack.pop()
114
+ if(a === undefined || b === undefined){this.isValid = false}
99
115
  this._addToStack(stack, a * b)
100
116
  } else if (element.token === '/') {
101
- const b = +stack.pop(),
102
- a = +stack.pop()
117
+ const b = stack.pop(),
118
+ a = stack.pop()
119
+ if(a === undefined || b === undefined){this.isValid = false}
103
120
  this._addToStack(stack, a / b)
104
121
  } else if (element.token === '+') {
105
- const b = +stack.pop(),
106
- a = +stack.pop()
107
- this._addToStack(stack, a + b)
122
+ const b = stack.pop(),
123
+ a = stack.pop()
124
+ if(a === undefined || b === undefined){this.isValid = false}
125
+ this._addToStack(stack, (+a) + (+b))
108
126
  } else if (element.token === '-') {
109
- const b = +stack.pop(),
110
- a = +stack.pop()
127
+ const b = stack.pop(),
128
+ a = stack.pop() || 0
129
+ if(b === undefined){this.isValid = false}
111
130
  this._addToStack(stack, a - b)
112
131
  } else if (element.token === '^') {
113
- const b = +stack.pop(),
114
- a = +stack.pop()
132
+ const b = stack.pop(),
133
+ a = stack.pop()
134
+ if(a === undefined || b === undefined){this.isValid = false}
115
135
  this._addToStack(stack, Math.pow(a, b))
116
136
  }
117
137
  } else if (element.tokenType === ShutingyardType.FUNCTION) {
118
- const a = +stack.pop()
138
+ const a = stack.pop()
139
+ if(a === undefined){this.isValid = false}
119
140
  if (element.token === 'sin') {
120
141
  this._addToStack(stack, Math.sin(a))
121
142
  } else if (element.token === 'cos') {
122
143
  this._addToStack(stack, Math.cos(a))
123
144
  } else if (element.token === 'tan') {
124
145
  this._addToStack(stack, Math.tan(a))
146
+ } else if(element.token === 'sqrt') {
147
+ this._addToStack(stack, Math.sqrt(a))
125
148
  }
126
149
  }
127
150
  }
@@ -129,8 +152,7 @@ export class NumExp {
129
152
  if (stack.length === 1) {
130
153
  return stack[0]
131
154
  } else {
132
- console.error('There was a problem parsing', this._expression, '. The RPN array is', this._rpn)
133
- return 0
155
+ throw `There was a problem parsing: ${this._expression}`
134
156
  }
135
157
  }
136
158
  }
@@ -0,0 +1,223 @@
1
+ import {Polynom} from "../algebra";
2
+ import {Fraction} from "../coefficients";
3
+
4
+ type Factor = {
5
+ polynom: Polynom,
6
+ degree: Fraction
7
+ }
8
+
9
+ export function isFactor(value: any): value is Factor {
10
+ return value && 'polynom' in value && 'degree' in value
11
+ }
12
+
13
+ export class PolynomExpFactor {
14
+ constructor(...values: unknown[]) {
15
+ this._powerAsInteger = true
16
+ this._factors = []
17
+
18
+ for (let factor of values) {
19
+ if (isFactor(factor)) {
20
+ this.addFactor({
21
+ polynom: factor.polynom,
22
+ degree: factor.degree
23
+ })
24
+ }
25
+ }
26
+ }
27
+
28
+ private _factors: Factor[]
29
+
30
+ get factors(): Factor[] {
31
+ return this._factors;
32
+ }
33
+
34
+ private _powerAsInteger: boolean
35
+
36
+ get powerAsInteger(): boolean {
37
+ return this._powerAsInteger;
38
+ }
39
+
40
+ set powerAsInteger(value: boolean) {
41
+ this._powerAsInteger = value;
42
+ }
43
+
44
+ get tex(): string {
45
+ // group positive and negative degrees.
46
+ const numerators: String[] = [],
47
+ denominators: String[] = []
48
+
49
+ for (const k of this._factors) {
50
+ if (k.degree.isPositive()) {
51
+ numerators.push(this._factorAsTex(k))
52
+ } else {
53
+ denominators.push(this._factorAsTex({
54
+ polynom: k.polynom,
55
+ degree: k.degree.clone().opposed()
56
+ }))
57
+ }
58
+ }
59
+
60
+ console.log(numerators.length)
61
+ if (denominators.length > 0) {
62
+ return `\\dfrac{ ${numerators.length > 0 ? numerators.join('') : 1} }{ ${denominators.join('')} }`
63
+ } else {
64
+ return numerators.join('')
65
+ }
66
+ }
67
+
68
+ addFactor = (value: Factor): PolynomExpFactor => {
69
+ this._factors.push({
70
+ polynom: new Polynom(value.polynom),
71
+ degree: new Fraction(value.degree)
72
+ })
73
+
74
+ return this
75
+ }
76
+
77
+ multiply = (value: PolynomExpFactor): PolynomExpFactor => {
78
+ for (const k of value.factors) {
79
+ this.addFactor(k)
80
+ }
81
+ return this
82
+ }
83
+
84
+ divide = (value: PolynomExpFactor): PolynomExpFactor => {
85
+ for (const k of value.factors) {
86
+ this.addFactor({
87
+ polynom: k.polynom,
88
+ degree: k.degree.clone().opposed()
89
+ })
90
+ }
91
+ return this
92
+ }
93
+
94
+ derivative = (letter: string): PolynomExp => {
95
+ // A*B*C*D =
96
+
97
+ // Basic version
98
+ // TODO: create derivative with more than only two factors.
99
+ if (this._factors.length === 2) {
100
+ const A = this._factors[0], B = this._factors[1],
101
+ P = new PolynomExp()
102
+ let Ad = this._factorDerivative(A),
103
+ Bd = this._factorDerivative(B)
104
+
105
+ P.add(
106
+ new PolynomExpFactor(
107
+ {
108
+ polynom: A.polynom.derivative(letter),
109
+ }
110
+ )
111
+ )
112
+ }
113
+ return
114
+ }
115
+
116
+ private _factorDerivative = (factor: Factor, letter?: string): PolynomExpFactor => {
117
+ let derivativeExpression = new PolynomExpFactor()
118
+ derivativeExpression.addFactor({polynom: new Polynom(factor.degree), degree: new Fraction().one()})
119
+ derivativeExpression.addFactor({polynom: factor.polynom, degree: factor.degree.subtract(1)})
120
+ derivativeExpression.addFactor({
121
+ polynom: factor.polynom.clone().derivative(letter),
122
+ degree: new Fraction().one()
123
+ })
124
+
125
+ return derivativeExpression
126
+
127
+ }
128
+
129
+ private _factorAsTex = (factor: Factor, withParenthesis?: Boolean): string => {
130
+ let tex: string = ''
131
+
132
+ if (factor.degree.isOne()) {
133
+ if (withParenthesis === undefined || withParenthesis) {
134
+ tex = `\\left(${factor.polynom.tex}\\right)`
135
+ } else {
136
+ tex = factor.polynom.tex
137
+ }
138
+ } else if (factor.degree.isNatural()) {
139
+ tex = `\\left(${factor.polynom.tex}\\right)^{ ${factor.degree.tex} }`
140
+ } else {
141
+ if (this._powerAsInteger) {
142
+ if (factor.degree.denominator === 2) {
143
+ tex = `\\sqrt{${factor.polynom.tex}}`
144
+ } else {
145
+ tex = `\\sqrt[${factor.degree.denominator}]{${factor.polynom.tex}}`
146
+ }
147
+
148
+ if (factor.degree.numerator !== 1) {
149
+ tex += `^{ ${factor.degree.numerator} }`
150
+ }
151
+ } else {
152
+ tex = `\\left(${factor.polynom.tex}\\right)^{ ${factor.degree.tex} }`
153
+ }
154
+ }
155
+ return tex
156
+ }
157
+ }
158
+
159
+ export class PolynomExp {
160
+ private _factors: { factors: PolynomExpFactor, positive: boolean }[]
161
+
162
+ constructor(...values: PolynomExpFactor[]) {
163
+ this._factors = []
164
+ if (values !== undefined) {
165
+ for (const factor of values) {
166
+ console.log('ADDING', factor.tex)
167
+ this._factors.push({
168
+ factors: factor,
169
+ positive: true
170
+ })
171
+ }
172
+ }
173
+ this._powerAsInteger = true
174
+ }
175
+
176
+ private _powerAsInteger: boolean
177
+
178
+ get powerAsInteger(): boolean {
179
+ return this._powerAsInteger;
180
+ }
181
+
182
+ set powerAsInteger(value: boolean) {
183
+ for (const factor of this._factors) {
184
+ factor.factors.powerAsInteger = value
185
+ }
186
+ this._powerAsInteger = value;
187
+ }
188
+
189
+ get tex(): string {
190
+ let tex = ''
191
+
192
+ for (const factor of this._factors) {
193
+ if (factor.factors.tex === '') {
194
+ continue
195
+ }
196
+ if (tex !== '' || !factor.positive) {
197
+ tex += factor.positive ? '+' : '-'
198
+ }
199
+ tex += factor.factors.tex
200
+ }
201
+
202
+
203
+ return tex
204
+ }
205
+
206
+ add = (value: PolynomExpFactor): PolynomExp => {
207
+ value.powerAsInteger = this._powerAsInteger
208
+ this._factors.push({
209
+ factors: value,
210
+ positive: true
211
+ })
212
+ return this
213
+ }
214
+
215
+ subtract = (value: PolynomExpFactor): PolynomExp => {
216
+ value.powerAsInteger = this._powerAsInteger
217
+ this._factors.push({
218
+ factors: value,
219
+ positive: false
220
+ })
221
+ return this
222
+ }
223
+ }