@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
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.55.2 */
1
+ /** Compute Engine 0.55.3 */
2
2
 
3
3
  // src/compute-engine/numerics/richardson.ts
4
4
  function extrapolate(f, x0, options = {}) {
@@ -3936,12 +3936,6 @@ function makeLambda(expr) {
3936
3936
  function applicable(fn) {
3937
3937
  return makeLambda(fn) ?? ((xs) => fn.engine.function("Apply", [fn, ...xs]).evaluate());
3938
3938
  }
3939
- function applicableN1(fn) {
3940
- const lambda = makeLambda(fn);
3941
- const ce = fn.engine;
3942
- if (lambda) return (x) => lambda([ce.number(x)])?.re ?? NaN;
3943
- return (x) => ce.function("Apply", [fn, ce.number(x)]).evaluate().re;
3944
- }
3945
3939
 
3946
3940
  // src/compute-engine/collection-utils.ts
3947
3941
  function isFiniteIndexedCollection(col) {
@@ -14666,8 +14660,11 @@ var BaseCompiler = class _BaseCompiler {
14666
14660
  for (const local of locals) {
14667
14661
  for (const arg of args) {
14668
14662
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
14669
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
14663
+ const rhs = arg.ops[1];
14664
+ if (_BaseCompiler.isComplexValued(rhs)) {
14670
14665
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
14666
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
14667
+ typeHints[local] = isWGSL ? "i32" : "int";
14671
14668
  }
14672
14669
  break;
14673
14670
  }
@@ -14872,12 +14869,9 @@ var BaseCompiler = class _BaseCompiler {
14872
14869
  /**
14873
14870
  * Determine at compile time whether an expression produces a complex value.
14874
14871
  *
14875
- * Rules:
14876
- * - Numbers: complex if im !== 0
14877
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
14878
- * (undefined is treated as real -- assume-real policy)
14879
- * - Functions: Abs, Arg, Re, Im always return real.
14880
- * All others: complex if any operand is complex.
14872
+ * Uses the expression's declared type (from operator signatures) when
14873
+ * available. Falls back to operand inspection for functions whose
14874
+ * return type is unknown.
14881
14875
  */
14882
14876
  static isComplexValued(expr) {
14883
14877
  if (isNumber(expr)) return expr.im !== 0;
@@ -14888,13 +14882,24 @@ var BaseCompiler = class _BaseCompiler {
14888
14882
  return t.matches("complex") && !t.matches("real");
14889
14883
  }
14890
14884
  if (isFunction2(expr)) {
14891
- const op = expr.operator;
14892
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
14893
- return false;
14885
+ const t = expr.type;
14886
+ if (t.matches("complex") && !t.matches("real")) return true;
14887
+ if (t.matches("real")) return false;
14894
14888
  return expr.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
14895
14889
  }
14896
14890
  return false;
14897
14891
  }
14892
+ /** True if the expression is provably integer-typed. */
14893
+ static isIntegerValued(expr) {
14894
+ if (isNumber(expr)) return expr.im === 0 && Number.isInteger(expr.re);
14895
+ const t = expr.type;
14896
+ return t ? t.matches("integer") : false;
14897
+ }
14898
+ /** True if the expression is provably non-negative (sign ≥ 0). */
14899
+ static isNonNegative(expr) {
14900
+ if (isNumber(expr)) return expr.im === 0 && expr.re >= 0;
14901
+ return expr.isNonNegative === true;
14902
+ }
14898
14903
  /**
14899
14904
  * Generate a temporary variable name
14900
14905
  */
@@ -15066,20 +15071,113 @@ function compile(expr, options) {
15066
15071
  } catch (e) {
15067
15072
  if (options?.fallback ?? true) {
15068
15073
  console.warn(
15069
- `Compilation fallback for "${expr.operator}": ${e.message}`
15074
+ `Compilation fallback for "${expr.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
15070
15075
  );
15076
+ const ce = expr.engine;
15077
+ const fallbackRun = ((vars) => {
15078
+ ce.pushScope();
15079
+ try {
15080
+ if (vars && typeof vars === "object") {
15081
+ for (const [k, v] of Object.entries(vars))
15082
+ ce.assign(k, v);
15083
+ }
15084
+ return expr.evaluate().re;
15085
+ } finally {
15086
+ ce.popScope();
15087
+ }
15088
+ });
15071
15089
  return {
15072
15090
  target: options?.to ?? "javascript",
15073
15091
  success: false,
15074
15092
  code: "",
15075
15093
  calling: "expression",
15076
- run: applicableN1(expr)
15094
+ run: fallbackRun
15077
15095
  };
15078
15096
  }
15079
15097
  throw e;
15080
15098
  }
15081
15099
  }
15082
15100
 
15101
+ // src/compute-engine/compilation/constant-folding.ts
15102
+ function formatFloat(n) {
15103
+ const str = n.toString();
15104
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
15105
+ return `${str}.0`;
15106
+ }
15107
+ return str;
15108
+ }
15109
+ function tryGetConstant(expr) {
15110
+ if (!isNumber(expr)) return void 0;
15111
+ if (expr.im !== 0) return void 0;
15112
+ const re = expr.re;
15113
+ if (!isFinite(re)) return void 0;
15114
+ return re;
15115
+ }
15116
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
15117
+ function foldTerms(terms, identity, op) {
15118
+ const identityValue = op === "+" ? 0 : 1;
15119
+ let numericAcc = null;
15120
+ const symbolic = [];
15121
+ for (const term of terms) {
15122
+ if (NUMERIC_LITERAL_RE.test(term)) {
15123
+ const val = parseFloat(term);
15124
+ if (op === "*" && val === 0) return "0.0";
15125
+ if (numericAcc === null) {
15126
+ numericAcc = val;
15127
+ } else {
15128
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
15129
+ }
15130
+ } else {
15131
+ symbolic.push(term);
15132
+ }
15133
+ }
15134
+ if (numericAcc !== null && numericAcc !== identityValue) {
15135
+ symbolic.unshift(formatFloat(numericAcc));
15136
+ }
15137
+ if (symbolic.length === 0) {
15138
+ if (numericAcc !== null) return formatFloat(numericAcc);
15139
+ return identity;
15140
+ }
15141
+ if (symbolic.length === 1) return symbolic[0];
15142
+ return symbolic.join(op === "+" ? " + " : " * ");
15143
+ }
15144
+ function tryGetComplexParts(expr, compile2) {
15145
+ if (isSymbol2(expr, "ImaginaryUnit")) {
15146
+ return { re: null, im: "1.0" };
15147
+ }
15148
+ if (isNumber(expr) && expr.im !== 0) {
15149
+ const re = expr.re;
15150
+ const im = expr.im;
15151
+ return {
15152
+ re: re !== 0 ? formatFloat(re) : null,
15153
+ im: formatFloat(im)
15154
+ };
15155
+ }
15156
+ if (isFunction2(expr, "Multiply")) {
15157
+ const ops = expr.ops;
15158
+ const iIndex = ops.findIndex(
15159
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
15160
+ );
15161
+ if (iIndex >= 0) {
15162
+ const iFactor = ops[iIndex];
15163
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
15164
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
15165
+ if (remaining.length === 0) {
15166
+ return { re: null, im: formatFloat(iScale) };
15167
+ }
15168
+ const compiledFactors = remaining.map((r) => compile2(r));
15169
+ if (iScale !== 1)
15170
+ compiledFactors.unshift(formatFloat(iScale));
15171
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
15172
+ return { re: null, im: imCode };
15173
+ }
15174
+ }
15175
+ if (BaseCompiler.isComplexValued(expr)) {
15176
+ return null;
15177
+ }
15178
+ return { re: compile2(expr), im: null };
15179
+ }
15180
+
15083
15181
  // node_modules/@arnog/colors/dist/index.mjs
15084
15182
  function gammaCorrect(channel) {
15085
15183
  const abs2 = Math.abs(channel);
@@ -19418,12 +19516,21 @@ var JAVASCRIPT_FUNCTIONS = {
19418
19516
  Abs: (args, compile2) => {
19419
19517
  if (BaseCompiler.isComplexValued(args[0]))
19420
19518
  return `_SYS.cabs(${compile2(args[0])})`;
19519
+ if (BaseCompiler.isNonNegative(args[0])) return compile2(args[0]);
19421
19520
  return `Math.abs(${compile2(args[0])})`;
19422
19521
  },
19423
19522
  Add: (args, compile2) => {
19424
19523
  if (args.length === 1) return compile2(args[0]);
19425
19524
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
19426
- if (!anyComplex) return `(${args.map((x) => compile2(x)).join(" + ")})`;
19525
+ if (!anyComplex) {
19526
+ const constants = args.map(tryGetConstant);
19527
+ if (constants.every((c) => c !== void 0))
19528
+ return String(constants.reduce((a, b) => a + b, 0));
19529
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
19530
+ if (nonZero.length === 0) return "0";
19531
+ if (nonZero.length === 1) return compile2(nonZero[0]);
19532
+ return `(${nonZero.map((x) => compile2(x)).join(" + ")})`;
19533
+ }
19427
19534
  const parts = args.map((a) => {
19428
19535
  const code = compile2(a);
19429
19536
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -19480,7 +19587,10 @@ var JAVASCRIPT_FUNCTIONS = {
19480
19587
  return `Math.atan(${compile2(args[0])})`;
19481
19588
  },
19482
19589
  Artanh: "Math.atanh",
19483
- Ceil: "Math.ceil",
19590
+ Ceil: (args, compile2) => {
19591
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19592
+ return `Math.ceil(${compile2(args[0])})`;
19593
+ },
19484
19594
  Chop: "_SYS.chop",
19485
19595
  Cos: (args, compile2) => {
19486
19596
  if (BaseCompiler.isComplexValued(args[0]))
@@ -19523,7 +19633,10 @@ var JAVASCRIPT_FUNCTIONS = {
19523
19633
  return `_SYS.cexp(${compile2(args[0])})`;
19524
19634
  return `Math.exp(${compile2(args[0])})`;
19525
19635
  },
19526
- Floor: "Math.floor",
19636
+ Floor: (args, compile2) => {
19637
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19638
+ return `Math.floor(${compile2(args[0])})`;
19639
+ },
19527
19640
  Fract: ([x], compile2) => {
19528
19641
  if (x === null) throw new Error("Fract: no argument");
19529
19642
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile2(x));
@@ -19619,12 +19732,20 @@ var JAVASCRIPT_FUNCTIONS = {
19619
19732
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
19620
19733
  return `_SYS.cpow(${compile2(base)}, ${compile2(exp3)})`;
19621
19734
  }
19622
- const expVal = exp3.re;
19623
- if (expVal === 0.5) return `Math.sqrt(${compile2(base)})`;
19624
- if (expVal === 1 / 3) return `Math.cbrt(${compile2(base)})`;
19625
- if (expVal === 1) return compile2(base);
19626
- if (expVal === -1) return `(1 / (${compile2(base)}))`;
19627
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile2(base)}))`;
19735
+ const bConst = tryGetConstant(base);
19736
+ const eConst = tryGetConstant(exp3);
19737
+ if (bConst !== void 0 && eConst !== void 0)
19738
+ return String(Math.pow(bConst, eConst));
19739
+ if (eConst === 0) return "1";
19740
+ if (eConst === 1) return compile2(base);
19741
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
19742
+ const code = compile2(base);
19743
+ return `(${code} * ${code})`;
19744
+ }
19745
+ if (eConst === -1) return `(1 / (${compile2(base)}))`;
19746
+ if (eConst === 0.5) return `Math.sqrt(${compile2(base)})`;
19747
+ if (eConst === 1 / 3) return `Math.cbrt(${compile2(base)})`;
19748
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile2(base)}))`;
19628
19749
  return `Math.pow(${compile2(base)}, ${compile2(exp3)})`;
19629
19750
  },
19630
19751
  Range: (args, compile2) => {
@@ -19661,16 +19782,29 @@ var JAVASCRIPT_FUNCTIONS = {
19661
19782
  Root: ([arg, exp3], compile2) => {
19662
19783
  if (arg === null) throw new Error("Root: no argument");
19663
19784
  if (exp3 === null) return `Math.sqrt(${compile2(arg)})`;
19664
- if (exp3?.re === 2) return `Math.sqrt(${compile2(arg)})`;
19665
- if (exp3?.re === 3) return `Math.cbrt(${compile2(arg)})`;
19666
- if (!isNaN(exp3?.re)) return `Math.pow(${compile2(arg)}, ${1 / exp3.re})`;
19785
+ const aConst = tryGetConstant(arg);
19786
+ const nConst = tryGetConstant(exp3);
19787
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
19788
+ return String(Math.pow(aConst, 1 / nConst));
19789
+ if (nConst === 2) return `Math.sqrt(${compile2(arg)})`;
19790
+ if (nConst === 3) return `Math.cbrt(${compile2(arg)})`;
19791
+ if (nConst !== void 0) return `Math.pow(${compile2(arg)}, ${1 / nConst})`;
19667
19792
  return `Math.pow(${compile2(arg)}, 1 / (${compile2(exp3)}))`;
19668
19793
  },
19669
19794
  Random: "Math.random",
19670
- Round: "Math.round",
19795
+ Round: (args, compile2) => {
19796
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19797
+ return `Math.round(${compile2(args[0])})`;
19798
+ },
19671
19799
  Square: (args, compile2) => {
19672
19800
  const arg = args[0];
19673
19801
  if (arg === null) throw new Error("Square: no argument");
19802
+ const c = tryGetConstant(arg);
19803
+ if (c !== void 0) return String(c * c);
19804
+ if (isSymbol2(arg)) {
19805
+ const code = compile2(arg);
19806
+ return `(${code} * ${code})`;
19807
+ }
19674
19808
  return `Math.pow(${compile2(arg)}, 2)`;
19675
19809
  },
19676
19810
  Sec: (args, compile2) => {
@@ -19703,6 +19837,8 @@ var JAVASCRIPT_FUNCTIONS = {
19703
19837
  Sqrt: (args, compile2) => {
19704
19838
  if (BaseCompiler.isComplexValued(args[0]))
19705
19839
  return `_SYS.csqrt(${compile2(args[0])})`;
19840
+ const c = tryGetConstant(args[0]);
19841
+ if (c !== void 0) return String(Math.sqrt(c));
19706
19842
  return `Math.sqrt(${compile2(args[0])})`;
19707
19843
  },
19708
19844
  Tan: (args, compile2) => {
@@ -19719,9 +19855,14 @@ var JAVASCRIPT_FUNCTIONS = {
19719
19855
  if (a === null || b === null) throw new Error("Mod: missing argument");
19720
19856
  const ca = compile2(a);
19721
19857
  const cb = compile2(b);
19858
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
19859
+ return `(${ca} % ${cb})`;
19722
19860
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
19723
19861
  },
19724
- Truncate: "Math.trunc",
19862
+ Truncate: (args, compile2) => {
19863
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19864
+ return `Math.trunc(${compile2(args[0])})`;
19865
+ },
19725
19866
  Remainder: ([a, b], compile2) => {
19726
19867
  if (a === null || b === null)
19727
19868
  throw new Error("Remainder: missing argument");
@@ -19729,25 +19870,20 @@ var JAVASCRIPT_FUNCTIONS = {
19729
19870
  a
19730
19871
  )} / ${compile2(b)}))`;
19731
19872
  },
19732
- // Arithmetic operators handled as functions for completeness
19733
- Subtract: ([a, b], compile2) => {
19734
- if (a === null || b === null) throw new Error("Subtract: missing argument");
19735
- const ac = BaseCompiler.isComplexValued(a);
19736
- const bc = BaseCompiler.isComplexValued(b);
19737
- if (!ac && !bc) return `(${compile2(a)} - ${compile2(b)})`;
19738
- const ca = compile2(a);
19739
- const cb = compile2(b);
19740
- const reA = ac ? `(${ca}).re` : ca;
19741
- const imA = ac ? `(${ca}).im` : "0";
19742
- const reB = bc ? `(${cb}).re` : cb;
19743
- const imB = bc ? `(${cb}).im` : "0";
19744
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
19745
- },
19873
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
19874
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
19746
19875
  Divide: ([a, b], compile2) => {
19747
19876
  if (a === null || b === null) throw new Error("Divide: missing argument");
19748
19877
  const ac = BaseCompiler.isComplexValued(a);
19749
19878
  const bc = BaseCompiler.isComplexValued(b);
19750
- if (!ac && !bc) return `(${compile2(a)} / ${compile2(b)})`;
19879
+ if (!ac && !bc) {
19880
+ const ca = tryGetConstant(a);
19881
+ const cb = tryGetConstant(b);
19882
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
19883
+ return String(ca / cb);
19884
+ if (cb === 1) return compile2(a);
19885
+ return `(${compile2(a)} / ${compile2(b)})`;
19886
+ }
19751
19887
  if (ac && bc) {
19752
19888
  return `(() => { const _a = ${compile2(a)}, _b = ${compile2(
19753
19889
  b
@@ -19764,13 +19900,26 @@ var JAVASCRIPT_FUNCTIONS = {
19764
19900
  },
19765
19901
  Negate: ([x], compile2) => {
19766
19902
  if (x === null) throw new Error("Negate: no argument");
19767
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile2(x)})`;
19903
+ if (!BaseCompiler.isComplexValued(x)) {
19904
+ const c = tryGetConstant(x);
19905
+ if (c !== void 0) return String(-c);
19906
+ return `(-${compile2(x)})`;
19907
+ }
19768
19908
  return `_SYS.cneg(${compile2(x)})`;
19769
19909
  },
19770
19910
  Multiply: (args, compile2) => {
19771
19911
  if (args.length === 1) return compile2(args[0]);
19772
19912
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
19773
- if (!anyComplex) return `(${args.map((x) => compile2(x)).join(" * ")})`;
19913
+ if (!anyComplex) {
19914
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
19915
+ const constants = args.map(tryGetConstant);
19916
+ if (constants.every((c) => c !== void 0))
19917
+ return String(constants.reduce((a, b) => a * b, 1));
19918
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
19919
+ if (nonOne.length === 0) return "1";
19920
+ if (nonOne.length === 1) return compile2(nonOne[0]);
19921
+ return `(${nonOne.map((x) => compile2(x)).join(" * ")})`;
19922
+ }
19774
19923
  if (args.length === 2) {
19775
19924
  const ac = BaseCompiler.isComplexValued(args[0]);
19776
19925
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -19855,20 +20004,30 @@ var JAVASCRIPT_FUNCTIONS = {
19855
20004
  AiryAi: "_SYS.airyAi",
19856
20005
  AiryBi: "_SYS.airyBi",
19857
20006
  // Combinatorics
20007
+ Mandelbrot: ([c, maxIter], compile2) => {
20008
+ if (c === null || maxIter === null)
20009
+ throw new Error("Mandelbrot: missing arguments");
20010
+ return `_SYS.mandelbrot(${compile2(c)}, ${compile2(maxIter)})`;
20011
+ },
20012
+ Julia: ([z, c, maxIter], compile2) => {
20013
+ if (z === null || c === null || maxIter === null)
20014
+ throw new Error("Julia: missing arguments");
20015
+ return `_SYS.julia(${compile2(z)}, ${compile2(c)}, ${compile2(maxIter)})`;
20016
+ },
19858
20017
  Binomial: (args, compile2) => `_SYS.binomial(${compile2(args[0])}, ${compile2(args[1])})`,
19859
20018
  Fibonacci: "_SYS.fibonacci",
19860
20019
  // Complex-specific functions
19861
- Re: (args, compile2) => {
20020
+ Real: (args, compile2) => {
19862
20021
  if (BaseCompiler.isComplexValued(args[0]))
19863
20022
  return `(${compile2(args[0])}).re`;
19864
20023
  return compile2(args[0]);
19865
20024
  },
19866
- Im: (args, compile2) => {
20025
+ Imaginary: (args, compile2) => {
19867
20026
  if (BaseCompiler.isComplexValued(args[0]))
19868
20027
  return `(${compile2(args[0])}).im`;
19869
20028
  return "0";
19870
20029
  },
19871
- Arg: (args, compile2) => {
20030
+ Argument: (args, compile2) => {
19872
20031
  if (BaseCompiler.isComplexValued(args[0]))
19873
20032
  return `_SYS.carg(${compile2(args[0])})`;
19874
20033
  return `(${compile2(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -20204,6 +20363,41 @@ var SYS_HELPERS = {
20204
20363
  sinc,
20205
20364
  fresnelS,
20206
20365
  fresnelC,
20366
+ mandelbrot: (c, maxIter) => {
20367
+ let zx = 0, zy = 0;
20368
+ const cx = typeof c === "number" ? c : c.re;
20369
+ const cy = typeof c === "number" ? 0 : c.im;
20370
+ const n = Math.round(maxIter);
20371
+ for (let i = 0; i < n; i++) {
20372
+ const newZx = zx * zx - zy * zy + cx;
20373
+ zy = 2 * zx * zy + cy;
20374
+ zx = newZx;
20375
+ const mag2 = zx * zx + zy * zy;
20376
+ if (mag2 > 4) {
20377
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
20378
+ return Math.max(0, Math.min(1, smooth));
20379
+ }
20380
+ }
20381
+ return 1;
20382
+ },
20383
+ julia: (z, c, maxIter) => {
20384
+ let zx = typeof z === "number" ? z : z.re;
20385
+ let zy = typeof z === "number" ? 0 : z.im;
20386
+ const cx = typeof c === "number" ? c : c.re;
20387
+ const cy = typeof c === "number" ? 0 : c.im;
20388
+ const n = Math.round(maxIter);
20389
+ for (let i = 0; i < n; i++) {
20390
+ const newZx = zx * zx - zy * zy + cx;
20391
+ zy = 2 * zx * zy + cy;
20392
+ zx = newZx;
20393
+ const mag2 = zx * zx + zy * zy;
20394
+ if (mag2 > 4) {
20395
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
20396
+ return Math.max(0, Math.min(1, smooth));
20397
+ }
20398
+ }
20399
+ return 1;
20400
+ },
20207
20401
  binomial: choose,
20208
20402
  fibonacci,
20209
20403
  // Complex helpers
@@ -20549,6 +20743,7 @@ var GPU_OPERATORS = {
20549
20743
  Add: ["+", 11],
20550
20744
  Negate: ["-", 14],
20551
20745
  Subtract: ["-", 11],
20746
+ // Subtract canonicalizes to Add+Negate; kept as fallback
20552
20747
  Multiply: ["*", 12],
20553
20748
  Divide: ["/", 13],
20554
20749
  Equal: ["==", 8],
@@ -20564,6 +20759,12 @@ var GPU_OPERATORS = {
20564
20759
  function gpuVec2(target) {
20565
20760
  return target?.language === "wgsl" ? "vec2f" : "vec2";
20566
20761
  }
20762
+ function compileIntArg(expr, compile2, target) {
20763
+ const c = tryGetConstant(expr);
20764
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
20765
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
20766
+ return `${intCast}(${compile2(expr)})`;
20767
+ }
20567
20768
  var GPU_UNROLL_LIMIT = 100;
20568
20769
  function compileGPUSumProduct(kind, args, _compile, target) {
20569
20770
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -20626,88 +20827,113 @@ var GPU_FUNCTIONS = {
20626
20827
  if (args.length === 0) return "0.0";
20627
20828
  if (args.length === 1) return compile2(args[0]);
20628
20829
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20629
- if (!anyComplex) return args.map((x) => compile2(x)).join(" + ");
20630
- const v2 = gpuVec2(target);
20631
- return args.map((a) => {
20632
- const code = compile2(a);
20633
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20634
- }).join(" + ");
20635
- },
20636
- Multiply: (args, compile2, _target) => {
20637
- if (args.length === 0) return "1.0";
20638
- if (args.length === 1) return compile2(args[0]);
20639
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20640
- if (!anyComplex) return args.map((x) => compile2(x)).join(" * ");
20641
- let result = compile2(args[0]);
20642
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
20643
- for (let i = 1; i < args.length; i++) {
20644
- const code = compile2(args[i]);
20645
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
20646
- if (!resultIsComplex && !argIsComplex) {
20647
- result = `(${result} * ${code})`;
20648
- } else if (resultIsComplex && !argIsComplex) {
20649
- result = `(${code} * ${result})`;
20650
- } else if (!resultIsComplex && argIsComplex) {
20651
- result = `(${result} * ${code})`;
20652
- resultIsComplex = true;
20653
- } else {
20654
- result = `_gpu_cmul(${result}, ${code})`;
20655
- }
20830
+ if (!anyComplex) {
20831
+ return foldTerms(
20832
+ args.map((x) => compile2(x)),
20833
+ "0.0",
20834
+ "+"
20835
+ );
20656
20836
  }
20657
- return result;
20658
- },
20659
- Subtract: (args, compile2, target) => {
20660
- if (args.length === 0) return "0.0";
20837
+ const parts = args.map((a) => tryGetComplexParts(a, compile2));
20838
+ if (parts.some((p) => p === null)) {
20839
+ const v2 = gpuVec2(target);
20840
+ return args.map((a) => {
20841
+ const code = compile2(a);
20842
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20843
+ }).join(" + ");
20844
+ }
20845
+ const reParts = [];
20846
+ const imParts = [];
20847
+ for (const p of parts) {
20848
+ if (p.re !== null) reParts.push(p.re);
20849
+ if (p.im !== null) imParts.push(p.im);
20850
+ }
20851
+ const reSum = foldTerms(reParts, "0.0", "+");
20852
+ const imSum = foldTerms(imParts, "0.0", "+");
20853
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
20854
+ },
20855
+ Multiply: (args, compile2, target) => {
20856
+ if (args.length === 0) return "1.0";
20661
20857
  if (args.length === 1) return compile2(args[0]);
20662
20858
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20663
20859
  if (!anyComplex) {
20664
- if (args.length === 2) return `${compile2(args[0])} - ${compile2(args[1])}`;
20665
- let result2 = compile2(args[0]);
20666
- for (let i = 1; i < args.length; i++) {
20667
- result2 = `${result2} - ${compile2(args[i])}`;
20668
- }
20669
- return result2;
20670
- }
20671
- const v2 = gpuVec2(target);
20672
- const promote = (a) => {
20673
- const code = compile2(a);
20674
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20675
- };
20676
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
20677
- let result = promote(args[0]);
20678
- for (let i = 1; i < args.length; i++) {
20679
- result = `${result} - ${promote(args[i])}`;
20860
+ return foldTerms(
20861
+ args.map((x) => compile2(x)),
20862
+ "1.0",
20863
+ "*"
20864
+ );
20680
20865
  }
20866
+ const iIndex = args.findIndex(
20867
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
20868
+ );
20869
+ if (iIndex >= 0) {
20870
+ const iFactor = args[iIndex];
20871
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
20872
+ const realFactors = args.filter((_, i) => i !== iIndex);
20873
+ const v2 = gpuVec2(target);
20874
+ if (realFactors.length === 0)
20875
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
20876
+ const factors = realFactors.map((f) => compile2(f));
20877
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
20878
+ const imCode = foldTerms(factors, "1.0", "*");
20879
+ return `${v2}(0.0, ${imCode})`;
20880
+ }
20881
+ const realCodes = [];
20882
+ const complexCodes = [];
20883
+ for (const a of args) {
20884
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile2(a));
20885
+ else realCodes.push(compile2(a));
20886
+ }
20887
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
20888
+ let result = complexCodes[0];
20889
+ for (let i = 1; i < complexCodes.length; i++) {
20890
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
20891
+ }
20892
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
20681
20893
  return result;
20682
20894
  },
20895
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
20896
+ // The operator entry in GPU_OPERATORS handles any edge cases.
20683
20897
  Divide: (args, compile2, target) => {
20684
20898
  if (args.length === 0) return "1.0";
20685
20899
  if (args.length === 1) return compile2(args[0]);
20686
20900
  const ac = BaseCompiler.isComplexValued(args[0]);
20687
20901
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
20688
20902
  if (!ac && !bc) {
20689
- if (args.length === 2) return `${compile2(args[0])} / ${compile2(args[1])}`;
20903
+ if (args.length === 2) {
20904
+ const a = tryGetConstant(args[0]);
20905
+ const b = tryGetConstant(args[1]);
20906
+ if (a !== void 0 && b !== void 0 && b !== 0)
20907
+ return formatFloat(a / b);
20908
+ if (b === 1) return compile2(args[0]);
20909
+ return `${compile2(args[0])} / ${compile2(args[1])}`;
20910
+ }
20690
20911
  let result = compile2(args[0]);
20691
- for (let i = 1; i < args.length; i++) {
20912
+ for (let i = 1; i < args.length; i++)
20692
20913
  result = `${result} / ${compile2(args[i])}`;
20693
- }
20694
20914
  return result;
20695
20915
  }
20696
20916
  if (ac && bc) return `_gpu_cdiv(${compile2(args[0])}, ${compile2(args[1])})`;
20697
- if (ac && !bc) {
20698
- return `(${compile2(args[0])} / ${compile2(args[1])})`;
20699
- }
20917
+ if (ac && !bc) return `(${compile2(args[0])} / ${compile2(args[1])})`;
20700
20918
  const v2 = gpuVec2(target);
20701
20919
  return `_gpu_cdiv(${v2}(${compile2(args[0])}, 0.0), ${compile2(args[1])})`;
20702
20920
  },
20703
- Negate: ([x], compile2) => {
20921
+ Negate: ([x], compile2, target) => {
20704
20922
  if (x === null) throw new Error("Negate: no argument");
20923
+ const c = tryGetConstant(x);
20924
+ if (c !== void 0) return formatFloat(-c);
20925
+ if (isNumber(x) && x.im !== 0) {
20926
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
20927
+ }
20928
+ if (isSymbol2(x, "ImaginaryUnit"))
20929
+ return `${gpuVec2(target)}(0.0, -1.0)`;
20705
20930
  return `(-${compile2(x)})`;
20706
20931
  },
20707
20932
  // Standard math functions with complex dispatch
20708
20933
  Abs: (args, compile2) => {
20709
20934
  if (BaseCompiler.isComplexValued(args[0]))
20710
20935
  return `length(${compile2(args[0])})`;
20936
+ if (BaseCompiler.isNonNegative(args[0])) return compile2(args[0]);
20711
20937
  return `abs(${compile2(args[0])})`;
20712
20938
  },
20713
20939
  Arccos: (args, compile2) => {
@@ -20725,7 +20951,10 @@ var GPU_FUNCTIONS = {
20725
20951
  return `_gpu_catan(${compile2(args[0])})`;
20726
20952
  return `atan(${compile2(args[0])})`;
20727
20953
  },
20728
- Ceil: "ceil",
20954
+ Ceil: (args, compile2) => {
20955
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
20956
+ return `ceil(${compile2(args[0])})`;
20957
+ },
20729
20958
  Clamp: "clamp",
20730
20959
  Cos: (args, compile2) => {
20731
20960
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20739,7 +20968,10 @@ var GPU_FUNCTIONS = {
20739
20968
  return `exp(${compile2(args[0])})`;
20740
20969
  },
20741
20970
  Exp2: "exp2",
20742
- Floor: "floor",
20971
+ Floor: (args, compile2) => {
20972
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
20973
+ return `floor(${compile2(args[0])})`;
20974
+ },
20743
20975
  Fract: "fract",
20744
20976
  Ln: (args, compile2) => {
20745
20977
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20761,10 +20993,25 @@ var GPU_FUNCTIONS = {
20761
20993
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile2(exp3) : `${v2}(${compile2(exp3)}, 0.0)`;
20762
20994
  return `_gpu_cpow(${bCode}, ${eCode})`;
20763
20995
  }
20996
+ const bConst = tryGetConstant(base);
20997
+ const eConst = tryGetConstant(exp3);
20998
+ if (bConst !== void 0 && eConst !== void 0)
20999
+ return formatFloat(Math.pow(bConst, eConst));
21000
+ if (eConst === 0) return "1.0";
21001
+ if (eConst === 1) return compile2(base);
21002
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
21003
+ const code = compile2(base);
21004
+ return `(${code} * ${code})`;
21005
+ }
21006
+ if (eConst === -1) return `(1.0 / ${compile2(base)})`;
21007
+ if (eConst === 0.5) return `sqrt(${compile2(base)})`;
20764
21008
  return `pow(${compile2(base)}, ${compile2(exp3)})`;
20765
21009
  },
20766
21010
  Radians: "radians",
20767
- Round: "round",
21011
+ Round: (args, compile2) => {
21012
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21013
+ return `round(${compile2(args[0])})`;
21014
+ },
20768
21015
  Sign: "sign",
20769
21016
  Sin: (args, compile2) => {
20770
21017
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20775,6 +21022,8 @@ var GPU_FUNCTIONS = {
20775
21022
  Sqrt: (args, compile2) => {
20776
21023
  if (BaseCompiler.isComplexValued(args[0]))
20777
21024
  return `_gpu_csqrt(${compile2(args[0])})`;
21025
+ const c = tryGetConstant(args[0]);
21026
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
20778
21027
  return `sqrt(${compile2(args[0])})`;
20779
21028
  },
20780
21029
  Step: "step",
@@ -20783,17 +21032,20 @@ var GPU_FUNCTIONS = {
20783
21032
  return `_gpu_ctan(${compile2(args[0])})`;
20784
21033
  return `tan(${compile2(args[0])})`;
20785
21034
  },
20786
- Truncate: "trunc",
21035
+ Truncate: (args, compile2) => {
21036
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21037
+ return `trunc(${compile2(args[0])})`;
21038
+ },
20787
21039
  // Complex-specific functions
20788
- Re: (args, compile2) => {
21040
+ Real: (args, compile2) => {
20789
21041
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile2(args[0])}).x`;
20790
21042
  return compile2(args[0]);
20791
21043
  },
20792
- Im: (args, compile2) => {
21044
+ Imaginary: (args, compile2) => {
20793
21045
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile2(args[0])}).y`;
20794
21046
  return "0.0";
20795
21047
  },
20796
- Arg: (args, compile2) => {
21048
+ Argument: (args, compile2) => {
20797
21049
  if (BaseCompiler.isComplexValued(args[0])) {
20798
21050
  const code = compile2(args[0]);
20799
21051
  return `atan(${code}.y, ${code}.x)`;
@@ -21008,13 +21260,20 @@ var GPU_FUNCTIONS = {
21008
21260
  },
21009
21261
  Square: ([x], compile2) => {
21010
21262
  if (x === null) throw new Error("Square: no argument");
21011
- const arg = compile2(x);
21012
- return `(${arg} * ${arg})`;
21263
+ if (isSymbol2(x) || isNumber(x)) {
21264
+ const arg = compile2(x);
21265
+ return `(${arg} * ${arg})`;
21266
+ }
21267
+ return `pow(${compile2(x)}, 2.0)`;
21013
21268
  },
21014
21269
  Root: ([x, n], compile2) => {
21015
21270
  if (x === null) throw new Error("Root: no argument");
21016
21271
  if (n === null || n === void 0) return `sqrt(${compile2(x)})`;
21017
- if (n?.re === 2) return `sqrt(${compile2(x)})`;
21272
+ const nConst = tryGetConstant(n);
21273
+ if (nConst === 2) return `sqrt(${compile2(x)})`;
21274
+ const xConst = tryGetConstant(x);
21275
+ if (xConst !== void 0 && nConst !== void 0)
21276
+ return formatFloat(Math.pow(xConst, 1 / nConst));
21018
21277
  return `pow(${compile2(x)}, 1.0 / ${compile2(n)})`;
21019
21278
  },
21020
21279
  // Color functions (pure-math, GPU-compilable)
@@ -21056,18 +21315,14 @@ var GPU_FUNCTIONS = {
21056
21315
  Mandelbrot: ([c, maxIter], compile2, target) => {
21057
21316
  if (c === null || maxIter === null)
21058
21317
  throw new Error("Mandelbrot: missing arguments");
21059
- const intCast = target?.language === "wgsl" ? "i32" : "int";
21060
- return `_fractal_mandelbrot(${compile2(c)}, ${intCast}(${compile2(
21061
- maxIter
21062
- )}))`;
21318
+ const iterCode = compileIntArg(maxIter, compile2, target);
21319
+ return `_fractal_mandelbrot(${compile2(c)}, ${iterCode})`;
21063
21320
  },
21064
21321
  Julia: ([z, c, maxIter], compile2, target) => {
21065
21322
  if (z === null || c === null || maxIter === null)
21066
21323
  throw new Error("Julia: missing arguments");
21067
- const intCast = target?.language === "wgsl" ? "i32" : "int";
21068
- return `_fractal_julia(${compile2(z)}, ${compile2(c)}, ${intCast}(${compile2(
21069
- maxIter
21070
- )}))`;
21324
+ const iterCode = compileIntArg(maxIter, compile2, target);
21325
+ return `_fractal_julia(${compile2(z)}, ${compile2(c)}, ${iterCode})`;
21071
21326
  },
21072
21327
  // Vector/Matrix operations
21073
21328
  Cross: "cross",
@@ -22422,6 +22677,7 @@ var PYTHON_OPERATORS = {
22422
22677
  Negate: ["-", 14],
22423
22678
  // Unary operator
22424
22679
  Subtract: ["-", 11],
22680
+ // Subtract canonicalizes to Add+Negate; kept as fallback
22425
22681
  Multiply: ["*", 12],
22426
22682
  Divide: ["/", 13],
22427
22683
  Power: ["**", 15],
@@ -22449,16 +22705,7 @@ var PYTHON_FUNCTIONS = {
22449
22705
  if (args.length === 1) return compile2(args[0]);
22450
22706
  return args.map((x) => compile2(x)).join(" * ");
22451
22707
  },
22452
- Subtract: (args, compile2) => {
22453
- if (args.length === 0) return "0";
22454
- if (args.length === 1) return compile2(args[0]);
22455
- if (args.length === 2) return `${compile2(args[0])} - ${compile2(args[1])}`;
22456
- let result = compile2(args[0]);
22457
- for (let i = 1; i < args.length; i++) {
22458
- result = `${result} - ${compile2(args[i])}`;
22459
- }
22460
- return result;
22461
- },
22708
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
22462
22709
  Divide: (args, compile2) => {
22463
22710
  if (args.length === 0) return "1";
22464
22711
  if (args.length === 1) return compile2(args[0]);
@@ -24123,6 +24370,7 @@ var INTERVAL_JAVASCRIPT_OPERATORS = {
24123
24370
  Add: ["_IA.add", 20],
24124
24371
  Negate: ["_IA.negate", 20],
24125
24372
  Subtract: ["_IA.sub", 20],
24373
+ // Subtract canonicalizes to Add+Negate; kept as fallback
24126
24374
  Multiply: ["_IA.mul", 20],
24127
24375
  Divide: ["_IA.div", 20],
24128
24376
  // Comparisons return BoolInterval
@@ -24147,17 +24395,7 @@ var INTERVAL_JAVASCRIPT_FUNCTIONS = {
24147
24395
  }
24148
24396
  return result;
24149
24397
  },
24150
- Subtract: (args, compile2) => {
24151
- if (args.length === 0) return "_IA.point(0)";
24152
- if (args.length === 1) return `_IA.negate(${compile2(args[0])})`;
24153
- if (args.length === 2)
24154
- return `_IA.sub(${compile2(args[0])}, ${compile2(args[1])})`;
24155
- let result = compile2(args[0]);
24156
- for (let i = 1; i < args.length; i++) {
24157
- result = `_IA.sub(${result}, ${compile2(args[i])})`;
24158
- }
24159
- return result;
24160
- },
24398
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
24161
24399
  Multiply: (args, compile2) => {
24162
24400
  if (args.length === 0) return "_IA.point(1)";
24163
24401
  if (args.length === 1) return compile2(args[0]);
@@ -24540,7 +24778,7 @@ function compileToIntervalTarget(expr, target) {
24540
24778
  }
24541
24779
 
24542
24780
  // src/compile.ts
24543
- var version = "0.55.2";
24781
+ var version = "0.55.3";
24544
24782
  export {
24545
24783
  BaseCompiler,
24546
24784
  GLSLTarget,