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,1305 +0,0 @@
1
- "use strict";
2
- /**
3
- * Polynom module contains everything necessary to handle polynoms.*
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Polynom = void 0;
7
- const monom_1 = require("./monom");
8
- const shutingyard_1 = require("../shutingyard");
9
- const numeric_1 = require("../numeric");
10
- const fraction_1 = require("../coefficients/fraction");
11
- const equation_1 = require("./equation");
12
- /**
13
- * Polynom class can handle polynoms, reorder, resolve, ...
14
- * ```
15
- * let P = new Polynom('3x-4')
16
- * ```
17
- */
18
- class Polynom {
19
- /**
20
- *
21
- * @param {string} polynomString (optional) Default polynom to parse on class creation
22
- * @param values
23
- */
24
- constructor(polynomString, ...values) {
25
- this.mark_as_dirty = () => {
26
- this.dirty_factors = true;
27
- this.dirty_zeroes = true;
28
- this.euclidianCache = {};
29
- };
30
- this.addToken = (stack, element) => {
31
- switch (element.tokenType) {
32
- case shutingyard_1.ShutingyardType.COEFFICIENT:
33
- stack.push(new Polynom(element.token));
34
- break;
35
- case shutingyard_1.ShutingyardType.VARIABLE:
36
- stack.push(new Polynom().add(new monom_1.Monom(element.token)));
37
- break;
38
- case shutingyard_1.ShutingyardType.CONSTANT:
39
- // TODO: add constant support to Polynom parsing.
40
- console.log('Actually, not supported - will be added later !');
41
- break;
42
- case shutingyard_1.ShutingyardType.OPERATION:
43
- if (stack.length >= 2) {
44
- const b = stack.pop(), a = stack.pop();
45
- if (element.token === '+') {
46
- stack.push(a.add(b));
47
- }
48
- else if (element.token === '-') {
49
- stack.push(a.subtract(b));
50
- }
51
- else if (element.token === '*') {
52
- stack.push(a.multiply(b));
53
- }
54
- else if (element.token === '/') {
55
- if (b.degree().isStrictlyPositive()) {
56
- console.log('divide by a polynom -> should create a rational polynom !');
57
- }
58
- else {
59
- stack.push(a.divide(b.monoms[0].coefficient));
60
- }
61
- }
62
- else if (element.token === '^') {
63
- if (b.degree().isStrictlyPositive()) {
64
- console.error('Cannot elevate a polynom with another polynom !', a.tex, b.tex);
65
- }
66
- else {
67
- if (b.monoms[0].coefficient.isRelative()) {
68
- // Integer power
69
- stack.push(a.pow(b.monoms[0].coefficient.value));
70
- }
71
- else {
72
- // Only allow power if the previous polynom is only a monom, without coefficient.
73
- if (a.monoms.length === 1 && a.monoms[0].coefficient.isOne()) {
74
- for (let letter in a.monoms[0].literal) {
75
- a.monoms[0].literal[letter].multiply(b.monoms[0].coefficient);
76
- }
77
- stack.push(a);
78
- }
79
- else {
80
- console.error('Cannot have power with fraction');
81
- }
82
- }
83
- }
84
- }
85
- }
86
- else {
87
- if (element.token === '-') {
88
- stack.push(stack.pop().opposed());
89
- }
90
- else {
91
- throw "Error parsing the polynom " + this._rawString;
92
- }
93
- }
94
- break;
95
- case shutingyard_1.ShutingyardType.MONOM:
96
- // Should never appear.
97
- console.error('The monom token should not appear here');
98
- break;
99
- case shutingyard_1.ShutingyardType.FUNCTION:
100
- // Should never appear.
101
- console.error('The function token should not appear here - might be introduced later.');
102
- break;
103
- }
104
- };
105
- // ------------------------------------------
106
- // Creation / parsing functions
107
- // ------------------------------------------
108
- /**
109
- * Parse a string to a polynom.
110
- * @param inputStr
111
- * @param values: as string, numbers or fractions
112
- */
113
- this.parse = (inputStr, ...values) => {
114
- // Reset the main variables.
115
- this._monoms = [];
116
- this._factors = [];
117
- this.mark_as_dirty();
118
- // TODO: allow to enter a liste of Fraction (a, b, c, ...) to make a polynom ax^n + bx^(n-1) + cx^(n-2) + ...
119
- if (typeof inputStr === 'string') {
120
- return this._parseString(inputStr, ...values);
121
- }
122
- else if ((typeof inputStr === 'number' || inputStr instanceof fraction_1.Fraction || inputStr instanceof monom_1.Monom)
123
- && (values === undefined || values.length === 0)) {
124
- this._monoms.push(new monom_1.Monom(inputStr));
125
- }
126
- else if (inputStr instanceof monom_1.Monom && values.length > 0) {
127
- this._monoms.push(new monom_1.Monom(inputStr));
128
- values.forEach(m => {
129
- this._monoms.push(new monom_1.Monom(m));
130
- });
131
- }
132
- else if (inputStr instanceof Polynom) {
133
- for (const m of inputStr.monoms) {
134
- this._monoms.push(m.clone());
135
- }
136
- }
137
- return this;
138
- };
139
- /**
140
- * Clone the polynom
141
- */
142
- this.clone = () => {
143
- const P = new Polynom();
144
- const M = [];
145
- for (const m of this._monoms) {
146
- M.push(m.clone());
147
- }
148
- P.monoms = M;
149
- return P;
150
- };
151
- /**
152
- * Set the polynom to zero.
153
- * @returns {this}
154
- */
155
- this.zero = () => {
156
- this._monoms = [];
157
- this._monoms.push(new monom_1.Monom().zero());
158
- this._rawString = '0';
159
- this.mark_as_dirty();
160
- return this;
161
- };
162
- this.one = () => {
163
- this._monoms = [];
164
- this._monoms.push(new monom_1.Monom().one());
165
- this._rawString = '1';
166
- this.mark_as_dirty();
167
- return this;
168
- };
169
- this.empty = () => {
170
- this._monoms = [];
171
- this._rawString = '';
172
- this.mark_as_dirty();
173
- return this;
174
- };
175
- // ------------------------------------------
176
- this.opposed = () => {
177
- this._monoms = this._monoms.map(m => m.opposed());
178
- this.mark_as_dirty();
179
- return this;
180
- };
181
- // ------------------------------------------
182
- // Mathematical operations
183
- this.add = (...values) => {
184
- this.mark_as_dirty();
185
- // @ts-ignore
186
- for (let value of values) {
187
- if (value instanceof Polynom) {
188
- this._monoms = this._monoms.concat(value.monoms);
189
- }
190
- else if (value instanceof monom_1.Monom) {
191
- this._monoms.push(value.clone());
192
- }
193
- else if (Number.isSafeInteger(value)) {
194
- this._monoms.push(new monom_1.Monom(value.toString()));
195
- }
196
- else {
197
- this._monoms.push(new monom_1.Monom(value));
198
- }
199
- }
200
- return this.reduce();
201
- };
202
- this.subtract = (...values) => {
203
- this.mark_as_dirty();
204
- for (let value of values) {
205
- if (value instanceof Polynom) {
206
- this._monoms = this._monoms.concat(value.clone().opposed().monoms);
207
- }
208
- else if (value instanceof monom_1.Monom) {
209
- this._monoms.push(value.clone().opposed());
210
- }
211
- else if (Number.isSafeInteger(value)) {
212
- this._monoms.push(new monom_1.Monom(value.toString()).opposed());
213
- }
214
- else {
215
- this._monoms.push(new monom_1.Monom(value).opposed());
216
- }
217
- }
218
- return this.reduce();
219
- };
220
- this.multiply = (value) => {
221
- this.mark_as_dirty();
222
- if (value instanceof Polynom) {
223
- return this.multiplyByPolynom(value);
224
- }
225
- else if (value instanceof fraction_1.Fraction) {
226
- return this.multiplyByFraction(value);
227
- }
228
- else if (value instanceof monom_1.Monom) {
229
- return this.multiplyByMonom(value);
230
- }
231
- else if (Number.isSafeInteger(value) && typeof value === 'number') {
232
- return this.multiplyByInteger(value);
233
- }
234
- // Something went wrong...
235
- return this;
236
- };
237
- /**
238
- * Divide the current polynom by another polynom.
239
- * @param P
240
- * returns {quotient: Polynom, reminder: Polynom}
241
- */
242
- this.euclidian = (P) => {
243
- if (this.euclidianCache[P.tex] !== undefined) {
244
- return this.euclidianCache[P.tex];
245
- }
246
- const letter = P.variables[0];
247
- const quotient = new Polynom().zero();
248
- const reminder = this.clone().reorder(letter);
249
- // There is no variable - means it's a number
250
- if (P.variables.length === 0) {
251
- let q = this.clone().divide(P);
252
- return {
253
- quotient: this.clone().divide(P).reduce(),
254
- reminder: new Polynom().zero()
255
- };
256
- }
257
- // Get at least a letter
258
- const maxMP = P.monomByDegree(undefined, letter);
259
- const degreeP = P.degree(letter);
260
- let newM;
261
- // Make the euclidian division of the two polynoms.
262
- let MaxIteration = this.degree(letter).value * 2;
263
- while (reminder.degree(letter).geq(degreeP) && MaxIteration > 0) {
264
- MaxIteration--;
265
- // Get the greatest monom divided by the max monom of the divider
266
- newM = reminder.monomByDegree(undefined, letter).clone().divide(maxMP);
267
- if (newM.isZero())
268
- break;
269
- // Get the new quotient and reminder.
270
- quotient.add(newM);
271
- reminder.subtract(P.clone().multiply(newM));
272
- // Check if the reminder is zero.
273
- if (newM.degree(letter).isZero())
274
- break;
275
- }
276
- quotient.reduce();
277
- reminder.reduce();
278
- return { quotient, reminder };
279
- };
280
- this.divide = (value) => {
281
- this.mark_as_dirty();
282
- if (value instanceof fraction_1.Fraction) {
283
- return this.divideByFraction(value);
284
- }
285
- else if (typeof value === 'number' && Number.isSafeInteger(value)) {
286
- return this.divideByInteger(value);
287
- }
288
- else if (value instanceof monom_1.Monom) {
289
- return this.divide(new Polynom(value));
290
- }
291
- else if (value instanceof Polynom) {
292
- if (value.monoms.length === 1 && value.variables.length === 0) {
293
- return this.divideByFraction(value.monoms[0].coefficient);
294
- }
295
- else {
296
- let { quotient, reminder } = this.euclidian(value);
297
- if (reminder.isZero()) {
298
- return quotient;
299
- }
300
- else {
301
- console.log(`${this.tex} is not divideable by ${value.tex}`);
302
- return new Polynom().zero();
303
- }
304
- }
305
- }
306
- };
307
- this.pow = (nb) => {
308
- this.mark_as_dirty();
309
- if (!Number.isSafeInteger(nb)) {
310
- return this.zero();
311
- }
312
- if (nb < 0) {
313
- return this.zero();
314
- }
315
- if (nb === 0) {
316
- return new Polynom();
317
- }
318
- const P = this.clone();
319
- for (let i = 1; i < nb; i++) {
320
- this.multiply(P);
321
- }
322
- return this.reduce();
323
- };
324
- // ------------------------------------------
325
- /**
326
- * Compare the current coefficient with another coefficient
327
- * @param P
328
- * @param sign (string| default is =): authorized values: =, <, <=, >, >= with some variations.
329
- */
330
- this.compare = (P, sign) => {
331
- if (sign === undefined) {
332
- sign = '=';
333
- }
334
- // Create clone version to reduce them without altering the original polynoms.
335
- const cP1 = this.clone().reduce().reorder();
336
- const cP2 = P.clone().reduce().reorder();
337
- switch (sign) {
338
- case '=':
339
- // They must have the isSame length and the isSame degree
340
- if (cP1.length !== cP2.length || cP1.degree().isNotEqual(cP2.degree())) {
341
- return false;
342
- }
343
- // Check if the coefficients are the isSame.
344
- for (const i in cP1.monoms) {
345
- if (!cP1.monoms[i].isEqual(cP2.monoms[i])) {
346
- return false;
347
- }
348
- }
349
- return true;
350
- case 'same':
351
- // They must have the isSame length and the isSame degree
352
- if (cP1.length !== cP2.length || cP1.degree() !== cP2.degree()) {
353
- return false;
354
- }
355
- for (const i in cP1.monoms) {
356
- if (!cP1.monoms[i].isSameAs(cP2.monoms[i])) {
357
- return false;
358
- }
359
- }
360
- return true;
361
- default:
362
- return false;
363
- }
364
- };
365
- this.isEqual = (P) => {
366
- return this.compare(P, '=');
367
- };
368
- this.isSameAs = (P) => {
369
- return this.compare(P, 'same');
370
- };
371
- this.isOpposedAt = (P) => {
372
- return this.compare(P.clone().opposed(), '=');
373
- };
374
- this.isFactorized = (polynomString, soft) => {
375
- let P;
376
- // Check if polynom is complete...
377
- if (polynomString.split('(').length !== polynomString.split(')').length) {
378
- return false;
379
- }
380
- // Try to build the polynom
381
- try {
382
- P = new Polynom(polynomString);
383
- }
384
- catch (e) {
385
- return false;
386
- }
387
- // Both polynom aren't the same (once developed and reduced => they cannot be equivalent)
388
- if (!this.isEqual(P)) {
389
- return false;
390
- }
391
- // Check if the provided (string) version is fully factorized.
392
- // Run a regex on the string.
393
- let polynomStringNormalized = polynomString.replaceAll('*', ''), polynomStringReduced = '' + polynomStringNormalized, factors = [];
394
- for (let x of polynomStringNormalized.matchAll(/\(([a-z0-9+\-]+)\)(\^[0-9]*)?/g)) {
395
- if (x[2] !== undefined) {
396
- // if there is an exponential value, add it multiple times
397
- for (let i = 0; i < +x[2].substring(1); i++) {
398
- factors.push(x[1]);
399
- }
400
- }
401
- else {
402
- // no power - add it once.
403
- factors.push(x[1]);
404
- }
405
- // Remove the current polynom
406
- polynomStringReduced = polynomStringReduced.replaceAll(x[0], '');
407
- }
408
- if (polynomStringReduced !== '') {
409
- factors.push(polynomStringReduced);
410
- }
411
- let polyFactors = factors.map(x => new Polynom(x));
412
- // polyFactors contain all polynoms.
413
- let checkPolyFactors = polyFactors.filter(x => x.degree().geq(1) && !x.commonMonom().isOne());
414
- // Some polynoms are not completely factorized.
415
- if (checkPolyFactors.length > 0 && !soft) {
416
- return false;
417
- }
418
- if (checkPolyFactors.length > 0 && soft) {
419
- polyFactors = polyFactors.filter(x => x.commonMonom().isOne());
420
- let FactorizedConstant = new fraction_1.Fraction().one();
421
- for (let p of checkPolyFactors) {
422
- let k = p.commonMonom(), pFactor = p.clone().divide(k);
423
- if (k.degree().isZero()) {
424
- FactorizedConstant.multiply(k.coefficient);
425
- polyFactors.push(pFactor.clone());
426
- }
427
- }
428
- }
429
- // Factorize the current polynom.
430
- this.factorize();
431
- // Compare the given factors with the generated factors
432
- let sign = 1, notFoundedFactors = [];
433
- for (let f of this.factors) {
434
- // The factor is just a coefficient. Might be opposed
435
- if (f.degree().isZero()) {
436
- if (f.monoms[0].coefficient.isNegativeOne()) {
437
- sign = -sign;
438
- }
439
- }
440
- let factorFound = false;
441
- for (let i = 0; i < polyFactors.length; i++) {
442
- if (f.isEqual(polyFactors[i])) {
443
- polyFactors.splice(i, 1);
444
- factorFound = true;
445
- break;
446
- }
447
- else if (f.isOpposedAt(polyFactors[i])) {
448
- polyFactors.splice(i, 1);
449
- sign = -sign;
450
- factorFound = true;
451
- break;
452
- }
453
- }
454
- if (!factorFound) {
455
- notFoundedFactors.push(f.clone());
456
- }
457
- }
458
- // The polyfactors must be empty and the cumulative opposite factors must be 1.
459
- return (polyFactors.length === 0 && sign === 1);
460
- };
461
- // ------------------------------------------
462
- // Compare functions
463
- this.isReduced = (polynomString) => {
464
- // The polynom must be developed to be reduced.
465
- if (!this.isDeveloped(polynomString)) {
466
- return false;
467
- }
468
- let P = new Polynom(polynomString);
469
- if (P.monoms.length > this.monoms.length) {
470
- return false;
471
- }
472
- // TODO: Not ur the reduced systme checking is working properly !
473
- for (let m of P.monoms) {
474
- if (!m.coefficient.isReduced()) {
475
- return false;
476
- }
477
- }
478
- return false;
479
- };
480
- this.isDeveloped = (polynomString) => {
481
- let P;
482
- // Start by removing the parenthis after a "power"
483
- let pString = polynomString.replaceAll(/\^\(([-0-9/]+)\)/g, '$1');
484
- // There is at least one parenthese - it is not developed.
485
- if (pString.includes('(') || pString.includes(')')) {
486
- return false;
487
- }
488
- // Try to build the polynom
489
- try {
490
- // Build the polynom
491
- P = new Polynom(polynomString);
492
- }
493
- catch (e) {
494
- return false;
495
- }
496
- // Both polynom aren't the same (once developed and reduced => they cannot be equivalent)
497
- if (!this.isEqual(P)) {
498
- return false;
499
- }
500
- // Check that everything is completely developed. Actually, there are no parentheses... so it is fully developed
501
- return true;
502
- // // maybe it wasn't reduced and not ordered...
503
- // // compare polynom string.
504
- //
505
- // // normalize the string
506
- // let polynomStringNormalized = polynomString.replaceAll('[*\s]', '')
507
- //
508
- // // Determine if it's the exact same string.
509
- // // TODO: Maybe it's enough to just make this test !a
510
- // return polynomStringNormalized === P.reduce().reorder().display
511
- };
512
- // -------------------------------------
513
- this.reduce = () => {
514
- // Reduce the polynom
515
- let values = this._monoms.map(x => x.clone()), vars = [...this.variables];
516
- // Group the monoms by similarity
517
- let i = 0;
518
- while (i < this._monoms.length) {
519
- for (let j = i + 1; j < this._monoms.length; j++) {
520
- if (this._monoms[i].isSameAs(this._monoms[j])) {
521
- this._monoms[i].add(this._monoms[j]);
522
- this._monoms.splice(j, 1);
523
- if (this._monoms[i].isZero()) {
524
- this._monoms[i] = new monom_1.Monom().zero();
525
- }
526
- j--;
527
- }
528
- }
529
- i++;
530
- }
531
- //
532
- //
533
- //
534
- // let coeffs = values.filter(x => x.variables.length === 0)
535
- //
536
- // if (coeffs.length > 0) {
537
- // this._monoms.push(coeffs.reduce((a, b) => a.add(b)))
538
- // }
539
- //
540
- // // Build the new monoms
541
- // for (let letter of vars) {
542
- // // Monom with same letters, but might be of different degrees
543
- // let M = values.filter(x => x.hasLetter(letter))
544
- //
545
- // while (M.length > 0) {
546
- // // Take the first element
547
- // const m = M.shift(), degree = m.degree(letter)
548
- //
549
- // for (let a of M.filter(x => x.degree(letter).isEqual(degree))) {
550
- // m.add(a)
551
- // }
552
- //
553
- // this._monoms.push(m)
554
- //
555
- // // Make the new array.
556
- // M = M.filter(x => x.degree(letter).isNotEqual(degree))
557
- // }
558
- // // reduce the monom
559
- //
560
- // }
561
- // Remove all null monoms
562
- this._monoms = this._monoms.filter((m) => {
563
- return m.coefficient.value !== 0;
564
- });
565
- // Reduce all monoms coefficient.
566
- for (const m of this._monoms) {
567
- m.coefficient.reduce();
568
- }
569
- if (this.length === 0) {
570
- return new Polynom().zero();
571
- }
572
- return this.reorder();
573
- };
574
- this.reorder = (letter = 'x', revert) => {
575
- if (revert === undefined) {
576
- revert = false;
577
- }
578
- // TODO: Must handle multiple setLetter reorder system
579
- let otherLetters = this.variables.filter(x => x !== letter);
580
- this._monoms.sort(function (a, b) {
581
- let da = a.degree(letter).value, db = b.degree(letter).value;
582
- // Values are different
583
- if (da !== db)
584
- return revert ? da - db : db - da;
585
- // if values are equals, check other letters - it must be revert in that case !
586
- if (otherLetters.length > 0) {
587
- for (let L of otherLetters) {
588
- let da = a.degree(L).value, db = b.degree(L).value;
589
- // Values are different
590
- if (da !== db)
591
- return revert ? da - db : db - da;
592
- }
593
- }
594
- return 0;
595
- // return b.degree(letter).clone().subtract(a.degree(letter)).value
596
- });
597
- return this;
598
- };
599
- this.degree = (letter) => {
600
- let d = new fraction_1.Fraction().zero();
601
- for (const m of this._monoms) {
602
- d = fraction_1.Fraction.max(m.degree(letter).value, d);
603
- }
604
- return d;
605
- };
606
- this.letters = () => {
607
- let L = [], S = new Set();
608
- for (let m of this._monoms) {
609
- S = new Set([...S, ...m.variables]);
610
- }
611
- // @ts-ignore
612
- return [...S];
613
- };
614
- /**
615
- * Replace a variable (letter) by a polynom.
616
- * @param letter
617
- * @param P
618
- */
619
- this.replaceBy = (letter, P) => {
620
- this.mark_as_dirty();
621
- let pow;
622
- const resultPolynom = new Polynom().zero();
623
- for (const m of this.monoms) {
624
- if (m.literal[letter] === undefined || m.literal[letter].isZero()) {
625
- resultPolynom.add(m.clone());
626
- }
627
- else {
628
- // We have found a setLetter.
629
- // Get the power and reset it.
630
- pow = m.literal[letter].clone();
631
- delete m.literal[letter];
632
- // TODO: replaceBy works only with positive and natural pow
633
- resultPolynom.add(P.clone().pow(Math.abs(pow.numerator)).multiply(m));
634
- }
635
- }
636
- this._monoms = resultPolynom.reduce().reorder().monoms;
637
- return this;
638
- };
639
- // Evaluate a polynom.
640
- this.evaluate = (values) => {
641
- const r = new fraction_1.Fraction().zero();
642
- this._monoms.forEach(monom => {
643
- //console.log('Evaluate polynom: ', monom.display, values, monom.evaluate(values).display);
644
- r.add(monom.evaluate(values));
645
- });
646
- return r;
647
- };
648
- this.evaluateAsNumeric = (values) => {
649
- let r = 0;
650
- this._monoms.forEach(monom => {
651
- r += monom.evaluateAsNumeric(values);
652
- });
653
- return r;
654
- };
655
- this.derivative = (letter) => {
656
- let dP = new Polynom();
657
- for (let m of this._monoms) {
658
- dP.add(m.derivative(letter));
659
- }
660
- return dP;
661
- };
662
- // ------------------------------------------
663
- // Misc polynoms functions
664
- this.primitive = (letter) => {
665
- let dP = new Polynom();
666
- for (let m of this._monoms) {
667
- dP.add(m.primitive(letter));
668
- }
669
- return dP;
670
- };
671
- this.integrate = (a, b, letter) => {
672
- const primitive = this.primitive(letter);
673
- if (letter === undefined) {
674
- letter = 'x';
675
- }
676
- let valuesA = {}, valuesB = {};
677
- valuesA[letter] = new fraction_1.Fraction(a);
678
- valuesB[letter] = new fraction_1.Fraction(b);
679
- return primitive.evaluate(valuesB).subtract(primitive.evaluate(valuesA));
680
- };
681
- // -------------------------------------
682
- /**
683
- * Factorize a polynom and store the best results in factors.
684
- * @param maxValue Defines the greatest value to search to (default is 20).
685
- */
686
- this.factorize = (letter) => {
687
- if (!this.dirty_factors) {
688
- return this._factors;
689
- }
690
- let factors = [];
691
- let P = this.clone().reorder();
692
- // Extract the common monom
693
- // 2x^3+6x^2 => 2x^2
694
- let M = P.commonMonom();
695
- // If the polynom starts with a negative monom, factorize it.
696
- if (P.monomByDegree().coefficient.isStrictlyNegative() && M.coefficient.isStrictlyPositive() && !M.isOne()) {
697
- M.opposed();
698
- }
699
- if (!M.isOne()) {
700
- let tempPolynom = new Polynom(M);
701
- factors = [tempPolynom.clone()];
702
- P = P.euclidian(tempPolynom).quotient;
703
- }
704
- // Main loop
705
- let securityLoop = P.degree().clone().multiply(2).value, maxDegree = 1;
706
- while (securityLoop >= 0) {
707
- securityLoop--;
708
- if (P.monoms.length < 2) {
709
- // The polynom has only one monom => 7x^2
710
- // No need to continue.
711
- if (!P.isOne()) {
712
- factors.push(P.clone());
713
- P.one();
714
- }
715
- break;
716
- }
717
- else if (P.degree(letter).isOne()) {
718
- // The polynom is a first degree polynom => 3x-5
719
- // No need to continue
720
- factors.push(P.clone());
721
- P.one();
722
- break;
723
- }
724
- else {
725
- // Create the list of all "potential" polynom dividers.
726
- let allDividers = this._getAllPotentialFactors(P, maxDegree, letter);
727
- maxDegree = P.degree(letter).value;
728
- // Actually: 100ms
729
- while (allDividers.length > 0) {
730
- let div = allDividers[0];
731
- if (!P.isDividableBy(div)) {
732
- // Not dividable. Remove it from the list
733
- allDividers.shift();
734
- }
735
- else {
736
- // It's dividable - so make the division
737
- let result = P.euclidian(div);
738
- // Add the factor
739
- factors.push(div);
740
- // As it's dividable, get the quotient.
741
- P = result.quotient.clone();
742
- // filter all dividers that are no more suitable.
743
- allDividers = allDividers.filter(x => {
744
- let pX = P.monoms[0], pC = P.monoms[P.monoms.length - 1], dX = x.monoms[0], dC = x.monoms[x.monoms.length - 1];
745
- // Check last item (degree zero)
746
- if (!pC.isDivisible(dC))
747
- return false;
748
- // Check the first item (degree max)
749
- return pX.isDivisible(dX);
750
- });
751
- }
752
- }
753
- }
754
- }
755
- // Maybe there is still something in the Polynom (not everything was possible to factorize)
756
- if (!P.isOne()) {
757
- factors.push(P.clone());
758
- }
759
- // Save the factors
760
- this._factors = factors;
761
- // The factors list is no more dirty
762
- this.dirty_factors = false;
763
- return this._factors;
764
- };
765
- this.isDividableBy = (div) => {
766
- // Quick evaluation.
767
- if (div.degree().isOne()) {
768
- let zero = div.getZeroes()[0];
769
- if (zero.exact instanceof fraction_1.Fraction) {
770
- return this.evaluate(zero.exact).isZero();
771
- }
772
- else {
773
- return false;
774
- }
775
- }
776
- else {
777
- this.euclidianCache[div.tex] = this.euclidian(div);
778
- return this.euclidianCache[div.tex].reminder.isZero();
779
- }
780
- };
781
- // TODO: get zeroes for more than first degree and for more than natural degrees
782
- this.getZeroes = () => {
783
- if (this.dirty_zeroes) {
784
- let equ = new equation_1.Equation(this.clone(), 0);
785
- equ.solve();
786
- this._zeroes = equ.solutions;
787
- this.dirty_zeroes = false;
788
- }
789
- return this._zeroes;
790
- };
791
- // TODO: analyse the next functions to determine if they are useful or not...
792
- this.monomByDegree = (degree, letter) => {
793
- if (degree === undefined) {
794
- // return the highest degree monom.
795
- return this.monomByDegree(this.degree(letter), letter);
796
- }
797
- // Reduce the polynom.
798
- const M = this.clone().reduce();
799
- for (const m of M._monoms) {
800
- if (m.degree(letter).isEqual(degree)) {
801
- return m.clone();
802
- }
803
- }
804
- // Nothing was found - return the null monom.
805
- return new monom_1.Monom().zero();
806
- };
807
- this.monomsByDegree = (degree, letter) => {
808
- if (degree === undefined) {
809
- // return the highest degree monom.
810
- return this.monomsByDegree(this.degree(letter));
811
- }
812
- // Reduce the polynom.
813
- let Ms = [];
814
- const M = this.clone().reduce();
815
- for (const m of M._monoms) {
816
- if (m.degree(letter) === degree) {
817
- Ms.push(m.clone());
818
- }
819
- }
820
- return Ms;
821
- // Nothing was found - retur
822
- };
823
- // Used in LinearSystem.tex
824
- this.monomByLetter = (letter) => {
825
- const M = this.clone().reduce();
826
- for (const m of M._monoms) {
827
- if (m.hasLetter(letter)) {
828
- return m.clone();
829
- }
830
- }
831
- return new monom_1.Monom().zero();
832
- };
833
- // Next functions are used for for commonMonom, which is used in the factorize method.
834
- this.getDenominators = () => {
835
- const denominators = [];
836
- for (const m of this._monoms) {
837
- denominators.push(m.coefficient.denominator);
838
- }
839
- return denominators;
840
- };
841
- this.getNumerators = () => {
842
- const numerators = [];
843
- for (const m of this._monoms) {
844
- numerators.push(m.coefficient.numerator);
845
- }
846
- return numerators;
847
- };
848
- this.lcmDenominator = () => {
849
- return numeric_1.Numeric.lcm(...this.getDenominators());
850
- };
851
- // ------------------------------------------
852
- // Polynoms factorization functions
853
- this.gcdDenominator = () => {
854
- return numeric_1.Numeric.gcd(...this.getDenominators());
855
- };
856
- this.lcmNumerator = () => {
857
- return numeric_1.Numeric.lcm(...this.getNumerators());
858
- };
859
- this.gcdNumerator = () => {
860
- return numeric_1.Numeric.gcd(...this.getNumerators());
861
- };
862
- // ------------------------------------------
863
- // Polynoms helpers functions
864
- // -------------------------------------
865
- this.commonMonom = () => {
866
- let M = new monom_1.Monom().one(), numerator, denominator, degree = this.degree();
867
- numerator = this.gcdNumerator();
868
- denominator = this.gcdDenominator();
869
- M.coefficient = new fraction_1.Fraction(numerator, denominator);
870
- for (let L of this.variables) {
871
- // Initialize the setLetter with the max degree
872
- M.setLetter(L, degree);
873
- for (let m of this._monoms) {
874
- M.setLetter(L, fraction_1.Fraction.min(m.degree(L), M.degree(L)));
875
- if (M.degree(L).isZero()) {
876
- break;
877
- }
878
- }
879
- }
880
- return M;
881
- };
882
- this.limitToInfinity = (letter) => {
883
- const M = this.monomByDegree(undefined, letter), sign = M.coefficient.sign(), degree = M.degree(letter);
884
- if (degree.isStrictlyPositive()) {
885
- return sign === 1 ? (new fraction_1.Fraction()).infinite() : (new fraction_1.Fraction()).infinite().opposed();
886
- }
887
- else if (degree.isZero()) {
888
- return M.coefficient;
889
- }
890
- // Any other cases
891
- return (new fraction_1.Fraction()).zero();
892
- };
893
- this.limitToNegativeInfinity = (letter) => {
894
- const M = this.monomByDegree(undefined, letter), sign = M.coefficient.sign(), degree = M.degree(letter);
895
- if (degree.isStrictlyPositive()) {
896
- return sign === -1 ? (new fraction_1.Fraction()).infinite() : (new fraction_1.Fraction()).infinite().opposed();
897
- }
898
- else if (degree.isZero()) {
899
- return M.coefficient;
900
- }
901
- // Any other cases
902
- return (new fraction_1.Fraction()).zero();
903
- };
904
- this._getAllPotentialFactors = (P, maxDegree, letter) => {
905
- let m1 = P.monoms[0].dividers, m2 = P.monoms[P.monoms.length - 1].dividers;
906
- let allDividers = [];
907
- m1.forEach(m1d => {
908
- // Get only polynom that has a degree less than a specific value
909
- if (m1d.degree(letter).leq(maxDegree)) {
910
- m2.forEach(m2d => {
911
- if (m1d.degree(letter).isNotEqual(m2d.degree(letter))) {
912
- allDividers.push(new Polynom(m1d, m2d));
913
- allDividers.push(new Polynom(m1d, m2d.clone().opposed()));
914
- }
915
- });
916
- }
917
- });
918
- return allDividers;
919
- };
920
- this.genDisplay = (output, forceSign, wrapParentheses, withAllMultSign) => {
921
- let P = '';
922
- for (const k of this._monoms) {
923
- if (k.coefficient.value === 0) {
924
- continue;
925
- }
926
- // The monom to be displayed
927
- let m;
928
- if (withAllMultSign) {
929
- m = k.plotFunction;
930
- }
931
- else {
932
- m = (output === 'tex') ? k.tex : k.display;
933
- }
934
- P += `${(k.coefficient.sign() === 1 && (P !== '' || forceSign === true)) ? '+' : ''}${m}`;
935
- }
936
- if (wrapParentheses === true && this.length > 1) {
937
- if (output === 'tex') {
938
- P = `\\left( ${P} \\right)`;
939
- }
940
- else {
941
- P = `(${P})`;
942
- }
943
- }
944
- if (P === '') {
945
- P = '0';
946
- }
947
- return P;
948
- };
949
- /**
950
- * Main parse using a shutting yard class
951
- * @param inputStr
952
- */
953
- this.shutingYardToReducedPolynom = (inputStr) => {
954
- // Get the RPN array of the current expression
955
- const SY = new shutingyard_1.Shutingyard().parse(inputStr);
956
- const rpn = SY.rpn;
957
- // New version for reducing shuting yard.
958
- this.zero();
959
- let stack = [], monom = new monom_1.Monom();
960
- // Loop through the
961
- for (const element of rpn) {
962
- this.addToken(stack, element);
963
- }
964
- if (stack.length === 1) {
965
- this.add(stack[0]);
966
- }
967
- return this.reorder();
968
- };
969
- this.multiplyByPolynom = (P) => {
970
- const M = [];
971
- for (const m1 of this._monoms) {
972
- for (const m2 of P.monoms) {
973
- M.push(monom_1.Monom.xmultiply(m1, m2));
974
- }
975
- }
976
- this._monoms = M;
977
- return this.reduce();
978
- };
979
- this.multiplyByFraction = (F) => {
980
- for (const m of this._monoms) {
981
- m.coefficient.multiply(F);
982
- }
983
- return this.reduce();
984
- };
985
- this.multiplyByInteger = (nb) => {
986
- return this.multiplyByFraction(new fraction_1.Fraction(nb));
987
- };
988
- this.multiplyByMonom = (M) => {
989
- for (const m of this._monoms) {
990
- m.multiply(M);
991
- }
992
- return this.reduce();
993
- };
994
- this.divideByInteger = (nb) => {
995
- const nbF = new fraction_1.Fraction(nb);
996
- for (const m of this._monoms) {
997
- m.coefficient.divide(nbF);
998
- }
999
- return this;
1000
- };
1001
- this.divideByFraction = (F) => {
1002
- for (const m of this._monoms) {
1003
- m.coefficient.divide(F);
1004
- }
1005
- return this;
1006
- };
1007
- this._factorize2ndDegree = (letter) => {
1008
- let P1, P2, a, b, c, delta, x1, x2, factor;
1009
- // One variable only
1010
- if (this.numberOfVars === 1) {
1011
- a = this.monomByDegree(2, letter).coefficient;
1012
- b = this.monomByDegree(1, letter).coefficient;
1013
- c = this.monomByDegree(0, letter).coefficient;
1014
- delta = b.clone().pow(2).subtract(a.clone().multiply(c).multiply(4));
1015
- if (delta.isZero()) {
1016
- x1 = b.clone().opposed().divide(a.clone().multiply(2));
1017
- P1 = new Polynom(letter).subtract(x1.display).multiply(x1.denominator);
1018
- P2 = new Polynom(letter).subtract(x1.display).multiply(x1.denominator);
1019
- factor = a.divide(x1.denominator).divide(x1.denominator);
1020
- if (!factor.isOne()) {
1021
- // TODO: Update new Polynom to accept anything...
1022
- return [new Polynom(factor.display), P1, P2];
1023
- }
1024
- else {
1025
- return [P1, P2];
1026
- }
1027
- }
1028
- else if (delta.isPositive() && delta.isSquare()) {
1029
- x1 = b.clone().opposed()
1030
- .add(delta.clone().sqrt())
1031
- .divide(a.clone().multiply(2));
1032
- x2 = b.clone().opposed()
1033
- .subtract(delta.clone().sqrt())
1034
- .divide(a.clone().multiply(2));
1035
- // (2x+5)(3x-2)
1036
- // 6x^2+11x-10
1037
- // a = 6, b = 11, c = -10
1038
- // delta = 121-4*6*(-10) = 361= 19^2
1039
- // x1 = (-11 + 19) / 12 = 8/12 = 2/3
1040
- // x2 = (-11 - 19) / 12 = -30/12 = -5/2
1041
- factor = a.divide(x1.denominator).divide(x2.denominator);
1042
- if (factor.isOne()) {
1043
- return [
1044
- new Polynom(letter).subtract(x1.display).multiply(x1.denominator),
1045
- new Polynom(letter).subtract(x2.display).multiply(x2.denominator),
1046
- ];
1047
- }
1048
- else {
1049
- return [
1050
- new Polynom(factor.display),
1051
- new Polynom(letter).subtract(x1.display).multiply(x1.denominator),
1052
- new Polynom(letter).subtract(x2.display).multiply(x2.denominator),
1053
- ];
1054
- }
1055
- }
1056
- else {
1057
- // No solution possible - return the complete value.
1058
- return [this.clone()];
1059
- }
1060
- }
1061
- else {
1062
- // If multiple variables, only handle perfect squares...
1063
- a = this.monomByDegree(2, letter);
1064
- b = this.monomByDegree(1, letter);
1065
- c = this.monomByDegree(0, letter);
1066
- if (a.isLiteralSquare() && c.isLiteralSquare()) {
1067
- // Check the middle item is same as...
1068
- if (b.clone().pow(2).isSameAs(a.clone().multiply(c))) {
1069
- // Determine if the coefficient values matches.
1070
- // Search 4 values (r, s, t, u) that matches:
1071
- // (r X + s Y)(t X + u Y) = rt X^2 + (ru + st) XY + su Y^2
1072
- let xPolynom = new Polynom('x', a.coefficient, b.coefficient, c.coefficient);
1073
- let xFactors = xPolynom._factorize2ndDegree('x');
1074
- let factors = [], xyzPolynom;
1075
- if (xFactors.length >= 2) {
1076
- for (let p of xFactors) {
1077
- if (p.degree().isZero()) {
1078
- factors.push(p.clone());
1079
- }
1080
- else {
1081
- xyzPolynom = p.clone();
1082
- xyzPolynom.monoms[0].literal = a.literalSqrt;
1083
- xyzPolynom.monoms[1].literal = c.literalSqrt;
1084
- factors.push(xyzPolynom.clone());
1085
- }
1086
- }
1087
- return factors;
1088
- }
1089
- }
1090
- }
1091
- return [this.clone()];
1092
- }
1093
- };
1094
- this._factorizeByGroups = () => {
1095
- // TODO: Factorize by groups.
1096
- return [];
1097
- };
1098
- this._monoms = [];
1099
- this._factors = [];
1100
- this.mark_as_dirty();
1101
- if (polynomString !== undefined) {
1102
- this.parse(polynomString, ...values);
1103
- }
1104
- return this;
1105
- }
1106
- // ------------------------------------------
1107
- get dirty_factors() {
1108
- return this._dirty_factors;
1109
- }
1110
- set dirty_factors(value) {
1111
- this._dirty_factors = value;
1112
- }
1113
- get dirty_zeroes() {
1114
- return this._dirty_zeroes;
1115
- }
1116
- set dirty_zeroes(value) {
1117
- this._dirty_zeroes = value;
1118
- }
1119
- get euclidianCache() {
1120
- return this._euclidianCache;
1121
- }
1122
- set euclidianCache(value) {
1123
- this._euclidianCache = value;
1124
- }
1125
- get factors() {
1126
- return this.factorize();
1127
- }
1128
- set factors(value) {
1129
- this.mark_as_dirty();
1130
- this._factors = value;
1131
- }
1132
- // ------------------------------------------
1133
- get monoms() {
1134
- return this._monoms;
1135
- }
1136
- set monoms(M) {
1137
- this._monoms = M;
1138
- }
1139
- get texString() {
1140
- return this._texString;
1141
- }
1142
- get zeroes() {
1143
- return this.getZeroes();
1144
- }
1145
- get texFactors() {
1146
- this.factorize();
1147
- if (this.factors.length <= 1) {
1148
- return this.tex;
1149
- }
1150
- // Build an array of texFactors with the number of similar items.
1151
- let factorsCount = {};
1152
- for (let f of this.factors) {
1153
- if (factorsCount[f.tex] !== undefined) {
1154
- factorsCount[f.tex].degree++;
1155
- }
1156
- else {
1157
- factorsCount[f.tex] = {
1158
- degree: 1,
1159
- factor: f
1160
- };
1161
- }
1162
- }
1163
- // First round to put the 'monom' first
1164
- let simpleFactor = new Polynom().one();
1165
- for (let item of Object.values(factorsCount).filter(item => item.factor.monoms.length === 1)) {
1166
- simpleFactor.multiply(item.factor);
1167
- }
1168
- let tex = simpleFactor.isOne() ? '' : simpleFactor.tex;
1169
- // Loop through all factors that contains at least 2 monoms.
1170
- for (let item of Object.values(factorsCount).filter(item => item.factor.monoms.length > 1)) {
1171
- if (item.factor.length > 1) {
1172
- tex += `\\left( ${item.factor.tex} \\right)${item.degree > 1 ? '^{ ' + item.degree + ' }' : ''}`;
1173
- }
1174
- }
1175
- return tex;
1176
- }
1177
- get displayFactors() {
1178
- this.factorize();
1179
- if (this.factors.length <= 1) {
1180
- return this.display;
1181
- }
1182
- // Build an array of texFactors with the number of similar items.
1183
- let factorsCount = {};
1184
- for (let f of this.factors) {
1185
- if (factorsCount[f.display] !== undefined) {
1186
- factorsCount[f.display].degree++;
1187
- }
1188
- else {
1189
- factorsCount[f.display] = {
1190
- degree: 1,
1191
- factor: f
1192
- };
1193
- }
1194
- }
1195
- // First round to put the 'monom' first
1196
- let simpleFactor = new Polynom().one();
1197
- for (let item of Object.values(factorsCount).filter(item => item.factor.monoms.length === 1)) {
1198
- simpleFactor.multiply(item.factor);
1199
- }
1200
- let display = simpleFactor.isOne() ? '' : simpleFactor.display;
1201
- // Loop through all factors that contains at least 2 monoms.
1202
- for (let item of Object.values(factorsCount).filter(item => item.factor.monoms.length > 1)) {
1203
- if (item.factor.length > 1) {
1204
- display += `(${item.factor.display})${item.degree > 1 ? '^(' + item.degree + ')' : ''}`;
1205
- }
1206
- }
1207
- return display;
1208
- }
1209
- get length() {
1210
- // TODO: Must reduce the monoms list to remove the zero coefficient.
1211
- return this._monoms.length;
1212
- }
1213
- get display() {
1214
- return this.genDisplay();
1215
- }
1216
- get raw() {
1217
- return this._rawString;
1218
- }
1219
- get tex() {
1220
- return this.genDisplay('tex');
1221
- }
1222
- get isMultiVariable() {
1223
- const B = false;
1224
- for (const m of this._monoms) {
1225
- if (m.variables.length > 1) {
1226
- return true;
1227
- }
1228
- }
1229
- return B;
1230
- }
1231
- get variables() {
1232
- let V = [];
1233
- for (const m of this._monoms) {
1234
- V = V.concat(m.variables);
1235
- }
1236
- // Remove duplicates.
1237
- V = [...new Set(V)];
1238
- V.sort();
1239
- return V;
1240
- }
1241
- get numberOfVars() {
1242
- return this.variables.length;
1243
- }
1244
- get plotFunction() {
1245
- return this.genDisplay('tex', false, false, true);
1246
- }
1247
- isZero() {
1248
- return (this._monoms.length === 1 && this._monoms[0].coefficient.isZero()) || this._monoms.length === 0;
1249
- }
1250
- isOne() {
1251
- return this._monoms.length === 1 && this._monoms[0].coefficient.isOne();
1252
- }
1253
- _parseString(inputStr, ...values) {
1254
- if (values === undefined || values.length === 0) {
1255
- inputStr = '' + inputStr;
1256
- this._rawString = inputStr.trim().replaceAll(' ', '');
1257
- // Parse the polynom using the shutting yard algorithm
1258
- if (inputStr !== '' && !isNaN(Number(inputStr))) {
1259
- this.empty();
1260
- // It's a simple number.
1261
- let m = new monom_1.Monom(inputStr);
1262
- // m.coefficient = new Fraction(inputStr);
1263
- // m.literalStr = '';
1264
- this.add(m);
1265
- return this;
1266
- }
1267
- // Parse the string.
1268
- return this.shutingYardToReducedPolynom(inputStr);
1269
- }
1270
- else if (/^[a-z]/.test(inputStr)) {
1271
- // We assume the inputStr contains only letters.
1272
- this.empty();
1273
- let fractions = values.map(x => new fraction_1.Fraction(x));
1274
- // Multiple setLetter version
1275
- if (inputStr.length > 1) {
1276
- // TODO: check that the number of values given correspond to the letters (+1 eventually)
1277
- let letters = inputStr.split(''), i = 0;
1278
- for (let F of fractions) {
1279
- let m = new monom_1.Monom();
1280
- m.coefficient = F.clone();
1281
- m.literalStr = letters[i] || '';
1282
- this.add(m);
1283
- i++;
1284
- }
1285
- }
1286
- // Single setLetter version
1287
- else {
1288
- let n = fractions.length - 1;
1289
- for (let F of fractions) {
1290
- let m = new monom_1.Monom();
1291
- m.coefficient = F.clone();
1292
- m.literalStr = `${inputStr}^${n}`;
1293
- this.add(m);
1294
- n--;
1295
- }
1296
- }
1297
- return this;
1298
- }
1299
- else {
1300
- return this.zero();
1301
- }
1302
- }
1303
- }
1304
- exports.Polynom = Polynom;
1305
- //# sourceMappingURL=polynom.js.map