@cortex-js/compute-engine 0.57.0 → 0.58.0

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 (259) hide show
  1. package/dist/compile.esm.js +690 -53
  2. package/dist/compile.min.esm.js +251 -51
  3. package/dist/compile.min.umd.cjs +251 -51
  4. package/dist/compile.umd.cjs +690 -53
  5. package/dist/compute-engine.esm.js +1079 -132
  6. package/dist/compute-engine.min.esm.js +271 -71
  7. package/dist/compute-engine.min.umd.cjs +270 -70
  8. package/dist/compute-engine.umd.cjs +1079 -132
  9. package/dist/core.esm.js +1078 -131
  10. package/dist/core.min.esm.js +269 -69
  11. package/dist/core.min.umd.cjs +269 -69
  12. package/dist/core.umd.cjs +1078 -131
  13. package/dist/interval.esm.js +132 -15
  14. package/dist/interval.min.esm.js +6 -6
  15. package/dist/interval.min.umd.cjs +6 -6
  16. package/dist/interval.umd.cjs +132 -15
  17. package/dist/latex-syntax.esm.js +132 -15
  18. package/dist/latex-syntax.min.esm.js +6 -6
  19. package/dist/latex-syntax.min.umd.cjs +6 -6
  20. package/dist/latex-syntax.umd.cjs +132 -15
  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 +38 -3
  26. package/dist/numerics.min.esm.js +3 -3
  27. package/dist/numerics.min.umd.cjs +4 -4
  28. package/dist/numerics.umd.cjs +38 -3
  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 +3 -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 +7 -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 +22 -10
  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 -1
  105. package/dist/types/compute-engine/compilation/compile-expression.d.ts +1 -1
  106. package/dist/types/compute-engine/compilation/constant-folding.d.ts +1 -1
  107. package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
  108. package/dist/types/compute-engine/compilation/gpu-target.d.ts +44 -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 +1 -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 +4 -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 +17 -2
  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-colors.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/random.d.ts +23 -0
  212. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  213. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  214. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  215. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  216. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  217. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  218. package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
  219. package/dist/types/compute-engine/oeis.d.ts +1 -1
  220. package/dist/types/compute-engine/sequence.d.ts +1 -1
  221. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  222. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  223. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  224. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  225. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  226. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  227. package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
  228. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  229. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  230. package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
  231. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  232. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  233. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  234. package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
  235. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  236. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  237. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  238. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  239. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  240. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  241. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  242. package/dist/types/compute-engine/types-definitions.d.ts +1 -1
  243. package/dist/types/compute-engine/types-engine.d.ts +34 -1
  244. package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
  245. package/dist/types/compute-engine/types-expression.d.ts +69 -1
  246. package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
  247. package/dist/types/compute-engine/types-kernel-serialization.d.ts +1 -1
  248. package/dist/types/compute-engine/types-serialization.d.ts +1 -1
  249. package/dist/types/compute-engine/types.d.ts +1 -1
  250. package/dist/types/compute-engine.d.ts +1 -1
  251. package/dist/types/core.d.ts +1 -1
  252. package/dist/types/interval.d.ts +1 -1
  253. package/dist/types/latex-syntax.d.ts +2 -2
  254. package/dist/types/math-json/symbols.d.ts +1 -1
  255. package/dist/types/math-json/types.d.ts +1 -1
  256. package/dist/types/math-json/utils.d.ts +1 -1
  257. package/dist/types/math-json.d.ts +2 -2
  258. package/dist/types/numerics.d.ts +1 -1
  259. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.57.0 */
1
+ /** Compute Engine 0.58.0 */
2
2
 
3
3
  // src/compute-engine/numerics/richardson.ts
4
4
  function extrapolate(f, x0, options = {}) {
@@ -3535,7 +3535,42 @@ function widen2(a, b) {
3535
3535
  if (b === "nothing") return a;
3536
3536
  if (isSubtype(a, b)) return b;
3537
3537
  if (isSubtype(b, a)) return a;
3538
- return superType(a, b);
3538
+ const sup = superType(a, b);
3539
+ if (LOSSY_SUPERTYPE.has(sup)) return unionTypes(a, b);
3540
+ return sup;
3541
+ }
3542
+ var LOSSY_SUPERTYPE = /* @__PURE__ */ new Set([
3543
+ "scalar",
3544
+ "value",
3545
+ "function",
3546
+ "expression",
3547
+ "collection",
3548
+ "indexed_collection",
3549
+ "list",
3550
+ "set",
3551
+ "tuple",
3552
+ "record",
3553
+ "dictionary",
3554
+ "map",
3555
+ "any"
3556
+ ]);
3557
+ function unionTypes(a, b) {
3558
+ const members = [];
3559
+ const push = (t) => {
3560
+ if (typeof t === "object" && t.kind === "union") {
3561
+ for (const m of t.types) push(m);
3562
+ return;
3563
+ }
3564
+ const key = typeof t === "string" ? t : JSON.stringify(t);
3565
+ if (!members.some(
3566
+ (m) => (typeof m === "string" ? m : JSON.stringify(m)) === key
3567
+ ))
3568
+ members.push(t);
3569
+ };
3570
+ push(a);
3571
+ push(b);
3572
+ if (members.length === 1) return members[0];
3573
+ return { kind: "union", types: members };
3539
3574
  }
3540
3575
  function narrow(...types) {
3541
3576
  if (types.length === 0) return "nothing";
@@ -3609,7 +3644,7 @@ function collectionElementType(type) {
3609
3644
  if (type.kind === "set") return type.elements;
3610
3645
  if (type.kind === "tuple") return widen(...type.elements.map((x) => x.type));
3611
3646
  if (type.kind === "dictionary")
3612
- return parseType(`tuple<string, ${type.values}>`);
3647
+ return parseType(`tuple<string, ${typeToString(type.values)}>`);
3613
3648
  if (type.kind === "record") {
3614
3649
  return parseType(
3615
3650
  `tuple<string, ${typeToString(widen(...Object.values(type.elements)))}>`
@@ -5511,15 +5546,16 @@ var DEFINITIONS_CORE = [
5511
5546
  precedence: ASSIGNMENT_PRECEDENCE,
5512
5547
  parse: parseAssign
5513
5548
  },
5514
- // General colon operator (type annotation, mapping notation)
5515
- // Precedence below assignment (260) so `:=` takes priority,
5516
- // and below arrows (270) so `f: A \to B` parses as `Colon(f, To(A, B))`
5549
+ // General colon operator (type annotation, mapping notation, Desmos piecewise)
5550
+ // Precedence below comparisons (245) so `cond : val` (Desmos compact piecewise)
5551
+ // parses as `Colon(cond, val)`, and below arrows (270) so
5552
+ // `f: A \to B` parses as `Colon(f, To(A, B))`.
5517
5553
  {
5518
5554
  name: "Colon",
5519
5555
  latexTrigger: ":",
5520
5556
  kind: "infix",
5521
5557
  associativity: "right",
5522
- precedence: 250,
5558
+ precedence: 240,
5523
5559
  serialize: (serializer, expr) => joinLatex([
5524
5560
  serializer.serialize(operand(expr, 1)),
5525
5561
  "\\colon",
@@ -5530,7 +5566,7 @@ var DEFINITIONS_CORE = [
5530
5566
  latexTrigger: "\\colon",
5531
5567
  kind: "infix",
5532
5568
  associativity: "right",
5533
- precedence: 250,
5569
+ precedence: 240,
5534
5570
  parse: "Colon"
5535
5571
  },
5536
5572
  {
@@ -7100,7 +7136,10 @@ function parseForComprehension(parser, lhs, until) {
7100
7136
  p.skipVisualSpace();
7101
7137
  const isComma = p.peek === ",";
7102
7138
  p.index = saved;
7103
- return isComma;
7139
+ if (isComma) return true;
7140
+ if (peekKeyword(p, "where")) return true;
7141
+ if (peekKeyword(p, "with")) return true;
7142
+ return false;
7104
7143
  }
7105
7144
  };
7106
7145
  const elements = [];
@@ -7141,6 +7180,25 @@ function parseWhereExpression(parser, lhs, until) {
7141
7180
  parser.skipVisualSpace();
7142
7181
  } while (parser.match(","));
7143
7182
  if (bindings.length === 0) return null;
7183
+ const forStart = parser.index;
7184
+ if (matchKeyword(parser, "for")) {
7185
+ const loop = parseForComprehension(parser, lhs, until);
7186
+ if (loop) {
7187
+ const block2 = [];
7188
+ for (const b of bindings) {
7189
+ const normalized = normalizeLocalAssign(b);
7190
+ if (operator(normalized) === "Assign") {
7191
+ block2.push(["Declare", operand(normalized, 1)]);
7192
+ block2.push(normalized);
7193
+ } else {
7194
+ block2.push(normalized);
7195
+ }
7196
+ }
7197
+ block2.push(loop);
7198
+ return ["Block", ...block2];
7199
+ }
7200
+ parser.index = forStart;
7201
+ }
7144
7202
  const block = [];
7145
7203
  for (const b of bindings) {
7146
7204
  const normalized = normalizeLocalAssign(b);
@@ -7354,6 +7412,17 @@ function parseIntervalBody(body, openLeft, openRight) {
7354
7412
  const upperExpr = openRight ? ["Open", upper] : upper;
7355
7413
  return ["Interval", lowerExpr, upperExpr];
7356
7414
  }
7415
+ var COMPARISON_HEADS = /* @__PURE__ */ new Set([
7416
+ "Less",
7417
+ "LessEqual",
7418
+ "Greater",
7419
+ "GreaterEqual",
7420
+ "Equal",
7421
+ "NotEqual",
7422
+ "And",
7423
+ "Or",
7424
+ "Not"
7425
+ ]);
7357
7426
  var DEFINITIONS_SETS = [
7358
7427
  //
7359
7428
  // Constants
@@ -7612,18 +7681,58 @@ var DEFINITIONS_SETS = [
7612
7681
  closeTrigger: "}",
7613
7682
  parse: (_parser, body) => {
7614
7683
  if (isEmptySequence(body)) return "EmptySet";
7684
+ if (operator(body) == "Delimiter" && stringValue(operand(body, 2)) === ",") {
7685
+ body = operand(body, 1);
7686
+ }
7615
7687
  const h = operator(body);
7616
- if (h === "Divides" || h === "Colon") {
7688
+ if (h === "Divides") {
7617
7689
  const expr = operand(body, 1);
7618
7690
  const condition = operand(body, 2);
7619
7691
  if (expr !== null && condition !== null)
7620
7692
  return ["Set", expr, ["Condition", condition]];
7621
7693
  }
7622
- if (operator(body) == "Delimiter" && stringValue(operand(body, 2)) === ",") {
7623
- body = operand(body, 1);
7694
+ if (h === "Colon") {
7695
+ const lhs = operand(body, 1);
7696
+ const rhs = operand(body, 2);
7697
+ if (lhs !== null && rhs !== null) {
7698
+ const lhsOp = operator(lhs);
7699
+ if (lhsOp !== null && COMPARISON_HEADS.has(lhsOp)) {
7700
+ return ["Which", lhs, rhs];
7701
+ }
7702
+ return ["Set", lhs, ["Condition", rhs]];
7703
+ }
7624
7704
  }
7625
- if (operator(body) !== "Sequence") return ["Set", body];
7626
- return ["Set", ...operands(body)];
7705
+ if (h === "Sequence") {
7706
+ const elements = operands(body);
7707
+ const colonElements = elements.filter((el) => operator(el) === "Colon");
7708
+ const allPiecewise = colonElements.length > 0 && colonElements.every((el) => {
7709
+ const lhs = operand(el, 1);
7710
+ const lhsOp = lhs !== null ? operator(lhs) : null;
7711
+ return lhsOp !== null && COMPARISON_HEADS.has(lhsOp);
7712
+ });
7713
+ if (allPiecewise) {
7714
+ const whichOps = [];
7715
+ for (let i = 0; i < elements.length; i++) {
7716
+ const el = elements[i];
7717
+ if (operator(el) === "Colon") {
7718
+ const cond = operand(el, 1);
7719
+ const val = operand(el, 2);
7720
+ if (cond === null || val === null) {
7721
+ return ["Set", ...elements];
7722
+ }
7723
+ whichOps.push(cond, val);
7724
+ } else {
7725
+ if (i !== elements.length - 1) {
7726
+ return ["Set", ...elements];
7727
+ }
7728
+ whichOps.push("True", el);
7729
+ }
7730
+ }
7731
+ return ["Which", ...whichOps];
7732
+ }
7733
+ return ["Set", ...elements];
7734
+ }
7735
+ return ["Set", body];
7627
7736
  },
7628
7737
  serialize: (serializer, expr) => {
7629
7738
  if (nops(expr) === 2 && operator(operand(expr, 2)) === "Condition") {
@@ -9597,7 +9706,8 @@ function parseTrig(op) {
9597
9706
  minPrec: MULTIPLICATION_PRECEDENCE,
9598
9707
  condition: (parser2) => trigCommands[parser2.peek] || (until?.condition?.(parser2) ?? false)
9599
9708
  });
9600
- const appliedFn = args === null ? fn : typeof fn === "string" ? [fn, ...args] : ["Apply", fn, ...args];
9709
+ const head = fn === "Arctan" && args?.length === 2 ? "Arctan2" : fn;
9710
+ const appliedFn = args === null ? fn : typeof head === "string" ? [head, ...args] : ["Apply", head, ...args];
9601
9711
  return sup === null ? appliedFn : ["Power", appliedFn, sup];
9602
9712
  };
9603
9713
  }
@@ -11720,10 +11830,17 @@ var DEFINITIONS_OTHERS = [
11720
11830
  // The capitalized library entries already exist; these are pure parse
11721
11831
  // aliases so the lowercase names don't land in `unsupported-operator`.
11722
11832
  // ---------------------------------------------------------------------------
11833
+ { latexTrigger: "\\operatorname{count}", parse: "Length" },
11723
11834
  { latexTrigger: "\\operatorname{random}", parse: "Random" },
11724
11835
  { latexTrigger: "\\operatorname{shuffle}", parse: "Shuffle" },
11725
11836
  { latexTrigger: "\\operatorname{repeat}", parse: "Repeat" },
11726
11837
  { latexTrigger: "\\operatorname{join}", parse: "Join" },
11838
+ { latexTrigger: "\\operatorname{range}", parse: "Range" },
11839
+ // Note: `\operatorname{with}` (Desmos's local-binding clause) is intentionally
11840
+ // NOT registered here. Use the math-notation equivalent `\operatorname{where}`
11841
+ // (with `\coloneq` for bindings), or register `with` as a custom dictionary
11842
+ // entry at the integration layer — see the "Desmos-Specific Syntax — Prefer
11843
+ // Custom LaTeX Dictionary" section in COMPUTE_ENGINE.md for a worked example.
11727
11844
  // ---------------------------------------------------------------------------
11728
11845
  // Geometric primitive heads. Registered as known typed heads so consumers
11729
11846
  // can branch on the operator name; CE itself doesn't render them. The
@@ -12914,6 +13031,15 @@ function interval(expr) {
12914
13031
  return void 0;
12915
13032
  }
12916
13033
 
13034
+ // src/compute-engine/numerics/random.ts
13035
+ function deterministicRandom(seed) {
13036
+ const v = Math.sin(seed * 12.9898) * 43758.5453;
13037
+ return v - Math.floor(v);
13038
+ }
13039
+ function nextSeed(seed) {
13040
+ return seed + 0.6180339887498949;
13041
+ }
13042
+
12917
13043
  // src/compute-engine/boxed-expression/canonical-utils.ts
12918
13044
  function canonical(ce, xs, scope) {
12919
13045
  if (xs.every((x) => x.isCanonical)) return xs;
@@ -12963,6 +13089,19 @@ var COLLECTIONS_LIBRARY = {
12963
13089
  indexWhere: void 0
12964
13090
  }
12965
13091
  },
13092
+ Length: {
13093
+ description: "Number of elements in a collection. Returns undefined for non-collections and for infinite collections.",
13094
+ complexity: 4e3,
13095
+ signature: "(any) -> integer",
13096
+ type: () => "integer",
13097
+ evaluate: ([xs], { engine }) => {
13098
+ if (!xs.isCollection) return void 0;
13099
+ if (xs.isEmptyCollection) return engine.Zero;
13100
+ const n = xs.count;
13101
+ if (n === void 0 || !isFinite(n)) return void 0;
13102
+ return engine.number(n);
13103
+ }
13104
+ },
12966
13105
  Tuple: {
12967
13106
  description: "A fixed number of heterogeneous elements",
12968
13107
  complexity: 8200,
@@ -13216,10 +13355,12 @@ var COLLECTIONS_LIBRARY = {
13216
13355
  const upper = expr.op2.re;
13217
13356
  let count = expr.op3.re;
13218
13357
  if (!isFinite(count)) count = DEFAULT_LINSPACE_COUNT;
13358
+ count = Math.floor(count);
13219
13359
  if (!isFinite(lower) || !isFinite(upper)) return void 0;
13220
13360
  if (index < 1 || index > count) return void 0;
13361
+ if (count === 1) return expr.engine.number(lower);
13221
13362
  return expr.engine.number(
13222
- lower + (upper - lower) * (index - 1) / count
13363
+ lower + (upper - lower) * (index - 1) / (count - 1)
13223
13364
  );
13224
13365
  },
13225
13366
  iterator: (expr) => {
@@ -13238,6 +13379,8 @@ var COLLECTIONS_LIBRARY = {
13238
13379
  !isFinite(expr.op3.re) ? DEFAULT_LINSPACE_COUNT : expr.op3.re
13239
13380
  );
13240
13381
  }
13382
+ totalCount = Math.floor(totalCount);
13383
+ const denom = totalCount > 1 ? totalCount - 1 : 1;
13241
13384
  let index = 1;
13242
13385
  return {
13243
13386
  next: () => {
@@ -13246,7 +13389,7 @@ var COLLECTIONS_LIBRARY = {
13246
13389
  index += 1;
13247
13390
  return {
13248
13391
  value: expr.engine.number(
13249
- lower + (upper - lower) * (index - 1 - 1) / totalCount
13392
+ lower + (upper - lower) * (index - 1 - 1) / denom
13250
13393
  ),
13251
13394
  done: false
13252
13395
  };
@@ -13262,9 +13405,14 @@ var COLLECTIONS_LIBRARY = {
13262
13405
  if (t < lower || t > upper) return false;
13263
13406
  let count = expr.op3.re;
13264
13407
  if (!isFinite(count)) count = DEFAULT_LINSPACE_COUNT;
13408
+ count = Math.floor(count);
13265
13409
  if (count === 0) return false;
13266
- const step = (upper - lower) / count;
13267
- return (t - lower) % step === 0;
13410
+ if (count === 1) return t === lower;
13411
+ const step = (upper - lower) / (count - 1);
13412
+ const k = (t - lower) / step;
13413
+ const tol = expr.engine.tolerance;
13414
+ const kRounded = Math.round(k);
13415
+ return kRounded >= 0 && kRounded <= count - 1 && Math.abs(k - kRounded) < tol;
13268
13416
  }
13269
13417
  }
13270
13418
  },
@@ -13589,10 +13737,12 @@ var COLLECTIONS_LIBRARY = {
13589
13737
  description: [
13590
13738
  "Access an element of an indexed collection.",
13591
13739
  "If the index is negative, it is counted from the end.",
13592
- "Multiple indices can be provided to access nested collections (e.g., matrices)."
13740
+ "Multiple indices can be provided to access nested collections (e.g., matrices).",
13741
+ "If the index is a finite collection of booleans, returns the elements where the mask is True.",
13742
+ "If the index is a finite collection of integers, returns the elements at those indices."
13593
13743
  ],
13594
13744
  complexity: 8200,
13595
- signature: "(value: indexed_collection, index: (number|string)+) -> unknown",
13745
+ signature: "(value: indexed_collection, index: (number|string|indexed_collection)+) -> unknown",
13596
13746
  type: ([xs]) => xs.operatorDefinition?.collection?.elttype?.(xs) ?? collectionElementType(xs.type.type) ?? "any",
13597
13747
  evaluate: (ops, { engine: ce }) => {
13598
13748
  let expr = ops[0];
@@ -13603,12 +13753,39 @@ var COLLECTIONS_LIBRARY = {
13603
13753
  if (!at) return void 0;
13604
13754
  const opAtIndex = ops[index];
13605
13755
  const s = isString(opAtIndex) ? opAtIndex.string : void 0;
13606
- if (s !== void 0) expr = at(expr, s) ?? ce.Nothing;
13607
- else {
13608
- const i = ops[index].re;
13609
- if (!Number.isInteger(i)) return void 0;
13610
- expr = at(expr, i) ?? ce.Nothing;
13756
+ if (s !== void 0) {
13757
+ expr = at(expr, s) ?? ce.Nothing;
13758
+ index += 1;
13759
+ continue;
13611
13760
  }
13761
+ if (opAtIndex.isCollection && opAtIndex.isFiniteCollection) {
13762
+ const indices = Array.from(opAtIndex.each());
13763
+ const isMask = indices.every((m) => {
13764
+ const name = sym(m);
13765
+ return name === "True" || name === "False";
13766
+ });
13767
+ const picked = [];
13768
+ if (isMask) {
13769
+ indices.forEach((m, i2) => {
13770
+ if (sym(m) !== "True") return;
13771
+ const v = at(expr, i2 + 1);
13772
+ if (v !== void 0) picked.push(v);
13773
+ });
13774
+ } else {
13775
+ for (const m of indices) {
13776
+ const k = m.re;
13777
+ if (!Number.isInteger(k)) return void 0;
13778
+ const v = at(expr, k);
13779
+ if (v !== void 0) picked.push(v);
13780
+ }
13781
+ }
13782
+ expr = ce._fn("List", picked);
13783
+ index += 1;
13784
+ continue;
13785
+ }
13786
+ const i = opAtIndex.re;
13787
+ if (!Number.isInteger(i)) return void 0;
13788
+ expr = at(expr, i) ?? ce.Nothing;
13612
13789
  index += 1;
13613
13790
  }
13614
13791
  return expr;
@@ -13619,7 +13796,7 @@ var COLLECTIONS_LIBRARY = {
13619
13796
  description: ["Return `n` elements from a collection."],
13620
13797
  complexity: 8200,
13621
13798
  signature: "(xs: indexed_collection, count: number) -> indexed_collection",
13622
- type: ([xs]) => `list<${collectionElementType(xs.type.type)}>`,
13799
+ type: ([xs]) => `list<${typeToString(collectionElementType(xs.type.type) ?? "any")}>`,
13623
13800
  evaluate: (ops, { engine, materialization: eager }) => {
13624
13801
  if (!eager) return void 0;
13625
13802
  const takeExpr = engine._fn("Take", ops);
@@ -13666,7 +13843,7 @@ var COLLECTIONS_LIBRARY = {
13666
13843
  description: ["Return the collection without the first n elements."],
13667
13844
  complexity: 8200,
13668
13845
  signature: "(xs: indexed_collection, count: number) -> indexed_collection",
13669
- type: ([xs]) => `list<${collectionElementType(xs.type.type)}>`,
13846
+ type: ([xs]) => `list<${typeToString(collectionElementType(xs.type.type) ?? "any")}>`,
13670
13847
  collection: {
13671
13848
  isLazy: (_expr) => true,
13672
13849
  count: (expr) => {
@@ -13862,7 +14039,9 @@ var COLLECTIONS_LIBRARY = {
13862
14039
  ],
13863
14040
  complexity: 8200,
13864
14041
  signature: "(value: indexed_collection, start: number, end: number) -> list",
13865
- type: ([xs]) => parseType(`list<${collectionElementType(xs.type.type)}>`),
14042
+ type: ([xs]) => parseType(
14043
+ `list<${typeToString(collectionElementType(xs.type.type) ?? "any")}>`
14044
+ ),
13866
14045
  collection: {
13867
14046
  isLazy: (_expr) => true,
13868
14047
  count: (expr) => {
@@ -14194,16 +14373,26 @@ var COLLECTIONS_LIBRARY = {
14194
14373
  },
14195
14374
  // Randomize the order of the elements in the collection.
14196
14375
  Shuffle: {
14197
- description: "Randomize the order of the elements in the collection.",
14376
+ description: "Randomize the order of the elements in the collection. With an optional `seed` argument, the shuffle is deterministic.",
14198
14377
  complexity: 8200,
14199
- signature: "(indexed_collection) -> indexed_collection",
14378
+ signature: "(indexed_collection, real?) -> indexed_collection",
14200
14379
  type: (ops) => ops[0].type,
14201
- evaluate: ([xs], { engine: ce }) => {
14380
+ evaluate: ([xs, seedOp], { engine: ce }) => {
14202
14381
  if (!xs.isFiniteCollection) return void 0;
14203
14382
  const data = Array.from(xs.each());
14204
- for (let i = data.length - 1; i > 0; i--) {
14205
- const j = Math.floor(Math.random() * (i + 1));
14206
- [data[i], data[j]] = [data[j], data[i]];
14383
+ const seed = seedOp?.re;
14384
+ if (seed !== void 0 && !Number.isNaN(seed)) {
14385
+ let s = seed;
14386
+ for (let i = data.length - 1; i > 0; i--) {
14387
+ const j = Math.floor(deterministicRandom(s) * (i + 1));
14388
+ [data[i], data[j]] = [data[j], data[i]];
14389
+ s = nextSeed(s);
14390
+ }
14391
+ } else {
14392
+ for (let i = data.length - 1; i > 0; i--) {
14393
+ const j = Math.floor(Math.random() * (i + 1));
14394
+ [data[i], data[j]] = [data[j], data[i]];
14395
+ }
14207
14396
  }
14208
14397
  return ce.function(xs.operator, data);
14209
14398
  }
@@ -14270,7 +14459,9 @@ var COLLECTIONS_LIBRARY = {
14270
14459
  if (t === "string")
14271
14460
  return parseType(`tuple<list<string>, list<integer>>`);
14272
14461
  return parseType(
14273
- `tuple<list<${collectionElementType(t)}>, list<integer>>`
14462
+ `tuple<list<${typeToString(
14463
+ collectionElementType(t) ?? "any"
14464
+ )}>, list<integer>>`
14274
14465
  );
14275
14466
  },
14276
14467
  evaluate: (ops, { engine: ce }) => {
@@ -14286,7 +14477,7 @@ var COLLECTIONS_LIBRARY = {
14286
14477
  description: "Return a list of the unique elements of the collection.",
14287
14478
  complexity: 8200,
14288
14479
  signature: "(collection) -> list",
14289
- type: ([xs]) => `list<${collectionElementType(xs.type.type)}>`,
14480
+ type: ([xs]) => `list<${typeToString(collectionElementType(xs.type.type) ?? "any")}>`,
14290
14481
  evaluate: (ops, { engine: ce }) => {
14291
14482
  if (!ops[0].isFiniteCollection) return void 0;
14292
14483
  const [values, _counts] = tally(ops[0]);
@@ -14298,7 +14489,7 @@ var COLLECTIONS_LIBRARY = {
14298
14489
  wikidata: "Q381060",
14299
14490
  complexity: 8200,
14300
14491
  signature: "(collection, integer | function) -> list",
14301
- type: ([xs]) => `list<${collectionElementType(xs.type.type)}>`,
14492
+ type: ([xs]) => `list<${typeToString(collectionElementType(xs.type.type) ?? "any")}>`,
14302
14493
  evaluate: ([xs, arg], { engine: ce }) => {
14303
14494
  if (!xs.isFiniteCollection) return void 0;
14304
14495
  const k = toInteger(arg);
@@ -14472,32 +14663,74 @@ var COLLECTIONS_LIBRARY = {
14472
14663
  }
14473
14664
  }
14474
14665
  },
14475
- // Repeat(x) -> [x, x, ...]
14476
- // This is an infinite series. Can use Take(Repeat(x), n) to get a finite series
14477
- // x is evaluated once. Although could use Hold()?
14478
- // So that First(Repeat(Hold(Random(5))), 10) would return 10 random numbers...
14666
+ // Repeat(x) -> [x, x, ...] — infinite sequence
14667
+ // Repeat(x, n) -> [x, x, ..., x] — finite list of n copies
14479
14668
  Repeat: {
14480
- description: "Produce an infinite sequence by repeating a single value.",
14669
+ description: "Produce a sequence by repeating a single value. With 1 argument, returns an infinite sequence; with 2 arguments (value, count), returns a finite list of `count` copies.",
14481
14670
  complexity: 8200,
14482
- signature: "(value: any) -> list",
14671
+ signature: "(value: any, count: integer?) -> list",
14672
+ evaluate: (ops, { engine }) => {
14673
+ if (ops.length !== 2) return void 0;
14674
+ const raw = toInteger(ops[1]);
14675
+ if (raw === null) return void 0;
14676
+ const n = Math.max(0, raw);
14677
+ if (n > engine.maxCollectionSize) return void 0;
14678
+ return engine._fn("List", Array(n).fill(ops[0]));
14679
+ },
14483
14680
  collection: {
14484
- isLazy: (_expr) => true,
14485
- count: () => Infinity,
14486
- isEmpty: (_expr) => false,
14487
- // Never empty
14488
- isFinite: () => false,
14489
- // Infinite collection
14681
+ isLazy: (expr) => isFunction2(expr) && expr.ops?.length === 1,
14682
+ count: (expr) => {
14683
+ if (!isFunction2(expr)) return void 0;
14684
+ if (expr.ops?.length === 2) {
14685
+ const n = toInteger(expr.op2);
14686
+ return n !== null ? Math.max(0, n) : void 0;
14687
+ }
14688
+ return Infinity;
14689
+ },
14690
+ isEmpty: (expr) => {
14691
+ if (!isFunction2(expr)) return void 0;
14692
+ if (expr.ops?.length === 2) {
14693
+ const n = toInteger(expr.op2);
14694
+ return n !== null ? n <= 0 : void 0;
14695
+ }
14696
+ return false;
14697
+ },
14698
+ isFinite: (expr) => isFunction2(expr) && expr.ops?.length === 2,
14490
14699
  contains: (expr, target) => {
14491
14700
  if (!isFunction2(expr)) return false;
14701
+ if (expr.ops?.length === 2) {
14702
+ const n = toInteger(expr.op2);
14703
+ if (n !== null && n <= 0) return false;
14704
+ }
14492
14705
  return expr.op1.isSame(target);
14493
14706
  },
14494
14707
  iterator: (expr) => {
14495
14708
  if (!isFunction2(expr))
14496
14709
  return { next: () => ({ value: void 0, done: true }) };
14710
+ if (expr.ops?.length === 2) {
14711
+ const n = toInteger(expr.op2);
14712
+ if (n === null) {
14713
+ return { next: () => ({ value: void 0, done: true }) };
14714
+ }
14715
+ const count = Math.max(0, n);
14716
+ let i = 0;
14717
+ return {
14718
+ next: () => i++ < count ? { value: expr.op1, done: false } : { value: void 0, done: true }
14719
+ };
14720
+ }
14497
14721
  return { next: () => ({ value: expr.op1, done: false }) };
14498
14722
  },
14499
- at: (expr, _index) => {
14723
+ // at is 1-based (consistent with Range, Take, and other collection handlers)
14724
+ at: (expr, index) => {
14500
14725
  if (!isFunction2(expr)) return void 0;
14726
+ if (typeof index !== "number") return void 0;
14727
+ if (expr.ops?.length === 2) {
14728
+ const n = toInteger(expr.op2);
14729
+ const count = n !== null ? Math.max(0, n) : 0;
14730
+ if (index < 1 || index > count) return void 0;
14731
+ } else {
14732
+ if (index < 1) return void 0;
14733
+ }
14501
14734
  return expr.op1;
14502
14735
  }
14503
14736
  }
@@ -15206,6 +15439,13 @@ var BaseCompiler = class _BaseCompiler {
15206
15439
  * GLSL (no dynamic arrays, no push). A compile-time error is thrown.
15207
15440
  * TODO(E3-GLSL): support GLSL multi-Element via a pre-declared fixed-size
15208
15441
  * array or by unrolling when bounds are known at compile time.
15442
+ *
15443
+ * Known issue (imperative form): the IIFE generated by form (1) has no
15444
+ * `return` statement, so `Loop(body, Element(i, Range(lo, hi)))` compiled
15445
+ * to JS evaluates to `undefined` at runtime, while CE evaluation returns a
15446
+ * `List` of body values. See `test/compute-engine/a1-c1-compile-parity.test.ts`
15447
+ * ("Loop compiles in JS") for the verify-only test that locks in the
15448
+ * current behavior.
15209
15449
  */
15210
15450
  static compileForLoop(args, target) {
15211
15451
  if (!args[0]) throw new Error("Loop: no body");
@@ -20293,6 +20533,7 @@ var JAVASCRIPT_FUNCTIONS = {
20293
20533
  return `_SYS.cexp(${compile2(args[0])})`;
20294
20534
  return `Math.exp(${compile2(args[0])})`;
20295
20535
  },
20536
+ First: (args, compile2) => `${compile2(args[0])}[0]`,
20296
20537
  Floor: (args, compile2) => {
20297
20538
  if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
20298
20539
  return `Math.floor(${compile2(args[0])})`;
@@ -20451,7 +20692,20 @@ var JAVASCRIPT_FUNCTIONS = {
20451
20692
  if (nConst !== void 0) return `Math.pow(${compile2(arg)}, ${1 / nConst})`;
20452
20693
  return `Math.pow(${compile2(arg)}, 1 / (${compile2(exp3)}))`;
20453
20694
  },
20454
- Random: "Math.random",
20695
+ Random: (args, compile2) => {
20696
+ if (args.length === 0) return "Math.random()";
20697
+ if (args.length === 2) {
20698
+ const m = compile2(args[0]);
20699
+ const n = compile2(args[1]);
20700
+ return `((${m}) + Math.floor(Math.random() * ((${n}) - (${m}))))`;
20701
+ }
20702
+ const arg = args[0];
20703
+ if (BaseCompiler.isIntegerValued(arg)) {
20704
+ return `Math.floor(Math.random() * (${compile2(arg)}))`;
20705
+ }
20706
+ const a = compile2(arg);
20707
+ return `(() => { const _s = (${a}) * 12.9898; const _v = Math.sin(_s) * 43758.5453; return _v - Math.floor(_v); })()`;
20708
+ },
20455
20709
  Round: (args, compile2) => {
20456
20710
  if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
20457
20711
  return `Math.round(${compile2(args[0])})`;
@@ -20479,6 +20733,7 @@ var JAVASCRIPT_FUNCTIONS = {
20479
20733
  if (BaseCompiler.isComplexValued(arg)) return `_SYS.csech(${compile2(arg)})`;
20480
20734
  return `1 / Math.cosh(${compile2(arg)})`;
20481
20735
  },
20736
+ Second: (args, compile2) => `${compile2(args[0])}[1]`,
20482
20737
  Heaviside: "_SYS.heaviside",
20483
20738
  Sign: "Math.sign",
20484
20739
  Sinc: "_SYS.sinc",
@@ -20511,6 +20766,7 @@ var JAVASCRIPT_FUNCTIONS = {
20511
20766
  return `_SYS.ctanh(${compile2(args[0])})`;
20512
20767
  return `Math.tanh(${compile2(args[0])})`;
20513
20768
  },
20769
+ Third: (args, compile2) => `${compile2(args[0])}[2]`,
20514
20770
  Mod: ([a, b], compile2) => {
20515
20771
  if (a === null || b === null) throw new Error("Mod: missing argument");
20516
20772
  const ca = compile2(a);
@@ -21790,6 +22046,14 @@ var GPU_FUNCTIONS = {
21790
22046
  return `exp(${compile2(args[0])})`;
21791
22047
  },
21792
22048
  Exp2: "exp2",
22049
+ // Component access — assumes the argument compiles to a vec2/vec3/vec4
22050
+ // (the common case for 2D/3D points). For 5+-element tuples that compile
22051
+ // to `float[N]` arrays, swizzle access is invalid GLSL and the shader
22052
+ // will fail to compile; that's an edge case `First`/`Second`/`Third`
22053
+ // aren't designed for. Vec swizzles are identical between GLSL and WGSL.
22054
+ First: (args, compile2) => `${compile2(args[0])}.x`,
22055
+ Second: (args, compile2) => `${compile2(args[0])}.y`,
22056
+ Third: (args, compile2) => `${compile2(args[0])}.z`,
21793
22057
  Floor: (args, compile2) => {
21794
22058
  if (BaseCompiler.isIntegerValued(args[0])) return compile2(args[0]);
21795
22059
  return `floor(${compile2(args[0])})`;
@@ -22267,6 +22531,39 @@ var GPU_FUNCTIONS = {
22267
22531
  // Sum/Product — unrolled or for-loop
22268
22532
  Sum: (args, compile2, target) => compileGPUSumProduct("Sum", args, compile2, target),
22269
22533
  Product: (args, compile2, target) => compileGPUSumProduct("Product", args, compile2, target),
22534
+ // Range — inline constant array literal (bounds must be compile-time constants)
22535
+ Range: (args, _compile, target) => {
22536
+ if (args.length < 2 || args.length > 3) {
22537
+ throw new Error(
22538
+ "Range: GPU compile expects 2 or 3 arguments (lo, hi, step?)"
22539
+ );
22540
+ }
22541
+ const lo = args[0].re;
22542
+ const hi = args[1].re;
22543
+ const step = args.length === 3 ? args[2].re : 1;
22544
+ if (!Number.isFinite(lo) || !Number.isFinite(hi) || !Number.isFinite(step)) {
22545
+ throw new Error(
22546
+ "Range: GPU compile requires constant numeric bounds (non-constant ranges must be materialized at JS host then uploaded as a uniform)"
22547
+ );
22548
+ }
22549
+ if (step === 0) throw new Error("Range: step cannot be zero");
22550
+ const count = Math.max(0, Math.floor((hi - lo) / step) + 1);
22551
+ if (count === 0) {
22552
+ throw new Error(
22553
+ "Range: empty range (lo > hi for positive step, or lo < hi for negative step)"
22554
+ );
22555
+ }
22556
+ if (count > 256) {
22557
+ throw new Error(
22558
+ `Range: GPU compile inlines ranges up to 256 elements (got ${count})`
22559
+ );
22560
+ }
22561
+ const values = [];
22562
+ for (let i = 0; i < count; i++) values.push(lo + i * step);
22563
+ const isWGSL = target.language === "wgsl";
22564
+ const arrayType = isWGSL ? `array<f32, ${count}>` : `float[${count}]`;
22565
+ return `${arrayType}(${values.map(formatGPUNumber).join(", ")})`;
22566
+ },
22270
22567
  // Loop — GPU for-loop (no IIFE, no let)
22271
22568
  Loop: (args, _compile, target) => {
22272
22569
  if (!args[0]) throw new Error("Loop: no body");
@@ -22295,6 +22592,134 @@ var GPU_FUNCTIONS = {
22295
22592
  ${bodyCode};
22296
22593
  }`;
22297
22594
  },
22595
+ // Statistical functions
22596
+ /**
22597
+ * GCD of two scalar arguments.
22598
+ *
22599
+ * Uses a preamble helper `_gpu_gcd` (Euclidean algorithm via `mod`).
22600
+ * Only two-argument form is supported in GPU targets.
22601
+ */
22602
+ GCD: (args, compile2) => {
22603
+ if (args.length < 2) throw new Error("GCD: need at least two arguments");
22604
+ if (args.length > 2)
22605
+ throw new Error("GCD: GPU target supports only two-argument GCD");
22606
+ const a = args[0];
22607
+ const b = args[1];
22608
+ if (a === null || b === null) throw new Error("GCD: missing argument");
22609
+ return `_gpu_gcd(${compile2(a)}, ${compile2(b)})`;
22610
+ },
22611
+ /**
22612
+ * Variance of a compile-time-known list.
22613
+ *
22614
+ * Accepts either a single `List(...)` argument or N scalar arguments.
22615
+ * Generates fully inline code: computes mean then sum of squared deviations,
22616
+ * divided by (N-1) for sample variance (matches JS `_SYS.variance`).
22617
+ */
22618
+ Variance: (args, compile2) => {
22619
+ let elems;
22620
+ if (args.length === 1 && isFunction2(args[0], "List")) {
22621
+ elems = args[0].ops;
22622
+ } else if (args.length >= 2) {
22623
+ elems = args;
22624
+ } else {
22625
+ throw new Error(
22626
+ "Variance: GPU target requires a List argument or at least 2 scalar arguments"
22627
+ );
22628
+ }
22629
+ const n = elems.length;
22630
+ if (n < 2) throw new Error("Variance: need at least 2 elements");
22631
+ const compiled = elems.map((e) => compile2(e));
22632
+ const sum = compiled.join(" + ");
22633
+ const mean2 = `((${sum}) / ${formatGPUNumber(n)})`;
22634
+ const sqDiffs = compiled.map((c) => `(${c} - ${mean2}) * (${c} - ${mean2})`).join(" + ");
22635
+ return `((${sqDiffs}) / ${formatGPUNumber(n - 1)})`;
22636
+ },
22637
+ /**
22638
+ * Median of a compile-time-known list.
22639
+ *
22640
+ * Accepts either a single `List(...)` argument or N scalar arguments.
22641
+ * For N ≤ 8: generates a fully unrolled inline sorting network followed by
22642
+ * a middle-element pick. For larger N, throws (too large to inline cleanly).
22643
+ *
22644
+ * The sorting network uses the "odd-even merge sort" comparator pattern
22645
+ * inlined as `min`/`max` calls — no GPU statements required.
22646
+ */
22647
+ Median: (args, compile2) => {
22648
+ let elems;
22649
+ if (args.length === 1 && isFunction2(args[0], "List")) {
22650
+ elems = args[0].ops;
22651
+ } else if (args.length >= 1) {
22652
+ elems = args;
22653
+ } else {
22654
+ throw new Error(
22655
+ "Median: GPU target requires a List argument or at least 1 scalar argument"
22656
+ );
22657
+ }
22658
+ const n = elems.length;
22659
+ if (n === 0) throw new Error("Median: empty list");
22660
+ if (n > 8) {
22661
+ throw new Error(
22662
+ `Median: GPU target supports up to 8 elements via inline sorting network (got ${n}). For larger lists, compute on the CPU and pass the result as a uniform.`
22663
+ );
22664
+ }
22665
+ const compiled = elems.map((e) => compile2(e));
22666
+ if (n === 1) return compiled[0];
22667
+ return `_gpu_median_${n}(${compiled.join(", ")})`;
22668
+ },
22669
+ /**
22670
+ * Deterministic pseudorandom for GPU.
22671
+ *
22672
+ * All emitted forms return a GLSL `float` (or WGSL `f32`) so the result
22673
+ * composes with surrounding float arithmetic without explicit casts. The
22674
+ * "integer-bound" forms return an integer-valued float (the result of
22675
+ * `floor`), matching the convention used by `Floor` and other ostensibly
22676
+ * integer-returning operators in this target.
22677
+ *
22678
+ * - 0 args (GLSL only): fall back to a fragment-coord-derived seed.
22679
+ * Only meaningful in fragment shaders (gl_FragCoord is FS-only).
22680
+ * - 0 args (WGSL): throws — WGSL has no built-in fragment coordinate;
22681
+ * caller must provide an explicit seed.
22682
+ * - 1 arg, real-typed: `_gpu_random(seed)` — deterministic float in [0, 1)
22683
+ * - 1 arg, integer-typed: `floor(_gpu_random(float(n)) * float(n))` —
22684
+ * integer-valued float in {0, 1, ..., n-1}. The seed is derived from
22685
+ * `n` itself, so the result is per-pixel-and-n deterministic in GLSL.
22686
+ * - 2 args (integer m, n): float in [m, n), seeded from gl_FragCoord.
22687
+ *
22688
+ * JS-side `Random` has matching semantics (see `library/core.ts`'s
22689
+ * polymorphic dispatch). JS↔GLSL parity is approximate — same seed yields
22690
+ * a similar value, not bit-identical, due to fp64 vs fp32 and platform
22691
+ * `sin` differences.
22692
+ */
22693
+ Random: (args, compile2, target) => {
22694
+ if (args.length === 0) {
22695
+ if (target.language === "wgsl") {
22696
+ throw new Error(
22697
+ "Random(): WGSL compile requires an explicit seed argument. WGSL has no gl_FragCoord built-in outside fragment entry points, so the no-arg fallback used in GLSL is unavailable. Use Random(seed) where seed is a deterministic per-invocation value."
22698
+ );
22699
+ }
22700
+ return "_gpu_random(gl_FragCoord.x + gl_FragCoord.y * 1024.0)";
22701
+ }
22702
+ if (args.length === 1) {
22703
+ const arg = args[0];
22704
+ if (BaseCompiler.isIntegerValued(arg)) {
22705
+ const compiled = compile2(arg);
22706
+ return `floor(_gpu_random(float(${compiled})) * float(${compiled}))`;
22707
+ }
22708
+ return `_gpu_random(${compile2(arg)})`;
22709
+ }
22710
+ if (args.length === 2) {
22711
+ if (target.language === "wgsl") {
22712
+ throw new Error(
22713
+ "Random(m, n): WGSL compile requires explicit seeding. Use a seeded variant or compute the integer range manually."
22714
+ );
22715
+ }
22716
+ const m = compile2(args[0]);
22717
+ const n = compile2(args[1]);
22718
+ const seed = "_gpu_random(gl_FragCoord.x + gl_FragCoord.y * 1024.0)";
22719
+ return `(float(${m}) + floor(${seed} * float((${n}) - (${m}))))`;
22720
+ }
22721
+ throw new Error("Random: GPU compile expects 0, 1, or 2 arguments");
22722
+ },
22298
22723
  // Function (lambda) — not supported in GPU
22299
22724
  Function: () => {
22300
22725
  throw new Error(
@@ -22894,6 +23319,212 @@ fn _fractal_julia(z_in: vec2f, c: vec2f, maxIter: i32) -> f32 {
22894
23319
  return 1.0;
22895
23320
  }
22896
23321
  `;
23322
+ var GPU_GCD_PREAMBLE_GLSL = `
23323
+ float _gpu_gcd(float a, float b) {
23324
+ a = abs(a); b = abs(b);
23325
+ for (int i = 0; i < 32; i++) {
23326
+ if (b < 0.5) break;
23327
+ float t = mod(a, b);
23328
+ a = b;
23329
+ b = t;
23330
+ }
23331
+ return a;
23332
+ }
23333
+ `;
23334
+ var GPU_GCD_PREAMBLE_WGSL = `
23335
+ fn _gpu_gcd(a_in: f32, b_in: f32) -> f32 {
23336
+ var a = abs(a_in); var b = abs(b_in);
23337
+ for (var i: i32 = 0; i < 32; i++) {
23338
+ if (b < 0.5) { break; }
23339
+ let t = a % b;
23340
+ a = b;
23341
+ b = t;
23342
+ }
23343
+ return a;
23344
+ }
23345
+ `;
23346
+ var GPU_RANDOM_PREAMBLE_GLSL = `
23347
+ // Deterministic pseudorandom in [0, 1) from a float seed.
23348
+ // Standard fract-sin hash; reproducible across runs for the same seed.
23349
+ // Note: this hash exhibits visible banding near seed \u2248 k\u03C0 for integer k.
23350
+ // For high-quality shader random, callers should use a more robust hash
23351
+ // (e.g. PCG or xxHash) and pre-seed it appropriately.
23352
+ float _gpu_random(float seed) {
23353
+ return fract(sin(seed * 12.9898) * 43758.5453);
23354
+ }
23355
+ `;
23356
+ var GPU_RANDOM_PREAMBLE_WGSL = `
23357
+ // Deterministic pseudorandom in [0, 1) from a float seed.
23358
+ // Standard fract-sin hash; reproducible across runs for the same seed.
23359
+ // Note: this hash exhibits visible banding near seed \u2248 k\u03C0 for integer k.
23360
+ // For high-quality shader random, callers should use a more robust hash
23361
+ // (e.g. PCG or xxHash) and pre-seed it appropriately.
23362
+ fn _gpu_random(seed: f32) -> f32 {
23363
+ return fract(sin(seed * 12.9898) * 43758.5453);
23364
+ }
23365
+ `;
23366
+ var GPU_MEDIAN_PREAMBLE_GLSL = `
23367
+ float _gpu_median_2(float a, float b) {
23368
+ return (a + b) * 0.5;
23369
+ }
23370
+ float _gpu_median_3(float a, float b, float c) {
23371
+ return max(min(a, b), min(max(a, b), c));
23372
+ }
23373
+ float _gpu_median_4(float a, float b, float c, float d) {
23374
+ float lo = max(min(a, b), min(c, d));
23375
+ float hi = min(max(a, b), max(c, d));
23376
+ return (lo + hi) * 0.5;
23377
+ }
23378
+ float _gpu_median_5(float a, float b, float c, float d, float e) {
23379
+ // 9-comparator Bose-Nelson sort; v2 holds the median.
23380
+ float t; float v0=a,v1=b,v2=c,v3=d,v4=e;
23381
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23382
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23383
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23384
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23385
+ t=min(v0,v3); v3=max(v0,v3); v0=t;
23386
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23387
+ t=min(v1,v4); v4=max(v1,v4); v1=t;
23388
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23389
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23390
+ return v2;
23391
+ }
23392
+ float _gpu_median_6(float a, float b, float c, float d, float e, float f) {
23393
+ float t; float v0=a,v1=b,v2=c,v3=d,v4=e,v5=f;
23394
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23395
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23396
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23397
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23398
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23399
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23400
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23401
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23402
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23403
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23404
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23405
+ return (v2 + v3) * 0.5;
23406
+ }
23407
+ float _gpu_median_7(float a, float b, float c, float d, float e, float f, float g) {
23408
+ float t; float v0=a,v1=b,v2=c,v3=d,v4=e,v5=f,v6=g;
23409
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23410
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23411
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23412
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23413
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23414
+ t=min(v4,v6); v6=max(v4,v6); v4=t;
23415
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23416
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23417
+ t=min(v2,v6); v6=max(v2,v6); v2=t;
23418
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23419
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23420
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23421
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23422
+ return v3;
23423
+ }
23424
+ float _gpu_median_8(float a, float b, float c, float d, float e, float f, float g, float h) {
23425
+ float t; float v0=a,v1=b,v2=c,v3=d,v4=e,v5=f,v6=g,v7=h;
23426
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23427
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23428
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23429
+ t=min(v6,v7); v7=max(v6,v7); v6=t;
23430
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23431
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23432
+ t=min(v4,v6); v6=max(v4,v6); v4=t;
23433
+ t=min(v5,v7); v7=max(v5,v7); v5=t;
23434
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23435
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23436
+ t=min(v2,v6); v6=max(v2,v6); v2=t;
23437
+ t=min(v3,v7); v7=max(v3,v7); v3=t;
23438
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23439
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23440
+ t=min(v5,v6); v6=max(v5,v6); v5=t;
23441
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23442
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23443
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23444
+ return (v3 + v4) * 0.5;
23445
+ }
23446
+ `;
23447
+ var GPU_MEDIAN_PREAMBLE_WGSL = `
23448
+ fn _gpu_median_2(a: f32, b: f32) -> f32 {
23449
+ return (a + b) * 0.5;
23450
+ }
23451
+ fn _gpu_median_3(a: f32, b: f32, c: f32) -> f32 {
23452
+ return max(min(a, b), min(max(a, b), c));
23453
+ }
23454
+ fn _gpu_median_4(a: f32, b: f32, c: f32, d: f32) -> f32 {
23455
+ let lo = max(min(a, b), min(c, d));
23456
+ let hi = min(max(a, b), max(c, d));
23457
+ return (lo + hi) * 0.5;
23458
+ }
23459
+ fn _gpu_median_5(a: f32, b: f32, c: f32, d: f32, e: f32) -> f32 {
23460
+ // 9-comparator Bose-Nelson sort; v2 holds the median.
23461
+ var v0=a; var v1=b; var v2=c; var v3=d; var v4=e; var t: f32;
23462
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23463
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23464
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23465
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23466
+ t=min(v0,v3); v3=max(v0,v3); v0=t;
23467
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23468
+ t=min(v1,v4); v4=max(v1,v4); v1=t;
23469
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23470
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23471
+ return v2;
23472
+ }
23473
+ fn _gpu_median_6(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> f32 {
23474
+ var v0=a; var v1=b; var v2=c; var v3=d; var v4=e; var v5=f; var t: f32;
23475
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23476
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23477
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23478
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23479
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23480
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23481
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23482
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23483
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23484
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23485
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23486
+ return (v2 + v3) * 0.5;
23487
+ }
23488
+ fn _gpu_median_7(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32, g: f32) -> f32 {
23489
+ var v0=a; var v1=b; var v2=c; var v3=d; var v4=e; var v5=f; var v6=g; var t: f32;
23490
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23491
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23492
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23493
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23494
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23495
+ t=min(v4,v6); v6=max(v4,v6); v4=t;
23496
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23497
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23498
+ t=min(v2,v6); v6=max(v2,v6); v2=t;
23499
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23500
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23501
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23502
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23503
+ return v3;
23504
+ }
23505
+ fn _gpu_median_8(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32, g: f32, h: f32) -> f32 {
23506
+ var v0=a; var v1=b; var v2=c; var v3=d; var v4=e; var v5=f; var v6=g; var v7=h; var t: f32;
23507
+ t=min(v0,v1); v1=max(v0,v1); v0=t;
23508
+ t=min(v2,v3); v3=max(v2,v3); v2=t;
23509
+ t=min(v4,v5); v5=max(v4,v5); v4=t;
23510
+ t=min(v6,v7); v7=max(v6,v7); v6=t;
23511
+ t=min(v0,v2); v2=max(v0,v2); v0=t;
23512
+ t=min(v1,v3); v3=max(v1,v3); v1=t;
23513
+ t=min(v4,v6); v6=max(v4,v6); v4=t;
23514
+ t=min(v5,v7); v7=max(v5,v7); v5=t;
23515
+ t=min(v0,v4); v4=max(v0,v4); v0=t;
23516
+ t=min(v1,v5); v5=max(v1,v5); v1=t;
23517
+ t=min(v2,v6); v6=max(v2,v6); v2=t;
23518
+ t=min(v3,v7); v7=max(v3,v7); v3=t;
23519
+ t=min(v1,v2); v2=max(v1,v2); v1=t;
23520
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23521
+ t=min(v5,v6); v6=max(v5,v6); v5=t;
23522
+ t=min(v3,v5); v5=max(v3,v5); v3=t;
23523
+ t=min(v2,v4); v4=max(v2,v4); v2=t;
23524
+ t=min(v3,v4); v4=max(v3,v4); v3=t;
23525
+ return (v3 + v4) * 0.5;
23526
+ }
23527
+ `;
22897
23528
  var GPU_COLOR_PREAMBLE_GLSL = `
22898
23529
  float _gpu_srgb_to_linear(float c) {
22899
23530
  if (c <= 0.04045) return c / 12.92;
@@ -23571,6 +24202,12 @@ var GPUShaderTarget = class {
23571
24202
  if (code.includes("_fractal_")) {
23572
24203
  preamble += this.languageId === "wgsl" ? GPU_FRACTAL_PREAMBLE_WGSL : GPU_FRACTAL_PREAMBLE_GLSL;
23573
24204
  }
24205
+ if (code.includes("_gpu_random"))
24206
+ preamble += this.languageId === "wgsl" ? GPU_RANDOM_PREAMBLE_WGSL : GPU_RANDOM_PREAMBLE_GLSL;
24207
+ if (code.includes("_gpu_gcd"))
24208
+ preamble += this.languageId === "wgsl" ? GPU_GCD_PREAMBLE_WGSL : GPU_GCD_PREAMBLE_GLSL;
24209
+ if (code.includes("_gpu_median_"))
24210
+ preamble += this.languageId === "wgsl" ? GPU_MEDIAN_PREAMBLE_WGSL : GPU_MEDIAN_PREAMBLE_GLSL;
23574
24211
  if (code.includes("_gpu_srgb_to") || code.includes("_gpu_oklab") || code.includes("_gpu_oklch") || code.includes("_gpu_color_mix") || code.includes("_gpu_apca")) {
23575
24212
  preamble += this.languageId === "wgsl" ? GPU_COLOR_PREAMBLE_WGSL : GPU_COLOR_PREAMBLE_GLSL;
23576
24213
  }
@@ -25909,7 +26546,7 @@ function compileToIntervalTarget(expr, target) {
25909
26546
  }
25910
26547
 
25911
26548
  // src/compile.ts
25912
- var version = "0.57.0";
26549
+ var version = "0.58.0";
25913
26550
  export {
25914
26551
  BaseCompiler,
25915
26552
  GLSLTarget,