@cortex-js/compute-engine 0.55.2 → 0.55.4

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 (258) hide show
  1. package/dist/compile.esm.js +908 -176
  2. package/dist/compile.min.esm.js +441 -71
  3. package/dist/compile.min.umd.cjs +442 -72
  4. package/dist/compile.umd.cjs +908 -176
  5. package/dist/compute-engine.esm.js +954 -185
  6. package/dist/compute-engine.min.esm.js +444 -74
  7. package/dist/compute-engine.min.umd.cjs +444 -74
  8. package/dist/compute-engine.umd.cjs +954 -185
  9. package/dist/core.esm.js +951 -174
  10. package/dist/core.min.esm.js +442 -72
  11. package/dist/core.min.umd.cjs +442 -72
  12. package/dist/core.umd.cjs +951 -174
  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 +43 -13
  18. package/dist/latex-syntax.min.esm.js +5 -5
  19. package/dist/latex-syntax.min.umd.cjs +5 -5
  20. package/dist/latex-syntax.umd.cjs +43 -13
  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/fractal-orbit.d.ts +13 -0
  108. package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
  109. package/dist/types/compute-engine/compilation/gpu-target.d.ts +47 -1
  110. package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +1 -1
  111. package/dist/types/compute-engine/compilation/javascript-target.d.ts +23 -1
  112. package/dist/types/compute-engine/compilation/python-target.d.ts +1 -1
  113. package/dist/types/compute-engine/compilation/types.d.ts +48 -1
  114. package/dist/types/compute-engine/compilation/wgsl-target.d.ts +1 -1
  115. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  116. package/dist/types/compute-engine/engine-assumptions.d.ts +1 -1
  117. package/dist/types/compute-engine/engine-cache.d.ts +1 -1
  118. package/dist/types/compute-engine/engine-common-symbols.d.ts +1 -1
  119. package/dist/types/compute-engine/engine-compilation-targets.d.ts +1 -1
  120. package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +1 -1
  121. package/dist/types/compute-engine/engine-declarations.d.ts +1 -1
  122. package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +1 -1
  123. package/dist/types/compute-engine/engine-extension-contracts.d.ts +1 -1
  124. package/dist/types/compute-engine/engine-library-bootstrap.d.ts +1 -1
  125. package/dist/types/compute-engine/engine-numeric-configuration.d.ts +1 -1
  126. package/dist/types/compute-engine/engine-runtime-state.d.ts +1 -1
  127. package/dist/types/compute-engine/engine-scope.d.ts +1 -1
  128. package/dist/types/compute-engine/engine-sequences.d.ts +1 -1
  129. package/dist/types/compute-engine/engine-simplification-rules.d.ts +1 -1
  130. package/dist/types/compute-engine/engine-startup-coordinator.d.ts +1 -1
  131. package/dist/types/compute-engine/engine-type-resolver.d.ts +1 -1
  132. package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +1 -1
  133. package/dist/types/compute-engine/free-functions.d.ts +1 -1
  134. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  135. package/dist/types/compute-engine/global-types.d.ts +1 -1
  136. package/dist/types/compute-engine/index.d.ts +1 -1
  137. package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
  138. package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
  139. package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
  140. package/dist/types/compute-engine/interval/index.d.ts +1 -1
  141. package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
  142. package/dist/types/compute-engine/interval/types.d.ts +1 -1
  143. package/dist/types/compute-engine/interval/util.d.ts +1 -1
  144. package/dist/types/compute-engine/latex-syntax/dictionary/default-dictionary.d.ts +1 -1
  145. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  146. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  147. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  148. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  149. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  150. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  151. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  152. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  153. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  154. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  155. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  156. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  157. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  158. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-units.d.ts +1 -1
  159. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  160. package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +1 -1
  161. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  162. package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +1 -1
  163. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
  164. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  165. package/dist/types/compute-engine/latex-syntax/serialize-dms.d.ts +1 -1
  166. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  167. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  168. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  169. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  170. package/dist/types/compute-engine/latex-syntax/types.d.ts +1 -1
  171. package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
  172. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  173. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  174. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  175. package/dist/types/compute-engine/library/colors.d.ts +1 -1
  176. package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
  177. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  178. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  179. package/dist/types/compute-engine/library/core.d.ts +1 -1
  180. package/dist/types/compute-engine/library/fractals.d.ts +1 -1
  181. package/dist/types/compute-engine/library/library.d.ts +1 -1
  182. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  183. package/dist/types/compute-engine/library/logic-analysis.d.ts +1 -1
  184. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  185. package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
  186. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  187. package/dist/types/compute-engine/library/quantity-arithmetic.d.ts +1 -1
  188. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  189. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  190. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  191. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  192. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  193. package/dist/types/compute-engine/library/type-handlers.d.ts +1 -1
  194. package/dist/types/compute-engine/library/unit-data.d.ts +1 -1
  195. package/dist/types/compute-engine/library/units.d.ts +1 -1
  196. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  197. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  198. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  199. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  200. package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
  201. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  202. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  203. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  204. package/dist/types/compute-engine/numerics/linear-algebra.d.ts +1 -1
  205. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  206. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  207. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  208. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  209. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  210. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  211. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  212. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  213. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  214. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  215. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  216. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  217. package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
  218. package/dist/types/compute-engine/oeis.d.ts +1 -1
  219. package/dist/types/compute-engine/sequence.d.ts +1 -1
  220. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  221. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  222. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  223. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  224. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  225. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  226. package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
  227. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  228. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  229. package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
  230. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  231. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  232. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  233. package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
  234. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  235. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  236. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  237. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  238. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  239. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  240. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  241. package/dist/types/compute-engine/types-definitions.d.ts +1 -1
  242. package/dist/types/compute-engine/types-engine.d.ts +1 -1
  243. package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
  244. package/dist/types/compute-engine/types-expression.d.ts +1 -1
  245. package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
  246. package/dist/types/compute-engine/types-kernel-serialization.d.ts +1 -1
  247. package/dist/types/compute-engine/types-serialization.d.ts +1 -1
  248. package/dist/types/compute-engine/types.d.ts +1 -1
  249. package/dist/types/compute-engine.d.ts +1 -1
  250. package/dist/types/core.d.ts +1 -1
  251. package/dist/types/interval.d.ts +1 -1
  252. package/dist/types/latex-syntax.d.ts +2 -2
  253. package/dist/types/math-json/symbols.d.ts +1 -1
  254. package/dist/types/math-json/types.d.ts +1 -1
  255. package/dist/types/math-json/utils.d.ts +1 -1
  256. package/dist/types/math-json.d.ts +2 -2
  257. package/dist/types/numerics.d.ts +1 -1
  258. package/package.json +3 -3
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.55.2 */
1
+ /** Compute Engine 0.55.4 */
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;
@@ -17227,6 +17227,10 @@ var ComputeEngine = (() => {
17227
17227
  parser.nextToken();
17228
17228
  continue;
17229
17229
  }
17230
+ if (EMOJIS.test(id + token)) {
17231
+ id += parser.nextToken();
17232
+ continue;
17233
+ }
17230
17234
  const next = parseSymbolToken(parser, { toplevel: false });
17231
17235
  if (next === null) return null;
17232
17236
  id += next;
@@ -17266,6 +17270,7 @@ var ComputeEngine = (() => {
17266
17270
  function matchPrefixedSymbol(parser) {
17267
17271
  const prefix = SYMBOL_PREFIX[parser.peek] ?? null;
17268
17272
  if (prefix === null) return null;
17273
+ const start = parser.index;
17269
17274
  parser.nextToken();
17270
17275
  if (parser.match("<{>")) {
17271
17276
  let body = "";
@@ -17286,17 +17291,48 @@ var ComputeEngine = (() => {
17286
17291
  parser.nextToken();
17287
17292
  }
17288
17293
  body += parseSymbolBody(parser);
17289
- if (body === null || !parser.match("<}>")) return null;
17294
+ if (body === null || !parser.match("<}>")) {
17295
+ parser.index = start;
17296
+ return null;
17297
+ }
17290
17298
  if (prefix === "_upright" && body.length > 1) return body;
17291
17299
  return body + prefix;
17292
17300
  }
17301
+ parser.index = start;
17293
17302
  return null;
17294
17303
  }
17295
17304
  function parseInvalidSymbol(parser) {
17296
17305
  const start = parser.index;
17297
17306
  const id = matchPrefixedSymbol(parser);
17298
- if (id === null || isValidSymbol(id)) return null;
17299
- return parser.error(["invalid-symbol", { str: validateSymbol(id) }], start);
17307
+ if (id !== null) {
17308
+ if (isValidSymbol(id)) return null;
17309
+ return parser.error(["invalid-symbol", { str: validateSymbol(id) }], start);
17310
+ }
17311
+ parser.index = start;
17312
+ const prefix = SYMBOL_PREFIX[parser.peek] ?? null;
17313
+ if (prefix === null) return null;
17314
+ parser.nextToken();
17315
+ if (!parser.match("<{>")) {
17316
+ parser.index = start;
17317
+ return null;
17318
+ }
17319
+ const bodyStart = parser.index;
17320
+ let level = 0;
17321
+ while (!parser.atEnd && !(level === 0 && parser.peek === "<}>")) {
17322
+ if (parser.peek === "<{>") level += 1;
17323
+ if (parser.peek === "<}>") level -= 1;
17324
+ parser.nextToken();
17325
+ }
17326
+ const bodyText = parser.latex(bodyStart, parser.index);
17327
+ if (isValidSymbol(bodyText)) {
17328
+ parser.index = start;
17329
+ return null;
17330
+ }
17331
+ parser.match("<}>");
17332
+ return parser.error(
17333
+ ["invalid-symbol", { str: validateSymbol(bodyText) }],
17334
+ start
17335
+ );
17300
17336
  }
17301
17337
  function parseSymbol(parser) {
17302
17338
  if (/^[a-zA-Z]$/.test(parser.peek) || /^\p{XIDS}$/u.test(parser.peek)) {
@@ -18251,18 +18287,12 @@ var ComputeEngine = (() => {
18251
18287
  this.index = start;
18252
18288
  return false;
18253
18289
  }
18254
- const matchedToken = this.nextToken();
18255
- const useLatexCommand = matchedToken.startsWith("\\");
18290
+ this.nextToken();
18256
18291
  if (hasBracedDelimiter && !this.match("<}>")) {
18257
18292
  this.index = start;
18258
18293
  return false;
18259
18294
  }
18260
- const closeTokens = DELIMITER_SHORTHAND2[close[0]] ?? [
18261
- close[0]
18262
- ];
18263
- const closeToken = closeTokens.find(
18264
- (t) => useLatexCommand ? t.startsWith("\\") : !t.startsWith("\\")
18265
- ) ?? closeTokens[0];
18295
+ const closeToken = close[0];
18266
18296
  const closeBoundary3 = closePrefix ? hasBracedDelimiter ? [closePrefix, "<{>", closeToken, "<}>"] : [closePrefix, closeToken] : [closeToken];
18267
18297
  this.addBoundary(closeBoundary3);
18268
18298
  return true;
@@ -30759,8 +30789,7 @@ ${lines.join("\n")}`;
30759
30789
  broadcastable: true,
30760
30790
  idempotent: true,
30761
30791
  complexity: 1200,
30762
- signature: "(number) -> number",
30763
- type: ([x]) => x.type,
30792
+ signature: "(number) -> real",
30764
30793
  sgn: ([x]) => {
30765
30794
  if (x.isSame(0)) return "zero";
30766
30795
  if (isNumber(x)) return "positive";
@@ -39658,30 +39687,6 @@ ${e.message}
39658
39687
  "#ae2036",
39659
39688
  "#b40426"
39660
39689
  ];
39661
- var OCEAN_BALANCE = [
39662
- "#00441b",
39663
- "#006d5b",
39664
- "#1a8c7d",
39665
- "#2fa394",
39666
- "#4fb3a3",
39667
- "#6fc1b3",
39668
- "#8dcfc3",
39669
- "#a6dbd1",
39670
- "#bfe6de",
39671
- "#d7f0ea",
39672
- "#f7f7f7",
39673
- "#eeeeee",
39674
- "#ddd8e6",
39675
- "#c7bcda",
39676
- "#b3a0d0",
39677
- "#9f86c7",
39678
- "#8d6dbd",
39679
- "#7b56b1",
39680
- "#6a42a3",
39681
- "#5a3093",
39682
- "#4a1f82",
39683
- "#3b0f70"
39684
- ];
39685
39690
  var reversePalette = (palette) => palette.slice().reverse();
39686
39691
  var DIVERGING_PALETTES = {
39687
39692
  roma: ROMA,
@@ -39693,9 +39698,7 @@ ${e.message}
39693
39698
  rdbu: RDBU,
39694
39699
  "rdbu-reversed": reversePalette(RDBU),
39695
39700
  coolwarm: COOLWARM,
39696
- "coolwarm-reversed": reversePalette(COOLWARM),
39697
- "ocean-balance": OCEAN_BALANCE,
39698
- "ocean-balance-reversed": reversePalette(OCEAN_BALANCE)
39701
+ "coolwarm-reversed": reversePalette(COOLWARM)
39699
39702
  };
39700
39703
  var TURBO = [
39701
39704
  "#30123b",
@@ -41779,6 +41782,40 @@ ${e.message}
41779
41782
  "#eeeeee",
41780
41783
  "#ffffff"
41781
41784
  ];
41785
+ var CMOCEAN_PHASE = [
41786
+ "#a8780d",
41787
+ "#b3701b",
41788
+ "#be6828",
41789
+ "#c75f35",
41790
+ "#cf5643",
41791
+ "#d54b53",
41792
+ "#db4066",
41793
+ "#de357b",
41794
+ "#df2a93",
41795
+ "#dc25ad",
41796
+ "#d529c4",
41797
+ "#cc34d7",
41798
+ "#c041e5",
41799
+ "#b24fef",
41800
+ "#a25cf3",
41801
+ "#9168f4",
41802
+ "#7d73f0",
41803
+ "#687ce8",
41804
+ "#5285dc",
41805
+ "#3d8bcd",
41806
+ "#2c90bc",
41807
+ "#2093ac",
41808
+ "#19959c",
41809
+ "#12978c",
41810
+ "#0c987c",
41811
+ "#119a69",
41812
+ "#249a52",
41813
+ "#409839",
41814
+ "#5e9420",
41815
+ "#778d12",
41816
+ "#8b860d",
41817
+ "#9b7f0d"
41818
+ ];
41782
41819
  var reversePalette2 = (palette) => palette.slice().reverse();
41783
41820
  var SEQUENTIAL_PALETTES = {
41784
41821
  turbo: TURBO,
@@ -41802,7 +41839,9 @@ ${e.message}
41802
41839
  "rocket-reversed": reversePalette2(ROCKET),
41803
41840
  mako: MAKO,
41804
41841
  // blue to teal
41805
- "mako-reversed": reversePalette2(MAKO)
41842
+ "mako-reversed": reversePalette2(MAKO),
41843
+ "cmocean-phase": CMOCEAN_PHASE,
41844
+ "cmocean-phase-reversed": reversePalette2(CMOCEAN_PHASE)
41806
41845
  };
41807
41846
 
41808
41847
  // src/compute-engine/library/colors.ts
@@ -54311,8 +54350,11 @@ Error in definition of "${name}"`,
54311
54350
  for (const local of locals) {
54312
54351
  for (const arg of args) {
54313
54352
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
54314
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
54353
+ const rhs = arg.ops[1];
54354
+ if (_BaseCompiler.isComplexValued(rhs)) {
54315
54355
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
54356
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
54357
+ typeHints[local] = isWGSL ? "i32" : "int";
54316
54358
  }
54317
54359
  break;
54318
54360
  }
@@ -54517,12 +54559,9 @@ Error in definition of "${name}"`,
54517
54559
  /**
54518
54560
  * Determine at compile time whether an expression produces a complex value.
54519
54561
  *
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.
54562
+ * Uses the expression's declared type (from operator signatures) when
54563
+ * available. Falls back to operand inspection for functions whose
54564
+ * return type is unknown.
54526
54565
  */
54527
54566
  static isComplexValued(expr2) {
54528
54567
  if (isNumber(expr2)) return expr2.im !== 0;
@@ -54533,13 +54572,24 @@ Error in definition of "${name}"`,
54533
54572
  return t.matches("complex") && !t.matches("real");
54534
54573
  }
54535
54574
  if (isFunction2(expr2)) {
54536
- const op = expr2.operator;
54537
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
54538
- return false;
54575
+ const t = expr2.type;
54576
+ if (t.matches("complex") && !t.matches("real")) return true;
54577
+ if (t.matches("real")) return false;
54539
54578
  return expr2.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
54540
54579
  }
54541
54580
  return false;
54542
54581
  }
54582
+ /** True if the expression is provably integer-typed. */
54583
+ static isIntegerValued(expr2) {
54584
+ if (isNumber(expr2)) return expr2.im === 0 && Number.isInteger(expr2.re);
54585
+ const t = expr2.type;
54586
+ return t ? t.matches("integer") : false;
54587
+ }
54588
+ /** True if the expression is provably non-negative (sign ≥ 0). */
54589
+ static isNonNegative(expr2) {
54590
+ if (isNumber(expr2)) return expr2.im === 0 && expr2.re >= 0;
54591
+ return expr2.isNonNegative === true;
54592
+ }
54543
54593
  /**
54544
54594
  * Generate a temporary variable name
54545
54595
  */
@@ -54793,14 +54843,27 @@ Error in definition of "${name}"`,
54793
54843
  } catch (e) {
54794
54844
  if (options?.fallback ?? true) {
54795
54845
  console.warn(
54796
- `Compilation fallback for "${expr2.operator}": ${e.message}`
54846
+ `Compilation fallback for "${expr2.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
54797
54847
  );
54848
+ const ce = expr2.engine;
54849
+ const fallbackRun = ((vars) => {
54850
+ ce.pushScope();
54851
+ try {
54852
+ if (vars && typeof vars === "object") {
54853
+ for (const [k, v] of Object.entries(vars))
54854
+ ce.assign(k, v);
54855
+ }
54856
+ return expr2.evaluate().re;
54857
+ } finally {
54858
+ ce.popScope();
54859
+ }
54860
+ });
54798
54861
  return {
54799
54862
  target: options?.to ?? "javascript",
54800
54863
  success: false,
54801
54864
  code: "",
54802
54865
  calling: "expression",
54803
- run: applicableN1(expr2)
54866
+ run: fallbackRun
54804
54867
  };
54805
54868
  }
54806
54869
  throw e;
@@ -59950,6 +60013,86 @@ Error in definition of "${name}"`,
59950
60013
  for (const symbol2 of Object.values(commonSymbols)) symbol2?.reset();
59951
60014
  }
59952
60015
 
60016
+ // src/compute-engine/compilation/constant-folding.ts
60017
+ function formatFloat(n) {
60018
+ const str = n.toString();
60019
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
60020
+ return `${str}.0`;
60021
+ }
60022
+ return str;
60023
+ }
60024
+ function tryGetConstant(expr2) {
60025
+ if (!isNumber(expr2)) return void 0;
60026
+ if (expr2.im !== 0) return void 0;
60027
+ const re = expr2.re;
60028
+ if (!isFinite(re)) return void 0;
60029
+ return re;
60030
+ }
60031
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
60032
+ function foldTerms(terms, identity, op) {
60033
+ const identityValue = op === "+" ? 0 : 1;
60034
+ let numericAcc = null;
60035
+ const symbolic = [];
60036
+ for (const term of terms) {
60037
+ if (NUMERIC_LITERAL_RE.test(term)) {
60038
+ const val = parseFloat(term);
60039
+ if (op === "*" && val === 0) return "0.0";
60040
+ if (numericAcc === null) {
60041
+ numericAcc = val;
60042
+ } else {
60043
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
60044
+ }
60045
+ } else {
60046
+ symbolic.push(term);
60047
+ }
60048
+ }
60049
+ if (numericAcc !== null && numericAcc !== identityValue) {
60050
+ symbolic.unshift(formatFloat(numericAcc));
60051
+ }
60052
+ if (symbolic.length === 0) {
60053
+ if (numericAcc !== null) return formatFloat(numericAcc);
60054
+ return identity;
60055
+ }
60056
+ if (symbolic.length === 1) return symbolic[0];
60057
+ return symbolic.join(op === "+" ? " + " : " * ");
60058
+ }
60059
+ function tryGetComplexParts(expr2, compile3) {
60060
+ if (isSymbol2(expr2, "ImaginaryUnit")) {
60061
+ return { re: null, im: "1.0" };
60062
+ }
60063
+ if (isNumber(expr2) && expr2.im !== 0) {
60064
+ const re = expr2.re;
60065
+ const im = expr2.im;
60066
+ return {
60067
+ re: re !== 0 ? formatFloat(re) : null,
60068
+ im: formatFloat(im)
60069
+ };
60070
+ }
60071
+ if (isFunction2(expr2, "Multiply")) {
60072
+ const ops = expr2.ops;
60073
+ const iIndex = ops.findIndex(
60074
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
60075
+ );
60076
+ if (iIndex >= 0) {
60077
+ const iFactor = ops[iIndex];
60078
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
60079
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
60080
+ if (remaining.length === 0) {
60081
+ return { re: null, im: formatFloat(iScale) };
60082
+ }
60083
+ const compiledFactors = remaining.map((r) => compile3(r));
60084
+ if (iScale !== 1)
60085
+ compiledFactors.unshift(formatFloat(iScale));
60086
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
60087
+ return { re: null, im: imCode };
60088
+ }
60089
+ }
60090
+ if (BaseCompiler.isComplexValued(expr2)) {
60091
+ return null;
60092
+ }
60093
+ return { re: compile3(expr2), im: null };
60094
+ }
60095
+
59953
60096
  // src/compute-engine/compilation/javascript-target.ts
59954
60097
  var JAVASCRIPT_OPERATORS = {
59955
60098
  Add: ["+", 11],
@@ -59973,12 +60116,21 @@ Error in definition of "${name}"`,
59973
60116
  Abs: (args, compile3) => {
59974
60117
  if (BaseCompiler.isComplexValued(args[0]))
59975
60118
  return `_SYS.cabs(${compile3(args[0])})`;
60119
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
59976
60120
  return `Math.abs(${compile3(args[0])})`;
59977
60121
  },
59978
60122
  Add: (args, compile3) => {
59979
60123
  if (args.length === 1) return compile3(args[0]);
59980
60124
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
59981
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" + ")})`;
60125
+ if (!anyComplex) {
60126
+ const constants = args.map(tryGetConstant);
60127
+ if (constants.every((c) => c !== void 0))
60128
+ return String(constants.reduce((a, b) => a + b, 0));
60129
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
60130
+ if (nonZero.length === 0) return "0";
60131
+ if (nonZero.length === 1) return compile3(nonZero[0]);
60132
+ return `(${nonZero.map((x) => compile3(x)).join(" + ")})`;
60133
+ }
59982
60134
  const parts = args.map((a) => {
59983
60135
  const code = compile3(a);
59984
60136
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -60035,7 +60187,10 @@ Error in definition of "${name}"`,
60035
60187
  return `Math.atan(${compile3(args[0])})`;
60036
60188
  },
60037
60189
  Artanh: "Math.atanh",
60038
- Ceil: "Math.ceil",
60190
+ Ceil: (args, compile3) => {
60191
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60192
+ return `Math.ceil(${compile3(args[0])})`;
60193
+ },
60039
60194
  Chop: "_SYS.chop",
60040
60195
  Cos: (args, compile3) => {
60041
60196
  if (BaseCompiler.isComplexValued(args[0]))
@@ -60078,7 +60233,10 @@ Error in definition of "${name}"`,
60078
60233
  return `_SYS.cexp(${compile3(args[0])})`;
60079
60234
  return `Math.exp(${compile3(args[0])})`;
60080
60235
  },
60081
- Floor: "Math.floor",
60236
+ Floor: (args, compile3) => {
60237
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60238
+ return `Math.floor(${compile3(args[0])})`;
60239
+ },
60082
60240
  Fract: ([x], compile3) => {
60083
60241
  if (x === null) throw new Error("Fract: no argument");
60084
60242
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile3(x));
@@ -60174,12 +60332,20 @@ Error in definition of "${name}"`,
60174
60332
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
60175
60333
  return `_SYS.cpow(${compile3(base)}, ${compile3(exp3)})`;
60176
60334
  }
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)}))`;
60335
+ const bConst = tryGetConstant(base);
60336
+ const eConst = tryGetConstant(exp3);
60337
+ if (bConst !== void 0 && eConst !== void 0)
60338
+ return String(Math.pow(bConst, eConst));
60339
+ if (eConst === 0) return "1";
60340
+ if (eConst === 1) return compile3(base);
60341
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
60342
+ const code = compile3(base);
60343
+ return `(${code} * ${code})`;
60344
+ }
60345
+ if (eConst === -1) return `(1 / (${compile3(base)}))`;
60346
+ if (eConst === 0.5) return `Math.sqrt(${compile3(base)})`;
60347
+ if (eConst === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60348
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60183
60349
  return `Math.pow(${compile3(base)}, ${compile3(exp3)})`;
60184
60350
  },
60185
60351
  Range: (args, compile3) => {
@@ -60216,16 +60382,29 @@ Error in definition of "${name}"`,
60216
60382
  Root: ([arg, exp3], compile3) => {
60217
60383
  if (arg === null) throw new Error("Root: no argument");
60218
60384
  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})`;
60385
+ const aConst = tryGetConstant(arg);
60386
+ const nConst = tryGetConstant(exp3);
60387
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
60388
+ return String(Math.pow(aConst, 1 / nConst));
60389
+ if (nConst === 2) return `Math.sqrt(${compile3(arg)})`;
60390
+ if (nConst === 3) return `Math.cbrt(${compile3(arg)})`;
60391
+ if (nConst !== void 0) return `Math.pow(${compile3(arg)}, ${1 / nConst})`;
60222
60392
  return `Math.pow(${compile3(arg)}, 1 / (${compile3(exp3)}))`;
60223
60393
  },
60224
60394
  Random: "Math.random",
60225
- Round: "Math.round",
60395
+ Round: (args, compile3) => {
60396
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60397
+ return `Math.round(${compile3(args[0])})`;
60398
+ },
60226
60399
  Square: (args, compile3) => {
60227
60400
  const arg = args[0];
60228
60401
  if (arg === null) throw new Error("Square: no argument");
60402
+ const c = tryGetConstant(arg);
60403
+ if (c !== void 0) return String(c * c);
60404
+ if (isSymbol2(arg)) {
60405
+ const code = compile3(arg);
60406
+ return `(${code} * ${code})`;
60407
+ }
60229
60408
  return `Math.pow(${compile3(arg)}, 2)`;
60230
60409
  },
60231
60410
  Sec: (args, compile3) => {
@@ -60258,6 +60437,8 @@ Error in definition of "${name}"`,
60258
60437
  Sqrt: (args, compile3) => {
60259
60438
  if (BaseCompiler.isComplexValued(args[0]))
60260
60439
  return `_SYS.csqrt(${compile3(args[0])})`;
60440
+ const c = tryGetConstant(args[0]);
60441
+ if (c !== void 0) return String(Math.sqrt(c));
60261
60442
  return `Math.sqrt(${compile3(args[0])})`;
60262
60443
  },
60263
60444
  Tan: (args, compile3) => {
@@ -60274,9 +60455,14 @@ Error in definition of "${name}"`,
60274
60455
  if (a === null || b === null) throw new Error("Mod: missing argument");
60275
60456
  const ca = compile3(a);
60276
60457
  const cb = compile3(b);
60458
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
60459
+ return `(${ca} % ${cb})`;
60277
60460
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
60278
60461
  },
60279
- Truncate: "Math.trunc",
60462
+ Truncate: (args, compile3) => {
60463
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60464
+ return `Math.trunc(${compile3(args[0])})`;
60465
+ },
60280
60466
  Remainder: ([a, b], compile3) => {
60281
60467
  if (a === null || b === null)
60282
60468
  throw new Error("Remainder: missing argument");
@@ -60284,25 +60470,20 @@ Error in definition of "${name}"`,
60284
60470
  a
60285
60471
  )} / ${compile3(b)}))`;
60286
60472
  },
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
- },
60473
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
60474
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
60301
60475
  Divide: ([a, b], compile3) => {
60302
60476
  if (a === null || b === null) throw new Error("Divide: missing argument");
60303
60477
  const ac = BaseCompiler.isComplexValued(a);
60304
60478
  const bc = BaseCompiler.isComplexValued(b);
60305
- if (!ac && !bc) return `(${compile3(a)} / ${compile3(b)})`;
60479
+ if (!ac && !bc) {
60480
+ const ca = tryGetConstant(a);
60481
+ const cb = tryGetConstant(b);
60482
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
60483
+ return String(ca / cb);
60484
+ if (cb === 1) return compile3(a);
60485
+ return `(${compile3(a)} / ${compile3(b)})`;
60486
+ }
60306
60487
  if (ac && bc) {
60307
60488
  return `(() => { const _a = ${compile3(a)}, _b = ${compile3(
60308
60489
  b
@@ -60319,13 +60500,26 @@ Error in definition of "${name}"`,
60319
60500
  },
60320
60501
  Negate: ([x], compile3) => {
60321
60502
  if (x === null) throw new Error("Negate: no argument");
60322
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile3(x)})`;
60503
+ if (!BaseCompiler.isComplexValued(x)) {
60504
+ const c = tryGetConstant(x);
60505
+ if (c !== void 0) return String(-c);
60506
+ return `(-${compile3(x)})`;
60507
+ }
60323
60508
  return `_SYS.cneg(${compile3(x)})`;
60324
60509
  },
60325
60510
  Multiply: (args, compile3) => {
60326
60511
  if (args.length === 1) return compile3(args[0]);
60327
60512
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60328
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" * ")})`;
60513
+ if (!anyComplex) {
60514
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
60515
+ const constants = args.map(tryGetConstant);
60516
+ if (constants.every((c) => c !== void 0))
60517
+ return String(constants.reduce((a, b) => a * b, 1));
60518
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
60519
+ if (nonOne.length === 0) return "1";
60520
+ if (nonOne.length === 1) return compile3(nonOne[0]);
60521
+ return `(${nonOne.map((x) => compile3(x)).join(" * ")})`;
60522
+ }
60329
60523
  if (args.length === 2) {
60330
60524
  const ac = BaseCompiler.isComplexValued(args[0]);
60331
60525
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -60410,20 +60604,30 @@ Error in definition of "${name}"`,
60410
60604
  AiryAi: "_SYS.airyAi",
60411
60605
  AiryBi: "_SYS.airyBi",
60412
60606
  // Combinatorics
60607
+ Mandelbrot: ([c, maxIter], compile3) => {
60608
+ if (c === null || maxIter === null)
60609
+ throw new Error("Mandelbrot: missing arguments");
60610
+ return `_SYS.mandelbrot(${compile3(c)}, ${compile3(maxIter)})`;
60611
+ },
60612
+ Julia: ([z, c, maxIter], compile3) => {
60613
+ if (z === null || c === null || maxIter === null)
60614
+ throw new Error("Julia: missing arguments");
60615
+ return `_SYS.julia(${compile3(z)}, ${compile3(c)}, ${compile3(maxIter)})`;
60616
+ },
60413
60617
  Binomial: (args, compile3) => `_SYS.binomial(${compile3(args[0])}, ${compile3(args[1])})`,
60414
60618
  Fibonacci: "_SYS.fibonacci",
60415
60619
  // Complex-specific functions
60416
- Re: (args, compile3) => {
60620
+ Real: (args, compile3) => {
60417
60621
  if (BaseCompiler.isComplexValued(args[0]))
60418
60622
  return `(${compile3(args[0])}).re`;
60419
60623
  return compile3(args[0]);
60420
60624
  },
60421
- Im: (args, compile3) => {
60625
+ Imaginary: (args, compile3) => {
60422
60626
  if (BaseCompiler.isComplexValued(args[0]))
60423
60627
  return `(${compile3(args[0])}).im`;
60424
60628
  return "0";
60425
60629
  },
60426
- Arg: (args, compile3) => {
60630
+ Argument: (args, compile3) => {
60427
60631
  if (BaseCompiler.isComplexValued(args[0]))
60428
60632
  return `_SYS.carg(${compile3(args[0])})`;
60429
60633
  return `(${compile3(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -60759,6 +60963,41 @@ Error in definition of "${name}"`,
60759
60963
  sinc,
60760
60964
  fresnelS,
60761
60965
  fresnelC,
60966
+ mandelbrot: (c, maxIter) => {
60967
+ let zx = 0, zy = 0;
60968
+ const cx = typeof c === "number" ? c : c.re;
60969
+ const cy = typeof c === "number" ? 0 : c.im;
60970
+ const n = Math.round(maxIter);
60971
+ for (let i = 0; i < n; i++) {
60972
+ const newZx = zx * zx - zy * zy + cx;
60973
+ zy = 2 * zx * zy + cy;
60974
+ zx = newZx;
60975
+ const mag2 = zx * zx + zy * zy;
60976
+ if (mag2 > 4) {
60977
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60978
+ return Math.max(0, Math.min(1, smooth));
60979
+ }
60980
+ }
60981
+ return 1;
60982
+ },
60983
+ julia: (z, c, maxIter) => {
60984
+ let zx = typeof z === "number" ? z : z.re;
60985
+ let zy = typeof z === "number" ? 0 : z.im;
60986
+ const cx = typeof c === "number" ? c : c.re;
60987
+ const cy = typeof c === "number" ? 0 : c.im;
60988
+ const n = Math.round(maxIter);
60989
+ for (let i = 0; i < n; i++) {
60990
+ const newZx = zx * zx - zy * zy + cx;
60991
+ zy = 2 * zx * zy + cy;
60992
+ zx = newZx;
60993
+ const mag2 = zx * zx + zy * zy;
60994
+ if (mag2 > 4) {
60995
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60996
+ return Math.max(0, Math.min(1, smooth));
60997
+ }
60998
+ }
60999
+ return 1;
61000
+ },
60762
61001
  binomial: choose,
60763
61002
  fibonacci,
60764
61003
  // Complex helpers
@@ -61099,11 +61338,39 @@ Error in definition of "${name}"`,
61099
61338
  return b;
61100
61339
  }
61101
61340
 
61341
+ // src/compute-engine/compilation/fractal-orbit.ts
61342
+ function computeReferenceOrbit(center, maxIter, precision) {
61343
+ const prevPrecision = BigDecimal.precision;
61344
+ BigDecimal.precision = precision;
61345
+ try {
61346
+ const cr = new BigDecimal(center[0]);
61347
+ const ci = new BigDecimal(center[1]);
61348
+ let zr = BigDecimal.ZERO;
61349
+ let zi = BigDecimal.ZERO;
61350
+ const ESCAPE = new BigDecimal(256);
61351
+ const points = [];
61352
+ for (let i = 0; i < maxIter; i++) {
61353
+ points.push(zr.toNumber(), zi.toNumber());
61354
+ const zr2 = zr.mul(zr).toPrecision(precision);
61355
+ const zi2 = zi.mul(zi).toPrecision(precision);
61356
+ const mag2 = zr2.add(zi2);
61357
+ if (mag2.cmp(ESCAPE) > 0) break;
61358
+ const new_zi = zr.mul(zi).toPrecision(precision).mul(2).add(ci);
61359
+ zr = zr2.sub(zi2).add(cr);
61360
+ zi = new_zi;
61361
+ }
61362
+ return new Float32Array(points);
61363
+ } finally {
61364
+ BigDecimal.precision = prevPrecision;
61365
+ }
61366
+ }
61367
+
61102
61368
  // src/compute-engine/compilation/gpu-target.ts
61103
61369
  var GPU_OPERATORS = {
61104
61370
  Add: ["+", 11],
61105
61371
  Negate: ["-", 14],
61106
61372
  Subtract: ["-", 11],
61373
+ // Subtract canonicalizes to Add+Negate; kept as fallback
61107
61374
  Multiply: ["*", 12],
61108
61375
  Divide: ["/", 13],
61109
61376
  Equal: ["==", 8],
@@ -61119,6 +61386,12 @@ Error in definition of "${name}"`,
61119
61386
  function gpuVec2(target) {
61120
61387
  return target?.language === "wgsl" ? "vec2f" : "vec2";
61121
61388
  }
61389
+ function compileIntArg(expr2, compile3, target) {
61390
+ const c = tryGetConstant(expr2);
61391
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
61392
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
61393
+ return `${intCast}(${compile3(expr2)})`;
61394
+ }
61122
61395
  var GPU_UNROLL_LIMIT = 100;
61123
61396
  function compileGPUSumProduct(kind, args, _compile2, target) {
61124
61397
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -61175,94 +61448,126 @@ Error in definition of "${name}"`,
61175
61448
  ];
61176
61449
  return lines.join("\n");
61177
61450
  }
61451
+ function selectFractalStrategy(target) {
61452
+ const radius = target.hints?.viewport?.radius;
61453
+ if (radius === void 0) return "single";
61454
+ if (radius > 1e-6) return "single";
61455
+ if (radius > 1e-14) return "double";
61456
+ return "perturbation";
61457
+ }
61178
61458
  var GPU_FUNCTIONS = {
61179
61459
  // Variadic arithmetic (for function-call form, e.g., with vectors)
61180
61460
  Add: (args, compile3, target) => {
61181
61461
  if (args.length === 0) return "0.0";
61182
61462
  if (args.length === 1) return compile3(args[0]);
61183
61463
  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
- }
61464
+ if (!anyComplex) {
61465
+ return foldTerms(
61466
+ args.map((x) => compile3(x)),
61467
+ "0.0",
61468
+ "+"
61469
+ );
61211
61470
  }
61212
- return result;
61213
- },
61214
- Subtract: (args, compile3, target) => {
61215
- if (args.length === 0) return "0.0";
61471
+ const parts = args.map((a) => tryGetComplexParts(a, compile3));
61472
+ if (parts.some((p) => p === null)) {
61473
+ const v2 = gpuVec2(target);
61474
+ return args.map((a) => {
61475
+ const code = compile3(a);
61476
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61477
+ }).join(" + ");
61478
+ }
61479
+ const reParts = [];
61480
+ const imParts = [];
61481
+ for (const p of parts) {
61482
+ if (p.re !== null) reParts.push(p.re);
61483
+ if (p.im !== null) imParts.push(p.im);
61484
+ }
61485
+ const reSum = foldTerms(reParts, "0.0", "+");
61486
+ const imSum = foldTerms(imParts, "0.0", "+");
61487
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
61488
+ },
61489
+ Multiply: (args, compile3, target) => {
61490
+ if (args.length === 0) return "1.0";
61216
61491
  if (args.length === 1) return compile3(args[0]);
61217
61492
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
61218
61493
  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])}`;
61494
+ return foldTerms(
61495
+ args.map((x) => compile3(x)),
61496
+ "1.0",
61497
+ "*"
61498
+ );
61235
61499
  }
61500
+ const iIndex = args.findIndex(
61501
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
61502
+ );
61503
+ if (iIndex >= 0) {
61504
+ const iFactor = args[iIndex];
61505
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
61506
+ const realFactors = args.filter((_, i) => i !== iIndex);
61507
+ const v2 = gpuVec2(target);
61508
+ if (realFactors.length === 0)
61509
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
61510
+ const factors = realFactors.map((f) => compile3(f));
61511
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
61512
+ const imCode = foldTerms(factors, "1.0", "*");
61513
+ return `${v2}(0.0, ${imCode})`;
61514
+ }
61515
+ const realCodes = [];
61516
+ const complexCodes = [];
61517
+ for (const a of args) {
61518
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile3(a));
61519
+ else realCodes.push(compile3(a));
61520
+ }
61521
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
61522
+ let result = complexCodes[0];
61523
+ for (let i = 1; i < complexCodes.length; i++) {
61524
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
61525
+ }
61526
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
61236
61527
  return result;
61237
61528
  },
61529
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
61530
+ // The operator entry in GPU_OPERATORS handles any edge cases.
61238
61531
  Divide: (args, compile3, target) => {
61239
61532
  if (args.length === 0) return "1.0";
61240
61533
  if (args.length === 1) return compile3(args[0]);
61241
61534
  const ac = BaseCompiler.isComplexValued(args[0]);
61242
61535
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
61243
61536
  if (!ac && !bc) {
61244
- if (args.length === 2) return `${compile3(args[0])} / ${compile3(args[1])}`;
61537
+ if (args.length === 2) {
61538
+ const a = tryGetConstant(args[0]);
61539
+ const b = tryGetConstant(args[1]);
61540
+ if (a !== void 0 && b !== void 0 && b !== 0)
61541
+ return formatFloat(a / b);
61542
+ if (b === 1) return compile3(args[0]);
61543
+ return `${compile3(args[0])} / ${compile3(args[1])}`;
61544
+ }
61245
61545
  let result = compile3(args[0]);
61246
- for (let i = 1; i < args.length; i++) {
61546
+ for (let i = 1; i < args.length; i++)
61247
61547
  result = `${result} / ${compile3(args[i])}`;
61248
- }
61249
61548
  return result;
61250
61549
  }
61251
61550
  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
- }
61551
+ if (ac && !bc) return `(${compile3(args[0])} / ${compile3(args[1])})`;
61255
61552
  const v2 = gpuVec2(target);
61256
61553
  return `_gpu_cdiv(${v2}(${compile3(args[0])}, 0.0), ${compile3(args[1])})`;
61257
61554
  },
61258
- Negate: ([x], compile3) => {
61555
+ Negate: ([x], compile3, target) => {
61259
61556
  if (x === null) throw new Error("Negate: no argument");
61557
+ const c = tryGetConstant(x);
61558
+ if (c !== void 0) return formatFloat(-c);
61559
+ if (isNumber(x) && x.im !== 0) {
61560
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
61561
+ }
61562
+ if (isSymbol2(x, "ImaginaryUnit"))
61563
+ return `${gpuVec2(target)}(0.0, -1.0)`;
61260
61564
  return `(-${compile3(x)})`;
61261
61565
  },
61262
61566
  // Standard math functions with complex dispatch
61263
61567
  Abs: (args, compile3) => {
61264
61568
  if (BaseCompiler.isComplexValued(args[0]))
61265
61569
  return `length(${compile3(args[0])})`;
61570
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
61266
61571
  return `abs(${compile3(args[0])})`;
61267
61572
  },
61268
61573
  Arccos: (args, compile3) => {
@@ -61280,7 +61585,10 @@ Error in definition of "${name}"`,
61280
61585
  return `_gpu_catan(${compile3(args[0])})`;
61281
61586
  return `atan(${compile3(args[0])})`;
61282
61587
  },
61283
- Ceil: "ceil",
61588
+ Ceil: (args, compile3) => {
61589
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61590
+ return `ceil(${compile3(args[0])})`;
61591
+ },
61284
61592
  Clamp: "clamp",
61285
61593
  Cos: (args, compile3) => {
61286
61594
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61294,7 +61602,10 @@ Error in definition of "${name}"`,
61294
61602
  return `exp(${compile3(args[0])})`;
61295
61603
  },
61296
61604
  Exp2: "exp2",
61297
- Floor: "floor",
61605
+ Floor: (args, compile3) => {
61606
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61607
+ return `floor(${compile3(args[0])})`;
61608
+ },
61298
61609
  Fract: "fract",
61299
61610
  Ln: (args, compile3) => {
61300
61611
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61316,10 +61627,25 @@ Error in definition of "${name}"`,
61316
61627
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile3(exp3) : `${v2}(${compile3(exp3)}, 0.0)`;
61317
61628
  return `_gpu_cpow(${bCode}, ${eCode})`;
61318
61629
  }
61630
+ const bConst = tryGetConstant(base);
61631
+ const eConst = tryGetConstant(exp3);
61632
+ if (bConst !== void 0 && eConst !== void 0)
61633
+ return formatFloat(Math.pow(bConst, eConst));
61634
+ if (eConst === 0) return "1.0";
61635
+ if (eConst === 1) return compile3(base);
61636
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
61637
+ const code = compile3(base);
61638
+ return `(${code} * ${code})`;
61639
+ }
61640
+ if (eConst === -1) return `(1.0 / ${compile3(base)})`;
61641
+ if (eConst === 0.5) return `sqrt(${compile3(base)})`;
61319
61642
  return `pow(${compile3(base)}, ${compile3(exp3)})`;
61320
61643
  },
61321
61644
  Radians: "radians",
61322
- Round: "round",
61645
+ Round: (args, compile3) => {
61646
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61647
+ return `round(${compile3(args[0])})`;
61648
+ },
61323
61649
  Sign: "sign",
61324
61650
  Sin: (args, compile3) => {
61325
61651
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61330,6 +61656,8 @@ Error in definition of "${name}"`,
61330
61656
  Sqrt: (args, compile3) => {
61331
61657
  if (BaseCompiler.isComplexValued(args[0]))
61332
61658
  return `_gpu_csqrt(${compile3(args[0])})`;
61659
+ const c = tryGetConstant(args[0]);
61660
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
61333
61661
  return `sqrt(${compile3(args[0])})`;
61334
61662
  },
61335
61663
  Step: "step",
@@ -61338,17 +61666,20 @@ Error in definition of "${name}"`,
61338
61666
  return `_gpu_ctan(${compile3(args[0])})`;
61339
61667
  return `tan(${compile3(args[0])})`;
61340
61668
  },
61341
- Truncate: "trunc",
61669
+ Truncate: (args, compile3) => {
61670
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61671
+ return `trunc(${compile3(args[0])})`;
61672
+ },
61342
61673
  // Complex-specific functions
61343
- Re: (args, compile3) => {
61674
+ Real: (args, compile3) => {
61344
61675
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).x`;
61345
61676
  return compile3(args[0]);
61346
61677
  },
61347
- Im: (args, compile3) => {
61678
+ Imaginary: (args, compile3) => {
61348
61679
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).y`;
61349
61680
  return "0.0";
61350
61681
  },
61351
- Arg: (args, compile3) => {
61682
+ Argument: (args, compile3) => {
61352
61683
  if (BaseCompiler.isComplexValued(args[0])) {
61353
61684
  const code = compile3(args[0]);
61354
61685
  return `atan(${code}.y, ${code}.x)`;
@@ -61563,13 +61894,20 @@ Error in definition of "${name}"`,
61563
61894
  },
61564
61895
  Square: ([x], compile3) => {
61565
61896
  if (x === null) throw new Error("Square: no argument");
61566
- const arg = compile3(x);
61567
- return `(${arg} * ${arg})`;
61897
+ if (isSymbol2(x) || isNumber(x)) {
61898
+ const arg = compile3(x);
61899
+ return `(${arg} * ${arg})`;
61900
+ }
61901
+ return `pow(${compile3(x)}, 2.0)`;
61568
61902
  },
61569
61903
  Root: ([x, n], compile3) => {
61570
61904
  if (x === null) throw new Error("Root: no argument");
61571
61905
  if (n === null || n === void 0) return `sqrt(${compile3(x)})`;
61572
- if (n?.re === 2) return `sqrt(${compile3(x)})`;
61906
+ const nConst = tryGetConstant(n);
61907
+ if (nConst === 2) return `sqrt(${compile3(x)})`;
61908
+ const xConst = tryGetConstant(x);
61909
+ if (xConst !== void 0 && nConst !== void 0)
61910
+ return formatFloat(Math.pow(xConst, 1 / nConst));
61573
61911
  return `pow(${compile3(x)}, 1.0 / ${compile3(n)})`;
61574
61912
  },
61575
61913
  // Color functions (pure-math, GPU-compilable)
@@ -61611,18 +61949,34 @@ Error in definition of "${name}"`,
61611
61949
  Mandelbrot: ([c, maxIter], compile3, target) => {
61612
61950
  if (c === null || maxIter === null)
61613
61951
  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
- )}))`;
61952
+ const iterCode = compileIntArg(maxIter, compile3, target);
61953
+ const strategy = selectFractalStrategy(target);
61954
+ if (strategy === "double") {
61955
+ const cCode = compile3(c);
61956
+ return `_fractal_mandelbrot_dp(vec4(${cCode}, vec2(0.0)), ${iterCode})`;
61957
+ }
61958
+ if (strategy === "perturbation") {
61959
+ const cCode = compile3(c);
61960
+ return `_fractal_mandelbrot_pt(${cCode}, ${iterCode})`;
61961
+ }
61962
+ return `_fractal_mandelbrot(${compile3(c)}, ${iterCode})`;
61618
61963
  },
61619
61964
  Julia: ([z, c, maxIter], compile3, target) => {
61620
61965
  if (z === null || c === null || maxIter === null)
61621
61966
  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
- )}))`;
61967
+ const iterCode = compileIntArg(maxIter, compile3, target);
61968
+ const strategy = selectFractalStrategy(target);
61969
+ if (strategy === "double") {
61970
+ const zCode = compile3(z);
61971
+ const cCode = compile3(c);
61972
+ return `_fractal_julia_dp(vec4(${zCode}, vec2(0.0)), vec4(${cCode}, vec2(0.0)), ${iterCode})`;
61973
+ }
61974
+ if (strategy === "perturbation") {
61975
+ const zCode = compile3(z);
61976
+ const cCode = compile3(c);
61977
+ return `_fractal_julia_pt(${zCode}, ${cCode}, ${iterCode})`;
61978
+ }
61979
+ return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${iterCode})`;
61626
61980
  },
61627
61981
  // Vector/Matrix operations
61628
61982
  Cross: "cross",
@@ -62218,6 +62572,200 @@ fn _gpu_besselJ(n_in: i32, x_in: f32) -> f32 {
62218
62572
  for (var k2: i32 = 2; k2 <= M; k2 += 2) { norm += 2.0 * vals[k2]; }
62219
62573
  return sgn * vals[n] / norm;
62220
62574
  }
62575
+ `;
62576
+ var GPU_DS_ARITHMETIC_PREAMBLE_GLSL = `
62577
+ // Split a float into high and low parts for exact multiplication
62578
+ vec2 ds_split(float a) {
62579
+ const float SPLIT = 4097.0; // 2^12 + 1
62580
+ float t = SPLIT * a;
62581
+ float hi = t - (t - a);
62582
+ float lo = a - hi;
62583
+ return vec2(hi, lo);
62584
+ }
62585
+
62586
+ // Create a double-single from a single float
62587
+ vec2 ds_from(float a) {
62588
+ return vec2(a, 0.0);
62589
+ }
62590
+
62591
+ // Error-free addition (Knuth TwoSum)
62592
+ vec2 ds_add(vec2 a, vec2 b) {
62593
+ float s = a.x + b.x;
62594
+ float v = s - a.x;
62595
+ float e = (a.x - (s - v)) + (b.x - v);
62596
+ float lo = (a.y + b.y) + e;
62597
+ float hi = s + lo;
62598
+ lo = lo - (hi - s);
62599
+ return vec2(hi, lo);
62600
+ }
62601
+
62602
+ // Double-single subtraction
62603
+ vec2 ds_sub(vec2 a, vec2 b) {
62604
+ return ds_add(a, vec2(-b.x, -b.y));
62605
+ }
62606
+
62607
+ // Error-free multiplication (Dekker TwoProduct)
62608
+ vec2 ds_mul(vec2 a, vec2 b) {
62609
+ float p = a.x * b.x;
62610
+ vec2 sa = ds_split(a.x);
62611
+ vec2 sb = ds_split(b.x);
62612
+ float err = ((sa.x * sb.x - p) + sa.x * sb.y + sa.y * sb.x) + sa.y * sb.y;
62613
+ err += a.x * b.y + a.y * b.x;
62614
+ float hi = p + err;
62615
+ float lo = err - (hi - p);
62616
+ return vec2(hi, lo);
62617
+ }
62618
+
62619
+ // Optimized self-multiply
62620
+ vec2 ds_sqr(vec2 a) {
62621
+ float p = a.x * a.x;
62622
+ vec2 sa = ds_split(a.x);
62623
+ float err = ((sa.x * sa.x - p) + 2.0 * sa.x * sa.y) + sa.y * sa.y;
62624
+ err += 2.0 * a.x * a.y;
62625
+ float hi = p + err;
62626
+ float lo = err - (hi - p);
62627
+ return vec2(hi, lo);
62628
+ }
62629
+
62630
+ // Compare magnitude: returns -1, 0, or 1
62631
+ float ds_cmp(vec2 a, vec2 b) {
62632
+ float d = a.x - b.x;
62633
+ if (d != 0.0) return sign(d);
62634
+ return sign(a.y - b.y);
62635
+ }
62636
+ `;
62637
+ var GPU_DS_ARITHMETIC_PREAMBLE_WGSL = `
62638
+ fn ds_split(a: f32) -> vec2f {
62639
+ const SPLIT: f32 = 4097.0;
62640
+ let t = SPLIT * a;
62641
+ let hi = t - (t - a);
62642
+ let lo = a - hi;
62643
+ return vec2f(hi, lo);
62644
+ }
62645
+
62646
+ fn ds_from(a: f32) -> vec2f {
62647
+ return vec2f(a, 0.0);
62648
+ }
62649
+
62650
+ fn ds_add(a: vec2f, b: vec2f) -> vec2f {
62651
+ let s = a.x + b.x;
62652
+ let v = s - a.x;
62653
+ let e = (a.x - (s - v)) + (b.x - v);
62654
+ let lo_t = (a.y + b.y) + e;
62655
+ let hi = s + lo_t;
62656
+ let lo = lo_t - (hi - s);
62657
+ return vec2f(hi, lo);
62658
+ }
62659
+
62660
+ fn ds_sub(a: vec2f, b: vec2f) -> vec2f {
62661
+ return ds_add(a, vec2f(-b.x, -b.y));
62662
+ }
62663
+
62664
+ fn ds_mul(a: vec2f, b: vec2f) -> vec2f {
62665
+ let p = a.x * b.x;
62666
+ let sa = ds_split(a.x);
62667
+ let sb = ds_split(b.x);
62668
+ var err = ((sa.x * sb.x - p) + sa.x * sb.y + sa.y * sb.x) + sa.y * sb.y;
62669
+ err += a.x * b.y + a.y * b.x;
62670
+ let hi = p + err;
62671
+ let lo = err - (hi - p);
62672
+ return vec2f(hi, lo);
62673
+ }
62674
+
62675
+ fn ds_sqr(a: vec2f) -> vec2f {
62676
+ let p = a.x * a.x;
62677
+ let sa = ds_split(a.x);
62678
+ var err = ((sa.x * sa.x - p) + 2.0 * sa.x * sa.y) + sa.y * sa.y;
62679
+ err += 2.0 * a.x * a.y;
62680
+ let hi = p + err;
62681
+ let lo = err - (hi - p);
62682
+ return vec2f(hi, lo);
62683
+ }
62684
+
62685
+ fn ds_cmp(a: vec2f, b: vec2f) -> f32 {
62686
+ let d = a.x - b.x;
62687
+ if (d != 0.0) { return sign(d); }
62688
+ return sign(a.y - b.y);
62689
+ }
62690
+ `;
62691
+ var GPU_FRACTAL_DP_PREAMBLE_GLSL = `
62692
+ float _fractal_mandelbrot_dp(vec4 c, int maxIter) {
62693
+ // c = (re_hi, im_hi, re_lo, im_lo)
62694
+ vec2 cr = vec2(c.x, c.z); // real part as ds
62695
+ vec2 ci = vec2(c.y, c.w); // imag part as ds
62696
+ vec2 zr = vec2(0.0, 0.0);
62697
+ vec2 zi = vec2(0.0, 0.0);
62698
+ for (int i = 0; i < maxIter; i++) {
62699
+ vec2 zr2 = ds_sqr(zr);
62700
+ vec2 zi2 = ds_sqr(zi);
62701
+ // |z|^2 > 4.0 ?
62702
+ vec2 mag2 = ds_add(zr2, zi2);
62703
+ if (mag2.x > 4.0)
62704
+ return clamp((float(i) - log2(log2(mag2.x)) + 4.0) / float(maxIter), 0.0, 1.0);
62705
+ // z = z^2 + c
62706
+ vec2 new_zi = ds_add(ds_mul(ds_add(zr, zr), zi), ci); // 2*zr*zi + ci
62707
+ zr = ds_add(ds_sub(zr2, zi2), cr); // zr^2 - zi^2 + cr
62708
+ zi = new_zi;
62709
+ }
62710
+ return 1.0;
62711
+ }
62712
+
62713
+ float _fractal_julia_dp(vec4 z_in, vec4 c, int maxIter) {
62714
+ vec2 zr = vec2(z_in.x, z_in.z);
62715
+ vec2 zi = vec2(z_in.y, z_in.w);
62716
+ vec2 cr = vec2(c.x, c.z);
62717
+ vec2 ci = vec2(c.y, c.w);
62718
+ for (int i = 0; i < maxIter; i++) {
62719
+ vec2 zr2 = ds_sqr(zr);
62720
+ vec2 zi2 = ds_sqr(zi);
62721
+ vec2 mag2 = ds_add(zr2, zi2);
62722
+ if (mag2.x > 4.0)
62723
+ return clamp((float(i) - log2(log2(mag2.x)) + 4.0) / float(maxIter), 0.0, 1.0);
62724
+ vec2 new_zi = ds_add(ds_mul(ds_add(zr, zr), zi), ci);
62725
+ zr = ds_add(ds_sub(zr2, zi2), cr);
62726
+ zi = new_zi;
62727
+ }
62728
+ return 1.0;
62729
+ }
62730
+ `;
62731
+ var GPU_FRACTAL_DP_PREAMBLE_WGSL = `
62732
+ fn _fractal_mandelbrot_dp(c: vec4f, maxIter: i32) -> f32 {
62733
+ let cr = vec2f(c.x, c.z);
62734
+ let ci = vec2f(c.y, c.w);
62735
+ var zr = vec2f(0.0, 0.0);
62736
+ var zi = vec2f(0.0, 0.0);
62737
+ for (var i: i32 = 0; i < maxIter; i++) {
62738
+ let zr2 = ds_sqr(zr);
62739
+ let zi2 = ds_sqr(zi);
62740
+ let mag2 = ds_add(zr2, zi2);
62741
+ if (mag2.x > 4.0) {
62742
+ return clamp((f32(i) - log2(log2(mag2.x)) + 4.0) / f32(maxIter), 0.0, 1.0);
62743
+ }
62744
+ let new_zi = ds_add(ds_mul(ds_add(zr, zr), zi), ci);
62745
+ zr = ds_add(ds_sub(zr2, zi2), cr);
62746
+ zi = new_zi;
62747
+ }
62748
+ return 1.0;
62749
+ }
62750
+
62751
+ fn _fractal_julia_dp(z_in: vec4f, c: vec4f, maxIter: i32) -> f32 {
62752
+ var zr = vec2f(z_in.x, z_in.z);
62753
+ var zi = vec2f(z_in.y, z_in.w);
62754
+ let cr = vec2f(c.x, c.z);
62755
+ let ci = vec2f(c.y, c.w);
62756
+ for (var i: i32 = 0; i < maxIter; i++) {
62757
+ let zr2 = ds_sqr(zr);
62758
+ let zi2 = ds_sqr(zi);
62759
+ let mag2 = ds_add(zr2, zi2);
62760
+ if (mag2.x > 4.0) {
62761
+ return clamp((f32(i) - log2(log2(mag2.x)) + 4.0) / f32(maxIter), 0.0, 1.0);
62762
+ }
62763
+ let new_zi = ds_add(ds_mul(ds_add(zr, zr), zi), ci);
62764
+ zr = ds_add(ds_sub(zr2, zi2), cr);
62765
+ zi = new_zi;
62766
+ }
62767
+ return 1.0;
62768
+ }
62221
62769
  `;
62222
62770
  var GPU_FRACTAL_PREAMBLE_GLSL = `
62223
62771
  float _fractal_mandelbrot(vec2 c, int maxIter) {
@@ -62261,6 +62809,188 @@ fn _fractal_julia(z_in: vec2f, c: vec2f, maxIter: i32) -> f32 {
62261
62809
  }
62262
62810
  return 1.0;
62263
62811
  }
62812
+ `;
62813
+ var GPU_FRACTAL_PT_PREAMBLE_GLSL = `
62814
+ uniform sampler2D _refOrbit;
62815
+ uniform int _refOrbitLen;
62816
+ uniform int _refOrbitTexWidth;
62817
+
62818
+ vec2 _pt_fetch_orbit(int i) {
62819
+ int y = i / _refOrbitTexWidth;
62820
+ int x = i - y * _refOrbitTexWidth;
62821
+ return texelFetch(_refOrbit, ivec2(x, y), 0).rg;
62822
+ }
62823
+
62824
+ float _fractal_mandelbrot_pt(vec2 delta_c, int maxIter) {
62825
+ float dr = 0.0;
62826
+ float di = 0.0;
62827
+ int orbitLen = min(maxIter, _refOrbitLen);
62828
+ for (int i = 0; i < orbitLen; i++) {
62829
+ vec2 Zn = _pt_fetch_orbit(i);
62830
+ // delta_{n+1} = 2*Z_n*delta_n + delta_n^2 + delta_c
62831
+ float new_dr = 2.0 * (Zn.x * dr - Zn.y * di) + dr * dr - di * di + delta_c.x;
62832
+ float new_di = 2.0 * (Zn.x * di + Zn.y * dr) + 2.0 * dr * di + delta_c.y;
62833
+ dr = new_dr;
62834
+ di = new_di;
62835
+ // Full z = Z_{n+1} + delta for escape check
62836
+ vec2 Zn1 = (i + 1 < orbitLen) ? _pt_fetch_orbit(i + 1) : vec2(0.0);
62837
+ float zr = Zn1.x + dr;
62838
+ float zi = Zn1.y + di;
62839
+ float mag2 = zr * zr + zi * zi;
62840
+ if (mag2 > 4.0)
62841
+ return clamp((float(i) - log2(log2(mag2)) + 4.0) / float(maxIter), 0.0, 1.0);
62842
+ // Glitch detection: |delta|^2 > |Z|^2
62843
+ float dmag2 = dr * dr + di * di;
62844
+ float Zmag2 = Zn.x * Zn.x + Zn.y * Zn.y;
62845
+ if (dmag2 > Zmag2 && Zmag2 > 0.0) {
62846
+ // Rebase to absolute coordinates and continue with single-float
62847
+ float abs_zr = Zn1.x + dr;
62848
+ float abs_zi = Zn1.y + di;
62849
+ // Reconstruct absolute c from reference + delta
62850
+ // (Use ds_from for the concept, but single-float suffices for fallback)
62851
+ float cx = abs_zr - dr + delta_c.x;
62852
+ float cy = abs_zi - di + delta_c.y;
62853
+ for (int j = i + 1; j < maxIter; j++) {
62854
+ float new_zr = abs_zr * abs_zr - abs_zi * abs_zi + cx;
62855
+ abs_zi = 2.0 * abs_zr * abs_zi + cy;
62856
+ abs_zr = new_zr;
62857
+ mag2 = abs_zr * abs_zr + abs_zi * abs_zi;
62858
+ if (mag2 > 4.0)
62859
+ return clamp((float(j) - log2(log2(mag2)) + 4.0) / float(maxIter), 0.0, 1.0);
62860
+ }
62861
+ return 1.0;
62862
+ }
62863
+ }
62864
+ return 1.0;
62865
+ }
62866
+
62867
+ float _fractal_julia_pt(vec2 z_delta, vec2 delta_c, int maxIter) {
62868
+ float dr = z_delta.x;
62869
+ float di = z_delta.y;
62870
+ int orbitLen = min(maxIter, _refOrbitLen);
62871
+ for (int i = 0; i < orbitLen; i++) {
62872
+ vec2 Zn = _pt_fetch_orbit(i);
62873
+ float new_dr = 2.0 * (Zn.x * dr - Zn.y * di) + dr * dr - di * di + delta_c.x;
62874
+ float new_di = 2.0 * (Zn.x * di + Zn.y * dr) + 2.0 * dr * di + delta_c.y;
62875
+ dr = new_dr;
62876
+ di = new_di;
62877
+ vec2 Zn1 = (i + 1 < orbitLen) ? _pt_fetch_orbit(i + 1) : vec2(0.0);
62878
+ float zr = Zn1.x + dr;
62879
+ float zi = Zn1.y + di;
62880
+ float mag2 = zr * zr + zi * zi;
62881
+ if (mag2 > 4.0)
62882
+ return clamp((float(i) - log2(log2(mag2)) + 4.0) / float(maxIter), 0.0, 1.0);
62883
+ float dmag2 = dr * dr + di * di;
62884
+ float Zmag2 = Zn.x * Zn.x + Zn.y * Zn.y;
62885
+ if (dmag2 > Zmag2 && Zmag2 > 0.0) {
62886
+ float abs_zr = Zn1.x + dr;
62887
+ float abs_zi = Zn1.y + di;
62888
+ float cx = delta_c.x;
62889
+ float cy = delta_c.y;
62890
+ for (int j = i + 1; j < maxIter; j++) {
62891
+ float new_zr = abs_zr * abs_zr - abs_zi * abs_zi + cx;
62892
+ abs_zi = 2.0 * abs_zr * abs_zi + cy;
62893
+ abs_zr = new_zr;
62894
+ mag2 = abs_zr * abs_zr + abs_zi * abs_zi;
62895
+ if (mag2 > 4.0)
62896
+ return clamp((float(j) - log2(log2(mag2)) + 4.0) / float(maxIter), 0.0, 1.0);
62897
+ }
62898
+ return 1.0;
62899
+ }
62900
+ }
62901
+ return 1.0;
62902
+ }
62903
+ `;
62904
+ var GPU_FRACTAL_PT_PREAMBLE_WGSL = `
62905
+ @group(0) @binding(1) var _refOrbit: texture_2d<f32>;
62906
+ var<uniform> _refOrbitLen: i32;
62907
+ var<uniform> _refOrbitTexWidth: i32;
62908
+
62909
+ fn _pt_fetch_orbit(i: i32) -> vec2f {
62910
+ let y = i / _refOrbitTexWidth;
62911
+ let x = i - y * _refOrbitTexWidth;
62912
+ return textureLoad(_refOrbit, vec2i(x, y), 0).rg;
62913
+ }
62914
+
62915
+ fn _fractal_mandelbrot_pt(delta_c: vec2f, maxIter: i32) -> f32 {
62916
+ var dr: f32 = 0.0;
62917
+ var di: f32 = 0.0;
62918
+ let orbitLen = min(maxIter, _refOrbitLen);
62919
+ for (var i: i32 = 0; i < orbitLen; i++) {
62920
+ let Zn = _pt_fetch_orbit(i);
62921
+ let new_dr = 2.0 * (Zn.x * dr - Zn.y * di) + dr * dr - di * di + delta_c.x;
62922
+ let new_di = 2.0 * (Zn.x * di + Zn.y * dr) + 2.0 * dr * di + delta_c.y;
62923
+ dr = new_dr;
62924
+ di = new_di;
62925
+ var Zn1 = vec2f(0.0);
62926
+ if (i + 1 < orbitLen) { Zn1 = _pt_fetch_orbit(i + 1); }
62927
+ let zr = Zn1.x + dr;
62928
+ let zi = Zn1.y + di;
62929
+ var mag2 = zr * zr + zi * zi;
62930
+ if (mag2 > 4.0) {
62931
+ return clamp((f32(i) - log2(log2(mag2)) + 4.0) / f32(maxIter), 0.0, 1.0);
62932
+ }
62933
+ let dmag2 = dr * dr + di * di;
62934
+ let Zmag2 = Zn.x * Zn.x + Zn.y * Zn.y;
62935
+ if (dmag2 > Zmag2 && Zmag2 > 0.0) {
62936
+ var f_zr = Zn1.x + dr;
62937
+ var f_zi = Zn1.y + di;
62938
+ let cx = delta_c.x;
62939
+ let cy = delta_c.y;
62940
+ for (var j: i32 = i + 1; j < maxIter; j++) {
62941
+ let t_zr = f_zr * f_zr - f_zi * f_zi + cx;
62942
+ f_zi = 2.0 * f_zr * f_zi + cy;
62943
+ f_zr = t_zr;
62944
+ mag2 = f_zr * f_zr + f_zi * f_zi;
62945
+ if (mag2 > 4.0) {
62946
+ return clamp((f32(j) - log2(log2(mag2)) + 4.0) / f32(maxIter), 0.0, 1.0);
62947
+ }
62948
+ }
62949
+ return 1.0;
62950
+ }
62951
+ }
62952
+ return 1.0;
62953
+ }
62954
+
62955
+ fn _fractal_julia_pt(z_delta: vec2f, delta_c: vec2f, maxIter: i32) -> f32 {
62956
+ var dr = z_delta.x;
62957
+ var di = z_delta.y;
62958
+ let orbitLen = min(maxIter, _refOrbitLen);
62959
+ for (var i: i32 = 0; i < orbitLen; i++) {
62960
+ let Zn = _pt_fetch_orbit(i);
62961
+ let new_dr = 2.0 * (Zn.x * dr - Zn.y * di) + dr * dr - di * di + delta_c.x;
62962
+ let new_di = 2.0 * (Zn.x * di + Zn.y * dr) + 2.0 * dr * di + delta_c.y;
62963
+ dr = new_dr;
62964
+ di = new_di;
62965
+ var Zn1 = vec2f(0.0);
62966
+ if (i + 1 < orbitLen) { Zn1 = _pt_fetch_orbit(i + 1); }
62967
+ let zr = Zn1.x + dr;
62968
+ let zi = Zn1.y + di;
62969
+ var mag2 = zr * zr + zi * zi;
62970
+ if (mag2 > 4.0) {
62971
+ return clamp((f32(i) - log2(log2(mag2)) + 4.0) / f32(maxIter), 0.0, 1.0);
62972
+ }
62973
+ let dmag2 = dr * dr + di * di;
62974
+ let Zmag2 = Zn.x * Zn.x + Zn.y * Zn.y;
62975
+ if (dmag2 > Zmag2 && Zmag2 > 0.0) {
62976
+ var f_zr = Zn1.x + dr;
62977
+ var f_zi = Zn1.y + di;
62978
+ let cx = delta_c.x;
62979
+ let cy = delta_c.y;
62980
+ for (var j: i32 = i + 1; j < maxIter; j++) {
62981
+ let t_zr = f_zr * f_zr - f_zi * f_zi + cx;
62982
+ f_zi = 2.0 * f_zr * f_zi + cy;
62983
+ f_zr = t_zr;
62984
+ mag2 = f_zr * f_zr + f_zi * f_zi;
62985
+ if (mag2 > 4.0) {
62986
+ return clamp((f32(j) - log2(log2(mag2)) + 4.0) / f32(maxIter), 0.0, 1.0);
62987
+ }
62988
+ }
62989
+ return 1.0;
62990
+ }
62991
+ }
62992
+ return 1.0;
62993
+ }
62264
62994
  `;
62265
62995
  var GPU_COLOR_PREAMBLE_GLSL = `
62266
62996
  float _gpu_srgb_to_linear(float c) {
@@ -62700,6 +63430,7 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
62700
63430
  const constants = this.getConstants();
62701
63431
  const v2 = this.languageId === "wgsl" ? "vec2f" : "vec2";
62702
63432
  const target = this.createTarget({
63433
+ hints: options.hints,
62703
63434
  functions: (id) => {
62704
63435
  if (userFunctions && id in userFunctions) {
62705
63436
  const fn = userFunctions[id];
@@ -62738,12 +63469,65 @@ fn _gpu_apca(bg: vec3f, fg: vec3f) -> f32 {
62738
63469
  if (code.includes("_gpu_besselJ"))
62739
63470
  preamble += this.languageId === "wgsl" ? GPU_BESSELJ_PREAMBLE_WGSL : GPU_BESSELJ_PREAMBLE_GLSL;
62740
63471
  if (code.includes("_fractal_")) {
62741
- preamble += this.languageId === "wgsl" ? GPU_FRACTAL_PREAMBLE_WGSL : GPU_FRACTAL_PREAMBLE_GLSL;
63472
+ if (code.includes("_fractal_mandelbrot_pt") || code.includes("_fractal_julia_pt")) {
63473
+ preamble += this.languageId === "wgsl" ? GPU_DS_ARITHMETIC_PREAMBLE_WGSL : GPU_DS_ARITHMETIC_PREAMBLE_GLSL;
63474
+ preamble += this.languageId === "wgsl" ? GPU_FRACTAL_PT_PREAMBLE_WGSL : GPU_FRACTAL_PT_PREAMBLE_GLSL;
63475
+ } else if (code.includes("_fractal_mandelbrot_dp") || code.includes("_fractal_julia_dp")) {
63476
+ preamble += this.languageId === "wgsl" ? GPU_DS_ARITHMETIC_PREAMBLE_WGSL : GPU_DS_ARITHMETIC_PREAMBLE_GLSL;
63477
+ preamble += this.languageId === "wgsl" ? GPU_FRACTAL_DP_PREAMBLE_WGSL : GPU_FRACTAL_DP_PREAMBLE_GLSL;
63478
+ } else {
63479
+ preamble += this.languageId === "wgsl" ? GPU_FRACTAL_PREAMBLE_WGSL : GPU_FRACTAL_PREAMBLE_GLSL;
63480
+ }
62742
63481
  }
62743
63482
  if (code.includes("_gpu_srgb_to") || code.includes("_gpu_oklab") || code.includes("_gpu_oklch") || code.includes("_gpu_color_mix") || code.includes("_gpu_apca")) {
62744
63483
  preamble += this.languageId === "wgsl" ? GPU_COLOR_PREAMBLE_WGSL : GPU_COLOR_PREAMBLE_GLSL;
62745
63484
  }
62746
63485
  if (preamble) result.preamble = preamble;
63486
+ if (code.includes("_fractal_") && options.hints?.viewport) {
63487
+ const strategy = selectFractalStrategy(target);
63488
+ const radius = options.hints.viewport.radius;
63489
+ switch (strategy) {
63490
+ case "single":
63491
+ result.staleWhen = { radiusBelow: 1e-6 };
63492
+ break;
63493
+ case "double":
63494
+ result.staleWhen = { radiusBelow: 1e-14, radiusAbove: 1e-5 };
63495
+ break;
63496
+ case "perturbation":
63497
+ result.staleWhen = {
63498
+ radiusAbove: 1e-5,
63499
+ radiusBelow: radius * 0.01,
63500
+ centerDistance: radius * 2
63501
+ };
63502
+ break;
63503
+ }
63504
+ }
63505
+ if ((code.includes("_fractal_mandelbrot_pt") || code.includes("_fractal_julia_pt")) && options.hints?.viewport) {
63506
+ const viewport = options.hints.viewport;
63507
+ const digits = Math.max(50, Math.ceil(-Math.log10(viewport.radius)) + 10);
63508
+ const maxIter = 1e3;
63509
+ const orbit = computeReferenceOrbit(
63510
+ viewport.center,
63511
+ maxIter,
63512
+ digits
63513
+ );
63514
+ const orbitLen = orbit.length / 2;
63515
+ const texWidth = Math.min(orbitLen, 4096);
63516
+ const texHeight = Math.ceil(orbitLen / texWidth);
63517
+ result.textures = {
63518
+ _refOrbit: {
63519
+ data: orbit,
63520
+ width: texWidth,
63521
+ height: texHeight,
63522
+ format: "rg32f"
63523
+ }
63524
+ };
63525
+ result.uniforms = {
63526
+ ...result.uniforms,
63527
+ _refOrbitLen: orbitLen,
63528
+ _refOrbitTexWidth: texWidth
63529
+ };
63530
+ }
62747
63531
  return result;
62748
63532
  }
62749
63533
  compileToSource(expr2, _options = {}) {
@@ -64254,6 +65038,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64254
65038
  Add: ["_IA.add", 20],
64255
65039
  Negate: ["_IA.negate", 20],
64256
65040
  Subtract: ["_IA.sub", 20],
65041
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64257
65042
  Multiply: ["_IA.mul", 20],
64258
65043
  Divide: ["_IA.div", 20],
64259
65044
  // Comparisons return BoolInterval
@@ -64278,17 +65063,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64278
65063
  }
64279
65064
  return result;
64280
65065
  },
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
- },
65066
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
64292
65067
  Multiply: (args, compile3) => {
64293
65068
  if (args.length === 0) return "_IA.point(1)";
64294
65069
  if (args.length === 1) return compile3(args[0]);
@@ -65224,6 +65999,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
65224
65999
  Negate: ["-", 14],
65225
66000
  // Unary operator
65226
66001
  Subtract: ["-", 11],
66002
+ // Subtract canonicalizes to Add+Negate; kept as fallback
65227
66003
  Multiply: ["*", 12],
65228
66004
  Divide: ["/", 13],
65229
66005
  Power: ["**", 15],
@@ -65251,16 +66027,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
65251
66027
  if (args.length === 1) return compile3(args[0]);
65252
66028
  return args.map((x) => compile3(x)).join(" * ");
65253
66029
  },
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
- },
66030
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
65264
66031
  Divide: (args, compile3) => {
65265
66032
  if (args.length === 0) return "1";
65266
66033
  if (args.length === 1) return compile3(args[0]);
@@ -67692,7 +68459,9 @@ ${code}`;
67692
68459
  }
67693
68460
  return new ExactNumericValue(value, makeNumericValue);
67694
68461
  }
67695
- throw Error("Unexpected value");
68462
+ throw Error(
68463
+ `Unexpected value: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`
68464
+ );
67696
68465
  }
67697
68466
  /**
67698
68467
  * 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 +68932,14 @@ ${code}`;
68163
68932
  _setDefaultEngineFactory(() => new ComputeEngine());
68164
68933
 
68165
68934
  // src/compute-engine.ts
68166
- var version = "0.55.2";
68935
+ var version = "0.55.4";
68167
68936
  ComputeEngine._latexSyntaxFactory = () => new LatexSyntax();
68168
68937
  _setDefaultEngineFactory(
68169
68938
  () => new ComputeEngine({ latexSyntax: new LatexSyntax() })
68170
68939
  );
68171
68940
  globalThis[/* @__PURE__ */ Symbol.for("io.cortexjs.compute-engine")] = {
68172
68941
  ComputeEngine: ComputeEngine.prototype.constructor,
68173
- version: "0.55.2"
68942
+ version: "0.55.4"
68174
68943
  };
68175
68944
  return __toCommonJS(compute_engine_exports);
68176
68945
  })();