nerdamer 1.1.13 → 2.0.0-rc.2

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 (387) hide show
  1. package/BREAKING_CHANGES.md +244 -5
  2. package/LICENSE.md +184 -0
  3. package/README.md +232 -318
  4. package/dist/bundle.js +2 -0
  5. package/dist/bundle.js.LICENSE.txt +7 -0
  6. package/dist/parser.js +2 -0
  7. package/dist/parser.js.LICENSE.txt +7 -0
  8. package/docs-data/parser-functions.json +2248 -0
  9. package/index.d.ts +10 -426
  10. package/output/algebra/adapters.d.ts +3 -0
  11. package/output/algebra/adapters.js +11 -0
  12. package/output/algebra/algorithms/arith.d.ts +84 -0
  13. package/output/algebra/algorithms/arith.js +324 -0
  14. package/output/algebra/algorithms/groebnerBase.d.ts +161 -0
  15. package/output/algebra/algorithms/groebnerBase.js +929 -0
  16. package/output/algebra/dispatch.d.ts +1 -0
  17. package/output/algebra/dispatch.js +25 -0
  18. package/output/algebra/factor/factor.d.ts +77 -0
  19. package/output/algebra/factor/factor.js +245 -0
  20. package/output/algebra/gcd/gcd.d.ts +30 -0
  21. package/output/algebra/gcd/gcd.js +95 -0
  22. package/output/algebra/groebner.d.ts +21 -0
  23. package/output/algebra/groebner.js +36 -0
  24. package/output/algebra/partfrac.d.ts +19 -0
  25. package/output/algebra/partfrac.js +365 -0
  26. package/output/algebra/polynomial/ModularSparsePolynomial.d.ts +131 -0
  27. package/output/algebra/polynomial/ModularSparsePolynomial.js +378 -0
  28. package/output/algebra/polynomial/ModularSparsePolynomialFactor.d.ts +40 -0
  29. package/output/algebra/polynomial/ModularSparsePolynomialFactor.js +661 -0
  30. package/output/algebra/polynomial/SparsePolynomialFactor.d.ts +64 -0
  31. package/output/algebra/polynomial/SparsePolynomialFactor.js +492 -0
  32. package/output/algebra/polynomial/SparsePolynomialGcd.d.ts +29 -0
  33. package/output/algebra/polynomial/SparsePolynomialGcd.js +259 -0
  34. package/output/algebra/polynomial/SparsePolynomialMultivariateFactor.d.ts +82 -0
  35. package/output/algebra/polynomial/SparsePolynomialMultivariateFactor.js +604 -0
  36. package/output/algebra/polynomial/modularGcd.d.ts +40 -0
  37. package/output/algebra/polynomial/modularGcd.js +365 -0
  38. package/output/algebra/polynomialize.d.ts +20 -0
  39. package/output/algebra/polynomialize.js +26 -0
  40. package/output/algebra/simplify/complexsimp.d.ts +7 -0
  41. package/output/algebra/simplify/complexsimp.js +24 -0
  42. package/output/algebra/simplify/factorCommon.d.ts +13 -0
  43. package/output/algebra/simplify/factorCommon.js +136 -0
  44. package/output/algebra/simplify/funcsimp.d.ts +26 -0
  45. package/output/algebra/simplify/funcsimp.js +633 -0
  46. package/output/algebra/simplify/invtrigrewrite.d.ts +10 -0
  47. package/output/algebra/simplify/invtrigrewrite.js +96 -0
  48. package/output/algebra/simplify/ratsimp.d.ts +21 -0
  49. package/output/algebra/simplify/ratsimp.js +140 -0
  50. package/output/algebra/simplify/simplify.d.ts +25 -0
  51. package/output/algebra/simplify/simplify.js +237 -0
  52. package/output/algebra/simplify/trigreduce.d.ts +38 -0
  53. package/output/algebra/simplify/trigreduce.js +424 -0
  54. package/output/algebra/simplify/trigrewrite.d.ts +17 -0
  55. package/output/algebra/simplify/trigrewrite.js +156 -0
  56. package/output/algebra/simplify/trigsimp.d.ts +11 -0
  57. package/output/algebra/simplify/trigsimp.js +369 -0
  58. package/output/algebra/simplify/utils.d.ts +32 -0
  59. package/output/algebra/simplify/utils.js +56 -0
  60. package/output/algebra/utils.d.ts +32 -0
  61. package/output/algebra/utils.js +48 -0
  62. package/output/api/advanced.d.ts +8 -0
  63. package/output/api/advanced.js +18 -0
  64. package/output/api/algebra.d.ts +17 -0
  65. package/output/api/algebra.js +35 -0
  66. package/output/api/assumptions.d.ts +4 -0
  67. package/output/api/assumptions.js +11 -0
  68. package/output/api/calculus.d.ts +10 -0
  69. package/output/api/calculus.js +22 -0
  70. package/output/api/core.d.ts +17 -0
  71. package/output/api/core.js +41 -0
  72. package/output/api/debug.d.ts +16 -0
  73. package/output/api/debug.js +23 -0
  74. package/output/api/languages/deu.d.ts +3 -0
  75. package/output/api/languages/deu.js +266 -0
  76. package/output/api/languages/fra.d.ts +3 -0
  77. package/output/api/languages/fra.js +266 -0
  78. package/output/api/languages/ita.d.ts +3 -0
  79. package/output/api/languages/ita.js +266 -0
  80. package/output/api/languages/nld.d.ts +3 -0
  81. package/output/api/languages/nld.js +266 -0
  82. package/output/api/languages/por.d.ts +3 -0
  83. package/output/api/languages/por.js +266 -0
  84. package/output/api/languages/spa.d.ts +3 -0
  85. package/output/api/languages/spa.js +266 -0
  86. package/output/api/parser.d.ts +93 -0
  87. package/output/api/parser.js +18 -0
  88. package/output/api/solve.d.ts +10 -0
  89. package/output/api/solve.js +17 -0
  90. package/output/api/structures.d.ts +10 -0
  91. package/output/api/structures.js +23 -0
  92. package/output/calculus/adapters.d.ts +8 -0
  93. package/output/calculus/adapters.js +41 -0
  94. package/output/calculus/derivative/diff.d.ts +30 -0
  95. package/output/calculus/derivative/diff.js +241 -0
  96. package/output/calculus/dispatch.d.ts +1 -0
  97. package/output/calculus/dispatch.js +25 -0
  98. package/output/calculus/fresnel.d.ts +16 -0
  99. package/output/calculus/fresnel.js +39 -0
  100. package/output/calculus/integrate/byParts.d.ts +18 -0
  101. package/output/calculus/integrate/byParts.js +339 -0
  102. package/output/calculus/integrate/bySubstitution.d.ts +135 -0
  103. package/output/calculus/integrate/bySubstitution.js +411 -0
  104. package/output/calculus/integrate/integrate.d.ts +38 -0
  105. package/output/calculus/integrate/integrate.js +652 -0
  106. package/output/calculus/integrate/integrationTable.d.ts +2 -0
  107. package/output/calculus/integrate/integrationTable.js +914 -0
  108. package/output/calculus/integrate/utils.d.ts +28 -0
  109. package/output/calculus/integrate/utils.js +96 -0
  110. package/output/calculus/laplace/ilaplace.d.ts +31 -0
  111. package/output/calculus/laplace/ilaplace.js +192 -0
  112. package/output/calculus/laplace/ilaplaceTable.d.ts +2 -0
  113. package/output/calculus/laplace/ilaplaceTable.js +200 -0
  114. package/output/calculus/laplace/laplace.d.ts +22 -0
  115. package/output/calculus/laplace/laplace.js +109 -0
  116. package/output/calculus/laplace/laplaceTable.d.ts +2 -0
  117. package/output/calculus/laplace/laplaceTable.js +187 -0
  118. package/output/calculus/limit/limit.d.ts +35 -0
  119. package/output/calculus/limit/limit.js +1183 -0
  120. package/output/calculus/limit/limitsTable.d.ts +2 -0
  121. package/output/calculus/limit/limitsTable.js +7 -0
  122. package/output/core/Settings.d.ts +32 -0
  123. package/output/core/Settings.js +68 -0
  124. package/output/core/classes/assumption/Assumption.d.ts +218 -0
  125. package/output/core/classes/assumption/Assumption.js +609 -0
  126. package/output/core/classes/assumption/assertiveFunctions.d.ts +6 -0
  127. package/output/core/classes/assumption/assertiveFunctions.js +64 -0
  128. package/output/core/classes/assumption/assume.d.ts +90 -0
  129. package/output/core/classes/assumption/assume.js +145 -0
  130. package/output/core/classes/assumption/dispatch.d.ts +1 -0
  131. package/output/core/classes/assumption/dispatch.js +11 -0
  132. package/output/core/classes/collection/Collection.d.ts +107 -0
  133. package/output/core/classes/collection/Collection.js +240 -0
  134. package/output/core/classes/complex/Complex.d.ts +109 -0
  135. package/output/core/classes/complex/Complex.js +153 -0
  136. package/output/core/classes/decimalSet/DecimalSet.d.ts +226 -0
  137. package/output/core/classes/decimalSet/DecimalSet.js +454 -0
  138. package/output/core/classes/dictionary/Dictionary.d.ts +102 -0
  139. package/output/core/classes/dictionary/Dictionary.js +216 -0
  140. package/output/core/classes/equation/Equation.d.ts +256 -0
  141. package/output/core/classes/equation/Equation.js +350 -0
  142. package/output/core/classes/expression/CoeffObject.d.ts +100 -0
  143. package/output/core/classes/expression/CoeffObject.js +197 -0
  144. package/output/core/classes/expression/Expression.d.ts +1691 -0
  145. package/output/core/classes/expression/Expression.js +2381 -0
  146. package/output/core/classes/expression/analysis.d.ts +25 -0
  147. package/output/core/classes/expression/analysis.js +218 -0
  148. package/output/core/classes/expression/collect.d.ts +9 -0
  149. package/output/core/classes/expression/collect.js +49 -0
  150. package/output/core/classes/expression/format.d.ts +54 -0
  151. package/output/core/classes/expression/format.js +340 -0
  152. package/output/core/classes/expression/products.d.ts +23 -0
  153. package/output/core/classes/expression/products.js +115 -0
  154. package/output/core/classes/expression/shortcuts.d.ts +35 -0
  155. package/output/core/classes/expression/shortcuts.js +129 -0
  156. package/output/core/classes/expression/traversal.d.ts +31 -0
  157. package/output/core/classes/expression/traversal.js +216 -0
  158. package/output/core/classes/expression/trig.d.ts +25 -0
  159. package/output/core/classes/expression/trig.js +40 -0
  160. package/output/core/classes/expression/utils.d.ts +61 -0
  161. package/output/core/classes/expression/utils.js +235 -0
  162. package/output/core/classes/lookupTable/LookupTable.d.ts +12 -0
  163. package/output/core/classes/lookupTable/LookupTable.js +55 -0
  164. package/output/core/classes/matrix/Matrix.d.ts +227 -0
  165. package/output/core/classes/matrix/Matrix.js +901 -0
  166. package/output/core/classes/matrix/Sylvester.d.ts +11 -0
  167. package/output/core/classes/matrix/Sylvester.js +54 -0
  168. package/output/core/classes/matrix/dispatch.d.ts +1 -0
  169. package/output/core/classes/matrix/dispatch.js +19 -0
  170. package/output/core/classes/matrix/functions.d.ts +28 -0
  171. package/output/core/classes/matrix/functions.js +57 -0
  172. package/output/core/classes/matrix/utils.d.ts +9 -0
  173. package/output/core/classes/matrix/utils.js +22 -0
  174. package/output/core/classes/parser/Parser.d.ts +493 -0
  175. package/output/core/classes/parser/Parser.js +1863 -0
  176. package/output/core/classes/parser/Token.d.ts +58 -0
  177. package/output/core/classes/parser/Token.js +137 -0
  178. package/output/core/classes/parser/constants.d.ts +162 -0
  179. package/output/core/classes/parser/constants.js +229 -0
  180. package/output/core/classes/parser/controlFlowSignals.d.ts +38 -0
  181. package/output/core/classes/parser/controlFlowSignals.js +60 -0
  182. package/output/core/classes/parser/helpers.d.ts +21 -0
  183. package/output/core/classes/parser/helpers.js +42 -0
  184. package/output/core/classes/parser/operations/add.d.ts +15 -0
  185. package/output/core/classes/parser/operations/add.js +236 -0
  186. package/output/core/classes/parser/operations/comma.d.ts +7 -0
  187. package/output/core/classes/parser/operations/comma.js +11 -0
  188. package/output/core/classes/parser/operations/compare.d.ts +21 -0
  189. package/output/core/classes/parser/operations/compare.js +474 -0
  190. package/output/core/classes/parser/operations/divide.d.ts +2 -0
  191. package/output/core/classes/parser/operations/divide.js +72 -0
  192. package/output/core/classes/parser/operations/functions.d.ts +46 -0
  193. package/output/core/classes/parser/operations/functions.js +438 -0
  194. package/output/core/classes/parser/operations/multiply.d.ts +11 -0
  195. package/output/core/classes/parser/operations/multiply.js +328 -0
  196. package/output/core/classes/parser/operations/power.d.ts +48 -0
  197. package/output/core/classes/parser/operations/power.js +673 -0
  198. package/output/core/classes/parser/operations/subtract.d.ts +3 -0
  199. package/output/core/classes/parser/operations/subtract.js +36 -0
  200. package/output/core/classes/parser/preprocess.d.ts +7 -0
  201. package/output/core/classes/parser/preprocess.js +246 -0
  202. package/output/core/classes/parser/scripting/controlFlow.d.ts +23 -0
  203. package/output/core/classes/parser/scripting/controlFlow.js +279 -0
  204. package/output/core/classes/parser/scripting/deferred.d.ts +4 -0
  205. package/output/core/classes/parser/scripting/deferred.js +65 -0
  206. package/output/core/classes/parser/scripting/dispatch.d.ts +1 -0
  207. package/output/core/classes/parser/scripting/dispatch.js +62 -0
  208. package/output/core/classes/parser/scripting/evaluate.d.ts +23 -0
  209. package/output/core/classes/parser/scripting/evaluate.js +128 -0
  210. package/output/core/classes/parser/scripting/functions.d.ts +14 -0
  211. package/output/core/classes/parser/scripting/functions.js +59 -0
  212. package/output/core/classes/parser/scripting/scope.d.ts +8 -0
  213. package/output/core/classes/parser/scripting/scope.js +132 -0
  214. package/output/core/classes/parser/types.d.ts +130 -0
  215. package/output/core/classes/parser/types.js +2 -0
  216. package/output/core/classes/parser/wrappers/IndexedReference.d.ts +31 -0
  217. package/output/core/classes/parser/wrappers/IndexedReference.js +68 -0
  218. package/output/core/classes/parser/wrappers/KeyValuePair.d.ts +24 -0
  219. package/output/core/classes/parser/wrappers/KeyValuePair.js +51 -0
  220. package/output/core/classes/polynomial/Polynomial.d.ts +504 -0
  221. package/output/core/classes/polynomial/Polynomial.js +1195 -0
  222. package/output/core/classes/polynomial/SparsePolynomial.d.ts +221 -0
  223. package/output/core/classes/polynomial/SparsePolynomial.js +824 -0
  224. package/output/core/classes/polynomial/SparsePolynomialAdapter.d.ts +56 -0
  225. package/output/core/classes/polynomial/SparsePolynomialAdapter.js +241 -0
  226. package/output/core/classes/polynomial/Term.d.ts +222 -0
  227. package/output/core/classes/polynomial/Term.js +430 -0
  228. package/output/core/classes/polynomial/adapters.d.ts +18 -0
  229. package/output/core/classes/polynomial/adapters.js +33 -0
  230. package/output/core/classes/polynomial/dispatch.d.ts +1 -0
  231. package/output/core/classes/polynomial/dispatch.js +14 -0
  232. package/output/core/classes/polynomial/functions.d.ts +77 -0
  233. package/output/core/classes/polynomial/functions.js +189 -0
  234. package/output/core/classes/polynomial/utils.d.ts +62 -0
  235. package/output/core/classes/polynomial/utils.js +207 -0
  236. package/output/core/classes/rational/Rational.d.ts +501 -0
  237. package/output/core/classes/rational/Rational.js +829 -0
  238. package/output/core/classes/seq/SEQ.d.ts +13 -0
  239. package/output/core/classes/seq/SEQ.js +137 -0
  240. package/output/core/classes/valuesSet/ValuesSet.d.ts +187 -0
  241. package/output/core/classes/valuesSet/ValuesSet.js +399 -0
  242. package/output/core/classes/vector/Vector.d.ts +222 -0
  243. package/output/core/classes/vector/Vector.js +501 -0
  244. package/output/core/classes/vector/dispatch.d.ts +1 -0
  245. package/output/core/classes/vector/dispatch.js +11 -0
  246. package/output/core/classes/vector/functions.d.ts +28 -0
  247. package/output/core/classes/vector/functions.js +47 -0
  248. package/output/core/common/classes/MathematicalAggregate.d.ts +53 -0
  249. package/output/core/common/classes/MathematicalAggregate.js +149 -0
  250. package/output/core/common/classes/Scope.d.ts +62 -0
  251. package/output/core/common/classes/Scope.js +122 -0
  252. package/output/core/common/classes/StructuredEntity.d.ts +55 -0
  253. package/output/core/common/classes/StructuredEntity.js +151 -0
  254. package/output/core/common/common.d.ts +62 -0
  255. package/output/core/common/common.js +74 -0
  256. package/output/core/common/functions/functions.d.ts +14 -0
  257. package/output/core/common/functions/functions.js +33 -0
  258. package/output/core/common/functions/structuredEntityUtils.d.ts +27 -0
  259. package/output/core/common/functions/structuredEntityUtils.js +40 -0
  260. package/output/core/converters/BaseConverter.d.ts +97 -0
  261. package/output/core/converters/BaseConverter.js +405 -0
  262. package/output/core/converters/Converter.d.ts +111 -0
  263. package/output/core/converters/Converter.js +806 -0
  264. package/output/core/converters/Pattern.d.ts +71 -0
  265. package/output/core/converters/Pattern.js +302 -0
  266. package/output/core/dispatch.d.ts +52 -0
  267. package/output/core/dispatch.js +26 -0
  268. package/output/core/errors.d.ts +316 -0
  269. package/output/core/errors.js +349 -0
  270. package/output/core/fullFunctions.d.ts +7 -0
  271. package/output/core/fullFunctions.js +31 -0
  272. package/output/core/functions/bigint/bigint.d.ts +133 -0
  273. package/output/core/functions/bigint/bigint.js +444 -0
  274. package/output/core/functions/bigint/primeFactor.d.ts +51 -0
  275. package/output/core/functions/bigint/primeFactor.js +267 -0
  276. package/output/core/functions/bigint/primes.d.ts +1 -0
  277. package/output/core/functions/bigint/primes.js +11 -0
  278. package/output/core/functions/build/definitions.d.ts +12 -0
  279. package/output/core/functions/build/definitions.js +139 -0
  280. package/output/core/functions/build/index.d.ts +32 -0
  281. package/output/core/functions/build/index.js +136 -0
  282. package/output/core/functions/complex.d.ts +90 -0
  283. package/output/core/functions/complex.dispatch.d.ts +1 -0
  284. package/output/core/functions/complex.dispatch.js +16 -0
  285. package/output/core/functions/complex.js +482 -0
  286. package/output/core/functions/decimal.d.ts +24 -0
  287. package/output/core/functions/decimal.js +665 -0
  288. package/output/core/functions/expand/expand.d.ts +42 -0
  289. package/output/core/functions/expand/expand.js +376 -0
  290. package/output/core/functions/fresnelNumeric.d.ts +12 -0
  291. package/output/core/functions/fresnelNumeric.js +123 -0
  292. package/output/core/functions/numeric.d.ts +140 -0
  293. package/output/core/functions/numeric.js +790 -0
  294. package/output/core/functions/rationalNormalization.d.ts +11 -0
  295. package/output/core/functions/rationalNormalization.js +117 -0
  296. package/output/core/functions/setFunction.d.ts +19 -0
  297. package/output/core/functions/setFunction.js +75 -0
  298. package/output/core/functions/string.d.ts +26 -0
  299. package/output/core/functions/string.js +102 -0
  300. package/output/core/functions/subst.d.ts +90 -0
  301. package/output/core/functions/subst.js +506 -0
  302. package/output/core/functions/utils.d.ts +24 -0
  303. package/output/core/functions/utils.js +51 -0
  304. package/output/core/parserFunctions.d.ts +7 -0
  305. package/output/core/parserFunctions.js +25 -0
  306. package/output/core/types.d.ts +48 -0
  307. package/output/core/types.js +2 -0
  308. package/output/index.d.ts +359 -0
  309. package/output/index.js +739 -0
  310. package/output/math/defint/defint.d.ts +18 -0
  311. package/output/math/defint/defint.js +60 -0
  312. package/output/math/defint/defintDecimal.d.ts +37 -0
  313. package/output/math/defint/defintDecimal.js +321 -0
  314. package/output/math/defint/defintNative.d.ts +57 -0
  315. package/output/math/defint/defintNative.js +281 -0
  316. package/output/math/dispatch.d.ts +1 -0
  317. package/output/math/dispatch.js +101 -0
  318. package/output/math/geometry.d.ts +12 -0
  319. package/output/math/geometry.js +55 -0
  320. package/output/math/math.d.ts +695 -0
  321. package/output/math/math.js +1955 -0
  322. package/output/math/trig.d.ts +518 -0
  323. package/output/math/trig.js +1444 -0
  324. package/output/math/trunc.d.ts +19 -0
  325. package/output/math/trunc.js +36 -0
  326. package/output/math/utils.d.ts +66 -0
  327. package/output/math/utils.js +219 -0
  328. package/output/solve/classes/DecimalMatrix.d.ts +17 -0
  329. package/output/solve/classes/DecimalMatrix.js +86 -0
  330. package/output/solve/classes/FunctionSolver.d.ts +135 -0
  331. package/output/solve/classes/FunctionSolver.js +433 -0
  332. package/output/solve/classes/MultivariateSolver.d.ts +84 -0
  333. package/output/solve/classes/MultivariateSolver.js +240 -0
  334. package/output/solve/classes/PolynomialSolver.d.ts +104 -0
  335. package/output/solve/classes/PolynomialSolver.js +492 -0
  336. package/output/solve/classes/SolutionSet.d.ts +172 -0
  337. package/output/solve/classes/SolutionSet.js +434 -0
  338. package/output/solve/classes/Solver.d.ts +17 -0
  339. package/output/solve/classes/Solver.js +132 -0
  340. package/output/solve/classes/SymbolicSolver.d.ts +74 -0
  341. package/output/solve/classes/SymbolicSolver.js +601 -0
  342. package/output/solve/dispatch.d.ts +1 -0
  343. package/output/solve/dispatch.js +13 -0
  344. package/output/solve/linsolve.d.ts +53 -0
  345. package/output/solve/linsolve.js +233 -0
  346. package/output/solve/solve.d.ts +44 -0
  347. package/output/solve/solve.js +368 -0
  348. package/output/solve/solveSystem.d.ts +31 -0
  349. package/output/solve/solveSystem.js +266 -0
  350. package/output/solve/utils.d.ts +10 -0
  351. package/output/solve/utils.js +58 -0
  352. package/output/utils/array.d.ts +42 -0
  353. package/output/utils/array.js +98 -0
  354. package/output/utils/debug.d.ts +131 -0
  355. package/output/utils/debug.js +296 -0
  356. package/output/utils/decimal.d.ts +3 -0
  357. package/output/utils/decimal.js +16 -0
  358. package/output/utils/numeric.d.ts +14 -0
  359. package/output/utils/numeric.js +51 -0
  360. package/output/utils/object.d.ts +26 -0
  361. package/output/utils/object.js +56 -0
  362. package/package.json +178 -57
  363. package/.travis.yml +0 -3
  364. package/Algebra.js +0 -4568
  365. package/CODE_OF_CONDUCT.md +0 -46
  366. package/CONTRIBUTING.md +0 -14
  367. package/Calculus.js +0 -2675
  368. package/Extra.js +0 -622
  369. package/Solve.js +0 -1783
  370. package/all.js +0 -16
  371. package/all.min.js +0 -1
  372. package/gulpfile.js +0 -16
  373. package/index.html +0 -158
  374. package/license.txt +0 -19
  375. package/nerdamer.core.js +0 -12510
  376. package/spec/LaTeX.spec.js +0 -302
  377. package/spec/TeXConvert.spec.js +0 -8
  378. package/spec/algebra.spec.js +0 -287
  379. package/spec/basic_parser.spec.js +0 -134
  380. package/spec/build.spec.js +0 -131
  381. package/spec/calculus.spec.js +0 -183
  382. package/spec/core.spec.js +0 -2970
  383. package/spec/extra.spec.js +0 -54
  384. package/spec/solve.spec.js +0 -126
  385. package/spec/support/jasmine.json +0 -11
  386. package/spec/support/utils.js +0 -42
  387. package/spec/text.spec.js +0 -132
@@ -0,0 +1,1195 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Polynomial = void 0;
7
+ const decimal_js_1 = __importDefault(require("decimal.js"));
8
+ const array_1 = require("../../../utils/array");
9
+ const object_1 = require("../../../utils/object");
10
+ const common_1 = require("../../common/common");
11
+ const errors_1 = require("../../errors");
12
+ const expand_1 = require("../../functions/expand/expand");
13
+ const utils_1 = require("../../functions/utils");
14
+ const analysis_1 = require("../expression/analysis");
15
+ const Expression_1 = require("../expression/Expression");
16
+ const shortcuts_1 = require("../expression/shortcuts");
17
+ const constants_1 = require("../parser/constants");
18
+ const Rational_1 = require("../rational/Rational");
19
+ const SparsePolynomialAdapter_1 = require("./SparsePolynomialAdapter");
20
+ const Term_1 = require("./Term");
21
+ const utils_2 = require("./utils");
22
+ /**
23
+ * Represents a polynomial as ordered {@link Term} objects with symbolic coefficients.
24
+ *
25
+ * @remarks
26
+ * Construction accepts polynomial-like expression input, expands bracketed or powered
27
+ * sums when necessary, and collects coefficients using either the supplied variable
28
+ * order or the expression's alphabetically sorted variables. Negative variable powers
29
+ * and other non-polynomial forms are rejected.
30
+ *
31
+ * The object is mutable: ordering methods sort {@link Polynomial.terms} in place, and
32
+ * `gcdFree(..., true)` edits term coefficients and powers. Arithmetic methods such as
33
+ * {@link Polynomial.plus}, {@link Polynomial.minus}, and {@link Polynomial.times}
34
+ * normally return new polynomials. Public arrays and terms expose mutable internal
35
+ * references; callers that edit them directly must preserve ordering and keep cached
36
+ * term data consistent.
37
+ *
38
+ * Coefficients are stored as `Expression` values. Operations such as
39
+ * {@link Polynomial.evaluate} convert supplied values to {@link Rational} and use exact
40
+ * rational arithmetic; {@link Polynomial.at} and numeric array conversions instead use
41
+ * JavaScript numbers and may lose precision.
42
+ *
43
+ * Supported orderings include lexicographic, reverse lexicographic, graded
44
+ * lexicographic, and graded reverse lexicographic order. Univariate polynomials always
45
+ * use descending degree order regardless of the requested multivariate ordering.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const polynomial = new Polynomial('x^2 + 2*x + 1', ['x']);
50
+ *
51
+ * polynomial.deg(); // 2
52
+ * polynomial.toArray().map(String); // ["1", "2", "1"]
53
+ * polynomial.evaluateToRational({ x: 2 }).toString(); // "5"
54
+ * ```
55
+ */
56
+ class Polynomial {
57
+ /** Default ordering chosen for newly parsed multivariate polynomials. */
58
+ static defaultOrdering = 'lex';
59
+ /** Runtime tag used by Nerdamer's polynomial type guard. */
60
+ dataType = constants_1.POLYNOMIAL;
61
+ /** Expression retained from construction or the most recent explicit rebuild. */
62
+ expression;
63
+ /** Whether the polynomial's variable list contains more than one variable. */
64
+ isMultivariate;
65
+ /** Current monomial ordering of {@link terms}. */
66
+ ordering = 'none';
67
+ /** Terms in their current monomial order. The array is mutable. */
68
+ terms;
69
+ /** Variable order used to interpret term multidegrees. The array is mutable. */
70
+ variables;
71
+ /**
72
+ * Constructs a polynomial from expression input or deep-copies another polynomial.
73
+ *
74
+ * @param p - Polynomial-like input or an existing polynomial to copy.
75
+ * @param vars - Variable names in the order used for multidegrees and monomial comparison.
76
+ * When omitted, variables are collected and sorted alphabetically.
77
+ * @param ordering - Requested monomial ordering for parsed multivariate input.
78
+ * @throws {@link core!PolynomialError} Thrown when the parsed expression is not polynomial-like.
79
+ * @throws {@link core!UnsupportedOperationError} Thrown when input cannot be converted to
80
+ * an expression supported by the polynomial representation.
81
+ */
82
+ constructor(p, vars, ordering) {
83
+ if (Polynomial.isPolynomial(p)) {
84
+ this.expression = p.expression.copy();
85
+ this.variables = [...p.variables];
86
+ this.ordering = p.ordering;
87
+ this.isMultivariate = p.isMultivariate;
88
+ this.terms = p.terms.map(t => t.copy());
89
+ }
90
+ else {
91
+ // Set the default ordering if none was provided
92
+ ordering ??= Polynomial.defaultOrdering;
93
+ // Parse it to a Expression
94
+ let x = Expression_1.Expression.create(p);
95
+ // Disqualify all other types except for Expression. This needs to be updated if other types
96
+ // are supported in the future.
97
+ if (!Expression_1.Expression.isExpression(x)) {
98
+ throw new errors_1.UnsupportedOperationError((0, errors_1.message)('unsupportedType', { type: typeof x }));
99
+ }
100
+ if (!x.isPolynomialLike()) {
101
+ throw new errors_1.PolynomialError((0, errors_1.message)('notAPolynomial'));
102
+ }
103
+ // Expand the expression but only if needed. This will be needed if the value has a bracket
104
+ // or if the expression has a power greater than one.
105
+ let expandedForCoefficients = false;
106
+ if (x.value.includes('(') || (x.isSum() && x.getPower().gt('1'))) {
107
+ x = (0, expand_1.expand)(x);
108
+ expandedForCoefficients = true;
109
+ }
110
+ this.expression = x;
111
+ let variables;
112
+ if (!vars) {
113
+ variables = x.variables();
114
+ if (!(0, utils_1.isSorted)(variables)) {
115
+ variables.sort();
116
+ }
117
+ }
118
+ else {
119
+ variables = vars;
120
+ }
121
+ this.variables = variables;
122
+ // Mark it as univariate or multivariate
123
+ this.isMultivariate = this.variables.length > 1;
124
+ // Add the elements to the terms array
125
+ this.terms = [];
126
+ // Calculate the coefficients. Coeffs will come back as a collection of objects so for instance
127
+ // a*x*y + b*x^2*y will come back as [a, '1,1'] & [b, '2,1']. It's preferred to have it in object form
128
+ // TODO: this should probably be done with `coeffs`.
129
+ (0, analysis_1.coeffs)(x, this.variables, undefined, !expandedForCoefficients).each((coeff, powers) => {
130
+ this.terms.push(new Term_1.Term(coeff, powers, this.variables));
131
+ });
132
+ // Set a blank array if none was provided or calculated
133
+ this.variables = this.variables || [];
134
+ // Order it
135
+ this.order(ordering);
136
+ }
137
+ }
138
+ /**
139
+ * Constructs a polynomial from dense ascending-power coefficients.
140
+ *
141
+ * @remarks
142
+ * Entry `arr[i]` becomes the coefficient of power `i`. When multiple variable names
143
+ * are supplied, their product is treated as one repeated base; this is not a general
144
+ * multidimensional coefficient tensor.
145
+ *
146
+ * @param arr - Coefficients ordered from constant term upward.
147
+ * @param vars - Variable names forming the polynomial base.
148
+ */
149
+ static fromArray(arr, vars) {
150
+ const coefficients = [];
151
+ let requiresCanonicalization = false;
152
+ for (const input of arr) {
153
+ const coefficient = Expression_1.Expression.isExpression(input)
154
+ ? input
155
+ : Expression_1.Expression.create(String(input));
156
+ coefficients.push(coefficient);
157
+ if (vars.some(variable => coefficient.hasVariable(variable))) {
158
+ requiresCanonicalization = true;
159
+ }
160
+ }
161
+ let retval;
162
+ if (requiresCanonicalization) {
163
+ retval = Polynomial.fromArrayCanonicalized(coefficients, vars);
164
+ }
165
+ else {
166
+ retval = new Polynomial((0, shortcuts_1.zero)(), vars);
167
+ retval.terms = [];
168
+ for (let i = 0; i < coefficients.length; i++) {
169
+ const coefficient = coefficients[i];
170
+ if (!coefficient.isZero()) {
171
+ const powers = {};
172
+ for (const variable of vars) {
173
+ powers[variable] = i;
174
+ }
175
+ retval.terms.push(new Term_1.Term(coefficient.copy(), powers, vars));
176
+ }
177
+ }
178
+ retval.order();
179
+ retval.updateExpression();
180
+ }
181
+ return retval;
182
+ }
183
+ /** Reconstructs dense coefficients through the normal constructor when powers overlap coefficients. */
184
+ static fromArrayCanonicalized(coefficients, vars) {
185
+ let base = (0, shortcuts_1.one)();
186
+ for (const variable of vars) {
187
+ base = base.times(Expression_1.Expression.Variable(variable));
188
+ }
189
+ let expression = (0, shortcuts_1.zero)();
190
+ for (let i = 0; i < coefficients.length; i++) {
191
+ expression = expression.plus(coefficients[i].times(base.pow(i)));
192
+ }
193
+ const retval = new Polynomial(expression, vars);
194
+ return retval;
195
+ }
196
+ /**
197
+ * Sorts by graded lexicographic order. The abs(power) is first compared and then
198
+ * revlex is used to break ties.
199
+ *
200
+ * @param a - First term to compare.
201
+ * @param b - Second term to compare.
202
+ * @returns A comparator value suitable for `Array.sort`.
203
+ */
204
+ static grevlex(a, b) {
205
+ if (a.getTotalPower() === b.getTotalPower()) {
206
+ return Polynomial.revlex(a, b);
207
+ }
208
+ return a.getTotalPower() > b.getTotalPower() ? -1 : 1;
209
+ }
210
+ /**
211
+ * Sorts by graded lexicographic order. The abs(power) is first compared and then
212
+ * lex is used to break ties.
213
+ *
214
+ * @param a - First term to compare.
215
+ * @param b - Second term to compare.
216
+ * @returns A comparator value suitable for `Array.sort`.
217
+ */
218
+ static grlex(a, b) {
219
+ if (a.getTotalPower() === b.getTotalPower()) {
220
+ return Polynomial.lex(a, b);
221
+ }
222
+ return a.getTotalPower() > b.getTotalPower() ? -1 : 1;
223
+ }
224
+ /**
225
+ * Checks if the given object is a Polynomial
226
+ *
227
+ * @param obj - Value to test.
228
+ * @returns Whether `obj` carries Nerdamer's polynomial discriminator.
229
+ */
230
+ static isPolynomial(obj) {
231
+ return (0, common_1.isNerdamerNativeType)(obj, constants_1.POLYNOMIAL);
232
+ }
233
+ /**
234
+ * Sorts by lexicographic order. The power tuples are subtracted and the first
235
+ * non-negative value from the left is used to sort. Note that variables are
236
+ * first sorted in alphabetical order.
237
+ *
238
+ * @param a - First term to compare.
239
+ * @param b - Second term to compare.
240
+ * @returns A comparator value suitable for `Array.sort`.
241
+ */
242
+ static lex(a, b) {
243
+ const difference = a.difference(b);
244
+ for (let i = 0; i < difference.length; i++) {
245
+ if (difference[i] !== 0) {
246
+ return Math.sign(difference[i]) === -1 ? 1 : -1;
247
+ }
248
+ }
249
+ return 1;
250
+ }
251
+ /**
252
+ * Sorts an array given a specific ordering using their LT
253
+ *
254
+ * @param polyArray - Polynomials to reorder. The array itself is sorted in place.
255
+ * @param ordering - Monomial ordering used for each polynomial and its leading term.
256
+ * @returns The same sorted array after each polynomial has also been reordered.
257
+ */
258
+ static polyArraySort(polyArray, ordering) {
259
+ ordering ??= Polynomial.defaultOrdering;
260
+ // First put all the polynomials in the correct ordering
261
+ polyArray = polyArray.map(x => {
262
+ return x.order(ordering);
263
+ });
264
+ let sortFunction;
265
+ switch (ordering) {
266
+ case 'lex':
267
+ sortFunction = Polynomial.lex;
268
+ break;
269
+ case 'grevlex':
270
+ sortFunction = Polynomial.grevlex;
271
+ break;
272
+ case 'revlex':
273
+ sortFunction = Polynomial.revlex;
274
+ break;
275
+ case 'grlex':
276
+ default:
277
+ sortFunction = Polynomial.grlex;
278
+ break;
279
+ }
280
+ polyArray = polyArray.sort((a, b) => {
281
+ return sortFunction(a.LT(), b.LT());
282
+ });
283
+ return polyArray;
284
+ }
285
+ /**
286
+ * Converts a Rational to a string suitable for polynomial construction.
287
+ * Handles both integers and fractions.
288
+ *
289
+ * @param r - Rational value to convert.
290
+ * @returns Parser text preserving an integer or fraction exactly.
291
+ */
292
+ static rationalToString(r) {
293
+ if (r.denominator === 1n) {
294
+ return r.numerator.toString();
295
+ }
296
+ return `(${r.numerator}/${r.denominator})`;
297
+ }
298
+ /**
299
+ * Sorts by reverse lexicographic order. The power tuples are subtracted and the first
300
+ * non-negative value from the right is used to sort
301
+ *
302
+ * @param a - First term to compare.
303
+ * @param b - Second term to compare.
304
+ * @returns A comparator value suitable for `Array.sort`.
305
+ */
306
+ static revlex(a, b) {
307
+ const difference = a.difference(b);
308
+ for (let i = difference.length - 1; i >= 0; i--) {
309
+ if (difference[i] !== 0) {
310
+ return Math.sign(difference[i]);
311
+ }
312
+ }
313
+ return -1;
314
+ }
315
+ /**
316
+ * Fetches the expression from the given object.
317
+ *
318
+ * @param p - Polynomial, expression, or parser text to convert.
319
+ * @returns The polynomial's stored expression reference, or the parsed expression for
320
+ * non-polynomial input.
321
+ */
322
+ static toExpression(p) {
323
+ if (!Polynomial.isPolynomial(p)) {
324
+ return Expression_1.Expression.create(p);
325
+ }
326
+ return p.getExpression();
327
+ }
328
+ /**
329
+ * Converts a string to a Polynomial. If a polynomial is provided, it's returned untouched.
330
+ * If a polynomial is provided and no ordering, then the polynomial's ordering will be used.
331
+ * If no ordering is provided for all others then the Polynomial.defaultOrdering will be used.
332
+ *
333
+ * @param p - Value to convert.
334
+ * @param ordering - Ordering to apply. Passing an existing polynomial may reorder it in place.
335
+ * @param variables - Variable order used only when constructing a new polynomial.
336
+ * @returns The existing polynomial or a newly constructed one.
337
+ */
338
+ static toPolynomial(p, ordering, variables) {
339
+ if (!Polynomial.isPolynomial(p)) {
340
+ p = new Polynomial(p, variables, ordering || Polynomial.defaultOrdering);
341
+ }
342
+ else if (p.ordering !== ordering) {
343
+ p.order(ordering);
344
+ }
345
+ return p;
346
+ }
347
+ /**
348
+ * Creates a copy and adds or subtracts every supplied term.
349
+ *
350
+ * @param x - Term or polynomial whose terms should be combined.
351
+ * @param action - Whether matching coefficients are added or subtracted.
352
+ * @returns A new combined polynomial.
353
+ */
354
+ append(x, action) {
355
+ const retval = new Polynomial(this);
356
+ retval.variables = (0, utils_1.arrayAddUnique)(x.variables, retval.variables);
357
+ // Sort if variables were added
358
+ if (retval.variables && x.variables && retval.variables.length > x.variables.length) {
359
+ retval.variables.sort();
360
+ }
361
+ retval.isMultivariate = retval.variables.length > 1;
362
+ if (Polynomial.isPolynomial(x)) {
363
+ for (const ta of x.terms) {
364
+ retval.appendTerm(ta, action);
365
+ }
366
+ }
367
+ else {
368
+ retval.appendTerm(x, action);
369
+ }
370
+ if (retval.terms.length !== 0) {
371
+ retval.updateExpression();
372
+ }
373
+ return retval.terms.length === 0 ? new Polynomial('0') : retval;
374
+ }
375
+ /**
376
+ * Adds or removes a term from the terms array of the polynomial
377
+ *
378
+ * @param t - Term to combine or append.
379
+ * @param action - Coefficient operation for a matching term.
380
+ */
381
+ appendTerm(t, action) {
382
+ // Try to add it to an existing
383
+ for (let i = 0; i < this.terms.length; i++) {
384
+ const e = this.terms[i];
385
+ if (e.canAddOrSubtract(t)) {
386
+ e.coeff = e.coeff[action](t.coeff);
387
+ if (e.coeff.isZero()) {
388
+ (0, array_1.remove)(this.terms, e);
389
+ }
390
+ return;
391
+ }
392
+ }
393
+ const c = t.copy();
394
+ if (action === 'minus') {
395
+ c.coeff = c.coeff.neg();
396
+ }
397
+ // No existing was found so add it to the terms
398
+ this.terms.push(c);
399
+ // Update since the ordering may have changes
400
+ this.order();
401
+ }
402
+ monomialGCDTerm(other) {
403
+ const vars = [...new Set([...this.variables, ...other.variables])].sort();
404
+ const a = this.commonTermVariables();
405
+ const b = other.commonTermVariables();
406
+ const powers = {};
407
+ for (const v of vars) {
408
+ const ea = a[v] ?? 0;
409
+ const eb = b[v] ?? 0;
410
+ const e = Math.min(ea, eb);
411
+ if (e > 0) {
412
+ powers[v] = e;
413
+ }
414
+ }
415
+ return new Term_1.Term('1', powers, vars);
416
+ }
417
+ updateExpression() {
418
+ let retval = (0, shortcuts_1.zero)();
419
+ for (const term of this.terms) {
420
+ // Rebuild from the current term fields rather than trusting a potentially stale cache.
421
+ retval = retval.plus(term.copy().getExpression());
422
+ }
423
+ this.expression = retval;
424
+ }
425
+ /**
426
+ * Numerically evaluates the univariate polynomial at a given point
427
+ * @param n - JavaScript number at which to evaluate the sole variable.
428
+ * @returns A JavaScript-number approximation.
429
+ * @throws {@link core!UnsupportedOperationError} Thrown for multivariate polynomials.
430
+ */
431
+ at(n) {
432
+ if (this.variables.length > 1) {
433
+ throw new errors_1.UnsupportedOperationError((0, errors_1.message)('univariatePolynomialOnly'));
434
+ }
435
+ let sum = 0;
436
+ const v = this.variables[0];
437
+ for (let i = 0; i < this.terms.length; i++) {
438
+ const term = this.terms[i];
439
+ const pow = term.powers[v];
440
+ sum += Math.pow(n, pow) * Number(term.coeff);
441
+ }
442
+ return sum;
443
+ }
444
+ /**
445
+ * Collects coefficients by power or multidegree.
446
+ *
447
+ * @param variable - Optional single main variable. When omitted, keys use all
448
+ * polynomial variables in their stored order.
449
+ * @returns A mutable coefficient object whose expressions are derived from this polynomial.
450
+ */
451
+ coeffs(variable) {
452
+ const c = (0, analysis_1.coeffs)(this.getExpression(), variable ? [variable] : this.variables);
453
+ return c;
454
+ }
455
+ /**
456
+ * Returns the positive variable powers shared by every term.
457
+ *
458
+ * @returns A new variable-to-minimum-power record; variables absent from any term are omitted.
459
+ */
460
+ commonTermVariables() {
461
+ const common = {};
462
+ // Collect common variables
463
+ for (const v of this.variables) {
464
+ let min;
465
+ for (const t of this.terms) {
466
+ const deg = t.deg(v);
467
+ if (deg === 0) {
468
+ // Remove the min
469
+ min = undefined;
470
+ break;
471
+ }
472
+ // Set the min common degree
473
+ min = min === undefined ? deg : Math.min(min, deg);
474
+ }
475
+ // Mark it
476
+ if (min) {
477
+ common[v] = min;
478
+ }
479
+ }
480
+ return common;
481
+ }
482
+ /**
483
+ * Returns the coefficient of the trailing constant term.
484
+ *
485
+ * @returns The stored coefficient reference when a constant term is present, otherwise
486
+ * a new zero expression.
487
+ */
488
+ constantTerm() {
489
+ const t = this.terms.at(-1);
490
+ if (t && t.isConstant()) {
491
+ return t.coeff;
492
+ }
493
+ return (0, shortcuts_1.zero)();
494
+ }
495
+ /**
496
+ * Returns the content of the polynomial (GCD of all coefficients).
497
+ * Does not rely on the Expression class.
498
+ *
499
+ * @returns A new exact rational greatest common divisor of the numeric coefficients.
500
+ */
501
+ content() {
502
+ const coefficients = this.numericCoeffs();
503
+ if (coefficients.length === 0) {
504
+ return Rational_1.Rational.create('1');
505
+ }
506
+ return Rational_1.Rational.GCD(...coefficients);
507
+ }
508
+ /**
509
+ * Returns the degree of the polynomial.
510
+ *
511
+ * With no variable, this is the total degree: the largest sum of powers in any term.
512
+ * When a variable is supplied, this is the largest power of that variable across all
513
+ * terms, with every other variable treated as part of the coefficient.
514
+ *
515
+ * @param variable - Optional variable whose degree should be returned.
516
+ * @returns The total degree, or the degree with respect to `variable`.
517
+ */
518
+ deg(variable) {
519
+ let retval = 0;
520
+ for (const term of this.terms) {
521
+ const degree = variable === undefined ? term.getTotalPower() : term.deg(variable);
522
+ retval = Math.max(retval, degree);
523
+ }
524
+ return retval;
525
+ }
526
+ /**
527
+ * Performs a derivative with respect to a variable using Term-based operations.
528
+ * Does not rely on the Expression class for differentiation.
529
+ *
530
+ * @param v - Variable to differentiate; defaults to the first stored variable.
531
+ * @param n - Non-negative derivative order.
532
+ * @returns A newly constructed polynomial; order zero returns a deep copy.
533
+ */
534
+ diff(v, n = 1) {
535
+ v = v || this.variables[0];
536
+ // Zero-th derivative is just a copy
537
+ if (n === 0) {
538
+ return new Polynomial(this);
539
+ }
540
+ // If no variable specified and polynomial has no variables, derivative is zero
541
+ if (!v) {
542
+ return new Polynomial('0');
543
+ }
544
+ // Differentiate each term and collect non-zero results
545
+ const diffTerms = [];
546
+ for (const term of this.terms) {
547
+ const diffTerm = term.diff(v, n);
548
+ if (!diffTerm.isZero()) {
549
+ // Ensure the differentiated term has proper variables
550
+ diffTerm.variables = [...this.variables];
551
+ diffTerms.push(diffTerm);
552
+ }
553
+ }
554
+ // If all terms differentiated to zero, return zero polynomial
555
+ if (diffTerms.length === 0) {
556
+ return new Polynomial('0');
557
+ }
558
+ // Build polynomial from differentiated terms
559
+ // Start with first term and add the rest
560
+ const result = new Polynomial('0', this.variables, this.ordering);
561
+ result.terms = [];
562
+ for (const t of diffTerms) {
563
+ result.terms.push(t);
564
+ }
565
+ // Ensure proper ordering and synchronize the stored expression.
566
+ result.order(this.ordering);
567
+ result.updateExpression();
568
+ return result;
569
+ }
570
+ /**
571
+ * Divides this polynomial by another polynomial.
572
+ *
573
+ * @param p - Divisor polynomial.
574
+ * @returns A two-element array containing quotient and remainder as new polynomials.
575
+ */
576
+ div(p) {
577
+ return (0, utils_2.divide)(this, p).map(x => new Polynomial(x));
578
+ }
579
+ /**
580
+ * Checks to see if a polynomial divides the given polynomial
581
+ *
582
+ * @param p - Polynomial whose leading term is tested as the dividend.
583
+ * @returns Whether this polynomial's leading term divides `p`'s leading term. This is
584
+ * a monomial divisibility test, not proof that the complete polynomial divides `p`.
585
+ */
586
+ divides(p) {
587
+ return this.LT().divides(p.LT());
588
+ }
589
+ /**
590
+ * Checks if two polynomials are equal
591
+ *
592
+ * @param p - Polynomial to compare.
593
+ * @returns Whether subtracting `p` produces the zero polynomial.
594
+ */
595
+ eq(p) {
596
+ return this.minus(p).isZero();
597
+ }
598
+ /**
599
+ * Evaluates the polynomial at given values using Term-based operations.
600
+ * Supports partial evaluation (substituting some variables while keeping others symbolic).
601
+ * Does not rely on the Expression class for computation.
602
+ *
603
+ * @param values - Variable names mapped to exact rational-compatible values.
604
+ * @returns A new partially evaluated polynomial; unmentioned variables remain symbolic.
605
+ */
606
+ evaluate(values) {
607
+ // Determine which variables remain after substitution
608
+ const remainingVars = this.variables.filter(v => !(v in values));
609
+ // Convert input values to Rationals for consistent arithmetic
610
+ const rationalValues = {};
611
+ for (const v in values) {
612
+ const val = values[v];
613
+ if (Rational_1.Rational.isRational(val)) {
614
+ rationalValues[v] = val;
615
+ }
616
+ else {
617
+ rationalValues[v] = Rational_1.Rational.create(val.toString());
618
+ }
619
+ }
620
+ // Accumulate result terms together with the powers needed to rebuild them natively.
621
+ const resultTerms = new Map();
622
+ for (const term of this.terms) {
623
+ // Start with the term's coefficient
624
+ let coeff = term.coeff.getMultiplier();
625
+ const newPowers = {};
626
+ // Process each variable in the polynomial
627
+ for (const v of this.variables) {
628
+ const power = term.deg(v);
629
+ if (v in rationalValues) {
630
+ // Substitute this variable: multiply coefficient by value^power
631
+ if (power !== 0) {
632
+ const valPow = rationalValues[v].pow(power.toString());
633
+ coeff = coeff.times(valPow);
634
+ }
635
+ }
636
+ else {
637
+ // Keep this variable
638
+ if (power !== 0) {
639
+ newPowers[v] = power;
640
+ }
641
+ }
642
+ }
643
+ // Skip zero coefficients
644
+ if (coeff.isZero()) {
645
+ continue;
646
+ }
647
+ // Create a key for combining like terms. The key is only an internal map key;
648
+ // the retained power object is used for reconstruction.
649
+ const termKey = remainingVars.length > 0
650
+ ? remainingVars.map(v => `${v}:${newPowers[v] || 0}`).join(',')
651
+ : 'constant';
652
+ const existing = resultTerms.get(termKey);
653
+ if (existing) {
654
+ existing.coefficient = existing.coefficient.plus(coeff);
655
+ }
656
+ else {
657
+ resultTerms.set(termKey, { coefficient: coeff, powers: newPowers });
658
+ }
659
+ }
660
+ let retval;
661
+ if (resultTerms.size === 0) {
662
+ retval = new Polynomial((0, shortcuts_1.zero)());
663
+ }
664
+ else if (remainingVars.length === 0) {
665
+ const result = resultTerms.get('constant');
666
+ const coefficient = result ? result.coefficient : Rational_1.Rational.create('0');
667
+ retval = new Polynomial(Expression_1.Expression.Number(coefficient.value));
668
+ }
669
+ else {
670
+ retval = new Polynomial((0, shortcuts_1.zero)(), remainingVars, this.ordering);
671
+ retval.terms = [];
672
+ for (const result of resultTerms.values()) {
673
+ if (!result.coefficient.isZero()) {
674
+ retval.terms.push(new Term_1.Term(Expression_1.Expression.Number(result.coefficient.value), { ...result.powers }, remainingVars));
675
+ }
676
+ }
677
+ retval.order(this.ordering);
678
+ retval.updateExpression();
679
+ }
680
+ return retval;
681
+ }
682
+ /**
683
+ * Numerically evaluates the polynomial at given values.
684
+ * All variables must be provided values.
685
+ *
686
+ * @param values - Values for every variable stored by the polynomial.
687
+ * @returns The exact rational result.
688
+ * @throws {@link core!UnsupportedOperationError} Thrown when any stored variable is missing.
689
+ */
690
+ evaluateToRational(values) {
691
+ // Verify all variables have values
692
+ for (const v of this.variables) {
693
+ if (!(v in values)) {
694
+ throw new errors_1.UnsupportedOperationError((0, errors_1.message)('unknownVariable', { variable: v }));
695
+ }
696
+ }
697
+ // Convert input values to Rationals
698
+ const rationalValues = {};
699
+ for (const v in values) {
700
+ const val = values[v];
701
+ if (Rational_1.Rational.isRational(val)) {
702
+ rationalValues[v] = val;
703
+ }
704
+ else {
705
+ rationalValues[v] = Rational_1.Rational.create(val.toString());
706
+ }
707
+ }
708
+ let sum = Rational_1.Rational.create('0');
709
+ for (const term of this.terms) {
710
+ let termValue = term.coeff.getMultiplier();
711
+ for (const v of this.variables) {
712
+ const power = term.deg(v);
713
+ if (power !== 0) {
714
+ termValue = termValue.times(rationalValues[v].pow(power.toString()));
715
+ }
716
+ }
717
+ sum = sum.plus(termValue);
718
+ }
719
+ return sum;
720
+ }
721
+ /**
722
+ * Divides all terms by their exact numeric content and optionally their common monomial.
723
+ *
724
+ * @param reduceVariables - Also subtract the minimum shared positive power of each variable.
725
+ * @param mutate - Modify and return this polynomial instead of a deep copy.
726
+ * @returns The normalized target polynomial.
727
+ */
728
+ gcdFree(reduceVariables = false, mutate = false) {
729
+ const target = mutate ? this : new Polynomial(this);
730
+ // If it's a constant then there's nothing left to do
731
+ if (target.isConstant()) {
732
+ return target;
733
+ }
734
+ let common;
735
+ let gcd = target.content();
736
+ if (target.LC().sign() === -1) {
737
+ gcd = gcd.neg();
738
+ }
739
+ if (reduceVariables) {
740
+ common = target.commonTermVariables();
741
+ }
742
+ for (let i = 0; i < target.terms.length; i++) {
743
+ const term = target.terms[i];
744
+ // If reduction of the variable is requested then do so.
745
+ if (common) {
746
+ // Reduce the variables
747
+ for (const x in common) {
748
+ term.powers[x] -= common[x];
749
+ }
750
+ }
751
+ // Divide coefficient by GCD using Rational arithmetic
752
+ const currentCoeff = term.coeff.getMultiplier();
753
+ const newCoeff = currentCoeff.div(gcd);
754
+ term.coeff = Expression_1.Expression.fromRational(newCoeff);
755
+ }
756
+ target.updateExpression();
757
+ return target;
758
+ }
759
+ /**
760
+ * Returns the expression retained by the polynomial.
761
+ *
762
+ * @remarks
763
+ * This is an internal reference, not a copy. The term array is the operative
764
+ * representation for many methods, and direct term mutation is not guaranteed to
765
+ * rebuild this stored expression automatically.
766
+ */
767
+ getExpression() {
768
+ return this.expression;
769
+ }
770
+ /**
771
+ * Sorts the terms by graded lexicographic order
772
+ * grevlex first compares their powers. If their powers are equal then it breaks ties using {@link revlex} reverse lexicographic order.
773
+ *
774
+ * @returns This polynomial after sorting its terms in place.
775
+ */
776
+ grevlexSort() {
777
+ this.terms.sort((a, b) => {
778
+ return Polynomial.grevlex(a, b);
779
+ });
780
+ // Mark it
781
+ this.ordering = 'grevlex';
782
+ return this;
783
+ }
784
+ /**
785
+ * Sorts the terms by graded lexicographic order
786
+ * grlex first compares their powers. If their powers are equal then it breaks ties using {@link lex} lexicographic order.
787
+ *
788
+ * @returns This polynomial after sorting its terms in place.
789
+ */
790
+ grlexSort() {
791
+ this.terms.sort((a, b) => {
792
+ return Polynomial.grlex(a, b);
793
+ });
794
+ //Mark it
795
+ this.ordering = 'grlex';
796
+ return this;
797
+ }
798
+ /**
799
+ * Tests whether the polynomial consists of one constant term.
800
+ *
801
+ * @returns `true` only for the one-term constant representation.
802
+ */
803
+ isConstant() {
804
+ return this.terms.length === 1 && this.terms[0].isConstant();
805
+ }
806
+ /**
807
+ * Tests whether this polynomial has no terms or a zero leading term.
808
+ *
809
+ * @returns Whether the current term representation is zero.
810
+ */
811
+ isZero() {
812
+ return this.terms.length === 0 || this.LT().isZero();
813
+ }
814
+ /**
815
+ * Returns the leading coefficient under the current ordering.
816
+ *
817
+ * @returns The leading term's internal coefficient reference.
818
+ */
819
+ LC() {
820
+ return this.LT().coeff;
821
+ }
822
+ /**
823
+ * Sorts the terms by {@link lex} lexicographic order
824
+ *
825
+ * @returns This polynomial after sorting its terms in place.
826
+ */
827
+ lexSort() {
828
+ this.terms.sort((a, b) => {
829
+ return Polynomial.lex(a, b);
830
+ });
831
+ // Mark it
832
+ this.ordering = 'lex';
833
+ return this;
834
+ }
835
+ /**
836
+ * Returns the leading monomial with unit coefficient.
837
+ *
838
+ * @returns A new term with copied powers and variables.
839
+ */
840
+ LM() {
841
+ const LT = this.LT();
842
+ return new Term_1.Term('1', { ...LT.powers }, [...LT.variables]);
843
+ }
844
+ /**
845
+ * Returns the leading term under the current ordering.
846
+ *
847
+ * @returns The internal first term reference.
848
+ */
849
+ LT() {
850
+ return this.terms[0];
851
+ }
852
+ /**
853
+ * Gets the maximum variable occurrence in the polynomial. If two or more variables have
854
+ * an equal number of occurrences then they will be included in the set
855
+ *
856
+ * @returns A new record containing every variable tied for the greatest term-occurrence count.
857
+ * @example
858
+ * ```ts
859
+ * new Polynomial('q^3*a+2*q^2*a').maxVariableFrequency();
860
+ * // { a: { variable: 'a', count: 2, deg: 2 },
861
+ * // q: { variable: 'q', count: 2, deg: 5 } }
862
+ * ```
863
+ */
864
+ maxVariableFrequency() {
865
+ const freq = this.variableFrequency();
866
+ let max = [];
867
+ for (const v in freq) {
868
+ const c = freq[v].count;
869
+ if (!max[0] || max[0].count < c) {
870
+ // Either set it or wipe the whole thing
871
+ max = [freq[v]];
872
+ }
873
+ else if (max[0].count === c) {
874
+ max.push(freq[v]);
875
+ }
876
+ }
877
+ return (0, object_1.arrayToObject)(max, k => {
878
+ return k.variable;
879
+ });
880
+ }
881
+ /**
882
+ * Subtracts a Polynomial or a Term
883
+ *
884
+ * @param x - Term or polynomial to subtract.
885
+ * @returns A new polynomial. Both operands are left unchanged.
886
+ */
887
+ minus(x) {
888
+ return this.append(x, 'minus');
889
+ }
890
+ /**
891
+ * Reduces each collected coefficient modulo `n`.
892
+ *
893
+ * @remarks
894
+ * The polynomial itself is not modified. The returned coefficient object is derived from
895
+ * the current polynomial and stores the reduced coefficient expressions at the same power
896
+ * keys.
897
+ *
898
+ * @param n - Modulus passed to the symbolic `mod` operation.
899
+ * @returns A coefficient object containing the coefficient remainders.
900
+ */
901
+ mod(n) {
902
+ n = Expression_1.Expression.create(n);
903
+ const coeffs = this.coeffs();
904
+ coeffs.each((x, p) => {
905
+ coeffs.coeffs[p] = x.mod(n);
906
+ });
907
+ return coeffs;
908
+ }
909
+ /**
910
+ * Returns a copy with the leading nonconstant coefficient normalized to one.
911
+ *
912
+ * Constant polynomials are copied without coefficient normalization.
913
+ *
914
+ * @returns A new polynomial with a unit leading coefficient when normalization applies.
915
+ */
916
+ monic() {
917
+ const p = new Polynomial(this);
918
+ const LT = this.LT();
919
+ // If it's a constant the we're done
920
+ if (LT && !LT.isConstant()) {
921
+ const c = LT.coeff;
922
+ for (const t of p.terms) {
923
+ t.coeff = t.coeff.div(c);
924
+ }
925
+ p.updateExpression();
926
+ }
927
+ return p;
928
+ }
929
+ /**
930
+ * Returns the leading term's multidegree under the current ordering.
931
+ *
932
+ * @returns The leading term's cached internal multidegree array.
933
+ */
934
+ multideg() {
935
+ return this.terms[0].multidegArray;
936
+ }
937
+ /**
938
+ * Gets all the numeric coefficients in the polynomial as Rationals.
939
+ *
940
+ * @returns New array containing each term coefficient's internal rational multiplier.
941
+ */
942
+ numericCoeffs() {
943
+ return this.terms.map(t => t.coeff.getMultiplier());
944
+ }
945
+ /**
946
+ * Reorders the polynomial in the requested ordering if it's not already in that particular ordering.
947
+ *
948
+ * @param ordering - Requested multivariate ordering. Univariate input is always degree-sorted.
949
+ * @returns This polynomial after sorting its term array in place.
950
+ */
951
+ order(ordering) {
952
+ if (this.variables.length < 2) {
953
+ this.sort();
954
+ this.ordering = 'deg';
955
+ }
956
+ else {
957
+ // Set the new ordering if provided
958
+ if (ordering) {
959
+ this.ordering = ordering;
960
+ }
961
+ switch (this.ordering) {
962
+ case 'lex':
963
+ this.lexSort();
964
+ break;
965
+ case 'grevlex':
966
+ this.grevlexSort();
967
+ break;
968
+ case 'revlex':
969
+ this.revlexSort();
970
+ break;
971
+ case 'grlex':
972
+ default:
973
+ this.grlexSort();
974
+ break;
975
+ }
976
+ }
977
+ return this;
978
+ }
979
+ /**
980
+ * Adds a Polynomial or a Term.
981
+ *
982
+ * @param x - Term or polynomial to add.
983
+ * @returns A new polynomial. Both operands are left unchanged.
984
+ */
985
+ plus(x) {
986
+ return this.append(x, 'plus');
987
+ }
988
+ /**
989
+ * Raises the polynomial to a power.
990
+ *
991
+ * Exact non-negative integer powers use sparse polynomial arithmetic when the
992
+ * coefficient domain permits it. Other exponents retain symbolic expression behavior.
993
+ *
994
+ * @param p - Exponent accepted by symbolic expression powers.
995
+ * @returns A new polynomial parsed from the powered expression.
996
+ * @throws {@link core!PolynomialError} Thrown when the powered result is not polynomial-like.
997
+ */
998
+ pow(p) {
999
+ const exponent = Expression_1.Expression.create(p);
1000
+ const source = this.getExpression();
1001
+ let sparseResult;
1002
+ if (exponent.isNUM() &&
1003
+ exponent.isInteger() &&
1004
+ exponent.sign() >= 0 &&
1005
+ !source.hasDecimal()) {
1006
+ try {
1007
+ const power = exponent.getMultiplier().numerator;
1008
+ const variables = [...this.variables];
1009
+ const converted = (0, SparsePolynomialAdapter_1.expressionToIntegerSparsePolynomial)(source, variables);
1010
+ const denominator = converted.denominator ** power;
1011
+ let expression = (0, SparsePolynomialAdapter_1.sparsePolynomialToExpression)(converted.polynomial.pow(power), variables);
1012
+ if (denominator !== 1n) {
1013
+ expression = expression.div(Expression_1.Expression.Number(denominator));
1014
+ }
1015
+ sparseResult = new Polynomial(expression, undefined, this.ordering);
1016
+ }
1017
+ catch { }
1018
+ }
1019
+ return sparseResult ?? new Polynomial(source.pow(exponent), undefined, this.ordering);
1020
+ }
1021
+ /**
1022
+ * Sorts the terms by {@link revlex} reverse lexicographic order
1023
+ *
1024
+ * @returns This polynomial after sorting its terms in place.
1025
+ */
1026
+ revlexSort() {
1027
+ // There is only one sort for univariate
1028
+ this.terms.sort((a, b) => {
1029
+ return Polynomial.revlex(a, b);
1030
+ });
1031
+ // Mark it
1032
+ this.ordering = 'revlex';
1033
+ return this;
1034
+ }
1035
+ /**
1036
+ * Sorts the terms in the standard form of decreasing powers.
1037
+ */
1038
+ sort() {
1039
+ this.terms.sort((a, b) => {
1040
+ return b.getTotalPower() > a.getTotalPower() ? 1 : -1;
1041
+ });
1042
+ return this;
1043
+ }
1044
+ stripMonomialGCD(other) {
1045
+ const m = this.monomialGCDTerm(other);
1046
+ const p = new Polynomial(this);
1047
+ const q = new Polynomial(other);
1048
+ for (let i = 0; i < p.terms.length; i++) {
1049
+ p.terms[i] = p.terms[i].div(m);
1050
+ }
1051
+ for (let i = 0; i < q.terms.length; i++) {
1052
+ q.terms[i] = q.terms[i].div(m);
1053
+ }
1054
+ const mGCD = new Polynomial(m.getExpression());
1055
+ p.updateExpression();
1056
+ q.updateExpression();
1057
+ return { mGCD, p, q };
1058
+ }
1059
+ /**
1060
+ * Formats the current ordered terms as canonical parser text.
1061
+ *
1062
+ * @returns `"0"` for an empty/zero term representation, otherwise the joined term text.
1063
+ */
1064
+ text() {
1065
+ if (this.terms.length === 0 || (this.terms.length === 1 && this.terms[0].isZero())) {
1066
+ return '0';
1067
+ }
1068
+ const textArray = [];
1069
+ for (const t of this.terms) {
1070
+ const txt = t.text(this.variables);
1071
+ if (txt !== '0') {
1072
+ textArray.push(txt);
1073
+ }
1074
+ }
1075
+ return textArray.join('+').replace(/\+-/g, '-');
1076
+ }
1077
+ /**
1078
+ * Multiplies this polynomial by a term or polynomial.
1079
+ *
1080
+ * @param x - Multiplier.
1081
+ * @returns A new polynomial. Term multiplication copies this polynomial before
1082
+ * updating its terms. Polynomial multiplication uses exact sparse arithmetic when
1083
+ * both operands have numeric coefficients and otherwise preserves symbolic multiplication.
1084
+ */
1085
+ times(x) {
1086
+ let retval;
1087
+ if (Term_1.Term.isTerm(x)) {
1088
+ retval = new Polynomial(this);
1089
+ // Update the variables
1090
+ retval.variables = (0, utils_1.arrayAddUnique)(retval.variables, x.variables);
1091
+ if ((0, utils_1.isSorted)(this.variables)) {
1092
+ retval.variables.sort();
1093
+ }
1094
+ retval.isMultivariate = retval.variables.length > 1;
1095
+ for (let i = 0; i < retval.terms.length; i++) {
1096
+ retval.terms[i] = retval.terms[i].times(x);
1097
+ }
1098
+ retval.updateExpression();
1099
+ }
1100
+ else {
1101
+ const leftExpression = this.getExpression();
1102
+ const rightExpression = x.getExpression();
1103
+ let sparseResult;
1104
+ if (!leftExpression.hasDecimal() && !rightExpression.hasDecimal()) {
1105
+ try {
1106
+ const variables = [...new Set([...this.variables, ...x.variables])].sort();
1107
+ const left = (0, SparsePolynomialAdapter_1.expressionToIntegerSparsePolynomial)(leftExpression, variables);
1108
+ const right = (0, SparsePolynomialAdapter_1.expressionToIntegerSparsePolynomial)(rightExpression, variables);
1109
+ const denominator = left.denominator * right.denominator;
1110
+ let expression = (0, SparsePolynomialAdapter_1.sparsePolynomialToExpression)(left.polynomial.multiply(right.polynomial), variables);
1111
+ if (denominator !== 1n) {
1112
+ expression = expression.div(Expression_1.Expression.Number(denominator));
1113
+ }
1114
+ sparseResult = new Polynomial(expression, undefined, this.ordering);
1115
+ }
1116
+ catch { }
1117
+ }
1118
+ retval =
1119
+ sparseResult ??
1120
+ new Polynomial(leftExpression.times(rightExpression), undefined, this.ordering);
1121
+ }
1122
+ return retval;
1123
+ }
1124
+ toArray(asNumbers = false, variable) {
1125
+ const arr = [];
1126
+ const n = Number(this.deg(variable));
1127
+ const coeffs = this.coeffs(variable);
1128
+ for (let i = 0; i <= n; i++) {
1129
+ arr.push(coeffs.hasPower(i) ? coeffs.getPower(i) : (0, shortcuts_1.zero)());
1130
+ }
1131
+ if (asNumbers) {
1132
+ return arr.map(x => Number(x));
1133
+ }
1134
+ return arr;
1135
+ }
1136
+ /**
1137
+ * Converts dense coefficients to numerator `bigint` values.
1138
+ *
1139
+ * @param assertInZ - Reject coefficients whose denominator is not one.
1140
+ * @param variable - Optional variable to collect as the dense power index.
1141
+ * @returns New ascending-power array of coefficient numerators.
1142
+ * @throws Error Thrown when `assertInZ` is true and a coefficient is non-integral.
1143
+ */
1144
+ toBigIntArray(assertInZ = true, variable) {
1145
+ return this.toArray(false, variable).map(x => {
1146
+ if (assertInZ && !x.isInteger()) {
1147
+ throw new Error((0, errors_1.message)('integerRequired'));
1148
+ }
1149
+ return x.getMultiplier().numerator;
1150
+ });
1151
+ }
1152
+ /**
1153
+ * Converts dense coefficients to new `Decimal` values using their expression text.
1154
+ *
1155
+ * @returns An ascending-power decimal coefficient array.
1156
+ */
1157
+ toDecimalArray() {
1158
+ return this.toArray().map(x => new decimal_js_1.default(x.text()));
1159
+ }
1160
+ /**
1161
+ * Returns the polynomial in a form for easy debugging.
1162
+ *
1163
+ * @returns The same canonical term text as {@link Polynomial.text}.
1164
+ */
1165
+ toString() {
1166
+ return this.text();
1167
+ }
1168
+ /**
1169
+ * Counts each variable's term occurrences and accumulated degree.
1170
+ *
1171
+ * @returns A new record keyed by variable. Each entry reports its name, number of
1172
+ * nonzero-power terms, and sum of powers across those terms.
1173
+ */
1174
+ variableFrequency() {
1175
+ const count = {};
1176
+ for (const t of this.terms) {
1177
+ for (const v in t.powers) {
1178
+ // The counter object
1179
+ let o = count[v];
1180
+ if (!o) {
1181
+ count[v] = o = { variable: v, count: 0, deg: 0 };
1182
+ }
1183
+ // Check the degree. We only increment for degrees > 0. Since it's a polynomial,
1184
+ // no negative degrees should exist
1185
+ const deg = t.powers[v];
1186
+ if (deg !== 0) {
1187
+ o.count++;
1188
+ o.deg += deg;
1189
+ }
1190
+ }
1191
+ }
1192
+ return count;
1193
+ }
1194
+ }
1195
+ exports.Polynomial = Polynomial;