@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.umd.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /** ComputeEngineCore 0.55.2 */
1
+ /** ComputeEngineCore 0.55.3 */
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.ComputeEngineCore = {}));})(this, (function (exports) { 'use strict';
3
3
  var ComputeEngineCore = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -30731,8 +30731,7 @@ ${lines.join("\n")}`;
30731
30731
  broadcastable: true,
30732
30732
  idempotent: true,
30733
30733
  complexity: 1200,
30734
- signature: "(number) -> number",
30735
- type: ([x]) => x.type,
30734
+ signature: "(number) -> real",
30736
30735
  sgn: ([x]) => {
30737
30736
  if (x.isSame(0)) return "zero";
30738
30737
  if (isNumber(x)) return "positive";
@@ -54283,8 +54282,11 @@ Error in definition of "${name}"`,
54283
54282
  for (const local of locals) {
54284
54283
  for (const arg of args) {
54285
54284
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
54286
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
54285
+ const rhs = arg.ops[1];
54286
+ if (_BaseCompiler.isComplexValued(rhs)) {
54287
54287
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
54288
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
54289
+ typeHints[local] = isWGSL ? "i32" : "int";
54288
54290
  }
54289
54291
  break;
54290
54292
  }
@@ -54489,12 +54491,9 @@ Error in definition of "${name}"`,
54489
54491
  /**
54490
54492
  * Determine at compile time whether an expression produces a complex value.
54491
54493
  *
54492
- * Rules:
54493
- * - Numbers: complex if im !== 0
54494
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
54495
- * (undefined is treated as real -- assume-real policy)
54496
- * - Functions: Abs, Arg, Re, Im always return real.
54497
- * All others: complex if any operand is complex.
54494
+ * Uses the expression's declared type (from operator signatures) when
54495
+ * available. Falls back to operand inspection for functions whose
54496
+ * return type is unknown.
54498
54497
  */
54499
54498
  static isComplexValued(expr2) {
54500
54499
  if (isNumber(expr2)) return expr2.im !== 0;
@@ -54505,13 +54504,24 @@ Error in definition of "${name}"`,
54505
54504
  return t.matches("complex") && !t.matches("real");
54506
54505
  }
54507
54506
  if (isFunction2(expr2)) {
54508
- const op = expr2.operator;
54509
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
54510
- return false;
54507
+ const t = expr2.type;
54508
+ if (t.matches("complex") && !t.matches("real")) return true;
54509
+ if (t.matches("real")) return false;
54511
54510
  return expr2.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
54512
54511
  }
54513
54512
  return false;
54514
54513
  }
54514
+ /** True if the expression is provably integer-typed. */
54515
+ static isIntegerValued(expr2) {
54516
+ if (isNumber(expr2)) return expr2.im === 0 && Number.isInteger(expr2.re);
54517
+ const t = expr2.type;
54518
+ return t ? t.matches("integer") : false;
54519
+ }
54520
+ /** True if the expression is provably non-negative (sign ≥ 0). */
54521
+ static isNonNegative(expr2) {
54522
+ if (isNumber(expr2)) return expr2.im === 0 && expr2.re >= 0;
54523
+ return expr2.isNonNegative === true;
54524
+ }
54515
54525
  /**
54516
54526
  * Generate a temporary variable name
54517
54527
  */
@@ -54765,14 +54775,27 @@ Error in definition of "${name}"`,
54765
54775
  } catch (e) {
54766
54776
  if (options?.fallback ?? true) {
54767
54777
  console.warn(
54768
- `Compilation fallback for "${expr2.operator}": ${e.message}`
54778
+ `Compilation fallback for "${expr2.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
54769
54779
  );
54780
+ const ce = expr2.engine;
54781
+ const fallbackRun = ((vars) => {
54782
+ ce.pushScope();
54783
+ try {
54784
+ if (vars && typeof vars === "object") {
54785
+ for (const [k, v] of Object.entries(vars))
54786
+ ce.assign(k, v);
54787
+ }
54788
+ return expr2.evaluate().re;
54789
+ } finally {
54790
+ ce.popScope();
54791
+ }
54792
+ });
54770
54793
  return {
54771
54794
  target: options?.to ?? "javascript",
54772
54795
  success: false,
54773
54796
  code: "",
54774
54797
  calling: "expression",
54775
- run: applicableN1(expr2)
54798
+ run: fallbackRun
54776
54799
  };
54777
54800
  }
54778
54801
  throw e;
@@ -59922,6 +59945,86 @@ Error in definition of "${name}"`,
59922
59945
  for (const symbol2 of Object.values(commonSymbols)) symbol2?.reset();
59923
59946
  }
59924
59947
 
59948
+ // src/compute-engine/compilation/constant-folding.ts
59949
+ function formatFloat(n) {
59950
+ const str = n.toString();
59951
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
59952
+ return `${str}.0`;
59953
+ }
59954
+ return str;
59955
+ }
59956
+ function tryGetConstant(expr2) {
59957
+ if (!isNumber(expr2)) return void 0;
59958
+ if (expr2.im !== 0) return void 0;
59959
+ const re = expr2.re;
59960
+ if (!isFinite(re)) return void 0;
59961
+ return re;
59962
+ }
59963
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
59964
+ function foldTerms(terms, identity, op) {
59965
+ const identityValue = op === "+" ? 0 : 1;
59966
+ let numericAcc = null;
59967
+ const symbolic = [];
59968
+ for (const term of terms) {
59969
+ if (NUMERIC_LITERAL_RE.test(term)) {
59970
+ const val = parseFloat(term);
59971
+ if (op === "*" && val === 0) return "0.0";
59972
+ if (numericAcc === null) {
59973
+ numericAcc = val;
59974
+ } else {
59975
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
59976
+ }
59977
+ } else {
59978
+ symbolic.push(term);
59979
+ }
59980
+ }
59981
+ if (numericAcc !== null && numericAcc !== identityValue) {
59982
+ symbolic.unshift(formatFloat(numericAcc));
59983
+ }
59984
+ if (symbolic.length === 0) {
59985
+ if (numericAcc !== null) return formatFloat(numericAcc);
59986
+ return identity;
59987
+ }
59988
+ if (symbolic.length === 1) return symbolic[0];
59989
+ return symbolic.join(op === "+" ? " + " : " * ");
59990
+ }
59991
+ function tryGetComplexParts(expr2, compile3) {
59992
+ if (isSymbol2(expr2, "ImaginaryUnit")) {
59993
+ return { re: null, im: "1.0" };
59994
+ }
59995
+ if (isNumber(expr2) && expr2.im !== 0) {
59996
+ const re = expr2.re;
59997
+ const im = expr2.im;
59998
+ return {
59999
+ re: re !== 0 ? formatFloat(re) : null,
60000
+ im: formatFloat(im)
60001
+ };
60002
+ }
60003
+ if (isFunction2(expr2, "Multiply")) {
60004
+ const ops = expr2.ops;
60005
+ const iIndex = ops.findIndex(
60006
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
60007
+ );
60008
+ if (iIndex >= 0) {
60009
+ const iFactor = ops[iIndex];
60010
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
60011
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
60012
+ if (remaining.length === 0) {
60013
+ return { re: null, im: formatFloat(iScale) };
60014
+ }
60015
+ const compiledFactors = remaining.map((r) => compile3(r));
60016
+ if (iScale !== 1)
60017
+ compiledFactors.unshift(formatFloat(iScale));
60018
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
60019
+ return { re: null, im: imCode };
60020
+ }
60021
+ }
60022
+ if (BaseCompiler.isComplexValued(expr2)) {
60023
+ return null;
60024
+ }
60025
+ return { re: compile3(expr2), im: null };
60026
+ }
60027
+
59925
60028
  // src/compute-engine/compilation/javascript-target.ts
59926
60029
  var JAVASCRIPT_OPERATORS = {
59927
60030
  Add: ["+", 11],
@@ -59945,12 +60048,21 @@ Error in definition of "${name}"`,
59945
60048
  Abs: (args, compile3) => {
59946
60049
  if (BaseCompiler.isComplexValued(args[0]))
59947
60050
  return `_SYS.cabs(${compile3(args[0])})`;
60051
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
59948
60052
  return `Math.abs(${compile3(args[0])})`;
59949
60053
  },
59950
60054
  Add: (args, compile3) => {
59951
60055
  if (args.length === 1) return compile3(args[0]);
59952
60056
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
59953
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" + ")})`;
60057
+ if (!anyComplex) {
60058
+ const constants = args.map(tryGetConstant);
60059
+ if (constants.every((c) => c !== void 0))
60060
+ return String(constants.reduce((a, b) => a + b, 0));
60061
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
60062
+ if (nonZero.length === 0) return "0";
60063
+ if (nonZero.length === 1) return compile3(nonZero[0]);
60064
+ return `(${nonZero.map((x) => compile3(x)).join(" + ")})`;
60065
+ }
59954
60066
  const parts = args.map((a) => {
59955
60067
  const code = compile3(a);
59956
60068
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -60007,7 +60119,10 @@ Error in definition of "${name}"`,
60007
60119
  return `Math.atan(${compile3(args[0])})`;
60008
60120
  },
60009
60121
  Artanh: "Math.atanh",
60010
- Ceil: "Math.ceil",
60122
+ Ceil: (args, compile3) => {
60123
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60124
+ return `Math.ceil(${compile3(args[0])})`;
60125
+ },
60011
60126
  Chop: "_SYS.chop",
60012
60127
  Cos: (args, compile3) => {
60013
60128
  if (BaseCompiler.isComplexValued(args[0]))
@@ -60050,7 +60165,10 @@ Error in definition of "${name}"`,
60050
60165
  return `_SYS.cexp(${compile3(args[0])})`;
60051
60166
  return `Math.exp(${compile3(args[0])})`;
60052
60167
  },
60053
- Floor: "Math.floor",
60168
+ Floor: (args, compile3) => {
60169
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60170
+ return `Math.floor(${compile3(args[0])})`;
60171
+ },
60054
60172
  Fract: ([x], compile3) => {
60055
60173
  if (x === null) throw new Error("Fract: no argument");
60056
60174
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile3(x));
@@ -60146,12 +60264,20 @@ Error in definition of "${name}"`,
60146
60264
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
60147
60265
  return `_SYS.cpow(${compile3(base)}, ${compile3(exp3)})`;
60148
60266
  }
60149
- const expVal = exp3.re;
60150
- if (expVal === 0.5) return `Math.sqrt(${compile3(base)})`;
60151
- if (expVal === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60152
- if (expVal === 1) return compile3(base);
60153
- if (expVal === -1) return `(1 / (${compile3(base)}))`;
60154
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60267
+ const bConst = tryGetConstant(base);
60268
+ const eConst = tryGetConstant(exp3);
60269
+ if (bConst !== void 0 && eConst !== void 0)
60270
+ return String(Math.pow(bConst, eConst));
60271
+ if (eConst === 0) return "1";
60272
+ if (eConst === 1) return compile3(base);
60273
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
60274
+ const code = compile3(base);
60275
+ return `(${code} * ${code})`;
60276
+ }
60277
+ if (eConst === -1) return `(1 / (${compile3(base)}))`;
60278
+ if (eConst === 0.5) return `Math.sqrt(${compile3(base)})`;
60279
+ if (eConst === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60280
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60155
60281
  return `Math.pow(${compile3(base)}, ${compile3(exp3)})`;
60156
60282
  },
60157
60283
  Range: (args, compile3) => {
@@ -60188,16 +60314,29 @@ Error in definition of "${name}"`,
60188
60314
  Root: ([arg, exp3], compile3) => {
60189
60315
  if (arg === null) throw new Error("Root: no argument");
60190
60316
  if (exp3 === null) return `Math.sqrt(${compile3(arg)})`;
60191
- if (exp3?.re === 2) return `Math.sqrt(${compile3(arg)})`;
60192
- if (exp3?.re === 3) return `Math.cbrt(${compile3(arg)})`;
60193
- if (!isNaN(exp3?.re)) return `Math.pow(${compile3(arg)}, ${1 / exp3.re})`;
60317
+ const aConst = tryGetConstant(arg);
60318
+ const nConst = tryGetConstant(exp3);
60319
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
60320
+ return String(Math.pow(aConst, 1 / nConst));
60321
+ if (nConst === 2) return `Math.sqrt(${compile3(arg)})`;
60322
+ if (nConst === 3) return `Math.cbrt(${compile3(arg)})`;
60323
+ if (nConst !== void 0) return `Math.pow(${compile3(arg)}, ${1 / nConst})`;
60194
60324
  return `Math.pow(${compile3(arg)}, 1 / (${compile3(exp3)}))`;
60195
60325
  },
60196
60326
  Random: "Math.random",
60197
- Round: "Math.round",
60327
+ Round: (args, compile3) => {
60328
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60329
+ return `Math.round(${compile3(args[0])})`;
60330
+ },
60198
60331
  Square: (args, compile3) => {
60199
60332
  const arg = args[0];
60200
60333
  if (arg === null) throw new Error("Square: no argument");
60334
+ const c = tryGetConstant(arg);
60335
+ if (c !== void 0) return String(c * c);
60336
+ if (isSymbol2(arg)) {
60337
+ const code = compile3(arg);
60338
+ return `(${code} * ${code})`;
60339
+ }
60201
60340
  return `Math.pow(${compile3(arg)}, 2)`;
60202
60341
  },
60203
60342
  Sec: (args, compile3) => {
@@ -60230,6 +60369,8 @@ Error in definition of "${name}"`,
60230
60369
  Sqrt: (args, compile3) => {
60231
60370
  if (BaseCompiler.isComplexValued(args[0]))
60232
60371
  return `_SYS.csqrt(${compile3(args[0])})`;
60372
+ const c = tryGetConstant(args[0]);
60373
+ if (c !== void 0) return String(Math.sqrt(c));
60233
60374
  return `Math.sqrt(${compile3(args[0])})`;
60234
60375
  },
60235
60376
  Tan: (args, compile3) => {
@@ -60246,9 +60387,14 @@ Error in definition of "${name}"`,
60246
60387
  if (a === null || b === null) throw new Error("Mod: missing argument");
60247
60388
  const ca = compile3(a);
60248
60389
  const cb = compile3(b);
60390
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
60391
+ return `(${ca} % ${cb})`;
60249
60392
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
60250
60393
  },
60251
- Truncate: "Math.trunc",
60394
+ Truncate: (args, compile3) => {
60395
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60396
+ return `Math.trunc(${compile3(args[0])})`;
60397
+ },
60252
60398
  Remainder: ([a, b], compile3) => {
60253
60399
  if (a === null || b === null)
60254
60400
  throw new Error("Remainder: missing argument");
@@ -60256,25 +60402,20 @@ Error in definition of "${name}"`,
60256
60402
  a
60257
60403
  )} / ${compile3(b)}))`;
60258
60404
  },
60259
- // Arithmetic operators handled as functions for completeness
60260
- Subtract: ([a, b], compile3) => {
60261
- if (a === null || b === null) throw new Error("Subtract: missing argument");
60262
- const ac = BaseCompiler.isComplexValued(a);
60263
- const bc = BaseCompiler.isComplexValued(b);
60264
- if (!ac && !bc) return `(${compile3(a)} - ${compile3(b)})`;
60265
- const ca = compile3(a);
60266
- const cb = compile3(b);
60267
- const reA = ac ? `(${ca}).re` : ca;
60268
- const imA = ac ? `(${ca}).im` : "0";
60269
- const reB = bc ? `(${cb}).re` : cb;
60270
- const imB = bc ? `(${cb}).im` : "0";
60271
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
60272
- },
60405
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
60406
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
60273
60407
  Divide: ([a, b], compile3) => {
60274
60408
  if (a === null || b === null) throw new Error("Divide: missing argument");
60275
60409
  const ac = BaseCompiler.isComplexValued(a);
60276
60410
  const bc = BaseCompiler.isComplexValued(b);
60277
- if (!ac && !bc) return `(${compile3(a)} / ${compile3(b)})`;
60411
+ if (!ac && !bc) {
60412
+ const ca = tryGetConstant(a);
60413
+ const cb = tryGetConstant(b);
60414
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
60415
+ return String(ca / cb);
60416
+ if (cb === 1) return compile3(a);
60417
+ return `(${compile3(a)} / ${compile3(b)})`;
60418
+ }
60278
60419
  if (ac && bc) {
60279
60420
  return `(() => { const _a = ${compile3(a)}, _b = ${compile3(
60280
60421
  b
@@ -60291,13 +60432,26 @@ Error in definition of "${name}"`,
60291
60432
  },
60292
60433
  Negate: ([x], compile3) => {
60293
60434
  if (x === null) throw new Error("Negate: no argument");
60294
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile3(x)})`;
60435
+ if (!BaseCompiler.isComplexValued(x)) {
60436
+ const c = tryGetConstant(x);
60437
+ if (c !== void 0) return String(-c);
60438
+ return `(-${compile3(x)})`;
60439
+ }
60295
60440
  return `_SYS.cneg(${compile3(x)})`;
60296
60441
  },
60297
60442
  Multiply: (args, compile3) => {
60298
60443
  if (args.length === 1) return compile3(args[0]);
60299
60444
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60300
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" * ")})`;
60445
+ if (!anyComplex) {
60446
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
60447
+ const constants = args.map(tryGetConstant);
60448
+ if (constants.every((c) => c !== void 0))
60449
+ return String(constants.reduce((a, b) => a * b, 1));
60450
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
60451
+ if (nonOne.length === 0) return "1";
60452
+ if (nonOne.length === 1) return compile3(nonOne[0]);
60453
+ return `(${nonOne.map((x) => compile3(x)).join(" * ")})`;
60454
+ }
60301
60455
  if (args.length === 2) {
60302
60456
  const ac = BaseCompiler.isComplexValued(args[0]);
60303
60457
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -60382,20 +60536,30 @@ Error in definition of "${name}"`,
60382
60536
  AiryAi: "_SYS.airyAi",
60383
60537
  AiryBi: "_SYS.airyBi",
60384
60538
  // Combinatorics
60539
+ Mandelbrot: ([c, maxIter], compile3) => {
60540
+ if (c === null || maxIter === null)
60541
+ throw new Error("Mandelbrot: missing arguments");
60542
+ return `_SYS.mandelbrot(${compile3(c)}, ${compile3(maxIter)})`;
60543
+ },
60544
+ Julia: ([z, c, maxIter], compile3) => {
60545
+ if (z === null || c === null || maxIter === null)
60546
+ throw new Error("Julia: missing arguments");
60547
+ return `_SYS.julia(${compile3(z)}, ${compile3(c)}, ${compile3(maxIter)})`;
60548
+ },
60385
60549
  Binomial: (args, compile3) => `_SYS.binomial(${compile3(args[0])}, ${compile3(args[1])})`,
60386
60550
  Fibonacci: "_SYS.fibonacci",
60387
60551
  // Complex-specific functions
60388
- Re: (args, compile3) => {
60552
+ Real: (args, compile3) => {
60389
60553
  if (BaseCompiler.isComplexValued(args[0]))
60390
60554
  return `(${compile3(args[0])}).re`;
60391
60555
  return compile3(args[0]);
60392
60556
  },
60393
- Im: (args, compile3) => {
60557
+ Imaginary: (args, compile3) => {
60394
60558
  if (BaseCompiler.isComplexValued(args[0]))
60395
60559
  return `(${compile3(args[0])}).im`;
60396
60560
  return "0";
60397
60561
  },
60398
- Arg: (args, compile3) => {
60562
+ Argument: (args, compile3) => {
60399
60563
  if (BaseCompiler.isComplexValued(args[0]))
60400
60564
  return `_SYS.carg(${compile3(args[0])})`;
60401
60565
  return `(${compile3(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -60731,6 +60895,41 @@ Error in definition of "${name}"`,
60731
60895
  sinc,
60732
60896
  fresnelS,
60733
60897
  fresnelC,
60898
+ mandelbrot: (c, maxIter) => {
60899
+ let zx = 0, zy = 0;
60900
+ const cx = typeof c === "number" ? c : c.re;
60901
+ const cy = typeof c === "number" ? 0 : c.im;
60902
+ const n = Math.round(maxIter);
60903
+ for (let i = 0; i < n; i++) {
60904
+ const newZx = zx * zx - zy * zy + cx;
60905
+ zy = 2 * zx * zy + cy;
60906
+ zx = newZx;
60907
+ const mag2 = zx * zx + zy * zy;
60908
+ if (mag2 > 4) {
60909
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60910
+ return Math.max(0, Math.min(1, smooth));
60911
+ }
60912
+ }
60913
+ return 1;
60914
+ },
60915
+ julia: (z, c, maxIter) => {
60916
+ let zx = typeof z === "number" ? z : z.re;
60917
+ let zy = typeof z === "number" ? 0 : z.im;
60918
+ const cx = typeof c === "number" ? c : c.re;
60919
+ const cy = typeof c === "number" ? 0 : c.im;
60920
+ const n = Math.round(maxIter);
60921
+ for (let i = 0; i < n; i++) {
60922
+ const newZx = zx * zx - zy * zy + cx;
60923
+ zy = 2 * zx * zy + cy;
60924
+ zx = newZx;
60925
+ const mag2 = zx * zx + zy * zy;
60926
+ if (mag2 > 4) {
60927
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60928
+ return Math.max(0, Math.min(1, smooth));
60929
+ }
60930
+ }
60931
+ return 1;
60932
+ },
60734
60933
  binomial: choose,
60735
60934
  fibonacci,
60736
60935
  // Complex helpers
@@ -61076,6 +61275,7 @@ Error in definition of "${name}"`,
61076
61275
  Add: ["+", 11],
61077
61276
  Negate: ["-", 14],
61078
61277
  Subtract: ["-", 11],
61278
+ // Subtract canonicalizes to Add+Negate; kept as fallback
61079
61279
  Multiply: ["*", 12],
61080
61280
  Divide: ["/", 13],
61081
61281
  Equal: ["==", 8],
@@ -61091,6 +61291,12 @@ Error in definition of "${name}"`,
61091
61291
  function gpuVec2(target) {
61092
61292
  return target?.language === "wgsl" ? "vec2f" : "vec2";
61093
61293
  }
61294
+ function compileIntArg(expr2, compile3, target) {
61295
+ const c = tryGetConstant(expr2);
61296
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
61297
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
61298
+ return `${intCast}(${compile3(expr2)})`;
61299
+ }
61094
61300
  var GPU_UNROLL_LIMIT = 100;
61095
61301
  function compileGPUSumProduct(kind, args, _compile2, target) {
61096
61302
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -61153,88 +61359,113 @@ Error in definition of "${name}"`,
61153
61359
  if (args.length === 0) return "0.0";
61154
61360
  if (args.length === 1) return compile3(args[0]);
61155
61361
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61156
- if (!anyComplex) return args.map((x) => compile3(x)).join(" + ");
61157
- const v2 = gpuVec2(target);
61158
- return args.map((a) => {
61159
- const code = compile3(a);
61160
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61161
- }).join(" + ");
61162
- },
61163
- Multiply: (args, compile3, _target) => {
61164
- if (args.length === 0) return "1.0";
61165
- if (args.length === 1) return compile3(args[0]);
61166
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61167
- if (!anyComplex) return args.map((x) => compile3(x)).join(" * ");
61168
- let result = compile3(args[0]);
61169
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
61170
- for (let i = 1; i < args.length; i++) {
61171
- const code = compile3(args[i]);
61172
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
61173
- if (!resultIsComplex && !argIsComplex) {
61174
- result = `(${result} * ${code})`;
61175
- } else if (resultIsComplex && !argIsComplex) {
61176
- result = `(${code} * ${result})`;
61177
- } else if (!resultIsComplex && argIsComplex) {
61178
- result = `(${result} * ${code})`;
61179
- resultIsComplex = true;
61180
- } else {
61181
- result = `_gpu_cmul(${result}, ${code})`;
61182
- }
61362
+ if (!anyComplex) {
61363
+ return foldTerms(
61364
+ args.map((x) => compile3(x)),
61365
+ "0.0",
61366
+ "+"
61367
+ );
61183
61368
  }
61184
- return result;
61185
- },
61186
- Subtract: (args, compile3, target) => {
61187
- if (args.length === 0) return "0.0";
61369
+ const parts = args.map((a) => tryGetComplexParts(a, compile3));
61370
+ if (parts.some((p) => p === null)) {
61371
+ const v2 = gpuVec2(target);
61372
+ return args.map((a) => {
61373
+ const code = compile3(a);
61374
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61375
+ }).join(" + ");
61376
+ }
61377
+ const reParts = [];
61378
+ const imParts = [];
61379
+ for (const p of parts) {
61380
+ if (p.re !== null) reParts.push(p.re);
61381
+ if (p.im !== null) imParts.push(p.im);
61382
+ }
61383
+ const reSum = foldTerms(reParts, "0.0", "+");
61384
+ const imSum = foldTerms(imParts, "0.0", "+");
61385
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
61386
+ },
61387
+ Multiply: (args, compile3, target) => {
61388
+ if (args.length === 0) return "1.0";
61188
61389
  if (args.length === 1) return compile3(args[0]);
61189
61390
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61190
61391
  if (!anyComplex) {
61191
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
61192
- let result2 = compile3(args[0]);
61193
- for (let i = 1; i < args.length; i++) {
61194
- result2 = `${result2} - ${compile3(args[i])}`;
61195
- }
61196
- return result2;
61197
- }
61198
- const v2 = gpuVec2(target);
61199
- const promote = (a) => {
61200
- const code = compile3(a);
61201
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61202
- };
61203
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
61204
- let result = promote(args[0]);
61205
- for (let i = 1; i < args.length; i++) {
61206
- result = `${result} - ${promote(args[i])}`;
61392
+ return foldTerms(
61393
+ args.map((x) => compile3(x)),
61394
+ "1.0",
61395
+ "*"
61396
+ );
61207
61397
  }
61398
+ const iIndex = args.findIndex(
61399
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
61400
+ );
61401
+ if (iIndex >= 0) {
61402
+ const iFactor = args[iIndex];
61403
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
61404
+ const realFactors = args.filter((_, i) => i !== iIndex);
61405
+ const v2 = gpuVec2(target);
61406
+ if (realFactors.length === 0)
61407
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
61408
+ const factors = realFactors.map((f) => compile3(f));
61409
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
61410
+ const imCode = foldTerms(factors, "1.0", "*");
61411
+ return `${v2}(0.0, ${imCode})`;
61412
+ }
61413
+ const realCodes = [];
61414
+ const complexCodes = [];
61415
+ for (const a of args) {
61416
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile3(a));
61417
+ else realCodes.push(compile3(a));
61418
+ }
61419
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
61420
+ let result = complexCodes[0];
61421
+ for (let i = 1; i < complexCodes.length; i++) {
61422
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
61423
+ }
61424
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
61208
61425
  return result;
61209
61426
  },
61427
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
61428
+ // The operator entry in GPU_OPERATORS handles any edge cases.
61210
61429
  Divide: (args, compile3, target) => {
61211
61430
  if (args.length === 0) return "1.0";
61212
61431
  if (args.length === 1) return compile3(args[0]);
61213
61432
  const ac = BaseCompiler.isComplexValued(args[0]);
61214
61433
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
61215
61434
  if (!ac && !bc) {
61216
- if (args.length === 2) return `${compile3(args[0])} / ${compile3(args[1])}`;
61435
+ if (args.length === 2) {
61436
+ const a = tryGetConstant(args[0]);
61437
+ const b = tryGetConstant(args[1]);
61438
+ if (a !== void 0 && b !== void 0 && b !== 0)
61439
+ return formatFloat(a / b);
61440
+ if (b === 1) return compile3(args[0]);
61441
+ return `${compile3(args[0])} / ${compile3(args[1])}`;
61442
+ }
61217
61443
  let result = compile3(args[0]);
61218
- for (let i = 1; i < args.length; i++) {
61444
+ for (let i = 1; i < args.length; i++)
61219
61445
  result = `${result} / ${compile3(args[i])}`;
61220
- }
61221
61446
  return result;
61222
61447
  }
61223
61448
  if (ac && bc) return `_gpu_cdiv(${compile3(args[0])}, ${compile3(args[1])})`;
61224
- if (ac && !bc) {
61225
- return `(${compile3(args[0])} / ${compile3(args[1])})`;
61226
- }
61449
+ if (ac && !bc) return `(${compile3(args[0])} / ${compile3(args[1])})`;
61227
61450
  const v2 = gpuVec2(target);
61228
61451
  return `_gpu_cdiv(${v2}(${compile3(args[0])}, 0.0), ${compile3(args[1])})`;
61229
61452
  },
61230
- Negate: ([x], compile3) => {
61453
+ Negate: ([x], compile3, target) => {
61231
61454
  if (x === null) throw new Error("Negate: no argument");
61455
+ const c = tryGetConstant(x);
61456
+ if (c !== void 0) return formatFloat(-c);
61457
+ if (isNumber(x) && x.im !== 0) {
61458
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
61459
+ }
61460
+ if (isSymbol2(x, "ImaginaryUnit"))
61461
+ return `${gpuVec2(target)}(0.0, -1.0)`;
61232
61462
  return `(-${compile3(x)})`;
61233
61463
  },
61234
61464
  // Standard math functions with complex dispatch
61235
61465
  Abs: (args, compile3) => {
61236
61466
  if (BaseCompiler.isComplexValued(args[0]))
61237
61467
  return `length(${compile3(args[0])})`;
61468
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
61238
61469
  return `abs(${compile3(args[0])})`;
61239
61470
  },
61240
61471
  Arccos: (args, compile3) => {
@@ -61252,7 +61483,10 @@ Error in definition of "${name}"`,
61252
61483
  return `_gpu_catan(${compile3(args[0])})`;
61253
61484
  return `atan(${compile3(args[0])})`;
61254
61485
  },
61255
- Ceil: "ceil",
61486
+ Ceil: (args, compile3) => {
61487
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61488
+ return `ceil(${compile3(args[0])})`;
61489
+ },
61256
61490
  Clamp: "clamp",
61257
61491
  Cos: (args, compile3) => {
61258
61492
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61266,7 +61500,10 @@ Error in definition of "${name}"`,
61266
61500
  return `exp(${compile3(args[0])})`;
61267
61501
  },
61268
61502
  Exp2: "exp2",
61269
- Floor: "floor",
61503
+ Floor: (args, compile3) => {
61504
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61505
+ return `floor(${compile3(args[0])})`;
61506
+ },
61270
61507
  Fract: "fract",
61271
61508
  Ln: (args, compile3) => {
61272
61509
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61288,10 +61525,25 @@ Error in definition of "${name}"`,
61288
61525
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile3(exp3) : `${v2}(${compile3(exp3)}, 0.0)`;
61289
61526
  return `_gpu_cpow(${bCode}, ${eCode})`;
61290
61527
  }
61528
+ const bConst = tryGetConstant(base);
61529
+ const eConst = tryGetConstant(exp3);
61530
+ if (bConst !== void 0 && eConst !== void 0)
61531
+ return formatFloat(Math.pow(bConst, eConst));
61532
+ if (eConst === 0) return "1.0";
61533
+ if (eConst === 1) return compile3(base);
61534
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
61535
+ const code = compile3(base);
61536
+ return `(${code} * ${code})`;
61537
+ }
61538
+ if (eConst === -1) return `(1.0 / ${compile3(base)})`;
61539
+ if (eConst === 0.5) return `sqrt(${compile3(base)})`;
61291
61540
  return `pow(${compile3(base)}, ${compile3(exp3)})`;
61292
61541
  },
61293
61542
  Radians: "radians",
61294
- Round: "round",
61543
+ Round: (args, compile3) => {
61544
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61545
+ return `round(${compile3(args[0])})`;
61546
+ },
61295
61547
  Sign: "sign",
61296
61548
  Sin: (args, compile3) => {
61297
61549
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61302,6 +61554,8 @@ Error in definition of "${name}"`,
61302
61554
  Sqrt: (args, compile3) => {
61303
61555
  if (BaseCompiler.isComplexValued(args[0]))
61304
61556
  return `_gpu_csqrt(${compile3(args[0])})`;
61557
+ const c = tryGetConstant(args[0]);
61558
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
61305
61559
  return `sqrt(${compile3(args[0])})`;
61306
61560
  },
61307
61561
  Step: "step",
@@ -61310,17 +61564,20 @@ Error in definition of "${name}"`,
61310
61564
  return `_gpu_ctan(${compile3(args[0])})`;
61311
61565
  return `tan(${compile3(args[0])})`;
61312
61566
  },
61313
- Truncate: "trunc",
61567
+ Truncate: (args, compile3) => {
61568
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61569
+ return `trunc(${compile3(args[0])})`;
61570
+ },
61314
61571
  // Complex-specific functions
61315
- Re: (args, compile3) => {
61572
+ Real: (args, compile3) => {
61316
61573
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).x`;
61317
61574
  return compile3(args[0]);
61318
61575
  },
61319
- Im: (args, compile3) => {
61576
+ Imaginary: (args, compile3) => {
61320
61577
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).y`;
61321
61578
  return "0.0";
61322
61579
  },
61323
- Arg: (args, compile3) => {
61580
+ Argument: (args, compile3) => {
61324
61581
  if (BaseCompiler.isComplexValued(args[0])) {
61325
61582
  const code = compile3(args[0]);
61326
61583
  return `atan(${code}.y, ${code}.x)`;
@@ -61535,13 +61792,20 @@ Error in definition of "${name}"`,
61535
61792
  },
61536
61793
  Square: ([x], compile3) => {
61537
61794
  if (x === null) throw new Error("Square: no argument");
61538
- const arg = compile3(x);
61539
- return `(${arg} * ${arg})`;
61795
+ if (isSymbol2(x) || isNumber(x)) {
61796
+ const arg = compile3(x);
61797
+ return `(${arg} * ${arg})`;
61798
+ }
61799
+ return `pow(${compile3(x)}, 2.0)`;
61540
61800
  },
61541
61801
  Root: ([x, n], compile3) => {
61542
61802
  if (x === null) throw new Error("Root: no argument");
61543
61803
  if (n === null || n === void 0) return `sqrt(${compile3(x)})`;
61544
- if (n?.re === 2) return `sqrt(${compile3(x)})`;
61804
+ const nConst = tryGetConstant(n);
61805
+ if (nConst === 2) return `sqrt(${compile3(x)})`;
61806
+ const xConst = tryGetConstant(x);
61807
+ if (xConst !== void 0 && nConst !== void 0)
61808
+ return formatFloat(Math.pow(xConst, 1 / nConst));
61545
61809
  return `pow(${compile3(x)}, 1.0 / ${compile3(n)})`;
61546
61810
  },
61547
61811
  // Color functions (pure-math, GPU-compilable)
@@ -61583,18 +61847,14 @@ Error in definition of "${name}"`,
61583
61847
  Mandelbrot: ([c, maxIter], compile3, target) => {
61584
61848
  if (c === null || maxIter === null)
61585
61849
  throw new Error("Mandelbrot: missing arguments");
61586
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61587
- return `_fractal_mandelbrot(${compile3(c)}, ${intCast}(${compile3(
61588
- maxIter
61589
- )}))`;
61850
+ const iterCode = compileIntArg(maxIter, compile3, target);
61851
+ return `_fractal_mandelbrot(${compile3(c)}, ${iterCode})`;
61590
61852
  },
61591
61853
  Julia: ([z, c, maxIter], compile3, target) => {
61592
61854
  if (z === null || c === null || maxIter === null)
61593
61855
  throw new Error("Julia: missing arguments");
61594
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61595
- return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${intCast}(${compile3(
61596
- maxIter
61597
- )}))`;
61856
+ const iterCode = compileIntArg(maxIter, compile3, target);
61857
+ return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${iterCode})`;
61598
61858
  },
61599
61859
  // Vector/Matrix operations
61600
61860
  Cross: "cross",
@@ -64226,6 +64486,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64226
64486
  Add: ["_IA.add", 20],
64227
64487
  Negate: ["_IA.negate", 20],
64228
64488
  Subtract: ["_IA.sub", 20],
64489
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64229
64490
  Multiply: ["_IA.mul", 20],
64230
64491
  Divide: ["_IA.div", 20],
64231
64492
  // Comparisons return BoolInterval
@@ -64250,17 +64511,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64250
64511
  }
64251
64512
  return result;
64252
64513
  },
64253
- Subtract: (args, compile3) => {
64254
- if (args.length === 0) return "_IA.point(0)";
64255
- if (args.length === 1) return `_IA.negate(${compile3(args[0])})`;
64256
- if (args.length === 2)
64257
- return `_IA.sub(${compile3(args[0])}, ${compile3(args[1])})`;
64258
- let result = compile3(args[0]);
64259
- for (let i = 1; i < args.length; i++) {
64260
- result = `_IA.sub(${result}, ${compile3(args[i])})`;
64261
- }
64262
- return result;
64263
- },
64514
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
64264
64515
  Multiply: (args, compile3) => {
64265
64516
  if (args.length === 0) return "_IA.point(1)";
64266
64517
  if (args.length === 1) return compile3(args[0]);
@@ -67234,7 +67485,9 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
67234
67485
  }
67235
67486
  return new ExactNumericValue(value, makeNumericValue);
67236
67487
  }
67237
- throw Error("Unexpected value");
67488
+ throw Error(
67489
+ `Unexpected value: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`
67490
+ );
67238
67491
  }
67239
67492
  /**
67240
67493
  * 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.
@@ -67705,7 +67958,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
67705
67958
  _setDefaultEngineFactory(() => new ComputeEngine());
67706
67959
 
67707
67960
  // src/core.ts
67708
- var version = "0.55.2";
67961
+ var version = "0.55.3";
67709
67962
  return __toCommonJS(core_exports);
67710
67963
  })();
67711
67964
  /*! Bundled license information: