@cortex-js/compute-engine 0.12.6 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/compute-engine.esm.js +358 -73
  2. package/dist/compute-engine.js +358 -73
  3. package/dist/compute-engine.min.esm.js +358 -73
  4. package/dist/compute-engine.min.js +358 -73
  5. package/dist/math-json.esm.js +2 -2
  6. package/dist/math-json.js +2 -2
  7. package/dist/math-json.min.esm.js +2 -2
  8. package/dist/math-json.min.js +2 -2
  9. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  10. package/dist/types/common/signals.d.ts +1 -1
  11. package/dist/types/common/utils.d.ts +1 -1
  12. package/dist/types/compute-engine/assume.d.ts +1 -1
  13. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +2 -1
  14. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +1 -1
  17. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  18. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  20. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  21. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  26. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  29. package/dist/types/compute-engine/compile.d.ts +3 -0
  30. package/dist/types/compute-engine/compute-engine.d.ts +4 -2
  31. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  32. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  33. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  34. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +3 -3
  44. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +4 -1
  45. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/public.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  52. package/dist/types/compute-engine/library/arithmetic-add.d.ts +2 -2
  53. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  54. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +1 -1
  55. package/dist/types/compute-engine/library/arithmetic-power.d.ts +1 -1
  56. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  57. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  58. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  59. package/dist/types/compute-engine/library/core.d.ts +1 -1
  60. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  61. package/dist/types/compute-engine/library/library.d.ts +1 -1
  62. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  63. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  64. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  65. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  66. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  67. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  68. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  69. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  70. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  71. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  72. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  73. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  74. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  75. package/dist/types/compute-engine/public.d.ts +3 -2
  76. package/dist/types/compute-engine/rules.d.ts +1 -1
  77. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  78. package/dist/types/compute-engine/solve.d.ts +1 -1
  79. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  80. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  81. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  82. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  83. package/dist/types/compute-engine/symbolic/product.d.ts +1 -1
  84. package/dist/types/compute-engine/symbolic/sum.d.ts +1 -1
  85. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  86. package/dist/types/compute-engine.d.ts +2 -2
  87. package/dist/types/math-json/math-json-format.d.ts +1 -1
  88. package/dist/types/math-json/utils.d.ts +3 -1
  89. package/dist/types/math-json.d.ts +2 -2
  90. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- /** CortexJS Compute Engine 0.12.6 */
1
+ /** CortexJS Compute Engine 0.13.0 */
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -3949,6 +3949,14 @@ function symbol(expr) {
3949
3949
  return null;
3950
3950
  return s;
3951
3951
  }
3952
+ function isListLike(expr) {
3953
+ if (expr === null)
3954
+ return false;
3955
+ const h = head(expr);
3956
+ if (!h || typeof h !== "string")
3957
+ return false;
3958
+ return /^(List|Sequence|Tuple|Single|Pair|Triple)$/.test(h);
3959
+ }
3952
3960
  function keyValuePair(expr) {
3953
3961
  const h = head(expr);
3954
3962
  if (h === "KeyValuePair" || h === "Tuple" || h === "Pair") {
@@ -4584,6 +4592,10 @@ var DEFINITIONS_ARITHMETIC = [
4584
4592
  parse: "ExponentialE",
4585
4593
  serialize: "\\exponentialE"
4586
4594
  },
4595
+ {
4596
+ trigger: "\\mathrm{e}",
4597
+ parse: "ExponentialE"
4598
+ },
4587
4599
  {
4588
4600
  kind: "function",
4589
4601
  trigger: "exp",
@@ -4597,6 +4609,10 @@ var DEFINITIONS_ARITHMETIC = [
4597
4609
  name: "ImaginaryUnit",
4598
4610
  trigger: ["\\imaginaryI"]
4599
4611
  },
4612
+ {
4613
+ trigger: "\\mathrm{i}",
4614
+ parse: "ImaginaryUnit"
4615
+ },
4600
4616
  // Operations
4601
4617
  {
4602
4618
  /** Could be the determinant if the argument is a matrix */
@@ -5156,7 +5172,7 @@ function parseSequence(parser, terminator, lhs, prec, sep) {
5156
5172
  }
5157
5173
  return result;
5158
5174
  }
5159
- function serializeSequence(sep = "") {
5175
+ function serializeOps(sep = "") {
5160
5176
  return (serializer, expr) => (ops(expr) ?? []).map((x) => serializer.serialize(x)).join(sep);
5161
5177
  }
5162
5178
  var DEFINITIONS_CORE = [
@@ -5224,22 +5240,19 @@ var DEFINITIONS_CORE = [
5224
5240
  return "";
5225
5241
  const style = serializer.options.groupStyle(expr, serializer.level + 1);
5226
5242
  const arg1 = op(expr, 1);
5227
- if (argCount === 1)
5228
- return serializer.wrapString(serializer.serialize(arg1), style);
5229
- let sep = "";
5230
- let open = "";
5231
- let close = "";
5243
+ const h1 = head(arg1);
5244
+ const defaultFence = { List: "[],", Sequence: "" }[typeof h1 === "string" ? h1 : ""] ?? "(),";
5245
+ let open = defaultFence[0] ?? "";
5246
+ let close = defaultFence[1] ?? "";
5247
+ let sep = defaultFence[2] ?? "";
5232
5248
  if (argCount > 1) {
5233
5249
  const op22 = stringValue(op(expr, 2)) ?? "";
5234
- open = op22[0] ?? "(";
5235
- close = op22[1] ?? ")";
5236
- sep = op22[2] ?? ",";
5250
+ open = op22[0] ?? defaultFence[0];
5251
+ close = op22[1] ?? defaultFence[1];
5252
+ sep = op22[2] ?? defaultFence[2];
5237
5253
  }
5238
- const body = head(arg1) === "List" ? serializeSequence(sep)(serializer, arg1) : serializer.serialize(arg1);
5239
- serializer.wrapString(body, style, stringValue(op(expr, 2)) ?? void 0);
5240
- if (!open || !close)
5241
- return serializer.wrapString(body, style);
5242
- return `${open} ${body} ${close}`;
5254
+ const body = isListLike(arg1) ? serializeOps(sep)(serializer, arg1) : serializer.serialize(arg1);
5255
+ return serializer.wrapString(body, style, open + close);
5243
5256
  }
5244
5257
  },
5245
5258
  {
@@ -5340,7 +5353,7 @@ var DEFINITIONS_CORE = [
5340
5353
  serialize: (serializer, expr) => {
5341
5354
  return joinLatex([
5342
5355
  "\\lbrack",
5343
- serializeSequence(", ")(serializer, expr),
5356
+ serializeOps(", ")(serializer, expr),
5344
5357
  "\\rbrack"
5345
5358
  ]);
5346
5359
  }
@@ -5378,7 +5391,7 @@ var DEFINITIONS_CORE = [
5378
5391
  },
5379
5392
  {
5380
5393
  name: "Sequence",
5381
- serialize: serializeSequence("")
5394
+ serialize: serializeOps("")
5382
5395
  },
5383
5396
  {
5384
5397
  trigger: [";"],
@@ -5508,24 +5521,17 @@ var DEFINITIONS_CORE = [
5508
5521
  return result;
5509
5522
  },
5510
5523
  serialize: (serialize2, expr) => {
5511
- if (head(op(expr, 1)) !== "List")
5512
- return "";
5513
- const rows = ops(op(expr, 1)) ?? [];
5514
- const body = [];
5515
- let rowSep = "";
5516
- for (const row of rows) {
5517
- if (head(row) === "Tuple" || head(row) === "Pair") {
5518
- body.push(rowSep);
5519
- if (op(row, 2)) {
5520
- body.push(serialize2.serialize(op(row, 2)));
5521
- const condition = op(row, 1);
5522
- if (condition !== null)
5523
- body.push("&", serialize2.serialize(condition));
5524
- }
5524
+ const rows = [];
5525
+ const args = ops(expr);
5526
+ if (args) {
5527
+ for (let i = 0; i <= args.length - 2; i += 2) {
5528
+ const row = [];
5529
+ row.push(serialize2.serialize(args[i + 1]));
5530
+ row.push(serialize2.serialize(args[i]));
5531
+ rows.push(row.join("&"));
5525
5532
  }
5526
- rowSep = "\\\\";
5527
5533
  }
5528
- return joinLatex(["\\begin{cases}", ...body, "\\end{cases}"]);
5534
+ return joinLatex(["\\begin{cases}", rows.join("\\\\"), "\\end{cases}"]);
5529
5535
  }
5530
5536
  }
5531
5537
  ];
@@ -7427,11 +7433,6 @@ var DEFAULT_DELIMITER = {
7427
7433
  "\\rceil": "\\rceil",
7428
7434
  "\\rfloor": "\\rfloor"
7429
7435
  };
7430
- function triggerLength(trigger) {
7431
- if (Array.isArray(trigger))
7432
- return trigger.length;
7433
- return 1;
7434
- }
7435
7436
  function addEntry(result, entry, onError) {
7436
7437
  const [trigger, indexedEntry] = makeIndexedEntry(entry, onError);
7437
7438
  if (indexedEntry === null)
@@ -7473,7 +7474,7 @@ function addEntry(result, entry, onError) {
7473
7474
  if (indexedEntry.kind === "matchfix") {
7474
7475
  result.matchfix.push(indexedEntry);
7475
7476
  } else if (indexedEntry.kind === "environment") {
7476
- const triggerString = tokensToString(entry.trigger ?? "");
7477
+ const triggerString = tokensToString(entry.trigger ?? []);
7477
7478
  if (result.environment.has(triggerString)) {
7478
7479
  onError({
7479
7480
  severity: "warning",
@@ -7487,22 +7488,31 @@ function addEntry(result, entry, onError) {
7487
7488
  result.environment.set(triggerString, indexedEntry);
7488
7489
  } else if (trigger) {
7489
7490
  console.assert(entry.trigger);
7490
- const triggerString = tokensToString(entry.trigger ?? "");
7491
- const n = triggerLength(trigger);
7492
- result.lookahead = Math.max(result.lookahead, n);
7493
7491
  if (indexedEntry.kind === "function") {
7494
- if (!result.function.has(triggerString))
7495
- result.function.set(triggerString, [indexedEntry]);
7492
+ console.assert(typeof entry.trigger === "string");
7493
+ const fnName = entry.trigger;
7494
+ if (!result.function.has(fnName))
7495
+ result.function.set(fnName, [indexedEntry]);
7496
7496
  else
7497
- result.function.set(triggerString, [
7498
- ...result.function.get(triggerString),
7497
+ result.function.set(fnName, [
7498
+ ...result.function.get(fnName),
7499
7499
  indexedEntry
7500
7500
  ]);
7501
7501
  } else {
7502
+ let triggerString;
7503
+ let tokenCount;
7504
+ if (typeof entry.trigger === "string") {
7505
+ triggerString = entry.trigger;
7506
+ tokenCount = countTokens(triggerString);
7507
+ } else {
7508
+ triggerString = tokensToString(entry.trigger ?? []);
7509
+ tokenCount = entry.trigger.length;
7510
+ }
7511
+ result.lookahead = Math.max(result.lookahead, tokenCount);
7502
7512
  const kind2 = indexedEntry.kind;
7503
- if (result[kind2][n] === void 0)
7504
- result[kind2][n] = /* @__PURE__ */ new Map();
7505
- const list = result[kind2][n];
7513
+ if (result[kind2][tokenCount] === void 0)
7514
+ result[kind2][tokenCount] = /* @__PURE__ */ new Map();
7515
+ const list = result[kind2][tokenCount];
7506
7516
  if (list.has(triggerString))
7507
7517
  list.get(triggerString).push(indexedEntry);
7508
7518
  else
@@ -7514,12 +7524,12 @@ function indexLatexDictionary(dic, onError) {
7514
7524
  const result = {
7515
7525
  lookahead: 1,
7516
7526
  name: /* @__PURE__ */ new Map(),
7517
- expression: /* @__PURE__ */ new Map(),
7518
- function: /* @__PURE__ */ new Map(),
7527
+ expression: [],
7519
7528
  symbol: [],
7520
7529
  infix: [],
7521
7530
  prefix: [],
7522
7531
  postfix: [],
7532
+ function: /* @__PURE__ */ new Map(),
7523
7533
  environment: /* @__PURE__ */ new Map(),
7524
7534
  matchfix: []
7525
7535
  };
@@ -7582,13 +7592,11 @@ function makeIndexedEntry(entry, onError) {
7582
7592
  if (result.kind === "expression" && isExpressionEntry(entry)) {
7583
7593
  result.serialize = entry.serialize ?? triggerString;
7584
7594
  if (typeof result.serialize === "string") {
7585
- const serializeExpr = result.serialize;
7595
+ const serializeStr = result.serialize;
7586
7596
  result.serialize = (serializer, expr) => {
7587
7597
  if (!head(expr))
7588
- return serializer.serialize(serializeExpr);
7589
- return `${serializer.serialize(
7590
- serializeExpr
7591
- )}${serializer.wrapArguments(expr)}`;
7598
+ return serializeStr;
7599
+ return `${serializeStr}${serializer.wrapArguments(expr)}`;
7592
7600
  };
7593
7601
  }
7594
7602
  {
@@ -8335,7 +8343,7 @@ var _Parser = class {
8335
8343
  defs = this.lookAhead().map((x, n) => this._dictionary[kind][n]?.get(x));
8336
8344
  }
8337
8345
  const result = [];
8338
- for (let i = defs.length; i > 0; i--) {
8346
+ for (let i = defs.length - 1; i > 0; i--) {
8339
8347
  if (defs[i] !== void 0) {
8340
8348
  console.assert(Array.isArray(defs[i]));
8341
8349
  for (const def of defs[i])
@@ -10089,14 +10097,16 @@ var Serializer = class {
10089
10097
  wrapString(s, style, fence) {
10090
10098
  if (style === "none")
10091
10099
  return s;
10092
- const openFence = fence?.[0] ?? "(";
10093
- const closeFence = fence?.[1] ?? ")";
10100
+ if (fence === void 0)
10101
+ fence = "()";
10102
+ const openFence = fence?.[0] ?? ".";
10103
+ const closeFence = fence?.[1] ?? ".";
10094
10104
  if ((openFence === "." || closeFence === ".") && style === "paren")
10095
10105
  style = "leftright";
10096
10106
  if (style === "leftright")
10097
- return `${openFence === "." ? "" : `\\left(${openFence}`}${s}${closeFence === "." ? "" : `\\right(${closeFence}`})`;
10107
+ return `\\left${openFence}${s}\\right${closeFence}}`;
10098
10108
  if (style === "big")
10099
- return `${openFence === "." ? "" : `\\Bigl(${openFence}`}${s}${closeFence === "." ? "" : `\\Bigr(${closeFence}`})`;
10109
+ return `${openFence === "." ? "" : `\\Bigl${openFence}`}${s}${closeFence === "." ? "" : `\\Bigr${closeFence}`})`;
10100
10110
  return openFence + s + closeFence;
10101
10111
  }
10102
10112
  wrapArguments(expr) {
@@ -10445,11 +10455,19 @@ var LatexSyntax = class _LatexSyntax {
10445
10455
  ...DEFAULT_SERIALIZE_LATEX_OPTIONS,
10446
10456
  ...opts
10447
10457
  };
10458
+ this._dictionaryInput = options.dictionary ?? _LatexSyntax.getDictionary();
10448
10459
  this._dictionary = indexLatexDictionary(
10449
- options.dictionary ?? _LatexSyntax.getDictionary(),
10460
+ this._dictionaryInput,
10450
10461
  (sig) => this.onError([sig])
10451
10462
  );
10452
10463
  }
10464
+ get dictionary() {
10465
+ return this._dictionaryInput;
10466
+ }
10467
+ set dictionary(val) {
10468
+ this._dictionaryInput = val;
10469
+ this._dictionary = indexLatexDictionary(val, (sig) => this.onError([sig]));
10470
+ }
10453
10471
  updateOptions(opt) {
10454
10472
  for (const k of Object.keys(this.options))
10455
10473
  if (k in opt)
@@ -13141,6 +13159,246 @@ function domainSetsLibrary() {
13141
13159
 
13142
13160
  // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13143
13161
  var import_complex6 = __toESM(require_complex());
13162
+
13163
+ // src/compute-engine/compile.ts
13164
+ function compileToJavascript(expr) {
13165
+ const js = compile(expr, expr.freeVars);
13166
+ try {
13167
+ return new Function("_", `return ${js}`);
13168
+ } catch (e) {
13169
+ console.error(`${e}
13170
+ ${expr.latex}
13171
+ ${js}`);
13172
+ }
13173
+ return void 0;
13174
+ }
13175
+ function compile(expr, freeVars = [], prec = 0) {
13176
+ const f = asFloat(expr);
13177
+ if (f !== null)
13178
+ return f.toString();
13179
+ const s = expr.symbol;
13180
+ if (s !== null) {
13181
+ const result = {
13182
+ True: "true",
13183
+ False: "false",
13184
+ Pi: "Math.PI",
13185
+ ExponentialE: "Math.E",
13186
+ I: "Math.I",
13187
+ NaN: "Number.NaN",
13188
+ ImaginaryUnit: "NaN"
13189
+ }[s];
13190
+ if (result !== void 0)
13191
+ return result;
13192
+ if (freeVars.includes(s))
13193
+ return `_.${s}`;
13194
+ return s;
13195
+ }
13196
+ const str = expr.string;
13197
+ if (str !== null)
13198
+ return JSON.stringify(str);
13199
+ const keys = expr.keys;
13200
+ if (keys !== null) {
13201
+ const result = [];
13202
+ for (const key of keys) {
13203
+ const value = expr.getKey(key);
13204
+ if (value)
13205
+ result.push(`${key}: ${compile(value, freeVars, 0)}`);
13206
+ }
13207
+ return `{${result.join(", ")}}`;
13208
+ }
13209
+ const h = expr.head;
13210
+ if (typeof h === "string") {
13211
+ if (h === "Negate") {
13212
+ const arg = expr.op1;
13213
+ if (arg === null)
13214
+ return "";
13215
+ return `-${compile(arg, freeVars, 3)}`;
13216
+ }
13217
+ if (h === "Error") {
13218
+ return "NaN";
13219
+ }
13220
+ if (h === "Sum")
13221
+ return compileLoop(expr, "+");
13222
+ if (h === "Product")
13223
+ return compileLoop(expr, "*");
13224
+ if (h === "Root") {
13225
+ const arg = expr.op1;
13226
+ if (arg === null)
13227
+ return "";
13228
+ const exp2 = expr.op2;
13229
+ if (exp2 === null)
13230
+ return `Math.sqrt(${compile(arg, freeVars, 0)})`;
13231
+ return `Math.pow(${compile(arg, freeVars)}, 1/${compile(exp2, freeVars)}`;
13232
+ }
13233
+ if (h === "Factorial") {
13234
+ const arg = expr.op1;
13235
+ if (arg === null)
13236
+ return "";
13237
+ return `${compile(arg, freeVars, 3)}!`;
13238
+ }
13239
+ const OPS = {
13240
+ Add: ["+", 1],
13241
+ Subtract: ["-", 1],
13242
+ Multiply: ["*", 2],
13243
+ Divide: ["/", 3]
13244
+ };
13245
+ const op3 = OPS[h];
13246
+ if (op3 !== void 0) {
13247
+ const args2 = expr.ops;
13248
+ if (args2 === null)
13249
+ return "";
13250
+ const result = [];
13251
+ for (const arg of args2)
13252
+ result.push(compile(arg, freeVars, op3[1]));
13253
+ return op3[1] < prec ? `(${result.join(op3[0])})` : result.join(op3[0]);
13254
+ }
13255
+ const fn = {
13256
+ Abs: "Math.abs",
13257
+ Arccos: "Math.acos",
13258
+ Arcosh: "Math.acosh",
13259
+ Arsin: "Math.asin",
13260
+ Arsinh: "Math.asinh",
13261
+ Arctan: "Math.atan",
13262
+ Artanh: "Math.atanh",
13263
+ // Math.cbrt
13264
+ Ceiling: "Math.ceil",
13265
+ Cos: "Math.cos",
13266
+ Cosh: "Math.cosh",
13267
+ Exp: "Math.exp",
13268
+ Floor: "Math.floor",
13269
+ // Math.hypot
13270
+ Ln: "Math.log",
13271
+ Log: "Math.log10",
13272
+ Lb: "Math.log2",
13273
+ Max: "Math.max",
13274
+ Min: "Math.min",
13275
+ Power: "Math.pow",
13276
+ Random: "Math.random",
13277
+ Round: "Math.round",
13278
+ Sgn: "Math.sign",
13279
+ Sin: "Math.sin",
13280
+ Sinh: "Math.sinh",
13281
+ Sqrt: "Math.sqrt",
13282
+ Tan: "Math.tan",
13283
+ Tanh: "Math.tanh"
13284
+ // Factorial: 'factorial', // TODO: implement
13285
+ // Hallucinated by Copilot, but interesting ideas...
13286
+ // Cot: 'Math.cot',
13287
+ // Sec: 'Math.sec',
13288
+ // Csc: 'Math.csc',
13289
+ // ArcCot: 'Math.acot',
13290
+ // ArcSec: 'Math.asec',
13291
+ // ArcCsc: 'Math.acsc',
13292
+ // Coth: 'Math.coth',
13293
+ // Sech: 'Math.sech',
13294
+ // Csch: 'Math.csch',
13295
+ // ArcCoth: 'Math.acoth',
13296
+ // ArcSech: 'Math.asech',
13297
+ // ArcCsch: 'Math.acsch',
13298
+ // Root: 'Math.root',
13299
+ // Gamma: 'Math.gamma',
13300
+ // Erf: 'Math.erf',
13301
+ // Erfc: 'Math.erfc',
13302
+ // Erfi: 'Math.erfi',
13303
+ // Zeta: 'Math.zeta',
13304
+ // PolyGamma: 'Math.polygamma',
13305
+ // HurwitzZeta: 'Math.hurwitzZeta',
13306
+ // DirichletEta: 'Math.dirichletEta',
13307
+ // Beta: 'Math.beta',
13308
+ // Binomial: 'Math.binomial',
13309
+ // Mod: 'Math.mod',
13310
+ // Quotient: 'Math.quotient',
13311
+ // GCD: 'Math.gcd',
13312
+ // LCM: 'Math.lcm',
13313
+ // Divisors: 'Math.divisors',
13314
+ // PrimeQ: 'Math.isPrime',
13315
+ // PrimePi: 'Math.primePi',
13316
+ // Prime: 'Math.prime',
13317
+ // NextPrime: 'Math.nextPrime',
13318
+ // PreviousPrime: 'Math.prevPrime',
13319
+ // PrimePowerQ: 'Math.isPrimePower',
13320
+ // PrimePowerPi: 'Math.primePowerPi',
13321
+ // PrimePower: 'Math.primePower',
13322
+ // NextPrimePower: 'Math.nextPrimePower',
13323
+ // PreviousPrimePower: 'Math.prevPrimePower',
13324
+ // PrimeFactors: 'Math.primeFactors',
13325
+ // DivisorSigma: 'Math.divisorSigma',
13326
+ // DivisorSigma0: 'Math.divisorSigma0',
13327
+ // DivisorSigma1: 'Math.divisorSigma1',
13328
+ // DivisorSigma2: 'Math.divisorSigma2',
13329
+ // DivisorSigma3: 'Math.divisorSigma3',
13330
+ // DivisorSigma4: 'Math.divisorSigma4',
13331
+ // DivisorCount: 'Math.divisorCount',
13332
+ // DivisorSum: 'Math.divisorSum',
13333
+ // MoebiusMu: 'Math.moebiusMu',
13334
+ // LiouvilleLambda: 'Math.liouvilleLambda',
13335
+ // CarmichaelLambda: 'Math.carmichaelLambda',
13336
+ // EulerPhi: 'Math.eulerPhi',
13337
+ // EulerPsi: 'Math.eulerPsi',
13338
+ // EulerGamma: 'Math.eulerGamma',
13339
+ // HarmonicNumber: 'Math.harmonicNumber',
13340
+ // BernoulliB: 'Math.bernoulliB',
13341
+ // StirlingS1: 'Math.stirlingS1',
13342
+ // StirlingS2: 'Math.stirlingS2',
13343
+ // BellB: 'Math.bellB',
13344
+ // BellNumber: 'Math.bellNumber',
13345
+ // LahS: 'Math.lahS',
13346
+ // LahL: 'Math.lahL',
13347
+ // RiemannR: 'Math.riemannR',
13348
+ // RiemannZeta: 'Math.riemannZeta',
13349
+ // RiemannXi: 'Math.riemannXi',
13350
+ // RiemannH: 'Math.riemannH',
13351
+ // RiemannZ: 'Math.riemannZ',
13352
+ // RiemannS: 'Math.riemannS',
13353
+ // RiemannXiZero: 'Math.riemannXiZero',
13354
+ // RiemannZetaZero: 'Math.riemannZetaZero',
13355
+ // RiemannHZero: 'Math.riemannHZero',
13356
+ // RiemannSZero: 'Math.riemannSZero',
13357
+ // RiemannPrimeCount: 'Math.riemannPrimeCount',
13358
+ // RiemannRLog: 'Math.riemannRLog',
13359
+ // RiemannRLogDerivative: 'Math.riemannRLogDerivative',
13360
+ // RiemannRLogZero: 'Math.riemannRLogZero',
13361
+ // RiemannRLogZeroDerivative: 'Math.riemannRLogZeroDerivative',
13362
+ // RiemannRZero: 'Math.riemannRZero',
13363
+ // RiemannRDerivative: 'Math.riemannRDerivative',
13364
+ // RiemannXiZeroDerivative: 'Math.riemannXiZeroDerivative',
13365
+ // RiemannZetaZeroDerivative: 'Math.riemannZetaZeroDerivative',
13366
+ // RiemannHZeroDerivative: 'Math.riemannHZeroDerivative',
13367
+ // RiemannSZeroDerivative: 'Math.riemannSZeroDerivative',
13368
+ // RiemannSZeroDerivative2: 'Math.riemannSZeroDerivative2',
13369
+ // RiemannSZeroDerivative3: 'Math.riemannSZeroDerivative3',
13370
+ // RiemannSZeroDerivative4: 'Math.riemannSZeroDerivative4',
13371
+ // RiemannSZeroDerivative5: 'Math.riemannSZeroDerivative5',
13372
+ // RiemannSZeroDerivative6: 'Math.riemannSZeroDerivative6',
13373
+ }[h] ?? h;
13374
+ const args = expr.ops;
13375
+ if (args !== null) {
13376
+ const result = [];
13377
+ for (const arg of args)
13378
+ result.push(compile(arg, freeVars, 0));
13379
+ return `${fn}(${result.join(", ")})`;
13380
+ }
13381
+ }
13382
+ return "";
13383
+ }
13384
+ function compileLoop(expr, op3) {
13385
+ const args = expr.ops;
13386
+ if (args === null)
13387
+ return "NaN";
13388
+ if (!expr.op1 || !expr.op2)
13389
+ return "NaN";
13390
+ const [index, lower, upper, isFinite2] = normalizeLimits(expr.op2);
13391
+ const fn = compile(expr.op1, [...expr.op1.freeVars, index], 0);
13392
+ return `((_) => {
13393
+ let acc = ${op3 === "+" ? "0" : "1"};
13394
+ const fn = (_) => ${fn};
13395
+ for (let i = ${lower}; i < ${upper}; i++)
13396
+ acc ${op3}= fn({ ..._, ${index}: i });
13397
+ return acc;
13398
+ })()`;
13399
+ }
13400
+
13401
+ // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13144
13402
  var AbstractBoxedExpression = class {
13145
13403
  constructor(ce, metadata) {
13146
13404
  this.engine = ce;
@@ -13465,6 +13723,9 @@ var AbstractBoxedExpression = class {
13465
13723
  N(_options) {
13466
13724
  return this.evaluate();
13467
13725
  }
13726
+ compile() {
13727
+ return compileToJavascript(this);
13728
+ }
13468
13729
  };
13469
13730
 
13470
13731
  // src/compute-engine/boxed-expression/serialize.ts
@@ -14927,7 +15188,7 @@ function validateNumericArgs(ce, ops2, count) {
14927
15188
  }
14928
15189
  }
14929
15190
  return xs.map(
14930
- (op3) => !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
15191
+ (op3) => op3 && !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
14931
15192
  );
14932
15193
  }
14933
15194
  function validateSignature(sig, ops2, codomain) {
@@ -15635,7 +15896,6 @@ var UNIVARIATE_ROOTS = [
15635
15896
  // ],
15636
15897
  ];
15637
15898
  function findUnivariateRoots(expr, x) {
15638
- console.log("findUnivariateRoots", expr.toString(), x);
15639
15899
  const ce = expr.engine;
15640
15900
  if (expr.head === "Equal") {
15641
15901
  expr = ce.add([expr.op1.canonical, ce.neg(expr.op2.canonical)]).simplify();
@@ -19574,6 +19834,18 @@ function randomExpression(level) {
19574
19834
  level ?? (level = 1);
19575
19835
  if (level === 1) {
19576
19836
  const h = oneOf([
19837
+ [
19838
+ "Sqrt",
19839
+ [
19840
+ "Multiply",
19841
+ 6,
19842
+ [
19843
+ "Sum",
19844
+ ["Divide", 1, ["Power", "n", 2]],
19845
+ ["Triple", ["Hold", "n"], 1, { num: "+Infinity" }]
19846
+ ]
19847
+ ]
19848
+ ],
19577
19849
  "Add",
19578
19850
  "Add",
19579
19851
  "Add",
@@ -19592,13 +19864,16 @@ function randomExpression(level) {
19592
19864
  "Negate",
19593
19865
  "trig"
19594
19866
  ]);
19595
- return randomExpressionWithHead(h, 1);
19867
+ if (typeof h === "string")
19868
+ return randomExpressionWithHead(h, 1);
19869
+ return h;
19596
19870
  }
19597
19871
  if (level === 2) {
19598
- if (Math.random() > 0.5)
19599
- return randomExpression(3);
19600
- if (Math.random() > 0.75)
19872
+ const r = Math.random();
19873
+ if (r > 0.75)
19601
19874
  return randomExpression(1);
19875
+ if (r > 0.5)
19876
+ return randomExpression(3);
19602
19877
  const h = oneOf([
19603
19878
  "Multiply",
19604
19879
  "Multiply",
@@ -19652,7 +19927,7 @@ function randomExpression(level) {
19652
19927
  ["Rational", 12, 15],
19653
19928
  ["Rational", 15, 12],
19654
19929
  "ExponentialE",
19655
- "ImaginaryUnit",
19930
+ // 'ImaginaryUnit',
19656
19931
  ["Sqrt", 3],
19657
19932
  ["Sqrt", 5],
19658
19933
  ["Sqrt", 15],
@@ -19670,8 +19945,8 @@ function randomExpression(level) {
19670
19945
  ["Power", "x", 4],
19671
19946
  ["Subtract", "x", 1],
19672
19947
  ["Add", "x", 1],
19673
- "a",
19674
- "b",
19948
+ // 'a',
19949
+ // 'b',
19675
19950
  "Pi"
19676
19951
  ]);
19677
19952
  }
@@ -23717,6 +23992,12 @@ var ComputeEngine = class _ComputeEngine {
23717
23992
  static getStandardLibrary(categories = "all") {
23718
23993
  return getStandardLibrary(categories);
23719
23994
  }
23995
+ get latexDictionary() {
23996
+ return this.latexSyntax.dictionary;
23997
+ }
23998
+ set latexDictionary(dic) {
23999
+ this.latexSyntax.dictionary = dic;
24000
+ }
23720
24001
  /** After the configuration of the engine has changed, clear the caches
23721
24002
  * so that new values can be recalculated.
23722
24003
  *
@@ -23911,6 +24192,10 @@ var ComputeEngine = class _ComputeEngine {
23911
24192
  return new this._bignum(a);
23912
24193
  }
23913
24194
  complex(a, b) {
24195
+ if (a instanceof Decimal)
24196
+ a = a.toNumber();
24197
+ if (b instanceof Decimal)
24198
+ b = b.toNumber();
23914
24199
  return new import_complex19.Complex(a, b);
23915
24200
  }
23916
24201
  isBignum(a) {
@@ -24606,10 +24891,10 @@ var ComputeEngine = class _ComputeEngine {
24606
24891
  };
24607
24892
 
24608
24893
  // src/compute-engine.ts
24609
- var version = "0.12.6";
24894
+ var version = "0.13.0";
24610
24895
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
24611
24896
  ComputeEngine: ComputeEngine.prototype.constructor,
24612
- version: "0.12.6"
24897
+ version: "0.13.0"
24613
24898
  };
24614
24899
  export {
24615
24900
  ComputeEngine,