pimath 0.0.127 → 0.0.129

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 (212) hide show
  1. package/dist/main.d.ts +39 -0
  2. package/{esm → dist}/maths/algebra/equation.d.ts +11 -10
  3. package/{esm → dist}/maths/algebra/linearSystem.d.ts +5 -4
  4. package/{esm → dist}/maths/algebra/monom.d.ts +5 -4
  5. package/{esm → dist}/maths/algebra/polynom.d.ts +11 -13
  6. package/{esm → dist}/maths/algebra/rational.d.ts +7 -9
  7. package/dist/maths/algebra/study/rationalStudy.d.ts +14 -0
  8. package/{esm → dist}/maths/algebra/study.d.ts +4 -7
  9. package/{esm → dist}/maths/geometry/circle.d.ts +9 -8
  10. package/{esm → dist}/maths/geometry/line.d.ts +17 -14
  11. package/{esm → dist}/maths/geometry/point.d.ts +11 -9
  12. package/{esm → dist}/maths/geometry/triangle.d.ts +4 -3
  13. package/{esm → dist}/maths/geometry/vector.d.ts +7 -10
  14. package/{esm → dist}/maths/randomization/random.d.ts +10 -7
  15. package/{esm → dist}/maths/randomization/rndFraction.d.ts +4 -3
  16. package/dist/maths/randomization/rndGeometryCircle.d.ts +13 -0
  17. package/{esm → dist}/maths/randomization/rndGeometryLine.d.ts +4 -3
  18. package/{esm → dist}/maths/randomization/rndGeometryPoint.d.ts +4 -3
  19. package/{esm → dist}/maths/randomization/rndMonom.d.ts +5 -4
  20. package/{esm → dist}/maths/randomization/rndPolynom.d.ts +5 -4
  21. package/{esm → dist}/maths/randomization/rndTypes.d.ts +7 -1
  22. package/dist/pimath.js +4239 -7819
  23. package/package.json +13 -7
  24. package/.eslintrc.js +0 -24
  25. package/.idea/$CACHE_FILE$ +0 -6
  26. package/.idea/PI.iml +0 -14
  27. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  28. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  29. package/.idea/misc.xml +0 -6
  30. package/.idea/modules.xml +0 -8
  31. package/.idea/php.xml +0 -19
  32. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_07_11_2023_08_30_[Default_Changelist]/shelved.patch +0 -192
  33. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_07_11_2023_08_30_[Default_Changelist]1/shelved.patch +0 -0
  34. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_07_11_2023_08_30__Default_Changelist_.xml +0 -4
  35. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_09_11_2023_10_43_[Default_Changelist]/shelved.patch +0 -2404
  36. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_09_11_2023_10_43__Default_Changelist_.xml +0 -4
  37. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_09_11_2023_11_01_[Default_Changelist]/shelved.patch +0 -1362
  38. package/.idea/shelf/Uncommitted_changes_before_Checkout_at_09_11_2023_11_01__Default_Changelist_.xml +0 -4
  39. package/.idea/shelf/Uncommitted_changes_before_Update_at_24_07_2023_15_31_[Default_Changelist]/shelved.patch +0 -90
  40. package/.idea/shelf/Uncommitted_changes_before_Update_at_24_07_2023_15_31__Default_Changelist_.xml +0 -4
  41. package/.idea/vcs.xml +0 -6
  42. package/dev/pimath.js +0 -7945
  43. package/dev/pimath.js.map +0 -1
  44. package/dist/pimath.js.map +0 -1
  45. package/dist/pimath.min.js +0 -2
  46. package/dist/pimath.min.js.map +0 -1
  47. package/docs/.nojekyll +0 -1
  48. package/docs/assets/highlight.css +0 -78
  49. package/docs/assets/main.js +0 -59
  50. package/docs/assets/navigation.js +0 -1
  51. package/docs/assets/search.js +0 -1
  52. package/docs/assets/style.css +0 -1383
  53. package/docs/classes/Logicalset.Logicalset.html +0 -217
  54. package/docs/classes/Polynom.Rational.html +0 -397
  55. package/docs/classes/Vector-1.Vector.html +0 -490
  56. package/docs/classes/Vector.Point.html +0 -337
  57. package/docs/classes/algebra_equation.Equation.html +0 -790
  58. package/docs/classes/algebra_linearSystem.LinearSystem.html +0 -404
  59. package/docs/classes/algebra_monom.Monom.html +0 -962
  60. package/docs/classes/algebra_polynom.Polynom.html +0 -1275
  61. package/docs/classes/coefficients_fraction.Fraction.html +0 -934
  62. package/docs/classes/geometry_circle.Circle.html +0 -472
  63. package/docs/classes/geometry_line.Line.html +0 -774
  64. package/docs/classes/geometry_triangle.Triangle.html +0 -429
  65. package/docs/classes/numeric.Numeric.html +0 -265
  66. package/docs/classes/shutingyard.Shutingyard.html +0 -250
  67. package/docs/enums/algebra_equation.PARTICULAR_SOLUTION.html +0 -83
  68. package/docs/enums/geometry_line.LinePropriety.html +0 -97
  69. package/docs/enums/shutingyard.ShutingyardMode.html +0 -97
  70. package/docs/enums/shutingyard.ShutingyardType.html +0 -111
  71. package/docs/index.html +0 -63
  72. package/docs/interfaces/algebra_equation.ISolution.html +0 -105
  73. package/docs/interfaces/algebra_polynom.IEuclidian.html +0 -87
  74. package/docs/interfaces/geometry_triangle.remarquableLines.html +0 -163
  75. package/docs/modules/Logicalset.html +0 -65
  76. package/docs/modules/Polynom.html +0 -65
  77. package/docs/modules/Vector-1.html +0 -65
  78. package/docs/modules/Vector.html +0 -65
  79. package/docs/modules/algebra_equation.html +0 -69
  80. package/docs/modules/algebra_linearSystem.html +0 -61
  81. package/docs/modules/algebra_monom.html +0 -65
  82. package/docs/modules/algebra_polynom.html +0 -69
  83. package/docs/modules/coefficients_fraction.html +0 -65
  84. package/docs/modules/geometry_circle.html +0 -61
  85. package/docs/modules/geometry_line.html +0 -65
  86. package/docs/modules/geometry_triangle.html +0 -65
  87. package/docs/modules/numeric.html +0 -61
  88. package/docs/modules/shutingyard.html +0 -75
  89. package/docs/types/algebra_monom.literalType.html +0 -61
  90. package/docs/types/algebra_polynom.PolynomParsingType.html +0 -56
  91. package/docs/types/coefficients_fraction.FractionParsingType.html +0 -56
  92. package/docs/types/shutingyard.Token.html +0 -63
  93. package/docs/types/shutingyard.tokenType.html +0 -68
  94. package/docs/variables/shutingyard.tokenConstant.html +0 -61
  95. package/esm/index.d.ts +0 -38
  96. package/esm/index.js +0 -44
  97. package/esm/index.js.map +0 -1
  98. package/esm/maths/algebra/equation.js +0 -797
  99. package/esm/maths/algebra/equation.js.map +0 -1
  100. package/esm/maths/algebra/linearSystem.js +0 -279
  101. package/esm/maths/algebra/linearSystem.js.map +0 -1
  102. package/esm/maths/algebra/logicalset.js +0 -158
  103. package/esm/maths/algebra/logicalset.js.map +0 -1
  104. package/esm/maths/algebra/monom.js +0 -909
  105. package/esm/maths/algebra/monom.js.map +0 -1
  106. package/esm/maths/algebra/polynom.js +0 -1305
  107. package/esm/maths/algebra/polynom.js.map +0 -1
  108. package/esm/maths/algebra/rational.js +0 -195
  109. package/esm/maths/algebra/rational.js.map +0 -1
  110. package/esm/maths/algebra/study/rationalStudy.d.ts +0 -28
  111. package/esm/maths/algebra/study/rationalStudy.js +0 -244
  112. package/esm/maths/algebra/study/rationalStudy.js.map +0 -1
  113. package/esm/maths/algebra/study.js +0 -380
  114. package/esm/maths/algebra/study.js.map +0 -1
  115. package/esm/maths/coefficients/fraction.js +0 -517
  116. package/esm/maths/coefficients/fraction.js.map +0 -1
  117. package/esm/maths/coefficients/nthRoot.js +0 -137
  118. package/esm/maths/coefficients/nthRoot.js.map +0 -1
  119. package/esm/maths/geometry/circle.js +0 -324
  120. package/esm/maths/geometry/circle.js.map +0 -1
  121. package/esm/maths/geometry/line.js +0 -485
  122. package/esm/maths/geometry/line.js.map +0 -1
  123. package/esm/maths/geometry/point.js +0 -167
  124. package/esm/maths/geometry/point.js.map +0 -1
  125. package/esm/maths/geometry/triangle.js +0 -276
  126. package/esm/maths/geometry/triangle.js.map +0 -1
  127. package/esm/maths/geometry/vector.js +0 -198
  128. package/esm/maths/geometry/vector.js.map +0 -1
  129. package/esm/maths/numeric.js +0 -136
  130. package/esm/maths/numeric.js.map +0 -1
  131. package/esm/maths/numexp.js +0 -186
  132. package/esm/maths/numexp.js.map +0 -1
  133. package/esm/maths/randomization/random.js +0 -79
  134. package/esm/maths/randomization/random.js.map +0 -1
  135. package/esm/maths/randomization/randomCore.js +0 -22
  136. package/esm/maths/randomization/randomCore.js.map +0 -1
  137. package/esm/maths/randomization/rndFraction.js +0 -44
  138. package/esm/maths/randomization/rndFraction.js.map +0 -1
  139. package/esm/maths/randomization/rndGeometryLine.js +0 -46
  140. package/esm/maths/randomization/rndGeometryLine.js.map +0 -1
  141. package/esm/maths/randomization/rndGeometryPoint.js +0 -61
  142. package/esm/maths/randomization/rndGeometryPoint.js.map +0 -1
  143. package/esm/maths/randomization/rndHelpers.js +0 -98
  144. package/esm/maths/randomization/rndHelpers.js.map +0 -1
  145. package/esm/maths/randomization/rndMonom.js +0 -53
  146. package/esm/maths/randomization/rndMonom.js.map +0 -1
  147. package/esm/maths/randomization/rndPolynom.js +0 -75
  148. package/esm/maths/randomization/rndPolynom.js.map +0 -1
  149. package/esm/maths/randomization/rndTypes.js +0 -3
  150. package/esm/maths/randomization/rndTypes.js.map +0 -1
  151. package/esm/maths/shutingyard.js +0 -443
  152. package/esm/maths/shutingyard.js.map +0 -1
  153. package/public/demo.css +0 -3
  154. package/public/index.html +0 -283
  155. package/public/matrices.html +0 -100
  156. package/public/playground.html +0 -168
  157. package/src/index.ts +0 -42
  158. package/src/maths/algebra/equation.ts +0 -897
  159. package/src/maths/algebra/linearSystem.ts +0 -370
  160. package/src/maths/algebra/logicalset.ts +0 -183
  161. package/src/maths/algebra/monom.ts +0 -1028
  162. package/src/maths/algebra/polynom.ts +0 -1537
  163. package/src/maths/algebra/rational.ts +0 -240
  164. package/src/maths/algebra/study/rationalStudy.ts +0 -287
  165. package/src/maths/algebra/study.ts +0 -506
  166. package/src/maths/coefficients/fraction.ts +0 -593
  167. package/src/maths/coefficients/nthRoot.ts +0 -148
  168. package/src/maths/geometry/circle.ts +0 -382
  169. package/src/maths/geometry/line.ts +0 -604
  170. package/src/maths/geometry/point.ts +0 -215
  171. package/src/maths/geometry/triangle.ts +0 -368
  172. package/src/maths/geometry/vector.ts +0 -242
  173. package/src/maths/numeric.ts +0 -162
  174. package/src/maths/numexp.ts +0 -184
  175. package/src/maths/randomization/random.ts +0 -80
  176. package/src/maths/randomization/randomCore.ts +0 -19
  177. package/src/maths/randomization/rndFraction.ts +0 -47
  178. package/src/maths/randomization/rndGeometryCircle.ts +0 -50
  179. package/src/maths/randomization/rndGeometryLine.ts +0 -53
  180. package/src/maths/randomization/rndGeometryPoint.ts +0 -69
  181. package/src/maths/randomization/rndHelpers.ts +0 -107
  182. package/src/maths/randomization/rndMonom.ts +0 -57
  183. package/src/maths/randomization/rndPolynom.ts +0 -90
  184. package/src/maths/randomization/rndTypes.ts +0 -43
  185. package/src/maths/shutingyard.ts +0 -496
  186. package/tests/algebra/equation.test.ts +0 -64
  187. package/tests/algebra/linear.test.ts +0 -58
  188. package/tests/algebra/monom.test.ts +0 -78
  189. package/tests/algebra/polynom.test.ts +0 -343
  190. package/tests/algebra/rationnal.test.ts +0 -64
  191. package/tests/algebra/study.test.ts +0 -48
  192. package/tests/coefficients/fraction.test.ts +0 -131
  193. package/tests/custom.test.ts +0 -33
  194. package/tests/geometry/circle.test.ts +0 -404
  195. package/tests/geometry/line.test.ts +0 -36
  196. package/tests/numeric.test.ts +0 -43
  197. package/tests/numexp.test.ts +0 -89
  198. package/tests/shutingyard.test.ts +0 -58
  199. package/tsconfig.json +0 -41
  200. package/tsconfig.testing.json +0 -28
  201. package/typedoc.katex.js +0 -11
  202. package/webpack-production-min.config.js +0 -26
  203. package/webpack-production.config.js +0 -26
  204. package/webpack.config.js +0 -26
  205. package/{esm → dist}/maths/algebra/logicalset.d.ts +6 -6
  206. package/{esm → dist}/maths/coefficients/fraction.d.ts +0 -0
  207. package/{esm → dist}/maths/coefficients/nthRoot.d.ts +0 -0
  208. package/{esm → dist}/maths/numeric.d.ts +0 -0
  209. package/{esm → dist}/maths/numexp.d.ts +3 -3
  210. /package/{esm → dist}/maths/randomization/randomCore.d.ts +0 -0
  211. /package/{esm → dist}/maths/randomization/rndHelpers.d.ts +0 -0
  212. /package/{esm → dist}/maths/shutingyard.d.ts +0 -0
@@ -1,1028 +0,0 @@
1
- /***
2
- * Monom class
3
- */
4
- import {Numeric} from "../numeric";
5
- import {Shutingyard, ShutingyardType, Token} from "../shutingyard";
6
- import {Fraction} from "../coefficients/fraction";
7
-
8
- export type literalType = {
9
- [Key: string]: Fraction
10
- }
11
-
12
- export class Monom {
13
- /**
14
- * Create a Monom
15
- * Defined as \\(k \\cdot x^{n}\\), where \\( k,n \in \\mathbb{Q}\\).
16
- * Examples: \\(3x^2\\) or \\(3/5x^2\\)
17
- * @param value (optional) string The value that should be parse. Can be a Monom, a Fraction, a string or a number. If nothing is provided, it will return the trivial monom (0).
18
- */
19
- constructor(value?: unknown) {
20
- this.zero();
21
-
22
- if (value !== undefined) {
23
- // A string is given - try to parse the value.
24
- this.parse(value);
25
- }
26
-
27
- return this;
28
- }
29
-
30
- private _coefficient: Fraction;
31
-
32
- // ------------------------------------------
33
- /**
34
- * Get the coefficient \\(k\\) of the Monom \\(k\\cdot x^{n}\\)
35
- * @returns {Fraction}
36
- */
37
- get coefficient(): Fraction {
38
- return this._coefficient;
39
- }
40
-
41
- // ------------------------------------------
42
- // Getter and setter
43
-
44
- /**
45
- * Set the coefficient \\(k\\) value of the monom
46
- * @param {Fraction | number | string} F
47
- */
48
- set coefficient(F: Fraction | number | string) {
49
- this._coefficient = new Fraction(F);
50
- }
51
-
52
- private _literal: literalType;
53
-
54
- /**
55
- * Get the literal part of \\(x^{n_1}y^{n_2}\\) as dictionary \\[\\begin{array}{ll}x&=n_1\\\\y&=n_2\\end{array}\\]
56
- * @returns {literalType}
57
- */
58
- get literal(): literalType {
59
- return this._literal;
60
- }
61
-
62
- /**
63
- * Set the literal part of the monom. Must be a dictionary {x: Fraction, y: Fraction, ...}
64
- * @param {literalType} L
65
- */
66
- set literal(L: literalType) {
67
- this._literal = L;
68
- }
69
-
70
- /**
71
- * Get the literal square roots of the Monom.
72
- * TODO: remove this getter ? Is it used and is it correct ?
73
- * @returns {literalType}
74
- */
75
- get literalSqrt(): literalType {
76
- if (this.isLiteralSquare()) {
77
- let L: literalType = {}
78
- for (let key in this._literal) {
79
- L[key] = this._literal[key].clone().sqrt()
80
- }
81
- return L;
82
- } else {
83
- return this._literal;
84
- }
85
- }
86
-
87
- /**
88
- * Set the literal part of the monom from a string
89
- * @param inputStr String like x^2y^3
90
- */
91
- set literalStr(inputStr: string) {
92
- // TODO : parse using shutingyard tree !
93
-
94
- // Match all x^n
95
- for (const v of [...inputStr.matchAll(/([a-z])\^([+-]?[0-9]+)/g)]) {
96
- // Create the default letter entry if necessary.
97
- if (!(v[1] in this._literal)) {
98
- this._literal[v[1]] = new Fraction().zero();
99
- }
100
-
101
- // Add the new value.
102
- // TODO: actually, it adds only numeric value
103
- this._literal[v[1]].add(+v[2]);
104
- }
105
-
106
- // Match all x
107
- for (const v of [...inputStr.matchAll(/([a-z](?!\^))/g)]) {
108
- // Match all single letters
109
- if (!(v[1] in this._literal)) {
110
- this._literal[v[1]] = new Fraction().zero();
111
- }
112
-
113
- // Add one to the value.
114
- this._literal[v[1]].add(1)
115
- }
116
- }
117
-
118
- // Getter helpers.
119
- /**
120
- * Get the variables letters
121
- */
122
- get variables(): string[] {
123
- let M = this.clone().clean();
124
- return Object.keys(M.literal)
125
- }
126
-
127
- // Display getter
128
- /**
129
- * This display getter is to be used in the polynom display getter
130
- */
131
- get display(): string {
132
- let L: string = '',
133
- letters = Object.keys(this._literal).sort()
134
- for (let letter of letters) {
135
- if (this._literal[letter].isNotZero()) {
136
- L += `${letter}`;
137
- if (this._literal[letter].isNotEqual(1)) {
138
- L += `^(${this._literal[letter].display})`;
139
- }
140
- }
141
- }
142
-
143
- if (L === '') {
144
- // No setLetter - means it's only a number !
145
- if (this._coefficient.value != 0) {
146
- return `${this._coefficient.display}`;
147
- } else {
148
- return '';
149
- }
150
- } else {
151
- if (this._coefficient.value === 1) {
152
- return L;
153
- } else if (this._coefficient.value === -1) {
154
- return `-${L}`;
155
- } else if (this._coefficient.value === 0) {
156
- return '0';
157
- } else {
158
- return `${this._coefficient.display}${L}`;
159
- }
160
- }
161
- }
162
-
163
- get dividers(): Monom[] {
164
- // Decompose only if the coefficient is a natural number
165
- if (!this.coefficient.isRelative()) {
166
- return [this.clone()]
167
- }
168
-
169
-
170
- // Decompose only if the power values are natural numbers.
171
- if (this.hasFractionCoefficient()) {
172
- return [this.clone()]
173
- }
174
-
175
- // Security : do not do this if greater than 10000
176
- if (this.coefficient.numerator > 1000000) {
177
- return [this.clone()]
178
- }
179
-
180
- const dividers = Numeric.dividers(Math.abs(this.coefficient.numerator))
181
-
182
- // Decompose the literals parts.
183
- let literals: literalType[] = [];
184
- for (let L in this.literal) {
185
- // L is the letter.
186
- literals = this._getLiteralDividers(literals, L)
187
- }
188
-
189
- const monomDividers: Monom[] = [];
190
- if (literals.length > 0 && dividers.length > 0) {
191
- for (let N of dividers) {
192
- for (let L of literals) {
193
- let M = new Monom();
194
- M.coefficient = new Fraction(N)
195
- M.literal = L
196
- monomDividers.push(M)
197
- }
198
- }
199
- } else if (dividers.length === 0) {
200
- for (let L of literals) {
201
- let M = new Monom();
202
- M.coefficient = new Fraction().one()
203
- M.literal = L
204
- monomDividers.push(M)
205
- }
206
- } else {
207
- for (let N of dividers) {
208
- let M = new Monom();
209
- M.coefficient = new Fraction(N)
210
- monomDividers.push(M)
211
- }
212
- }
213
-
214
- return monomDividers.length === 0 ? [new Monom().one()] : monomDividers;
215
- }
216
-
217
- /**
218
- * Display the monom, forcing the '+' sign to appear
219
- */
220
- get displayWithSign(): string {
221
- let d: String = this.display;
222
- return (d[0] !== '-' ? '+' : '') + d;
223
- }
224
-
225
- get texWithSign(): string {
226
- if (this.coefficient.isStrictlyPositive()) {
227
- return '+' + this.tex
228
- }
229
-
230
- return this.tex
231
- }
232
-
233
- get plotFunction(): string {
234
-
235
- let L: string = '',
236
- letters = Object.keys(this._literal).sort()
237
-
238
- for (let letter of letters) {
239
- if (this._literal[letter].isNotZero()) {
240
- L += (L === '' ? "" : "*") + `${letter}`
241
- if (this._literal[letter].isNotEqual(1)) {
242
- L += `^(${this._literal[letter].display})`;
243
- }
244
- }
245
- }
246
-
247
- // No literal part
248
- if (L === '') {
249
- // No setLetter - means it's only a number !
250
- if (this._coefficient.value != 0) {
251
- return `${this._coefficient.display}`;
252
- } else {
253
- return '';
254
- }
255
- } else {
256
- if (this._coefficient.value === 1) {
257
- return L;
258
- } else if (this._coefficient.value === -1) {
259
- return `-${L}`;
260
- } else if (this._coefficient.value === 0) {
261
- return '0';
262
- } else {
263
- return `${this._coefficient.display}*${L}`;
264
- }
265
- }
266
- }
267
-
268
- /**
269
- * Get the tex output of the monom
270
- */
271
- get tex(): string {
272
- // TODO: display with square root !
273
- let L: string = '',
274
- letters = Object.keys(this._literal).sort()
275
-
276
- for (let letter of letters) {
277
- if (this._literal[letter].isNotZero()) {
278
- L += `${letter}`;
279
- if (this._literal[letter].isNotEqual(1)) {
280
- L += `^{${this._literal[letter].tfrac}}`;
281
- }
282
- }
283
- }
284
-
285
- if (L === '') {
286
- // No setLetter - means it's only a number !
287
- if (this._coefficient.value != 0) {
288
- return `${this._coefficient.frac}`;
289
- } else {
290
- return '0';
291
- }
292
- } else {
293
- if (this._coefficient.value === 1) {
294
- return L;
295
- } else if (this._coefficient.value === -1) {
296
- return `-${L}`;
297
- } else if (this._coefficient.value === 0) {
298
- return '0';
299
- } else {
300
- return `${this._coefficient.frac}${L}`;
301
- }
302
- }
303
- }
304
-
305
- /**
306
- * Get the least common multiple of monoms
307
- * @param monoms Array of monoms
308
- */
309
- static lcm = (...monoms: Monom[]): Monom => {
310
- // All the monoms must be with natural powers...
311
- for (let m of monoms) {
312
- if (m.hasFractionCoefficient()) {
313
- return new Monom().zero()
314
- }
315
- }
316
-
317
-
318
- let M = new Monom(),
319
- coeffN: number[] = monoms.map(value => value.coefficient.numerator),
320
- coeffD: number[] = monoms.map(value => value.coefficient.denominator),
321
- n = Numeric.gcd(...coeffN),
322
- d = Numeric.lcm(...coeffD);
323
-
324
- // Get the coefficient.
325
- M.coefficient = new Fraction(n, d).reduce();
326
-
327
- // Set the literal parts - go through each monoms literal parts and get only the lowest degree of each letters.
328
- for (let m of monoms) {
329
- // Remove the inexistant letters from the resulting monom
330
- for (let letter in M.literal) {
331
- if (!(letter in m.literal)) {
332
- M.literal[letter].zero();
333
- }
334
- }
335
- for (let letter in m.literal) {
336
- if (M.literal[letter] === undefined && m.literal[letter].isStrictlyPositive()) {
337
- M.literal[letter] = m.literal[letter].clone();
338
- } else {
339
- M.literal[letter] = new Fraction(Math.min(m.literal[letter].value, M.literal[letter].value))
340
- }
341
- }
342
- }
343
-
344
- return M;
345
- };
346
-
347
- // ------------------------------------------
348
- // Creation / parsing functions
349
-
350
- /**
351
- * Multiply two monoms and return a NEW monom.
352
- * @param monoms
353
- */
354
- static xmultiply = (...monoms: Monom[]): Monom => {
355
- let M = new Monom().one();
356
-
357
- for (let m of monoms) {
358
- M.multiply(m);
359
- }
360
-
361
- return M;
362
- };
363
-
364
- // -----------------------------------------
365
- /**
366
- * Parse a string to a monom. The string may include fraction.
367
- * @param inputStr
368
- */
369
- parse = (inputStr: unknown): Monom => {
370
-
371
- if (typeof inputStr === 'string') {
372
- this._shutingYardToReducedMonom(inputStr)
373
- } else if (typeof inputStr === 'number') {
374
- this._coefficient = new Fraction(inputStr)
375
- this._literal = {}
376
- } else if (inputStr instanceof Fraction) {
377
- this._coefficient = inputStr.clone()
378
- this._literal = {}
379
- } else if (inputStr instanceof Monom) {
380
- this._coefficient = inputStr._coefficient.clone()
381
- this._literal = this.copyLiterals(inputStr.literal)
382
- }
383
-
384
- return this;
385
- };
386
-
387
- addToken = (stack: Monom[], element: Token): void => {
388
-
389
- let q1: Monom, q2: Monom, m: Monom, letter: string, pow: Fraction
390
-
391
- if (element.tokenType === ShutingyardType.COEFFICIENT) {
392
- stack.push(new Monom(new Fraction(element.token)))
393
-
394
- } else if (element.tokenType === ShutingyardType.VARIABLE) {
395
- let M = new Monom().one()
396
- M.setLetter(element.token, 1)
397
- stack.push(M.clone())
398
-
399
- } else if (element.tokenType === ShutingyardType.OPERATION) {
400
- switch (element.token) {
401
- case '-':
402
- // this should only happen for negative powers or for negative coefficient.
403
- q2 = (stack.pop()) || new Monom().zero()
404
- q1 = (stack.pop()) || new Monom().zero()
405
-
406
- stack.push(q1.subtract(q2))
407
-
408
- break;
409
- case '*':
410
- // Get the last element in the stack
411
- q2 = (stack.pop()) || new Monom().one()
412
- q1 = (stack.pop()) || new Monom().one()
413
-
414
- stack.push(q1.multiply(q2))
415
- break
416
- case '/':
417
- // Get the last element in the stack
418
- q2 = (stack.pop()) || new Monom().one()
419
- q1 = (stack.pop()) || new Monom().one()
420
-
421
- stack.push(q1.divide(q2))
422
- break
423
- case '^':
424
- // get the two last elements in the stack
425
- pow = (stack.pop().coefficient) || new Fraction().one()
426
- m = (stack.pop()) || new Monom().one()
427
-
428
- letter = m.variables[0]
429
-
430
- if (letter !== undefined) {
431
- m.setLetter(letter, pow)
432
- }
433
-
434
- stack.push(m)
435
- // this.multiply(m.clone())
436
- break
437
- }
438
- }
439
- }
440
-
441
- /**
442
- * Clone the current Monom.
443
- */
444
- clone = (): Monom => {
445
- let F: Monom = new Monom();
446
-
447
- F.coefficient = this._coefficient.clone();
448
-
449
- // Copy the literal parts.
450
- for (let k in this._literal) {
451
- F.setLetter(k, this._literal[k].clone());
452
- }
453
- return F;
454
- };
455
-
456
- copyLiterals = (literal: literalType): literalType => {
457
- let L: literalType = {}
458
-
459
- for (let k in literal) {
460
- L[k] = literal[k].clone()
461
- }
462
- return L
463
- }
464
-
465
- makeSame = (M: Monom): Monom => {
466
- // Copy the literal parts.
467
- for (let k in M._literal) {
468
- this.setLetter(k, M._literal[k].clone());
469
- }
470
- return this
471
- }
472
-
473
- /**
474
- * Create a zero value monom
475
- */
476
- zero = (): Monom => {
477
- this._coefficient = new Fraction().zero();
478
- this._literal = {};
479
- return this;
480
- };
481
-
482
- /**
483
- * Create a one value monom
484
- */
485
- one = (): Monom => {
486
- this._coefficient = new Fraction().one();
487
- this._literal = {};
488
- return this;
489
- };
490
-
491
- /**
492
- * Clean the monom by removing each letters with a power of zero.
493
- */
494
- clean = (): Monom => {
495
- for (let letter in this._literal) {
496
- if (this._literal[letter].isZero()) {
497
- delete this._literal[letter];
498
- }
499
- }
500
- return this;
501
- };
502
-
503
- reduce = (): Monom => {
504
- this.clean()
505
- this.coefficient.reduce()
506
- return this
507
- }
508
-
509
- // ------------------------------------------
510
- // Mathematical operations
511
- // ------------------------------------------
512
-
513
- /**
514
- * Get the opposed
515
- * Returns a monom.
516
- */
517
- opposed = (): Monom => {
518
- this._coefficient.opposed();
519
- return this;
520
- };
521
-
522
- /**
523
- * Add all similar monoms. If they aren't similar, they are simply skipped.
524
- * @param M (Monom[]) The monoms to add.
525
- */
526
- add = (...M: Monom[]): Monom => {
527
- for (let m of M) {
528
- if (this.isSameAs(m)) {
529
- if (this.isZero()) {
530
- this.makeSame(m)
531
- }
532
- this._coefficient.add(m.coefficient);
533
- } else {
534
- console.log('Add monom: ' + this.display + ' is not similar with ', m.display);
535
- }
536
- }
537
- return this;
538
- };
539
-
540
- /**
541
- * Subtract multiple monoms
542
- * @param M (Monom[]) The monoms to subtract
543
- */
544
- subtract = (...M: Monom[]): Monom => {
545
- for (let m of M) {
546
- if (this.isSameAs(m)) {
547
- if (this.isZero()) {
548
- this.makeSame(m)
549
- }
550
- this._coefficient.add(m.clone().coefficient.opposed());
551
- } else {
552
- console.log('Subtract: Is not similar: ', m.display);
553
- }
554
- }
555
- return this;
556
- };
557
-
558
- /**
559
- * Multiple multiple monoms to the current monom
560
- * @param M (Monom[]) The monoms to multiply to.
561
- */
562
- multiply = (...M: Monom[]): Monom => {
563
- for (let m of M) {
564
- // Multiply the coefficient.
565
- this._coefficient.multiply(m.coefficient);
566
-
567
- // Multiply the literal parts.
568
- for (let letter in m.literal) {
569
- if (this._literal[letter] === undefined) {
570
- this._literal[letter] = m.literal[letter].clone()
571
- } else {
572
- this._literal[letter].add(m.literal[letter])
573
- }
574
-
575
- }
576
- }
577
- return this;
578
- };
579
-
580
- multiplyByNumber = (F: Fraction | number): Monom => {
581
- this._coefficient.multiply(F);
582
- return this;
583
- }
584
-
585
- /**
586
- * Divide the current monoms by multiple monoms
587
- * @param M (Monom[])
588
- */
589
- divide = (...M: Monom[]): Monom => {
590
- // Depending on the given value, choose the current item
591
- for (let v of M) {
592
- // Divide the coefficient
593
- this._coefficient.divide(v.coefficient);
594
-
595
- // Subtract the power values
596
- for (let letter in v.literal) {
597
- this._literal[letter] = (this._literal[letter] === undefined) ? v.literal[letter].clone().opposed() : this._literal[letter].subtract(v.literal[letter])
598
-
599
- // If the power of a particular setLetter is zero, delete it from the literal part..
600
- if (this._literal[letter].isZero()) {
601
- delete this._literal[letter];
602
- }
603
- }
604
- }
605
- return this;
606
- };
607
-
608
- /**
609
- * Get the pow of a monom.
610
- * @param nb (number) : Mathematical pow
611
- */
612
- pow = (nb: number | Fraction): Monom => {
613
- this._coefficient.pow(nb);
614
- for (let letter in this._literal) {
615
- this._literal[letter].multiply(nb)
616
- }
617
- return this;
618
- };
619
-
620
- /**
621
- * Get the nth-root of the monom
622
- * @param p
623
- */
624
- root = (p: number): Monom => {
625
- // TODO: determiner the nth root of a monom
626
- return this;
627
- }
628
-
629
- /**
630
- * Return the square root of a monom
631
- */
632
- sqrt = (): Monom => {
633
- if (this.isSquare()) {
634
- this._coefficient.sqrt();
635
- for (let letter in this._literal) {
636
- this._literal[letter].clone().divide(2)
637
- }
638
- }
639
- return this.root(2);
640
- }
641
-
642
- // ------------------------------------------
643
- // Compare functions
644
- // ------------------------------------------
645
- compare = (M: Monom, sign?: string): boolean => {
646
- // TODO: Build the compare systems.
647
- if (sign === undefined) {
648
- sign = '=';
649
- }
650
-
651
-
652
- switch (sign) {
653
- case '=':
654
- // To be equal, they must be the isSame
655
- if (!this.compare(M, 'same')) {
656
- return false;
657
- }
658
-
659
- // The literal parts are the isSame. The coefficient must be equal
660
- return this._coefficient.isEqual(M.coefficient);
661
- case 'same':
662
- // Get the list of all variables from both monoms.
663
- let M1: string[] = this.variables,
664
- M2: string[] = M.variables,
665
- K: string[] = M1.concat(M2.filter((item) => M1.indexOf(item) < 0));
666
-
667
- if (M1.length === 0 && M2.length === 0) {
668
- return true
669
- }
670
- // To compare, both must be different than zero.
671
- if (!this.isZero() && !M.isZero()) {
672
- for (let key of K) {
673
- // The setLetter is not available in one of the monom
674
- if (this._literal[key] === undefined || M.literal[key] === undefined) {
675
- return false;
676
- }
677
- // The setLetter does not have the isSame power in each monoms.
678
- if (!this._literal[key].isEqual(M.literal[key])) {
679
- return false;
680
- }
681
- }
682
- }
683
-
684
- // All are positive check - the monoms are the sames.
685
- return true;
686
- default:
687
- return false;
688
- }
689
- }
690
-
691
- /**
692
- * Determine if the monom is null
693
- */
694
- isZero(): boolean {
695
- return this._coefficient.value === 0;
696
- }
697
-
698
- /**
699
- * Determine if the monom is one
700
- */
701
- isOne(): boolean {
702
- return this._coefficient.value === 1 && this.variables.length === 0;
703
- }
704
-
705
- /**
706
- * Determine if two monoms are equals
707
- * @param M
708
- */
709
- isEqual = (M: Monom): boolean => {
710
- return this.compare(M, '=');
711
- };
712
-
713
- /**
714
- * Determine if two monoms are similar
715
- * @param M
716
- */
717
- isSameAs = (M: Monom): boolean => {
718
- return this.compare(M, 'same');
719
- };
720
-
721
- isSquare = (): boolean => {
722
- if (!this.coefficient.isSquare()) {
723
- return false;
724
- }
725
- return this.isLiteralSquare();
726
- }
727
-
728
- isLiteralSquare = (): boolean => {
729
- for (let letter in this.literal) {
730
- // A literal square must have a natural power
731
- if (this.literal[letter].isRational()) {
732
- return false
733
- }
734
-
735
- // The natural power must be be even
736
- if (this.literal[letter].isEven()) {
737
- return false;
738
- }
739
- }
740
-
741
- return true;
742
- }
743
-
744
- hasFractionCoefficient = (): boolean => {
745
- for (let letter in this._literal) {
746
- if (this._literal[letter].isRational()) {
747
- return true
748
- }
749
- }
750
-
751
- return false
752
- }
753
- // ------------------------------------------
754
- // Misc monoms functions
755
- // -------------------------------------
756
- /**
757
- * Determine if a monom contains a setLetter in it's literal part
758
- * @param letter
759
- */
760
- hasLetter = (letter?: string): boolean => {
761
- // The letter was not found
762
- if (this._literal[letter === undefined ? 'x' : letter] === undefined) {
763
- return false
764
- }
765
-
766
- // The letter is found and is not zero !
767
- return this._literal[letter === undefined ? 'x' : letter].isNotZero();
768
- };
769
-
770
- /**
771
- * Set the power of a particular setLetter
772
- * @param letter (string) Letter to change
773
- * @param pow (number) Power of the setLetter (must be positive integer.
774
- */
775
- setLetter = (letter: string, pow: Fraction | number): void => {
776
- if (pow instanceof Fraction) {
777
- // Set the power of the letter to zero => remove it
778
- if (this.hasLetter(letter) && pow.isZero()) {
779
- delete this._literal[letter]
780
- }
781
-
782
- this._literal[letter] = pow.clone()
783
- } else {
784
- this.setLetter(letter, new Fraction(pow))
785
- }
786
- };
787
-
788
- /**
789
- * Get the degree of a monom. If no setLetter is given, the result will be the global degree.
790
- * @param letter (string) Letter to get to degree (power)
791
- */
792
- degree = (letter?: string): Fraction => {
793
- if (this.variables.length === 0) {
794
- return new Fraction().zero();
795
- }
796
- if (letter === undefined) {
797
- // Not setLetter given -> we get the global monom degree (sum of all the letters).
798
- return Object.values(this._literal).reduce((t, n) => t.clone().add(n));
799
- } else {
800
- // A setLetter is given -> get the corresponding power.
801
- return this._literal[letter] === undefined ? new Fraction().zero() : this._literal[letter].clone();
802
- }
803
- };
804
-
805
- /**
806
- * Evaluate a monom. Each setLetter must be assigned to a Fraction.
807
- * @param values Dictionary of <setLetter: Fraction>
808
- */
809
- evaluate = (values: literalType | Fraction | number): Fraction => {
810
- let r = this.coefficient.clone();
811
-
812
- if (typeof values === 'number' || values instanceof Fraction) {
813
- let tmpValues: literalType = {}
814
- tmpValues[this.variables[0]] = new Fraction(values)
815
- return this.evaluate(tmpValues);
816
- }
817
-
818
- if (typeof values === 'object') {
819
- if (this.variables.length === 0) {
820
- return this.coefficient
821
- }
822
- for (let L in this._literal) {
823
- if (values[L] === undefined) {
824
- return new Fraction().zero();
825
- }
826
-
827
- let value = new Fraction(values[L])
828
-
829
- r.multiply(value.pow(this._literal[L]))
830
- }
831
- }
832
-
833
- return r;
834
- };
835
-
836
- evaluateAsNumeric = (values: { [Key: string]: number } | number): number => {
837
- let r = this.coefficient.value
838
-
839
- if (typeof values === 'number') {
840
- let tmpValues: { [Key: string]: number } = {}
841
- tmpValues[this.variables[0]] = values
842
- return this.evaluateAsNumeric(tmpValues);
843
- }
844
-
845
- if (typeof values === 'object') {
846
- if (this.variables.length === 0) {
847
- return this.coefficient.value
848
- }
849
- for (let L in this._literal) {
850
- if (values[L] === undefined) {
851
- return 0;
852
- }
853
-
854
- r *= values[L] ** (this._literal[L].value)
855
- }
856
- }
857
-
858
- return r
859
- }
860
-
861
- /**
862
- * Derivative the monom
863
- * @param letter
864
- */
865
- derivative = (letter?: string): Monom => {
866
- // No setLetter given - assume it's the setLetter 'x'
867
- if (letter === undefined) {
868
- letter = 'x';
869
- }
870
-
871
- if (this.hasLetter(letter)) {
872
- let d = this._literal[letter].clone(),
873
- dM = this.clone();
874
-
875
- // Subtract one to the degree.
876
- dM._literal[letter].subtract(1)
877
-
878
- // Multiply the coefficient by the previous degree
879
- dM._coefficient.multiply(new Fraction(d.clone()));
880
- return dM;
881
- } else {
882
- return new Monom().zero();
883
- }
884
- };
885
-
886
- primitive = (letter?: string): Monom => {
887
- // TODO: derivative including the ln value => implies creating different monom system ?
888
- if (letter === undefined) {
889
- letter = 'x'
890
- }
891
-
892
- // Zero monom
893
- let M = this.clone(), degree
894
-
895
- if (M.hasLetter(letter)) {
896
- degree = M.degree(letter).clone().add(1)
897
- M.coefficient = M.coefficient.clone().divide(degree)
898
- M.setLetter(letter, degree)
899
- } else {
900
- // There is no letter.
901
-
902
- // The coefficient might be zero (=> x) or a number a (=> ax)
903
- if (M.coefficient.isZero()) {
904
- M.coefficient = new Fraction().one()
905
- }
906
- M.setLetter(letter, 1)
907
- }
908
-
909
- return M
910
- }
911
- // ----------------------------------------
912
- // Static functions
913
- // ----------------------------------------
914
-
915
- // TODO: The rest of the functions are not used or unnecessary ?
916
- /**
917
- * Determine if multiple monoms are similar
918
- * @param M
919
- */
920
- areSameAs = (...M: Monom[]): boolean => {
921
- let result: boolean = true;
922
-
923
- // Check all monoms if they are the isSame as the "this" one.
924
- for (let i = 0; i < M.length; i++) {
925
- if (!this.isSameAs(M[i])) {
926
- return false;
927
- }
928
- }
929
-
930
- // All check passed -> all the monoms are similar.
931
- return result;
932
- };
933
-
934
- /**
935
- * Determine if multiple monoms are equals
936
- * @param M
937
- */
938
- areEquals = (...M: Monom[]): boolean => {
939
- // They are not similar.
940
- if (!this.areSameAs(...M)) {
941
- return false;
942
- }
943
-
944
- // Check all coefficient. They must be equals.
945
- for (let m of M) {
946
- if (!this._coefficient.isEqual(m.coefficient)) {
947
- return false;
948
- }
949
- }
950
-
951
- // All checks passed.
952
- return true;
953
- };
954
-
955
- isDivisible = (div: Monom): boolean => {
956
- // For all variables (letters), the current monom must have a degree higher than the divider
957
- if (div.degree().isStrictlyPositive()) {
958
- for (let letter of div.variables) {
959
- if (!this.degree(letter).geq(div.degree(letter))) {
960
- return false
961
- }
962
- }
963
- }
964
-
965
- // If the coefficient is rational, we suppose we don't need to check the division by the coefficient.
966
- if (this.coefficient.isRational() || div.coefficient.isRational()) {
967
- return true
968
- }
969
-
970
- return this.coefficient.clone().divide(div.coefficient).isRelative()
971
- }
972
-
973
- private _getLiteralDividers(arr: literalType[], letter: string): literalType[] {
974
- let tmpList: { [key: string]: Fraction }[] = [];
975
-
976
- // Be default, this.literal[letter] should be a rational number.
977
- for (let d = 0; d <= this.literal[letter].value; d++) {
978
- if (arr.length === 0) {
979
- let litt: literalType = {}
980
- litt[letter] = new Fraction(d)
981
- tmpList.push(litt)
982
- } else {
983
- for (let item of arr) {
984
- let litt: literalType = {}
985
- for (let currentLetter in item) {
986
- litt[currentLetter] = item[currentLetter]
987
- }
988
- litt[letter] = new Fraction(d)
989
- tmpList.push(litt)
990
- }
991
- }
992
- }
993
- return tmpList;
994
- }
995
-
996
- private _shutingYardToReducedMonom = (inputStr: string): Monom => {
997
- // Get the RPN array of the current expression
998
- const SY: Shutingyard = new Shutingyard().parse(inputStr);
999
- const rpn: { token: string, tokenType: string }[] = SY.rpn;
1000
-
1001
- let stack: Monom[] = [], m, pow, letter, q1, q2
1002
-
1003
- if (rpn.length === 0) {
1004
- this.zero()
1005
- return this
1006
- } else if (rpn.length === 1) {
1007
- const element = rpn[0]
1008
-
1009
- this.one()
1010
- if (element.tokenType === 'coefficient') {
1011
- this.coefficient = new Fraction(element.token)
1012
- } else if (element.tokenType === 'variable') {
1013
- this.setLetter(element.token, 1)
1014
- }
1015
- return this
1016
- } else {
1017
- // Reset the monom
1018
- for (const element of rpn) {
1019
- this.addToken(stack, element)
1020
- }
1021
- }
1022
-
1023
- this.one()
1024
- this.multiply(stack[0])
1025
- return this
1026
- }
1027
-
1028
- }