@cortex-js/compute-engine 0.55.2 → 0.55.3

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 (257) hide show
  1. package/dist/compile.esm.js +385 -147
  2. package/dist/compile.min.esm.js +51 -51
  3. package/dist/compile.min.umd.cjs +52 -52
  4. package/dist/compile.umd.cjs +385 -147
  5. package/dist/compute-engine.esm.js +390 -145
  6. package/dist/compute-engine.min.esm.js +74 -74
  7. package/dist/compute-engine.min.umd.cjs +74 -74
  8. package/dist/compute-engine.umd.cjs +390 -145
  9. package/dist/core.esm.js +387 -134
  10. package/dist/core.min.esm.js +72 -72
  11. package/dist/core.min.umd.cjs +72 -72
  12. package/dist/core.umd.cjs +387 -134
  13. package/dist/interval.esm.js +2 -2
  14. package/dist/interval.min.esm.js +2 -2
  15. package/dist/interval.min.umd.cjs +2 -2
  16. package/dist/interval.umd.cjs +2 -2
  17. package/dist/latex-syntax.esm.js +2 -2
  18. package/dist/latex-syntax.min.esm.js +2 -2
  19. package/dist/latex-syntax.min.umd.cjs +2 -2
  20. package/dist/latex-syntax.umd.cjs +2 -2
  21. package/dist/math-json.esm.js +2 -2
  22. package/dist/math-json.min.esm.js +2 -2
  23. package/dist/math-json.min.umd.cjs +2 -2
  24. package/dist/math-json.umd.cjs +2 -2
  25. package/dist/numerics.esm.js +2 -2
  26. package/dist/numerics.min.esm.js +2 -2
  27. package/dist/numerics.min.umd.cjs +2 -2
  28. package/dist/numerics.umd.cjs +2 -2
  29. package/dist/types/big-decimal/big-decimal.d.ts +1 -1
  30. package/dist/types/big-decimal/index.d.ts +1 -1
  31. package/dist/types/big-decimal/transcendentals.d.ts +1 -1
  32. package/dist/types/big-decimal/utils.d.ts +1 -1
  33. package/dist/types/common/ansi-codes.d.ts +1 -1
  34. package/dist/types/common/configuration-change.d.ts +1 -1
  35. package/dist/types/common/fuzzy-string-match.d.ts +1 -1
  36. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  37. package/dist/types/common/interruptible.d.ts +1 -1
  38. package/dist/types/common/one-of.d.ts +1 -1
  39. package/dist/types/common/signals.d.ts +1 -1
  40. package/dist/types/common/type/ast-nodes.d.ts +1 -1
  41. package/dist/types/common/type/boxed-type.d.ts +1 -1
  42. package/dist/types/common/type/lexer.d.ts +1 -1
  43. package/dist/types/common/type/parse.d.ts +1 -1
  44. package/dist/types/common/type/parser.d.ts +1 -1
  45. package/dist/types/common/type/primitive.d.ts +1 -1
  46. package/dist/types/common/type/reduce.d.ts +1 -1
  47. package/dist/types/common/type/serialize.d.ts +1 -1
  48. package/dist/types/common/type/subtype.d.ts +1 -1
  49. package/dist/types/common/type/type-builder.d.ts +1 -1
  50. package/dist/types/common/type/types.d.ts +1 -1
  51. package/dist/types/common/type/utils.d.ts +1 -1
  52. package/dist/types/common/utils.d.ts +1 -1
  53. package/dist/types/compile.d.ts +1 -1
  54. package/dist/types/compute-engine/assume.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  60. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
  66. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  68. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  69. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +1 -1
  70. package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +1 -1
  71. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  72. package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
  73. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  74. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  75. package/dist/types/compute-engine/boxed-expression/constants.d.ts +1 -1
  76. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  77. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  78. package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
  79. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  80. package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
  81. package/dist/types/compute-engine/boxed-expression/inequality-bounds.d.ts +1 -1
  82. package/dist/types/compute-engine/boxed-expression/init-lazy-refs.d.ts +1 -1
  83. package/dist/types/compute-engine/boxed-expression/invisible-operator.d.ts +1 -1
  84. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  85. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  86. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  87. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  88. package/dist/types/compute-engine/boxed-expression/pattern-utils.d.ts +1 -1
  89. package/dist/types/compute-engine/boxed-expression/polynomial-degree.d.ts +1 -1
  90. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  91. package/dist/types/compute-engine/boxed-expression/predicates.d.ts +1 -1
  92. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  93. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  94. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  95. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  96. package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +1 -1
  97. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  98. package/dist/types/compute-engine/boxed-expression/stochastic-equal.d.ts +1 -1
  99. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  100. package/dist/types/compute-engine/boxed-expression/type-guards.d.ts +1 -1
  101. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  102. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  103. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  104. package/dist/types/compute-engine/compilation/base-compiler.d.ts +8 -7
  105. package/dist/types/compute-engine/compilation/compile-expression.d.ts +1 -1
  106. package/dist/types/compute-engine/compilation/constant-folding.d.ts +50 -0
  107. package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
  108. package/dist/types/compute-engine/compilation/gpu-target.d.ts +1 -1
  109. package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +1 -1
  110. package/dist/types/compute-engine/compilation/javascript-target.d.ts +23 -1
  111. package/dist/types/compute-engine/compilation/python-target.d.ts +1 -1
  112. package/dist/types/compute-engine/compilation/types.d.ts +1 -1
  113. package/dist/types/compute-engine/compilation/wgsl-target.d.ts +1 -1
  114. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  115. package/dist/types/compute-engine/engine-assumptions.d.ts +1 -1
  116. package/dist/types/compute-engine/engine-cache.d.ts +1 -1
  117. package/dist/types/compute-engine/engine-common-symbols.d.ts +1 -1
  118. package/dist/types/compute-engine/engine-compilation-targets.d.ts +1 -1
  119. package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +1 -1
  120. package/dist/types/compute-engine/engine-declarations.d.ts +1 -1
  121. package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +1 -1
  122. package/dist/types/compute-engine/engine-extension-contracts.d.ts +1 -1
  123. package/dist/types/compute-engine/engine-library-bootstrap.d.ts +1 -1
  124. package/dist/types/compute-engine/engine-numeric-configuration.d.ts +1 -1
  125. package/dist/types/compute-engine/engine-runtime-state.d.ts +1 -1
  126. package/dist/types/compute-engine/engine-scope.d.ts +1 -1
  127. package/dist/types/compute-engine/engine-sequences.d.ts +1 -1
  128. package/dist/types/compute-engine/engine-simplification-rules.d.ts +1 -1
  129. package/dist/types/compute-engine/engine-startup-coordinator.d.ts +1 -1
  130. package/dist/types/compute-engine/engine-type-resolver.d.ts +1 -1
  131. package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +1 -1
  132. package/dist/types/compute-engine/free-functions.d.ts +1 -1
  133. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  134. package/dist/types/compute-engine/global-types.d.ts +1 -1
  135. package/dist/types/compute-engine/index.d.ts +1 -1
  136. package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
  137. package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
  138. package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
  139. package/dist/types/compute-engine/interval/index.d.ts +1 -1
  140. package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
  141. package/dist/types/compute-engine/interval/types.d.ts +1 -1
  142. package/dist/types/compute-engine/interval/util.d.ts +1 -1
  143. package/dist/types/compute-engine/latex-syntax/dictionary/default-dictionary.d.ts +1 -1
  144. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  145. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  146. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  147. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  148. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  149. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  150. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  151. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  152. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  153. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  154. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  155. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  156. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  157. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-units.d.ts +1 -1
  158. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  159. package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +1 -1
  160. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  161. package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +1 -1
  162. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
  163. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  164. package/dist/types/compute-engine/latex-syntax/serialize-dms.d.ts +1 -1
  165. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  166. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  167. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  168. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  169. package/dist/types/compute-engine/latex-syntax/types.d.ts +1 -1
  170. package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
  171. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  172. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  173. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  174. package/dist/types/compute-engine/library/colors.d.ts +1 -1
  175. package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
  176. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  177. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  178. package/dist/types/compute-engine/library/core.d.ts +1 -1
  179. package/dist/types/compute-engine/library/fractals.d.ts +1 -1
  180. package/dist/types/compute-engine/library/library.d.ts +1 -1
  181. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  182. package/dist/types/compute-engine/library/logic-analysis.d.ts +1 -1
  183. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  184. package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
  185. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  186. package/dist/types/compute-engine/library/quantity-arithmetic.d.ts +1 -1
  187. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  188. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  189. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  190. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  191. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  192. package/dist/types/compute-engine/library/type-handlers.d.ts +1 -1
  193. package/dist/types/compute-engine/library/unit-data.d.ts +1 -1
  194. package/dist/types/compute-engine/library/units.d.ts +1 -1
  195. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  196. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  197. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  198. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  199. package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
  200. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  201. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  202. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  203. package/dist/types/compute-engine/numerics/linear-algebra.d.ts +1 -1
  204. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  205. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  206. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  207. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  208. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  209. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  210. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  211. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  212. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  213. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  214. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  215. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  216. package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
  217. package/dist/types/compute-engine/oeis.d.ts +1 -1
  218. package/dist/types/compute-engine/sequence.d.ts +1 -1
  219. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  220. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  221. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  222. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  223. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  224. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  225. package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
  226. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  227. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  228. package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
  229. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  230. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  231. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  232. package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
  233. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  234. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  235. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  236. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  237. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  238. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  239. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  240. package/dist/types/compute-engine/types-definitions.d.ts +1 -1
  241. package/dist/types/compute-engine/types-engine.d.ts +1 -1
  242. package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
  243. package/dist/types/compute-engine/types-expression.d.ts +1 -1
  244. package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
  245. package/dist/types/compute-engine/types-kernel-serialization.d.ts +1 -1
  246. package/dist/types/compute-engine/types-serialization.d.ts +1 -1
  247. package/dist/types/compute-engine/types.d.ts +1 -1
  248. package/dist/types/compute-engine.d.ts +1 -1
  249. package/dist/types/core.d.ts +1 -1
  250. package/dist/types/interval.d.ts +1 -1
  251. package/dist/types/latex-syntax.d.ts +2 -2
  252. package/dist/types/math-json/symbols.d.ts +1 -1
  253. package/dist/types/math-json/types.d.ts +1 -1
  254. package/dist/types/math-json/utils.d.ts +1 -1
  255. package/dist/types/math-json.d.ts +2 -2
  256. package/dist/types/numerics.d.ts +1 -1
  257. package/package.json +1 -1
package/dist/core.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.55.2 */
1
+ /** Compute Engine 0.55.3 */
2
2
 
3
3
  // node_modules/complex-esm/dist/src/complex.js
4
4
  var cosh = Math.cosh || function(x) {
@@ -30684,8 +30684,7 @@ var ARITHMETIC_LIBRARY = [
30684
30684
  broadcastable: true,
30685
30685
  idempotent: true,
30686
30686
  complexity: 1200,
30687
- signature: "(number) -> number",
30688
- type: ([x]) => x.type,
30687
+ signature: "(number) -> real",
30689
30688
  sgn: ([x]) => {
30690
30689
  if (x.isSame(0)) return "zero";
30691
30690
  if (isNumber(x)) return "positive";
@@ -54236,8 +54235,11 @@ var BaseCompiler = class _BaseCompiler {
54236
54235
  for (const local of locals) {
54237
54236
  for (const arg of args) {
54238
54237
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
54239
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
54238
+ const rhs = arg.ops[1];
54239
+ if (_BaseCompiler.isComplexValued(rhs)) {
54240
54240
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
54241
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
54242
+ typeHints[local] = isWGSL ? "i32" : "int";
54241
54243
  }
54242
54244
  break;
54243
54245
  }
@@ -54442,12 +54444,9 @@ var BaseCompiler = class _BaseCompiler {
54442
54444
  /**
54443
54445
  * Determine at compile time whether an expression produces a complex value.
54444
54446
  *
54445
- * Rules:
54446
- * - Numbers: complex if im !== 0
54447
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
54448
- * (undefined is treated as real -- assume-real policy)
54449
- * - Functions: Abs, Arg, Re, Im always return real.
54450
- * All others: complex if any operand is complex.
54447
+ * Uses the expression's declared type (from operator signatures) when
54448
+ * available. Falls back to operand inspection for functions whose
54449
+ * return type is unknown.
54451
54450
  */
54452
54451
  static isComplexValued(expr2) {
54453
54452
  if (isNumber(expr2)) return expr2.im !== 0;
@@ -54458,13 +54457,24 @@ var BaseCompiler = class _BaseCompiler {
54458
54457
  return t.matches("complex") && !t.matches("real");
54459
54458
  }
54460
54459
  if (isFunction2(expr2)) {
54461
- const op = expr2.operator;
54462
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
54463
- return false;
54460
+ const t = expr2.type;
54461
+ if (t.matches("complex") && !t.matches("real")) return true;
54462
+ if (t.matches("real")) return false;
54464
54463
  return expr2.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
54465
54464
  }
54466
54465
  return false;
54467
54466
  }
54467
+ /** True if the expression is provably integer-typed. */
54468
+ static isIntegerValued(expr2) {
54469
+ if (isNumber(expr2)) return expr2.im === 0 && Number.isInteger(expr2.re);
54470
+ const t = expr2.type;
54471
+ return t ? t.matches("integer") : false;
54472
+ }
54473
+ /** True if the expression is provably non-negative (sign ≥ 0). */
54474
+ static isNonNegative(expr2) {
54475
+ if (isNumber(expr2)) return expr2.im === 0 && expr2.re >= 0;
54476
+ return expr2.isNonNegative === true;
54477
+ }
54468
54478
  /**
54469
54479
  * Generate a temporary variable name
54470
54480
  */
@@ -54718,14 +54728,27 @@ function compile(expr2, options) {
54718
54728
  } catch (e) {
54719
54729
  if (options?.fallback ?? true) {
54720
54730
  console.warn(
54721
- `Compilation fallback for "${expr2.operator}": ${e.message}`
54731
+ `Compilation fallback for "${expr2.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
54722
54732
  );
54733
+ const ce = expr2.engine;
54734
+ const fallbackRun = ((vars) => {
54735
+ ce.pushScope();
54736
+ try {
54737
+ if (vars && typeof vars === "object") {
54738
+ for (const [k, v] of Object.entries(vars))
54739
+ ce.assign(k, v);
54740
+ }
54741
+ return expr2.evaluate().re;
54742
+ } finally {
54743
+ ce.popScope();
54744
+ }
54745
+ });
54723
54746
  return {
54724
54747
  target: options?.to ?? "javascript",
54725
54748
  success: false,
54726
54749
  code: "",
54727
54750
  calling: "expression",
54728
- run: applicableN1(expr2)
54751
+ run: fallbackRun
54729
54752
  };
54730
54753
  }
54731
54754
  throw e;
@@ -59875,6 +59898,86 @@ function resetCommonSymbols(commonSymbols) {
59875
59898
  for (const symbol2 of Object.values(commonSymbols)) symbol2?.reset();
59876
59899
  }
59877
59900
 
59901
+ // src/compute-engine/compilation/constant-folding.ts
59902
+ function formatFloat(n) {
59903
+ const str = n.toString();
59904
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
59905
+ return `${str}.0`;
59906
+ }
59907
+ return str;
59908
+ }
59909
+ function tryGetConstant(expr2) {
59910
+ if (!isNumber(expr2)) return void 0;
59911
+ if (expr2.im !== 0) return void 0;
59912
+ const re = expr2.re;
59913
+ if (!isFinite(re)) return void 0;
59914
+ return re;
59915
+ }
59916
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
59917
+ function foldTerms(terms, identity, op) {
59918
+ const identityValue = op === "+" ? 0 : 1;
59919
+ let numericAcc = null;
59920
+ const symbolic = [];
59921
+ for (const term of terms) {
59922
+ if (NUMERIC_LITERAL_RE.test(term)) {
59923
+ const val = parseFloat(term);
59924
+ if (op === "*" && val === 0) return "0.0";
59925
+ if (numericAcc === null) {
59926
+ numericAcc = val;
59927
+ } else {
59928
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
59929
+ }
59930
+ } else {
59931
+ symbolic.push(term);
59932
+ }
59933
+ }
59934
+ if (numericAcc !== null && numericAcc !== identityValue) {
59935
+ symbolic.unshift(formatFloat(numericAcc));
59936
+ }
59937
+ if (symbolic.length === 0) {
59938
+ if (numericAcc !== null) return formatFloat(numericAcc);
59939
+ return identity;
59940
+ }
59941
+ if (symbolic.length === 1) return symbolic[0];
59942
+ return symbolic.join(op === "+" ? " + " : " * ");
59943
+ }
59944
+ function tryGetComplexParts(expr2, compile3) {
59945
+ if (isSymbol2(expr2, "ImaginaryUnit")) {
59946
+ return { re: null, im: "1.0" };
59947
+ }
59948
+ if (isNumber(expr2) && expr2.im !== 0) {
59949
+ const re = expr2.re;
59950
+ const im = expr2.im;
59951
+ return {
59952
+ re: re !== 0 ? formatFloat(re) : null,
59953
+ im: formatFloat(im)
59954
+ };
59955
+ }
59956
+ if (isFunction2(expr2, "Multiply")) {
59957
+ const ops = expr2.ops;
59958
+ const iIndex = ops.findIndex(
59959
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
59960
+ );
59961
+ if (iIndex >= 0) {
59962
+ const iFactor = ops[iIndex];
59963
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
59964
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
59965
+ if (remaining.length === 0) {
59966
+ return { re: null, im: formatFloat(iScale) };
59967
+ }
59968
+ const compiledFactors = remaining.map((r) => compile3(r));
59969
+ if (iScale !== 1)
59970
+ compiledFactors.unshift(formatFloat(iScale));
59971
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
59972
+ return { re: null, im: imCode };
59973
+ }
59974
+ }
59975
+ if (BaseCompiler.isComplexValued(expr2)) {
59976
+ return null;
59977
+ }
59978
+ return { re: compile3(expr2), im: null };
59979
+ }
59980
+
59878
59981
  // src/compute-engine/compilation/javascript-target.ts
59879
59982
  var JAVASCRIPT_OPERATORS = {
59880
59983
  Add: ["+", 11],
@@ -59898,12 +60001,21 @@ var JAVASCRIPT_FUNCTIONS = {
59898
60001
  Abs: (args, compile3) => {
59899
60002
  if (BaseCompiler.isComplexValued(args[0]))
59900
60003
  return `_SYS.cabs(${compile3(args[0])})`;
60004
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
59901
60005
  return `Math.abs(${compile3(args[0])})`;
59902
60006
  },
59903
60007
  Add: (args, compile3) => {
59904
60008
  if (args.length === 1) return compile3(args[0]);
59905
60009
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
59906
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" + ")})`;
60010
+ if (!anyComplex) {
60011
+ const constants = args.map(tryGetConstant);
60012
+ if (constants.every((c) => c !== void 0))
60013
+ return String(constants.reduce((a, b) => a + b, 0));
60014
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
60015
+ if (nonZero.length === 0) return "0";
60016
+ if (nonZero.length === 1) return compile3(nonZero[0]);
60017
+ return `(${nonZero.map((x) => compile3(x)).join(" + ")})`;
60018
+ }
59907
60019
  const parts = args.map((a) => {
59908
60020
  const code = compile3(a);
59909
60021
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -59960,7 +60072,10 @@ var JAVASCRIPT_FUNCTIONS = {
59960
60072
  return `Math.atan(${compile3(args[0])})`;
59961
60073
  },
59962
60074
  Artanh: "Math.atanh",
59963
- Ceil: "Math.ceil",
60075
+ Ceil: (args, compile3) => {
60076
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60077
+ return `Math.ceil(${compile3(args[0])})`;
60078
+ },
59964
60079
  Chop: "_SYS.chop",
59965
60080
  Cos: (args, compile3) => {
59966
60081
  if (BaseCompiler.isComplexValued(args[0]))
@@ -60003,7 +60118,10 @@ var JAVASCRIPT_FUNCTIONS = {
60003
60118
  return `_SYS.cexp(${compile3(args[0])})`;
60004
60119
  return `Math.exp(${compile3(args[0])})`;
60005
60120
  },
60006
- Floor: "Math.floor",
60121
+ Floor: (args, compile3) => {
60122
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60123
+ return `Math.floor(${compile3(args[0])})`;
60124
+ },
60007
60125
  Fract: ([x], compile3) => {
60008
60126
  if (x === null) throw new Error("Fract: no argument");
60009
60127
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile3(x));
@@ -60099,12 +60217,20 @@ var JAVASCRIPT_FUNCTIONS = {
60099
60217
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
60100
60218
  return `_SYS.cpow(${compile3(base)}, ${compile3(exp3)})`;
60101
60219
  }
60102
- const expVal = exp3.re;
60103
- if (expVal === 0.5) return `Math.sqrt(${compile3(base)})`;
60104
- if (expVal === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60105
- if (expVal === 1) return compile3(base);
60106
- if (expVal === -1) return `(1 / (${compile3(base)}))`;
60107
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60220
+ const bConst = tryGetConstant(base);
60221
+ const eConst = tryGetConstant(exp3);
60222
+ if (bConst !== void 0 && eConst !== void 0)
60223
+ return String(Math.pow(bConst, eConst));
60224
+ if (eConst === 0) return "1";
60225
+ if (eConst === 1) return compile3(base);
60226
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
60227
+ const code = compile3(base);
60228
+ return `(${code} * ${code})`;
60229
+ }
60230
+ if (eConst === -1) return `(1 / (${compile3(base)}))`;
60231
+ if (eConst === 0.5) return `Math.sqrt(${compile3(base)})`;
60232
+ if (eConst === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60233
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60108
60234
  return `Math.pow(${compile3(base)}, ${compile3(exp3)})`;
60109
60235
  },
60110
60236
  Range: (args, compile3) => {
@@ -60141,16 +60267,29 @@ var JAVASCRIPT_FUNCTIONS = {
60141
60267
  Root: ([arg, exp3], compile3) => {
60142
60268
  if (arg === null) throw new Error("Root: no argument");
60143
60269
  if (exp3 === null) return `Math.sqrt(${compile3(arg)})`;
60144
- if (exp3?.re === 2) return `Math.sqrt(${compile3(arg)})`;
60145
- if (exp3?.re === 3) return `Math.cbrt(${compile3(arg)})`;
60146
- if (!isNaN(exp3?.re)) return `Math.pow(${compile3(arg)}, ${1 / exp3.re})`;
60270
+ const aConst = tryGetConstant(arg);
60271
+ const nConst = tryGetConstant(exp3);
60272
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
60273
+ return String(Math.pow(aConst, 1 / nConst));
60274
+ if (nConst === 2) return `Math.sqrt(${compile3(arg)})`;
60275
+ if (nConst === 3) return `Math.cbrt(${compile3(arg)})`;
60276
+ if (nConst !== void 0) return `Math.pow(${compile3(arg)}, ${1 / nConst})`;
60147
60277
  return `Math.pow(${compile3(arg)}, 1 / (${compile3(exp3)}))`;
60148
60278
  },
60149
60279
  Random: "Math.random",
60150
- Round: "Math.round",
60280
+ Round: (args, compile3) => {
60281
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60282
+ return `Math.round(${compile3(args[0])})`;
60283
+ },
60151
60284
  Square: (args, compile3) => {
60152
60285
  const arg = args[0];
60153
60286
  if (arg === null) throw new Error("Square: no argument");
60287
+ const c = tryGetConstant(arg);
60288
+ if (c !== void 0) return String(c * c);
60289
+ if (isSymbol2(arg)) {
60290
+ const code = compile3(arg);
60291
+ return `(${code} * ${code})`;
60292
+ }
60154
60293
  return `Math.pow(${compile3(arg)}, 2)`;
60155
60294
  },
60156
60295
  Sec: (args, compile3) => {
@@ -60183,6 +60322,8 @@ var JAVASCRIPT_FUNCTIONS = {
60183
60322
  Sqrt: (args, compile3) => {
60184
60323
  if (BaseCompiler.isComplexValued(args[0]))
60185
60324
  return `_SYS.csqrt(${compile3(args[0])})`;
60325
+ const c = tryGetConstant(args[0]);
60326
+ if (c !== void 0) return String(Math.sqrt(c));
60186
60327
  return `Math.sqrt(${compile3(args[0])})`;
60187
60328
  },
60188
60329
  Tan: (args, compile3) => {
@@ -60199,9 +60340,14 @@ var JAVASCRIPT_FUNCTIONS = {
60199
60340
  if (a === null || b === null) throw new Error("Mod: missing argument");
60200
60341
  const ca = compile3(a);
60201
60342
  const cb = compile3(b);
60343
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
60344
+ return `(${ca} % ${cb})`;
60202
60345
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
60203
60346
  },
60204
- Truncate: "Math.trunc",
60347
+ Truncate: (args, compile3) => {
60348
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60349
+ return `Math.trunc(${compile3(args[0])})`;
60350
+ },
60205
60351
  Remainder: ([a, b], compile3) => {
60206
60352
  if (a === null || b === null)
60207
60353
  throw new Error("Remainder: missing argument");
@@ -60209,25 +60355,20 @@ var JAVASCRIPT_FUNCTIONS = {
60209
60355
  a
60210
60356
  )} / ${compile3(b)}))`;
60211
60357
  },
60212
- // Arithmetic operators handled as functions for completeness
60213
- Subtract: ([a, b], compile3) => {
60214
- if (a === null || b === null) throw new Error("Subtract: missing argument");
60215
- const ac = BaseCompiler.isComplexValued(a);
60216
- const bc = BaseCompiler.isComplexValued(b);
60217
- if (!ac && !bc) return `(${compile3(a)} - ${compile3(b)})`;
60218
- const ca = compile3(a);
60219
- const cb = compile3(b);
60220
- const reA = ac ? `(${ca}).re` : ca;
60221
- const imA = ac ? `(${ca}).im` : "0";
60222
- const reB = bc ? `(${cb}).re` : cb;
60223
- const imB = bc ? `(${cb}).im` : "0";
60224
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
60225
- },
60358
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
60359
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
60226
60360
  Divide: ([a, b], compile3) => {
60227
60361
  if (a === null || b === null) throw new Error("Divide: missing argument");
60228
60362
  const ac = BaseCompiler.isComplexValued(a);
60229
60363
  const bc = BaseCompiler.isComplexValued(b);
60230
- if (!ac && !bc) return `(${compile3(a)} / ${compile3(b)})`;
60364
+ if (!ac && !bc) {
60365
+ const ca = tryGetConstant(a);
60366
+ const cb = tryGetConstant(b);
60367
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
60368
+ return String(ca / cb);
60369
+ if (cb === 1) return compile3(a);
60370
+ return `(${compile3(a)} / ${compile3(b)})`;
60371
+ }
60231
60372
  if (ac && bc) {
60232
60373
  return `(() => { const _a = ${compile3(a)}, _b = ${compile3(
60233
60374
  b
@@ -60244,13 +60385,26 @@ var JAVASCRIPT_FUNCTIONS = {
60244
60385
  },
60245
60386
  Negate: ([x], compile3) => {
60246
60387
  if (x === null) throw new Error("Negate: no argument");
60247
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile3(x)})`;
60388
+ if (!BaseCompiler.isComplexValued(x)) {
60389
+ const c = tryGetConstant(x);
60390
+ if (c !== void 0) return String(-c);
60391
+ return `(-${compile3(x)})`;
60392
+ }
60248
60393
  return `_SYS.cneg(${compile3(x)})`;
60249
60394
  },
60250
60395
  Multiply: (args, compile3) => {
60251
60396
  if (args.length === 1) return compile3(args[0]);
60252
60397
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60253
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" * ")})`;
60398
+ if (!anyComplex) {
60399
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
60400
+ const constants = args.map(tryGetConstant);
60401
+ if (constants.every((c) => c !== void 0))
60402
+ return String(constants.reduce((a, b) => a * b, 1));
60403
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
60404
+ if (nonOne.length === 0) return "1";
60405
+ if (nonOne.length === 1) return compile3(nonOne[0]);
60406
+ return `(${nonOne.map((x) => compile3(x)).join(" * ")})`;
60407
+ }
60254
60408
  if (args.length === 2) {
60255
60409
  const ac = BaseCompiler.isComplexValued(args[0]);
60256
60410
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -60335,20 +60489,30 @@ var JAVASCRIPT_FUNCTIONS = {
60335
60489
  AiryAi: "_SYS.airyAi",
60336
60490
  AiryBi: "_SYS.airyBi",
60337
60491
  // Combinatorics
60492
+ Mandelbrot: ([c, maxIter], compile3) => {
60493
+ if (c === null || maxIter === null)
60494
+ throw new Error("Mandelbrot: missing arguments");
60495
+ return `_SYS.mandelbrot(${compile3(c)}, ${compile3(maxIter)})`;
60496
+ },
60497
+ Julia: ([z, c, maxIter], compile3) => {
60498
+ if (z === null || c === null || maxIter === null)
60499
+ throw new Error("Julia: missing arguments");
60500
+ return `_SYS.julia(${compile3(z)}, ${compile3(c)}, ${compile3(maxIter)})`;
60501
+ },
60338
60502
  Binomial: (args, compile3) => `_SYS.binomial(${compile3(args[0])}, ${compile3(args[1])})`,
60339
60503
  Fibonacci: "_SYS.fibonacci",
60340
60504
  // Complex-specific functions
60341
- Re: (args, compile3) => {
60505
+ Real: (args, compile3) => {
60342
60506
  if (BaseCompiler.isComplexValued(args[0]))
60343
60507
  return `(${compile3(args[0])}).re`;
60344
60508
  return compile3(args[0]);
60345
60509
  },
60346
- Im: (args, compile3) => {
60510
+ Imaginary: (args, compile3) => {
60347
60511
  if (BaseCompiler.isComplexValued(args[0]))
60348
60512
  return `(${compile3(args[0])}).im`;
60349
60513
  return "0";
60350
60514
  },
60351
- Arg: (args, compile3) => {
60515
+ Argument: (args, compile3) => {
60352
60516
  if (BaseCompiler.isComplexValued(args[0]))
60353
60517
  return `_SYS.carg(${compile3(args[0])})`;
60354
60518
  return `(${compile3(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -60684,6 +60848,41 @@ var SYS_HELPERS = {
60684
60848
  sinc,
60685
60849
  fresnelS,
60686
60850
  fresnelC,
60851
+ mandelbrot: (c, maxIter) => {
60852
+ let zx = 0, zy = 0;
60853
+ const cx = typeof c === "number" ? c : c.re;
60854
+ const cy = typeof c === "number" ? 0 : c.im;
60855
+ const n = Math.round(maxIter);
60856
+ for (let i = 0; i < n; i++) {
60857
+ const newZx = zx * zx - zy * zy + cx;
60858
+ zy = 2 * zx * zy + cy;
60859
+ zx = newZx;
60860
+ const mag2 = zx * zx + zy * zy;
60861
+ if (mag2 > 4) {
60862
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60863
+ return Math.max(0, Math.min(1, smooth));
60864
+ }
60865
+ }
60866
+ return 1;
60867
+ },
60868
+ julia: (z, c, maxIter) => {
60869
+ let zx = typeof z === "number" ? z : z.re;
60870
+ let zy = typeof z === "number" ? 0 : z.im;
60871
+ const cx = typeof c === "number" ? c : c.re;
60872
+ const cy = typeof c === "number" ? 0 : c.im;
60873
+ const n = Math.round(maxIter);
60874
+ for (let i = 0; i < n; i++) {
60875
+ const newZx = zx * zx - zy * zy + cx;
60876
+ zy = 2 * zx * zy + cy;
60877
+ zx = newZx;
60878
+ const mag2 = zx * zx + zy * zy;
60879
+ if (mag2 > 4) {
60880
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60881
+ return Math.max(0, Math.min(1, smooth));
60882
+ }
60883
+ }
60884
+ return 1;
60885
+ },
60687
60886
  binomial: choose,
60688
60887
  fibonacci,
60689
60888
  // Complex helpers
@@ -61029,6 +61228,7 @@ var GPU_OPERATORS = {
61029
61228
  Add: ["+", 11],
61030
61229
  Negate: ["-", 14],
61031
61230
  Subtract: ["-", 11],
61231
+ // Subtract canonicalizes to Add+Negate; kept as fallback
61032
61232
  Multiply: ["*", 12],
61033
61233
  Divide: ["/", 13],
61034
61234
  Equal: ["==", 8],
@@ -61044,6 +61244,12 @@ var GPU_OPERATORS = {
61044
61244
  function gpuVec2(target) {
61045
61245
  return target?.language === "wgsl" ? "vec2f" : "vec2";
61046
61246
  }
61247
+ function compileIntArg(expr2, compile3, target) {
61248
+ const c = tryGetConstant(expr2);
61249
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
61250
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
61251
+ return `${intCast}(${compile3(expr2)})`;
61252
+ }
61047
61253
  var GPU_UNROLL_LIMIT = 100;
61048
61254
  function compileGPUSumProduct(kind, args, _compile2, target) {
61049
61255
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -61106,88 +61312,113 @@ var GPU_FUNCTIONS = {
61106
61312
  if (args.length === 0) return "0.0";
61107
61313
  if (args.length === 1) return compile3(args[0]);
61108
61314
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61109
- if (!anyComplex) return args.map((x) => compile3(x)).join(" + ");
61110
- const v2 = gpuVec2(target);
61111
- return args.map((a) => {
61112
- const code = compile3(a);
61113
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61114
- }).join(" + ");
61115
- },
61116
- Multiply: (args, compile3, _target) => {
61117
- if (args.length === 0) return "1.0";
61118
- if (args.length === 1) return compile3(args[0]);
61119
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61120
- if (!anyComplex) return args.map((x) => compile3(x)).join(" * ");
61121
- let result = compile3(args[0]);
61122
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
61123
- for (let i = 1; i < args.length; i++) {
61124
- const code = compile3(args[i]);
61125
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
61126
- if (!resultIsComplex && !argIsComplex) {
61127
- result = `(${result} * ${code})`;
61128
- } else if (resultIsComplex && !argIsComplex) {
61129
- result = `(${code} * ${result})`;
61130
- } else if (!resultIsComplex && argIsComplex) {
61131
- result = `(${result} * ${code})`;
61132
- resultIsComplex = true;
61133
- } else {
61134
- result = `_gpu_cmul(${result}, ${code})`;
61135
- }
61315
+ if (!anyComplex) {
61316
+ return foldTerms(
61317
+ args.map((x) => compile3(x)),
61318
+ "0.0",
61319
+ "+"
61320
+ );
61136
61321
  }
61137
- return result;
61138
- },
61139
- Subtract: (args, compile3, target) => {
61140
- if (args.length === 0) return "0.0";
61322
+ const parts = args.map((a) => tryGetComplexParts(a, compile3));
61323
+ if (parts.some((p) => p === null)) {
61324
+ const v2 = gpuVec2(target);
61325
+ return args.map((a) => {
61326
+ const code = compile3(a);
61327
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61328
+ }).join(" + ");
61329
+ }
61330
+ const reParts = [];
61331
+ const imParts = [];
61332
+ for (const p of parts) {
61333
+ if (p.re !== null) reParts.push(p.re);
61334
+ if (p.im !== null) imParts.push(p.im);
61335
+ }
61336
+ const reSum = foldTerms(reParts, "0.0", "+");
61337
+ const imSum = foldTerms(imParts, "0.0", "+");
61338
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
61339
+ },
61340
+ Multiply: (args, compile3, target) => {
61341
+ if (args.length === 0) return "1.0";
61141
61342
  if (args.length === 1) return compile3(args[0]);
61142
61343
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61143
61344
  if (!anyComplex) {
61144
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
61145
- let result2 = compile3(args[0]);
61146
- for (let i = 1; i < args.length; i++) {
61147
- result2 = `${result2} - ${compile3(args[i])}`;
61148
- }
61149
- return result2;
61150
- }
61151
- const v2 = gpuVec2(target);
61152
- const promote = (a) => {
61153
- const code = compile3(a);
61154
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61155
- };
61156
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
61157
- let result = promote(args[0]);
61158
- for (let i = 1; i < args.length; i++) {
61159
- result = `${result} - ${promote(args[i])}`;
61345
+ return foldTerms(
61346
+ args.map((x) => compile3(x)),
61347
+ "1.0",
61348
+ "*"
61349
+ );
61160
61350
  }
61351
+ const iIndex = args.findIndex(
61352
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
61353
+ );
61354
+ if (iIndex >= 0) {
61355
+ const iFactor = args[iIndex];
61356
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
61357
+ const realFactors = args.filter((_, i) => i !== iIndex);
61358
+ const v2 = gpuVec2(target);
61359
+ if (realFactors.length === 0)
61360
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
61361
+ const factors = realFactors.map((f) => compile3(f));
61362
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
61363
+ const imCode = foldTerms(factors, "1.0", "*");
61364
+ return `${v2}(0.0, ${imCode})`;
61365
+ }
61366
+ const realCodes = [];
61367
+ const complexCodes = [];
61368
+ for (const a of args) {
61369
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile3(a));
61370
+ else realCodes.push(compile3(a));
61371
+ }
61372
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
61373
+ let result = complexCodes[0];
61374
+ for (let i = 1; i < complexCodes.length; i++) {
61375
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
61376
+ }
61377
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
61161
61378
  return result;
61162
61379
  },
61380
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
61381
+ // The operator entry in GPU_OPERATORS handles any edge cases.
61163
61382
  Divide: (args, compile3, target) => {
61164
61383
  if (args.length === 0) return "1.0";
61165
61384
  if (args.length === 1) return compile3(args[0]);
61166
61385
  const ac = BaseCompiler.isComplexValued(args[0]);
61167
61386
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
61168
61387
  if (!ac && !bc) {
61169
- if (args.length === 2) return `${compile3(args[0])} / ${compile3(args[1])}`;
61388
+ if (args.length === 2) {
61389
+ const a = tryGetConstant(args[0]);
61390
+ const b = tryGetConstant(args[1]);
61391
+ if (a !== void 0 && b !== void 0 && b !== 0)
61392
+ return formatFloat(a / b);
61393
+ if (b === 1) return compile3(args[0]);
61394
+ return `${compile3(args[0])} / ${compile3(args[1])}`;
61395
+ }
61170
61396
  let result = compile3(args[0]);
61171
- for (let i = 1; i < args.length; i++) {
61397
+ for (let i = 1; i < args.length; i++)
61172
61398
  result = `${result} / ${compile3(args[i])}`;
61173
- }
61174
61399
  return result;
61175
61400
  }
61176
61401
  if (ac && bc) return `_gpu_cdiv(${compile3(args[0])}, ${compile3(args[1])})`;
61177
- if (ac && !bc) {
61178
- return `(${compile3(args[0])} / ${compile3(args[1])})`;
61179
- }
61402
+ if (ac && !bc) return `(${compile3(args[0])} / ${compile3(args[1])})`;
61180
61403
  const v2 = gpuVec2(target);
61181
61404
  return `_gpu_cdiv(${v2}(${compile3(args[0])}, 0.0), ${compile3(args[1])})`;
61182
61405
  },
61183
- Negate: ([x], compile3) => {
61406
+ Negate: ([x], compile3, target) => {
61184
61407
  if (x === null) throw new Error("Negate: no argument");
61408
+ const c = tryGetConstant(x);
61409
+ if (c !== void 0) return formatFloat(-c);
61410
+ if (isNumber(x) && x.im !== 0) {
61411
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
61412
+ }
61413
+ if (isSymbol2(x, "ImaginaryUnit"))
61414
+ return `${gpuVec2(target)}(0.0, -1.0)`;
61185
61415
  return `(-${compile3(x)})`;
61186
61416
  },
61187
61417
  // Standard math functions with complex dispatch
61188
61418
  Abs: (args, compile3) => {
61189
61419
  if (BaseCompiler.isComplexValued(args[0]))
61190
61420
  return `length(${compile3(args[0])})`;
61421
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
61191
61422
  return `abs(${compile3(args[0])})`;
61192
61423
  },
61193
61424
  Arccos: (args, compile3) => {
@@ -61205,7 +61436,10 @@ var GPU_FUNCTIONS = {
61205
61436
  return `_gpu_catan(${compile3(args[0])})`;
61206
61437
  return `atan(${compile3(args[0])})`;
61207
61438
  },
61208
- Ceil: "ceil",
61439
+ Ceil: (args, compile3) => {
61440
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61441
+ return `ceil(${compile3(args[0])})`;
61442
+ },
61209
61443
  Clamp: "clamp",
61210
61444
  Cos: (args, compile3) => {
61211
61445
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61219,7 +61453,10 @@ var GPU_FUNCTIONS = {
61219
61453
  return `exp(${compile3(args[0])})`;
61220
61454
  },
61221
61455
  Exp2: "exp2",
61222
- Floor: "floor",
61456
+ Floor: (args, compile3) => {
61457
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61458
+ return `floor(${compile3(args[0])})`;
61459
+ },
61223
61460
  Fract: "fract",
61224
61461
  Ln: (args, compile3) => {
61225
61462
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61241,10 +61478,25 @@ var GPU_FUNCTIONS = {
61241
61478
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile3(exp3) : `${v2}(${compile3(exp3)}, 0.0)`;
61242
61479
  return `_gpu_cpow(${bCode}, ${eCode})`;
61243
61480
  }
61481
+ const bConst = tryGetConstant(base);
61482
+ const eConst = tryGetConstant(exp3);
61483
+ if (bConst !== void 0 && eConst !== void 0)
61484
+ return formatFloat(Math.pow(bConst, eConst));
61485
+ if (eConst === 0) return "1.0";
61486
+ if (eConst === 1) return compile3(base);
61487
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
61488
+ const code = compile3(base);
61489
+ return `(${code} * ${code})`;
61490
+ }
61491
+ if (eConst === -1) return `(1.0 / ${compile3(base)})`;
61492
+ if (eConst === 0.5) return `sqrt(${compile3(base)})`;
61244
61493
  return `pow(${compile3(base)}, ${compile3(exp3)})`;
61245
61494
  },
61246
61495
  Radians: "radians",
61247
- Round: "round",
61496
+ Round: (args, compile3) => {
61497
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61498
+ return `round(${compile3(args[0])})`;
61499
+ },
61248
61500
  Sign: "sign",
61249
61501
  Sin: (args, compile3) => {
61250
61502
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61255,6 +61507,8 @@ var GPU_FUNCTIONS = {
61255
61507
  Sqrt: (args, compile3) => {
61256
61508
  if (BaseCompiler.isComplexValued(args[0]))
61257
61509
  return `_gpu_csqrt(${compile3(args[0])})`;
61510
+ const c = tryGetConstant(args[0]);
61511
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
61258
61512
  return `sqrt(${compile3(args[0])})`;
61259
61513
  },
61260
61514
  Step: "step",
@@ -61263,17 +61517,20 @@ var GPU_FUNCTIONS = {
61263
61517
  return `_gpu_ctan(${compile3(args[0])})`;
61264
61518
  return `tan(${compile3(args[0])})`;
61265
61519
  },
61266
- Truncate: "trunc",
61520
+ Truncate: (args, compile3) => {
61521
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61522
+ return `trunc(${compile3(args[0])})`;
61523
+ },
61267
61524
  // Complex-specific functions
61268
- Re: (args, compile3) => {
61525
+ Real: (args, compile3) => {
61269
61526
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).x`;
61270
61527
  return compile3(args[0]);
61271
61528
  },
61272
- Im: (args, compile3) => {
61529
+ Imaginary: (args, compile3) => {
61273
61530
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).y`;
61274
61531
  return "0.0";
61275
61532
  },
61276
- Arg: (args, compile3) => {
61533
+ Argument: (args, compile3) => {
61277
61534
  if (BaseCompiler.isComplexValued(args[0])) {
61278
61535
  const code = compile3(args[0]);
61279
61536
  return `atan(${code}.y, ${code}.x)`;
@@ -61488,13 +61745,20 @@ var GPU_FUNCTIONS = {
61488
61745
  },
61489
61746
  Square: ([x], compile3) => {
61490
61747
  if (x === null) throw new Error("Square: no argument");
61491
- const arg = compile3(x);
61492
- return `(${arg} * ${arg})`;
61748
+ if (isSymbol2(x) || isNumber(x)) {
61749
+ const arg = compile3(x);
61750
+ return `(${arg} * ${arg})`;
61751
+ }
61752
+ return `pow(${compile3(x)}, 2.0)`;
61493
61753
  },
61494
61754
  Root: ([x, n], compile3) => {
61495
61755
  if (x === null) throw new Error("Root: no argument");
61496
61756
  if (n === null || n === void 0) return `sqrt(${compile3(x)})`;
61497
- if (n?.re === 2) return `sqrt(${compile3(x)})`;
61757
+ const nConst = tryGetConstant(n);
61758
+ if (nConst === 2) return `sqrt(${compile3(x)})`;
61759
+ const xConst = tryGetConstant(x);
61760
+ if (xConst !== void 0 && nConst !== void 0)
61761
+ return formatFloat(Math.pow(xConst, 1 / nConst));
61498
61762
  return `pow(${compile3(x)}, 1.0 / ${compile3(n)})`;
61499
61763
  },
61500
61764
  // Color functions (pure-math, GPU-compilable)
@@ -61536,18 +61800,14 @@ var GPU_FUNCTIONS = {
61536
61800
  Mandelbrot: ([c, maxIter], compile3, target) => {
61537
61801
  if (c === null || maxIter === null)
61538
61802
  throw new Error("Mandelbrot: missing arguments");
61539
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61540
- return `_fractal_mandelbrot(${compile3(c)}, ${intCast}(${compile3(
61541
- maxIter
61542
- )}))`;
61803
+ const iterCode = compileIntArg(maxIter, compile3, target);
61804
+ return `_fractal_mandelbrot(${compile3(c)}, ${iterCode})`;
61543
61805
  },
61544
61806
  Julia: ([z, c, maxIter], compile3, target) => {
61545
61807
  if (z === null || c === null || maxIter === null)
61546
61808
  throw new Error("Julia: missing arguments");
61547
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61548
- return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${intCast}(${compile3(
61549
- maxIter
61550
- )}))`;
61809
+ const iterCode = compileIntArg(maxIter, compile3, target);
61810
+ return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${iterCode})`;
61551
61811
  },
61552
61812
  // Vector/Matrix operations
61553
61813
  Cross: "cross",
@@ -64179,6 +64439,7 @@ var INTERVAL_JAVASCRIPT_OPERATORS = {
64179
64439
  Add: ["_IA.add", 20],
64180
64440
  Negate: ["_IA.negate", 20],
64181
64441
  Subtract: ["_IA.sub", 20],
64442
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64182
64443
  Multiply: ["_IA.mul", 20],
64183
64444
  Divide: ["_IA.div", 20],
64184
64445
  // Comparisons return BoolInterval
@@ -64203,17 +64464,7 @@ var INTERVAL_JAVASCRIPT_FUNCTIONS = {
64203
64464
  }
64204
64465
  return result;
64205
64466
  },
64206
- Subtract: (args, compile3) => {
64207
- if (args.length === 0) return "_IA.point(0)";
64208
- if (args.length === 1) return `_IA.negate(${compile3(args[0])})`;
64209
- if (args.length === 2)
64210
- return `_IA.sub(${compile3(args[0])}, ${compile3(args[1])})`;
64211
- let result = compile3(args[0]);
64212
- for (let i = 1; i < args.length; i++) {
64213
- result = `_IA.sub(${result}, ${compile3(args[i])})`;
64214
- }
64215
- return result;
64216
- },
64467
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
64217
64468
  Multiply: (args, compile3) => {
64218
64469
  if (args.length === 0) return "_IA.point(1)";
64219
64470
  if (args.length === 1) return compile3(args[0]);
@@ -67187,7 +67438,9 @@ var ComputeEngine = class _ComputeEngine {
67187
67438
  }
67188
67439
  return new ExactNumericValue(value, makeNumericValue);
67189
67440
  }
67190
- throw Error("Unexpected value");
67441
+ throw Error(
67442
+ `Unexpected value: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`
67443
+ );
67191
67444
  }
67192
67445
  /**
67193
67446
  * The cost function is used to determine the "cost" of an expression. For example, when simplifying an expression, the simplification that results in the lowest cost is chosen.
@@ -67658,7 +67911,7 @@ var ComputeEngine = class _ComputeEngine {
67658
67911
  _setDefaultEngineFactory(() => new ComputeEngine());
67659
67912
 
67660
67913
  // src/core.ts
67661
- var version = "0.55.2";
67914
+ var version = "0.55.3";
67662
67915
  export {
67663
67916
  ComputeEngine,
67664
67917
  N,