@cortex-js/compute-engine 0.53.0 → 0.54.0

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 (227) hide show
  1. package/dist/compute-engine.esm.js +1684 -333
  2. package/dist/compute-engine.min.esm.js +527 -55
  3. package/dist/compute-engine.min.umd.cjs +1035 -0
  4. package/dist/{compute-engine.umd.js → compute-engine.umd.cjs} +1684 -333
  5. package/dist/math-json.esm.js +2 -2
  6. package/dist/math-json.min.esm.js +2 -2
  7. package/dist/{math-json.min.umd.js → math-json.min.umd.cjs} +2 -2
  8. package/dist/{math-json.umd.js → math-json.umd.cjs} +2 -2
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/configuration-change.d.ts +1 -1
  11. package/dist/types/common/fuzzy-string-match.d.ts +1 -1
  12. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  13. package/dist/types/common/interruptible.d.ts +1 -1
  14. package/dist/types/common/one-of.d.ts +1 -1
  15. package/dist/types/common/signals.d.ts +1 -1
  16. package/dist/types/common/type/ast-nodes.d.ts +1 -1
  17. package/dist/types/common/type/boxed-type.d.ts +1 -1
  18. package/dist/types/common/type/lexer.d.ts +1 -1
  19. package/dist/types/common/type/parse.d.ts +1 -1
  20. package/dist/types/common/type/parser.d.ts +1 -1
  21. package/dist/types/common/type/primitive.d.ts +1 -1
  22. package/dist/types/common/type/reduce.d.ts +1 -1
  23. package/dist/types/common/type/serialize.d.ts +1 -1
  24. package/dist/types/common/type/subtype.d.ts +1 -1
  25. package/dist/types/common/type/type-builder.d.ts +1 -1
  26. package/dist/types/common/type/types.d.ts +1 -1
  27. package/dist/types/common/type/utils.d.ts +1 -1
  28. package/dist/types/common/utils.d.ts +1 -1
  29. package/dist/types/compute-engine/assume.d.ts +1 -1
  30. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +14 -3
  31. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  32. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  33. package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
  34. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  35. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  36. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  37. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  38. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +3 -3
  39. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +3 -3
  40. package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
  41. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  42. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  43. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +3 -3
  44. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +1 -1
  45. package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +1 -1
  46. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  47. package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
  48. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  49. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  50. package/dist/types/compute-engine/boxed-expression/constants.d.ts +1 -1
  51. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  52. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  53. package/dist/types/compute-engine/boxed-expression/factor.d.ts +37 -4
  54. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/inequality-bounds.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/init-lazy-refs.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/invisible-operator.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/match.d.ts +2 -2
  60. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/pattern-utils.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/polynomial-degree.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  66. package/dist/types/compute-engine/boxed-expression/predicates.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  68. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  69. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  70. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  71. package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +1 -1
  72. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  73. package/dist/types/compute-engine/boxed-expression/stochastic-equal.d.ts +1 -1
  74. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  75. package/dist/types/compute-engine/boxed-expression/type-guards.d.ts +1 -1
  76. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  77. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  78. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  79. package/dist/types/compute-engine/compilation/base-compiler.d.ts +1 -1
  80. package/dist/types/compute-engine/compilation/compile-expression.d.ts +1 -1
  81. package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
  82. package/dist/types/compute-engine/compilation/gpu-target.d.ts +68 -1
  83. package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +1 -1
  84. package/dist/types/compute-engine/compilation/javascript-target.d.ts +1 -1
  85. package/dist/types/compute-engine/compilation/python-target.d.ts +1 -1
  86. package/dist/types/compute-engine/compilation/types.d.ts +9 -1
  87. package/dist/types/compute-engine/compilation/wgsl-target.d.ts +1 -1
  88. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  89. package/dist/types/compute-engine/engine-assumptions.d.ts +1 -1
  90. package/dist/types/compute-engine/engine-cache.d.ts +1 -1
  91. package/dist/types/compute-engine/engine-common-symbols.d.ts +1 -1
  92. package/dist/types/compute-engine/engine-compilation-targets.d.ts +1 -1
  93. package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +1 -1
  94. package/dist/types/compute-engine/engine-declarations.d.ts +1 -1
  95. package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +1 -1
  96. package/dist/types/compute-engine/engine-extension-contracts.d.ts +1 -1
  97. package/dist/types/compute-engine/engine-latex-dictionary-state.d.ts +1 -1
  98. package/dist/types/compute-engine/engine-library-bootstrap.d.ts +1 -1
  99. package/dist/types/compute-engine/engine-numeric-configuration.d.ts +1 -1
  100. package/dist/types/compute-engine/engine-parse-entrypoint.d.ts +1 -1
  101. package/dist/types/compute-engine/engine-runtime-state.d.ts +1 -1
  102. package/dist/types/compute-engine/engine-scope.d.ts +1 -1
  103. package/dist/types/compute-engine/engine-sequences.d.ts +1 -1
  104. package/dist/types/compute-engine/engine-simplification-rules.d.ts +1 -1
  105. package/dist/types/compute-engine/engine-startup-coordinator.d.ts +1 -1
  106. package/dist/types/compute-engine/engine-type-resolver.d.ts +1 -1
  107. package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +1 -1
  108. package/dist/types/compute-engine/free-functions.d.ts +1 -1
  109. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  110. package/dist/types/compute-engine/global-types.d.ts +1 -1
  111. package/dist/types/compute-engine/index.d.ts +1 -1
  112. package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
  113. package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
  114. package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
  115. package/dist/types/compute-engine/interval/index.d.ts +1 -1
  116. package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
  117. package/dist/types/compute-engine/interval/types.d.ts +1 -1
  118. package/dist/types/compute-engine/interval/util.d.ts +1 -1
  119. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  120. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  121. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  122. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  123. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  124. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  125. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  126. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  127. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  128. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  129. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  130. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  131. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  132. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-units.d.ts +1 -1
  133. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  134. package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +1 -1
  135. package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +1 -1
  136. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
  137. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  138. package/dist/types/compute-engine/latex-syntax/serialize-dms.d.ts +1 -1
  139. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  140. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  141. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  142. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  143. package/dist/types/compute-engine/latex-syntax/types.d.ts +1 -1
  144. package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
  145. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  146. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  147. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  148. package/dist/types/compute-engine/library/colors.d.ts +1 -1
  149. package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
  150. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  151. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  152. package/dist/types/compute-engine/library/core.d.ts +1 -1
  153. package/dist/types/compute-engine/library/fractals.d.ts +1 -1
  154. package/dist/types/compute-engine/library/library.d.ts +1 -1
  155. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  156. package/dist/types/compute-engine/library/logic-analysis.d.ts +1 -1
  157. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  158. package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
  159. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  160. package/dist/types/compute-engine/library/quantity-arithmetic.d.ts +1 -1
  161. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  162. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  163. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  164. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  165. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  166. package/dist/types/compute-engine/library/type-handlers.d.ts +1 -1
  167. package/dist/types/compute-engine/library/unit-data.d.ts +1 -1
  168. package/dist/types/compute-engine/library/units.d.ts +1 -1
  169. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  170. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  171. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  172. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  173. package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
  174. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  175. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  176. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  177. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  178. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  179. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +2 -2
  180. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  181. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  182. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  183. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  184. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  185. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  186. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  187. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  188. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  189. package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
  190. package/dist/types/compute-engine/oeis.d.ts +1 -1
  191. package/dist/types/compute-engine/sequence.d.ts +1 -1
  192. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  193. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  194. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  195. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  196. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  197. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  198. package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
  199. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  200. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  201. package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
  202. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  203. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  204. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  205. package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
  206. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  207. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  208. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  209. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  210. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  211. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  212. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  213. package/dist/types/compute-engine/types-definitions.d.ts +1 -1
  214. package/dist/types/compute-engine/types-engine.d.ts +1 -1
  215. package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
  216. package/dist/types/compute-engine/types-expression.d.ts +61 -2
  217. package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
  218. package/dist/types/compute-engine/types-kernel-serialization.d.ts +12 -1
  219. package/dist/types/compute-engine/types-serialization.d.ts +1 -1
  220. package/dist/types/compute-engine/types.d.ts +1 -1
  221. package/dist/types/compute-engine.d.ts +1 -1
  222. package/dist/types/math-json/symbols.d.ts +1 -1
  223. package/dist/types/math-json/types.d.ts +1 -1
  224. package/dist/types/math-json/utils.d.ts +1 -1
  225. package/dist/types/math-json.d.ts +2 -2
  226. package/package.json +3 -3
  227. package/dist/compute-engine.min.umd.js +0 -563
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.53.0 */
1
+ /** Compute Engine 0.54.0 */
2
2
  (function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ComputeEngine = {}));})(this, (function (exports) { 'use strict';
3
3
  var ComputeEngine = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -11144,7 +11144,7 @@ var ComputeEngine = (() => {
11144
11144
  condition: (p) => {
11145
11145
  if (until?.condition?.(p)) return true;
11146
11146
  const saved = p.index;
11147
- p.skipSpace();
11147
+ p.skipVisualSpace();
11148
11148
  const isComma = p.peek === ",";
11149
11149
  p.index = saved;
11150
11150
  return isComma;
@@ -11152,11 +11152,11 @@ var ComputeEngine = (() => {
11152
11152
  };
11153
11153
  const bindings = [];
11154
11154
  do {
11155
- parser.skipSpace();
11155
+ parser.skipVisualSpace();
11156
11156
  const binding = parser.parseExpression(bindingTerminator);
11157
11157
  if (!binding) break;
11158
11158
  bindings.push(binding);
11159
- parser.skipSpace();
11159
+ parser.skipVisualSpace();
11160
11160
  } while (parser.match(","));
11161
11161
  if (bindings.length === 0) return null;
11162
11162
  const block = [];
@@ -11655,14 +11655,11 @@ var ComputeEngine = (() => {
11655
11655
  let loop = n;
11656
11656
  let sum2 = n;
11657
11657
  let val = n;
11658
- let counter = 0;
11659
11658
  while (loop > 2) {
11660
11659
  loop -= BigInt(2);
11661
11660
  sum2 += loop;
11662
11661
  val *= sum2;
11663
- counter += 1;
11664
- if (counter % 5e4 === 0 || counter > 1e4 && counter % 500 === 0)
11665
- yield val;
11662
+ yield val;
11666
11663
  }
11667
11664
  return val;
11668
11665
  }
@@ -12354,6 +12351,18 @@ var ComputeEngine = (() => {
12354
12351
  function _setExpandForIs(fn) {
12355
12352
  _expandForIs = fn;
12356
12353
  }
12354
+ var _getPolynomialCoefficients;
12355
+ function _setGetPolynomialCoefficients(fn) {
12356
+ _getPolynomialCoefficients = fn;
12357
+ }
12358
+ var _getPolynomialDegree;
12359
+ function _setGetPolynomialDegree(fn) {
12360
+ _getPolynomialDegree = fn;
12361
+ }
12362
+ var _findUnivariateRoots;
12363
+ function _setFindUnivariateRoots(fn) {
12364
+ _findUnivariateRoots = fn;
12365
+ }
12357
12366
  var EXPANDABLE_OPS = ["Multiply", "Power", "Negate", "Divide"];
12358
12367
  function _couldBenefitFromExpand(a, b) {
12359
12368
  if (a.has(EXPANDABLE_OPS)) return true;
@@ -12669,6 +12678,34 @@ var ComputeEngine = (() => {
12669
12678
  factors() {
12670
12679
  return [this];
12671
12680
  }
12681
+ polynomialCoefficients(variable) {
12682
+ let vars;
12683
+ if (variable === void 0) {
12684
+ const unknowns = this.unknowns;
12685
+ if (unknowns.length !== 1) return void 0;
12686
+ vars = [unknowns[0]];
12687
+ } else if (typeof variable === "string") {
12688
+ vars = [variable];
12689
+ } else {
12690
+ if (variable.length === 0) return void 0;
12691
+ vars = variable;
12692
+ }
12693
+ for (const v of vars) {
12694
+ if (_getPolynomialDegree(this, v) < 0) return void 0;
12695
+ }
12696
+ const coeffs = _getPolynomialCoefficients(this, vars[0]);
12697
+ if (coeffs === null) return void 0;
12698
+ return coeffs.reverse();
12699
+ }
12700
+ polynomialRoots(variable) {
12701
+ if (variable === void 0) {
12702
+ const unknowns = this.unknowns;
12703
+ if (unknowns.length !== 1) return void 0;
12704
+ variable = unknowns[0];
12705
+ }
12706
+ if (_getPolynomialDegree(this, variable) < 0) return void 0;
12707
+ return _findUnivariateRoots(this, variable);
12708
+ }
12672
12709
  is(other, tolerance) {
12673
12710
  if (this.isSame(other)) return true;
12674
12711
  if (_expandForIs && _couldBenefitFromExpand(this, other)) {
@@ -16128,10 +16165,14 @@ ${lines.join("\n")}`;
16128
16165
  return expr.operator === "Sqrt" || expr.operator === "Power" && expr.op2.im === 0 && expr.op2.re === 0.5 || expr.operator === "Root" && expr.op2.im === 0 && expr.op2.re === 2;
16129
16166
  }
16130
16167
  function asRadical(expr) {
16131
- if (isSqrt(expr) && isFunction2(expr)) return asRational(expr.op1) ?? null;
16168
+ if (isSqrt(expr) && isFunction2(expr)) {
16169
+ const r = asRational(expr.op1);
16170
+ if (r === void 0 || r[0] < 0 || r[1] < 0) return null;
16171
+ return r;
16172
+ }
16132
16173
  if (isFunction2(expr, "Divide") && expr.op1.isSame(1) && isSqrt(expr.op2)) {
16133
16174
  const n = expr.op2.re;
16134
- if (!Number.isInteger(n)) return null;
16175
+ if (!Number.isInteger(n) || n <= 0) return null;
16135
16176
  return [1, n];
16136
16177
  }
16137
16178
  return null;
@@ -16731,13 +16772,14 @@ ${lines.join("\n")}`;
16731
16772
  function lcm3(a, b) {
16732
16773
  return a.mul(b).div(gcd3(a, b));
16733
16774
  }
16734
- function factorial23(ce, n) {
16775
+ function* factorial23(ce, n) {
16735
16776
  if (!n.isInteger() || n.isNegative()) return ce._BIGNUM_NAN;
16736
16777
  if (n.lessThan(1)) return ce._BIGNUM_ONE;
16737
16778
  let result = n;
16738
16779
  while (n.greaterThan(2)) {
16739
16780
  n = n.minus(2);
16740
16781
  result = result.mul(n);
16782
+ yield result;
16741
16783
  }
16742
16784
  return result;
16743
16785
  }
@@ -20254,24 +20296,30 @@ ${lines.join("\n")}`;
20254
20296
  if (initial.type.matches("real") && collection.type.matches(ce.type("collection<real>"))) {
20255
20297
  const compiled = ce._compile(fn);
20256
20298
  if (compiled.calling !== "lambda" || !compiled.run) return void 0;
20257
- let accumulator2 = initial.re;
20258
- let first2 = true;
20259
- for (const item of collection.each()) {
20260
- if (first2) accumulator2 = item.re;
20261
- else accumulator2 = compiled.run(accumulator2, item.re);
20262
- first2 = false;
20263
- }
20264
- return ce.box(accumulator2);
20299
+ return run(
20300
+ (function* () {
20301
+ let accumulator = initial.re;
20302
+ let first = true;
20303
+ for (const item of collection.each()) {
20304
+ if (first) accumulator = item.re;
20305
+ else accumulator = compiled.run(accumulator, item.re);
20306
+ first = false;
20307
+ yield;
20308
+ }
20309
+ return ce.box(accumulator);
20310
+ })(),
20311
+ ce._timeRemaining
20312
+ );
20265
20313
  }
20266
20314
  const f = applicable(fn);
20267
- let accumulator = initial;
20268
- let first = true;
20269
- for (const item of collection.each()) {
20270
- if (first) accumulator = item;
20271
- else accumulator = f([accumulator, item]) ?? ce.Nothing;
20272
- first = false;
20273
- }
20274
- return accumulator;
20315
+ return run(
20316
+ reduceCollection(
20317
+ collection,
20318
+ (acc, x) => f([acc, x]) ?? ce.Nothing,
20319
+ initial
20320
+ ),
20321
+ ce._timeRemaining
20322
+ );
20275
20323
  }
20276
20324
  },
20277
20325
  Join: {
@@ -21496,12 +21544,10 @@ ${lines.join("\n")}`;
21496
21544
  }
21497
21545
  function* reduceCollection(collection, fn, initial) {
21498
21546
  let acc = initial;
21499
- let counter = 0;
21500
21547
  for (const x of collection.each()) {
21501
21548
  const result = fn(acc, x);
21502
21549
  if (result === null) return void 0;
21503
- counter += 1;
21504
- if (counter % 1e3 === 0) yield acc;
21550
+ yield acc;
21505
21551
  acc = result;
21506
21552
  }
21507
21553
  return acc;
@@ -22920,12 +22966,10 @@ ${lines.join("\n")}`;
22920
22966
  const indexingSets = normalizeIndexingSets(indexes);
22921
22967
  const cartesianArray = indexingSetCartesianProduct(indexingSets);
22922
22968
  let result = initial;
22923
- let counter = 0;
22924
22969
  for (const element of cartesianArray) {
22925
22970
  indexingSets.forEach((x, i) => ce.assign(x.index, element[i]));
22926
22971
  result = fn(result, body) ?? void 0;
22927
- counter += 1;
22928
- if (counter % 1e3 === 0) yield result;
22972
+ yield result;
22929
22973
  if (result === void 0) break;
22930
22974
  }
22931
22975
  return result ?? void 0;
@@ -22992,7 +23036,6 @@ ${lines.join("\n")}`;
22992
23036
  return initial;
22993
23037
  }
22994
23038
  let result = initial;
22995
- let counter = 0;
22996
23039
  while (true) {
22997
23040
  for (let i = 0; i < elementDomains.length; i++) {
22998
23041
  ce.assign(
@@ -23001,8 +23044,7 @@ ${lines.join("\n")}`;
23001
23044
  );
23002
23045
  }
23003
23046
  result = fn(result, body) ?? void 0;
23004
- counter++;
23005
- if (counter % 1e3 === 0) yield result;
23047
+ yield result;
23006
23048
  if (result === void 0) break;
23007
23049
  let dim = elementDomains.length - 1;
23008
23050
  while (dim >= 0) {
@@ -23972,7 +24014,8 @@ ${lines.join("\n")}`;
23972
24014
  ce._timeRemaining
23973
24015
  )
23974
24016
  );
23975
- } catch {
24017
+ } catch (e) {
24018
+ if (e instanceof CancellationError) throw e;
23976
24019
  return void 0;
23977
24020
  }
23978
24021
  },
@@ -23991,7 +24034,8 @@ ${lines.join("\n")}`;
23991
24034
  signal
23992
24035
  )
23993
24036
  );
23994
- } catch {
24037
+ } catch (e) {
24038
+ if (e instanceof CancellationError) throw e;
23995
24039
  return void 0;
23996
24040
  }
23997
24041
  }
@@ -24009,7 +24053,9 @@ ${lines.join("\n")}`;
24009
24053
  if (n === null) return void 0;
24010
24054
  const ce = x.engine;
24011
24055
  if (bignumPreferred(ce))
24012
- return ce.number(factorial23(ce, ce.bignum(n)));
24056
+ return ce.number(
24057
+ run(factorial23(ce, ce.bignum(n)), ce._timeRemaining)
24058
+ );
24013
24059
  return ce.number(factorial22(n));
24014
24060
  }
24015
24061
  },
@@ -26410,6 +26456,268 @@ ${e.message}
26410
26456
  return expand2(result);
26411
26457
  }
26412
26458
 
26459
+ // src/compute-engine/boxed-expression/polynomials.ts
26460
+ function polynomialDegree(expr, variable) {
26461
+ if (isNumber(expr)) return 0;
26462
+ if (isSymbol2(expr)) {
26463
+ if (expr.symbol === variable) return 1;
26464
+ return 0;
26465
+ }
26466
+ if (!isFunction2(expr)) {
26467
+ if (expr.has(variable)) return -1;
26468
+ return 0;
26469
+ }
26470
+ const op = expr.operator;
26471
+ if (op === "Negate") return polynomialDegree(expr.op1, variable);
26472
+ if (op === "Add" || op === "Subtract") {
26473
+ let maxDeg = 0;
26474
+ for (const arg of expr.ops) {
26475
+ const deg = polynomialDegree(arg, variable);
26476
+ if (deg < 0) return -1;
26477
+ maxDeg = Math.max(maxDeg, deg);
26478
+ }
26479
+ return maxDeg;
26480
+ }
26481
+ if (op === "Multiply") {
26482
+ let totalDeg = 0;
26483
+ for (const arg of expr.ops) {
26484
+ const deg = polynomialDegree(arg, variable);
26485
+ if (deg < 0) return -1;
26486
+ totalDeg += deg;
26487
+ }
26488
+ return totalDeg;
26489
+ }
26490
+ if (op === "Power") {
26491
+ const baseDeg = polynomialDegree(expr.op1, variable);
26492
+ if (baseDeg < 0) return -1;
26493
+ if (baseDeg === 0) {
26494
+ if (expr.op2.has(variable)) return -1;
26495
+ return 0;
26496
+ }
26497
+ const exp3 = asSmallInteger(expr.op2);
26498
+ if (exp3 === null || exp3 < 0) return -1;
26499
+ return baseDeg * exp3;
26500
+ }
26501
+ if (expr.has(variable)) return -1;
26502
+ return 0;
26503
+ }
26504
+ function getPolynomialCoefficients(expr, variable) {
26505
+ const ce = expr.engine;
26506
+ const degree = polynomialDegree(expr, variable);
26507
+ if (degree < 0) return null;
26508
+ const coeffs = new Array(degree + 1).fill(ce.Zero);
26509
+ const expanded = expand2(expr);
26510
+ const addCoefficient = (coef, deg) => {
26511
+ if (deg > degree) return false;
26512
+ coeffs[deg] = coeffs[deg].add(coef);
26513
+ return true;
26514
+ };
26515
+ const processTerm = (term) => {
26516
+ const termDeg = polynomialDegree(term, variable);
26517
+ if (termDeg < 0) return false;
26518
+ if (termDeg === 0) {
26519
+ return addCoefficient(term, 0);
26520
+ }
26521
+ if (isSymbol2(term, variable)) {
26522
+ return addCoefficient(ce.One, 1);
26523
+ }
26524
+ if (isFunction2(term, "Negate")) {
26525
+ const innerDeg = polynomialDegree(term.op1, variable);
26526
+ if (innerDeg === 0) {
26527
+ return addCoefficient(term, 0);
26528
+ }
26529
+ const innerCoeffs = getPolynomialCoefficients(term.op1, variable);
26530
+ if (!innerCoeffs) return false;
26531
+ for (let i = 0; i < innerCoeffs.length; i++) {
26532
+ if (!innerCoeffs[i].isSame(0)) {
26533
+ addCoefficient(innerCoeffs[i].neg(), i);
26534
+ }
26535
+ }
26536
+ return true;
26537
+ }
26538
+ if (isFunction2(term, "Power")) {
26539
+ if (isSymbol2(term.op1, variable)) {
26540
+ const exp3 = asSmallInteger(term.op2);
26541
+ if (exp3 !== null && exp3 >= 0) {
26542
+ return addCoefficient(ce.One, exp3);
26543
+ }
26544
+ }
26545
+ if (!term.op1.has(variable)) {
26546
+ return addCoefficient(term, 0);
26547
+ }
26548
+ return false;
26549
+ }
26550
+ if (isFunction2(term, "Multiply")) {
26551
+ const factors = term.ops;
26552
+ let coef = ce.One;
26553
+ let varDeg = 0;
26554
+ for (const factor3 of factors) {
26555
+ if (!factor3.has(variable)) {
26556
+ coef = coef.mul(factor3);
26557
+ } else if (isSymbol2(factor3, variable)) {
26558
+ varDeg += 1;
26559
+ } else if (isFunction2(factor3, "Power") && isSymbol2(factor3.op1, variable)) {
26560
+ const exp3 = asSmallInteger(factor3.op2);
26561
+ if (exp3 !== null && exp3 >= 0) {
26562
+ varDeg += exp3;
26563
+ } else {
26564
+ return false;
26565
+ }
26566
+ } else {
26567
+ return false;
26568
+ }
26569
+ }
26570
+ return addCoefficient(coef, varDeg);
26571
+ }
26572
+ return false;
26573
+ };
26574
+ if (isFunction2(expanded, "Add")) {
26575
+ for (const term of expanded.ops) {
26576
+ if (!processTerm(term)) return null;
26577
+ }
26578
+ } else {
26579
+ if (!processTerm(expanded)) return null;
26580
+ }
26581
+ return coeffs;
26582
+ }
26583
+ function fromCoefficients(coeffs, variable) {
26584
+ if (coeffs.length === 0) return coeffs[0]?.engine.Zero ?? null;
26585
+ const ce = coeffs[0].engine;
26586
+ const x = ce.symbol(variable);
26587
+ const terms = [];
26588
+ for (let i = 0; i < coeffs.length; i++) {
26589
+ const coef = coeffs[i];
26590
+ if (coef.isSame(0)) continue;
26591
+ if (i === 0) {
26592
+ terms.push(coef);
26593
+ } else if (i === 1) {
26594
+ if (coef.isSame(1)) {
26595
+ terms.push(x);
26596
+ } else if (coef.isSame(-1)) {
26597
+ terms.push(x.neg());
26598
+ } else {
26599
+ terms.push(coef.mul(x));
26600
+ }
26601
+ } else {
26602
+ const xPow = ce.box(["Power", variable, i]);
26603
+ if (coef.isSame(1)) {
26604
+ terms.push(xPow);
26605
+ } else if (coef.isSame(-1)) {
26606
+ terms.push(xPow.neg());
26607
+ } else {
26608
+ terms.push(coef.mul(xPow));
26609
+ }
26610
+ }
26611
+ }
26612
+ if (terms.length === 0) return ce.Zero;
26613
+ if (terms.length === 1) return terms[0];
26614
+ return add3(...terms);
26615
+ }
26616
+ function polynomialDivide(dividend, divisor, variable) {
26617
+ const ce = dividend.engine;
26618
+ const dividendCoeffs = getPolynomialCoefficients(dividend, variable);
26619
+ const divisorCoeffs = getPolynomialCoefficients(divisor, variable);
26620
+ if (!dividendCoeffs || !divisorCoeffs) return null;
26621
+ if (divisorCoeffs.every((c) => c.isSame(0))) return null;
26622
+ const actualDegree = (coeffs) => {
26623
+ for (let i = coeffs.length - 1; i >= 0; i--) {
26624
+ if (!coeffs[i].isSame(0)) return i;
26625
+ }
26626
+ return -1;
26627
+ };
26628
+ const dividendDeg = actualDegree(dividendCoeffs);
26629
+ const divisorDeg = actualDegree(divisorCoeffs);
26630
+ if (divisorDeg < 0) return null;
26631
+ if (dividendDeg < 0) {
26632
+ return [ce.Zero, ce.Zero];
26633
+ }
26634
+ if (dividendDeg < divisorDeg) {
26635
+ return [ce.Zero, dividend];
26636
+ }
26637
+ const remainder2 = dividendCoeffs.map((c) => c);
26638
+ const quotientCoeffs = new Array(
26639
+ dividendDeg - divisorDeg + 1
26640
+ ).fill(ce.Zero);
26641
+ const leadingDivisor = divisorCoeffs[divisorDeg];
26642
+ for (let i = dividendDeg; i >= divisorDeg; i--) {
26643
+ if (remainder2[i].isSame(0)) continue;
26644
+ const quotientCoef = remainder2[i].div(leadingDivisor);
26645
+ quotientCoeffs[i - divisorDeg] = quotientCoef;
26646
+ for (let j = 0; j <= divisorDeg; j++) {
26647
+ const product = quotientCoef.mul(divisorCoeffs[j]);
26648
+ remainder2[i - divisorDeg + j] = remainder2[i - divisorDeg + j].sub(product);
26649
+ }
26650
+ }
26651
+ const quotient = fromCoefficients(quotientCoeffs, variable);
26652
+ const remainderPoly = fromCoefficients(remainder2, variable);
26653
+ return [quotient, remainderPoly];
26654
+ }
26655
+ function polynomialGCD(a, b, variable) {
26656
+ const ce = a.engine;
26657
+ const degA = polynomialDegree(a, variable);
26658
+ const degB = polynomialDegree(b, variable);
26659
+ if (degA < 0 || degB < 0) return ce.One;
26660
+ const aCoeffs = getPolynomialCoefficients(a, variable);
26661
+ const bCoeffs = getPolynomialCoefficients(b, variable);
26662
+ if (!aCoeffs || aCoeffs.every((c) => c.isSame(0))) {
26663
+ return makeMonic(b, variable);
26664
+ }
26665
+ if (!bCoeffs || bCoeffs.every((c) => c.isSame(0))) {
26666
+ return makeMonic(a, variable);
26667
+ }
26668
+ let p = a;
26669
+ let q = b;
26670
+ while (true) {
26671
+ const qCoeffs = getPolynomialCoefficients(q, variable);
26672
+ if (!qCoeffs || qCoeffs.every((c) => c.isSame(0))) {
26673
+ break;
26674
+ }
26675
+ const divResult = polynomialDivide(p, q, variable);
26676
+ if (!divResult) {
26677
+ return ce.One;
26678
+ }
26679
+ const [, remainder2] = divResult;
26680
+ p = q;
26681
+ q = remainder2;
26682
+ }
26683
+ return makeMonic(p, variable);
26684
+ }
26685
+ function makeMonic(poly, variable) {
26686
+ const coeffs = getPolynomialCoefficients(poly, variable);
26687
+ if (!coeffs) return poly;
26688
+ let leadingCoef = null;
26689
+ for (let i = coeffs.length - 1; i >= 0; i--) {
26690
+ if (!coeffs[i].isSame(0)) {
26691
+ leadingCoef = coeffs[i];
26692
+ break;
26693
+ }
26694
+ }
26695
+ if (!leadingCoef || leadingCoef.isSame(1)) return poly;
26696
+ const monicCoeffs = coeffs.map((c) => c.div(leadingCoef));
26697
+ return fromCoefficients(monicCoeffs, variable);
26698
+ }
26699
+ function cancelCommonFactors(expr, variable) {
26700
+ if (!isFunction2(expr, "Divide")) return expr;
26701
+ const numerator = expr.op1;
26702
+ const denominator = expr.op2;
26703
+ const numDeg = polynomialDegree(numerator, variable);
26704
+ const denDeg = polynomialDegree(denominator, variable);
26705
+ if (numDeg < 0 || denDeg < 0) return expr;
26706
+ const gcd7 = polynomialGCD(numerator, denominator, variable);
26707
+ const gcdDeg = polynomialDegree(gcd7, variable);
26708
+ if (gcdDeg <= 0) return expr;
26709
+ const numDivResult = polynomialDivide(numerator, gcd7, variable);
26710
+ const denDivResult = polynomialDivide(denominator, gcd7, variable);
26711
+ if (!numDivResult || !denDivResult) return expr;
26712
+ const [newNumerator] = numDivResult;
26713
+ const [newDenominator] = denDivResult;
26714
+ const denCoeffs = getPolynomialCoefficients(newDenominator, variable);
26715
+ if (denCoeffs && denCoeffs.length === 1 && denCoeffs[0].isSame(1)) {
26716
+ return newNumerator;
26717
+ }
26718
+ return newNumerator.div(newDenominator);
26719
+ }
26720
+
26413
26721
  // src/compute-engine/boxed-expression/solve.ts
26414
26722
  function numericApproximation(value) {
26415
26723
  if (typeof value === "number") return value;
@@ -27233,6 +27541,13 @@ ${e.message}
27233
27541
  )
27234
27542
  );
27235
27543
  }
27544
+ if (result.length === 0) {
27545
+ const deg = polynomialDegree(originalExpr, x);
27546
+ if (deg >= 3) {
27547
+ const rationalRoots = findRationalRoots(originalExpr, x, ce);
27548
+ if (rationalRoots.length > 0) result = rationalRoots;
27549
+ }
27550
+ }
27236
27551
  } finally {
27237
27552
  ce.popScope();
27238
27553
  }
@@ -27371,267 +27686,49 @@ ${e.message}
27371
27686
  return true;
27372
27687
  });
27373
27688
  }
27374
-
27375
- // src/compute-engine/boxed-expression/polynomials.ts
27376
- function polynomialDegree(expr, variable) {
27377
- if (isNumber(expr)) return 0;
27378
- if (isSymbol2(expr)) {
27379
- if (expr.symbol === variable) return 1;
27380
- return 0;
27381
- }
27382
- if (!isFunction2(expr)) {
27383
- if (expr.has(variable)) return -1;
27384
- return 0;
27385
- }
27386
- const op = expr.operator;
27387
- if (op === "Negate") return polynomialDegree(expr.op1, variable);
27388
- if (op === "Add" || op === "Subtract") {
27389
- let maxDeg = 0;
27390
- for (const arg of expr.ops) {
27391
- const deg = polynomialDegree(arg, variable);
27392
- if (deg < 0) return -1;
27393
- maxDeg = Math.max(maxDeg, deg);
27394
- }
27395
- return maxDeg;
27396
- }
27397
- if (op === "Multiply") {
27398
- let totalDeg = 0;
27399
- for (const arg of expr.ops) {
27400
- const deg = polynomialDegree(arg, variable);
27401
- if (deg < 0) return -1;
27402
- totalDeg += deg;
27403
- }
27404
- return totalDeg;
27405
- }
27406
- if (op === "Power") {
27407
- const baseDeg = polynomialDegree(expr.op1, variable);
27408
- if (baseDeg < 0) return -1;
27409
- if (baseDeg === 0) {
27410
- if (expr.op2.has(variable)) return -1;
27411
- return 0;
27412
- }
27413
- const exp3 = asSmallInteger(expr.op2);
27414
- if (exp3 === null || exp3 < 0) return -1;
27415
- return baseDeg * exp3;
27416
- }
27417
- if (expr.has(variable)) return -1;
27418
- return 0;
27419
- }
27420
- function getPolynomialCoefficients(expr, variable) {
27421
- const ce = expr.engine;
27422
- const degree = polynomialDegree(expr, variable);
27423
- if (degree < 0) return null;
27424
- const coeffs = new Array(degree + 1).fill(ce.Zero);
27425
- const expanded = expand2(expr);
27426
- const addCoefficient = (coef, deg) => {
27427
- if (deg > degree) return false;
27428
- coeffs[deg] = coeffs[deg].add(coef);
27429
- return true;
27430
- };
27431
- const processTerm = (term) => {
27432
- const termDeg = polynomialDegree(term, variable);
27433
- if (termDeg < 0) return false;
27434
- if (termDeg === 0) {
27435
- return addCoefficient(term, 0);
27436
- }
27437
- if (isSymbol2(term, variable)) {
27438
- return addCoefficient(ce.One, 1);
27439
- }
27440
- if (isFunction2(term, "Negate")) {
27441
- const innerDeg = polynomialDegree(term.op1, variable);
27442
- if (innerDeg === 0) {
27443
- return addCoefficient(term, 0);
27444
- }
27445
- const innerCoeffs = getPolynomialCoefficients(term.op1, variable);
27446
- if (!innerCoeffs) return false;
27447
- for (let i = 0; i < innerCoeffs.length; i++) {
27448
- if (!innerCoeffs[i].isSame(0)) {
27449
- addCoefficient(innerCoeffs[i].neg(), i);
27450
- }
27451
- }
27452
- return true;
27453
- }
27454
- if (isFunction2(term, "Power")) {
27455
- if (isSymbol2(term.op1, variable)) {
27456
- const exp3 = asSmallInteger(term.op2);
27457
- if (exp3 !== null && exp3 >= 0) {
27458
- return addCoefficient(ce.One, exp3);
27459
- }
27460
- }
27461
- if (!term.op1.has(variable)) {
27462
- return addCoefficient(term, 0);
27689
+ function findRationalRoots(expr, variable, ce) {
27690
+ const coeffs = getPolynomialCoefficients(expr, variable);
27691
+ if (!coeffs) return [];
27692
+ const degree = coeffs.length - 1;
27693
+ if (degree < 1) return [];
27694
+ const constantInt = asSmallInteger(coeffs[0]);
27695
+ const leadingInt = asSmallInteger(coeffs[degree]);
27696
+ if (leadingInt === null || constantInt === null) return [];
27697
+ if (leadingInt === 0 || constantInt === 0) return [];
27698
+ const divisors = (n) => {
27699
+ n = Math.abs(n);
27700
+ const result = [];
27701
+ for (let i = 1; i * i <= n; i++) {
27702
+ if (n % i === 0) {
27703
+ result.push(i);
27704
+ if (i !== n / i) result.push(n / i);
27463
27705
  }
27464
- return false;
27465
27706
  }
27466
- if (isFunction2(term, "Multiply")) {
27467
- const factors = term.ops;
27468
- let coef = ce.One;
27469
- let varDeg = 0;
27470
- for (const factor3 of factors) {
27471
- if (!factor3.has(variable)) {
27472
- coef = coef.mul(factor3);
27473
- } else if (isSymbol2(factor3, variable)) {
27474
- varDeg += 1;
27475
- } else if (isFunction2(factor3, "Power") && isSymbol2(factor3.op1, variable)) {
27476
- const exp3 = asSmallInteger(factor3.op2);
27477
- if (exp3 !== null && exp3 >= 0) {
27478
- varDeg += exp3;
27479
- } else {
27480
- return false;
27481
- }
27482
- } else {
27483
- return false;
27707
+ return result;
27708
+ };
27709
+ const pDivisors = divisors(constantInt);
27710
+ const qDivisors = divisors(leadingInt);
27711
+ const candidates = [];
27712
+ const seen = /* @__PURE__ */ new Set();
27713
+ for (const p of pDivisors) {
27714
+ for (const q of qDivisors) {
27715
+ for (const sign3 of [1, -1]) {
27716
+ const val = sign3 * p / q;
27717
+ if (!seen.has(val)) {
27718
+ seen.add(val);
27719
+ candidates.push(val);
27484
27720
  }
27485
27721
  }
27486
- return addCoefficient(coef, varDeg);
27487
- }
27488
- return false;
27489
- };
27490
- if (isFunction2(expanded, "Add")) {
27491
- for (const term of expanded.ops) {
27492
- if (!processTerm(term)) return null;
27493
27722
  }
27494
- } else {
27495
- if (!processTerm(expanded)) return null;
27496
27723
  }
27497
- return coeffs;
27498
- }
27499
- function fromCoefficients(coeffs, variable) {
27500
- if (coeffs.length === 0) return coeffs[0]?.engine.Zero ?? null;
27501
- const ce = coeffs[0].engine;
27502
- const x = ce.symbol(variable);
27503
- const terms = [];
27504
- for (let i = 0; i < coeffs.length; i++) {
27505
- const coef = coeffs[i];
27506
- if (coef.isSame(0)) continue;
27507
- if (i === 0) {
27508
- terms.push(coef);
27509
- } else if (i === 1) {
27510
- if (coef.isSame(1)) {
27511
- terms.push(x);
27512
- } else if (coef.isSame(-1)) {
27513
- terms.push(x.neg());
27514
- } else {
27515
- terms.push(coef.mul(x));
27516
- }
27517
- } else {
27518
- const xPow = ce.box(["Power", variable, i]);
27519
- if (coef.isSame(1)) {
27520
- terms.push(xPow);
27521
- } else if (coef.isSame(-1)) {
27522
- terms.push(xPow.neg());
27523
- } else {
27524
- terms.push(coef.mul(xPow));
27525
- }
27526
- }
27724
+ if (candidates.length > 100) return [];
27725
+ const roots = [];
27726
+ for (const candidate of candidates) {
27727
+ const root2 = ce.number(candidate);
27728
+ const value = expr.subs({ [variable]: root2 }).N();
27729
+ if (value.isSame(0)) roots.push(root2);
27527
27730
  }
27528
- if (terms.length === 0) return ce.Zero;
27529
- if (terms.length === 1) return terms[0];
27530
- return add3(...terms);
27531
- }
27532
- function polynomialDivide(dividend, divisor, variable) {
27533
- const ce = dividend.engine;
27534
- const dividendCoeffs = getPolynomialCoefficients(dividend, variable);
27535
- const divisorCoeffs = getPolynomialCoefficients(divisor, variable);
27536
- if (!dividendCoeffs || !divisorCoeffs) return null;
27537
- if (divisorCoeffs.every((c) => c.isSame(0))) return null;
27538
- const actualDegree = (coeffs) => {
27539
- for (let i = coeffs.length - 1; i >= 0; i--) {
27540
- if (!coeffs[i].isSame(0)) return i;
27541
- }
27542
- return -1;
27543
- };
27544
- const dividendDeg = actualDegree(dividendCoeffs);
27545
- const divisorDeg = actualDegree(divisorCoeffs);
27546
- if (divisorDeg < 0) return null;
27547
- if (dividendDeg < 0) {
27548
- return [ce.Zero, ce.Zero];
27549
- }
27550
- if (dividendDeg < divisorDeg) {
27551
- return [ce.Zero, dividend];
27552
- }
27553
- const remainder2 = dividendCoeffs.map((c) => c);
27554
- const quotientCoeffs = new Array(
27555
- dividendDeg - divisorDeg + 1
27556
- ).fill(ce.Zero);
27557
- const leadingDivisor = divisorCoeffs[divisorDeg];
27558
- for (let i = dividendDeg; i >= divisorDeg; i--) {
27559
- if (remainder2[i].isSame(0)) continue;
27560
- const quotientCoef = remainder2[i].div(leadingDivisor);
27561
- quotientCoeffs[i - divisorDeg] = quotientCoef;
27562
- for (let j = 0; j <= divisorDeg; j++) {
27563
- const product = quotientCoef.mul(divisorCoeffs[j]);
27564
- remainder2[i - divisorDeg + j] = remainder2[i - divisorDeg + j].sub(product);
27565
- }
27566
- }
27567
- const quotient = fromCoefficients(quotientCoeffs, variable);
27568
- const remainderPoly = fromCoefficients(remainder2, variable);
27569
- return [quotient, remainderPoly];
27570
- }
27571
- function polynomialGCD(a, b, variable) {
27572
- const ce = a.engine;
27573
- const degA = polynomialDegree(a, variable);
27574
- const degB = polynomialDegree(b, variable);
27575
- if (degA < 0 || degB < 0) return ce.One;
27576
- const aCoeffs = getPolynomialCoefficients(a, variable);
27577
- const bCoeffs = getPolynomialCoefficients(b, variable);
27578
- if (!aCoeffs || aCoeffs.every((c) => c.isSame(0))) {
27579
- return makeMonic(b, variable);
27580
- }
27581
- if (!bCoeffs || bCoeffs.every((c) => c.isSame(0))) {
27582
- return makeMonic(a, variable);
27583
- }
27584
- let p = a;
27585
- let q = b;
27586
- while (true) {
27587
- const qCoeffs = getPolynomialCoefficients(q, variable);
27588
- if (!qCoeffs || qCoeffs.every((c) => c.isSame(0))) {
27589
- break;
27590
- }
27591
- const divResult = polynomialDivide(p, q, variable);
27592
- if (!divResult) {
27593
- return ce.One;
27594
- }
27595
- const [, remainder2] = divResult;
27596
- p = q;
27597
- q = remainder2;
27598
- }
27599
- return makeMonic(p, variable);
27600
- }
27601
- function makeMonic(poly, variable) {
27602
- const coeffs = getPolynomialCoefficients(poly, variable);
27603
- if (!coeffs) return poly;
27604
- let leadingCoef = null;
27605
- for (let i = coeffs.length - 1; i >= 0; i--) {
27606
- if (!coeffs[i].isSame(0)) {
27607
- leadingCoef = coeffs[i];
27608
- break;
27609
- }
27610
- }
27611
- if (!leadingCoef || leadingCoef.isSame(1)) return poly;
27612
- const monicCoeffs = coeffs.map((c) => c.div(leadingCoef));
27613
- return fromCoefficients(monicCoeffs, variable);
27614
- }
27615
- function cancelCommonFactors(expr, variable) {
27616
- if (!isFunction2(expr, "Divide")) return expr;
27617
- const numerator = expr.op1;
27618
- const denominator = expr.op2;
27619
- const numDeg = polynomialDegree(numerator, variable);
27620
- const denDeg = polynomialDegree(denominator, variable);
27621
- if (numDeg < 0 || denDeg < 0) return expr;
27622
- const gcd6 = polynomialGCD(numerator, denominator, variable);
27623
- const gcdDeg = polynomialDegree(gcd6, variable);
27624
- if (gcdDeg <= 0) return expr;
27625
- const numDivResult = polynomialDivide(numerator, gcd6, variable);
27626
- const denDivResult = polynomialDivide(denominator, gcd6, variable);
27627
- if (!numDivResult || !denDivResult) return expr;
27628
- const [newNumerator] = numDivResult;
27629
- const [newDenominator] = denDivResult;
27630
- const denCoeffs = getPolynomialCoefficients(newDenominator, variable);
27631
- if (denCoeffs && denCoeffs.length === 1 && denCoeffs[0].isSame(1)) {
27632
- return newNumerator;
27633
- }
27634
- return newNumerator.div(newDenominator);
27731
+ return roots;
27635
27732
  }
27636
27733
 
27637
27734
  // src/compute-engine/symbolic/antiderivative.ts
@@ -33600,7 +33697,7 @@ ${e.message}
33600
33697
  if (isFunction2(result, "Break")) return result.op1;
33601
33698
  if (result.operator === "Return") return result;
33602
33699
  i2 += 1;
33603
- if (i2 % 1e3 === 0) yield result;
33700
+ yield result;
33604
33701
  if (i2 > ce.iterationLimit)
33605
33702
  throw new CancellationError({ cause: "iteration-limit-exceeded" });
33606
33703
  }
@@ -33612,7 +33709,7 @@ ${e.message}
33612
33709
  if (isFunction2(result, "Break")) return result.op1;
33613
33710
  if (result.operator === "Return") return result;
33614
33711
  i += 1;
33615
- if (i % 1e3 === 0) yield result;
33712
+ yield result;
33616
33713
  if (i > ce.iterationLimit)
33617
33714
  throw new CancellationError({ cause: "iteration-limit-exceeded" });
33618
33715
  }
@@ -38275,7 +38372,107 @@ ${e.message}
38275
38372
  return ce.box(["Multiply", a.json, factor1.json, factor22.json]);
38276
38373
  }
38277
38374
  }
38375
+ function factorByRationalRoots(expr, variable) {
38376
+ const ce = expr.engine;
38377
+ const coeffs = getPolynomialCoefficients(expr, variable);
38378
+ if (!coeffs) return null;
38379
+ const degree = coeffs.length - 1;
38380
+ if (degree < 2) return null;
38381
+ const leadingInt = asSmallInteger(coeffs[degree]);
38382
+ const constantInt = asSmallInteger(coeffs[0]);
38383
+ if (leadingInt === null || constantInt === null) return null;
38384
+ if (leadingInt === 0 || constantInt === 0) return null;
38385
+ const divisors = (n) => {
38386
+ n = Math.abs(n);
38387
+ const result = [];
38388
+ for (let i = 1; i * i <= n; i++) {
38389
+ if (n % i === 0) {
38390
+ result.push(i);
38391
+ if (i !== n / i) result.push(n / i);
38392
+ }
38393
+ }
38394
+ return result;
38395
+ };
38396
+ const pDivisors = divisors(constantInt);
38397
+ const qDivisors = divisors(leadingInt);
38398
+ const candidates = [];
38399
+ const seen = /* @__PURE__ */ new Set();
38400
+ for (const p of pDivisors) {
38401
+ for (const q of qDivisors) {
38402
+ const pos = p / q;
38403
+ const neg2 = -p / q;
38404
+ if (!seen.has(pos)) {
38405
+ seen.add(pos);
38406
+ candidates.push([p, q]);
38407
+ }
38408
+ if (!seen.has(neg2)) {
38409
+ seen.add(neg2);
38410
+ candidates.push([-p, q]);
38411
+ }
38412
+ }
38413
+ }
38414
+ if (candidates.length > 100) return null;
38415
+ const x = ce.symbol(variable);
38416
+ const factors = [];
38417
+ let remaining = expr;
38418
+ for (const [p, q] of candidates) {
38419
+ const remDeg2 = polynomialDegree(remaining, variable);
38420
+ if (remDeg2 <= 0) break;
38421
+ const root2 = q === 1 ? ce.number(p) : ce.number(p).div(ce.number(q));
38422
+ const value = remaining.subs({ [variable]: root2 }).N();
38423
+ if (!value.isSame(0)) continue;
38424
+ const linearFactor = q === 1 ? x.sub(ce.number(p)) : ce.number(q).mul(x).sub(ce.number(p));
38425
+ const divResult = polynomialDivide(remaining, linearFactor, variable);
38426
+ if (!divResult) continue;
38427
+ factors.push(linearFactor);
38428
+ remaining = divResult[0];
38429
+ }
38430
+ if (factors.length === 0) return null;
38431
+ const remDeg = polynomialDegree(remaining, variable);
38432
+ if (remDeg === 2) {
38433
+ const quadFactored = factorQuadratic(remaining, variable);
38434
+ if (quadFactored !== null) remaining = quadFactored;
38435
+ }
38436
+ factors.push(remaining);
38437
+ if (factors.length === 1) return factors[0];
38438
+ return ce.box(["Multiply", ...factors.map((f) => f.json)]);
38439
+ }
38440
+ function extractContent(expr, variable) {
38441
+ const ce = expr.engine;
38442
+ const coeffs = getPolynomialCoefficients(expr, variable);
38443
+ if (!coeffs) return null;
38444
+ const intCoeffs = [];
38445
+ for (const c of coeffs) {
38446
+ const n = asSmallInteger(c);
38447
+ if (n === null) return null;
38448
+ intCoeffs.push(n);
38449
+ }
38450
+ const gcd7 = (a, b) => {
38451
+ a = Math.abs(a);
38452
+ b = Math.abs(b);
38453
+ while (b) {
38454
+ [a, b] = [b, a % b];
38455
+ }
38456
+ return a;
38457
+ };
38458
+ let content = 0;
38459
+ for (const c of intCoeffs) {
38460
+ if (c !== 0) content = gcd7(content, c);
38461
+ }
38462
+ if (content <= 1) return null;
38463
+ const primitiveCoeffs = coeffs.map((c) => {
38464
+ const n = asSmallInteger(c);
38465
+ return ce.number(n / content);
38466
+ });
38467
+ const primitive = fromCoefficients(primitiveCoeffs, variable);
38468
+ const factoredPrimitive = factorPolynomial(primitive, variable);
38469
+ return ce.number(content).mul(factoredPrimitive);
38470
+ }
38278
38471
  function factorPolynomial(expr, variable) {
38472
+ if (variable !== void 0) {
38473
+ const contentFactored = extractContent(expr, variable);
38474
+ if (contentFactored !== null) return contentFactored;
38475
+ }
38279
38476
  const perfectSquare = factorPerfectSquare(expr);
38280
38477
  if (perfectSquare !== null) return perfectSquare;
38281
38478
  const diffSquares = factorDifferenceOfSquares(expr);
@@ -38283,6 +38480,8 @@ ${e.message}
38283
38480
  if (variable !== void 0) {
38284
38481
  const quadratic = factorQuadratic(expr, variable);
38285
38482
  if (quadratic !== null) return quadratic;
38483
+ const rationalRoot = factorByRationalRoots(expr, variable);
38484
+ if (rationalRoot !== null) return rationalRoot;
38286
38485
  }
38287
38486
  return factor(expr);
38288
38487
  }
@@ -38328,6 +38527,257 @@ ${e.message}
38328
38527
  }
38329
38528
  return Product.from(together(expr)).asExpression();
38330
38529
  }
38530
+ function collectFactors(expr, variable) {
38531
+ const rawFactors = [];
38532
+ collectFactorsRaw(expr, variable, rawFactors);
38533
+ const merged = [];
38534
+ for (const f of rawFactors) {
38535
+ let found = false;
38536
+ for (const m of merged) {
38537
+ if (m.factor.isSame(f.factor)) {
38538
+ m.multiplicity += f.multiplicity;
38539
+ found = true;
38540
+ break;
38541
+ }
38542
+ }
38543
+ if (!found) merged.push({ ...f });
38544
+ }
38545
+ return merged;
38546
+ }
38547
+ function collectFactorsRaw(expr, variable, result) {
38548
+ if (isFunction2(expr, "Multiply")) {
38549
+ for (const op of expr.ops) {
38550
+ collectFactorsRaw(op, variable, result);
38551
+ }
38552
+ return;
38553
+ }
38554
+ if (isFunction2(expr, "Power")) {
38555
+ const base = expr.op1;
38556
+ const exp3 = asSmallInteger(expr.op2);
38557
+ if (exp3 !== null && exp3 > 0 && base.has(variable)) {
38558
+ const deg3 = polynomialDegree(base, variable);
38559
+ result.push({ factor: base, multiplicity: exp3, degree: deg3 });
38560
+ return;
38561
+ }
38562
+ if (!expr.has(variable)) return;
38563
+ const deg2 = polynomialDegree(expr, variable);
38564
+ result.push({ factor: expr, multiplicity: 1, degree: deg2 });
38565
+ return;
38566
+ }
38567
+ if (!expr.has(variable)) return;
38568
+ if (isNumber(expr)) return;
38569
+ const deg = polynomialDegree(expr, variable);
38570
+ result.push({ factor: expr, multiplicity: 1, degree: deg });
38571
+ }
38572
+ function solveLinearSystem(matrix, numVars) {
38573
+ const rows = matrix.length;
38574
+ const cols = numVars + 1;
38575
+ const m = matrix.map((row) => [...row]);
38576
+ const pivotRow = new Array(numVars).fill(-1);
38577
+ let currentRow = 0;
38578
+ for (let col = 0; col < numVars && currentRow < rows; col++) {
38579
+ let maxVal = 0;
38580
+ let maxRow = -1;
38581
+ for (let row = currentRow; row < rows; row++) {
38582
+ const absVal = Math.abs(m[row][col]);
38583
+ if (absVal > maxVal) {
38584
+ maxVal = absVal;
38585
+ maxRow = row;
38586
+ }
38587
+ }
38588
+ if (maxVal === 0) continue;
38589
+ if (maxRow !== currentRow) {
38590
+ [m[currentRow], m[maxRow]] = [m[maxRow], m[currentRow]];
38591
+ }
38592
+ pivotRow[col] = currentRow;
38593
+ for (let row = 0; row < rows; row++) {
38594
+ if (row === currentRow) continue;
38595
+ if (m[row][col] === 0) continue;
38596
+ const factor3 = m[row][col];
38597
+ const pivotVal = m[currentRow][col];
38598
+ for (let j = 0; j < cols; j++) {
38599
+ m[row][j] = m[row][j] * pivotVal - factor3 * m[currentRow][j];
38600
+ }
38601
+ }
38602
+ currentRow++;
38603
+ }
38604
+ const solution = new Array(numVars);
38605
+ for (let col = 0; col < numVars; col++) {
38606
+ const pr = pivotRow[col];
38607
+ if (pr === -1) {
38608
+ solution[col] = [0, 1];
38609
+ continue;
38610
+ }
38611
+ const num = m[pr][cols - 1];
38612
+ const den = m[pr][col];
38613
+ if (den === 0) return null;
38614
+ const g = gcd4(Math.abs(num), Math.abs(den));
38615
+ const sign3 = den < 0 ? -1 : 1;
38616
+ solution[col] = [sign3 * num / g, sign3 * den / g];
38617
+ }
38618
+ return solution;
38619
+ }
38620
+ function gcd4(a, b) {
38621
+ a = Math.abs(a);
38622
+ b = Math.abs(b);
38623
+ while (b) {
38624
+ [a, b] = [b, a % b];
38625
+ }
38626
+ return a || 1;
38627
+ }
38628
+ function partialFraction(expr, variable) {
38629
+ const ce = expr.engine;
38630
+ if (!isFunction2(expr, "Divide")) return expr;
38631
+ const numer = expr.op1;
38632
+ const denom = expr.op2;
38633
+ const numerDeg = polynomialDegree(numer, variable);
38634
+ const denomDeg = polynomialDegree(denom, variable);
38635
+ if (numerDeg < 0 || denomDeg < 0) return expr;
38636
+ if (denomDeg === 0) return expr;
38637
+ let quotient = null;
38638
+ let remainder2;
38639
+ if (numerDeg >= denomDeg) {
38640
+ const divResult = polynomialDivide(numer, denom, variable);
38641
+ if (!divResult) return expr;
38642
+ quotient = divResult[0];
38643
+ remainder2 = divResult[1];
38644
+ const remCoeffs2 = getPolynomialCoefficients(remainder2, variable);
38645
+ if (remCoeffs2 && remCoeffs2.every((c) => c.isSame(0))) {
38646
+ return quotient;
38647
+ }
38648
+ } else {
38649
+ remainder2 = numer;
38650
+ }
38651
+ const factoredDenom = factorPolynomial(denom, variable);
38652
+ const factors = collectFactors(factoredDenom, variable);
38653
+ if (factors.length === 0) return expr;
38654
+ if (factors.length === 1 && factors[0].multiplicity === 1) {
38655
+ if (quotient) return quotient.add(remainder2.div(denom));
38656
+ return expr;
38657
+ }
38658
+ for (const f of factors) {
38659
+ if (f.degree > 2 || f.degree < 0) return expr;
38660
+ }
38661
+ let totalFactorDeg = 0;
38662
+ for (const f of factors) {
38663
+ totalFactorDeg += f.degree * f.multiplicity;
38664
+ }
38665
+ if (totalFactorDeg !== denomDeg) return expr;
38666
+ const templateTerms = [];
38667
+ let unknownCount = 0;
38668
+ for (const f of factors) {
38669
+ for (let k = 1; k <= f.multiplicity; k++) {
38670
+ if (f.degree === 1) {
38671
+ templateTerms.push({
38672
+ isLinear: true,
38673
+ factor: f.factor,
38674
+ power: k,
38675
+ unknownIndex: unknownCount
38676
+ });
38677
+ unknownCount++;
38678
+ } else {
38679
+ templateTerms.push({
38680
+ isLinear: false,
38681
+ factor: f.factor,
38682
+ power: k,
38683
+ unknownIndex: unknownCount
38684
+ });
38685
+ unknownCount += 2;
38686
+ }
38687
+ }
38688
+ }
38689
+ if (unknownCount !== denomDeg) return expr;
38690
+ const expandedDenom = expand2(denom);
38691
+ const denomCoeffs = getPolynomialCoefficients(expandedDenom, variable);
38692
+ if (!denomCoeffs) return expr;
38693
+ const expandedRemainder = expand2(remainder2);
38694
+ const remCoeffs = getPolynomialCoefficients(expandedRemainder, variable);
38695
+ if (!remCoeffs) return expr;
38696
+ const systemRows = denomDeg;
38697
+ const augMatrix = [];
38698
+ for (let i = 0; i < systemRows; i++) {
38699
+ augMatrix.push(new Array(unknownCount + 1).fill(0));
38700
+ }
38701
+ for (let i = 0; i < systemRows; i++) {
38702
+ const coeff = i < remCoeffs.length ? asSmallInteger(remCoeffs[i]) : 0;
38703
+ if (coeff === null) return expr;
38704
+ augMatrix[i][unknownCount] = coeff;
38705
+ }
38706
+ for (const t of templateTerms) {
38707
+ let termDenom;
38708
+ if (t.power === 1) {
38709
+ termDenom = t.factor;
38710
+ } else {
38711
+ termDenom = ce.box(["Power", t.factor.json, t.power]);
38712
+ }
38713
+ const cofactorResult = polynomialDivide(expandedDenom, termDenom, variable);
38714
+ if (!cofactorResult) return expr;
38715
+ const cofactor = cofactorResult[0];
38716
+ const cofRem = cofactorResult[1];
38717
+ const cofRemCoeffs = getPolynomialCoefficients(cofRem, variable);
38718
+ if (!cofRemCoeffs || !cofRemCoeffs.every((c) => c.isSame(0))) return expr;
38719
+ const expandedCofactor = expand2(cofactor);
38720
+ const cofCoeffs = getPolynomialCoefficients(expandedCofactor, variable);
38721
+ if (!cofCoeffs) return expr;
38722
+ const intCofCoeffs = [];
38723
+ for (let i = 0; i < systemRows; i++) {
38724
+ const c = i < cofCoeffs.length ? asSmallInteger(cofCoeffs[i]) : 0;
38725
+ if (c === null) return expr;
38726
+ intCofCoeffs.push(c);
38727
+ }
38728
+ if (t.isLinear) {
38729
+ for (let i = 0; i < systemRows; i++) {
38730
+ augMatrix[i][t.unknownIndex] += intCofCoeffs[i];
38731
+ }
38732
+ } else {
38733
+ const aIdx = t.unknownIndex;
38734
+ const bIdx = t.unknownIndex + 1;
38735
+ for (let i = 0; i < systemRows; i++) {
38736
+ augMatrix[i][bIdx] += intCofCoeffs[i];
38737
+ if (i > 0) {
38738
+ augMatrix[i][aIdx] += intCofCoeffs[i - 1];
38739
+ }
38740
+ }
38741
+ }
38742
+ }
38743
+ const solution = solveLinearSystem(augMatrix, unknownCount);
38744
+ if (!solution) return expr;
38745
+ const x = ce.symbol(variable);
38746
+ const partialTerms = [];
38747
+ if (quotient) partialTerms.push(quotient);
38748
+ for (const t of templateTerms) {
38749
+ let termDenom;
38750
+ if (t.power === 1) {
38751
+ termDenom = t.factor;
38752
+ } else {
38753
+ termDenom = ce.box(["Power", t.factor.json, t.power]);
38754
+ }
38755
+ let termNumer;
38756
+ if (t.isLinear) {
38757
+ const [num, den] = solution[t.unknownIndex];
38758
+ if (num === 0) continue;
38759
+ termNumer = den === 1 ? ce.number(num) : ce.number(num).div(ce.number(den));
38760
+ } else {
38761
+ const [aNum, aDen] = solution[t.unknownIndex];
38762
+ const [bNum, bDen] = solution[t.unknownIndex + 1];
38763
+ if (aNum === 0 && bNum === 0) continue;
38764
+ const terms = [];
38765
+ if (aNum !== 0) {
38766
+ const aCoeff = aDen === 1 ? ce.number(aNum) : ce.number(aNum).div(ce.number(aDen));
38767
+ terms.push(aCoeff.mul(x));
38768
+ }
38769
+ if (bNum !== 0) {
38770
+ const bCoeff = bDen === 1 ? ce.number(bNum) : ce.number(bNum).div(ce.number(bDen));
38771
+ terms.push(bCoeff);
38772
+ }
38773
+ termNumer = terms.length === 1 ? terms[0] : add3(...terms);
38774
+ }
38775
+ partialTerms.push(termNumer.div(termDenom));
38776
+ }
38777
+ if (partialTerms.length === 0) return ce.Zero;
38778
+ if (partialTerms.length === 1) return partialTerms[0];
38779
+ return add3(...partialTerms);
38780
+ }
38331
38781
 
38332
38782
  // src/compute-engine/symbolic/distribute.ts
38333
38783
  function distribute2(lhs, rhs, g, f) {
@@ -38402,7 +38852,7 @@ ${e.message}
38402
38852
  }
38403
38853
  },
38404
38854
  CoefficientList: {
38405
- description: "Return the list of coefficients of a polynomial, from lowest to highest degree. Example: CoefficientList(x\xB3 + 2x + 1, x) \u2192 [1, 2, 0, 1]",
38855
+ description: "Return the list of coefficients of a polynomial, from highest to lowest degree. Example: CoefficientList(x\xB3 + 2x + 1, x) \u2192 [1, 0, 2, 1]",
38406
38856
  lazy: true,
38407
38857
  signature: "(value, symbol) -> list<value>",
38408
38858
  evaluate: ([poly, varExpr]) => {
@@ -38411,7 +38861,7 @@ ${e.message}
38411
38861
  if (!variable) return void 0;
38412
38862
  const coeffs = getPolynomialCoefficients(poly.canonical, variable);
38413
38863
  if (!coeffs) return void 0;
38414
- return poly.engine.box(["List", ...coeffs]);
38864
+ return poly.engine.box(["List", ...coeffs.reverse()]);
38415
38865
  }
38416
38866
  },
38417
38867
  PolynomialQuotient: {
@@ -38467,6 +38917,85 @@ ${e.message}
38467
38917
  if (!variable) return void 0;
38468
38918
  return cancelCommonFactors(expr.canonical, variable);
38469
38919
  }
38920
+ },
38921
+ PartialFraction: {
38922
+ description: "Decompose a rational expression into partial fractions. Example: PartialFraction(1/((x+1)(x+2)), x) \u2192 1/(x+1) - 1/(x+2)",
38923
+ lazy: true,
38924
+ signature: "(value, symbol) -> value",
38925
+ evaluate: ([expr, varExpr]) => {
38926
+ if (!expr || !varExpr) return void 0;
38927
+ const variable = sym(varExpr.canonical);
38928
+ if (!variable) return void 0;
38929
+ return partialFraction(expr.canonical, variable);
38930
+ }
38931
+ },
38932
+ Apart: {
38933
+ description: "Alias for PartialFraction. Decompose a rational expression into partial fractions.",
38934
+ lazy: true,
38935
+ signature: "(value, symbol) -> value",
38936
+ evaluate: ([expr, varExpr]) => {
38937
+ if (!expr || !varExpr) return void 0;
38938
+ const variable = sym(varExpr.canonical);
38939
+ if (!variable) return void 0;
38940
+ return partialFraction(expr.canonical, variable);
38941
+ }
38942
+ },
38943
+ PolynomialRoots: {
38944
+ description: "Return the roots of a polynomial expression. Example: PolynomialRoots(x\xB2 - 5x + 6, x) \u2192 {2, 3}",
38945
+ lazy: true,
38946
+ signature: "(value, symbol) -> set<value>",
38947
+ evaluate: ([poly, varExpr]) => {
38948
+ if (!poly || !varExpr) return void 0;
38949
+ const variable = sym(varExpr.canonical);
38950
+ if (!variable) return void 0;
38951
+ const roots = poly.canonical.polynomialRoots(variable);
38952
+ if (!roots || roots.length === 0) return void 0;
38953
+ return poly.engine.box(["Set", ...roots.map((r) => r.json)]);
38954
+ }
38955
+ },
38956
+ Discriminant: {
38957
+ description: "Return the discriminant of a polynomial. Example: Discriminant(x\xB2 - 5x + 6, x) \u2192 1",
38958
+ lazy: true,
38959
+ signature: "(value, symbol) -> value",
38960
+ evaluate: ([poly, varExpr]) => {
38961
+ if (!poly || !varExpr) return void 0;
38962
+ const variable = sym(varExpr.canonical);
38963
+ if (!variable) return void 0;
38964
+ const coeffsAsc = getPolynomialCoefficients(poly.canonical, variable);
38965
+ if (!coeffsAsc) return void 0;
38966
+ const coeffs = [...coeffsAsc].reverse();
38967
+ const degree = coeffs.length - 1;
38968
+ const ce = poly.engine;
38969
+ if (degree === 2) {
38970
+ const [a, b, c] = coeffs;
38971
+ return b.mul(b).sub(ce.number(4).mul(a).mul(c));
38972
+ }
38973
+ if (degree === 3) {
38974
+ const [a, b, c, d] = coeffs;
38975
+ return b.mul(b).mul(c).mul(c).sub(ce.number(4).mul(a).mul(c).mul(c).mul(c)).sub(ce.number(4).mul(b).mul(b).mul(b).mul(d)).add(ce.number(18).mul(a).mul(b).mul(c).mul(d)).sub(ce.number(27).mul(a).mul(a).mul(d).mul(d));
38976
+ }
38977
+ if (degree === 4) {
38978
+ const [a, b, c, d, e] = coeffs;
38979
+ return ce.number(256).mul(a).mul(a).mul(a).mul(e).mul(e).mul(e).sub(ce.number(192).mul(a).mul(a).mul(b).mul(d).mul(e).mul(e)).sub(ce.number(128).mul(a).mul(a).mul(c).mul(c).mul(e).mul(e)).add(ce.number(144).mul(a).mul(a).mul(c).mul(d).mul(d).mul(e)).sub(ce.number(27).mul(a).mul(a).mul(d).mul(d).mul(d).mul(d)).add(ce.number(144).mul(a).mul(b).mul(b).mul(c).mul(e).mul(e)).sub(ce.number(6).mul(a).mul(b).mul(b).mul(d).mul(d).mul(e)).sub(ce.number(80).mul(a).mul(b).mul(c).mul(c).mul(d).mul(e)).add(ce.number(18).mul(a).mul(b).mul(c).mul(d).mul(d).mul(d)).add(ce.number(16).mul(a).mul(c).mul(c).mul(c).mul(c).mul(e)).sub(ce.number(4).mul(a).mul(c).mul(c).mul(c).mul(d).mul(d)).sub(ce.number(27).mul(b).mul(b).mul(b).mul(b).mul(e).mul(e)).add(ce.number(18).mul(b).mul(b).mul(b).mul(c).mul(d).mul(e)).sub(ce.number(4).mul(b).mul(b).mul(b).mul(d).mul(d).mul(d)).sub(ce.number(4).mul(b).mul(b).mul(c).mul(c).mul(c).mul(e)).add(b.mul(b).mul(c).mul(c).mul(d).mul(d));
38980
+ }
38981
+ return void 0;
38982
+ }
38983
+ },
38984
+ Polynomial: {
38985
+ description: "Construct a polynomial from a list of coefficients (highest to lowest degree) and a variable. Example: Polynomial([1, 0, 2, 1], x) \u2192 x\xB3 + 2x + 1",
38986
+ lazy: true,
38987
+ signature: "(list<value>, symbol) -> value",
38988
+ evaluate: ([coeffList, varExpr]) => {
38989
+ if (!coeffList || !varExpr) return void 0;
38990
+ const variable = sym(varExpr.canonical);
38991
+ if (!variable) return void 0;
38992
+ const canonical2 = coeffList.canonical;
38993
+ if (!isFunction2(canonical2, "List")) return void 0;
38994
+ const coeffs = canonical2.ops;
38995
+ if (coeffs.length === 0) return void 0;
38996
+ const ascending = [...coeffs].reverse();
38997
+ return fromCoefficients(ascending, variable);
38998
+ }
38470
38999
  }
38471
39000
  }
38472
39001
  ];
@@ -46465,7 +46994,7 @@ Error in definition of "${name}"`,
46465
46994
  };
46466
46995
  return checkTerm(expr);
46467
46996
  }
46468
- function solveLinearSystem(equations, variables) {
46997
+ function solveLinearSystem2(equations, variables) {
46469
46998
  if (equations.length === 0 || variables.length === 0) return null;
46470
46999
  const ce = equations[0].engine;
46471
47000
  const n = variables.length;
@@ -47450,6 +47979,15 @@ Error in definition of "${name}"`,
47450
47979
  } else if (operator2 === expr.operator) {
47451
47980
  const matchPerms = options.matchPermutations ?? true;
47452
47981
  result = pattern.operatorDefinition.commutative && matchPerms ? matchPermutation(expr, pattern, substitution, options) : matchArguments(expr, pattern.ops, substitution, options);
47982
+ if (result === null && options.matchMissingTerms && isFunction2(expr) && isFunction2(pattern) && expr.nops < pattern.nops) {
47983
+ result = matchWithMissingTerms(
47984
+ expr,
47985
+ pattern,
47986
+ substitution,
47987
+ options,
47988
+ ce
47989
+ );
47990
+ }
47453
47991
  }
47454
47992
  if (result === null && useVariations) {
47455
47993
  if (!acceptVariants) return null;
@@ -47747,8 +48285,8 @@ Error in definition of "${name}"`,
47747
48285
  const minCapture = 1;
47748
48286
  const maxCapture2 = remaining.length - countMinNeeded(restWildcards);
47749
48287
  for (let count = maxCapture2; count >= minCapture; count--) {
47750
- const combinations = getCombinations(remaining.length, count);
47751
- for (const indices of combinations) {
48288
+ const combinations2 = getCombinations(remaining.length, count);
48289
+ for (const indices of combinations2) {
47752
48290
  const captured = indices.map((i) => remaining[i]);
47753
48291
  const capturedExpr = wrapCaptured(captured);
47754
48292
  const newSub = captureWildcard(wcName, capturedExpr, sub3);
@@ -47777,8 +48315,8 @@ Error in definition of "${name}"`,
47777
48315
  if (result !== null) return result;
47778
48316
  }
47779
48317
  } else {
47780
- const combinations = getCombinations(remaining.length, count);
47781
- for (const indices of combinations) {
48318
+ const combinations2 = getCombinations(remaining.length, count);
48319
+ for (const indices of combinations2) {
47782
48320
  const captured = indices.map((i) => remaining[i]);
47783
48321
  const capturedExpr = wrapCaptured(captured);
47784
48322
  const newSub = captureWildcard(wcName, capturedExpr, sub3);
@@ -47952,17 +48490,140 @@ Error in definition of "${name}"`,
47952
48490
  }
47953
48491
  }
47954
48492
  }
48493
+ function matchWithMissingTerms(expr, pattern, substitution, options, ce) {
48494
+ if (!isFunction2(expr) || !isFunction2(pattern)) return null;
48495
+ const operator2 = expr.operator;
48496
+ const identity = operator2 === "Add" ? ce.Zero : operator2 === "Multiply" ? ce.One : null;
48497
+ if (!identity) return null;
48498
+ const patOps = pattern.ops;
48499
+ const missing = patOps.length - expr.nops;
48500
+ if (missing <= 0) return null;
48501
+ const combos = combinations(patOps.length, missing);
48502
+ combos.sort((a, b) => {
48503
+ const scoreA = a.reduce((s, i) => s + patternComplexity(patOps[i]), 0);
48504
+ const scoreB = b.reduce((s, i) => s + patternComplexity(patOps[i]), 0);
48505
+ return scoreA - scoreB;
48506
+ });
48507
+ for (const identityIndices of combos) {
48508
+ const activePatOps = patOps.filter((_, i) => !identityIndices.includes(i));
48509
+ const identityPatOps = identityIndices.map((i) => patOps[i]);
48510
+ const subPattern = ce.function(operator2, activePatOps, { form: "raw" });
48511
+ let sub3 = matchPermutation(expr, subPattern, substitution, options);
48512
+ if (sub3 === null) continue;
48513
+ let failed = false;
48514
+ for (const patOp of identityPatOps) {
48515
+ const result = matchIdentityTerm(patOp, identity, sub3, options, ce);
48516
+ if (result === null) {
48517
+ failed = true;
48518
+ break;
48519
+ }
48520
+ sub3 = result;
48521
+ }
48522
+ if (!failed) return sub3;
48523
+ }
48524
+ return null;
48525
+ }
48526
+ function matchIdentityTerm(patOp, identity, sub3, options, ce) {
48527
+ const result = matchOnce(identity, patOp, sub3, options);
48528
+ if (result !== null) return result;
48529
+ if (identity.isSame(0) && isFunction2(patOp, "Multiply")) {
48530
+ for (const op of patOp.ops) {
48531
+ const wn = wildcardName(op);
48532
+ if (wn && !(wn in sub3)) {
48533
+ return captureWildcard(wn, identity, sub3);
48534
+ }
48535
+ }
48536
+ }
48537
+ if (identity.isSame(1) && isFunction2(patOp, "Power")) {
48538
+ const expOp = patOp.ops[1];
48539
+ if (expOp) {
48540
+ const wn = wildcardName(expOp);
48541
+ if (wn && !(wn in sub3)) {
48542
+ return captureWildcard(wn, ce.Zero, sub3);
48543
+ }
48544
+ }
48545
+ }
48546
+ return null;
48547
+ }
48548
+ function patternComplexity(expr) {
48549
+ if (isWildcard(expr)) return 0;
48550
+ if (isNumber(expr) || isSymbol2(expr) || isString(expr)) return 1;
48551
+ if (isFunction2(expr))
48552
+ return 1 + expr.ops.reduce((s, op) => s + patternComplexity(op), 0);
48553
+ return 1;
48554
+ }
48555
+ function combinations(n, k) {
48556
+ const result = [];
48557
+ const combo = [];
48558
+ function backtrack(start) {
48559
+ if (combo.length === k) {
48560
+ result.push([...combo]);
48561
+ return;
48562
+ }
48563
+ for (let i = start; i < n; i++) {
48564
+ combo.push(i);
48565
+ backtrack(i + 1);
48566
+ combo.pop();
48567
+ }
48568
+ }
48569
+ backtrack(0);
48570
+ return result;
48571
+ }
47955
48572
  function match(subject, pattern, options) {
47956
- pattern = pattern.structural;
47957
- const useVariations = options?.useVariations ?? false;
48573
+ const ce = subject.engine;
48574
+ let autoWildcard = false;
48575
+ let boxedPattern;
48576
+ if (typeof pattern === "string") {
48577
+ autoWildcard = true;
48578
+ boxedPattern = ce.parse(pattern).map(
48579
+ (x) => isSymbol2(x) && x.symbol.length === 1 ? ce.symbol("_" + x.symbol) : x,
48580
+ { canonical: false }
48581
+ );
48582
+ } else if ("engine" in pattern) {
48583
+ boxedPattern = pattern;
48584
+ } else {
48585
+ boxedPattern = ce.box(pattern);
48586
+ }
48587
+ boxedPattern = boxedPattern.structural;
48588
+ const useVariations = options?.useVariations ?? autoWildcard;
47958
48589
  const opts = {
47959
48590
  recursive: options?.recursive ?? false,
47960
48591
  useVariations,
47961
48592
  acceptVariants: useVariations,
47962
- matchPermutations: options?.matchPermutations ?? true
48593
+ matchPermutations: options?.matchPermutations ?? true,
48594
+ matchMissingTerms: options?.matchMissingTerms ?? autoWildcard
47963
48595
  };
47964
- const substitution = options?.substitution ?? {};
47965
- return matchOnce(subject.structural, pattern.structural, substitution, opts);
48596
+ let substitution = options?.substitution ?? {};
48597
+ if (autoWildcard && Object.keys(substitution).length > 0) {
48598
+ const prefixed = {};
48599
+ for (const [k, v] of Object.entries(substitution)) {
48600
+ if (k.startsWith("_")) {
48601
+ prefixed[k] = v;
48602
+ } else {
48603
+ if (!(`_${k}` in substitution)) prefixed[`_${k}`] = v;
48604
+ prefixed[k] = v;
48605
+ }
48606
+ }
48607
+ substitution = prefixed;
48608
+ }
48609
+ const result = matchOnce(
48610
+ subject.structural,
48611
+ boxedPattern.structural,
48612
+ substitution,
48613
+ opts
48614
+ );
48615
+ if (!result) return null;
48616
+ if (autoWildcard) {
48617
+ const clean = {};
48618
+ for (const [k, v] of Object.entries(result)) {
48619
+ if (!k.startsWith("_")) continue;
48620
+ const name = k.slice(1);
48621
+ if (isSymbol2(v) && v.symbol === name) continue;
48622
+ clean[name] = v;
48623
+ }
48624
+ return clean;
48625
+ }
48626
+ return result;
47966
48627
  }
47967
48628
 
47968
48629
  // src/compute-engine/boxed-expression/sgn.ts
@@ -48818,7 +49479,7 @@ Error in definition of "${name}"`,
48818
49479
  };
48819
49480
  function solveSystem(ce, equations, varNames) {
48820
49481
  if (equations && equations.every((eq2) => eq2.operator === "Equal")) {
48821
- const linearResult = solveLinearSystem([...equations], varNames);
49482
+ const linearResult = solveLinearSystem2([...equations], varNames);
48822
49483
  if (linearResult && filterSolutionByTypes(ce, varNames, linearResult))
48823
49484
  return linearResult;
48824
49485
  const polyResult = solvePolynomialSystem([...equations], varNames);
@@ -48848,7 +49509,7 @@ Error in definition of "${name}"`,
48848
49509
  (eq2) => inequalityOps.includes(eq2.operator ?? "")
48849
49510
  );
48850
49511
  if (equalities.length > 0 && inequalities.length > 0 && equalities.length + inequalities.length === equations.length) {
48851
- const linearResult = solveLinearSystem([...equalities], varNames);
49512
+ const linearResult = solveLinearSystem2([...equalities], varNames);
48852
49513
  if (linearResult) {
48853
49514
  if (satisfiesInequalities(linearResult, inequalities))
48854
49515
  return filterSolutionByTypes(ce, varNames, linearResult) ? linearResult : null;
@@ -48972,9 +49633,11 @@ Error in definition of "${name}"`,
48972
49633
  return () => {
48973
49634
  if (engine._deadline === void 0) {
48974
49635
  engine._deadline = Date.now() + engine.timeLimit;
48975
- const result = fn();
48976
- engine._deadline = void 0;
48977
- return result;
49636
+ try {
49637
+ return fn();
49638
+ } finally {
49639
+ engine._deadline = void 0;
49640
+ }
48978
49641
  }
48979
49642
  return fn();
48980
49643
  };
@@ -48983,9 +49646,11 @@ Error in definition of "${name}"`,
48983
49646
  return async () => {
48984
49647
  if (engine._deadline === void 0) {
48985
49648
  engine._deadline = Date.now() + engine.timeLimit;
48986
- const result = await fn();
48987
- engine._deadline = void 0;
48988
- return result;
49649
+ try {
49650
+ return await fn();
49651
+ } finally {
49652
+ engine._deadline = void 0;
49653
+ }
48989
49654
  }
48990
49655
  return fn();
48991
49656
  };
@@ -50483,6 +51148,9 @@ Error in definition of "${name}"`,
50483
51148
  }
50484
51149
  }
50485
51150
  if (h === "Function") {
51151
+ const fnFn = target.functions?.(h);
51152
+ if (typeof fnFn === "function")
51153
+ return fnFn(args, (expr) => _BaseCompiler.compile(expr, target), target);
50486
51154
  const params = args.slice(1).map((x) => isSymbol2(x) ? x.symbol : "_");
50487
51155
  return `((${params.join(", ")}) => ${_BaseCompiler.compile(
50488
51156
  args[0].canonical,
@@ -50492,15 +51160,26 @@ Error in definition of "${name}"`,
50492
51160
  }
50493
51161
  )})`;
50494
51162
  }
50495
- if (h === "Declare")
50496
- return `let ${isSymbol2(args[0]) ? args[0].symbol : "_"}`;
51163
+ if (h === "Declare") {
51164
+ const name = isSymbol2(args[0]) ? args[0].symbol : "_";
51165
+ return target.declare ? target.declare(name) : `let ${name}`;
51166
+ }
50497
51167
  if (h === "Assign")
50498
51168
  return `${isSymbol2(args[0]) ? args[0].symbol : "_"} = ${_BaseCompiler.compile(args[1], target)}`;
50499
51169
  if (h === "Return")
50500
51170
  return `return ${_BaseCompiler.compile(args[0], target)}`;
50501
51171
  if (h === "Break") return "break";
50502
51172
  if (h === "Continue") return "continue";
50503
- if (h === "Loop") return _BaseCompiler.compileForLoop(args, target);
51173
+ if (h === "Loop") {
51174
+ const loopFn = target.functions?.(h);
51175
+ if (typeof loopFn === "function")
51176
+ return loopFn(
51177
+ args,
51178
+ (expr) => _BaseCompiler.compile(expr, target),
51179
+ target
51180
+ );
51181
+ return _BaseCompiler.compileForLoop(args, target);
51182
+ }
50504
51183
  if (h === "If") {
50505
51184
  if (args.length !== 3) throw new Error("If: wrong number of arguments");
50506
51185
  const fn2 = target.functions?.(h);
@@ -50577,16 +51256,38 @@ Error in definition of "${name}"`,
50577
51256
  if (args.length === 1 && locals.length === 0) {
50578
51257
  return _BaseCompiler.compile(args[0], target);
50579
51258
  }
50580
- const result = args.filter((a) => !isSymbol2(a, "Nothing")).map(
50581
- (arg) => _BaseCompiler.compile(arg, {
50582
- ...target,
50583
- var: (id) => {
50584
- if (locals.includes(id)) return id;
50585
- return target.var(id);
51259
+ const typeHints = {};
51260
+ if (target.declare && target.language) {
51261
+ const isWGSL = target.language === "wgsl";
51262
+ for (const local of locals) {
51263
+ for (const arg of args) {
51264
+ if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
51265
+ if (_BaseCompiler.isComplexValued(arg.ops[1])) {
51266
+ typeHints[local] = isWGSL ? "vec2f" : "vec2";
51267
+ }
51268
+ break;
51269
+ }
50586
51270
  }
50587
- })
50588
- ).filter((s) => s !== "");
51271
+ }
51272
+ }
51273
+ const localTarget = {
51274
+ ...target,
51275
+ var: (id) => {
51276
+ if (locals.includes(id)) return id;
51277
+ return target.var(id);
51278
+ }
51279
+ };
51280
+ const result = args.filter((a) => !isSymbol2(a, "Nothing")).map((arg) => {
51281
+ if (isFunction2(arg, "Declare") && isSymbol2(arg.ops[0]) && target.declare) {
51282
+ return target.declare(
51283
+ arg.ops[0].symbol,
51284
+ typeHints[arg.ops[0].symbol]
51285
+ );
51286
+ }
51287
+ return _BaseCompiler.compile(arg, localTarget);
51288
+ }).filter((s) => s !== "");
50589
51289
  if (result.length === 0) return "";
51290
+ if (target.block) return target.block(result);
50590
51291
  result[result.length - 1] = `return ${result[result.length - 1]}`;
50591
51292
  return `(() => {${target.ws("\n")}${result.join(
50592
51293
  `;${target.ws("\n")}`
@@ -51072,6 +51773,9 @@ Error in definition of "${name}"`,
51072
51773
  _setSerializeJson(serializeJson);
51073
51774
  _setProduct(Product);
51074
51775
  _setCompile(compile);
51776
+ _setGetPolynomialCoefficients(getPolynomialCoefficients);
51777
+ _setGetPolynomialDegree(polynomialDegree);
51778
+ _setFindUnivariateRoots(findUnivariateRoots);
51075
51779
 
51076
51780
  // src/compute-engine/symbolic/simplify-logic.ts
51077
51781
  function simplifyLogicFunction(x) {
@@ -54010,6 +54714,27 @@ Error in definition of "${name}"`,
54010
54714
  if (result.isSame(x)) return void 0;
54011
54715
  return { value: result, because: "cancel common polynomial factors" };
54012
54716
  },
54717
+ //
54718
+ // Auto partial fraction decomposition for Divide with factored denominator
54719
+ // e.g., 1/((x+1)(x+2)) → 1/(x+1) - 1/(x+2)
54720
+ // Only triggers when denominator is already in factored form (Multiply or Power)
54721
+ //
54722
+ // IMPORTANT: partialFraction must not call .simplify() on its result
54723
+ //
54724
+ (x) => {
54725
+ if (!isFunction2(x, "Divide")) return void 0;
54726
+ const denom = x.op2;
54727
+ const denomOp = denom.operator;
54728
+ if (denomOp !== "Multiply" && denomOp !== "Power") return void 0;
54729
+ const unknowns = x.unknowns;
54730
+ if (unknowns.length !== 1) return void 0;
54731
+ const variable = unknowns[0];
54732
+ const result = partialFraction(x, variable);
54733
+ if (result.isSame(x)) return void 0;
54734
+ const ce = x.engine;
54735
+ if (ce.costFunction(result) >= ce.costFunction(x)) return void 0;
54736
+ return { value: result, because: "partial fraction decomposition" };
54737
+ },
54013
54738
  // Quick a/a -> 1 check for identical numerator/denominator
54014
54739
  // Must run before expand to avoid decomposing the fraction first
54015
54740
  (x) => {
@@ -57352,6 +58077,62 @@ Error in definition of "${name}"`,
57352
58077
  function gpuVec2(target) {
57353
58078
  return target?.language === "wgsl" ? "vec2f" : "vec2";
57354
58079
  }
58080
+ var GPU_UNROLL_LIMIT = 100;
58081
+ function compileGPUSumProduct(kind, args, _compile2, target) {
58082
+ if (!args[0]) throw new Error(`${kind}: no body`);
58083
+ if (!args[1]) throw new Error(`${kind}: no indexing set`);
58084
+ if (BaseCompiler.isComplexValued(args[0]))
58085
+ throw new Error(
58086
+ `${kind}: complex-valued body not supported in GPU targets`
58087
+ );
58088
+ const limitsExpr = args[1];
58089
+ if (!isFunction2(limitsExpr, "Limits"))
58090
+ throw new Error(`${kind}: expected Limits indexing set`);
58091
+ const limitsOps = limitsExpr.ops;
58092
+ const index = isSymbol2(limitsOps[0]) ? limitsOps[0].symbol : "_";
58093
+ const lowerRe = limitsOps[1].re;
58094
+ const upperRe = limitsOps[2].re;
58095
+ const lowerNum = !isNaN(lowerRe) && Number.isFinite(lowerRe) ? Math.floor(lowerRe) : void 0;
58096
+ const upperNum = !isNaN(upperRe) && Number.isFinite(upperRe) ? Math.floor(upperRe) : void 0;
58097
+ const isSum = kind === "Sum";
58098
+ const op = isSum ? "+" : "*";
58099
+ const identity = isSum ? "0.0" : "1.0";
58100
+ const isWGSL = target.language === "wgsl";
58101
+ const bothConstant = lowerNum !== void 0 && upperNum !== void 0;
58102
+ if (bothConstant && lowerNum > upperNum) return identity;
58103
+ if (bothConstant && upperNum - lowerNum + 1 <= GPU_UNROLL_LIMIT) {
58104
+ const terms = [];
58105
+ for (let k = lowerNum; k <= upperNum; k++) {
58106
+ const kStr = formatGPUNumber(k);
58107
+ const innerTarget = {
58108
+ ...target,
58109
+ var: (id) => id === index ? kStr : target.var(id)
58110
+ };
58111
+ terms.push(`(${BaseCompiler.compile(args[0], innerTarget)})`);
58112
+ }
58113
+ return `(${terms.join(` ${op} `)})`;
58114
+ }
58115
+ const acc = BaseCompiler.tempVar();
58116
+ const floatType = isWGSL ? "f32" : "float";
58117
+ const intType = isWGSL ? "i32" : "int";
58118
+ const bodyTarget = {
58119
+ ...target,
58120
+ var: (id) => id === index ? isWGSL ? `f32(${index})` : `float(${index})` : target.var(id)
58121
+ };
58122
+ const body = BaseCompiler.compile(args[0], bodyTarget);
58123
+ const lowerStr = lowerNum !== void 0 ? String(lowerNum) : BaseCompiler.compile(limitsOps[1], target);
58124
+ const upperStr = upperNum !== void 0 ? String(upperNum) : BaseCompiler.compile(limitsOps[2], target);
58125
+ const accDecl = isWGSL ? `var ${acc}: ${floatType}` : `${floatType} ${acc}`;
58126
+ const indexDecl = isWGSL ? `var ${index}: ${intType}` : `${intType} ${index}`;
58127
+ const lines = [
58128
+ `${accDecl} = ${identity};`,
58129
+ `for (${indexDecl} = ${lowerStr}; ${index} <= ${upperStr}; ${index}++) {`,
58130
+ ` ${acc} ${op}= ${body};`,
58131
+ `}`,
58132
+ `return ${acc}`
58133
+ ];
58134
+ return lines.join("\n");
58135
+ }
57355
58136
  var GPU_FUNCTIONS = {
57356
58137
  // Variadic arithmetic (for function-call form, e.g., with vectors)
57357
58138
  Add: (args, compile3, target) => {
@@ -57701,6 +58482,28 @@ Error in definition of "${name}"`,
57701
58482
  if (x === null) throw new Error("ErfInv: no argument");
57702
58483
  return `_gpu_erfinv(${compile3(x)})`;
57703
58484
  },
58485
+ Heaviside: ([x], compile3) => {
58486
+ if (x === null) throw new Error("Heaviside: no argument");
58487
+ return `_gpu_heaviside(${compile3(x)})`;
58488
+ },
58489
+ Sinc: ([x], compile3) => {
58490
+ if (x === null) throw new Error("Sinc: no argument");
58491
+ return `_gpu_sinc(${compile3(x)})`;
58492
+ },
58493
+ FresnelC: ([x], compile3) => {
58494
+ if (x === null) throw new Error("FresnelC: no argument");
58495
+ return `_gpu_fresnelC(${compile3(x)})`;
58496
+ },
58497
+ FresnelS: ([x], compile3) => {
58498
+ if (x === null) throw new Error("FresnelS: no argument");
58499
+ return `_gpu_fresnelS(${compile3(x)})`;
58500
+ },
58501
+ BesselJ: ([n, x], compile3, target) => {
58502
+ if (n === null || x === null)
58503
+ throw new Error("BesselJ: need two arguments");
58504
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
58505
+ return `_gpu_besselJ(${intCast}(${compile3(n)}), ${compile3(x)})`;
58506
+ },
57704
58507
  // Additional math functions
57705
58508
  Lb: "log2",
57706
58509
  Log: (args, compile3) => {
@@ -57786,7 +58589,44 @@ Error in definition of "${name}"`,
57786
58589
  Length: "length",
57787
58590
  Normalize: "normalize",
57788
58591
  Reflect: "reflect",
57789
- Refract: "refract"
58592
+ Refract: "refract",
58593
+ // Sum/Product — unrolled or for-loop
58594
+ Sum: (args, compile3, target) => compileGPUSumProduct("Sum", args, compile3, target),
58595
+ Product: (args, compile3, target) => compileGPUSumProduct("Product", args, compile3, target),
58596
+ // Loop — GPU for-loop (no IIFE, no let)
58597
+ Loop: (args, _compile2, target) => {
58598
+ if (!args[0]) throw new Error("Loop: no body");
58599
+ if (!args[1]) throw new Error("Loop: no indexing set");
58600
+ const indexing = args[1];
58601
+ if (!isFunction2(indexing, "Element"))
58602
+ throw new Error("Loop: expected Element(index, Range(lo, hi))");
58603
+ const indexExpr = indexing.ops[0];
58604
+ const rangeExpr = indexing.ops[1];
58605
+ if (!isSymbol2(indexExpr)) throw new Error("Loop: index must be a symbol");
58606
+ if (!isFunction2(rangeExpr, "Range"))
58607
+ throw new Error("Loop: expected Range(lo, hi)");
58608
+ const index = indexExpr.symbol;
58609
+ const lower = Math.floor(rangeExpr.ops[0].re);
58610
+ const upper = Math.floor(rangeExpr.ops[1].re);
58611
+ if (!Number.isFinite(lower) || !Number.isFinite(upper))
58612
+ throw new Error("Loop: bounds must be finite numbers");
58613
+ const isWGSL = target.language === "wgsl";
58614
+ const intType = isWGSL ? "i32" : "int";
58615
+ const bodyCode = BaseCompiler.compile(args[0], {
58616
+ ...target,
58617
+ var: (id) => id === index ? index : target.var(id)
58618
+ });
58619
+ const indexDecl = isWGSL ? `var ${index}: ${intType}` : `${intType} ${index}`;
58620
+ return `for (${indexDecl} = ${lower}; ${index} <= ${upper}; ${index}++) {
58621
+ ${bodyCode};
58622
+ }`;
58623
+ },
58624
+ // Function (lambda) — not supported in GPU
58625
+ Function: () => {
58626
+ throw new Error(
58627
+ "Anonymous functions (Function) are not supported in GPU targets"
58628
+ );
58629
+ }
57790
58630
  };
57791
58631
  function compileGPUMatrix(args, compile3, vecFn, matFn, arrayFn) {
57792
58632
  const body = args[0];
@@ -57866,6 +58706,476 @@ float _gpu_erfinv(float x) {
57866
58706
  float x9 = x7 * x2;
57867
58707
  return sqrt(pi) * 0.5 * (x + (pi / 12.0) * x3 + (7.0 * pi * pi / 480.0) * x5 + (127.0 * pi * pi * pi / 40320.0) * x7 + (4369.0 * pi * pi * pi * pi / 5806080.0) * x9);
57868
58708
  }
58709
+ `;
58710
+ var GPU_HEAVISIDE_PREAMBLE_GLSL = `
58711
+ float _gpu_heaviside(float x) {
58712
+ if (x < 0.0) return 0.0;
58713
+ if (x > 0.0) return 1.0;
58714
+ return 0.5;
58715
+ }
58716
+ `;
58717
+ var GPU_HEAVISIDE_PREAMBLE_WGSL = `
58718
+ fn _gpu_heaviside(x: f32) -> f32 {
58719
+ if (x < 0.0) { return 0.0; }
58720
+ if (x > 0.0) { return 1.0; }
58721
+ return 0.5;
58722
+ }
58723
+ `;
58724
+ var GPU_SINC_PREAMBLE_GLSL = `
58725
+ float _gpu_sinc(float x) {
58726
+ if (abs(x) < 1e-10) return 1.0;
58727
+ return sin(x) / x;
58728
+ }
58729
+ `;
58730
+ var GPU_SINC_PREAMBLE_WGSL = `
58731
+ fn _gpu_sinc(x: f32) -> f32 {
58732
+ if (abs(x) < 1e-10) { return 1.0; }
58733
+ return sin(x) / x;
58734
+ }
58735
+ `;
58736
+ var GPU_POLEVL_PREAMBLE_GLSL = `
58737
+ float _gpu_polevl(float x, float c[12], int n) {
58738
+ float ans = c[0];
58739
+ for (int i = 1; i < n; i++) ans = ans * x + c[i];
58740
+ return ans;
58741
+ }
58742
+ `;
58743
+ var GPU_POLEVL_PREAMBLE_WGSL = `
58744
+ fn _gpu_polevl(x: f32, c: array<f32, 12>, n: i32) -> f32 {
58745
+ var ans = c[0];
58746
+ for (var i: i32 = 1; i < n; i++) { ans = ans * x + c[i]; }
58747
+ return ans;
58748
+ }
58749
+ `;
58750
+ var GPU_FRESNELC_PREAMBLE_GLSL = `
58751
+ float _gpu_fresnelC(float x_in) {
58752
+ float sgn = x_in < 0.0 ? -1.0 : 1.0;
58753
+ float x = abs(x_in);
58754
+
58755
+ if (x < 1.6) {
58756
+ float x2 = x * x;
58757
+ float t = x2 * x2;
58758
+ float cn[6] = float[6](
58759
+ -4.98843114573573548651e-8, 9.50428062829859605134e-6,
58760
+ -6.45191435683965050962e-4, 1.88843319396703850064e-2,
58761
+ -2.05525900955013891793e-1, 9.99999999999999998822e-1
58762
+ );
58763
+ float cd[7] = float[7](
58764
+ 3.99982968972495980367e-12, 9.15439215774657478799e-10,
58765
+ 1.25001862479598821474e-7, 1.22262789024179030997e-5,
58766
+ 8.68029542941784300606e-4, 4.12142090722199792936e-2, 1.0
58767
+ );
58768
+ return sgn * x * _gpu_polevl(t, cn, 6) / _gpu_polevl(t, cd, 7);
58769
+ }
58770
+
58771
+ if (x < 36.0) {
58772
+ float x2 = x * x;
58773
+ float t = 3.14159265358979 * x2;
58774
+ float u = 1.0 / (t * t);
58775
+ float fn[10] = float[10](
58776
+ 4.21543555043677546506e-1, 1.43407919780758885261e-1,
58777
+ 1.15220955073585758835e-2, 3.450179397825740279e-4,
58778
+ 4.63613749287867322088e-6, 3.05568983790257605827e-8,
58779
+ 1.02304514164907233465e-10, 1.72010743268161828879e-13,
58780
+ 1.34283276233062758925e-16, 3.76329711269987889006e-20
58781
+ );
58782
+ float fd[11] = float[11](
58783
+ 1.0, 7.51586398353378947175e-1,
58784
+ 1.16888925859191382142e-1, 6.44051526508858611005e-3,
58785
+ 1.55934409164153020873e-4, 1.8462756734893054587e-6,
58786
+ 1.12699224763999035261e-8, 3.60140029589371370404e-11,
58787
+ 5.8875453362157841001e-14, 4.52001434074129701496e-17,
58788
+ 1.25443237090011264384e-20
58789
+ );
58790
+ float gn[11] = float[11](
58791
+ 5.04442073643383265887e-1, 1.97102833525523411709e-1,
58792
+ 1.87648584092575249293e-2, 6.84079380915393090172e-4,
58793
+ 1.15138826111884280931e-5, 9.82852443688422223854e-8,
58794
+ 4.45344415861750144738e-10, 1.08268041139020870318e-12,
58795
+ 1.37555460633261799868e-15, 8.36354435630677421531e-19,
58796
+ 1.86958710162783235106e-22
58797
+ );
58798
+ float gd[12] = float[12](
58799
+ 1.0, 1.47495759925128324529,
58800
+ 3.37748989120019970451e-1, 2.53603741420338795122e-2,
58801
+ 8.14679107184306179049e-4, 1.27545075667729118702e-5,
58802
+ 1.04314589657571990585e-7, 4.60680728515232032307e-10,
58803
+ 1.10273215066240270757e-12, 1.38796531259578871258e-15,
58804
+ 8.39158816283118707363e-19, 1.86958710162783236342e-22
58805
+ );
58806
+ float f = 1.0 - u * _gpu_polevl(u, fn, 10) / _gpu_polevl(u, fd, 11);
58807
+ float g = (1.0 / t) * _gpu_polevl(u, gn, 11) / _gpu_polevl(u, gd, 12);
58808
+ float z = 1.5707963267948966 * x2;
58809
+ float c = cos(z);
58810
+ float s = sin(z);
58811
+ return sgn * (0.5 + (f * s - g * c) / (3.14159265358979 * x));
58812
+ }
58813
+
58814
+ return sgn * 0.5;
58815
+ }
58816
+ `;
58817
+ var GPU_FRESNELC_PREAMBLE_WGSL = `
58818
+ fn _gpu_fresnelC(x_in: f32) -> f32 {
58819
+ let sgn: f32 = select(1.0, -1.0, x_in < 0.0);
58820
+ let x = abs(x_in);
58821
+
58822
+ if (x < 1.6) {
58823
+ let x2 = x * x;
58824
+ let t = x2 * x2;
58825
+ var cn = array<f32, 12>(
58826
+ -4.98843114573573548651e-8, 9.50428062829859605134e-6,
58827
+ -6.45191435683965050962e-4, 1.88843319396703850064e-2,
58828
+ -2.05525900955013891793e-1, 9.99999999999999998822e-1,
58829
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
58830
+ );
58831
+ var cd = array<f32, 12>(
58832
+ 3.99982968972495980367e-12, 9.15439215774657478799e-10,
58833
+ 1.25001862479598821474e-7, 1.22262789024179030997e-5,
58834
+ 8.68029542941784300606e-4, 4.12142090722199792936e-2, 1.0,
58835
+ 0.0, 0.0, 0.0, 0.0, 0.0
58836
+ );
58837
+ return sgn * x * _gpu_polevl(t, cn, 6) / _gpu_polevl(t, cd, 7);
58838
+ }
58839
+
58840
+ if (x < 36.0) {
58841
+ let x2 = x * x;
58842
+ let t = 3.14159265358979 * x2;
58843
+ let u = 1.0 / (t * t);
58844
+ var fn = array<f32, 12>(
58845
+ 4.21543555043677546506e-1, 1.43407919780758885261e-1,
58846
+ 1.15220955073585758835e-2, 3.450179397825740279e-4,
58847
+ 4.63613749287867322088e-6, 3.05568983790257605827e-8,
58848
+ 1.02304514164907233465e-10, 1.72010743268161828879e-13,
58849
+ 1.34283276233062758925e-16, 3.76329711269987889006e-20,
58850
+ 0.0, 0.0
58851
+ );
58852
+ var fd = array<f32, 12>(
58853
+ 1.0, 7.51586398353378947175e-1,
58854
+ 1.16888925859191382142e-1, 6.44051526508858611005e-3,
58855
+ 1.55934409164153020873e-4, 1.8462756734893054587e-6,
58856
+ 1.12699224763999035261e-8, 3.60140029589371370404e-11,
58857
+ 5.8875453362157841001e-14, 4.52001434074129701496e-17,
58858
+ 1.25443237090011264384e-20, 0.0
58859
+ );
58860
+ var gn = array<f32, 12>(
58861
+ 5.04442073643383265887e-1, 1.97102833525523411709e-1,
58862
+ 1.87648584092575249293e-2, 6.84079380915393090172e-4,
58863
+ 1.15138826111884280931e-5, 9.82852443688422223854e-8,
58864
+ 4.45344415861750144738e-10, 1.08268041139020870318e-12,
58865
+ 1.37555460633261799868e-15, 8.36354435630677421531e-19,
58866
+ 1.86958710162783235106e-22, 0.0
58867
+ );
58868
+ var gd = array<f32, 12>(
58869
+ 1.0, 1.47495759925128324529,
58870
+ 3.37748989120019970451e-1, 2.53603741420338795122e-2,
58871
+ 8.14679107184306179049e-4, 1.27545075667729118702e-5,
58872
+ 1.04314589657571990585e-7, 4.60680728515232032307e-10,
58873
+ 1.10273215066240270757e-12, 1.38796531259578871258e-15,
58874
+ 8.39158816283118707363e-19, 1.86958710162783236342e-22
58875
+ );
58876
+ let f = 1.0 - u * _gpu_polevl(u, fn, 10) / _gpu_polevl(u, fd, 11);
58877
+ let g = (1.0 / t) * _gpu_polevl(u, gn, 11) / _gpu_polevl(u, gd, 12);
58878
+ let z = 1.5707963267948966 * x2;
58879
+ let c = cos(z);
58880
+ let s = sin(z);
58881
+ return sgn * (0.5 + (f * s - g * c) / (3.14159265358979 * x));
58882
+ }
58883
+
58884
+ return sgn * 0.5;
58885
+ }
58886
+ `;
58887
+ var GPU_FRESNELS_PREAMBLE_GLSL = `
58888
+ float _gpu_fresnelS(float x_in) {
58889
+ float sgn = x_in < 0.0 ? -1.0 : 1.0;
58890
+ float x = abs(x_in);
58891
+
58892
+ if (x < 1.6) {
58893
+ float x2 = x * x;
58894
+ float t = x2 * x2;
58895
+ float sn[6] = float[6](
58896
+ -2.99181919401019853726e3, 7.08840045257738576863e5,
58897
+ -6.29741486205862506537e7, 2.54890880573376359104e9,
58898
+ -4.42979518059697779103e10, 3.18016297876567817986e11
58899
+ );
58900
+ float sd[7] = float[7](
58901
+ 1.0, 2.81376268889994315696e2, 4.55847810806532581675e4,
58902
+ 5.1734388877009640073e6, 4.19320245898111231129e8, 2.2441179564534092094e10,
58903
+ 6.07366389490084914091e11
58904
+ );
58905
+ return sgn * x * x2 * _gpu_polevl(t, sn, 6) / _gpu_polevl(t, sd, 7);
58906
+ }
58907
+
58908
+ if (x < 36.0) {
58909
+ float x2 = x * x;
58910
+ float t = 3.14159265358979 * x2;
58911
+ float u = 1.0 / (t * t);
58912
+ float fn[10] = float[10](
58913
+ 4.21543555043677546506e-1, 1.43407919780758885261e-1,
58914
+ 1.15220955073585758835e-2, 3.450179397825740279e-4,
58915
+ 4.63613749287867322088e-6, 3.05568983790257605827e-8,
58916
+ 1.02304514164907233465e-10, 1.72010743268161828879e-13,
58917
+ 1.34283276233062758925e-16, 3.76329711269987889006e-20
58918
+ );
58919
+ float fd[11] = float[11](
58920
+ 1.0, 7.51586398353378947175e-1,
58921
+ 1.16888925859191382142e-1, 6.44051526508858611005e-3,
58922
+ 1.55934409164153020873e-4, 1.8462756734893054587e-6,
58923
+ 1.12699224763999035261e-8, 3.60140029589371370404e-11,
58924
+ 5.8875453362157841001e-14, 4.52001434074129701496e-17,
58925
+ 1.25443237090011264384e-20
58926
+ );
58927
+ float gn[11] = float[11](
58928
+ 5.04442073643383265887e-1, 1.97102833525523411709e-1,
58929
+ 1.87648584092575249293e-2, 6.84079380915393090172e-4,
58930
+ 1.15138826111884280931e-5, 9.82852443688422223854e-8,
58931
+ 4.45344415861750144738e-10, 1.08268041139020870318e-12,
58932
+ 1.37555460633261799868e-15, 8.36354435630677421531e-19,
58933
+ 1.86958710162783235106e-22
58934
+ );
58935
+ float gd[12] = float[12](
58936
+ 1.0, 1.47495759925128324529,
58937
+ 3.37748989120019970451e-1, 2.53603741420338795122e-2,
58938
+ 8.14679107184306179049e-4, 1.27545075667729118702e-5,
58939
+ 1.04314589657571990585e-7, 4.60680728515232032307e-10,
58940
+ 1.10273215066240270757e-12, 1.38796531259578871258e-15,
58941
+ 8.39158816283118707363e-19, 1.86958710162783236342e-22
58942
+ );
58943
+ float f = 1.0 - u * _gpu_polevl(u, fn, 10) / _gpu_polevl(u, fd, 11);
58944
+ float g = (1.0 / t) * _gpu_polevl(u, gn, 11) / _gpu_polevl(u, gd, 12);
58945
+ float z = 1.5707963267948966 * x2;
58946
+ float c = cos(z);
58947
+ float s = sin(z);
58948
+ return sgn * (0.5 - (f * c + g * s) / (3.14159265358979 * x));
58949
+ }
58950
+
58951
+ return sgn * 0.5;
58952
+ }
58953
+ `;
58954
+ var GPU_FRESNELS_PREAMBLE_WGSL = `
58955
+ fn _gpu_fresnelS(x_in: f32) -> f32 {
58956
+ let sgn: f32 = select(1.0, -1.0, x_in < 0.0);
58957
+ let x = abs(x_in);
58958
+
58959
+ if (x < 1.6) {
58960
+ let x2 = x * x;
58961
+ let t = x2 * x2;
58962
+ var sn = array<f32, 12>(
58963
+ -2.99181919401019853726e3, 7.08840045257738576863e5,
58964
+ -6.29741486205862506537e7, 2.54890880573376359104e9,
58965
+ -4.42979518059697779103e10, 3.18016297876567817986e11,
58966
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
58967
+ );
58968
+ var sd = array<f32, 12>(
58969
+ 1.0, 2.81376268889994315696e2, 4.55847810806532581675e4,
58970
+ 5.1734388877009640073e6, 4.19320245898111231129e8, 2.2441179564534092094e10,
58971
+ 6.07366389490084914091e11,
58972
+ 0.0, 0.0, 0.0, 0.0, 0.0
58973
+ );
58974
+ return sgn * x * x2 * _gpu_polevl(t, sn, 6) / _gpu_polevl(t, sd, 7);
58975
+ }
58976
+
58977
+ if (x < 36.0) {
58978
+ let x2 = x * x;
58979
+ let t = 3.14159265358979 * x2;
58980
+ let u = 1.0 / (t * t);
58981
+ var fn = array<f32, 12>(
58982
+ 4.21543555043677546506e-1, 1.43407919780758885261e-1,
58983
+ 1.15220955073585758835e-2, 3.450179397825740279e-4,
58984
+ 4.63613749287867322088e-6, 3.05568983790257605827e-8,
58985
+ 1.02304514164907233465e-10, 1.72010743268161828879e-13,
58986
+ 1.34283276233062758925e-16, 3.76329711269987889006e-20,
58987
+ 0.0, 0.0
58988
+ );
58989
+ var fd = array<f32, 12>(
58990
+ 1.0, 7.51586398353378947175e-1,
58991
+ 1.16888925859191382142e-1, 6.44051526508858611005e-3,
58992
+ 1.55934409164153020873e-4, 1.8462756734893054587e-6,
58993
+ 1.12699224763999035261e-8, 3.60140029589371370404e-11,
58994
+ 5.8875453362157841001e-14, 4.52001434074129701496e-17,
58995
+ 1.25443237090011264384e-20, 0.0
58996
+ );
58997
+ var gn = array<f32, 12>(
58998
+ 5.04442073643383265887e-1, 1.97102833525523411709e-1,
58999
+ 1.87648584092575249293e-2, 6.84079380915393090172e-4,
59000
+ 1.15138826111884280931e-5, 9.82852443688422223854e-8,
59001
+ 4.45344415861750144738e-10, 1.08268041139020870318e-12,
59002
+ 1.37555460633261799868e-15, 8.36354435630677421531e-19,
59003
+ 1.86958710162783235106e-22, 0.0
59004
+ );
59005
+ var gd = array<f32, 12>(
59006
+ 1.0, 1.47495759925128324529,
59007
+ 3.37748989120019970451e-1, 2.53603741420338795122e-2,
59008
+ 8.14679107184306179049e-4, 1.27545075667729118702e-5,
59009
+ 1.04314589657571990585e-7, 4.60680728515232032307e-10,
59010
+ 1.10273215066240270757e-12, 1.38796531259578871258e-15,
59011
+ 8.39158816283118707363e-19, 1.86958710162783236342e-22
59012
+ );
59013
+ let f = 1.0 - u * _gpu_polevl(u, fn, 10) / _gpu_polevl(u, fd, 11);
59014
+ let g = (1.0 / t) * _gpu_polevl(u, gn, 11) / _gpu_polevl(u, gd, 12);
59015
+ let z = 1.5707963267948966 * x2;
59016
+ let c = cos(z);
59017
+ let s = sin(z);
59018
+ return sgn * (0.5 - (f * c + g * s) / (3.14159265358979 * x));
59019
+ }
59020
+
59021
+ return sgn * 0.5;
59022
+ }
59023
+ `;
59024
+ var GPU_BESSELJ_PREAMBLE_GLSL = `
59025
+ float _gpu_factorial(int n) {
59026
+ float f = 1.0;
59027
+ for (int i = 2; i <= n; i++) f *= float(i);
59028
+ return f;
59029
+ }
59030
+
59031
+ float _gpu_besselJ_series(int n, float x) {
59032
+ float halfX = x / 2.0;
59033
+ float negQ = -(x * x) / 4.0;
59034
+ float term = 1.0;
59035
+ for (int i = 1; i <= n; i++) term /= float(i);
59036
+ float s = term;
59037
+ for (int k = 1; k <= 60; k++) {
59038
+ term *= negQ / (float(k) * float(n + k));
59039
+ s += term;
59040
+ if (abs(term) < abs(s) * 1e-7) break;
59041
+ }
59042
+ return s * pow(halfX, float(n));
59043
+ }
59044
+
59045
+ float _gpu_besselJ_asymptotic(int n, float x) {
59046
+ float mu = 4.0 * float(n) * float(n);
59047
+ float P = 1.0;
59048
+ float Q = 0.0;
59049
+ float ak = 1.0;
59050
+ float e8x = 8.0 * x;
59051
+ for (int k = 1; k <= 12; k++) {
59052
+ float twokm1 = float(2 * k - 1);
59053
+ ak *= mu - twokm1 * twokm1;
59054
+ float denom = _gpu_factorial(k) * pow(e8x, float(k));
59055
+ float contrib = ak / denom;
59056
+ if (k == 1 || k == 3 || k == 5 || k == 7 || k == 9 || k == 11) {
59057
+ // odd k: contributes to Q
59058
+ if (((k - 1) / 2) % 2 == 0) Q += contrib;
59059
+ else Q -= contrib;
59060
+ } else {
59061
+ // even k: contributes to P
59062
+ if ((k / 2) % 2 == 1) P -= contrib;
59063
+ else P += contrib;
59064
+ }
59065
+ if (abs(contrib) < 1e-7) break;
59066
+ }
59067
+ float chi = x - (float(n) / 2.0 + 0.25) * 3.14159265358979;
59068
+ return sqrt(2.0 / (3.14159265358979 * x)) * (P * cos(chi) - Q * sin(chi));
59069
+ }
59070
+
59071
+ float _gpu_besselJ(int n, float x) {
59072
+ if (x == 0.0) return n == 0 ? 1.0 : 0.0;
59073
+ float sgn = 1.0;
59074
+ if (n < 0) {
59075
+ n = -n;
59076
+ if (n % 2 != 0) sgn = -1.0;
59077
+ }
59078
+ if (x < 0.0) {
59079
+ x = -x;
59080
+ if (n % 2 != 0) sgn *= -1.0;
59081
+ }
59082
+ if (x > 25.0 + float(n * n) / 2.0) return sgn * _gpu_besselJ_asymptotic(n, x);
59083
+ if (x < 5.0 + float(n)) return sgn * _gpu_besselJ_series(n, x);
59084
+ // Miller's backward recurrence
59085
+ int M = max(n + 20, int(ceil(x)) + 30);
59086
+ if (M > 200) return sgn * _gpu_besselJ_series(n, x);
59087
+ float vals[201];
59088
+ float jp1 = 0.0;
59089
+ float jk = 1.0;
59090
+ vals[M] = jk;
59091
+ for (int k = M; k >= 1; k--) {
59092
+ float jm1 = (2.0 * float(k) / x) * jk - jp1;
59093
+ jp1 = jk;
59094
+ jk = jm1;
59095
+ vals[k - 1] = jk;
59096
+ }
59097
+ float norm = vals[0];
59098
+ for (int k = 2; k <= M; k += 2) norm += 2.0 * vals[k];
59099
+ return sgn * vals[n] / norm;
59100
+ }
59101
+ `;
59102
+ var GPU_BESSELJ_PREAMBLE_WGSL = `
59103
+ fn _gpu_factorial(n: i32) -> f32 {
59104
+ var f: f32 = 1.0;
59105
+ for (var i: i32 = 2; i <= n; i++) { f *= f32(i); }
59106
+ return f;
59107
+ }
59108
+
59109
+ fn _gpu_besselJ_series(n_in: i32, x: f32) -> f32 {
59110
+ let halfX = x / 2.0;
59111
+ let negQ = -(x * x) / 4.0;
59112
+ var term: f32 = 1.0;
59113
+ for (var i: i32 = 1; i <= n_in; i++) { term /= f32(i); }
59114
+ var s = term;
59115
+ for (var k: i32 = 1; k <= 60; k++) {
59116
+ term *= negQ / (f32(k) * f32(n_in + k));
59117
+ s += term;
59118
+ if (abs(term) < abs(s) * 1e-7) { break; }
59119
+ }
59120
+ return s * pow(halfX, f32(n_in));
59121
+ }
59122
+
59123
+ fn _gpu_besselJ_asymptotic(n_in: i32, x: f32) -> f32 {
59124
+ let mu = 4.0 * f32(n_in) * f32(n_in);
59125
+ var P: f32 = 1.0;
59126
+ var Q: f32 = 0.0;
59127
+ var ak: f32 = 1.0;
59128
+ let e8x = 8.0 * x;
59129
+ for (var k: i32 = 1; k <= 12; k++) {
59130
+ let twokm1 = f32(2 * k - 1);
59131
+ ak *= mu - twokm1 * twokm1;
59132
+ let denom = _gpu_factorial(k) * pow(e8x, f32(k));
59133
+ let contrib = ak / denom;
59134
+ if (k == 1 || k == 3 || k == 5 || k == 7 || k == 9 || k == 11) {
59135
+ if (((k - 1) / 2) % 2 == 0) { Q += contrib; }
59136
+ else { Q -= contrib; }
59137
+ } else {
59138
+ if ((k / 2) % 2 == 1) { P -= contrib; }
59139
+ else { P += contrib; }
59140
+ }
59141
+ if (abs(contrib) < 1e-7) { break; }
59142
+ }
59143
+ let chi = x - (f32(n_in) / 2.0 + 0.25) * 3.14159265358979;
59144
+ return sqrt(2.0 / (3.14159265358979 * x)) * (P * cos(chi) - Q * sin(chi));
59145
+ }
59146
+
59147
+ fn _gpu_besselJ(n_in: i32, x_in: f32) -> f32 {
59148
+ var n = n_in;
59149
+ var x = x_in;
59150
+ if (x == 0.0) { return select(0.0, 1.0, n == 0); }
59151
+ var sgn: f32 = 1.0;
59152
+ if (n < 0) {
59153
+ n = -n;
59154
+ if (n % 2 != 0) { sgn = -1.0; }
59155
+ }
59156
+ if (x < 0.0) {
59157
+ x = -x;
59158
+ if (n % 2 != 0) { sgn *= -1.0; }
59159
+ }
59160
+ if (x > 25.0 + f32(n * n) / 2.0) { return sgn * _gpu_besselJ_asymptotic(n, x); }
59161
+ if (x < 5.0 + f32(n)) { return sgn * _gpu_besselJ_series(n, x); }
59162
+ // Miller's backward recurrence
59163
+ var M = max(n + 20, i32(ceil(x)) + 30);
59164
+ if (M > 200) { return sgn * _gpu_besselJ_series(n, x); }
59165
+ var vals: array<f32, 201>;
59166
+ var jp1: f32 = 0.0;
59167
+ var jk: f32 = 1.0;
59168
+ vals[M] = jk;
59169
+ for (var k: i32 = M; k >= 1; k--) {
59170
+ let jm1 = (2.0 * f32(k) / x) * jk - jp1;
59171
+ jp1 = jk;
59172
+ jk = jm1;
59173
+ vals[k - 1] = jk;
59174
+ }
59175
+ var norm = vals[0];
59176
+ for (var k2: i32 = 2; k2 <= M; k2 += 2) { norm += 2.0 * vals[k2]; }
59177
+ return sgn * vals[n] / norm;
59178
+ }
57869
59179
  `;
57870
59180
  var GPU_FRACTAL_PREAMBLE_GLSL = `
57871
59181
  float _fractal_mandelbrot(vec2 c, int maxIter) {
@@ -58329,6 +59639,16 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
58329
59639
  indent: 0,
58330
59640
  ws: (s) => s ?? "",
58331
59641
  preamble: "",
59642
+ declare: (name, typeHint) => {
59643
+ const type2 = typeHint ?? (this.languageId === "wgsl" ? "f32" : "float");
59644
+ return this.languageId === "wgsl" ? `var ${name}: ${type2}` : `${type2} ${name}`;
59645
+ },
59646
+ block: (stmts) => {
59647
+ if (stmts.length === 0) return "";
59648
+ const last = stmts.length - 1;
59649
+ stmts[last] = `return ${stmts[last]}`;
59650
+ return stmts.join(";\n");
59651
+ },
58332
59652
  ...options
58333
59653
  };
58334
59654
  }
@@ -58363,6 +59683,18 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
58363
59683
  preamble += buildComplexPreamble(code, this.languageId);
58364
59684
  if (code.includes("_gpu_gamma")) preamble += GPU_GAMMA_PREAMBLE;
58365
59685
  if (code.includes("_gpu_erf")) preamble += GPU_ERF_PREAMBLE;
59686
+ if (code.includes("_gpu_heaviside"))
59687
+ preamble += this.languageId === "wgsl" ? GPU_HEAVISIDE_PREAMBLE_WGSL : GPU_HEAVISIDE_PREAMBLE_GLSL;
59688
+ if (code.includes("_gpu_sinc"))
59689
+ preamble += this.languageId === "wgsl" ? GPU_SINC_PREAMBLE_WGSL : GPU_SINC_PREAMBLE_GLSL;
59690
+ if (code.includes("_gpu_fresnel"))
59691
+ preamble += this.languageId === "wgsl" ? GPU_POLEVL_PREAMBLE_WGSL : GPU_POLEVL_PREAMBLE_GLSL;
59692
+ if (code.includes("_gpu_fresnelC"))
59693
+ preamble += this.languageId === "wgsl" ? GPU_FRESNELC_PREAMBLE_WGSL : GPU_FRESNELC_PREAMBLE_GLSL;
59694
+ if (code.includes("_gpu_fresnelS"))
59695
+ preamble += this.languageId === "wgsl" ? GPU_FRESNELS_PREAMBLE_WGSL : GPU_FRESNELS_PREAMBLE_GLSL;
59696
+ if (code.includes("_gpu_besselJ"))
59697
+ preamble += this.languageId === "wgsl" ? GPU_BESSELJ_PREAMBLE_WGSL : GPU_BESSELJ_PREAMBLE_GLSL;
58366
59698
  if (code.includes("_fractal_")) {
58367
59699
  preamble += this.languageId === "wgsl" ? GPU_FRACTAL_PREAMBLE_WGSL : GPU_FRACTAL_PREAMBLE_GLSL;
58368
59700
  }
@@ -58411,6 +59743,12 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
58411
59743
  const target = this.createTarget();
58412
59744
  const body = BaseCompiler.compile(expr, target);
58413
59745
  const params = parameters.map(([name, type2]) => `${type2} ${name}`).join(", ");
59746
+ if (body.includes("\n")) {
59747
+ const indented = body.split("\n").map((l) => ` ${l}`).join("\n");
59748
+ return `${returnType} ${functionName}(${params}) {
59749
+ ${indented};
59750
+ }`;
59751
+ }
58414
59752
  return `${returnType} ${functionName}(${params}) {
58415
59753
  return ${body};
58416
59754
  }`;
@@ -58514,6 +59852,14 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
58514
59852
  const target = this.createTarget();
58515
59853
  const body = BaseCompiler.compile(expr, target);
58516
59854
  const params = parameters.map(([name, type2]) => `${name}: ${toWGSLType(type2)}`).join(", ");
59855
+ if (body.includes("\n")) {
59856
+ const indented = body.split("\n").map((l) => ` ${l}`).join("\n");
59857
+ return `fn ${functionName}(${params}) -> ${toWGSLType(
59858
+ returnType
59859
+ )} {
59860
+ ${indented};
59861
+ }`;
59862
+ }
58517
59863
  return `fn ${functionName}(${params}) -> ${toWGSLType(returnType)} {
58518
59864
  return ${body};
58519
59865
  }`;
@@ -59158,7 +60504,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
59158
60504
  ];
59159
60505
  return ok({ lo: Math.min(...vals), hi: Math.max(...vals) });
59160
60506
  }
59161
- function gcd4(a, b) {
60507
+ function gcd5(a, b) {
59162
60508
  const uA = unwrapOrPropagate(a);
59163
60509
  if (!Array.isArray(uA)) return uA;
59164
60510
  const uB = unwrapOrPropagate(b);
@@ -59810,7 +61156,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
59810
61156
  factorial: factorial5,
59811
61157
  factorial2: factorial24,
59812
61158
  binomial: binomial2,
59813
- gcd: gcd4,
61159
+ gcd: gcd5,
59814
61160
  lcm: lcm4,
59815
61161
  chop: chop4,
59816
61162
  erf: erf2,
@@ -63589,6 +64935,11 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
63589
64935
  if (this.atEnd) return lhs;
63590
64936
  console.assert(lhs !== null);
63591
64937
  const index = this.index;
64938
+ if (this.options.strict === false && typeof lhs === "string" && lhs.length === 1 && /^[a-zA-Z]$/.test(lhs) && /^[2-9]$/.test(this.peek)) {
64939
+ const digit = parseInt(this.peek);
64940
+ this.index++;
64941
+ return this.parseSupsub(["Power", lhs, digit]);
64942
+ }
63592
64943
  this.skipSpace();
63593
64944
  const superscripts = [];
63594
64945
  const subscripts = [];
@@ -65693,9 +67044,9 @@ ${code}`;
65693
67044
  return ce._fn("Tan", [tanCArg]).neg();
65694
67045
  }
65695
67046
  function lcm5(a, b) {
65696
- return Math.abs(a * b) / gcd5(a, b);
67047
+ return Math.abs(a * b) / gcd6(a, b);
65697
67048
  }
65698
- function gcd5(a, b) {
67049
+ function gcd6(a, b) {
65699
67050
  a = Math.abs(a);
65700
67051
  b = Math.abs(b);
65701
67052
  while (b) {
@@ -67381,10 +68732,10 @@ ${code}`;
67381
68732
  _setComputeEngineClass(ComputeEngine);
67382
68733
 
67383
68734
  // src/compute-engine.ts
67384
- var version = "0.53.0";
68735
+ var version = "0.54.0";
67385
68736
  globalThis[/* @__PURE__ */ Symbol.for("io.cortexjs.compute-engine")] = {
67386
68737
  ComputeEngine: ComputeEngine.prototype.constructor,
67387
- version: "0.53.0"
68738
+ version: "0.54.0"
67388
68739
  };
67389
68740
  return __toCommonJS(compute_engine_exports);
67390
68741
  })();