@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
  (function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ComputeEngine = {}));})(this, (function (exports) { 'use strict';
3
3
  var ComputeEngine = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -30759,8 +30759,7 @@ ${lines.join("\n")}`;
30759
30759
  broadcastable: true,
30760
30760
  idempotent: true,
30761
30761
  complexity: 1200,
30762
- signature: "(number) -> number",
30763
- type: ([x]) => x.type,
30762
+ signature: "(number) -> real",
30764
30763
  sgn: ([x]) => {
30765
30764
  if (x.isSame(0)) return "zero";
30766
30765
  if (isNumber(x)) return "positive";
@@ -54311,8 +54310,11 @@ Error in definition of "${name}"`,
54311
54310
  for (const local of locals) {
54312
54311
  for (const arg of args) {
54313
54312
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
54314
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
54313
+ const rhs = arg.ops[1];
54314
+ if (_BaseCompiler.isComplexValued(rhs)) {
54315
54315
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
54316
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
54317
+ typeHints[local] = isWGSL ? "i32" : "int";
54316
54318
  }
54317
54319
  break;
54318
54320
  }
@@ -54517,12 +54519,9 @@ Error in definition of "${name}"`,
54517
54519
  /**
54518
54520
  * Determine at compile time whether an expression produces a complex value.
54519
54521
  *
54520
- * Rules:
54521
- * - Numbers: complex if im !== 0
54522
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
54523
- * (undefined is treated as real -- assume-real policy)
54524
- * - Functions: Abs, Arg, Re, Im always return real.
54525
- * All others: complex if any operand is complex.
54522
+ * Uses the expression's declared type (from operator signatures) when
54523
+ * available. Falls back to operand inspection for functions whose
54524
+ * return type is unknown.
54526
54525
  */
54527
54526
  static isComplexValued(expr2) {
54528
54527
  if (isNumber(expr2)) return expr2.im !== 0;
@@ -54533,13 +54532,24 @@ Error in definition of "${name}"`,
54533
54532
  return t.matches("complex") && !t.matches("real");
54534
54533
  }
54535
54534
  if (isFunction2(expr2)) {
54536
- const op = expr2.operator;
54537
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
54538
- return false;
54535
+ const t = expr2.type;
54536
+ if (t.matches("complex") && !t.matches("real")) return true;
54537
+ if (t.matches("real")) return false;
54539
54538
  return expr2.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
54540
54539
  }
54541
54540
  return false;
54542
54541
  }
54542
+ /** True if the expression is provably integer-typed. */
54543
+ static isIntegerValued(expr2) {
54544
+ if (isNumber(expr2)) return expr2.im === 0 && Number.isInteger(expr2.re);
54545
+ const t = expr2.type;
54546
+ return t ? t.matches("integer") : false;
54547
+ }
54548
+ /** True if the expression is provably non-negative (sign ≥ 0). */
54549
+ static isNonNegative(expr2) {
54550
+ if (isNumber(expr2)) return expr2.im === 0 && expr2.re >= 0;
54551
+ return expr2.isNonNegative === true;
54552
+ }
54543
54553
  /**
54544
54554
  * Generate a temporary variable name
54545
54555
  */
@@ -54793,14 +54803,27 @@ Error in definition of "${name}"`,
54793
54803
  } catch (e) {
54794
54804
  if (options?.fallback ?? true) {
54795
54805
  console.warn(
54796
- `Compilation fallback for "${expr2.operator}": ${e.message}`
54806
+ `Compilation fallback for "${expr2.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
54797
54807
  );
54808
+ const ce = expr2.engine;
54809
+ const fallbackRun = ((vars) => {
54810
+ ce.pushScope();
54811
+ try {
54812
+ if (vars && typeof vars === "object") {
54813
+ for (const [k, v] of Object.entries(vars))
54814
+ ce.assign(k, v);
54815
+ }
54816
+ return expr2.evaluate().re;
54817
+ } finally {
54818
+ ce.popScope();
54819
+ }
54820
+ });
54798
54821
  return {
54799
54822
  target: options?.to ?? "javascript",
54800
54823
  success: false,
54801
54824
  code: "",
54802
54825
  calling: "expression",
54803
- run: applicableN1(expr2)
54826
+ run: fallbackRun
54804
54827
  };
54805
54828
  }
54806
54829
  throw e;
@@ -59950,6 +59973,86 @@ Error in definition of "${name}"`,
59950
59973
  for (const symbol2 of Object.values(commonSymbols)) symbol2?.reset();
59951
59974
  }
59952
59975
 
59976
+ // src/compute-engine/compilation/constant-folding.ts
59977
+ function formatFloat(n) {
59978
+ const str = n.toString();
59979
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
59980
+ return `${str}.0`;
59981
+ }
59982
+ return str;
59983
+ }
59984
+ function tryGetConstant(expr2) {
59985
+ if (!isNumber(expr2)) return void 0;
59986
+ if (expr2.im !== 0) return void 0;
59987
+ const re = expr2.re;
59988
+ if (!isFinite(re)) return void 0;
59989
+ return re;
59990
+ }
59991
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
59992
+ function foldTerms(terms, identity, op) {
59993
+ const identityValue = op === "+" ? 0 : 1;
59994
+ let numericAcc = null;
59995
+ const symbolic = [];
59996
+ for (const term of terms) {
59997
+ if (NUMERIC_LITERAL_RE.test(term)) {
59998
+ const val = parseFloat(term);
59999
+ if (op === "*" && val === 0) return "0.0";
60000
+ if (numericAcc === null) {
60001
+ numericAcc = val;
60002
+ } else {
60003
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
60004
+ }
60005
+ } else {
60006
+ symbolic.push(term);
60007
+ }
60008
+ }
60009
+ if (numericAcc !== null && numericAcc !== identityValue) {
60010
+ symbolic.unshift(formatFloat(numericAcc));
60011
+ }
60012
+ if (symbolic.length === 0) {
60013
+ if (numericAcc !== null) return formatFloat(numericAcc);
60014
+ return identity;
60015
+ }
60016
+ if (symbolic.length === 1) return symbolic[0];
60017
+ return symbolic.join(op === "+" ? " + " : " * ");
60018
+ }
60019
+ function tryGetComplexParts(expr2, compile3) {
60020
+ if (isSymbol2(expr2, "ImaginaryUnit")) {
60021
+ return { re: null, im: "1.0" };
60022
+ }
60023
+ if (isNumber(expr2) && expr2.im !== 0) {
60024
+ const re = expr2.re;
60025
+ const im = expr2.im;
60026
+ return {
60027
+ re: re !== 0 ? formatFloat(re) : null,
60028
+ im: formatFloat(im)
60029
+ };
60030
+ }
60031
+ if (isFunction2(expr2, "Multiply")) {
60032
+ const ops = expr2.ops;
60033
+ const iIndex = ops.findIndex(
60034
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
60035
+ );
60036
+ if (iIndex >= 0) {
60037
+ const iFactor = ops[iIndex];
60038
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
60039
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
60040
+ if (remaining.length === 0) {
60041
+ return { re: null, im: formatFloat(iScale) };
60042
+ }
60043
+ const compiledFactors = remaining.map((r) => compile3(r));
60044
+ if (iScale !== 1)
60045
+ compiledFactors.unshift(formatFloat(iScale));
60046
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
60047
+ return { re: null, im: imCode };
60048
+ }
60049
+ }
60050
+ if (BaseCompiler.isComplexValued(expr2)) {
60051
+ return null;
60052
+ }
60053
+ return { re: compile3(expr2), im: null };
60054
+ }
60055
+
59953
60056
  // src/compute-engine/compilation/javascript-target.ts
59954
60057
  var JAVASCRIPT_OPERATORS = {
59955
60058
  Add: ["+", 11],
@@ -59973,12 +60076,21 @@ Error in definition of "${name}"`,
59973
60076
  Abs: (args, compile3) => {
59974
60077
  if (BaseCompiler.isComplexValued(args[0]))
59975
60078
  return `_SYS.cabs(${compile3(args[0])})`;
60079
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
59976
60080
  return `Math.abs(${compile3(args[0])})`;
59977
60081
  },
59978
60082
  Add: (args, compile3) => {
59979
60083
  if (args.length === 1) return compile3(args[0]);
59980
60084
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
59981
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" + ")})`;
60085
+ if (!anyComplex) {
60086
+ const constants = args.map(tryGetConstant);
60087
+ if (constants.every((c) => c !== void 0))
60088
+ return String(constants.reduce((a, b) => a + b, 0));
60089
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
60090
+ if (nonZero.length === 0) return "0";
60091
+ if (nonZero.length === 1) return compile3(nonZero[0]);
60092
+ return `(${nonZero.map((x) => compile3(x)).join(" + ")})`;
60093
+ }
59982
60094
  const parts = args.map((a) => {
59983
60095
  const code = compile3(a);
59984
60096
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -60035,7 +60147,10 @@ Error in definition of "${name}"`,
60035
60147
  return `Math.atan(${compile3(args[0])})`;
60036
60148
  },
60037
60149
  Artanh: "Math.atanh",
60038
- Ceil: "Math.ceil",
60150
+ Ceil: (args, compile3) => {
60151
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60152
+ return `Math.ceil(${compile3(args[0])})`;
60153
+ },
60039
60154
  Chop: "_SYS.chop",
60040
60155
  Cos: (args, compile3) => {
60041
60156
  if (BaseCompiler.isComplexValued(args[0]))
@@ -60078,7 +60193,10 @@ Error in definition of "${name}"`,
60078
60193
  return `_SYS.cexp(${compile3(args[0])})`;
60079
60194
  return `Math.exp(${compile3(args[0])})`;
60080
60195
  },
60081
- Floor: "Math.floor",
60196
+ Floor: (args, compile3) => {
60197
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60198
+ return `Math.floor(${compile3(args[0])})`;
60199
+ },
60082
60200
  Fract: ([x], compile3) => {
60083
60201
  if (x === null) throw new Error("Fract: no argument");
60084
60202
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile3(x));
@@ -60174,12 +60292,20 @@ Error in definition of "${name}"`,
60174
60292
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
60175
60293
  return `_SYS.cpow(${compile3(base)}, ${compile3(exp3)})`;
60176
60294
  }
60177
- const expVal = exp3.re;
60178
- if (expVal === 0.5) return `Math.sqrt(${compile3(base)})`;
60179
- if (expVal === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60180
- if (expVal === 1) return compile3(base);
60181
- if (expVal === -1) return `(1 / (${compile3(base)}))`;
60182
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60295
+ const bConst = tryGetConstant(base);
60296
+ const eConst = tryGetConstant(exp3);
60297
+ if (bConst !== void 0 && eConst !== void 0)
60298
+ return String(Math.pow(bConst, eConst));
60299
+ if (eConst === 0) return "1";
60300
+ if (eConst === 1) return compile3(base);
60301
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
60302
+ const code = compile3(base);
60303
+ return `(${code} * ${code})`;
60304
+ }
60305
+ if (eConst === -1) return `(1 / (${compile3(base)}))`;
60306
+ if (eConst === 0.5) return `Math.sqrt(${compile3(base)})`;
60307
+ if (eConst === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60308
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60183
60309
  return `Math.pow(${compile3(base)}, ${compile3(exp3)})`;
60184
60310
  },
60185
60311
  Range: (args, compile3) => {
@@ -60216,16 +60342,29 @@ Error in definition of "${name}"`,
60216
60342
  Root: ([arg, exp3], compile3) => {
60217
60343
  if (arg === null) throw new Error("Root: no argument");
60218
60344
  if (exp3 === null) return `Math.sqrt(${compile3(arg)})`;
60219
- if (exp3?.re === 2) return `Math.sqrt(${compile3(arg)})`;
60220
- if (exp3?.re === 3) return `Math.cbrt(${compile3(arg)})`;
60221
- if (!isNaN(exp3?.re)) return `Math.pow(${compile3(arg)}, ${1 / exp3.re})`;
60345
+ const aConst = tryGetConstant(arg);
60346
+ const nConst = tryGetConstant(exp3);
60347
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
60348
+ return String(Math.pow(aConst, 1 / nConst));
60349
+ if (nConst === 2) return `Math.sqrt(${compile3(arg)})`;
60350
+ if (nConst === 3) return `Math.cbrt(${compile3(arg)})`;
60351
+ if (nConst !== void 0) return `Math.pow(${compile3(arg)}, ${1 / nConst})`;
60222
60352
  return `Math.pow(${compile3(arg)}, 1 / (${compile3(exp3)}))`;
60223
60353
  },
60224
60354
  Random: "Math.random",
60225
- Round: "Math.round",
60355
+ Round: (args, compile3) => {
60356
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60357
+ return `Math.round(${compile3(args[0])})`;
60358
+ },
60226
60359
  Square: (args, compile3) => {
60227
60360
  const arg = args[0];
60228
60361
  if (arg === null) throw new Error("Square: no argument");
60362
+ const c = tryGetConstant(arg);
60363
+ if (c !== void 0) return String(c * c);
60364
+ if (isSymbol2(arg)) {
60365
+ const code = compile3(arg);
60366
+ return `(${code} * ${code})`;
60367
+ }
60229
60368
  return `Math.pow(${compile3(arg)}, 2)`;
60230
60369
  },
60231
60370
  Sec: (args, compile3) => {
@@ -60258,6 +60397,8 @@ Error in definition of "${name}"`,
60258
60397
  Sqrt: (args, compile3) => {
60259
60398
  if (BaseCompiler.isComplexValued(args[0]))
60260
60399
  return `_SYS.csqrt(${compile3(args[0])})`;
60400
+ const c = tryGetConstant(args[0]);
60401
+ if (c !== void 0) return String(Math.sqrt(c));
60261
60402
  return `Math.sqrt(${compile3(args[0])})`;
60262
60403
  },
60263
60404
  Tan: (args, compile3) => {
@@ -60274,9 +60415,14 @@ Error in definition of "${name}"`,
60274
60415
  if (a === null || b === null) throw new Error("Mod: missing argument");
60275
60416
  const ca = compile3(a);
60276
60417
  const cb = compile3(b);
60418
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
60419
+ return `(${ca} % ${cb})`;
60277
60420
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
60278
60421
  },
60279
- Truncate: "Math.trunc",
60422
+ Truncate: (args, compile3) => {
60423
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60424
+ return `Math.trunc(${compile3(args[0])})`;
60425
+ },
60280
60426
  Remainder: ([a, b], compile3) => {
60281
60427
  if (a === null || b === null)
60282
60428
  throw new Error("Remainder: missing argument");
@@ -60284,25 +60430,20 @@ Error in definition of "${name}"`,
60284
60430
  a
60285
60431
  )} / ${compile3(b)}))`;
60286
60432
  },
60287
- // Arithmetic operators handled as functions for completeness
60288
- Subtract: ([a, b], compile3) => {
60289
- if (a === null || b === null) throw new Error("Subtract: missing argument");
60290
- const ac = BaseCompiler.isComplexValued(a);
60291
- const bc = BaseCompiler.isComplexValued(b);
60292
- if (!ac && !bc) return `(${compile3(a)} - ${compile3(b)})`;
60293
- const ca = compile3(a);
60294
- const cb = compile3(b);
60295
- const reA = ac ? `(${ca}).re` : ca;
60296
- const imA = ac ? `(${ca}).im` : "0";
60297
- const reB = bc ? `(${cb}).re` : cb;
60298
- const imB = bc ? `(${cb}).im` : "0";
60299
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
60300
- },
60433
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
60434
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
60301
60435
  Divide: ([a, b], compile3) => {
60302
60436
  if (a === null || b === null) throw new Error("Divide: missing argument");
60303
60437
  const ac = BaseCompiler.isComplexValued(a);
60304
60438
  const bc = BaseCompiler.isComplexValued(b);
60305
- if (!ac && !bc) return `(${compile3(a)} / ${compile3(b)})`;
60439
+ if (!ac && !bc) {
60440
+ const ca = tryGetConstant(a);
60441
+ const cb = tryGetConstant(b);
60442
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
60443
+ return String(ca / cb);
60444
+ if (cb === 1) return compile3(a);
60445
+ return `(${compile3(a)} / ${compile3(b)})`;
60446
+ }
60306
60447
  if (ac && bc) {
60307
60448
  return `(() => { const _a = ${compile3(a)}, _b = ${compile3(
60308
60449
  b
@@ -60319,13 +60460,26 @@ Error in definition of "${name}"`,
60319
60460
  },
60320
60461
  Negate: ([x], compile3) => {
60321
60462
  if (x === null) throw new Error("Negate: no argument");
60322
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile3(x)})`;
60463
+ if (!BaseCompiler.isComplexValued(x)) {
60464
+ const c = tryGetConstant(x);
60465
+ if (c !== void 0) return String(-c);
60466
+ return `(-${compile3(x)})`;
60467
+ }
60323
60468
  return `_SYS.cneg(${compile3(x)})`;
60324
60469
  },
60325
60470
  Multiply: (args, compile3) => {
60326
60471
  if (args.length === 1) return compile3(args[0]);
60327
60472
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60328
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" * ")})`;
60473
+ if (!anyComplex) {
60474
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
60475
+ const constants = args.map(tryGetConstant);
60476
+ if (constants.every((c) => c !== void 0))
60477
+ return String(constants.reduce((a, b) => a * b, 1));
60478
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
60479
+ if (nonOne.length === 0) return "1";
60480
+ if (nonOne.length === 1) return compile3(nonOne[0]);
60481
+ return `(${nonOne.map((x) => compile3(x)).join(" * ")})`;
60482
+ }
60329
60483
  if (args.length === 2) {
60330
60484
  const ac = BaseCompiler.isComplexValued(args[0]);
60331
60485
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -60410,20 +60564,30 @@ Error in definition of "${name}"`,
60410
60564
  AiryAi: "_SYS.airyAi",
60411
60565
  AiryBi: "_SYS.airyBi",
60412
60566
  // Combinatorics
60567
+ Mandelbrot: ([c, maxIter], compile3) => {
60568
+ if (c === null || maxIter === null)
60569
+ throw new Error("Mandelbrot: missing arguments");
60570
+ return `_SYS.mandelbrot(${compile3(c)}, ${compile3(maxIter)})`;
60571
+ },
60572
+ Julia: ([z, c, maxIter], compile3) => {
60573
+ if (z === null || c === null || maxIter === null)
60574
+ throw new Error("Julia: missing arguments");
60575
+ return `_SYS.julia(${compile3(z)}, ${compile3(c)}, ${compile3(maxIter)})`;
60576
+ },
60413
60577
  Binomial: (args, compile3) => `_SYS.binomial(${compile3(args[0])}, ${compile3(args[1])})`,
60414
60578
  Fibonacci: "_SYS.fibonacci",
60415
60579
  // Complex-specific functions
60416
- Re: (args, compile3) => {
60580
+ Real: (args, compile3) => {
60417
60581
  if (BaseCompiler.isComplexValued(args[0]))
60418
60582
  return `(${compile3(args[0])}).re`;
60419
60583
  return compile3(args[0]);
60420
60584
  },
60421
- Im: (args, compile3) => {
60585
+ Imaginary: (args, compile3) => {
60422
60586
  if (BaseCompiler.isComplexValued(args[0]))
60423
60587
  return `(${compile3(args[0])}).im`;
60424
60588
  return "0";
60425
60589
  },
60426
- Arg: (args, compile3) => {
60590
+ Argument: (args, compile3) => {
60427
60591
  if (BaseCompiler.isComplexValued(args[0]))
60428
60592
  return `_SYS.carg(${compile3(args[0])})`;
60429
60593
  return `(${compile3(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -60759,6 +60923,41 @@ Error in definition of "${name}"`,
60759
60923
  sinc,
60760
60924
  fresnelS,
60761
60925
  fresnelC,
60926
+ mandelbrot: (c, maxIter) => {
60927
+ let zx = 0, zy = 0;
60928
+ const cx = typeof c === "number" ? c : c.re;
60929
+ const cy = typeof c === "number" ? 0 : c.im;
60930
+ const n = Math.round(maxIter);
60931
+ for (let i = 0; i < n; i++) {
60932
+ const newZx = zx * zx - zy * zy + cx;
60933
+ zy = 2 * zx * zy + cy;
60934
+ zx = newZx;
60935
+ const mag2 = zx * zx + zy * zy;
60936
+ if (mag2 > 4) {
60937
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60938
+ return Math.max(0, Math.min(1, smooth));
60939
+ }
60940
+ }
60941
+ return 1;
60942
+ },
60943
+ julia: (z, c, maxIter) => {
60944
+ let zx = typeof z === "number" ? z : z.re;
60945
+ let zy = typeof z === "number" ? 0 : z.im;
60946
+ const cx = typeof c === "number" ? c : c.re;
60947
+ const cy = typeof c === "number" ? 0 : c.im;
60948
+ const n = Math.round(maxIter);
60949
+ for (let i = 0; i < n; i++) {
60950
+ const newZx = zx * zx - zy * zy + cx;
60951
+ zy = 2 * zx * zy + cy;
60952
+ zx = newZx;
60953
+ const mag2 = zx * zx + zy * zy;
60954
+ if (mag2 > 4) {
60955
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60956
+ return Math.max(0, Math.min(1, smooth));
60957
+ }
60958
+ }
60959
+ return 1;
60960
+ },
60762
60961
  binomial: choose,
60763
60962
  fibonacci,
60764
60963
  // Complex helpers
@@ -61104,6 +61303,7 @@ Error in definition of "${name}"`,
61104
61303
  Add: ["+", 11],
61105
61304
  Negate: ["-", 14],
61106
61305
  Subtract: ["-", 11],
61306
+ // Subtract canonicalizes to Add+Negate; kept as fallback
61107
61307
  Multiply: ["*", 12],
61108
61308
  Divide: ["/", 13],
61109
61309
  Equal: ["==", 8],
@@ -61119,6 +61319,12 @@ Error in definition of "${name}"`,
61119
61319
  function gpuVec2(target) {
61120
61320
  return target?.language === "wgsl" ? "vec2f" : "vec2";
61121
61321
  }
61322
+ function compileIntArg(expr2, compile3, target) {
61323
+ const c = tryGetConstant(expr2);
61324
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
61325
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
61326
+ return `${intCast}(${compile3(expr2)})`;
61327
+ }
61122
61328
  var GPU_UNROLL_LIMIT = 100;
61123
61329
  function compileGPUSumProduct(kind, args, _compile2, target) {
61124
61330
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -61181,88 +61387,113 @@ Error in definition of "${name}"`,
61181
61387
  if (args.length === 0) return "0.0";
61182
61388
  if (args.length === 1) return compile3(args[0]);
61183
61389
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61184
- if (!anyComplex) return args.map((x) => compile3(x)).join(" + ");
61185
- const v2 = gpuVec2(target);
61186
- return args.map((a) => {
61187
- const code = compile3(a);
61188
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61189
- }).join(" + ");
61190
- },
61191
- Multiply: (args, compile3, _target) => {
61192
- if (args.length === 0) return "1.0";
61193
- if (args.length === 1) return compile3(args[0]);
61194
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61195
- if (!anyComplex) return args.map((x) => compile3(x)).join(" * ");
61196
- let result = compile3(args[0]);
61197
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
61198
- for (let i = 1; i < args.length; i++) {
61199
- const code = compile3(args[i]);
61200
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
61201
- if (!resultIsComplex && !argIsComplex) {
61202
- result = `(${result} * ${code})`;
61203
- } else if (resultIsComplex && !argIsComplex) {
61204
- result = `(${code} * ${result})`;
61205
- } else if (!resultIsComplex && argIsComplex) {
61206
- result = `(${result} * ${code})`;
61207
- resultIsComplex = true;
61208
- } else {
61209
- result = `_gpu_cmul(${result}, ${code})`;
61210
- }
61390
+ if (!anyComplex) {
61391
+ return foldTerms(
61392
+ args.map((x) => compile3(x)),
61393
+ "0.0",
61394
+ "+"
61395
+ );
61211
61396
  }
61212
- return result;
61213
- },
61214
- Subtract: (args, compile3, target) => {
61215
- if (args.length === 0) return "0.0";
61397
+ const parts = args.map((a) => tryGetComplexParts(a, compile3));
61398
+ if (parts.some((p) => p === null)) {
61399
+ const v2 = gpuVec2(target);
61400
+ return args.map((a) => {
61401
+ const code = compile3(a);
61402
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61403
+ }).join(" + ");
61404
+ }
61405
+ const reParts = [];
61406
+ const imParts = [];
61407
+ for (const p of parts) {
61408
+ if (p.re !== null) reParts.push(p.re);
61409
+ if (p.im !== null) imParts.push(p.im);
61410
+ }
61411
+ const reSum = foldTerms(reParts, "0.0", "+");
61412
+ const imSum = foldTerms(imParts, "0.0", "+");
61413
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
61414
+ },
61415
+ Multiply: (args, compile3, target) => {
61416
+ if (args.length === 0) return "1.0";
61216
61417
  if (args.length === 1) return compile3(args[0]);
61217
61418
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61218
61419
  if (!anyComplex) {
61219
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
61220
- let result2 = compile3(args[0]);
61221
- for (let i = 1; i < args.length; i++) {
61222
- result2 = `${result2} - ${compile3(args[i])}`;
61223
- }
61224
- return result2;
61225
- }
61226
- const v2 = gpuVec2(target);
61227
- const promote = (a) => {
61228
- const code = compile3(a);
61229
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61230
- };
61231
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
61232
- let result = promote(args[0]);
61233
- for (let i = 1; i < args.length; i++) {
61234
- result = `${result} - ${promote(args[i])}`;
61420
+ return foldTerms(
61421
+ args.map((x) => compile3(x)),
61422
+ "1.0",
61423
+ "*"
61424
+ );
61235
61425
  }
61426
+ const iIndex = args.findIndex(
61427
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
61428
+ );
61429
+ if (iIndex >= 0) {
61430
+ const iFactor = args[iIndex];
61431
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
61432
+ const realFactors = args.filter((_, i) => i !== iIndex);
61433
+ const v2 = gpuVec2(target);
61434
+ if (realFactors.length === 0)
61435
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
61436
+ const factors = realFactors.map((f) => compile3(f));
61437
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
61438
+ const imCode = foldTerms(factors, "1.0", "*");
61439
+ return `${v2}(0.0, ${imCode})`;
61440
+ }
61441
+ const realCodes = [];
61442
+ const complexCodes = [];
61443
+ for (const a of args) {
61444
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile3(a));
61445
+ else realCodes.push(compile3(a));
61446
+ }
61447
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
61448
+ let result = complexCodes[0];
61449
+ for (let i = 1; i < complexCodes.length; i++) {
61450
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
61451
+ }
61452
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
61236
61453
  return result;
61237
61454
  },
61455
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
61456
+ // The operator entry in GPU_OPERATORS handles any edge cases.
61238
61457
  Divide: (args, compile3, target) => {
61239
61458
  if (args.length === 0) return "1.0";
61240
61459
  if (args.length === 1) return compile3(args[0]);
61241
61460
  const ac = BaseCompiler.isComplexValued(args[0]);
61242
61461
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
61243
61462
  if (!ac && !bc) {
61244
- if (args.length === 2) return `${compile3(args[0])} / ${compile3(args[1])}`;
61463
+ if (args.length === 2) {
61464
+ const a = tryGetConstant(args[0]);
61465
+ const b = tryGetConstant(args[1]);
61466
+ if (a !== void 0 && b !== void 0 && b !== 0)
61467
+ return formatFloat(a / b);
61468
+ if (b === 1) return compile3(args[0]);
61469
+ return `${compile3(args[0])} / ${compile3(args[1])}`;
61470
+ }
61245
61471
  let result = compile3(args[0]);
61246
- for (let i = 1; i < args.length; i++) {
61472
+ for (let i = 1; i < args.length; i++)
61247
61473
  result = `${result} / ${compile3(args[i])}`;
61248
- }
61249
61474
  return result;
61250
61475
  }
61251
61476
  if (ac && bc) return `_gpu_cdiv(${compile3(args[0])}, ${compile3(args[1])})`;
61252
- if (ac && !bc) {
61253
- return `(${compile3(args[0])} / ${compile3(args[1])})`;
61254
- }
61477
+ if (ac && !bc) return `(${compile3(args[0])} / ${compile3(args[1])})`;
61255
61478
  const v2 = gpuVec2(target);
61256
61479
  return `_gpu_cdiv(${v2}(${compile3(args[0])}, 0.0), ${compile3(args[1])})`;
61257
61480
  },
61258
- Negate: ([x], compile3) => {
61481
+ Negate: ([x], compile3, target) => {
61259
61482
  if (x === null) throw new Error("Negate: no argument");
61483
+ const c = tryGetConstant(x);
61484
+ if (c !== void 0) return formatFloat(-c);
61485
+ if (isNumber(x) && x.im !== 0) {
61486
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
61487
+ }
61488
+ if (isSymbol2(x, "ImaginaryUnit"))
61489
+ return `${gpuVec2(target)}(0.0, -1.0)`;
61260
61490
  return `(-${compile3(x)})`;
61261
61491
  },
61262
61492
  // Standard math functions with complex dispatch
61263
61493
  Abs: (args, compile3) => {
61264
61494
  if (BaseCompiler.isComplexValued(args[0]))
61265
61495
  return `length(${compile3(args[0])})`;
61496
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
61266
61497
  return `abs(${compile3(args[0])})`;
61267
61498
  },
61268
61499
  Arccos: (args, compile3) => {
@@ -61280,7 +61511,10 @@ Error in definition of "${name}"`,
61280
61511
  return `_gpu_catan(${compile3(args[0])})`;
61281
61512
  return `atan(${compile3(args[0])})`;
61282
61513
  },
61283
- Ceil: "ceil",
61514
+ Ceil: (args, compile3) => {
61515
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61516
+ return `ceil(${compile3(args[0])})`;
61517
+ },
61284
61518
  Clamp: "clamp",
61285
61519
  Cos: (args, compile3) => {
61286
61520
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61294,7 +61528,10 @@ Error in definition of "${name}"`,
61294
61528
  return `exp(${compile3(args[0])})`;
61295
61529
  },
61296
61530
  Exp2: "exp2",
61297
- Floor: "floor",
61531
+ Floor: (args, compile3) => {
61532
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61533
+ return `floor(${compile3(args[0])})`;
61534
+ },
61298
61535
  Fract: "fract",
61299
61536
  Ln: (args, compile3) => {
61300
61537
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61316,10 +61553,25 @@ Error in definition of "${name}"`,
61316
61553
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile3(exp3) : `${v2}(${compile3(exp3)}, 0.0)`;
61317
61554
  return `_gpu_cpow(${bCode}, ${eCode})`;
61318
61555
  }
61556
+ const bConst = tryGetConstant(base);
61557
+ const eConst = tryGetConstant(exp3);
61558
+ if (bConst !== void 0 && eConst !== void 0)
61559
+ return formatFloat(Math.pow(bConst, eConst));
61560
+ if (eConst === 0) return "1.0";
61561
+ if (eConst === 1) return compile3(base);
61562
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
61563
+ const code = compile3(base);
61564
+ return `(${code} * ${code})`;
61565
+ }
61566
+ if (eConst === -1) return `(1.0 / ${compile3(base)})`;
61567
+ if (eConst === 0.5) return `sqrt(${compile3(base)})`;
61319
61568
  return `pow(${compile3(base)}, ${compile3(exp3)})`;
61320
61569
  },
61321
61570
  Radians: "radians",
61322
- Round: "round",
61571
+ Round: (args, compile3) => {
61572
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61573
+ return `round(${compile3(args[0])})`;
61574
+ },
61323
61575
  Sign: "sign",
61324
61576
  Sin: (args, compile3) => {
61325
61577
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61330,6 +61582,8 @@ Error in definition of "${name}"`,
61330
61582
  Sqrt: (args, compile3) => {
61331
61583
  if (BaseCompiler.isComplexValued(args[0]))
61332
61584
  return `_gpu_csqrt(${compile3(args[0])})`;
61585
+ const c = tryGetConstant(args[0]);
61586
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
61333
61587
  return `sqrt(${compile3(args[0])})`;
61334
61588
  },
61335
61589
  Step: "step",
@@ -61338,17 +61592,20 @@ Error in definition of "${name}"`,
61338
61592
  return `_gpu_ctan(${compile3(args[0])})`;
61339
61593
  return `tan(${compile3(args[0])})`;
61340
61594
  },
61341
- Truncate: "trunc",
61595
+ Truncate: (args, compile3) => {
61596
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61597
+ return `trunc(${compile3(args[0])})`;
61598
+ },
61342
61599
  // Complex-specific functions
61343
- Re: (args, compile3) => {
61600
+ Real: (args, compile3) => {
61344
61601
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).x`;
61345
61602
  return compile3(args[0]);
61346
61603
  },
61347
- Im: (args, compile3) => {
61604
+ Imaginary: (args, compile3) => {
61348
61605
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).y`;
61349
61606
  return "0.0";
61350
61607
  },
61351
- Arg: (args, compile3) => {
61608
+ Argument: (args, compile3) => {
61352
61609
  if (BaseCompiler.isComplexValued(args[0])) {
61353
61610
  const code = compile3(args[0]);
61354
61611
  return `atan(${code}.y, ${code}.x)`;
@@ -61563,13 +61820,20 @@ Error in definition of "${name}"`,
61563
61820
  },
61564
61821
  Square: ([x], compile3) => {
61565
61822
  if (x === null) throw new Error("Square: no argument");
61566
- const arg = compile3(x);
61567
- return `(${arg} * ${arg})`;
61823
+ if (isSymbol2(x) || isNumber(x)) {
61824
+ const arg = compile3(x);
61825
+ return `(${arg} * ${arg})`;
61826
+ }
61827
+ return `pow(${compile3(x)}, 2.0)`;
61568
61828
  },
61569
61829
  Root: ([x, n], compile3) => {
61570
61830
  if (x === null) throw new Error("Root: no argument");
61571
61831
  if (n === null || n === void 0) return `sqrt(${compile3(x)})`;
61572
- if (n?.re === 2) return `sqrt(${compile3(x)})`;
61832
+ const nConst = tryGetConstant(n);
61833
+ if (nConst === 2) return `sqrt(${compile3(x)})`;
61834
+ const xConst = tryGetConstant(x);
61835
+ if (xConst !== void 0 && nConst !== void 0)
61836
+ return formatFloat(Math.pow(xConst, 1 / nConst));
61573
61837
  return `pow(${compile3(x)}, 1.0 / ${compile3(n)})`;
61574
61838
  },
61575
61839
  // Color functions (pure-math, GPU-compilable)
@@ -61611,18 +61875,14 @@ Error in definition of "${name}"`,
61611
61875
  Mandelbrot: ([c, maxIter], compile3, target) => {
61612
61876
  if (c === null || maxIter === null)
61613
61877
  throw new Error("Mandelbrot: missing arguments");
61614
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61615
- return `_fractal_mandelbrot(${compile3(c)}, ${intCast}(${compile3(
61616
- maxIter
61617
- )}))`;
61878
+ const iterCode = compileIntArg(maxIter, compile3, target);
61879
+ return `_fractal_mandelbrot(${compile3(c)}, ${iterCode})`;
61618
61880
  },
61619
61881
  Julia: ([z, c, maxIter], compile3, target) => {
61620
61882
  if (z === null || c === null || maxIter === null)
61621
61883
  throw new Error("Julia: missing arguments");
61622
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61623
- return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${intCast}(${compile3(
61624
- maxIter
61625
- )}))`;
61884
+ const iterCode = compileIntArg(maxIter, compile3, target);
61885
+ return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${iterCode})`;
61626
61886
  },
61627
61887
  // Vector/Matrix operations
61628
61888
  Cross: "cross",
@@ -64254,6 +64514,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64254
64514
  Add: ["_IA.add", 20],
64255
64515
  Negate: ["_IA.negate", 20],
64256
64516
  Subtract: ["_IA.sub", 20],
64517
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64257
64518
  Multiply: ["_IA.mul", 20],
64258
64519
  Divide: ["_IA.div", 20],
64259
64520
  // Comparisons return BoolInterval
@@ -64278,17 +64539,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64278
64539
  }
64279
64540
  return result;
64280
64541
  },
64281
- Subtract: (args, compile3) => {
64282
- if (args.length === 0) return "_IA.point(0)";
64283
- if (args.length === 1) return `_IA.negate(${compile3(args[0])})`;
64284
- if (args.length === 2)
64285
- return `_IA.sub(${compile3(args[0])}, ${compile3(args[1])})`;
64286
- let result = compile3(args[0]);
64287
- for (let i = 1; i < args.length; i++) {
64288
- result = `_IA.sub(${result}, ${compile3(args[i])})`;
64289
- }
64290
- return result;
64291
- },
64542
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
64292
64543
  Multiply: (args, compile3) => {
64293
64544
  if (args.length === 0) return "_IA.point(1)";
64294
64545
  if (args.length === 1) return compile3(args[0]);
@@ -65224,6 +65475,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
65224
65475
  Negate: ["-", 14],
65225
65476
  // Unary operator
65226
65477
  Subtract: ["-", 11],
65478
+ // Subtract canonicalizes to Add+Negate; kept as fallback
65227
65479
  Multiply: ["*", 12],
65228
65480
  Divide: ["/", 13],
65229
65481
  Power: ["**", 15],
@@ -65251,16 +65503,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
65251
65503
  if (args.length === 1) return compile3(args[0]);
65252
65504
  return args.map((x) => compile3(x)).join(" * ");
65253
65505
  },
65254
- Subtract: (args, compile3) => {
65255
- if (args.length === 0) return "0";
65256
- if (args.length === 1) return compile3(args[0]);
65257
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
65258
- let result = compile3(args[0]);
65259
- for (let i = 1; i < args.length; i++) {
65260
- result = `${result} - ${compile3(args[i])}`;
65261
- }
65262
- return result;
65263
- },
65506
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
65264
65507
  Divide: (args, compile3) => {
65265
65508
  if (args.length === 0) return "1";
65266
65509
  if (args.length === 1) return compile3(args[0]);
@@ -67692,7 +67935,9 @@ ${code}`;
67692
67935
  }
67693
67936
  return new ExactNumericValue(value, makeNumericValue);
67694
67937
  }
67695
- throw Error("Unexpected value");
67938
+ throw Error(
67939
+ `Unexpected value: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`
67940
+ );
67696
67941
  }
67697
67942
  /**
67698
67943
  * The cost function is used to determine the "cost" of an expression. For example, when simplifying an expression, the simplification that results in the lowest cost is chosen.
@@ -68163,14 +68408,14 @@ ${code}`;
68163
68408
  _setDefaultEngineFactory(() => new ComputeEngine());
68164
68409
 
68165
68410
  // src/compute-engine.ts
68166
- var version = "0.55.2";
68411
+ var version = "0.55.3";
68167
68412
  ComputeEngine._latexSyntaxFactory = () => new LatexSyntax();
68168
68413
  _setDefaultEngineFactory(
68169
68414
  () => new ComputeEngine({ latexSyntax: new LatexSyntax() })
68170
68415
  );
68171
68416
  globalThis[/* @__PURE__ */ Symbol.for("io.cortexjs.compute-engine")] = {
68172
68417
  ComputeEngine: ComputeEngine.prototype.constructor,
68173
- version: "0.55.2"
68418
+ version: "0.55.3"
68174
68419
  };
68175
68420
  return __toCommonJS(compute_engine_exports);
68176
68421
  })();