@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
- /** Compile 0.55.2 */
1
+ /** Compile 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.Compile = {}));})(this, (function (exports) { 'use strict';
3
3
  var Compile = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -3969,12 +3969,6 @@ var Compile = (() => {
3969
3969
  function applicable(fn) {
3970
3970
  return makeLambda(fn) ?? ((xs) => fn.engine.function("Apply", [fn, ...xs]).evaluate());
3971
3971
  }
3972
- function applicableN1(fn) {
3973
- const lambda = makeLambda(fn);
3974
- const ce = fn.engine;
3975
- if (lambda) return (x) => lambda([ce.number(x)])?.re ?? NaN;
3976
- return (x) => ce.function("Apply", [fn, ce.number(x)]).evaluate().re;
3977
- }
3978
3972
 
3979
3973
  // src/compute-engine/collection-utils.ts
3980
3974
  function isFiniteIndexedCollection(col) {
@@ -14699,8 +14693,11 @@ ${lines.join("\n")}`;
14699
14693
  for (const local of locals) {
14700
14694
  for (const arg of args) {
14701
14695
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
14702
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
14696
+ const rhs = arg.ops[1];
14697
+ if (_BaseCompiler.isComplexValued(rhs)) {
14703
14698
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
14699
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
14700
+ typeHints[local] = isWGSL ? "i32" : "int";
14704
14701
  }
14705
14702
  break;
14706
14703
  }
@@ -14905,12 +14902,9 @@ ${lines.join("\n")}`;
14905
14902
  /**
14906
14903
  * Determine at compile time whether an expression produces a complex value.
14907
14904
  *
14908
- * Rules:
14909
- * - Numbers: complex if im !== 0
14910
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
14911
- * (undefined is treated as real -- assume-real policy)
14912
- * - Functions: Abs, Arg, Re, Im always return real.
14913
- * All others: complex if any operand is complex.
14905
+ * Uses the expression's declared type (from operator signatures) when
14906
+ * available. Falls back to operand inspection for functions whose
14907
+ * return type is unknown.
14914
14908
  */
14915
14909
  static isComplexValued(expr) {
14916
14910
  if (isNumber(expr)) return expr.im !== 0;
@@ -14921,13 +14915,24 @@ ${lines.join("\n")}`;
14921
14915
  return t.matches("complex") && !t.matches("real");
14922
14916
  }
14923
14917
  if (isFunction2(expr)) {
14924
- const op = expr.operator;
14925
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
14926
- return false;
14918
+ const t = expr.type;
14919
+ if (t.matches("complex") && !t.matches("real")) return true;
14920
+ if (t.matches("real")) return false;
14927
14921
  return expr.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
14928
14922
  }
14929
14923
  return false;
14930
14924
  }
14925
+ /** True if the expression is provably integer-typed. */
14926
+ static isIntegerValued(expr) {
14927
+ if (isNumber(expr)) return expr.im === 0 && Number.isInteger(expr.re);
14928
+ const t = expr.type;
14929
+ return t ? t.matches("integer") : false;
14930
+ }
14931
+ /** True if the expression is provably non-negative (sign ≥ 0). */
14932
+ static isNonNegative(expr) {
14933
+ if (isNumber(expr)) return expr.im === 0 && expr.re >= 0;
14934
+ return expr.isNonNegative === true;
14935
+ }
14931
14936
  /**
14932
14937
  * Generate a temporary variable name
14933
14938
  */
@@ -15099,20 +15104,113 @@ ${lines.join("\n")}`;
15099
15104
  } catch (e) {
15100
15105
  if (options?.fallback ?? true) {
15101
15106
  console.warn(
15102
- `Compilation fallback for "${expr.operator}": ${e.message}`
15107
+ `Compilation fallback for "${expr.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
15103
15108
  );
15109
+ const ce = expr.engine;
15110
+ const fallbackRun = ((vars) => {
15111
+ ce.pushScope();
15112
+ try {
15113
+ if (vars && typeof vars === "object") {
15114
+ for (const [k, v] of Object.entries(vars))
15115
+ ce.assign(k, v);
15116
+ }
15117
+ return expr.evaluate().re;
15118
+ } finally {
15119
+ ce.popScope();
15120
+ }
15121
+ });
15104
15122
  return {
15105
15123
  target: options?.to ?? "javascript",
15106
15124
  success: false,
15107
15125
  code: "",
15108
15126
  calling: "expression",
15109
- run: applicableN1(expr)
15127
+ run: fallbackRun
15110
15128
  };
15111
15129
  }
15112
15130
  throw e;
15113
15131
  }
15114
15132
  }
15115
15133
 
15134
+ // src/compute-engine/compilation/constant-folding.ts
15135
+ function formatFloat(n) {
15136
+ const str = n.toString();
15137
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
15138
+ return `${str}.0`;
15139
+ }
15140
+ return str;
15141
+ }
15142
+ function tryGetConstant(expr) {
15143
+ if (!isNumber(expr)) return void 0;
15144
+ if (expr.im !== 0) return void 0;
15145
+ const re = expr.re;
15146
+ if (!isFinite(re)) return void 0;
15147
+ return re;
15148
+ }
15149
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
15150
+ function foldTerms(terms, identity, op) {
15151
+ const identityValue = op === "+" ? 0 : 1;
15152
+ let numericAcc = null;
15153
+ const symbolic = [];
15154
+ for (const term of terms) {
15155
+ if (NUMERIC_LITERAL_RE.test(term)) {
15156
+ const val = parseFloat(term);
15157
+ if (op === "*" && val === 0) return "0.0";
15158
+ if (numericAcc === null) {
15159
+ numericAcc = val;
15160
+ } else {
15161
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
15162
+ }
15163
+ } else {
15164
+ symbolic.push(term);
15165
+ }
15166
+ }
15167
+ if (numericAcc !== null && numericAcc !== identityValue) {
15168
+ symbolic.unshift(formatFloat(numericAcc));
15169
+ }
15170
+ if (symbolic.length === 0) {
15171
+ if (numericAcc !== null) return formatFloat(numericAcc);
15172
+ return identity;
15173
+ }
15174
+ if (symbolic.length === 1) return symbolic[0];
15175
+ return symbolic.join(op === "+" ? " + " : " * ");
15176
+ }
15177
+ function tryGetComplexParts(expr, compile2) {
15178
+ if (isSymbol2(expr, "ImaginaryUnit")) {
15179
+ return { re: null, im: "1.0" };
15180
+ }
15181
+ if (isNumber(expr) && expr.im !== 0) {
15182
+ const re = expr.re;
15183
+ const im = expr.im;
15184
+ return {
15185
+ re: re !== 0 ? formatFloat(re) : null,
15186
+ im: formatFloat(im)
15187
+ };
15188
+ }
15189
+ if (isFunction2(expr, "Multiply")) {
15190
+ const ops = expr.ops;
15191
+ const iIndex = ops.findIndex(
15192
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
15193
+ );
15194
+ if (iIndex >= 0) {
15195
+ const iFactor = ops[iIndex];
15196
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
15197
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
15198
+ if (remaining.length === 0) {
15199
+ return { re: null, im: formatFloat(iScale) };
15200
+ }
15201
+ const compiledFactors = remaining.map((r) => compile2(r));
15202
+ if (iScale !== 1)
15203
+ compiledFactors.unshift(formatFloat(iScale));
15204
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
15205
+ return { re: null, im: imCode };
15206
+ }
15207
+ }
15208
+ if (BaseCompiler.isComplexValued(expr)) {
15209
+ return null;
15210
+ }
15211
+ return { re: compile2(expr), im: null };
15212
+ }
15213
+
15116
15214
  // node_modules/@arnog/colors/dist/index.mjs
15117
15215
  function gammaCorrect(channel) {
15118
15216
  const abs2 = Math.abs(channel);
@@ -19451,12 +19549,21 @@ ${lines.join("\n")}`;
19451
19549
  Abs: (args, compile2) => {
19452
19550
  if (BaseCompiler.isComplexValued(args[0]))
19453
19551
  return `_SYS.cabs(${compile2(args[0])})`;
19552
+ if (BaseCompiler.isNonNegative(args[0])) return compile2(args[0]);
19454
19553
  return `Math.abs(${compile2(args[0])})`;
19455
19554
  },
19456
19555
  Add: (args, compile2) => {
19457
19556
  if (args.length === 1) return compile2(args[0]);
19458
19557
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
19459
- if (!anyComplex) return `(${args.map((x) => compile2(x)).join(" + ")})`;
19558
+ if (!anyComplex) {
19559
+ const constants = args.map(tryGetConstant);
19560
+ if (constants.every((c) => c !== void 0))
19561
+ return String(constants.reduce((a, b) => a + b, 0));
19562
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
19563
+ if (nonZero.length === 0) return "0";
19564
+ if (nonZero.length === 1) return compile2(nonZero[0]);
19565
+ return `(${nonZero.map((x) => compile2(x)).join(" + ")})`;
19566
+ }
19460
19567
  const parts = args.map((a) => {
19461
19568
  const code = compile2(a);
19462
19569
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -19513,7 +19620,10 @@ ${lines.join("\n")}`;
19513
19620
  return `Math.atan(${compile2(args[0])})`;
19514
19621
  },
19515
19622
  Artanh: "Math.atanh",
19516
- Ceil: "Math.ceil",
19623
+ Ceil: (args, compile2) => {
19624
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19625
+ return `Math.ceil(${compile2(args[0])})`;
19626
+ },
19517
19627
  Chop: "_SYS.chop",
19518
19628
  Cos: (args, compile2) => {
19519
19629
  if (BaseCompiler.isComplexValued(args[0]))
@@ -19556,7 +19666,10 @@ ${lines.join("\n")}`;
19556
19666
  return `_SYS.cexp(${compile2(args[0])})`;
19557
19667
  return `Math.exp(${compile2(args[0])})`;
19558
19668
  },
19559
- Floor: "Math.floor",
19669
+ Floor: (args, compile2) => {
19670
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19671
+ return `Math.floor(${compile2(args[0])})`;
19672
+ },
19560
19673
  Fract: ([x], compile2) => {
19561
19674
  if (x === null) throw new Error("Fract: no argument");
19562
19675
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile2(x));
@@ -19652,12 +19765,20 @@ ${lines.join("\n")}`;
19652
19765
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
19653
19766
  return `_SYS.cpow(${compile2(base)}, ${compile2(exp3)})`;
19654
19767
  }
19655
- const expVal = exp3.re;
19656
- if (expVal === 0.5) return `Math.sqrt(${compile2(base)})`;
19657
- if (expVal === 1 / 3) return `Math.cbrt(${compile2(base)})`;
19658
- if (expVal === 1) return compile2(base);
19659
- if (expVal === -1) return `(1 / (${compile2(base)}))`;
19660
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile2(base)}))`;
19768
+ const bConst = tryGetConstant(base);
19769
+ const eConst = tryGetConstant(exp3);
19770
+ if (bConst !== void 0 && eConst !== void 0)
19771
+ return String(Math.pow(bConst, eConst));
19772
+ if (eConst === 0) return "1";
19773
+ if (eConst === 1) return compile2(base);
19774
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
19775
+ const code = compile2(base);
19776
+ return `(${code} * ${code})`;
19777
+ }
19778
+ if (eConst === -1) return `(1 / (${compile2(base)}))`;
19779
+ if (eConst === 0.5) return `Math.sqrt(${compile2(base)})`;
19780
+ if (eConst === 1 / 3) return `Math.cbrt(${compile2(base)})`;
19781
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile2(base)}))`;
19661
19782
  return `Math.pow(${compile2(base)}, ${compile2(exp3)})`;
19662
19783
  },
19663
19784
  Range: (args, compile2) => {
@@ -19694,16 +19815,29 @@ ${lines.join("\n")}`;
19694
19815
  Root: ([arg, exp3], compile2) => {
19695
19816
  if (arg === null) throw new Error("Root: no argument");
19696
19817
  if (exp3 === null) return `Math.sqrt(${compile2(arg)})`;
19697
- if (exp3?.re === 2) return `Math.sqrt(${compile2(arg)})`;
19698
- if (exp3?.re === 3) return `Math.cbrt(${compile2(arg)})`;
19699
- if (!isNaN(exp3?.re)) return `Math.pow(${compile2(arg)}, ${1 / exp3.re})`;
19818
+ const aConst = tryGetConstant(arg);
19819
+ const nConst = tryGetConstant(exp3);
19820
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
19821
+ return String(Math.pow(aConst, 1 / nConst));
19822
+ if (nConst === 2) return `Math.sqrt(${compile2(arg)})`;
19823
+ if (nConst === 3) return `Math.cbrt(${compile2(arg)})`;
19824
+ if (nConst !== void 0) return `Math.pow(${compile2(arg)}, ${1 / nConst})`;
19700
19825
  return `Math.pow(${compile2(arg)}, 1 / (${compile2(exp3)}))`;
19701
19826
  },
19702
19827
  Random: "Math.random",
19703
- Round: "Math.round",
19828
+ Round: (args, compile2) => {
19829
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19830
+ return `Math.round(${compile2(args[0])})`;
19831
+ },
19704
19832
  Square: (args, compile2) => {
19705
19833
  const arg = args[0];
19706
19834
  if (arg === null) throw new Error("Square: no argument");
19835
+ const c = tryGetConstant(arg);
19836
+ if (c !== void 0) return String(c * c);
19837
+ if (isSymbol2(arg)) {
19838
+ const code = compile2(arg);
19839
+ return `(${code} * ${code})`;
19840
+ }
19707
19841
  return `Math.pow(${compile2(arg)}, 2)`;
19708
19842
  },
19709
19843
  Sec: (args, compile2) => {
@@ -19736,6 +19870,8 @@ ${lines.join("\n")}`;
19736
19870
  Sqrt: (args, compile2) => {
19737
19871
  if (BaseCompiler.isComplexValued(args[0]))
19738
19872
  return `_SYS.csqrt(${compile2(args[0])})`;
19873
+ const c = tryGetConstant(args[0]);
19874
+ if (c !== void 0) return String(Math.sqrt(c));
19739
19875
  return `Math.sqrt(${compile2(args[0])})`;
19740
19876
  },
19741
19877
  Tan: (args, compile2) => {
@@ -19752,9 +19888,14 @@ ${lines.join("\n")}`;
19752
19888
  if (a === null || b === null) throw new Error("Mod: missing argument");
19753
19889
  const ca = compile2(a);
19754
19890
  const cb = compile2(b);
19891
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
19892
+ return `(${ca} % ${cb})`;
19755
19893
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
19756
19894
  },
19757
- Truncate: "Math.trunc",
19895
+ Truncate: (args, compile2) => {
19896
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
19897
+ return `Math.trunc(${compile2(args[0])})`;
19898
+ },
19758
19899
  Remainder: ([a, b], compile2) => {
19759
19900
  if (a === null || b === null)
19760
19901
  throw new Error("Remainder: missing argument");
@@ -19762,25 +19903,20 @@ ${lines.join("\n")}`;
19762
19903
  a
19763
19904
  )} / ${compile2(b)}))`;
19764
19905
  },
19765
- // Arithmetic operators handled as functions for completeness
19766
- Subtract: ([a, b], compile2) => {
19767
- if (a === null || b === null) throw new Error("Subtract: missing argument");
19768
- const ac = BaseCompiler.isComplexValued(a);
19769
- const bc = BaseCompiler.isComplexValued(b);
19770
- if (!ac && !bc) return `(${compile2(a)} - ${compile2(b)})`;
19771
- const ca = compile2(a);
19772
- const cb = compile2(b);
19773
- const reA = ac ? `(${ca}).re` : ca;
19774
- const imA = ac ? `(${ca}).im` : "0";
19775
- const reB = bc ? `(${cb}).re` : cb;
19776
- const imB = bc ? `(${cb}).im` : "0";
19777
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
19778
- },
19906
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
19907
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
19779
19908
  Divide: ([a, b], compile2) => {
19780
19909
  if (a === null || b === null) throw new Error("Divide: missing argument");
19781
19910
  const ac = BaseCompiler.isComplexValued(a);
19782
19911
  const bc = BaseCompiler.isComplexValued(b);
19783
- if (!ac && !bc) return `(${compile2(a)} / ${compile2(b)})`;
19912
+ if (!ac && !bc) {
19913
+ const ca = tryGetConstant(a);
19914
+ const cb = tryGetConstant(b);
19915
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
19916
+ return String(ca / cb);
19917
+ if (cb === 1) return compile2(a);
19918
+ return `(${compile2(a)} / ${compile2(b)})`;
19919
+ }
19784
19920
  if (ac && bc) {
19785
19921
  return `(() => { const _a = ${compile2(a)}, _b = ${compile2(
19786
19922
  b
@@ -19797,13 +19933,26 @@ ${lines.join("\n")}`;
19797
19933
  },
19798
19934
  Negate: ([x], compile2) => {
19799
19935
  if (x === null) throw new Error("Negate: no argument");
19800
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile2(x)})`;
19936
+ if (!BaseCompiler.isComplexValued(x)) {
19937
+ const c = tryGetConstant(x);
19938
+ if (c !== void 0) return String(-c);
19939
+ return `(-${compile2(x)})`;
19940
+ }
19801
19941
  return `_SYS.cneg(${compile2(x)})`;
19802
19942
  },
19803
19943
  Multiply: (args, compile2) => {
19804
19944
  if (args.length === 1) return compile2(args[0]);
19805
19945
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
19806
- if (!anyComplex) return `(${args.map((x) => compile2(x)).join(" * ")})`;
19946
+ if (!anyComplex) {
19947
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
19948
+ const constants = args.map(tryGetConstant);
19949
+ if (constants.every((c) => c !== void 0))
19950
+ return String(constants.reduce((a, b) => a * b, 1));
19951
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
19952
+ if (nonOne.length === 0) return "1";
19953
+ if (nonOne.length === 1) return compile2(nonOne[0]);
19954
+ return `(${nonOne.map((x) => compile2(x)).join(" * ")})`;
19955
+ }
19807
19956
  if (args.length === 2) {
19808
19957
  const ac = BaseCompiler.isComplexValued(args[0]);
19809
19958
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -19888,20 +20037,30 @@ ${lines.join("\n")}`;
19888
20037
  AiryAi: "_SYS.airyAi",
19889
20038
  AiryBi: "_SYS.airyBi",
19890
20039
  // Combinatorics
20040
+ Mandelbrot: ([c, maxIter], compile2) => {
20041
+ if (c === null || maxIter === null)
20042
+ throw new Error("Mandelbrot: missing arguments");
20043
+ return `_SYS.mandelbrot(${compile2(c)}, ${compile2(maxIter)})`;
20044
+ },
20045
+ Julia: ([z, c, maxIter], compile2) => {
20046
+ if (z === null || c === null || maxIter === null)
20047
+ throw new Error("Julia: missing arguments");
20048
+ return `_SYS.julia(${compile2(z)}, ${compile2(c)}, ${compile2(maxIter)})`;
20049
+ },
19891
20050
  Binomial: (args, compile2) => `_SYS.binomial(${compile2(args[0])}, ${compile2(args[1])})`,
19892
20051
  Fibonacci: "_SYS.fibonacci",
19893
20052
  // Complex-specific functions
19894
- Re: (args, compile2) => {
20053
+ Real: (args, compile2) => {
19895
20054
  if (BaseCompiler.isComplexValued(args[0]))
19896
20055
  return `(${compile2(args[0])}).re`;
19897
20056
  return compile2(args[0]);
19898
20057
  },
19899
- Im: (args, compile2) => {
20058
+ Imaginary: (args, compile2) => {
19900
20059
  if (BaseCompiler.isComplexValued(args[0]))
19901
20060
  return `(${compile2(args[0])}).im`;
19902
20061
  return "0";
19903
20062
  },
19904
- Arg: (args, compile2) => {
20063
+ Argument: (args, compile2) => {
19905
20064
  if (BaseCompiler.isComplexValued(args[0]))
19906
20065
  return `_SYS.carg(${compile2(args[0])})`;
19907
20066
  return `(${compile2(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -20237,6 +20396,41 @@ ${lines.join("\n")}`;
20237
20396
  sinc,
20238
20397
  fresnelS,
20239
20398
  fresnelC,
20399
+ mandelbrot: (c, maxIter) => {
20400
+ let zx = 0, zy = 0;
20401
+ const cx = typeof c === "number" ? c : c.re;
20402
+ const cy = typeof c === "number" ? 0 : c.im;
20403
+ const n = Math.round(maxIter);
20404
+ for (let i = 0; i < n; i++) {
20405
+ const newZx = zx * zx - zy * zy + cx;
20406
+ zy = 2 * zx * zy + cy;
20407
+ zx = newZx;
20408
+ const mag2 = zx * zx + zy * zy;
20409
+ if (mag2 > 4) {
20410
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
20411
+ return Math.max(0, Math.min(1, smooth));
20412
+ }
20413
+ }
20414
+ return 1;
20415
+ },
20416
+ julia: (z, c, maxIter) => {
20417
+ let zx = typeof z === "number" ? z : z.re;
20418
+ let zy = typeof z === "number" ? 0 : z.im;
20419
+ const cx = typeof c === "number" ? c : c.re;
20420
+ const cy = typeof c === "number" ? 0 : c.im;
20421
+ const n = Math.round(maxIter);
20422
+ for (let i = 0; i < n; i++) {
20423
+ const newZx = zx * zx - zy * zy + cx;
20424
+ zy = 2 * zx * zy + cy;
20425
+ zx = newZx;
20426
+ const mag2 = zx * zx + zy * zy;
20427
+ if (mag2 > 4) {
20428
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
20429
+ return Math.max(0, Math.min(1, smooth));
20430
+ }
20431
+ }
20432
+ return 1;
20433
+ },
20240
20434
  binomial: choose,
20241
20435
  fibonacci,
20242
20436
  // Complex helpers
@@ -20582,6 +20776,7 @@ ${lines.join("\n")}`;
20582
20776
  Add: ["+", 11],
20583
20777
  Negate: ["-", 14],
20584
20778
  Subtract: ["-", 11],
20779
+ // Subtract canonicalizes to Add+Negate; kept as fallback
20585
20780
  Multiply: ["*", 12],
20586
20781
  Divide: ["/", 13],
20587
20782
  Equal: ["==", 8],
@@ -20597,6 +20792,12 @@ ${lines.join("\n")}`;
20597
20792
  function gpuVec2(target) {
20598
20793
  return target?.language === "wgsl" ? "vec2f" : "vec2";
20599
20794
  }
20795
+ function compileIntArg(expr, compile2, target) {
20796
+ const c = tryGetConstant(expr);
20797
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
20798
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
20799
+ return `${intCast}(${compile2(expr)})`;
20800
+ }
20600
20801
  var GPU_UNROLL_LIMIT = 100;
20601
20802
  function compileGPUSumProduct(kind, args, _compile, target) {
20602
20803
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -20659,88 +20860,113 @@ ${lines.join("\n")}`;
20659
20860
  if (args.length === 0) return "0.0";
20660
20861
  if (args.length === 1) return compile2(args[0]);
20661
20862
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20662
- if (!anyComplex) return args.map((x) => compile2(x)).join(" + ");
20663
- const v2 = gpuVec2(target);
20664
- return args.map((a) => {
20665
- const code = compile2(a);
20666
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20667
- }).join(" + ");
20668
- },
20669
- Multiply: (args, compile2, _target) => {
20670
- if (args.length === 0) return "1.0";
20671
- if (args.length === 1) return compile2(args[0]);
20672
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20673
- if (!anyComplex) return args.map((x) => compile2(x)).join(" * ");
20674
- let result = compile2(args[0]);
20675
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
20676
- for (let i = 1; i < args.length; i++) {
20677
- const code = compile2(args[i]);
20678
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
20679
- if (!resultIsComplex && !argIsComplex) {
20680
- result = `(${result} * ${code})`;
20681
- } else if (resultIsComplex && !argIsComplex) {
20682
- result = `(${code} * ${result})`;
20683
- } else if (!resultIsComplex && argIsComplex) {
20684
- result = `(${result} * ${code})`;
20685
- resultIsComplex = true;
20686
- } else {
20687
- result = `_gpu_cmul(${result}, ${code})`;
20688
- }
20863
+ if (!anyComplex) {
20864
+ return foldTerms(
20865
+ args.map((x) => compile2(x)),
20866
+ "0.0",
20867
+ "+"
20868
+ );
20689
20869
  }
20690
- return result;
20691
- },
20692
- Subtract: (args, compile2, target) => {
20693
- if (args.length === 0) return "0.0";
20870
+ const parts = args.map((a) => tryGetComplexParts(a, compile2));
20871
+ if (parts.some((p) => p === null)) {
20872
+ const v2 = gpuVec2(target);
20873
+ return args.map((a) => {
20874
+ const code = compile2(a);
20875
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20876
+ }).join(" + ");
20877
+ }
20878
+ const reParts = [];
20879
+ const imParts = [];
20880
+ for (const p of parts) {
20881
+ if (p.re !== null) reParts.push(p.re);
20882
+ if (p.im !== null) imParts.push(p.im);
20883
+ }
20884
+ const reSum = foldTerms(reParts, "0.0", "+");
20885
+ const imSum = foldTerms(imParts, "0.0", "+");
20886
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
20887
+ },
20888
+ Multiply: (args, compile2, target) => {
20889
+ if (args.length === 0) return "1.0";
20694
20890
  if (args.length === 1) return compile2(args[0]);
20695
20891
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
20696
20892
  if (!anyComplex) {
20697
- if (args.length === 2) return `${compile2(args[0])} - ${compile2(args[1])}`;
20698
- let result2 = compile2(args[0]);
20699
- for (let i = 1; i < args.length; i++) {
20700
- result2 = `${result2} - ${compile2(args[i])}`;
20701
- }
20702
- return result2;
20703
- }
20704
- const v2 = gpuVec2(target);
20705
- const promote = (a) => {
20706
- const code = compile2(a);
20707
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
20708
- };
20709
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
20710
- let result = promote(args[0]);
20711
- for (let i = 1; i < args.length; i++) {
20712
- result = `${result} - ${promote(args[i])}`;
20893
+ return foldTerms(
20894
+ args.map((x) => compile2(x)),
20895
+ "1.0",
20896
+ "*"
20897
+ );
20713
20898
  }
20899
+ const iIndex = args.findIndex(
20900
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
20901
+ );
20902
+ if (iIndex >= 0) {
20903
+ const iFactor = args[iIndex];
20904
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
20905
+ const realFactors = args.filter((_, i) => i !== iIndex);
20906
+ const v2 = gpuVec2(target);
20907
+ if (realFactors.length === 0)
20908
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
20909
+ const factors = realFactors.map((f) => compile2(f));
20910
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
20911
+ const imCode = foldTerms(factors, "1.0", "*");
20912
+ return `${v2}(0.0, ${imCode})`;
20913
+ }
20914
+ const realCodes = [];
20915
+ const complexCodes = [];
20916
+ for (const a of args) {
20917
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile2(a));
20918
+ else realCodes.push(compile2(a));
20919
+ }
20920
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
20921
+ let result = complexCodes[0];
20922
+ for (let i = 1; i < complexCodes.length; i++) {
20923
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
20924
+ }
20925
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
20714
20926
  return result;
20715
20927
  },
20928
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
20929
+ // The operator entry in GPU_OPERATORS handles any edge cases.
20716
20930
  Divide: (args, compile2, target) => {
20717
20931
  if (args.length === 0) return "1.0";
20718
20932
  if (args.length === 1) return compile2(args[0]);
20719
20933
  const ac = BaseCompiler.isComplexValued(args[0]);
20720
20934
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
20721
20935
  if (!ac && !bc) {
20722
- if (args.length === 2) return `${compile2(args[0])} / ${compile2(args[1])}`;
20936
+ if (args.length === 2) {
20937
+ const a = tryGetConstant(args[0]);
20938
+ const b = tryGetConstant(args[1]);
20939
+ if (a !== void 0 && b !== void 0 && b !== 0)
20940
+ return formatFloat(a / b);
20941
+ if (b === 1) return compile2(args[0]);
20942
+ return `${compile2(args[0])} / ${compile2(args[1])}`;
20943
+ }
20723
20944
  let result = compile2(args[0]);
20724
- for (let i = 1; i < args.length; i++) {
20945
+ for (let i = 1; i < args.length; i++)
20725
20946
  result = `${result} / ${compile2(args[i])}`;
20726
- }
20727
20947
  return result;
20728
20948
  }
20729
20949
  if (ac && bc) return `_gpu_cdiv(${compile2(args[0])}, ${compile2(args[1])})`;
20730
- if (ac && !bc) {
20731
- return `(${compile2(args[0])} / ${compile2(args[1])})`;
20732
- }
20950
+ if (ac && !bc) return `(${compile2(args[0])} / ${compile2(args[1])})`;
20733
20951
  const v2 = gpuVec2(target);
20734
20952
  return `_gpu_cdiv(${v2}(${compile2(args[0])}, 0.0), ${compile2(args[1])})`;
20735
20953
  },
20736
- Negate: ([x], compile2) => {
20954
+ Negate: ([x], compile2, target) => {
20737
20955
  if (x === null) throw new Error("Negate: no argument");
20956
+ const c = tryGetConstant(x);
20957
+ if (c !== void 0) return formatFloat(-c);
20958
+ if (isNumber(x) && x.im !== 0) {
20959
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
20960
+ }
20961
+ if (isSymbol2(x, "ImaginaryUnit"))
20962
+ return `${gpuVec2(target)}(0.0, -1.0)`;
20738
20963
  return `(-${compile2(x)})`;
20739
20964
  },
20740
20965
  // Standard math functions with complex dispatch
20741
20966
  Abs: (args, compile2) => {
20742
20967
  if (BaseCompiler.isComplexValued(args[0]))
20743
20968
  return `length(${compile2(args[0])})`;
20969
+ if (BaseCompiler.isNonNegative(args[0])) return compile2(args[0]);
20744
20970
  return `abs(${compile2(args[0])})`;
20745
20971
  },
20746
20972
  Arccos: (args, compile2) => {
@@ -20758,7 +20984,10 @@ ${lines.join("\n")}`;
20758
20984
  return `_gpu_catan(${compile2(args[0])})`;
20759
20985
  return `atan(${compile2(args[0])})`;
20760
20986
  },
20761
- Ceil: "ceil",
20987
+ Ceil: (args, compile2) => {
20988
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
20989
+ return `ceil(${compile2(args[0])})`;
20990
+ },
20762
20991
  Clamp: "clamp",
20763
20992
  Cos: (args, compile2) => {
20764
20993
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20772,7 +21001,10 @@ ${lines.join("\n")}`;
20772
21001
  return `exp(${compile2(args[0])})`;
20773
21002
  },
20774
21003
  Exp2: "exp2",
20775
- Floor: "floor",
21004
+ Floor: (args, compile2) => {
21005
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21006
+ return `floor(${compile2(args[0])})`;
21007
+ },
20776
21008
  Fract: "fract",
20777
21009
  Ln: (args, compile2) => {
20778
21010
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20794,10 +21026,25 @@ ${lines.join("\n")}`;
20794
21026
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile2(exp3) : `${v2}(${compile2(exp3)}, 0.0)`;
20795
21027
  return `_gpu_cpow(${bCode}, ${eCode})`;
20796
21028
  }
21029
+ const bConst = tryGetConstant(base);
21030
+ const eConst = tryGetConstant(exp3);
21031
+ if (bConst !== void 0 && eConst !== void 0)
21032
+ return formatFloat(Math.pow(bConst, eConst));
21033
+ if (eConst === 0) return "1.0";
21034
+ if (eConst === 1) return compile2(base);
21035
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
21036
+ const code = compile2(base);
21037
+ return `(${code} * ${code})`;
21038
+ }
21039
+ if (eConst === -1) return `(1.0 / ${compile2(base)})`;
21040
+ if (eConst === 0.5) return `sqrt(${compile2(base)})`;
20797
21041
  return `pow(${compile2(base)}, ${compile2(exp3)})`;
20798
21042
  },
20799
21043
  Radians: "radians",
20800
- Round: "round",
21044
+ Round: (args, compile2) => {
21045
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21046
+ return `round(${compile2(args[0])})`;
21047
+ },
20801
21048
  Sign: "sign",
20802
21049
  Sin: (args, compile2) => {
20803
21050
  if (BaseCompiler.isComplexValued(args[0]))
@@ -20808,6 +21055,8 @@ ${lines.join("\n")}`;
20808
21055
  Sqrt: (args, compile2) => {
20809
21056
  if (BaseCompiler.isComplexValued(args[0]))
20810
21057
  return `_gpu_csqrt(${compile2(args[0])})`;
21058
+ const c = tryGetConstant(args[0]);
21059
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
20811
21060
  return `sqrt(${compile2(args[0])})`;
20812
21061
  },
20813
21062
  Step: "step",
@@ -20816,17 +21065,20 @@ ${lines.join("\n")}`;
20816
21065
  return `_gpu_ctan(${compile2(args[0])})`;
20817
21066
  return `tan(${compile2(args[0])})`;
20818
21067
  },
20819
- Truncate: "trunc",
21068
+ Truncate: (args, compile2) => {
21069
+ if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21070
+ return `trunc(${compile2(args[0])})`;
21071
+ },
20820
21072
  // Complex-specific functions
20821
- Re: (args, compile2) => {
21073
+ Real: (args, compile2) => {
20822
21074
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile2(args[0])}).x`;
20823
21075
  return compile2(args[0]);
20824
21076
  },
20825
- Im: (args, compile2) => {
21077
+ Imaginary: (args, compile2) => {
20826
21078
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile2(args[0])}).y`;
20827
21079
  return "0.0";
20828
21080
  },
20829
- Arg: (args, compile2) => {
21081
+ Argument: (args, compile2) => {
20830
21082
  if (BaseCompiler.isComplexValued(args[0])) {
20831
21083
  const code = compile2(args[0]);
20832
21084
  return `atan(${code}.y, ${code}.x)`;
@@ -21041,13 +21293,20 @@ ${lines.join("\n")}`;
21041
21293
  },
21042
21294
  Square: ([x], compile2) => {
21043
21295
  if (x === null) throw new Error("Square: no argument");
21044
- const arg = compile2(x);
21045
- return `(${arg} * ${arg})`;
21296
+ if (isSymbol2(x) || isNumber(x)) {
21297
+ const arg = compile2(x);
21298
+ return `(${arg} * ${arg})`;
21299
+ }
21300
+ return `pow(${compile2(x)}, 2.0)`;
21046
21301
  },
21047
21302
  Root: ([x, n], compile2) => {
21048
21303
  if (x === null) throw new Error("Root: no argument");
21049
21304
  if (n === null || n === void 0) return `sqrt(${compile2(x)})`;
21050
- if (n?.re === 2) return `sqrt(${compile2(x)})`;
21305
+ const nConst = tryGetConstant(n);
21306
+ if (nConst === 2) return `sqrt(${compile2(x)})`;
21307
+ const xConst = tryGetConstant(x);
21308
+ if (xConst !== void 0 && nConst !== void 0)
21309
+ return formatFloat(Math.pow(xConst, 1 / nConst));
21051
21310
  return `pow(${compile2(x)}, 1.0 / ${compile2(n)})`;
21052
21311
  },
21053
21312
  // Color functions (pure-math, GPU-compilable)
@@ -21089,18 +21348,14 @@ ${lines.join("\n")}`;
21089
21348
  Mandelbrot: ([c, maxIter], compile2, target) => {
21090
21349
  if (c === null || maxIter === null)
21091
21350
  throw new Error("Mandelbrot: missing arguments");
21092
- const intCast = target?.language === "wgsl" ? "i32" : "int";
21093
- return `_fractal_mandelbrot(${compile2(c)}, ${intCast}(${compile2(
21094
- maxIter
21095
- )}))`;
21351
+ const iterCode = compileIntArg(maxIter, compile2, target);
21352
+ return `_fractal_mandelbrot(${compile2(c)}, ${iterCode})`;
21096
21353
  },
21097
21354
  Julia: ([z, c, maxIter], compile2, target) => {
21098
21355
  if (z === null || c === null || maxIter === null)
21099
21356
  throw new Error("Julia: missing arguments");
21100
- const intCast = target?.language === "wgsl" ? "i32" : "int";
21101
- return `_fractal_julia(${compile2(z)}, ${compile2(c)}, ${intCast}(${compile2(
21102
- maxIter
21103
- )}))`;
21357
+ const iterCode = compileIntArg(maxIter, compile2, target);
21358
+ return `_fractal_julia(${compile2(z)}, ${compile2(c)}, ${iterCode})`;
21104
21359
  },
21105
21360
  // Vector/Matrix operations
21106
21361
  Cross: "cross",
@@ -22455,6 +22710,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
22455
22710
  Negate: ["-", 14],
22456
22711
  // Unary operator
22457
22712
  Subtract: ["-", 11],
22713
+ // Subtract canonicalizes to Add+Negate; kept as fallback
22458
22714
  Multiply: ["*", 12],
22459
22715
  Divide: ["/", 13],
22460
22716
  Power: ["**", 15],
@@ -22482,16 +22738,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
22482
22738
  if (args.length === 1) return compile2(args[0]);
22483
22739
  return args.map((x) => compile2(x)).join(" * ");
22484
22740
  },
22485
- Subtract: (args, compile2) => {
22486
- if (args.length === 0) return "0";
22487
- if (args.length === 1) return compile2(args[0]);
22488
- if (args.length === 2) return `${compile2(args[0])} - ${compile2(args[1])}`;
22489
- let result = compile2(args[0]);
22490
- for (let i = 1; i < args.length; i++) {
22491
- result = `${result} - ${compile2(args[i])}`;
22492
- }
22493
- return result;
22494
- },
22741
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
22495
22742
  Divide: (args, compile2) => {
22496
22743
  if (args.length === 0) return "1";
22497
22744
  if (args.length === 1) return compile2(args[0]);
@@ -24156,6 +24403,7 @@ ${code}`;
24156
24403
  Add: ["_IA.add", 20],
24157
24404
  Negate: ["_IA.negate", 20],
24158
24405
  Subtract: ["_IA.sub", 20],
24406
+ // Subtract canonicalizes to Add+Negate; kept as fallback
24159
24407
  Multiply: ["_IA.mul", 20],
24160
24408
  Divide: ["_IA.div", 20],
24161
24409
  // Comparisons return BoolInterval
@@ -24180,17 +24428,7 @@ ${code}`;
24180
24428
  }
24181
24429
  return result;
24182
24430
  },
24183
- Subtract: (args, compile2) => {
24184
- if (args.length === 0) return "_IA.point(0)";
24185
- if (args.length === 1) return `_IA.negate(${compile2(args[0])})`;
24186
- if (args.length === 2)
24187
- return `_IA.sub(${compile2(args[0])}, ${compile2(args[1])})`;
24188
- let result = compile2(args[0]);
24189
- for (let i = 1; i < args.length; i++) {
24190
- result = `_IA.sub(${result}, ${compile2(args[i])})`;
24191
- }
24192
- return result;
24193
- },
24431
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
24194
24432
  Multiply: (args, compile2) => {
24195
24433
  if (args.length === 0) return "_IA.point(1)";
24196
24434
  if (args.length === 1) return compile2(args[0]);
@@ -24573,7 +24811,7 @@ ${code}`;
24573
24811
  }
24574
24812
 
24575
24813
  // src/compile.ts
24576
- var version = "0.55.2";
24814
+ var version = "0.55.3";
24577
24815
  return __toCommonJS(compile_exports);
24578
24816
  })();
24579
24817
  /*! Bundled license information: