@cortex-js/compute-engine 0.55.0 → 0.55.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/dist/compile.esm.js +2498 -2108
  2. package/dist/compile.min.esm.js +48 -48
  3. package/dist/compile.min.umd.cjs +49 -49
  4. package/dist/compile.umd.cjs +2498 -2108
  5. package/dist/compute-engine.esm.js +1219 -719
  6. package/dist/compute-engine.min.esm.js +73 -73
  7. package/dist/compute-engine.min.umd.cjs +73 -73
  8. package/dist/compute-engine.umd.cjs +1219 -719
  9. package/dist/core.esm.js +1223 -715
  10. package/dist/core.min.esm.js +72 -72
  11. package/dist/core.min.umd.cjs +72 -72
  12. package/dist/core.umd.cjs +1223 -715
  13. package/dist/interval.esm.js +1564 -1412
  14. package/dist/interval.min.esm.js +6 -6
  15. package/dist/interval.min.umd.cjs +6 -6
  16. package/dist/interval.umd.cjs +1564 -1412
  17. package/dist/latex-syntax.esm.js +673 -492
  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 +673 -492
  21. package/dist/math-json.esm.js +2 -2
  22. package/dist/math-json.min.esm.js +2 -2
  23. package/dist/math-json.min.umd.cjs +2 -2
  24. package/dist/math-json.umd.cjs +2 -2
  25. package/dist/numerics.esm.js +2 -2
  26. package/dist/numerics.min.esm.js +2 -2
  27. package/dist/numerics.min.umd.cjs +2 -2
  28. package/dist/numerics.umd.cjs +2 -2
  29. package/dist/types/big-decimal/big-decimal.d.ts +1 -1
  30. package/dist/types/big-decimal/index.d.ts +1 -1
  31. package/dist/types/big-decimal/transcendentals.d.ts +1 -1
  32. package/dist/types/big-decimal/utils.d.ts +1 -1
  33. package/dist/types/common/ansi-codes.d.ts +1 -1
  34. package/dist/types/common/configuration-change.d.ts +1 -1
  35. package/dist/types/common/fuzzy-string-match.d.ts +1 -1
  36. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  37. package/dist/types/common/interruptible.d.ts +1 -1
  38. package/dist/types/common/one-of.d.ts +1 -1
  39. package/dist/types/common/signals.d.ts +1 -1
  40. package/dist/types/common/type/ast-nodes.d.ts +1 -1
  41. package/dist/types/common/type/boxed-type.d.ts +1 -1
  42. package/dist/types/common/type/lexer.d.ts +1 -1
  43. package/dist/types/common/type/parse.d.ts +1 -1
  44. package/dist/types/common/type/parser.d.ts +1 -1
  45. package/dist/types/common/type/primitive.d.ts +1 -1
  46. package/dist/types/common/type/reduce.d.ts +1 -1
  47. package/dist/types/common/type/serialize.d.ts +1 -1
  48. package/dist/types/common/type/subtype.d.ts +1 -1
  49. package/dist/types/common/type/type-builder.d.ts +1 -1
  50. package/dist/types/common/type/types.d.ts +1 -1
  51. package/dist/types/common/type/utils.d.ts +1 -1
  52. package/dist/types/common/utils.d.ts +1 -1
  53. package/dist/types/compile.d.ts +1 -1
  54. package/dist/types/compute-engine/assume.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  60. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
  66. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  68. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  69. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +1 -1
  70. package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +1 -1
  71. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  72. package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
  73. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  74. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  75. package/dist/types/compute-engine/boxed-expression/constants.d.ts +1 -1
  76. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  77. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  78. package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
  79. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  80. package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
  81. package/dist/types/compute-engine/boxed-expression/inequality-bounds.d.ts +1 -1
  82. package/dist/types/compute-engine/boxed-expression/init-lazy-refs.d.ts +1 -1
  83. package/dist/types/compute-engine/boxed-expression/invisible-operator.d.ts +1 -1
  84. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  85. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  86. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  87. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  88. package/dist/types/compute-engine/boxed-expression/pattern-utils.d.ts +1 -1
  89. package/dist/types/compute-engine/boxed-expression/polynomial-degree.d.ts +1 -1
  90. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  91. package/dist/types/compute-engine/boxed-expression/predicates.d.ts +1 -1
  92. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  93. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  94. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  95. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  96. package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +1 -1
  97. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  98. package/dist/types/compute-engine/boxed-expression/stochastic-equal.d.ts +1 -1
  99. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  100. package/dist/types/compute-engine/boxed-expression/type-guards.d.ts +1 -1
  101. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  102. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  103. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  104. package/dist/types/compute-engine/compilation/base-compiler.d.ts +8 -7
  105. package/dist/types/compute-engine/compilation/compile-expression.d.ts +1 -1
  106. package/dist/types/compute-engine/compilation/constant-folding.d.ts +50 -0
  107. package/dist/types/compute-engine/compilation/glsl-target.d.ts +1 -1
  108. package/dist/types/compute-engine/compilation/gpu-target.d.ts +1 -1
  109. package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +1 -1
  110. package/dist/types/compute-engine/compilation/javascript-target.d.ts +23 -1
  111. package/dist/types/compute-engine/compilation/python-target.d.ts +1 -1
  112. package/dist/types/compute-engine/compilation/types.d.ts +1 -1
  113. package/dist/types/compute-engine/compilation/wgsl-target.d.ts +1 -1
  114. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  115. package/dist/types/compute-engine/engine-assumptions.d.ts +1 -1
  116. package/dist/types/compute-engine/engine-cache.d.ts +1 -1
  117. package/dist/types/compute-engine/engine-common-symbols.d.ts +1 -1
  118. package/dist/types/compute-engine/engine-compilation-targets.d.ts +1 -1
  119. package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +1 -1
  120. package/dist/types/compute-engine/engine-declarations.d.ts +1 -1
  121. package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +1 -1
  122. package/dist/types/compute-engine/engine-extension-contracts.d.ts +1 -1
  123. package/dist/types/compute-engine/engine-library-bootstrap.d.ts +1 -1
  124. package/dist/types/compute-engine/engine-numeric-configuration.d.ts +1 -1
  125. package/dist/types/compute-engine/engine-runtime-state.d.ts +1 -1
  126. package/dist/types/compute-engine/engine-scope.d.ts +1 -1
  127. package/dist/types/compute-engine/engine-sequences.d.ts +1 -1
  128. package/dist/types/compute-engine/engine-simplification-rules.d.ts +1 -1
  129. package/dist/types/compute-engine/engine-startup-coordinator.d.ts +1 -1
  130. package/dist/types/compute-engine/engine-type-resolver.d.ts +1 -1
  131. package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +1 -1
  132. package/dist/types/compute-engine/free-functions.d.ts +1 -1
  133. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  134. package/dist/types/compute-engine/global-types.d.ts +1 -1
  135. package/dist/types/compute-engine/index.d.ts +1 -1
  136. package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
  137. package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
  138. package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
  139. package/dist/types/compute-engine/interval/index.d.ts +1 -1
  140. package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
  141. package/dist/types/compute-engine/interval/types.d.ts +1 -1
  142. package/dist/types/compute-engine/interval/util.d.ts +1 -1
  143. package/dist/types/compute-engine/latex-syntax/dictionary/default-dictionary.d.ts +1 -1
  144. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  145. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  146. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  147. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  148. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  149. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  150. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +3 -1
  151. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  152. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  153. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  154. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  155. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  156. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  157. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-units.d.ts +1 -1
  158. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  159. package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +1 -1
  160. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  161. package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +1 -1
  162. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
  163. package/dist/types/compute-engine/latex-syntax/parse.d.ts +11 -1
  164. package/dist/types/compute-engine/latex-syntax/serialize-dms.d.ts +1 -1
  165. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  166. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  167. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  168. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  169. package/dist/types/compute-engine/latex-syntax/types.d.ts +1 -1
  170. package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
  171. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  172. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  173. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  174. package/dist/types/compute-engine/library/colors.d.ts +1 -1
  175. package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
  176. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  177. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  178. package/dist/types/compute-engine/library/core.d.ts +1 -1
  179. package/dist/types/compute-engine/library/fractals.d.ts +1 -1
  180. package/dist/types/compute-engine/library/library.d.ts +1 -1
  181. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  182. package/dist/types/compute-engine/library/logic-analysis.d.ts +1 -1
  183. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  184. package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
  185. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  186. package/dist/types/compute-engine/library/quantity-arithmetic.d.ts +1 -1
  187. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  188. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  189. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  190. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  191. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  192. package/dist/types/compute-engine/library/type-handlers.d.ts +1 -1
  193. package/dist/types/compute-engine/library/unit-data.d.ts +1 -1
  194. package/dist/types/compute-engine/library/units.d.ts +1 -1
  195. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  196. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  197. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  198. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  199. package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
  200. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  201. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  202. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  203. package/dist/types/compute-engine/numerics/linear-algebra.d.ts +1 -1
  204. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  205. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  206. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  207. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  208. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  209. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  210. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  211. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  212. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  213. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  214. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  215. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  216. package/dist/types/compute-engine/numerics/unit-data.d.ts +1 -1
  217. package/dist/types/compute-engine/oeis.d.ts +1 -1
  218. package/dist/types/compute-engine/sequence.d.ts +1 -1
  219. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
  220. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  221. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  222. package/dist/types/compute-engine/symbolic/fu-cost.d.ts +1 -1
  223. package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +1 -1
  224. package/dist/types/compute-engine/symbolic/fu.d.ts +1 -1
  225. package/dist/types/compute-engine/symbolic/logic-utils.d.ts +1 -1
  226. package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +1 -1
  227. package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +1 -1
  228. package/dist/types/compute-engine/symbolic/simplify-factorial.d.ts +1 -1
  229. package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +1 -1
  230. package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +1 -1
  231. package/dist/types/compute-engine/symbolic/simplify-log.d.ts +1 -1
  232. package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +1 -1
  233. package/dist/types/compute-engine/symbolic/simplify-power.d.ts +1 -1
  234. package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
  235. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  236. package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
  237. package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +1 -1
  238. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
  239. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  240. package/dist/types/compute-engine/types-definitions.d.ts +1 -1
  241. package/dist/types/compute-engine/types-engine.d.ts +1 -1
  242. package/dist/types/compute-engine/types-evaluation.d.ts +1 -1
  243. package/dist/types/compute-engine/types-expression.d.ts +1 -1
  244. package/dist/types/compute-engine/types-kernel-evaluation.d.ts +1 -1
  245. package/dist/types/compute-engine/types-kernel-serialization.d.ts +1 -1
  246. package/dist/types/compute-engine/types-serialization.d.ts +1 -1
  247. package/dist/types/compute-engine/types.d.ts +1 -1
  248. package/dist/types/compute-engine.d.ts +1 -1
  249. package/dist/types/core.d.ts +1 -1
  250. package/dist/types/interval.d.ts +1 -1
  251. package/dist/types/latex-syntax.d.ts +2 -2
  252. package/dist/types/math-json/symbols.d.ts +1 -1
  253. package/dist/types/math-json/types.d.ts +1 -1
  254. package/dist/types/math-json/utils.d.ts +1 -1
  255. package/dist/types/math-json.d.ts +2 -2
  256. package/dist/types/numerics.d.ts +1 -1
  257. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.55.0 */
1
+ /** Compute Engine 0.55.3 */
2
2
  (function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ComputeEngine = {}));})(this, (function (exports) { 'use strict';
3
3
  var ComputeEngine = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -9601,6 +9601,452 @@ var ComputeEngine = (() => {
9601
9601
  return expr2;
9602
9602
  }
9603
9603
 
9604
+ // src/compute-engine/latex-syntax/dictionary/definitions-logic.ts
9605
+ var DEFINITIONS_LOGIC = [
9606
+ // Constants
9607
+ {
9608
+ name: "True",
9609
+ kind: "symbol",
9610
+ latexTrigger: ["\\top"]
9611
+ // ⊤ U+22A4
9612
+ },
9613
+ {
9614
+ kind: "symbol",
9615
+ latexTrigger: "\\mathrm{True}",
9616
+ parse: "True"
9617
+ },
9618
+ {
9619
+ kind: "symbol",
9620
+ latexTrigger: "\\operatorname{True}",
9621
+ parse: "True"
9622
+ },
9623
+ {
9624
+ kind: "symbol",
9625
+ latexTrigger: "\\mathsf{T}",
9626
+ parse: "True"
9627
+ },
9628
+ {
9629
+ name: "False",
9630
+ kind: "symbol",
9631
+ latexTrigger: ["\\bot"]
9632
+ // ⊥ U+22A5
9633
+ },
9634
+ {
9635
+ kind: "symbol",
9636
+ latexTrigger: "\\operatorname{False}",
9637
+ parse: "False"
9638
+ },
9639
+ {
9640
+ kind: "symbol",
9641
+ latexTrigger: "\\mathsf{F}",
9642
+ parse: "False"
9643
+ },
9644
+ // Operators
9645
+ // Logic operators have lower precedence than comparisons (245)
9646
+ // so that `x = 1 \lor x = 2` parses as `(x = 1) \lor (x = 2)`
9647
+ // See https://github.com/cortex-js/compute-engine/issues/243
9648
+ {
9649
+ name: "And",
9650
+ kind: "infix",
9651
+ latexTrigger: ["\\land"],
9652
+ precedence: 235
9653
+ // serialize: '\\land',
9654
+ },
9655
+ { kind: "infix", latexTrigger: ["\\wedge"], parse: "And", precedence: 235 },
9656
+ { kind: "infix", latexTrigger: "\\&", parse: "And", precedence: 235 },
9657
+ {
9658
+ kind: "infix",
9659
+ latexTrigger: "\\operatorname{and}",
9660
+ parse: "And",
9661
+ precedence: 235
9662
+ },
9663
+ {
9664
+ name: "Or",
9665
+ kind: "infix",
9666
+ latexTrigger: ["\\lor"],
9667
+ precedence: 230
9668
+ },
9669
+ { kind: "infix", latexTrigger: ["\\vee"], parse: "Or", precedence: 230 },
9670
+ { kind: "infix", latexTrigger: "\\parallel", parse: "Or", precedence: 230 },
9671
+ {
9672
+ kind: "infix",
9673
+ latexTrigger: "\\operatorname{or}",
9674
+ parse: "Or",
9675
+ precedence: 230
9676
+ },
9677
+ {
9678
+ name: "Xor",
9679
+ kind: "infix",
9680
+ latexTrigger: ["\\veebar"],
9681
+ precedence: 232
9682
+ },
9683
+ // Possible alt: \oplus ⊕ U+2295
9684
+ {
9685
+ name: "Not",
9686
+ kind: "prefix",
9687
+ latexTrigger: ["\\lnot"],
9688
+ precedence: 880
9689
+ },
9690
+ {
9691
+ kind: "prefix",
9692
+ latexTrigger: ["\\neg"],
9693
+ parse: "Not",
9694
+ precedence: 880
9695
+ },
9696
+ {
9697
+ name: "Nand",
9698
+ kind: "infix",
9699
+ latexTrigger: ["\\barwedge"],
9700
+ precedence: 232
9701
+ // serialize: '\\mid',
9702
+ },
9703
+ {
9704
+ name: "Nor",
9705
+ kind: "infix",
9706
+ latexTrigger: ["\u22BD"],
9707
+ // bar vee
9708
+ precedence: 232
9709
+ // serialize: '\\downarrow',
9710
+ },
9711
+ // Functions
9712
+ {
9713
+ kind: "function",
9714
+ symbolTrigger: "and",
9715
+ parse: "And"
9716
+ },
9717
+ {
9718
+ kind: "function",
9719
+ symbolTrigger: "or",
9720
+ parse: "Or"
9721
+ },
9722
+ {
9723
+ kind: "function",
9724
+ symbolTrigger: "not",
9725
+ parse: "Not"
9726
+ },
9727
+ // Relations
9728
+ {
9729
+ name: "Implies",
9730
+ kind: "infix",
9731
+ precedence: 220,
9732
+ associativity: "right",
9733
+ latexTrigger: ["\\implies"],
9734
+ serialize: "\\implies"
9735
+ },
9736
+ {
9737
+ latexTrigger: ["\\Rightarrow"],
9738
+ kind: "infix",
9739
+ precedence: 220,
9740
+ associativity: "right",
9741
+ parse: "Implies"
9742
+ },
9743
+ {
9744
+ latexTrigger: ["\\rightarrow"],
9745
+ kind: "infix",
9746
+ precedence: 220,
9747
+ associativity: "right",
9748
+ parse: "Implies"
9749
+ },
9750
+ {
9751
+ latexTrigger: ["\\Longrightarrow"],
9752
+ kind: "infix",
9753
+ precedence: 220,
9754
+ associativity: "right",
9755
+ parse: "Implies"
9756
+ },
9757
+ {
9758
+ latexTrigger: ["\\longrightarrow"],
9759
+ kind: "infix",
9760
+ precedence: 220,
9761
+ associativity: "right",
9762
+ parse: "Implies"
9763
+ },
9764
+ {
9765
+ // Non-strict mode: => for implies
9766
+ latexTrigger: ["=", ">"],
9767
+ kind: "infix",
9768
+ precedence: 220,
9769
+ associativity: "right",
9770
+ parse: (parser, lhs, until) => {
9771
+ if (parser.options.strict !== false) return null;
9772
+ const rhs = parser.parseExpression({ ...until, minPrec: 220 });
9773
+ if (rhs === null) return null;
9774
+ return ["Implies", lhs, rhs];
9775
+ }
9776
+ },
9777
+ {
9778
+ name: "Equivalent",
9779
+ // MathML: identical to, Mathematica: Congruent
9780
+ latexTrigger: ["\\iff"],
9781
+ kind: "infix",
9782
+ associativity: "right",
9783
+ precedence: 219
9784
+ },
9785
+ {
9786
+ latexTrigger: ["\\Leftrightarrow"],
9787
+ kind: "infix",
9788
+ associativity: "right",
9789
+ precedence: 219,
9790
+ parse: "Equivalent"
9791
+ },
9792
+ {
9793
+ latexTrigger: ["\\leftrightarrow"],
9794
+ kind: "infix",
9795
+ associativity: "right",
9796
+ precedence: 219,
9797
+ parse: "Equivalent"
9798
+ },
9799
+ {
9800
+ latexTrigger: ["\\Longleftrightarrow"],
9801
+ kind: "infix",
9802
+ associativity: "right",
9803
+ precedence: 219,
9804
+ parse: "Equivalent"
9805
+ },
9806
+ {
9807
+ latexTrigger: ["\\longleftrightarrow"],
9808
+ kind: "infix",
9809
+ associativity: "right",
9810
+ precedence: 219,
9811
+ parse: "Equivalent"
9812
+ },
9813
+ {
9814
+ // Non-strict mode: <=> for equivalence
9815
+ latexTrigger: ["<", "=", ">"],
9816
+ kind: "infix",
9817
+ precedence: 219,
9818
+ associativity: "right",
9819
+ parse: (parser, lhs, until) => {
9820
+ if (parser.options.strict !== false) return null;
9821
+ const rhs = parser.parseExpression({ ...until, minPrec: 219 });
9822
+ if (rhs === null) return null;
9823
+ return ["Equivalent", lhs, rhs];
9824
+ }
9825
+ },
9826
+ {
9827
+ latexTrigger: ["\\equiv"],
9828
+ kind: "infix",
9829
+ associativity: "right",
9830
+ precedence: 219,
9831
+ parse: (parser, lhs, terminator) => {
9832
+ const rhs = parser.parseExpression({ ...terminator, minPrec: 219 });
9833
+ const index = parser.index;
9834
+ const modulus = parser.parseExpression({ ...terminator, minPrec: 219 });
9835
+ if (modulus !== null && operator(modulus) === "Mod")
9836
+ return ["Congruent", lhs, rhs, missingIfEmpty(operand(modulus, 1))];
9837
+ parser.index = index;
9838
+ return ["Equivalent", lhs, missingIfEmpty(rhs)];
9839
+ }
9840
+ },
9841
+ {
9842
+ name: "Proves",
9843
+ kind: "infix",
9844
+ latexTrigger: ["\\vdash"],
9845
+ precedence: 220,
9846
+ associativity: "right",
9847
+ serialize: "\\vdash"
9848
+ },
9849
+ {
9850
+ name: "Entails",
9851
+ kind: "infix",
9852
+ latexTrigger: ["\\vDash"],
9853
+ precedence: 220,
9854
+ associativity: "right",
9855
+ serialize: "\\vDash"
9856
+ },
9857
+ {
9858
+ name: "Satisfies",
9859
+ kind: "infix",
9860
+ latexTrigger: ["\\models"],
9861
+ precedence: 220,
9862
+ associativity: "right",
9863
+ serialize: "\\models"
9864
+ },
9865
+ // Quantifiers: for all, exists
9866
+ {
9867
+ name: "ForAll",
9868
+ kind: "prefix",
9869
+ latexTrigger: ["\\forall"],
9870
+ precedence: 200,
9871
+ // Has to be lower than COMPARISON_PRECEDENCE
9872
+ serialize: serializeQuantifier("\\forall"),
9873
+ parse: parseQuantifier("ForAll")
9874
+ },
9875
+ {
9876
+ name: "Exists",
9877
+ kind: "prefix",
9878
+ latexTrigger: ["\\exists"],
9879
+ precedence: 200,
9880
+ // Has to be lower than COMPARISON_PRECEDENCE,
9881
+ serialize: serializeQuantifier("\\exists"),
9882
+ parse: parseQuantifier("Exists")
9883
+ },
9884
+ {
9885
+ name: "ExistsUnique",
9886
+ kind: "prefix",
9887
+ latexTrigger: ["\\exists", "!"],
9888
+ precedence: 200,
9889
+ // Has to be lower than COMPARISON_PRECEDENCE,
9890
+ serialize: serializeQuantifier("\\exists!"),
9891
+ parse: parseQuantifier("ExistsUnique")
9892
+ },
9893
+ {
9894
+ name: "NotForAll",
9895
+ kind: "prefix",
9896
+ latexTrigger: ["\\lnot", "\\forall"],
9897
+ precedence: 200,
9898
+ // Has to be lower than COMPARISON_PRECEDENCE
9899
+ serialize: serializeQuantifier("\\lnot\\forall"),
9900
+ parse: parseQuantifier("NotForAll")
9901
+ },
9902
+ {
9903
+ name: "NotExists",
9904
+ kind: "prefix",
9905
+ latexTrigger: ["\\lnot", "\\exists"],
9906
+ precedence: 200,
9907
+ // Has to be lower than COMPARISON_PRECEDENCE,
9908
+ serialize: serializeQuantifier("\\lnot\\exists"),
9909
+ parse: parseQuantifier("NotExists")
9910
+ },
9911
+ {
9912
+ name: "KroneckerDelta",
9913
+ kind: "prefix",
9914
+ latexTrigger: ["\\delta", "_"],
9915
+ precedence: 200,
9916
+ serialize: (serializer, expr2) => {
9917
+ const args = operands(expr2);
9918
+ if (args.length === 0) return "\\delta";
9919
+ if (args.every((x) => symbol(x)))
9920
+ return `\\delta_{${args.map((arg) => serializer.serialize(arg)).join("")}}`;
9921
+ return `\\delta_{${args.map((arg) => serializer.serialize(arg)).join(", ")}}`;
9922
+ },
9923
+ parse: (parser) => {
9924
+ const group = parser.parseGroup();
9925
+ if (group === null) {
9926
+ const token = parser.parseToken();
9927
+ if (!token) return null;
9928
+ return ["KroneckerDelta", token];
9929
+ }
9930
+ const seq = getSequence(group);
9931
+ if (seq && seq.length <= 2) return ["KroneckerDelta", ...seq];
9932
+ if (operator(group) === "InvisibleOperator")
9933
+ return ["KroneckerDelta", ...operands(group)];
9934
+ if (group !== null) return ["KroneckerDelta", group];
9935
+ return null;
9936
+ }
9937
+ },
9938
+ // Iverson brackets. Also called the "indicator function"
9939
+ // Must have a single argument, a relational expression, i.e.
9940
+ // `[ a = b ]` or `[ x \leq 0 ]`
9941
+ // Otherwise, it gets rejected, it could be something else, like a list or
9942
+ // tuple.
9943
+ {
9944
+ name: "Boole",
9945
+ kind: "matchfix",
9946
+ openTrigger: "[",
9947
+ closeTrigger: "]",
9948
+ // serialize: (serializer: Serializer, expr: MathJsonExpression) => {
9949
+ // const args = ops(expr);
9950
+ // return `[${serializer.serialize(arg)}]`;
9951
+ // },
9952
+ parse: (_parser, body) => {
9953
+ const h = operator(body);
9954
+ if (!h) return null;
9955
+ if (!DEFINITIONS_INEQUALITIES.some((x) => x.name === h)) return null;
9956
+ return ["Boole", body];
9957
+ }
9958
+ },
9959
+ {
9960
+ kind: "matchfix",
9961
+ openTrigger: "\\llbracket",
9962
+ closeTrigger: "\\rrbracket",
9963
+ parse: (_parser, body) => {
9964
+ const h = operator(body);
9965
+ if (!h) return null;
9966
+ if (!DEFINITIONS_INEQUALITIES.some((x) => x.name === h)) return null;
9967
+ return ["Boole", body];
9968
+ }
9969
+ },
9970
+ // Predicate application in First-Order Logic.
9971
+ // ["Predicate", "P", "x", "y"] serializes to "P(x, y)"
9972
+ {
9973
+ name: "Predicate",
9974
+ serialize: (serializer, expr2) => {
9975
+ const args = operands(expr2);
9976
+ if (args.length === 0) return "";
9977
+ const pred = args[0];
9978
+ const predStr = typeof pred === "string" ? pred : serializer.serialize(pred);
9979
+ if (args.length === 1) return predStr;
9980
+ const argStrs = args.slice(1).map((arg) => serializer.serialize(arg));
9981
+ return `${predStr}(${argStrs.join(", ")})`;
9982
+ }
9983
+ }
9984
+ ];
9985
+ function serializeQuantifier(quantifierSymbol) {
9986
+ return (serializer, expr2) => {
9987
+ const args = operands(expr2);
9988
+ if (args.length === 0) return quantifierSymbol;
9989
+ if (args.length === 1)
9990
+ return `${quantifierSymbol} ${serializer.serialize(args[0])}`;
9991
+ const boundVar = serializer.serialize(args[0]);
9992
+ const body = serializer.serialize(args[1]);
9993
+ return `${quantifierSymbol} ${boundVar}, ${body}`;
9994
+ };
9995
+ }
9996
+ function tightBindingCondition(p, terminator) {
9997
+ return p.peek === "\\to" || p.peek === "\\rightarrow" || p.peek === "\\implies" || p.peek === "\\Rightarrow" || p.peek === "\\iff" || p.peek === "\\Leftrightarrow" || p.peek === "\\land" || p.peek === "\\wedge" || p.peek === "\\lor" || p.peek === "\\vee" || (terminator.condition?.(p) ?? false);
9998
+ }
9999
+ function parseQuantifier(kind) {
10000
+ return (parser, terminator) => {
10001
+ const index = parser.index;
10002
+ const useTightBinding = parser.options.quantifierScope !== "loose";
10003
+ const symbol2 = parser.parseSymbol(terminator);
10004
+ if (symbol2) {
10005
+ parser.skipSpace();
10006
+ if (parser.match(",") || parser.match("\\mid") || parser.match(".") || parser.match(":") || parser.match("\\colon")) {
10007
+ const bodyTerminator = useTightBinding ? {
10008
+ ...terminator,
10009
+ condition: (p) => tightBindingCondition(p, terminator)
10010
+ } : terminator;
10011
+ parser.enterQuantifierScope();
10012
+ const body2 = parser.parseExpression(bodyTerminator);
10013
+ parser.exitQuantifierScope();
10014
+ return [kind, symbol2, missingIfEmpty(body2)];
10015
+ }
10016
+ parser.enterQuantifierScope();
10017
+ const body = parser.parseEnclosure();
10018
+ parser.exitQuantifierScope();
10019
+ if (body) return [kind, symbol2, missingIfEmpty(body)];
10020
+ }
10021
+ parser.index = index;
10022
+ const condTerminator = {
10023
+ ...terminator,
10024
+ condition: (p) => p.peek === ":" || p.peek === "\\colon" || (terminator.condition?.(p) ?? false)
10025
+ };
10026
+ const condition = parser.parseExpression(condTerminator);
10027
+ if (condition === null) return null;
10028
+ parser.skipSpace();
10029
+ if (parser.matchAny([",", "\\mid", ":", "\\colon"])) {
10030
+ const bodyTerminator = useTightBinding ? {
10031
+ ...terminator,
10032
+ condition: (p) => tightBindingCondition(p, terminator)
10033
+ } : terminator;
10034
+ parser.enterQuantifierScope();
10035
+ const body = parser.parseExpression(bodyTerminator);
10036
+ parser.exitQuantifierScope();
10037
+ return [kind, condition, missingIfEmpty(body)];
10038
+ }
10039
+ if (parser.match("(")) {
10040
+ parser.enterQuantifierScope();
10041
+ const body = parser.parseExpression(terminator);
10042
+ parser.exitQuantifierScope();
10043
+ if (!parser.match(")")) return null;
10044
+ return [kind, condition, missingIfEmpty(body)];
10045
+ }
10046
+ return null;
10047
+ };
10048
+ }
10049
+
9604
10050
  // src/compute-engine/latex-syntax/dictionary/definitions-core.ts
9605
10051
  function parseSequence(parser, terminator, lhs, prec, sep) {
9606
10052
  if (terminator && terminator.minPrec >= prec) return null;
@@ -10211,6 +10657,120 @@ var ComputeEngine = (() => {
10211
10657
  precedence: 21,
10212
10658
  parse: (parser, lhs, until) => parseWhereExpression(parser, lhs, until)
10213
10659
  },
10660
+ // \text{and} — logical conjunction infix
10661
+ {
10662
+ latexTrigger: ["\\text"],
10663
+ kind: "infix",
10664
+ associativity: "right",
10665
+ precedence: 235,
10666
+ // Same as \land
10667
+ parse: (parser, lhs, until) => {
10668
+ const start = parser.index;
10669
+ if (!matchTextKeyword(parser, "and")) {
10670
+ parser.index = start;
10671
+ return null;
10672
+ }
10673
+ const rhs = parser.parseExpression({ ...until, minPrec: 235 });
10674
+ return ["And", lhs, rhs ?? "Nothing"];
10675
+ }
10676
+ },
10677
+ // \text{or} — logical disjunction infix
10678
+ {
10679
+ latexTrigger: ["\\text"],
10680
+ kind: "infix",
10681
+ associativity: "right",
10682
+ precedence: 230,
10683
+ // Same as \lor
10684
+ parse: (parser, lhs, until) => {
10685
+ const start = parser.index;
10686
+ if (!matchTextKeyword(parser, "or")) {
10687
+ parser.index = start;
10688
+ return null;
10689
+ }
10690
+ const rhs = parser.parseExpression({ ...until, minPrec: 230 });
10691
+ return ["Or", lhs, rhs ?? "Nothing"];
10692
+ }
10693
+ },
10694
+ // \text{iff} — biconditional (if and only if)
10695
+ {
10696
+ latexTrigger: ["\\text"],
10697
+ kind: "infix",
10698
+ associativity: "right",
10699
+ precedence: 219,
10700
+ // Same as \iff
10701
+ parse: (parser, lhs, until) => {
10702
+ const start = parser.index;
10703
+ if (!matchTextKeyword(parser, "iff")) {
10704
+ parser.index = start;
10705
+ return null;
10706
+ }
10707
+ const rhs = parser.parseExpression({ ...until, minPrec: 219 });
10708
+ return ["Equivalent", lhs, rhs ?? "Nothing"];
10709
+ }
10710
+ },
10711
+ // \text{if and only if} — verbose biconditional
10712
+ {
10713
+ latexTrigger: ["\\text"],
10714
+ kind: "infix",
10715
+ associativity: "right",
10716
+ precedence: 219,
10717
+ parse: (parser, lhs, until) => {
10718
+ const start = parser.index;
10719
+ if (!matchTextKeyword(parser, "if and only if")) {
10720
+ parser.index = start;
10721
+ return null;
10722
+ }
10723
+ const rhs = parser.parseExpression({ ...until, minPrec: 219 });
10724
+ return ["Equivalent", lhs, rhs ?? "Nothing"];
10725
+ }
10726
+ },
10727
+ // \text{such that} — constraint separator (like : in set-builder notation)
10728
+ {
10729
+ latexTrigger: ["\\text"],
10730
+ kind: "infix",
10731
+ associativity: "right",
10732
+ precedence: 21,
10733
+ // Low precedence to capture full condition (same as 'where')
10734
+ parse: (parser, lhs, until) => {
10735
+ const start = parser.index;
10736
+ if (!matchTextKeyword(parser, "such that")) {
10737
+ parser.index = start;
10738
+ return null;
10739
+ }
10740
+ const rhs = parser.parseExpression({ ...until, minPrec: 21 });
10741
+ return ["Colon", lhs, rhs ?? "Nothing"];
10742
+ }
10743
+ },
10744
+ // \text{for all} — universal quantifier
10745
+ {
10746
+ latexTrigger: ["\\text"],
10747
+ kind: "prefix",
10748
+ precedence: 200,
10749
+ // Same as \forall
10750
+ parse: (parser, until) => {
10751
+ const start = parser.index;
10752
+ if (!matchTextKeyword(parser, "for all")) {
10753
+ parser.index = start;
10754
+ return null;
10755
+ }
10756
+ return parseQuantifier("ForAll")(parser, until);
10757
+ }
10758
+ },
10759
+ // \text{there exists} — existential quantifier
10760
+ {
10761
+ latexTrigger: ["\\text"],
10762
+ kind: "prefix",
10763
+ precedence: 200,
10764
+ // Same as \exists
10765
+ parse: (parser, until) => {
10766
+ const start = parser.index;
10767
+ if (!matchTextKeyword(parser, "there exists")) {
10768
+ parser.index = start;
10769
+ return null;
10770
+ }
10771
+ return parseQuantifier("Exists")(parser, until);
10772
+ }
10773
+ },
10214
10774
  // Block serializer — used by both `where` and semicolon blocks
10215
10775
  {
10216
10776
  name: "Block",
@@ -10326,6 +10886,39 @@ var ComputeEngine = (() => {
10326
10886
  parser.parseExpression(until) ?? "Nothing"
10327
10887
  ]
10328
10888
  },
10889
+ // Text serializer — reconstructs \text{...} with inline $...$ for math
10890
+ {
10891
+ name: "Text",
10892
+ serialize: (serializer, expr2) => {
10893
+ const args = operands(expr2);
10894
+ if (args.length === 0) return "";
10895
+ let firstStr = -1;
10896
+ let lastStr = -1;
10897
+ for (let i = 0; i < args.length; i++) {
10898
+ if (stringValue(args[i]) !== null) {
10899
+ if (firstStr < 0) firstStr = i;
10900
+ lastStr = i;
10901
+ }
10902
+ }
10903
+ if (firstStr < 0)
10904
+ return joinLatex(args.map((a) => serializer.serialize(a)));
10905
+ const parts = [];
10906
+ for (let i = 0; i < firstStr; i++)
10907
+ parts.push(serializer.serialize(args[i]));
10908
+ let textContent = "";
10909
+ for (let i = firstStr; i <= lastStr; i++) {
10910
+ const s = stringValue(args[i]);
10911
+ if (s !== null) textContent += sanitizeLatex(s);
10912
+ else if (operator(args[i]) === "Annotated" || operator(args[i]) === "Text")
10913
+ textContent += serializer.serialize(args[i]);
10914
+ else textContent += "$" + serializer.serialize(args[i]) + "$";
10915
+ }
10916
+ parts.push("\\text{" + textContent + "}");
10917
+ for (let i = lastStr + 1; i < args.length; i++)
10918
+ parts.push(serializer.serialize(args[i]));
10919
+ return joinLatex(parts);
10920
+ }
10921
+ },
10329
10922
  {
10330
10923
  name: "String",
10331
10924
  latexTrigger: ["\\text"],
@@ -10716,9 +11309,10 @@ var ComputeEngine = (() => {
10716
11309
  } else if (parser.match("\\textcolor")) {
10717
11310
  const pos = parser.index;
10718
11311
  const color = parser.parseStringGroup();
10719
- const body2 = parser.parseExpression();
10720
- if (color !== null && body2 !== null) {
10721
- runs.push(["Annotated", body2, { dict: { color } }]);
11312
+ if (color !== null) {
11313
+ flush();
11314
+ const body2 = parseTextRun(parser);
11315
+ runs.push(["Annotated", body2, dictionaryFromEntries({ color })]);
10722
11316
  } else {
10723
11317
  parser.index = pos;
10724
11318
  text += "\\textcolor";
@@ -10736,6 +11330,7 @@ var ComputeEngine = (() => {
10736
11330
  const expr2 = parser.parseExpression() ?? "Nothing";
10737
11331
  parser.skipSpace();
10738
11332
  if (parser.match("<$>")) {
11333
+ flush();
10739
11334
  runs.push(expr2);
10740
11335
  } else {
10741
11336
  text += "$";
@@ -10746,6 +11341,7 @@ var ComputeEngine = (() => {
10746
11341
  const expr2 = parser.parseExpression() ?? "Nothing";
10747
11342
  parser.skipSpace();
10748
11343
  if (parser.match("<$$>")) {
11344
+ flush();
10749
11345
  runs.push(expr2);
10750
11346
  } else {
10751
11347
  text += "$$";
@@ -11096,14 +11692,20 @@ var ComputeEngine = (() => {
11096
11692
  }
11097
11693
  while (parser.match("<space>")) {
11098
11694
  }
11099
- let text = "";
11100
- while (!parser.atEnd && parser.peek !== "<}>" && parser.peek !== "<space>") {
11101
- const tok = parser.peek;
11102
- if (/^[a-zA-Z]$/.test(tok)) {
11103
- text += tok;
11104
- parser.nextToken();
11695
+ for (let i = 0; i < keyword.length; i++) {
11696
+ if (keyword[i] === " ") {
11697
+ if (!parser.match("<space>")) {
11698
+ parser.index = start;
11699
+ return false;
11700
+ }
11701
+ while (parser.match("<space>")) {
11702
+ }
11105
11703
  } else {
11106
- break;
11704
+ if (parser.peek !== keyword[i]) {
11705
+ parser.index = start;
11706
+ return false;
11707
+ }
11708
+ parser.nextToken();
11107
11709
  }
11108
11710
  }
11109
11711
  while (parser.match("<space>")) {
@@ -11112,10 +11714,6 @@ var ComputeEngine = (() => {
11112
11714
  parser.index = start;
11113
11715
  return false;
11114
11716
  }
11115
- if (text !== keyword) {
11116
- parser.index = start;
11117
- return false;
11118
- }
11119
11717
  return true;
11120
11718
  }
11121
11719
  function matchKeyword(parser, keyword) {
@@ -11290,554 +11888,108 @@ var ComputeEngine = (() => {
11290
11888
  //GREEK SMALL LETTER FINAL SIGMA
11291
11889
  ["tau", "\\tau", 964],
11292
11890
  ["phi", "\\phi", 981],
11293
- // Note GREEK PHI SYMBOL, but common usage in math
11294
- ["phiLetter", "\\varphi", 966],
11295
- ["upsilon", "\\upsilon", 965],
11296
- ["chi", "\\chi", 967],
11297
- ["psi", "\\psi", 968],
11298
- ["omega", "\\omega", 969],
11299
- ["Alpha", "\\Alpha", 913],
11300
- ["Beta", "\\Beta", 914],
11301
- ["Gamma", "\\Gamma", 915],
11302
- ["Delta", "\\Delta", 916],
11303
- ["Epsilon", "\\Epsilon", 917],
11304
- ["Zeta", "\\Zeta", 918],
11305
- ["Eta", "\\Eta", 919],
11306
- ["Theta", "\\Theta", 920],
11307
- ["Iota", "\\Iota", 921],
11308
- ["Kappa", "\\Kappa", 922],
11309
- ["Lambda", "\\Lambda", 923],
11310
- ["Mu", "\\Mu", 924],
11311
- ["Nu", "\\Nu", 925],
11312
- ["Xi", "\\Xi", 926],
11313
- ["Omicron", "\\Omicron", 927],
11314
- // ['Pi', '\\Pi', 0x03a0],
11315
- ["Rho", "\\Rho", 929],
11316
- ["Sigma", "\\Sigma", 931],
11317
- ["Tau", "\\Tau", 932],
11318
- ["Phi", "\\Phi", 934],
11319
- ["Upsilon", "\\Upsilon", 933],
11320
- ["Chi", "\\Chi", 935],
11321
- ["Psi", "\\Psi", 936],
11322
- ["Omega", "\\Omega", 937],
11323
- ["digamma", "\\digamma", 989],
11324
- // Hebrew
11325
- ["aleph", "\\aleph", 8501],
11326
- // Unicode ALEF SYMBOL
11327
- ["bet", "\\beth", 8502],
11328
- ["gimel", "\\gimel", 8503],
11329
- ["dalet", "\\daleth", 8504],
11330
- // Letter-like
11331
- ["ell", "\\ell", 8499],
11332
- // Unicode SCRIPT SMALL L
11333
- ["turnedCapitalF", "\\Finv", 8498],
11334
- // Unicode TURNED CAPITAL F'
11335
- ["turnedCapitalG", "\\Game", 8513],
11336
- // TURNED SANS-SERIF CAPITAL G
11337
- ["weierstrass", "\\wp", 8472],
11338
- // Unicode SCRIPT CAPITAL P
11339
- ["eth", "\\eth", 240],
11340
- ["invertedOhm", "\\mho", 8487],
11341
- // Unicode INVERTED OHM SIGN
11342
- ["hBar", "\\hbar", 295],
11343
- // Unicode LATIN SMALL LETTER H WITH STROKE
11344
- ["hSlash", "\\hslash", 8463],
11345
- // Unicode PLANCK CONSTANT OVER TWO PI
11346
- // Symbols
11347
- ["blackClubSuit", "\\clubsuit", 9827],
11348
- ["whiteHeartSuit", "\\heartsuit", 9825],
11349
- ["blackSpadeSuit", "\\spadesuit", 9824],
11350
- ["whiteDiamondSuit", "\\diamondsuit", 9826],
11351
- ["sharp", "\\sharp", 9839],
11352
- ["flat", "\\flat", 9837],
11353
- ["natural", "\\natural", 9838]
11354
- ];
11355
- var DEFINITIONS_SYMBOLS = [
11356
- ...SYMBOLS2.map(([symbol2, latex, _codepoint]) => {
11357
- return {
11358
- kind: "symbol",
11359
- name: symbol2,
11360
- latexTrigger: [latex],
11361
- parse: symbol2
11362
- };
11363
- }),
11364
- ...SYMBOLS2.map(([symbol2, _latex, codepoint]) => {
11365
- return {
11366
- kind: "symbol",
11367
- latexTrigger: [String.fromCodePoint(codepoint)],
11368
- parse: symbol2
11369
- };
11370
- })
11371
- ];
11372
-
11373
- // src/compute-engine/latex-syntax/dictionary/definitions-algebra.ts
11374
- var DEFINITIONS_ALGEBRA = [
11375
- {
11376
- name: "To",
11377
- latexTrigger: ["\\to"],
11378
- kind: "infix",
11379
- precedence: 270
11380
- // MathML rightwards arrow
11381
- },
11382
- {
11383
- // Non-strict mode: -> for maps-to arrow
11384
- latexTrigger: ["-", ">"],
11385
- kind: "infix",
11386
- precedence: 270,
11387
- parse: (parser, lhs, until) => {
11388
- if (parser.options.strict !== false) return null;
11389
- const rhs = parser.parseExpression({ ...until, minPrec: 270 });
11390
- if (rhs === null) return null;
11391
- return ["To", lhs, rhs];
11392
- }
11393
- }
11394
- ];
11395
-
11396
- // src/compute-engine/latex-syntax/dictionary/definitions-logic.ts
11397
- var DEFINITIONS_LOGIC = [
11398
- // Constants
11399
- {
11400
- name: "True",
11401
- kind: "symbol",
11402
- latexTrigger: ["\\top"]
11403
- // ⊤ U+22A4
11404
- },
11405
- {
11406
- kind: "symbol",
11407
- latexTrigger: "\\mathrm{True}",
11408
- parse: "True"
11409
- },
11410
- {
11411
- kind: "symbol",
11412
- latexTrigger: "\\operatorname{True}",
11413
- parse: "True"
11414
- },
11415
- {
11416
- kind: "symbol",
11417
- latexTrigger: "\\mathsf{T}",
11418
- parse: "True"
11419
- },
11420
- {
11421
- name: "False",
11422
- kind: "symbol",
11423
- latexTrigger: ["\\bot"]
11424
- // ⊥ U+22A5
11425
- },
11426
- {
11427
- kind: "symbol",
11428
- latexTrigger: "\\operatorname{False}",
11429
- parse: "False"
11430
- },
11431
- {
11432
- kind: "symbol",
11433
- latexTrigger: "\\mathsf{F}",
11434
- parse: "False"
11435
- },
11436
- // Operators
11437
- // Logic operators have lower precedence than comparisons (245)
11438
- // so that `x = 1 \lor x = 2` parses as `(x = 1) \lor (x = 2)`
11439
- // See https://github.com/cortex-js/compute-engine/issues/243
11440
- {
11441
- name: "And",
11442
- kind: "infix",
11443
- latexTrigger: ["\\land"],
11444
- precedence: 235
11445
- // serialize: '\\land',
11446
- },
11447
- { kind: "infix", latexTrigger: ["\\wedge"], parse: "And", precedence: 235 },
11448
- { kind: "infix", latexTrigger: "\\&", parse: "And", precedence: 235 },
11449
- {
11450
- kind: "infix",
11451
- latexTrigger: "\\operatorname{and}",
11452
- parse: "And",
11453
- precedence: 235
11454
- },
11455
- {
11456
- name: "Or",
11457
- kind: "infix",
11458
- latexTrigger: ["\\lor"],
11459
- precedence: 230
11460
- },
11461
- { kind: "infix", latexTrigger: ["\\vee"], parse: "Or", precedence: 230 },
11462
- { kind: "infix", latexTrigger: "\\parallel", parse: "Or", precedence: 230 },
11463
- {
11464
- kind: "infix",
11465
- latexTrigger: "\\operatorname{or}",
11466
- parse: "Or",
11467
- precedence: 230
11468
- },
11469
- {
11470
- name: "Xor",
11471
- kind: "infix",
11472
- latexTrigger: ["\\veebar"],
11473
- precedence: 232
11474
- },
11475
- // Possible alt: \oplus ⊕ U+2295
11476
- {
11477
- name: "Not",
11478
- kind: "prefix",
11479
- latexTrigger: ["\\lnot"],
11480
- precedence: 880
11481
- },
11482
- {
11483
- kind: "prefix",
11484
- latexTrigger: ["\\neg"],
11485
- parse: "Not",
11486
- precedence: 880
11487
- },
11488
- {
11489
- name: "Nand",
11490
- kind: "infix",
11491
- latexTrigger: ["\\barwedge"],
11492
- precedence: 232
11493
- // serialize: '\\mid',
11494
- },
11495
- {
11496
- name: "Nor",
11497
- kind: "infix",
11498
- latexTrigger: ["\u22BD"],
11499
- // bar vee
11500
- precedence: 232
11501
- // serialize: '\\downarrow',
11502
- },
11503
- // Functions
11504
- {
11505
- kind: "function",
11506
- symbolTrigger: "and",
11507
- parse: "And"
11508
- },
11509
- {
11510
- kind: "function",
11511
- symbolTrigger: "or",
11512
- parse: "Or"
11513
- },
11514
- {
11515
- kind: "function",
11516
- symbolTrigger: "not",
11517
- parse: "Not"
11518
- },
11519
- // Relations
11520
- {
11521
- name: "Implies",
11522
- kind: "infix",
11523
- precedence: 220,
11524
- associativity: "right",
11525
- latexTrigger: ["\\implies"],
11526
- serialize: "\\implies"
11527
- },
11528
- {
11529
- latexTrigger: ["\\Rightarrow"],
11530
- kind: "infix",
11531
- precedence: 220,
11532
- associativity: "right",
11533
- parse: "Implies"
11534
- },
11535
- {
11536
- latexTrigger: ["\\rightarrow"],
11537
- kind: "infix",
11538
- precedence: 220,
11539
- associativity: "right",
11540
- parse: "Implies"
11541
- },
11542
- {
11543
- latexTrigger: ["\\Longrightarrow"],
11544
- kind: "infix",
11545
- precedence: 220,
11546
- associativity: "right",
11547
- parse: "Implies"
11548
- },
11549
- {
11550
- latexTrigger: ["\\longrightarrow"],
11551
- kind: "infix",
11552
- precedence: 220,
11553
- associativity: "right",
11554
- parse: "Implies"
11555
- },
11556
- {
11557
- // Non-strict mode: => for implies
11558
- latexTrigger: ["=", ">"],
11559
- kind: "infix",
11560
- precedence: 220,
11561
- associativity: "right",
11562
- parse: (parser, lhs, until) => {
11563
- if (parser.options.strict !== false) return null;
11564
- const rhs = parser.parseExpression({ ...until, minPrec: 220 });
11565
- if (rhs === null) return null;
11566
- return ["Implies", lhs, rhs];
11567
- }
11568
- },
11569
- {
11570
- name: "Equivalent",
11571
- // MathML: identical to, Mathematica: Congruent
11572
- latexTrigger: ["\\iff"],
11573
- kind: "infix",
11574
- associativity: "right",
11575
- precedence: 219
11576
- },
11577
- {
11578
- latexTrigger: ["\\Leftrightarrow"],
11579
- kind: "infix",
11580
- associativity: "right",
11581
- precedence: 219,
11582
- parse: "Equivalent"
11583
- },
11584
- {
11585
- latexTrigger: ["\\leftrightarrow"],
11586
- kind: "infix",
11587
- associativity: "right",
11588
- precedence: 219,
11589
- parse: "Equivalent"
11590
- },
11591
- {
11592
- latexTrigger: ["\\Longleftrightarrow"],
11593
- kind: "infix",
11594
- associativity: "right",
11595
- precedence: 219,
11596
- parse: "Equivalent"
11597
- },
11891
+ // Note GREEK PHI SYMBOL, but common usage in math
11892
+ ["phiLetter", "\\varphi", 966],
11893
+ ["upsilon", "\\upsilon", 965],
11894
+ ["chi", "\\chi", 967],
11895
+ ["psi", "\\psi", 968],
11896
+ ["omega", "\\omega", 969],
11897
+ ["Alpha", "\\Alpha", 913],
11898
+ ["Beta", "\\Beta", 914],
11899
+ ["Gamma", "\\Gamma", 915],
11900
+ ["Delta", "\\Delta", 916],
11901
+ ["Epsilon", "\\Epsilon", 917],
11902
+ ["Zeta", "\\Zeta", 918],
11903
+ ["Eta", "\\Eta", 919],
11904
+ ["Theta", "\\Theta", 920],
11905
+ ["Iota", "\\Iota", 921],
11906
+ ["Kappa", "\\Kappa", 922],
11907
+ ["Lambda", "\\Lambda", 923],
11908
+ ["Mu", "\\Mu", 924],
11909
+ ["Nu", "\\Nu", 925],
11910
+ ["Xi", "\\Xi", 926],
11911
+ ["Omicron", "\\Omicron", 927],
11912
+ // ['Pi', '\\Pi', 0x03a0],
11913
+ ["Rho", "\\Rho", 929],
11914
+ ["Sigma", "\\Sigma", 931],
11915
+ ["Tau", "\\Tau", 932],
11916
+ ["Phi", "\\Phi", 934],
11917
+ ["Upsilon", "\\Upsilon", 933],
11918
+ ["Chi", "\\Chi", 935],
11919
+ ["Psi", "\\Psi", 936],
11920
+ ["Omega", "\\Omega", 937],
11921
+ ["digamma", "\\digamma", 989],
11922
+ // Hebrew
11923
+ ["aleph", "\\aleph", 8501],
11924
+ // Unicode ALEF SYMBOL
11925
+ ["bet", "\\beth", 8502],
11926
+ ["gimel", "\\gimel", 8503],
11927
+ ["dalet", "\\daleth", 8504],
11928
+ // Letter-like
11929
+ ["ell", "\\ell", 8499],
11930
+ // Unicode SCRIPT SMALL L
11931
+ ["turnedCapitalF", "\\Finv", 8498],
11932
+ // Unicode TURNED CAPITAL F'
11933
+ ["turnedCapitalG", "\\Game", 8513],
11934
+ // TURNED SANS-SERIF CAPITAL G
11935
+ ["weierstrass", "\\wp", 8472],
11936
+ // Unicode SCRIPT CAPITAL P
11937
+ ["eth", "\\eth", 240],
11938
+ ["invertedOhm", "\\mho", 8487],
11939
+ // Unicode INVERTED OHM SIGN
11940
+ ["hBar", "\\hbar", 295],
11941
+ // Unicode LATIN SMALL LETTER H WITH STROKE
11942
+ ["hSlash", "\\hslash", 8463],
11943
+ // Unicode PLANCK CONSTANT OVER TWO PI
11944
+ // Symbols
11945
+ ["blackClubSuit", "\\clubsuit", 9827],
11946
+ ["whiteHeartSuit", "\\heartsuit", 9825],
11947
+ ["blackSpadeSuit", "\\spadesuit", 9824],
11948
+ ["whiteDiamondSuit", "\\diamondsuit", 9826],
11949
+ ["sharp", "\\sharp", 9839],
11950
+ ["flat", "\\flat", 9837],
11951
+ ["natural", "\\natural", 9838]
11952
+ ];
11953
+ var DEFINITIONS_SYMBOLS = [
11954
+ ...SYMBOLS2.map(([symbol2, latex, _codepoint]) => {
11955
+ return {
11956
+ kind: "symbol",
11957
+ name: symbol2,
11958
+ latexTrigger: [latex],
11959
+ parse: symbol2
11960
+ };
11961
+ }),
11962
+ ...SYMBOLS2.map(([symbol2, _latex, codepoint]) => {
11963
+ return {
11964
+ kind: "symbol",
11965
+ latexTrigger: [String.fromCodePoint(codepoint)],
11966
+ parse: symbol2
11967
+ };
11968
+ })
11969
+ ];
11970
+
11971
+ // src/compute-engine/latex-syntax/dictionary/definitions-algebra.ts
11972
+ var DEFINITIONS_ALGEBRA = [
11598
11973
  {
11599
- latexTrigger: ["\\longleftrightarrow"],
11974
+ name: "To",
11975
+ latexTrigger: ["\\to"],
11600
11976
  kind: "infix",
11601
- associativity: "right",
11602
- precedence: 219,
11603
- parse: "Equivalent"
11977
+ precedence: 270
11978
+ // MathML rightwards arrow
11604
11979
  },
11605
11980
  {
11606
- // Non-strict mode: <=> for equivalence
11607
- latexTrigger: ["<", "=", ">"],
11981
+ // Non-strict mode: -> for maps-to arrow
11982
+ latexTrigger: ["-", ">"],
11608
11983
  kind: "infix",
11609
- precedence: 219,
11610
- associativity: "right",
11984
+ precedence: 270,
11611
11985
  parse: (parser, lhs, until) => {
11612
11986
  if (parser.options.strict !== false) return null;
11613
- const rhs = parser.parseExpression({ ...until, minPrec: 219 });
11987
+ const rhs = parser.parseExpression({ ...until, minPrec: 270 });
11614
11988
  if (rhs === null) return null;
11615
- return ["Equivalent", lhs, rhs];
11616
- }
11617
- },
11618
- {
11619
- latexTrigger: ["\\equiv"],
11620
- kind: "infix",
11621
- associativity: "right",
11622
- precedence: 219,
11623
- parse: (parser, lhs, terminator) => {
11624
- const rhs = parser.parseExpression({ ...terminator, minPrec: 219 });
11625
- const index = parser.index;
11626
- const modulus = parser.parseExpression({ ...terminator, minPrec: 219 });
11627
- if (modulus !== null && operator(modulus) === "Mod")
11628
- return ["Congruent", lhs, rhs, missingIfEmpty(operand(modulus, 1))];
11629
- parser.index = index;
11630
- return ["Equivalent", lhs, missingIfEmpty(rhs)];
11631
- }
11632
- },
11633
- {
11634
- name: "Proves",
11635
- kind: "infix",
11636
- latexTrigger: ["\\vdash"],
11637
- precedence: 220,
11638
- associativity: "right",
11639
- serialize: "\\vdash"
11640
- },
11641
- {
11642
- name: "Entails",
11643
- kind: "infix",
11644
- latexTrigger: ["\\vDash"],
11645
- precedence: 220,
11646
- associativity: "right",
11647
- serialize: "\\vDash"
11648
- },
11649
- {
11650
- name: "Satisfies",
11651
- kind: "infix",
11652
- latexTrigger: ["\\models"],
11653
- precedence: 220,
11654
- associativity: "right",
11655
- serialize: "\\models"
11656
- },
11657
- // Quantifiers: for all, exists
11658
- {
11659
- name: "ForAll",
11660
- kind: "prefix",
11661
- latexTrigger: ["\\forall"],
11662
- precedence: 200,
11663
- // Has to be lower than COMPARISON_PRECEDENCE
11664
- serialize: serializeQuantifier("\\forall"),
11665
- parse: parseQuantifier("ForAll")
11666
- },
11667
- {
11668
- name: "Exists",
11669
- kind: "prefix",
11670
- latexTrigger: ["\\exists"],
11671
- precedence: 200,
11672
- // Has to be lower than COMPARISON_PRECEDENCE,
11673
- serialize: serializeQuantifier("\\exists"),
11674
- parse: parseQuantifier("Exists")
11675
- },
11676
- {
11677
- name: "ExistsUnique",
11678
- kind: "prefix",
11679
- latexTrigger: ["\\exists", "!"],
11680
- precedence: 200,
11681
- // Has to be lower than COMPARISON_PRECEDENCE,
11682
- serialize: serializeQuantifier("\\exists!"),
11683
- parse: parseQuantifier("ExistsUnique")
11684
- },
11685
- {
11686
- name: "NotForAll",
11687
- kind: "prefix",
11688
- latexTrigger: ["\\lnot", "\\forall"],
11689
- precedence: 200,
11690
- // Has to be lower than COMPARISON_PRECEDENCE
11691
- serialize: serializeQuantifier("\\lnot\\forall"),
11692
- parse: parseQuantifier("NotForAll")
11693
- },
11694
- {
11695
- name: "NotExists",
11696
- kind: "prefix",
11697
- latexTrigger: ["\\lnot", "\\exists"],
11698
- precedence: 200,
11699
- // Has to be lower than COMPARISON_PRECEDENCE,
11700
- serialize: serializeQuantifier("\\lnot\\exists"),
11701
- parse: parseQuantifier("NotExists")
11702
- },
11703
- {
11704
- name: "KroneckerDelta",
11705
- kind: "prefix",
11706
- latexTrigger: ["\\delta", "_"],
11707
- precedence: 200,
11708
- serialize: (serializer, expr2) => {
11709
- const args = operands(expr2);
11710
- if (args.length === 0) return "\\delta";
11711
- if (args.every((x) => symbol(x)))
11712
- return `\\delta_{${args.map((arg) => serializer.serialize(arg)).join("")}}`;
11713
- return `\\delta_{${args.map((arg) => serializer.serialize(arg)).join(", ")}}`;
11714
- },
11715
- parse: (parser) => {
11716
- const group = parser.parseGroup();
11717
- if (group === null) {
11718
- const token = parser.parseToken();
11719
- if (!token) return null;
11720
- return ["KroneckerDelta", token];
11721
- }
11722
- const seq = getSequence(group);
11723
- if (seq && seq.length <= 2) return ["KroneckerDelta", ...seq];
11724
- if (operator(group) === "InvisibleOperator")
11725
- return ["KroneckerDelta", ...operands(group)];
11726
- if (group !== null) return ["KroneckerDelta", group];
11727
- return null;
11728
- }
11729
- },
11730
- // Iverson brackets. Also called the "indicator function"
11731
- // Must have a single argument, a relational expression, i.e.
11732
- // `[ a = b ]` or `[ x \leq 0 ]`
11733
- // Otherwise, it gets rejected, it could be something else, like a list or
11734
- // tuple.
11735
- {
11736
- name: "Boole",
11737
- kind: "matchfix",
11738
- openTrigger: "[",
11739
- closeTrigger: "]",
11740
- // serialize: (serializer: Serializer, expr: MathJsonExpression) => {
11741
- // const args = ops(expr);
11742
- // return `[${serializer.serialize(arg)}]`;
11743
- // },
11744
- parse: (_parser, body) => {
11745
- const h = operator(body);
11746
- if (!h) return null;
11747
- if (!DEFINITIONS_INEQUALITIES.some((x) => x.name === h)) return null;
11748
- return ["Boole", body];
11749
- }
11750
- },
11751
- {
11752
- kind: "matchfix",
11753
- openTrigger: "\\llbracket",
11754
- closeTrigger: "\\rrbracket",
11755
- parse: (_parser, body) => {
11756
- const h = operator(body);
11757
- if (!h) return null;
11758
- if (!DEFINITIONS_INEQUALITIES.some((x) => x.name === h)) return null;
11759
- return ["Boole", body];
11760
- }
11761
- },
11762
- // Predicate application in First-Order Logic.
11763
- // ["Predicate", "P", "x", "y"] serializes to "P(x, y)"
11764
- {
11765
- name: "Predicate",
11766
- serialize: (serializer, expr2) => {
11767
- const args = operands(expr2);
11768
- if (args.length === 0) return "";
11769
- const pred = args[0];
11770
- const predStr = typeof pred === "string" ? pred : serializer.serialize(pred);
11771
- if (args.length === 1) return predStr;
11772
- const argStrs = args.slice(1).map((arg) => serializer.serialize(arg));
11773
- return `${predStr}(${argStrs.join(", ")})`;
11989
+ return ["To", lhs, rhs];
11774
11990
  }
11775
11991
  }
11776
11992
  ];
11777
- function serializeQuantifier(quantifierSymbol) {
11778
- return (serializer, expr2) => {
11779
- const args = operands(expr2);
11780
- if (args.length === 0) return quantifierSymbol;
11781
- if (args.length === 1)
11782
- return `${quantifierSymbol} ${serializer.serialize(args[0])}`;
11783
- const boundVar = serializer.serialize(args[0]);
11784
- const body = serializer.serialize(args[1]);
11785
- return `${quantifierSymbol} ${boundVar}, ${body}`;
11786
- };
11787
- }
11788
- function tightBindingCondition(p, terminator) {
11789
- return p.peek === "\\to" || p.peek === "\\rightarrow" || p.peek === "\\implies" || p.peek === "\\Rightarrow" || p.peek === "\\iff" || p.peek === "\\Leftrightarrow" || p.peek === "\\land" || p.peek === "\\wedge" || p.peek === "\\lor" || p.peek === "\\vee" || (terminator.condition?.(p) ?? false);
11790
- }
11791
- function parseQuantifier(kind) {
11792
- return (parser, terminator) => {
11793
- const index = parser.index;
11794
- const useTightBinding = parser.options.quantifierScope !== "loose";
11795
- const symbol2 = parser.parseSymbol(terminator);
11796
- if (symbol2) {
11797
- parser.skipSpace();
11798
- if (parser.match(",") || parser.match("\\mid") || parser.match(".") || parser.match(":") || parser.match("\\colon")) {
11799
- const bodyTerminator = useTightBinding ? {
11800
- ...terminator,
11801
- condition: (p) => tightBindingCondition(p, terminator)
11802
- } : terminator;
11803
- parser.enterQuantifierScope();
11804
- const body2 = parser.parseExpression(bodyTerminator);
11805
- parser.exitQuantifierScope();
11806
- return [kind, symbol2, missingIfEmpty(body2)];
11807
- }
11808
- parser.enterQuantifierScope();
11809
- const body = parser.parseEnclosure();
11810
- parser.exitQuantifierScope();
11811
- if (body) return [kind, symbol2, missingIfEmpty(body)];
11812
- }
11813
- parser.index = index;
11814
- const condTerminator = {
11815
- ...terminator,
11816
- condition: (p) => p.peek === ":" || p.peek === "\\colon" || (terminator.condition?.(p) ?? false)
11817
- };
11818
- const condition = parser.parseExpression(condTerminator);
11819
- if (condition === null) return null;
11820
- parser.skipSpace();
11821
- if (parser.matchAny([",", "\\mid", ":", "\\colon"])) {
11822
- const bodyTerminator = useTightBinding ? {
11823
- ...terminator,
11824
- condition: (p) => tightBindingCondition(p, terminator)
11825
- } : terminator;
11826
- parser.enterQuantifierScope();
11827
- const body = parser.parseExpression(bodyTerminator);
11828
- parser.exitQuantifierScope();
11829
- return [kind, condition, missingIfEmpty(body)];
11830
- }
11831
- if (parser.match("(")) {
11832
- parser.enterQuantifierScope();
11833
- const body = parser.parseExpression(terminator);
11834
- parser.exitQuantifierScope();
11835
- if (!parser.match(")")) return null;
11836
- return [kind, condition, missingIfEmpty(body)];
11837
- }
11838
- return null;
11839
- };
11840
- }
11841
11993
 
11842
11994
  // src/compute-engine/latex-syntax/dictionary/definitions-sets.ts
11843
11995
  function parseIntervalBody(body, openLeft, openRight) {
@@ -16343,7 +16495,7 @@ var ComputeEngine = (() => {
16343
16495
  var FLAG_SEQUENCE = "\\p{RI}\\p{RI}";
16344
16496
  var TAG_MOD = `(?:[\\u{E0020}-\\u{E007E}]+\\u{E007F})`;
16345
16497
  var EMOJI_MOD = `(?:\\p{EMod}|${VS16}${KEYCAP}?|${TAG_MOD})`;
16346
- var EMOJI_NOT_SYMBOL = `(?:(?=\\P{XIDC})\\p{Emoji})`;
16498
+ var EMOJI_NOT_SYMBOL = `(?:(?=\\P{XIDC})(?=[^\\x23\\x2a\\x30-\\x39])\\p{Emoji})`;
16347
16499
  var ZWJ_ELEMENT = `(?:${EMOJI_NOT_SYMBOL}${EMOJI_MOD}*|\\p{Emoji}${EMOJI_MOD}+|${FLAG_SEQUENCE})`;
16348
16500
  var POSSIBLE_EMOJI = `(?:${ZWJ_ELEMENT})(${ZWJ2}${ZWJ_ELEMENT})*`;
16349
16501
  var SOME_EMOJI = new RegExp(`(?:${POSSIBLE_EMOJI})+`, "u");
@@ -17052,7 +17204,9 @@ var ComputeEngine = (() => {
17052
17204
  }
17053
17205
  return c;
17054
17206
  }
17055
- return parser.nextToken();
17207
+ const raw = parser.peek;
17208
+ if (raw && /^[\p{XIDC}\p{M}]/u.test(raw)) return parser.nextToken();
17209
+ return null;
17056
17210
  }
17057
17211
  function parseSymbolBody(parser) {
17058
17212
  let id = matchPrefixedSymbol(parser);
@@ -18342,6 +18496,29 @@ var ComputeEngine = (() => {
18342
18496
  this.index = start;
18343
18497
  return null;
18344
18498
  }
18499
+ /**
18500
+ * Speculatively check if any \text infix entry (e.g. "and", "or", "where")
18501
+ * would match the upcoming tokens. This is used to prevent InvisibleOperator
18502
+ * from consuming \text{keyword} as a text run when the keyword is actually
18503
+ * an infix operator that was skipped due to precedence constraints.
18504
+ *
18505
+ * Returns true if any entry's parse function would succeed (non-null result).
18506
+ * The parser index is always restored to its original position.
18507
+ */
18508
+ wouldMatchTextInfix(opDefs) {
18509
+ const start = this.index;
18510
+ for (const [def, n] of opDefs) {
18511
+ if (def.kind !== "infix") continue;
18512
+ this.index = start + n;
18513
+ const result = def.parse(this, "Nothing", { minPrec: 0 });
18514
+ if (result !== null) {
18515
+ this.index = start;
18516
+ return true;
18517
+ }
18518
+ }
18519
+ this.index = start;
18520
+ return false;
18521
+ }
18345
18522
  /**
18346
18523
  * This returns an array of arguments (as in a function application),
18347
18524
  * or null if there is no match.
@@ -18967,6 +19144,7 @@ var ComputeEngine = (() => {
18967
19144
  if (isDelimiterCommand(this))
18968
19145
  return this.error("unexpected-delimiter", start);
18969
19146
  if (command[0] !== "\\") {
19147
+ this.nextToken();
18970
19148
  return this.error(
18971
19149
  ["unexpected-token", { str: tokensToString(command) }],
18972
19150
  start
@@ -19095,25 +19273,28 @@ var ComputeEngine = (() => {
19095
19273
  if (result === null && until.minPrec <= INVISIBLE_OP_PRECEDENCE) {
19096
19274
  const opDefs = this.peekDefinitions("operator");
19097
19275
  if (opDefs.length === 0 || opDefs.every(([def]) => def.latexTrigger === "\\text")) {
19098
- const rhs = this.parseExpression({
19099
- ...until,
19100
- minPrec: INVISIBLE_OP_PRECEDENCE + 1
19101
- });
19102
- if (rhs !== null) {
19103
- if (operator(lhs) === "InvisibleOperator") {
19104
- if (operator(rhs) === "InvisibleOperator")
19105
- result = [
19106
- "InvisibleOperator",
19107
- ...operands(lhs),
19108
- ...operands(rhs)
19109
- ];
19110
- else result = ["InvisibleOperator", ...operands(lhs), rhs];
19111
- } else if (operator(rhs) === "InvisibleOperator") {
19112
- result = ["InvisibleOperator", lhs, ...operands(rhs)];
19113
- } else result = ["InvisibleOperator", lhs, rhs];
19276
+ if (opDefs.length > 0 && this.wouldMatchTextInfix(opDefs)) {
19114
19277
  } else {
19115
- if (result === null) {
19116
- result = this.options.parseUnexpectedToken?.(lhs, this) ?? null;
19278
+ const rhs = this.parseExpression({
19279
+ ...until,
19280
+ minPrec: INVISIBLE_OP_PRECEDENCE + 1
19281
+ });
19282
+ if (rhs !== null) {
19283
+ if (operator(lhs) === "InvisibleOperator") {
19284
+ if (operator(rhs) === "InvisibleOperator")
19285
+ result = [
19286
+ "InvisibleOperator",
19287
+ ...operands(lhs),
19288
+ ...operands(rhs)
19289
+ ];
19290
+ else result = ["InvisibleOperator", ...operands(lhs), rhs];
19291
+ } else if (operator(rhs) === "InvisibleOperator") {
19292
+ result = ["InvisibleOperator", lhs, ...operands(rhs)];
19293
+ } else result = ["InvisibleOperator", lhs, rhs];
19294
+ } else {
19295
+ if (result === null) {
19296
+ result = this.options.parseUnexpectedToken?.(lhs, this) ?? null;
19297
+ }
19117
19298
  }
19118
19299
  }
19119
19300
  }
@@ -30578,8 +30759,7 @@ ${lines.join("\n")}`;
30578
30759
  broadcastable: true,
30579
30760
  idempotent: true,
30580
30761
  complexity: 1200,
30581
- signature: "(number) -> number",
30582
- type: ([x]) => x.type,
30762
+ signature: "(number) -> real",
30583
30763
  sgn: ([x]) => {
30584
30764
  if (x.isSame(0)) return "zero";
30585
30765
  if (isNumber(x)) return "positive";
@@ -37938,6 +38118,10 @@ ${e.message}
37938
38118
  scoped: true,
37939
38119
  lazy: true,
37940
38120
  signature: "(expression, variable:symbol, variables:symbol+) -> expression",
38121
+ type: ([body]) => {
38122
+ if (body && body.type.matches("number")) return body.type;
38123
+ return void 0;
38124
+ },
37941
38125
  canonical: (ops, { engine: ce, scope }) => {
37942
38126
  if (isSymbol2(ops[0]) && ops[0].canonical.operatorDefinition) {
37943
38127
  const vars = ops.slice(1);
@@ -42509,6 +42693,18 @@ ${e.message}
42509
42693
  }
42510
42694
  }
42511
42695
  ops = flattenInvisibleOperator(ops);
42696
+ if (ops.some((op) => isFunction2(op, "Text") || isString(op))) {
42697
+ const runs = [];
42698
+ for (const op of ops) {
42699
+ if (isFunction2(op, "Text")) {
42700
+ runs.push(...op.ops);
42701
+ } else if (op.operator !== "HorizontalSpacing") {
42702
+ runs.push(op.canonical);
42703
+ }
42704
+ }
42705
+ return ce._fn("Text", runs);
42706
+ }
42707
+ ops = combineFunctionApplications(ce, ops);
42512
42708
  {
42513
42709
  const significant = ops.filter((x) => x.operator !== "HorizontalSpacing");
42514
42710
  if (significant.length === 2) {
@@ -42523,7 +42719,7 @@ ${e.message}
42523
42719
  }
42524
42720
  ops = flatten(ops);
42525
42721
  if (ops.every(
42526
- (x) => x.isValid && (x.type.isUnknown || x.type.matches("number") || x.isIndexedCollection && !isString(x))
42722
+ (x) => x.isValid && (x.type.isUnknown || x.type.type === "any" || x.type.type === "expression" || x.type.matches("number") || x.isIndexedCollection && !isString(x))
42527
42723
  )) {
42528
42724
  return ce._fn("Multiply", ops);
42529
42725
  }
@@ -42538,6 +42734,40 @@ ${e.message}
42538
42734
  }
42539
42735
  return ys;
42540
42736
  }
42737
+ function combineFunctionApplications(ce, ops) {
42738
+ const result = [];
42739
+ let i = 0;
42740
+ while (i < ops.length) {
42741
+ const op = ops[i];
42742
+ if (i < ops.length - 1 && isSymbol2(op) && isFunction2(ops[i + 1], "Delimiter")) {
42743
+ const symName = op.symbol;
42744
+ const def = ce.lookupDefinition(symName);
42745
+ const delim = ops[i + 1];
42746
+ if (def && (isOperatorDef(def) || def.value?.type?.matches("function"))) {
42747
+ let args = delim.op1 ? isFunction2(delim.op1, "Sequence") ? delim.op1.ops : [delim.op1] : [];
42748
+ args = flatten(args);
42749
+ result.push(ce.function(symName, args));
42750
+ i += 2;
42751
+ continue;
42752
+ }
42753
+ if (delim.op1 && isFunction2(delim.op1, "Sequence")) {
42754
+ let args = delim.op1.ops;
42755
+ args = flatten(args);
42756
+ if (args.length > 1) {
42757
+ if (!def) ce.declare(symName, "function");
42758
+ else if (!isOperatorDef(def) && def.value?.type?.isUnknown)
42759
+ op.canonical.infer("function");
42760
+ result.push(ce.function(symName, args));
42761
+ i += 2;
42762
+ continue;
42763
+ }
42764
+ }
42765
+ }
42766
+ result.push(ops[i]);
42767
+ i++;
42768
+ }
42769
+ return result;
42770
+ }
42541
42771
  function asInteger(expr2) {
42542
42772
  if (isNumber(expr2)) {
42543
42773
  const n = expr2.re;
@@ -43541,7 +43771,21 @@ ${e.message}
43541
43771
  },
43542
43772
  Text: {
43543
43773
  description: "A sequence of strings, annotated expressions and other Text expressions",
43544
- signature: "(any*) -> expression"
43774
+ signature: "(any*) -> string",
43775
+ evaluate: (ops, { engine: ce }) => {
43776
+ if (ops.length === 0) return ce.string("");
43777
+ const parts = [];
43778
+ for (const op of ops) {
43779
+ const unwrapped = op.operator === "Annotated" ? op.op1 : op;
43780
+ if (isString(unwrapped)) parts.push(unwrapped.string);
43781
+ else {
43782
+ const evaluated = unwrapped.evaluate();
43783
+ if (isString(evaluated)) parts.push(evaluated.string);
43784
+ else parts.push(evaluated.toString());
43785
+ }
43786
+ }
43787
+ return ce.string(parts.join(""));
43788
+ }
43545
43789
  }
43546
43790
  },
43547
43791
  {
@@ -43636,7 +43880,16 @@ ${e.message}
43636
43880
  if (!isSymbol2(symbol2)) {
43637
43881
  symbol2 = checkType(ce, lhs, "symbol");
43638
43882
  }
43639
- return ce._fn("Assign", [symbol2, args[1].canonical]);
43883
+ const canonRhs = args[1].canonical;
43884
+ const result = ce._fn("Assign", [symbol2, canonRhs]);
43885
+ const symbolName2 = sym(symbol2);
43886
+ if (symbolName2 && isFunction2(canonRhs, "Function")) {
43887
+ if (!ce.lookupDefinition(symbolName2)) ce.symbol(symbolName2);
43888
+ const def = ce.lookupDefinition(symbolName2);
43889
+ if (def && isValueDef(def) && def.value.inferredType)
43890
+ def.value.type = ce.type("function");
43891
+ }
43892
+ return result;
43640
43893
  },
43641
43894
  evaluate: ([op1, op2], { engine: ce }) => {
43642
43895
  if (isFunction2(op1, "Subscript") && sym(op1.op1)) {
@@ -43833,6 +44086,7 @@ ${e.message}
43833
44086
  description: "Simplify an expression.",
43834
44087
  lazy: true,
43835
44088
  signature: "(any) -> expression",
44089
+ type: ([x]) => x?.type ?? void 0,
43836
44090
  canonical: (ops, { engine: ce }) => ce._fn("Simplify", checkArity(ce, ops, 1)),
43837
44091
  evaluate: ([x]) => x.simplify() ?? void 0
43838
44092
  },
@@ -54056,8 +54310,11 @@ Error in definition of "${name}"`,
54056
54310
  for (const local of locals) {
54057
54311
  for (const arg of args) {
54058
54312
  if (isFunction2(arg, "Assign") && isSymbol2(arg.ops[0], local)) {
54059
- if (_BaseCompiler.isComplexValued(arg.ops[1])) {
54313
+ const rhs = arg.ops[1];
54314
+ if (_BaseCompiler.isComplexValued(rhs)) {
54060
54315
  typeHints[local] = isWGSL ? "vec2f" : "vec2";
54316
+ } else if (_BaseCompiler.isIntegerValued(rhs)) {
54317
+ typeHints[local] = isWGSL ? "i32" : "int";
54061
54318
  }
54062
54319
  break;
54063
54320
  }
@@ -54262,12 +54519,9 @@ Error in definition of "${name}"`,
54262
54519
  /**
54263
54520
  * Determine at compile time whether an expression produces a complex value.
54264
54521
  *
54265
- * Rules:
54266
- * - Numbers: complex if im !== 0
54267
- * - Symbols: ImaginaryUnit is complex; others use expr.isReal
54268
- * (undefined is treated as real -- assume-real policy)
54269
- * - Functions: Abs, Arg, Re, Im always return real.
54270
- * All others: complex if any operand is complex.
54522
+ * Uses the expression's declared type (from operator signatures) when
54523
+ * available. Falls back to operand inspection for functions whose
54524
+ * return type is unknown.
54271
54525
  */
54272
54526
  static isComplexValued(expr2) {
54273
54527
  if (isNumber(expr2)) return expr2.im !== 0;
@@ -54278,13 +54532,24 @@ Error in definition of "${name}"`,
54278
54532
  return t.matches("complex") && !t.matches("real");
54279
54533
  }
54280
54534
  if (isFunction2(expr2)) {
54281
- const op = expr2.operator;
54282
- if (op === "Abs" || op === "Arg" || op === "Re" || op === "Im")
54283
- return false;
54535
+ const t = expr2.type;
54536
+ if (t.matches("complex") && !t.matches("real")) return true;
54537
+ if (t.matches("real")) return false;
54284
54538
  return expr2.ops.some((arg) => _BaseCompiler.isComplexValued(arg));
54285
54539
  }
54286
54540
  return false;
54287
54541
  }
54542
+ /** True if the expression is provably integer-typed. */
54543
+ static isIntegerValued(expr2) {
54544
+ if (isNumber(expr2)) return expr2.im === 0 && Number.isInteger(expr2.re);
54545
+ const t = expr2.type;
54546
+ return t ? t.matches("integer") : false;
54547
+ }
54548
+ /** True if the expression is provably non-negative (sign ≥ 0). */
54549
+ static isNonNegative(expr2) {
54550
+ if (isNumber(expr2)) return expr2.im === 0 && expr2.re >= 0;
54551
+ return expr2.isNonNegative === true;
54552
+ }
54288
54553
  /**
54289
54554
  * Generate a temporary variable name
54290
54555
  */
@@ -54538,14 +54803,27 @@ Error in definition of "${name}"`,
54538
54803
  } catch (e) {
54539
54804
  if (options?.fallback ?? true) {
54540
54805
  console.warn(
54541
- `Compilation fallback for "${expr2.operator}": ${e.message}`
54806
+ `Compilation fallback for "${expr2.operator}" (target: ${options?.to ?? "javascript"}): ${e.message}`
54542
54807
  );
54808
+ const ce = expr2.engine;
54809
+ const fallbackRun = ((vars) => {
54810
+ ce.pushScope();
54811
+ try {
54812
+ if (vars && typeof vars === "object") {
54813
+ for (const [k, v] of Object.entries(vars))
54814
+ ce.assign(k, v);
54815
+ }
54816
+ return expr2.evaluate().re;
54817
+ } finally {
54818
+ ce.popScope();
54819
+ }
54820
+ });
54543
54821
  return {
54544
54822
  target: options?.to ?? "javascript",
54545
54823
  success: false,
54546
54824
  code: "",
54547
54825
  calling: "expression",
54548
- run: applicableN1(expr2)
54826
+ run: fallbackRun
54549
54827
  };
54550
54828
  }
54551
54829
  throw e;
@@ -59695,6 +59973,86 @@ Error in definition of "${name}"`,
59695
59973
  for (const symbol2 of Object.values(commonSymbols)) symbol2?.reset();
59696
59974
  }
59697
59975
 
59976
+ // src/compute-engine/compilation/constant-folding.ts
59977
+ function formatFloat(n) {
59978
+ const str = n.toString();
59979
+ if (!str.includes(".") && !str.includes("e") && !str.includes("E")) {
59980
+ return `${str}.0`;
59981
+ }
59982
+ return str;
59983
+ }
59984
+ function tryGetConstant(expr2) {
59985
+ if (!isNumber(expr2)) return void 0;
59986
+ if (expr2.im !== 0) return void 0;
59987
+ const re = expr2.re;
59988
+ if (!isFinite(re)) return void 0;
59989
+ return re;
59990
+ }
59991
+ var NUMERIC_LITERAL_RE = /^-?\d+(\.\d+)?$/;
59992
+ function foldTerms(terms, identity, op) {
59993
+ const identityValue = op === "+" ? 0 : 1;
59994
+ let numericAcc = null;
59995
+ const symbolic = [];
59996
+ for (const term of terms) {
59997
+ if (NUMERIC_LITERAL_RE.test(term)) {
59998
+ const val = parseFloat(term);
59999
+ if (op === "*" && val === 0) return "0.0";
60000
+ if (numericAcc === null) {
60001
+ numericAcc = val;
60002
+ } else {
60003
+ numericAcc = op === "+" ? numericAcc + val : numericAcc * val;
60004
+ }
60005
+ } else {
60006
+ symbolic.push(term);
60007
+ }
60008
+ }
60009
+ if (numericAcc !== null && numericAcc !== identityValue) {
60010
+ symbolic.unshift(formatFloat(numericAcc));
60011
+ }
60012
+ if (symbolic.length === 0) {
60013
+ if (numericAcc !== null) return formatFloat(numericAcc);
60014
+ return identity;
60015
+ }
60016
+ if (symbolic.length === 1) return symbolic[0];
60017
+ return symbolic.join(op === "+" ? " + " : " * ");
60018
+ }
60019
+ function tryGetComplexParts(expr2, compile3) {
60020
+ if (isSymbol2(expr2, "ImaginaryUnit")) {
60021
+ return { re: null, im: "1.0" };
60022
+ }
60023
+ if (isNumber(expr2) && expr2.im !== 0) {
60024
+ const re = expr2.re;
60025
+ const im = expr2.im;
60026
+ return {
60027
+ re: re !== 0 ? formatFloat(re) : null,
60028
+ im: formatFloat(im)
60029
+ };
60030
+ }
60031
+ if (isFunction2(expr2, "Multiply")) {
60032
+ const ops = expr2.ops;
60033
+ const iIndex = ops.findIndex(
60034
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
60035
+ );
60036
+ if (iIndex >= 0) {
60037
+ const iFactor = ops[iIndex];
60038
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
60039
+ const remaining = ops.filter((_, idx) => idx !== iIndex);
60040
+ if (remaining.length === 0) {
60041
+ return { re: null, im: formatFloat(iScale) };
60042
+ }
60043
+ const compiledFactors = remaining.map((r) => compile3(r));
60044
+ if (iScale !== 1)
60045
+ compiledFactors.unshift(formatFloat(iScale));
60046
+ const imCode = foldTerms(compiledFactors, "1.0", "*");
60047
+ return { re: null, im: imCode };
60048
+ }
60049
+ }
60050
+ if (BaseCompiler.isComplexValued(expr2)) {
60051
+ return null;
60052
+ }
60053
+ return { re: compile3(expr2), im: null };
60054
+ }
60055
+
59698
60056
  // src/compute-engine/compilation/javascript-target.ts
59699
60057
  var JAVASCRIPT_OPERATORS = {
59700
60058
  Add: ["+", 11],
@@ -59718,12 +60076,21 @@ Error in definition of "${name}"`,
59718
60076
  Abs: (args, compile3) => {
59719
60077
  if (BaseCompiler.isComplexValued(args[0]))
59720
60078
  return `_SYS.cabs(${compile3(args[0])})`;
60079
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
59721
60080
  return `Math.abs(${compile3(args[0])})`;
59722
60081
  },
59723
60082
  Add: (args, compile3) => {
59724
60083
  if (args.length === 1) return compile3(args[0]);
59725
60084
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
59726
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" + ")})`;
60085
+ if (!anyComplex) {
60086
+ const constants = args.map(tryGetConstant);
60087
+ if (constants.every((c) => c !== void 0))
60088
+ return String(constants.reduce((a, b) => a + b, 0));
60089
+ const nonZero = args.filter((a) => tryGetConstant(a) !== 0);
60090
+ if (nonZero.length === 0) return "0";
60091
+ if (nonZero.length === 1) return compile3(nonZero[0]);
60092
+ return `(${nonZero.map((x) => compile3(x)).join(" + ")})`;
60093
+ }
59727
60094
  const parts = args.map((a) => {
59728
60095
  const code = compile3(a);
59729
60096
  return { code, isComplex: BaseCompiler.isComplexValued(a) };
@@ -59780,7 +60147,10 @@ Error in definition of "${name}"`,
59780
60147
  return `Math.atan(${compile3(args[0])})`;
59781
60148
  },
59782
60149
  Artanh: "Math.atanh",
59783
- Ceil: "Math.ceil",
60150
+ Ceil: (args, compile3) => {
60151
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60152
+ return `Math.ceil(${compile3(args[0])})`;
60153
+ },
59784
60154
  Chop: "_SYS.chop",
59785
60155
  Cos: (args, compile3) => {
59786
60156
  if (BaseCompiler.isComplexValued(args[0]))
@@ -59823,7 +60193,10 @@ Error in definition of "${name}"`,
59823
60193
  return `_SYS.cexp(${compile3(args[0])})`;
59824
60194
  return `Math.exp(${compile3(args[0])})`;
59825
60195
  },
59826
- Floor: "Math.floor",
60196
+ Floor: (args, compile3) => {
60197
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60198
+ return `Math.floor(${compile3(args[0])})`;
60199
+ },
59827
60200
  Fract: ([x], compile3) => {
59828
60201
  if (x === null) throw new Error("Fract: no argument");
59829
60202
  return BaseCompiler.inlineExpression("${x} - Math.floor(${x})", compile3(x));
@@ -59919,12 +60292,20 @@ Error in definition of "${name}"`,
59919
60292
  if (BaseCompiler.isComplexValued(base) || BaseCompiler.isComplexValued(exp3)) {
59920
60293
  return `_SYS.cpow(${compile3(base)}, ${compile3(exp3)})`;
59921
60294
  }
59922
- const expVal = exp3.re;
59923
- if (expVal === 0.5) return `Math.sqrt(${compile3(base)})`;
59924
- if (expVal === 1 / 3) return `Math.cbrt(${compile3(base)})`;
59925
- if (expVal === 1) return compile3(base);
59926
- if (expVal === -1) return `(1 / (${compile3(base)}))`;
59927
- if (expVal === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
60295
+ const bConst = tryGetConstant(base);
60296
+ const eConst = tryGetConstant(exp3);
60297
+ if (bConst !== void 0 && eConst !== void 0)
60298
+ return String(Math.pow(bConst, eConst));
60299
+ if (eConst === 0) return "1";
60300
+ if (eConst === 1) return compile3(base);
60301
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
60302
+ const code = compile3(base);
60303
+ return `(${code} * ${code})`;
60304
+ }
60305
+ if (eConst === -1) return `(1 / (${compile3(base)}))`;
60306
+ if (eConst === 0.5) return `Math.sqrt(${compile3(base)})`;
60307
+ if (eConst === 1 / 3) return `Math.cbrt(${compile3(base)})`;
60308
+ if (eConst === -0.5) return `(1 / Math.sqrt(${compile3(base)}))`;
59928
60309
  return `Math.pow(${compile3(base)}, ${compile3(exp3)})`;
59929
60310
  },
59930
60311
  Range: (args, compile3) => {
@@ -59961,16 +60342,29 @@ Error in definition of "${name}"`,
59961
60342
  Root: ([arg, exp3], compile3) => {
59962
60343
  if (arg === null) throw new Error("Root: no argument");
59963
60344
  if (exp3 === null) return `Math.sqrt(${compile3(arg)})`;
59964
- if (exp3?.re === 2) return `Math.sqrt(${compile3(arg)})`;
59965
- if (exp3?.re === 3) return `Math.cbrt(${compile3(arg)})`;
59966
- if (!isNaN(exp3?.re)) return `Math.pow(${compile3(arg)}, ${1 / exp3.re})`;
60345
+ const aConst = tryGetConstant(arg);
60346
+ const nConst = tryGetConstant(exp3);
60347
+ if (aConst !== void 0 && nConst !== void 0 && nConst !== 0)
60348
+ return String(Math.pow(aConst, 1 / nConst));
60349
+ if (nConst === 2) return `Math.sqrt(${compile3(arg)})`;
60350
+ if (nConst === 3) return `Math.cbrt(${compile3(arg)})`;
60351
+ if (nConst !== void 0) return `Math.pow(${compile3(arg)}, ${1 / nConst})`;
59967
60352
  return `Math.pow(${compile3(arg)}, 1 / (${compile3(exp3)}))`;
59968
60353
  },
59969
60354
  Random: "Math.random",
59970
- Round: "Math.round",
60355
+ Round: (args, compile3) => {
60356
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60357
+ return `Math.round(${compile3(args[0])})`;
60358
+ },
59971
60359
  Square: (args, compile3) => {
59972
60360
  const arg = args[0];
59973
60361
  if (arg === null) throw new Error("Square: no argument");
60362
+ const c = tryGetConstant(arg);
60363
+ if (c !== void 0) return String(c * c);
60364
+ if (isSymbol2(arg)) {
60365
+ const code = compile3(arg);
60366
+ return `(${code} * ${code})`;
60367
+ }
59974
60368
  return `Math.pow(${compile3(arg)}, 2)`;
59975
60369
  },
59976
60370
  Sec: (args, compile3) => {
@@ -60003,6 +60397,8 @@ Error in definition of "${name}"`,
60003
60397
  Sqrt: (args, compile3) => {
60004
60398
  if (BaseCompiler.isComplexValued(args[0]))
60005
60399
  return `_SYS.csqrt(${compile3(args[0])})`;
60400
+ const c = tryGetConstant(args[0]);
60401
+ if (c !== void 0) return String(Math.sqrt(c));
60006
60402
  return `Math.sqrt(${compile3(args[0])})`;
60007
60403
  },
60008
60404
  Tan: (args, compile3) => {
@@ -60019,9 +60415,14 @@ Error in definition of "${name}"`,
60019
60415
  if (a === null || b === null) throw new Error("Mod: missing argument");
60020
60416
  const ca = compile3(a);
60021
60417
  const cb = compile3(b);
60418
+ if (BaseCompiler.isIntegerValued(a) && BaseCompiler.isIntegerValued(b) && BaseCompiler.isNonNegative(a))
60419
+ return `(${ca} % ${cb})`;
60022
60420
  return `((${ca} % ${cb}) + ${cb}) % ${cb}`;
60023
60421
  },
60024
- Truncate: "Math.trunc",
60422
+ Truncate: (args, compile3) => {
60423
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
60424
+ return `Math.trunc(${compile3(args[0])})`;
60425
+ },
60025
60426
  Remainder: ([a, b], compile3) => {
60026
60427
  if (a === null || b === null)
60027
60428
  throw new Error("Remainder: missing argument");
@@ -60029,25 +60430,20 @@ Error in definition of "${name}"`,
60029
60430
  a
60030
60431
  )} / ${compile3(b)}))`;
60031
60432
  },
60032
- // Arithmetic operators handled as functions for completeness
60033
- Subtract: ([a, b], compile3) => {
60034
- if (a === null || b === null) throw new Error("Subtract: missing argument");
60035
- const ac = BaseCompiler.isComplexValued(a);
60036
- const bc = BaseCompiler.isComplexValued(b);
60037
- if (!ac && !bc) return `(${compile3(a)} - ${compile3(b)})`;
60038
- const ca = compile3(a);
60039
- const cb = compile3(b);
60040
- const reA = ac ? `(${ca}).re` : ca;
60041
- const imA = ac ? `(${ca}).im` : "0";
60042
- const reB = bc ? `(${cb}).re` : cb;
60043
- const imB = bc ? `(${cb}).im` : "0";
60044
- return `({ re: ${reA} - ${reB}, im: ${imA} - ${imB} })`;
60045
- },
60433
+ // No Subtract function handler Subtract canonicalizes to Add+Negate.
60434
+ // The operator entry in JAVASCRIPT_OPERATORS handles any edge cases.
60046
60435
  Divide: ([a, b], compile3) => {
60047
60436
  if (a === null || b === null) throw new Error("Divide: missing argument");
60048
60437
  const ac = BaseCompiler.isComplexValued(a);
60049
60438
  const bc = BaseCompiler.isComplexValued(b);
60050
- if (!ac && !bc) return `(${compile3(a)} / ${compile3(b)})`;
60439
+ if (!ac && !bc) {
60440
+ const ca = tryGetConstant(a);
60441
+ const cb = tryGetConstant(b);
60442
+ if (ca !== void 0 && cb !== void 0 && cb !== 0)
60443
+ return String(ca / cb);
60444
+ if (cb === 1) return compile3(a);
60445
+ return `(${compile3(a)} / ${compile3(b)})`;
60446
+ }
60051
60447
  if (ac && bc) {
60052
60448
  return `(() => { const _a = ${compile3(a)}, _b = ${compile3(
60053
60449
  b
@@ -60064,13 +60460,26 @@ Error in definition of "${name}"`,
60064
60460
  },
60065
60461
  Negate: ([x], compile3) => {
60066
60462
  if (x === null) throw new Error("Negate: no argument");
60067
- if (!BaseCompiler.isComplexValued(x)) return `(-${compile3(x)})`;
60463
+ if (!BaseCompiler.isComplexValued(x)) {
60464
+ const c = tryGetConstant(x);
60465
+ if (c !== void 0) return String(-c);
60466
+ return `(-${compile3(x)})`;
60467
+ }
60068
60468
  return `_SYS.cneg(${compile3(x)})`;
60069
60469
  },
60070
60470
  Multiply: (args, compile3) => {
60071
60471
  if (args.length === 1) return compile3(args[0]);
60072
60472
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60073
- if (!anyComplex) return `(${args.map((x) => compile3(x)).join(" * ")})`;
60473
+ if (!anyComplex) {
60474
+ if (args.some((a) => tryGetConstant(a) === 0)) return "0";
60475
+ const constants = args.map(tryGetConstant);
60476
+ if (constants.every((c) => c !== void 0))
60477
+ return String(constants.reduce((a, b) => a * b, 1));
60478
+ const nonOne = args.filter((a) => tryGetConstant(a) !== 1);
60479
+ if (nonOne.length === 0) return "1";
60480
+ if (nonOne.length === 1) return compile3(nonOne[0]);
60481
+ return `(${nonOne.map((x) => compile3(x)).join(" * ")})`;
60482
+ }
60074
60483
  if (args.length === 2) {
60075
60484
  const ac = BaseCompiler.isComplexValued(args[0]);
60076
60485
  const bc = BaseCompiler.isComplexValued(args[1]);
@@ -60155,20 +60564,30 @@ Error in definition of "${name}"`,
60155
60564
  AiryAi: "_SYS.airyAi",
60156
60565
  AiryBi: "_SYS.airyBi",
60157
60566
  // Combinatorics
60567
+ Mandelbrot: ([c, maxIter], compile3) => {
60568
+ if (c === null || maxIter === null)
60569
+ throw new Error("Mandelbrot: missing arguments");
60570
+ return `_SYS.mandelbrot(${compile3(c)}, ${compile3(maxIter)})`;
60571
+ },
60572
+ Julia: ([z, c, maxIter], compile3) => {
60573
+ if (z === null || c === null || maxIter === null)
60574
+ throw new Error("Julia: missing arguments");
60575
+ return `_SYS.julia(${compile3(z)}, ${compile3(c)}, ${compile3(maxIter)})`;
60576
+ },
60158
60577
  Binomial: (args, compile3) => `_SYS.binomial(${compile3(args[0])}, ${compile3(args[1])})`,
60159
60578
  Fibonacci: "_SYS.fibonacci",
60160
60579
  // Complex-specific functions
60161
- Re: (args, compile3) => {
60580
+ Real: (args, compile3) => {
60162
60581
  if (BaseCompiler.isComplexValued(args[0]))
60163
60582
  return `(${compile3(args[0])}).re`;
60164
60583
  return compile3(args[0]);
60165
60584
  },
60166
- Im: (args, compile3) => {
60585
+ Imaginary: (args, compile3) => {
60167
60586
  if (BaseCompiler.isComplexValued(args[0]))
60168
60587
  return `(${compile3(args[0])}).im`;
60169
60588
  return "0";
60170
60589
  },
60171
- Arg: (args, compile3) => {
60590
+ Argument: (args, compile3) => {
60172
60591
  if (BaseCompiler.isComplexValued(args[0]))
60173
60592
  return `_SYS.carg(${compile3(args[0])})`;
60174
60593
  return `(${compile3(args[0])} >= 0 ? 0 : Math.PI)`;
@@ -60504,6 +60923,41 @@ Error in definition of "${name}"`,
60504
60923
  sinc,
60505
60924
  fresnelS,
60506
60925
  fresnelC,
60926
+ mandelbrot: (c, maxIter) => {
60927
+ let zx = 0, zy = 0;
60928
+ const cx = typeof c === "number" ? c : c.re;
60929
+ const cy = typeof c === "number" ? 0 : c.im;
60930
+ const n = Math.round(maxIter);
60931
+ for (let i = 0; i < n; i++) {
60932
+ const newZx = zx * zx - zy * zy + cx;
60933
+ zy = 2 * zx * zy + cy;
60934
+ zx = newZx;
60935
+ const mag2 = zx * zx + zy * zy;
60936
+ if (mag2 > 4) {
60937
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60938
+ return Math.max(0, Math.min(1, smooth));
60939
+ }
60940
+ }
60941
+ return 1;
60942
+ },
60943
+ julia: (z, c, maxIter) => {
60944
+ let zx = typeof z === "number" ? z : z.re;
60945
+ let zy = typeof z === "number" ? 0 : z.im;
60946
+ const cx = typeof c === "number" ? c : c.re;
60947
+ const cy = typeof c === "number" ? 0 : c.im;
60948
+ const n = Math.round(maxIter);
60949
+ for (let i = 0; i < n; i++) {
60950
+ const newZx = zx * zx - zy * zy + cx;
60951
+ zy = 2 * zx * zy + cy;
60952
+ zx = newZx;
60953
+ const mag2 = zx * zx + zy * zy;
60954
+ if (mag2 > 4) {
60955
+ const smooth = (i - Math.log2(Math.log2(mag2)) + 4) / n;
60956
+ return Math.max(0, Math.min(1, smooth));
60957
+ }
60958
+ }
60959
+ return 1;
60960
+ },
60507
60961
  binomial: choose,
60508
60962
  fibonacci,
60509
60963
  // Complex helpers
@@ -60849,6 +61303,7 @@ Error in definition of "${name}"`,
60849
61303
  Add: ["+", 11],
60850
61304
  Negate: ["-", 14],
60851
61305
  Subtract: ["-", 11],
61306
+ // Subtract canonicalizes to Add+Negate; kept as fallback
60852
61307
  Multiply: ["*", 12],
60853
61308
  Divide: ["/", 13],
60854
61309
  Equal: ["==", 8],
@@ -60864,6 +61319,12 @@ Error in definition of "${name}"`,
60864
61319
  function gpuVec2(target) {
60865
61320
  return target?.language === "wgsl" ? "vec2f" : "vec2";
60866
61321
  }
61322
+ function compileIntArg(expr2, compile3, target) {
61323
+ const c = tryGetConstant(expr2);
61324
+ if (c !== void 0 && Number.isInteger(c)) return c.toString();
61325
+ const intCast = target?.language === "wgsl" ? "i32" : "int";
61326
+ return `${intCast}(${compile3(expr2)})`;
61327
+ }
60867
61328
  var GPU_UNROLL_LIMIT = 100;
60868
61329
  function compileGPUSumProduct(kind, args, _compile2, target) {
60869
61330
  if (!args[0]) throw new Error(`${kind}: no body`);
@@ -60926,88 +61387,113 @@ Error in definition of "${name}"`,
60926
61387
  if (args.length === 0) return "0.0";
60927
61388
  if (args.length === 1) return compile3(args[0]);
60928
61389
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60929
- if (!anyComplex) return args.map((x) => compile3(x)).join(" + ");
60930
- const v2 = gpuVec2(target);
60931
- return args.map((a) => {
60932
- const code = compile3(a);
60933
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
60934
- }).join(" + ");
60935
- },
60936
- Multiply: (args, compile3, _target) => {
60937
- if (args.length === 0) return "1.0";
60938
- if (args.length === 1) return compile3(args[0]);
60939
- const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60940
- if (!anyComplex) return args.map((x) => compile3(x)).join(" * ");
60941
- let result = compile3(args[0]);
60942
- let resultIsComplex = BaseCompiler.isComplexValued(args[0]);
60943
- for (let i = 1; i < args.length; i++) {
60944
- const code = compile3(args[i]);
60945
- const argIsComplex = BaseCompiler.isComplexValued(args[i]);
60946
- if (!resultIsComplex && !argIsComplex) {
60947
- result = `(${result} * ${code})`;
60948
- } else if (resultIsComplex && !argIsComplex) {
60949
- result = `(${code} * ${result})`;
60950
- } else if (!resultIsComplex && argIsComplex) {
60951
- result = `(${result} * ${code})`;
60952
- resultIsComplex = true;
60953
- } else {
60954
- result = `_gpu_cmul(${result}, ${code})`;
60955
- }
61390
+ if (!anyComplex) {
61391
+ return foldTerms(
61392
+ args.map((x) => compile3(x)),
61393
+ "0.0",
61394
+ "+"
61395
+ );
60956
61396
  }
60957
- return result;
60958
- },
60959
- Subtract: (args, compile3, target) => {
60960
- if (args.length === 0) return "0.0";
61397
+ const parts = args.map((a) => tryGetComplexParts(a, compile3));
61398
+ if (parts.some((p) => p === null)) {
61399
+ const v2 = gpuVec2(target);
61400
+ return args.map((a) => {
61401
+ const code = compile3(a);
61402
+ return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
61403
+ }).join(" + ");
61404
+ }
61405
+ const reParts = [];
61406
+ const imParts = [];
61407
+ for (const p of parts) {
61408
+ if (p.re !== null) reParts.push(p.re);
61409
+ if (p.im !== null) imParts.push(p.im);
61410
+ }
61411
+ const reSum = foldTerms(reParts, "0.0", "+");
61412
+ const imSum = foldTerms(imParts, "0.0", "+");
61413
+ return `${gpuVec2(target)}(${reSum}, ${imSum})`;
61414
+ },
61415
+ Multiply: (args, compile3, target) => {
61416
+ if (args.length === 0) return "1.0";
60961
61417
  if (args.length === 1) return compile3(args[0]);
60962
61418
  const anyComplex = args.some((a) => BaseCompiler.isComplexValued(a));
60963
61419
  if (!anyComplex) {
60964
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
60965
- let result2 = compile3(args[0]);
60966
- for (let i = 1; i < args.length; i++) {
60967
- result2 = `${result2} - ${compile3(args[i])}`;
60968
- }
60969
- return result2;
60970
- }
60971
- const v2 = gpuVec2(target);
60972
- const promote = (a) => {
60973
- const code = compile3(a);
60974
- return BaseCompiler.isComplexValued(a) ? code : `${v2}(${code}, 0.0)`;
60975
- };
60976
- if (args.length === 2) return `${promote(args[0])} - ${promote(args[1])}`;
60977
- let result = promote(args[0]);
60978
- for (let i = 1; i < args.length; i++) {
60979
- result = `${result} - ${promote(args[i])}`;
61420
+ return foldTerms(
61421
+ args.map((x) => compile3(x)),
61422
+ "1.0",
61423
+ "*"
61424
+ );
60980
61425
  }
61426
+ const iIndex = args.findIndex(
61427
+ (op) => isSymbol2(op, "ImaginaryUnit") || isNumber(op) && op.re === 0 && op.im !== 0
61428
+ );
61429
+ if (iIndex >= 0) {
61430
+ const iFactor = args[iIndex];
61431
+ const iScale = isSymbol2(iFactor, "ImaginaryUnit") ? 1 : iFactor.im;
61432
+ const realFactors = args.filter((_, i) => i !== iIndex);
61433
+ const v2 = gpuVec2(target);
61434
+ if (realFactors.length === 0)
61435
+ return `${v2}(0.0, ${formatFloat(iScale)})`;
61436
+ const factors = realFactors.map((f) => compile3(f));
61437
+ if (iScale !== 1) factors.unshift(formatFloat(iScale));
61438
+ const imCode = foldTerms(factors, "1.0", "*");
61439
+ return `${v2}(0.0, ${imCode})`;
61440
+ }
61441
+ const realCodes = [];
61442
+ const complexCodes = [];
61443
+ for (const a of args) {
61444
+ if (BaseCompiler.isComplexValued(a)) complexCodes.push(compile3(a));
61445
+ else realCodes.push(compile3(a));
61446
+ }
61447
+ const scalarCode = foldTerms(realCodes, "1.0", "*");
61448
+ let result = complexCodes[0];
61449
+ for (let i = 1; i < complexCodes.length; i++) {
61450
+ result = `_gpu_cmul(${result}, ${complexCodes[i]})`;
61451
+ }
61452
+ if (scalarCode !== "1.0") result = `(${scalarCode} * ${result})`;
60981
61453
  return result;
60982
61454
  },
61455
+ // No Subtract function handler — Subtract canonicalizes to Add+Negate.
61456
+ // The operator entry in GPU_OPERATORS handles any edge cases.
60983
61457
  Divide: (args, compile3, target) => {
60984
61458
  if (args.length === 0) return "1.0";
60985
61459
  if (args.length === 1) return compile3(args[0]);
60986
61460
  const ac = BaseCompiler.isComplexValued(args[0]);
60987
61461
  const bc = args.length >= 2 && BaseCompiler.isComplexValued(args[1]);
60988
61462
  if (!ac && !bc) {
60989
- if (args.length === 2) return `${compile3(args[0])} / ${compile3(args[1])}`;
61463
+ if (args.length === 2) {
61464
+ const a = tryGetConstant(args[0]);
61465
+ const b = tryGetConstant(args[1]);
61466
+ if (a !== void 0 && b !== void 0 && b !== 0)
61467
+ return formatFloat(a / b);
61468
+ if (b === 1) return compile3(args[0]);
61469
+ return `${compile3(args[0])} / ${compile3(args[1])}`;
61470
+ }
60990
61471
  let result = compile3(args[0]);
60991
- for (let i = 1; i < args.length; i++) {
61472
+ for (let i = 1; i < args.length; i++)
60992
61473
  result = `${result} / ${compile3(args[i])}`;
60993
- }
60994
61474
  return result;
60995
61475
  }
60996
61476
  if (ac && bc) return `_gpu_cdiv(${compile3(args[0])}, ${compile3(args[1])})`;
60997
- if (ac && !bc) {
60998
- return `(${compile3(args[0])} / ${compile3(args[1])})`;
60999
- }
61477
+ if (ac && !bc) return `(${compile3(args[0])} / ${compile3(args[1])})`;
61000
61478
  const v2 = gpuVec2(target);
61001
61479
  return `_gpu_cdiv(${v2}(${compile3(args[0])}, 0.0), ${compile3(args[1])})`;
61002
61480
  },
61003
- Negate: ([x], compile3) => {
61481
+ Negate: ([x], compile3, target) => {
61004
61482
  if (x === null) throw new Error("Negate: no argument");
61483
+ const c = tryGetConstant(x);
61484
+ if (c !== void 0) return formatFloat(-c);
61485
+ if (isNumber(x) && x.im !== 0) {
61486
+ return `${gpuVec2(target)}(${formatFloat(-x.re)}, ${formatFloat(-x.im)})`;
61487
+ }
61488
+ if (isSymbol2(x, "ImaginaryUnit"))
61489
+ return `${gpuVec2(target)}(0.0, -1.0)`;
61005
61490
  return `(-${compile3(x)})`;
61006
61491
  },
61007
61492
  // Standard math functions with complex dispatch
61008
61493
  Abs: (args, compile3) => {
61009
61494
  if (BaseCompiler.isComplexValued(args[0]))
61010
61495
  return `length(${compile3(args[0])})`;
61496
+ if (BaseCompiler.isNonNegative(args[0])) return compile3(args[0]);
61011
61497
  return `abs(${compile3(args[0])})`;
61012
61498
  },
61013
61499
  Arccos: (args, compile3) => {
@@ -61025,7 +61511,10 @@ Error in definition of "${name}"`,
61025
61511
  return `_gpu_catan(${compile3(args[0])})`;
61026
61512
  return `atan(${compile3(args[0])})`;
61027
61513
  },
61028
- Ceil: "ceil",
61514
+ Ceil: (args, compile3) => {
61515
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61516
+ return `ceil(${compile3(args[0])})`;
61517
+ },
61029
61518
  Clamp: "clamp",
61030
61519
  Cos: (args, compile3) => {
61031
61520
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61039,7 +61528,10 @@ Error in definition of "${name}"`,
61039
61528
  return `exp(${compile3(args[0])})`;
61040
61529
  },
61041
61530
  Exp2: "exp2",
61042
- Floor: "floor",
61531
+ Floor: (args, compile3) => {
61532
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61533
+ return `floor(${compile3(args[0])})`;
61534
+ },
61043
61535
  Fract: "fract",
61044
61536
  Ln: (args, compile3) => {
61045
61537
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61061,10 +61553,25 @@ Error in definition of "${name}"`,
61061
61553
  const eCode = BaseCompiler.isComplexValued(exp3) ? compile3(exp3) : `${v2}(${compile3(exp3)}, 0.0)`;
61062
61554
  return `_gpu_cpow(${bCode}, ${eCode})`;
61063
61555
  }
61556
+ const bConst = tryGetConstant(base);
61557
+ const eConst = tryGetConstant(exp3);
61558
+ if (bConst !== void 0 && eConst !== void 0)
61559
+ return formatFloat(Math.pow(bConst, eConst));
61560
+ if (eConst === 0) return "1.0";
61561
+ if (eConst === 1) return compile3(base);
61562
+ if (eConst === 2 && (isSymbol2(base) || isNumber(base))) {
61563
+ const code = compile3(base);
61564
+ return `(${code} * ${code})`;
61565
+ }
61566
+ if (eConst === -1) return `(1.0 / ${compile3(base)})`;
61567
+ if (eConst === 0.5) return `sqrt(${compile3(base)})`;
61064
61568
  return `pow(${compile3(base)}, ${compile3(exp3)})`;
61065
61569
  },
61066
61570
  Radians: "radians",
61067
- Round: "round",
61571
+ Round: (args, compile3) => {
61572
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61573
+ return `round(${compile3(args[0])})`;
61574
+ },
61068
61575
  Sign: "sign",
61069
61576
  Sin: (args, compile3) => {
61070
61577
  if (BaseCompiler.isComplexValued(args[0]))
@@ -61075,6 +61582,8 @@ Error in definition of "${name}"`,
61075
61582
  Sqrt: (args, compile3) => {
61076
61583
  if (BaseCompiler.isComplexValued(args[0]))
61077
61584
  return `_gpu_csqrt(${compile3(args[0])})`;
61585
+ const c = tryGetConstant(args[0]);
61586
+ if (c !== void 0) return formatFloat(Math.sqrt(c));
61078
61587
  return `sqrt(${compile3(args[0])})`;
61079
61588
  },
61080
61589
  Step: "step",
@@ -61083,17 +61592,20 @@ Error in definition of "${name}"`,
61083
61592
  return `_gpu_ctan(${compile3(args[0])})`;
61084
61593
  return `tan(${compile3(args[0])})`;
61085
61594
  },
61086
- Truncate: "trunc",
61595
+ Truncate: (args, compile3) => {
61596
+ if (BaseCompiler.isIntegerValued(args[0])) return compile3(args[0]);
61597
+ return `trunc(${compile3(args[0])})`;
61598
+ },
61087
61599
  // Complex-specific functions
61088
- Re: (args, compile3) => {
61600
+ Real: (args, compile3) => {
61089
61601
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).x`;
61090
61602
  return compile3(args[0]);
61091
61603
  },
61092
- Im: (args, compile3) => {
61604
+ Imaginary: (args, compile3) => {
61093
61605
  if (BaseCompiler.isComplexValued(args[0])) return `(${compile3(args[0])}).y`;
61094
61606
  return "0.0";
61095
61607
  },
61096
- Arg: (args, compile3) => {
61608
+ Argument: (args, compile3) => {
61097
61609
  if (BaseCompiler.isComplexValued(args[0])) {
61098
61610
  const code = compile3(args[0]);
61099
61611
  return `atan(${code}.y, ${code}.x)`;
@@ -61308,13 +61820,20 @@ Error in definition of "${name}"`,
61308
61820
  },
61309
61821
  Square: ([x], compile3) => {
61310
61822
  if (x === null) throw new Error("Square: no argument");
61311
- const arg = compile3(x);
61312
- return `(${arg} * ${arg})`;
61823
+ if (isSymbol2(x) || isNumber(x)) {
61824
+ const arg = compile3(x);
61825
+ return `(${arg} * ${arg})`;
61826
+ }
61827
+ return `pow(${compile3(x)}, 2.0)`;
61313
61828
  },
61314
61829
  Root: ([x, n], compile3) => {
61315
61830
  if (x === null) throw new Error("Root: no argument");
61316
61831
  if (n === null || n === void 0) return `sqrt(${compile3(x)})`;
61317
- if (n?.re === 2) return `sqrt(${compile3(x)})`;
61832
+ const nConst = tryGetConstant(n);
61833
+ if (nConst === 2) return `sqrt(${compile3(x)})`;
61834
+ const xConst = tryGetConstant(x);
61835
+ if (xConst !== void 0 && nConst !== void 0)
61836
+ return formatFloat(Math.pow(xConst, 1 / nConst));
61318
61837
  return `pow(${compile3(x)}, 1.0 / ${compile3(n)})`;
61319
61838
  },
61320
61839
  // Color functions (pure-math, GPU-compilable)
@@ -61356,18 +61875,14 @@ Error in definition of "${name}"`,
61356
61875
  Mandelbrot: ([c, maxIter], compile3, target) => {
61357
61876
  if (c === null || maxIter === null)
61358
61877
  throw new Error("Mandelbrot: missing arguments");
61359
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61360
- return `_fractal_mandelbrot(${compile3(c)}, ${intCast}(${compile3(
61361
- maxIter
61362
- )}))`;
61878
+ const iterCode = compileIntArg(maxIter, compile3, target);
61879
+ return `_fractal_mandelbrot(${compile3(c)}, ${iterCode})`;
61363
61880
  },
61364
61881
  Julia: ([z, c, maxIter], compile3, target) => {
61365
61882
  if (z === null || c === null || maxIter === null)
61366
61883
  throw new Error("Julia: missing arguments");
61367
- const intCast = target?.language === "wgsl" ? "i32" : "int";
61368
- return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${intCast}(${compile3(
61369
- maxIter
61370
- )}))`;
61884
+ const iterCode = compileIntArg(maxIter, compile3, target);
61885
+ return `_fractal_julia(${compile3(z)}, ${compile3(c)}, ${iterCode})`;
61371
61886
  },
61372
61887
  // Vector/Matrix operations
61373
61888
  Cross: "cross",
@@ -63999,6 +64514,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
63999
64514
  Add: ["_IA.add", 20],
64000
64515
  Negate: ["_IA.negate", 20],
64001
64516
  Subtract: ["_IA.sub", 20],
64517
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64002
64518
  Multiply: ["_IA.mul", 20],
64003
64519
  Divide: ["_IA.div", 20],
64004
64520
  // Comparisons return BoolInterval
@@ -64023,17 +64539,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64023
64539
  }
64024
64540
  return result;
64025
64541
  },
64026
- Subtract: (args, compile3) => {
64027
- if (args.length === 0) return "_IA.point(0)";
64028
- if (args.length === 1) return `_IA.negate(${compile3(args[0])})`;
64029
- if (args.length === 2)
64030
- return `_IA.sub(${compile3(args[0])}, ${compile3(args[1])})`;
64031
- let result = compile3(args[0]);
64032
- for (let i = 1; i < args.length; i++) {
64033
- result = `_IA.sub(${result}, ${compile3(args[i])})`;
64034
- }
64035
- return result;
64036
- },
64542
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
64037
64543
  Multiply: (args, compile3) => {
64038
64544
  if (args.length === 0) return "_IA.point(1)";
64039
64545
  if (args.length === 1) return compile3(args[0]);
@@ -64969,6 +65475,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64969
65475
  Negate: ["-", 14],
64970
65476
  // Unary operator
64971
65477
  Subtract: ["-", 11],
65478
+ // Subtract canonicalizes to Add+Negate; kept as fallback
64972
65479
  Multiply: ["*", 12],
64973
65480
  Divide: ["/", 13],
64974
65481
  Power: ["**", 15],
@@ -64996,16 +65503,7 @@ ${workgroupAttr}fn main(${paramStr})${returnStr} {
64996
65503
  if (args.length === 1) return compile3(args[0]);
64997
65504
  return args.map((x) => compile3(x)).join(" * ");
64998
65505
  },
64999
- Subtract: (args, compile3) => {
65000
- if (args.length === 0) return "0";
65001
- if (args.length === 1) return compile3(args[0]);
65002
- if (args.length === 2) return `${compile3(args[0])} - ${compile3(args[1])}`;
65003
- let result = compile3(args[0]);
65004
- for (let i = 1; i < args.length; i++) {
65005
- result = `${result} - ${compile3(args[i])}`;
65006
- }
65007
- return result;
65008
- },
65506
+ // No Subtract handler canonicalizes to Add+Negate before compilation.
65009
65507
  Divide: (args, compile3) => {
65010
65508
  if (args.length === 0) return "1";
65011
65509
  if (args.length === 1) return compile3(args[0]);
@@ -67437,7 +67935,9 @@ ${code}`;
67437
67935
  }
67438
67936
  return new ExactNumericValue(value, makeNumericValue);
67439
67937
  }
67440
- throw Error("Unexpected value");
67938
+ throw Error(
67939
+ `Unexpected value: ${typeof value === "object" ? JSON.stringify(value) : String(value)}`
67940
+ );
67441
67941
  }
67442
67942
  /**
67443
67943
  * The cost function is used to determine the "cost" of an expression. For example, when simplifying an expression, the simplification that results in the lowest cost is chosen.
@@ -67908,14 +68408,14 @@ ${code}`;
67908
68408
  _setDefaultEngineFactory(() => new ComputeEngine());
67909
68409
 
67910
68410
  // src/compute-engine.ts
67911
- var version = "0.55.0";
68411
+ var version = "0.55.3";
67912
68412
  ComputeEngine._latexSyntaxFactory = () => new LatexSyntax();
67913
68413
  _setDefaultEngineFactory(
67914
68414
  () => new ComputeEngine({ latexSyntax: new LatexSyntax() })
67915
68415
  );
67916
68416
  globalThis[/* @__PURE__ */ Symbol.for("io.cortexjs.compute-engine")] = {
67917
68417
  ComputeEngine: ComputeEngine.prototype.constructor,
67918
- version: "0.55.0"
68418
+ version: "0.55.3"
67919
68419
  };
67920
68420
  return __toCommonJS(compute_engine_exports);
67921
68421
  })();