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,829 @@
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.Rational = exports.MAX_SCIENTIFIC_SIGNIFICANT_DIGITS = void 0;
7
+ exports.validateScientificSignificantDigits = validateScientificSignificantDigits;
8
+ const decimal_js_1 = __importDefault(require("decimal.js"));
9
+ const common_1 = require("../../common/common");
10
+ const errors_1 = require("../../errors");
11
+ const bigint_1 = require("../../functions/bigint/bigint");
12
+ const bigint_2 = require("../../functions/bigint/bigint");
13
+ const string_1 = require("../../functions/string");
14
+ const Expression_1 = require("../expression/Expression");
15
+ const constants_1 = require("../parser/constants");
16
+ /** Maximum scientific significant-digit count accepted by decimal.js. */
17
+ exports.MAX_SCIENTIFIC_SIGNIFICANT_DIGITS = 1_000_000_000;
18
+ function validateScientificSignificantDigits(value, received = String(value)) {
19
+ if (typeof value !== 'number' ||
20
+ !Number.isInteger(value) ||
21
+ value < 1 ||
22
+ value > exports.MAX_SCIENTIFIC_SIGNIFICANT_DIGITS) {
23
+ throw new errors_1.UnexpectedInputError((0, errors_1.message)('wrongInput', {
24
+ expected: `an integer from 1 to ${exports.MAX_SCIENTIFIC_SIGNIFICANT_DIGITS}`,
25
+ received,
26
+ }));
27
+ }
28
+ return value;
29
+ }
30
+ /**
31
+ * Represents an exact rational value as a `bigint` numerator and denominator.
32
+ *
33
+ * @remarks
34
+ * Core rational arithmetic is exact: decimal and scientific-notation input is converted to
35
+ * the exact fraction represented by the input text rather than stored as a floating-point
36
+ * approximation. The notable exception is non-integer {@link Rational.pow}, which uses
37
+ * `decimal.js` numerical exponentiation. The {@link Rational.asDecimal} flag records
38
+ * presentation intent only; it does not change the underlying rational representation.
39
+ *
40
+ * Values produced by {@link Rational.create} and the arithmetic methods are normally reduced
41
+ * and use a positive denominator. The public constructor is lower level: it stores its two
42
+ * `bigint` arguments exactly as supplied and does not reduce the fraction, normalize the
43
+ * denominator sign, or reject a zero denominator. Sign, comparison, and integer helpers assume
44
+ * the normal representation, so callers constructing values directly are responsible for
45
+ * preserving that representation.
46
+ *
47
+ * `Rational` instances are mutable because their representation fields are public and
48
+ * {@link Rational.updateValue} updates the instance in place. The ordinary arithmetic methods
49
+ * return new values and do not mutate their operands.
50
+ */
51
+ class Rational {
52
+ /**
53
+ * Finite-precision rational approximation of Euler's number at the current configured precision.
54
+ *
55
+ * Recomputed when {@link Rational.set} changes the precision.
56
+ */
57
+ static E = Rational.create(constants_1.PARSER_CONSTANTS.e());
58
+ /**
59
+ * Finite-precision rational approximation of pi at the current configured precision.
60
+ *
61
+ * Recomputed when {@link Rational.set} changes the precision.
62
+ */
63
+ static PI = Rational.create(constants_1.PARSER_CONSTANTS.pi());
64
+ /**
65
+ * Precision setting shared with `decimal.js` and used as the default digit count by
66
+ * {@link Rational.toDecimalString}. Change it through {@link Rational.set}.
67
+ */
68
+ static precision = decimal_js_1.default.precision;
69
+ /**
70
+ * Whether text output should preserve decimal presentation by default.
71
+ *
72
+ * This flag does not change the exact numerator/denominator representation. Most rational
73
+ * arithmetic propagates decimal intent when either operand has it set.
74
+ */
75
+ asDecimal = false;
76
+ /** Marker used by Nerdamer's runtime type guards. */
77
+ dataType = constants_1.RATIONAL;
78
+ /** Denominator of the exact rational representation. */
79
+ denominator;
80
+ /** Numerator of the exact rational representation. */
81
+ numerator;
82
+ /**
83
+ * The supplied textual value.
84
+ *
85
+ * Numeric behavior is defined by {@link Rational.numerator} and
86
+ * {@link Rational.denominator}; callers should not treat this field as an independent
87
+ * authoritative numeric representation.
88
+ */
89
+ value;
90
+ /**
91
+ * Creates a rational from raw numerator and denominator components.
92
+ *
93
+ * @remarks
94
+ * This constructor performs no normalization or validation. Prefer
95
+ * {@link Rational.create} for user input and for values that must use Nerdamer's normal
96
+ * reduced-fraction representation.
97
+ *
98
+ * @param a - Numerator to store.
99
+ * @param b - Denominator to store.
100
+ */
101
+ constructor(a, b) {
102
+ this.numerator = a;
103
+ this.denominator = b;
104
+ this.value = b === 1n ? String(a) : `${a}/${b}`;
105
+ }
106
+ /**
107
+ * Tests whether every supplied rational is strictly negative.
108
+ *
109
+ * @param args - Rational values to test.
110
+ * @returns `true` when each value has sign `-1`.
111
+ */
112
+ static allNegative(...args) {
113
+ for (const e of args) {
114
+ if (e.sign() !== -1) {
115
+ return false;
116
+ }
117
+ }
118
+ return true;
119
+ }
120
+ /**
121
+ * Creates a normalized rational from an integer, fraction, decimal, or scientific-notation value.
122
+ *
123
+ * @remarks
124
+ * Fraction strings are reduced and a negative denominator is moved to the numerator. Decimal
125
+ * and scientific-notation strings are converted to the exact rational represented by their
126
+ * finite decimal text. Those forms also set {@link Rational.asDecimal}, so later text output
127
+ * normally remains decimal even though the stored arithmetic stays exact.
128
+ *
129
+ * @param value - Integer `bigint` or numeric string to convert.
130
+ * @returns A new normalized `Rational`.
131
+ *
132
+ * @throws {@link DivisionByZeroError}
133
+ * Thrown when a fraction string has a zero denominator.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * Rational.create('2/4').text(); // "1/2"
138
+ * Rational.create('0.125').text(); // "0.125"
139
+ * Rational.create('4e1').text(); // "40.0"
140
+ * ```
141
+ */
142
+ static create(value) {
143
+ let a;
144
+ let b;
145
+ let asDecimal = false;
146
+ if (typeof value === 'bigint') {
147
+ a = value;
148
+ b = 1n;
149
+ value = String(a);
150
+ }
151
+ else if (/^[+-]?\d+$/.test(value)) {
152
+ a = BigInt(value);
153
+ b = 1n;
154
+ }
155
+ else {
156
+ if (value.includes('/')) {
157
+ [a, b] = value.split('/').map(x => BigInt(x));
158
+ if (b === 0n) {
159
+ throw new errors_1.DivisionByZeroError((0, errors_1.message)('divisionByZero'));
160
+ }
161
+ if (b < 0n) {
162
+ a = -a;
163
+ b = -b;
164
+ }
165
+ [a, b] = (0, bigint_1.simplifyRatio)(a, b);
166
+ value = b === 1n ? String(a) : `${a}/${b}`;
167
+ }
168
+ else {
169
+ const isScientific = value.toLocaleLowerCase().includes('e');
170
+ asDecimal = isScientific || value.includes(constants_1.DOT);
171
+ if (isScientific) {
172
+ value = (0, string_1.scientificToDecimal)(value);
173
+ // All scientific numbers are decimal
174
+ asDecimal = true;
175
+ }
176
+ [a, b] = (0, bigint_1.convert)(value);
177
+ }
178
+ }
179
+ const rational = new Rational(a, b);
180
+ rational.value = value;
181
+ rational.asDecimal = asDecimal;
182
+ return rational;
183
+ }
184
+ /**
185
+ * Computes the rational greatest common divisor across the supplied values.
186
+ *
187
+ * @remarks
188
+ * The operation is folded pairwise and preserves decimal presentation intent when it is
189
+ * present on an operand.
190
+ *
191
+ * @param args - Rational values whose common divisor should be computed.
192
+ * @returns The pairwise rational GCD.
193
+ */
194
+ static GCD(...args) {
195
+ let retval = args[0];
196
+ for (let i = 1; i < args.length; i++) {
197
+ retval = retval.GCD(args[i]);
198
+ }
199
+ return retval;
200
+ }
201
+ /**
202
+ * Returns the currently configured rational/Decimal precision setting.
203
+ *
204
+ * @returns The configured precision value.
205
+ */
206
+ static getPrecision() {
207
+ return Rational.precision;
208
+ }
209
+ /**
210
+ * Returns a value unchanged for compatibility with generic numeric hooks.
211
+ *
212
+ * @param value - Value to pass through.
213
+ * @returns The same value reference or primitive supplied by the caller.
214
+ */
215
+ static hook(value) {
216
+ return value;
217
+ }
218
+ /**
219
+ * Tests whether a value carries Nerdamer's rational runtime type marker.
220
+ *
221
+ * @remarks
222
+ * This is a marker-based guard rather than an `instanceof` check, which allows compatible
223
+ * Nerdamer rational objects to be recognized where constructor identity is not the useful
224
+ * distinction.
225
+ *
226
+ * @param value - Value to inspect.
227
+ * @returns `true` when `value.dataType` is Nerdamer's rational marker.
228
+ */
229
+ static isRational(value) {
230
+ return (0, common_1.isNerdamerNativeType)(value, constants_1.RATIONAL);
231
+ }
232
+ /**
233
+ * Computes the rational least common multiple across the supplied values.
234
+ *
235
+ * @remarks
236
+ * The operation is folded pairwise and preserves decimal presentation intent when it is
237
+ * present on an operand.
238
+ *
239
+ * @param args - Rational values whose common multiple should be computed.
240
+ * @returns The pairwise rational LCM.
241
+ */
242
+ static LCM(...args) {
243
+ let retval = args[0];
244
+ for (let i = 1; i < args.length; i++) {
245
+ retval = retval.LCM(args[i]);
246
+ }
247
+ return retval;
248
+ }
249
+ /**
250
+ * Updates shared numeric settings used by rational decimal conversion.
251
+ *
252
+ * @remarks
253
+ * Currently only `precision` is acted upon. Setting it updates the global `decimal.js`
254
+ * precision, records the value used by {@link Rational.toDecimalString}, and recomputes
255
+ * {@link Rational.PI} and {@link Rational.E} at the new precision.
256
+ *
257
+ * @param values - Settings object; `precision` is the supported numeric setting.
258
+ */
259
+ static set(values) {
260
+ if ('precision' in values) {
261
+ const precision = Number(values.precision);
262
+ // Update the precision for decimal to match
263
+ decimal_js_1.default.set({
264
+ precision: precision,
265
+ });
266
+ // Set the precision for this class after Decimal accepts the value
267
+ Rational.precision = precision;
268
+ // Update pi
269
+ Rational.PI = Rational.create(constants_1.PARSER_CONSTANTS.pi());
270
+ Rational.E = Rational.create(constants_1.PARSER_CONSTANTS.e());
271
+ }
272
+ }
273
+ /**
274
+ * Converts a string to a rational or normalizes rational ownership for a caller.
275
+ *
276
+ * @remarks
277
+ * Strings always produce a new value through {@link Rational.create}. An existing `Rational`
278
+ * is returned by identity unless `ensureCopy` is `true`.
279
+ *
280
+ * @param x - Numeric string or rational value.
281
+ * @param ensureCopy - Guarantee a distinct object when `x` is already a `Rational`.
282
+ * @returns The converted rational, a copy, or the original rational according to `ensureCopy`.
283
+ */
284
+ static toRational(x, ensureCopy = false) {
285
+ // Convert any strings to Rationals
286
+ if (typeof x === 'string') {
287
+ return Rational.create(x);
288
+ }
289
+ // If they want a guaranteed copy then just return a copy
290
+ if (ensureCopy) {
291
+ return x.copy();
292
+ }
293
+ // Otherwise just return the original rational
294
+ return x;
295
+ }
296
+ /**
297
+ * Returns the absolute value without modifying this rational.
298
+ *
299
+ * @returns A copied rational whose numerator is nonnegative.
300
+ */
301
+ abs() {
302
+ const retval = this.copy();
303
+ retval.numerator = (0, bigint_1.abs)(retval.numerator);
304
+ retval.updateValue();
305
+ return retval;
306
+ }
307
+ /**
308
+ * Creates a distinct copy of this rational.
309
+ *
310
+ * @returns A new object with the same numeric representation and presentation metadata.
311
+ */
312
+ copy() {
313
+ const copy = new Rational(this.numerator, this.denominator);
314
+ copy.value = this.value;
315
+ copy.asDecimal = this.asDecimal;
316
+ return copy;
317
+ }
318
+ div(num) {
319
+ let retval;
320
+ if (Expression_1.Expression.isExpression(num)) {
321
+ retval = Expression_1.Expression.create(this).div(num);
322
+ }
323
+ else {
324
+ num = Rational.toRational(num, true);
325
+ // Invert and multiply
326
+ // There is no need to track the result type in this function since it's being handled by invert and multiply
327
+ retval = this.times(num.invert());
328
+ }
329
+ return retval;
330
+ }
331
+ eq(num) {
332
+ if (Expression_1.Expression.isExpression(num)) {
333
+ return Expression_1.Expression.create(this).eq(num);
334
+ }
335
+ else {
336
+ num = Rational.toRational(num);
337
+ return this.numerator * num.denominator === num.numerator * this.denominator;
338
+ }
339
+ }
340
+ /**
341
+ * Tests whether the stored denominator is even.
342
+ *
343
+ * @returns `true` when {@link Rational.denominator} is divisible by two.
344
+ */
345
+ evenDenominator() {
346
+ return (0, bigint_1.isEven)(this.denominator);
347
+ }
348
+ /**
349
+ * Tests whether the stored numerator is even.
350
+ *
351
+ * @returns `true` when {@link Rational.numerator} is divisible by two.
352
+ */
353
+ evenNumerator() {
354
+ return (0, bigint_1.isEven)(this.numerator);
355
+ }
356
+ /**
357
+ * Computes the rational greatest common divisor with another value.
358
+ *
359
+ * @param num - Rational or numeric string to combine with this value.
360
+ * @returns A new reduced rational GCD. Decimal presentation is preserved when either operand
361
+ * was marked for decimal output.
362
+ */
363
+ GCD(num) {
364
+ num = Rational.toRational(num);
365
+ const [n, d] = (0, bigint_1.simplifyRatio)((0, bigint_2.GCD)(this.numerator * num.denominator, this.denominator * num.numerator), this.denominator * num.denominator);
366
+ const retval = new Rational(n, d);
367
+ retval.asDecimal = this.asDecimal || num.asDecimal;
368
+ return retval;
369
+ }
370
+ gt(num) {
371
+ if (Expression_1.Expression.isExpression(num)) {
372
+ return Expression_1.Expression.create(this).gt(num);
373
+ }
374
+ num = Rational.toRational(num);
375
+ return this.numerator * num.denominator > num.numerator * this.denominator;
376
+ }
377
+ /**
378
+ * Tests whether this rational is greater than or equal to another rational value.
379
+ *
380
+ * @param num - Rational or numeric string to compare against.
381
+ * @returns `true` when this rational is greater than or equal to `num`.
382
+ */
383
+ gte(num) {
384
+ num = Rational.toRational(num);
385
+ return this.numerator * num.denominator >= num.numerator * this.denominator;
386
+ }
387
+ /**
388
+ * Returns the multiplicative inverse of this rational.
389
+ *
390
+ * @remarks
391
+ * The original object is not modified. For normally constructed rationals, the returned
392
+ * denominator remains positive and the sign is carried by the numerator.
393
+ *
394
+ * @returns A new rational representing `1 / this`.
395
+ * @throws {@link DivisionByZeroError}
396
+ * Thrown when this rational is exactly zero.
397
+ */
398
+ invert() {
399
+ // Throw if trying to divide by zero
400
+ if (this.isZero()) {
401
+ throw new errors_1.DivisionByZeroError((0, errors_1.message)('divisionByZero'));
402
+ }
403
+ const retval = this.copy();
404
+ // Store the sign
405
+ const sgn = retval.sign();
406
+ // Remove it from the numerator.
407
+ retval.numerator = (0, bigint_1.abs)(retval.numerator);
408
+ // Switch them
409
+ [retval.numerator, retval.denominator] = [retval.denominator, retval.numerator];
410
+ // Put back the sign and return
411
+ return sgn === -1 ? retval.neg() : retval.updateValue();
412
+ }
413
+ /**
414
+ * Tests whether this value is an even integer.
415
+ *
416
+ * @returns `true` only when the denominator is one and the numerator is even.
417
+ */
418
+ isEven() {
419
+ return this.isInteger() && this.evenNumerator();
420
+ }
421
+ /**
422
+ * Tests whether this rational is stored in integer form.
423
+ *
424
+ * @remarks
425
+ * This checks only whether the denominator is exactly `1n`. Values built with the raw
426
+ * constructor must therefore be reduced first if equivalent forms such as `8/4` should be
427
+ * recognized as integers.
428
+ *
429
+ * @returns `true` when the stored denominator is one.
430
+ */
431
+ isInteger() {
432
+ return this.denominator === 1n;
433
+ }
434
+ /**
435
+ * Tests whether this rational is exactly `-1` in normalized integer form.
436
+ *
437
+ * @returns `true` for numerator `-1n` and denominator `1n`.
438
+ */
439
+ isMinusOne() {
440
+ return this.numerator === -1n && this.denominator === 1n;
441
+ }
442
+ /**
443
+ * Tests whether the stored numerator is negative.
444
+ *
445
+ * @remarks
446
+ * Nerdamer's normal rational representation keeps the denominator positive, so the numerator
447
+ * carries the sign. Raw constructor values with a negative denominator do not follow that
448
+ * representation.
449
+ *
450
+ * @returns `true` when the numerator is negative.
451
+ */
452
+ isNegative() {
453
+ return this.numerator < 0n;
454
+ }
455
+ /**
456
+ * Tests whether this rational is exactly `1` in normalized integer form.
457
+ *
458
+ * @returns `true` for numerator `1n` and denominator `1n`.
459
+ */
460
+ isOne() {
461
+ return this.numerator === 1n && this.denominator === 1n;
462
+ }
463
+ /**
464
+ * Tests whether this rational is exactly zero.
465
+ *
466
+ * @returns `true` when the numerator is zero.
467
+ */
468
+ isZero() {
469
+ return this.numerator === 0n;
470
+ }
471
+ /**
472
+ * Computes the rational least common multiple with another value.
473
+ *
474
+ * @param num - Rational or numeric string to combine with this value.
475
+ * @returns A new reduced, nonnegative rational LCM. Zero combined with any rational returns
476
+ * zero. Decimal presentation is preserved when either operand was marked for decimal output.
477
+ */
478
+ LCM(num) {
479
+ num = Rational.toRational(num);
480
+ let n = 0n;
481
+ let d = 1n;
482
+ if (!this.isZero() && !num.isZero()) {
483
+ [n, d] = (0, bigint_1.simplifyRatio)((0, bigint_1.abs)(this.numerator * num.numerator), (0, bigint_2.GCD)(this.numerator * num.denominator, this.denominator * num.numerator));
484
+ }
485
+ const retval = new Rational(n, d);
486
+ retval.asDecimal = this.asDecimal || num.asDecimal;
487
+ return retval;
488
+ }
489
+ /**
490
+ * Tests whether this rational is less than another rational value.
491
+ *
492
+ * @param num - Rational or numeric string to compare against.
493
+ * @returns `true` when this rational is less than `num`.
494
+ */
495
+ lt(num) {
496
+ num = Rational.toRational(num);
497
+ return this.numerator * num.denominator < num.numerator * this.denominator;
498
+ }
499
+ /**
500
+ * Tests whether this rational is less than or equal to another rational value.
501
+ *
502
+ * @param num - Rational or numeric string to compare against.
503
+ * @returns `true` when this rational is less than or equal to `num`.
504
+ */
505
+ lte(num) {
506
+ num = Rational.toRational(num);
507
+ return this.numerator * num.denominator <= num.numerator * this.denominator;
508
+ }
509
+ minus(num) {
510
+ let retval;
511
+ if (Expression_1.Expression.isExpression(num)) {
512
+ retval = Expression_1.Expression.create(this).minus(num);
513
+ }
514
+ else {
515
+ num = Rational.toRational(num, true);
516
+ retval = this.plus(num.neg());
517
+ }
518
+ // Negate and add
519
+ return retval;
520
+ }
521
+ /**
522
+ * Computes the exact rational modulo with another value.
523
+ *
524
+ * @remarks
525
+ * The operands are converted to a common denominator, Nerdamer's integer modulo operation is
526
+ * applied to the corresponding numerators, and the resulting fraction is reduced. Decimal
527
+ * presentation is preserved when either operand was marked for decimal output.
528
+ *
529
+ * @param num - Nonzero rational or numeric string used as the modulus.
530
+ * @returns The reduced rational remainder.
531
+ * @throws A native `RangeError` when `num` is zero.
532
+ */
533
+ mod(num) {
534
+ num = Rational.toRational(num);
535
+ // Make their denominators common and get the mod of the common numerators
536
+ const numerator = (0, bigint_2.mod)(this.numerator * num.denominator, num.numerator * this.denominator);
537
+ const denominator = this.denominator * num.denominator;
538
+ const result = new Rational(...(0, bigint_1.simplifyRatio)(numerator, denominator));
539
+ result.asDecimal = this.asDecimal || num.asDecimal;
540
+ return result;
541
+ }
542
+ /**
543
+ * Returns the additive inverse of this rational.
544
+ *
545
+ * @returns A new rational with the numerator sign reversed.
546
+ */
547
+ neg() {
548
+ const retval = this.copy();
549
+ retval.numerator *= -1n;
550
+ retval.updateValue();
551
+ return retval;
552
+ }
553
+ plus(num) {
554
+ let retval;
555
+ if (Expression_1.Expression.isExpression(num)) {
556
+ retval = Expression_1.Expression.create(this).plus(num);
557
+ }
558
+ else {
559
+ num = Rational.toRational(num);
560
+ let numerator;
561
+ let denominator;
562
+ // let result: Rational = new Rational('0');
563
+ // If they have the same denominator then we can add the numerators.
564
+ if (num.denominator === this.denominator) {
565
+ numerator = num.numerator + this.numerator;
566
+ denominator = num.denominator;
567
+ }
568
+ else {
569
+ // Fractional addition and then simplify using their gcd.
570
+ numerator = this.numerator * num.denominator + num.numerator * this.denominator;
571
+ denominator = this.denominator * num.denominator;
572
+ }
573
+ [numerator, denominator] = (0, bigint_1.simplifyRatio)(numerator, denominator);
574
+ retval = new Rational(numerator, denominator);
575
+ // Ensure that any operation with a decimal results in a decimal
576
+ retval.asDecimal = this.asDecimal || num.asDecimal;
577
+ }
578
+ return retval;
579
+ }
580
+ pow(num) {
581
+ let retval;
582
+ if (Expression_1.Expression.isExpression(num)) {
583
+ retval = Expression_1.Expression.create(this).pow(num);
584
+ }
585
+ else {
586
+ num = Rational.toRational(num);
587
+ if (this.isZero() && num.isZero()) {
588
+ throw new errors_1.ZeroToZeroPowerError((0, errors_1.message)('zeroToZeroPower'));
589
+ }
590
+ if (num.isInteger()) {
591
+ let exponent = num.numerator;
592
+ let baseNumerator = this.numerator;
593
+ let baseDenominator = this.denominator;
594
+ if (exponent < 0n) {
595
+ const inverted = this.invert();
596
+ baseNumerator = inverted.numerator;
597
+ baseDenominator = inverted.denominator;
598
+ exponent = (0, bigint_1.abs)(exponent);
599
+ }
600
+ const [numerator, denominator] = (0, bigint_1.simplifyRatio)(baseNumerator ** exponent, baseDenominator ** exponent);
601
+ retval = new Rational(numerator, denominator);
602
+ retval.asDecimal = this.asDecimal;
603
+ }
604
+ else {
605
+ const s = this.toDecimal().toPower(num.toDecimal()).toString();
606
+ retval = Rational.create(s);
607
+ retval.asDecimal = this.asDecimal || retval.asDecimal;
608
+ }
609
+ }
610
+ return retval;
611
+ }
612
+ /**
613
+ * Returns the sign carried by the numerator.
614
+ *
615
+ * @returns `-1` for a negative numerator, `0` for zero, or `1` for a positive numerator.
616
+ */
617
+ sign() {
618
+ return (0, bigint_1.sign)(this.numerator);
619
+ }
620
+ /**
621
+ * Formats this rational as fraction, integer, or decimal text.
622
+ *
623
+ * @remarks
624
+ * Scientific formatting is selected when `options.scientific` supplies an integer significant-digit
625
+ * count from 1 through 1e9. The requested count is preserved in the coefficient, including
626
+ * trailing zeroes. Otherwise decimal formatting is selected when {@link Rational.asDecimal} is set or when
627
+ * `options.decimal` is truthy. Non-integer decimal output uses `decimal.js`; an optional
628
+ * `options.precision` temporarily controls its significant-digit precision for this conversion.
629
+ * Integer decimal output includes a `.0` suffix. Without decimal formatting, non-integer
630
+ * fractions are emitted from the stored numerator and denominator, and integers as plain text.
631
+ *
632
+ * @param options - Formatting options. `scientific` selects significant-digit scientific output;
633
+ * `decimal` forces decimal output and `precision` controls decimal conversion precision when applicable.
634
+ * @returns The formatted numeric text.
635
+ */
636
+ text(options) {
637
+ let value;
638
+ let scientificValue;
639
+ const thisIsInteger = this.isInteger();
640
+ const integerDigits = thisIsInteger
641
+ ? (this.numerator < 0n ? -this.numerator : this.numerator).toString().length
642
+ : 0;
643
+ const exactScientific = options?.exactScientific === true;
644
+ const scientificDigits = options && 'scientific' in options && options.scientific !== undefined
645
+ ? validateScientificSignificantDigits(options.scientific)
646
+ : undefined;
647
+ if (scientificDigits !== undefined) {
648
+ if (this.isZero()) {
649
+ scientificValue = '0';
650
+ }
651
+ else {
652
+ const previousPrecision = decimal_js_1.default.precision;
653
+ const precision = Math.max(previousPrecision, scientificDigits);
654
+ try {
655
+ if (precision !== previousPrecision) {
656
+ decimal_js_1.default.set({ precision });
657
+ }
658
+ const scientific = this.toDecimal().toExponential(scientificDigits - 1);
659
+ const [coefficient, exponent] = scientific.split('e');
660
+ const normalizedExponent = BigInt(exponent);
661
+ const candidate = normalizedExponent === 0n
662
+ ? coefficient
663
+ : `${coefficient}e${normalizedExponent}`;
664
+ if (!exactScientific ||
665
+ (thisIsInteger &&
666
+ integerDigits > scientificDigits &&
667
+ normalizedExponent !== 0n &&
668
+ Rational.create(candidate).eq(this))) {
669
+ scientificValue = candidate;
670
+ }
671
+ }
672
+ finally {
673
+ if (decimal_js_1.default.precision !== previousPrecision) {
674
+ decimal_js_1.default.set({ precision: previousPrecision });
675
+ }
676
+ }
677
+ }
678
+ }
679
+ if (scientificValue !== undefined) {
680
+ value = scientificValue;
681
+ }
682
+ // Mark it as a decimal if it is such
683
+ else if (this.asDecimal || options?.decimal) {
684
+ if (thisIsInteger) {
685
+ value = `${this.numerator}.0`;
686
+ }
687
+ else {
688
+ const previousPrecision = decimal_js_1.default.precision;
689
+ const precision = Number(options?.precision) || previousPrecision;
690
+ if (precision === previousPrecision) {
691
+ value = this.toDecimal().toString();
692
+ }
693
+ else {
694
+ try {
695
+ decimal_js_1.default.set({ precision: precision });
696
+ value = this.toDecimal().toString();
697
+ }
698
+ finally {
699
+ decimal_js_1.default.set({ precision: previousPrecision });
700
+ }
701
+ }
702
+ // decimal.js trims insignificant zeros and can round a non-integer rational
703
+ // to a plain integer string. Preserve the requested decimal presentation.
704
+ if (!value.includes('.') && !/[eE]/.test(value)) {
705
+ value += '.0';
706
+ }
707
+ }
708
+ }
709
+ else if (thisIsInteger) {
710
+ value = `${this.numerator}`;
711
+ }
712
+ else {
713
+ value = `${this.numerator}/${this.denominator}`;
714
+ }
715
+ return value;
716
+ }
717
+ times(num) {
718
+ let retval;
719
+ if (Expression_1.Expression.isExpression(num)) {
720
+ retval = num.times(this);
721
+ }
722
+ else {
723
+ num = Rational.toRational(num);
724
+ const [numerator, denominator] = (0, bigint_1.simplifyRatio)(this.numerator * num.numerator, this.denominator * num.denominator);
725
+ retval = new Rational(numerator, denominator);
726
+ // Ensure that any operation with a decimal results in a decimal
727
+ retval.asDecimal = this.asDecimal || num.asDecimal;
728
+ }
729
+ return retval;
730
+ }
731
+ /**
732
+ * Converts the exact fraction to a `decimal.js` value at the current global Decimal precision.
733
+ *
734
+ * Integer rationals avoid an unnecessary Decimal division.
735
+ *
736
+ * @returns A new Decimal representing `numerator / denominator`.
737
+ */
738
+ toDecimal() {
739
+ let retval = new decimal_js_1.default(String(this.numerator));
740
+ if (this.denominator !== 1n) {
741
+ retval = retval.div(String(this.denominator));
742
+ }
743
+ return retval;
744
+ }
745
+ /**
746
+ * Converts the exact fraction to decimal text using integer arithmetic.
747
+ *
748
+ * @remarks
749
+ * `precision` is the maximum number of digits generated after the decimal point. The
750
+ * conversion truncates at that position rather than rounding, then removes trailing zeros
751
+ * and a trailing decimal point. When omitted, the configured Rational precision is used.
752
+ *
753
+ * This method does not depend on {@link Rational.asDecimal}; it always requests decimal text.
754
+ *
755
+ * @param precision - Number of fractional digits to generate.
756
+ * @returns Truncated decimal text with unnecessary trailing zeros removed.
757
+ *
758
+ * @example
759
+ * ```ts
760
+ * Rational.create('1/3').toDecimalString(5); // "0.33333"
761
+ * Rational.create('7/4').toDecimalString(5); // "1.75"
762
+ * ```
763
+ */
764
+ toDecimalString(precision) {
765
+ // return this.toDecimal().toString();
766
+ const sgn = (0, bigint_1.sign)(this.numerator);
767
+ const a = (0, bigint_1.abs)(this.numerator);
768
+ const b = this.denominator;
769
+ const whole = a / b;
770
+ const rem = a % b;
771
+ const decimalPlaces = precision ?? Rational.precision;
772
+ const dec = ((10n ** BigInt(decimalPlaces) * rem) / b).toString();
773
+ const retval = `${sgn < 0 ? '-' : ''}${whole}.${'0'.repeat(Math.max(0, decimalPlaces - dec.length))}${dec}`
774
+ .replace(/0+$/g, '')
775
+ .replace(/\.$/g, '');
776
+ return retval;
777
+ }
778
+ /**
779
+ * Returns the same formatted representation as {@link Rational.text}.
780
+ *
781
+ * @param options - Formatting options forwarded to `text`.
782
+ * @returns The formatted rational string.
783
+ */
784
+ toString(options) {
785
+ return this.text(options);
786
+ }
787
+ /**
788
+ * Refreshes {@link Rational.value} from the current numerator and denominator.
789
+ *
790
+ * @remarks
791
+ * This is one of the few mutating methods on `Rational`; it updates this object and returns
792
+ * the same reference.
793
+ *
794
+ * @returns This rational instance.
795
+ */
796
+ updateValue() {
797
+ this.value =
798
+ this.denominator === 1n
799
+ ? String(this.numerator)
800
+ : `${this.numerator}/${this.denominator}`;
801
+ return this;
802
+ }
803
+ /**
804
+ * Converts this rational to a native JavaScript number.
805
+ *
806
+ * @remarks
807
+ * This conversion is approximate and subject to the range and precision limits of
808
+ * JavaScript `number`. Numerator and denominator values that are both safe integers use
809
+ * native division directly; larger values retain the Decimal-backed fallback so finite ratios
810
+ * are not lost merely because an individual component exceeds the native numeric range.
811
+ * Use the rational representation or {@link Rational.toDecimal} when native-number limits
812
+ * are unacceptable.
813
+ *
814
+ * @returns The approximate native numeric value.
815
+ */
816
+ valueOf() {
817
+ const numerator = Number(this.numerator);
818
+ const denominator = Number(this.denominator);
819
+ let retval;
820
+ if (Number.isSafeInteger(numerator) && Number.isSafeInteger(denominator)) {
821
+ retval = numerator / denominator;
822
+ }
823
+ else {
824
+ retval = Number(this.toDecimal());
825
+ }
826
+ return retval;
827
+ }
828
+ }
829
+ exports.Rational = Rational;