@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
  /* @__PURE__ */ 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
+ /* @__PURE__ */ 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
  /* @__PURE__ */ 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)
@@ -13140,6 +13158,246 @@ function domainSetsLibrary() {
13140
13158
 
13141
13159
  // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13142
13160
  var import_complex6 = __toESM(require_complex());
13161
+
13162
+ // src/compute-engine/compile.ts
13163
+ function compileToJavascript(expr) {
13164
+ const js = compile(expr, expr.freeVars);
13165
+ try {
13166
+ return new Function("_", `return ${js}`);
13167
+ } catch (e) {
13168
+ console.error(`${e}
13169
+ ${expr.latex}
13170
+ ${js}`);
13171
+ }
13172
+ return void 0;
13173
+ }
13174
+ function compile(expr, freeVars = [], prec = 0) {
13175
+ const f = asFloat(expr);
13176
+ if (f !== null)
13177
+ return f.toString();
13178
+ const s = expr.symbol;
13179
+ if (s !== null) {
13180
+ const result = {
13181
+ True: "true",
13182
+ False: "false",
13183
+ Pi: "Math.PI",
13184
+ ExponentialE: "Math.E",
13185
+ I: "Math.I",
13186
+ NaN: "Number.NaN",
13187
+ ImaginaryUnit: "NaN"
13188
+ }[s];
13189
+ if (result !== void 0)
13190
+ return result;
13191
+ if (freeVars.includes(s))
13192
+ return `_.${s}`;
13193
+ return s;
13194
+ }
13195
+ const str = expr.string;
13196
+ if (str !== null)
13197
+ return JSON.stringify(str);
13198
+ const keys = expr.keys;
13199
+ if (keys !== null) {
13200
+ const result = [];
13201
+ for (const key of keys) {
13202
+ const value = expr.getKey(key);
13203
+ if (value)
13204
+ result.push(`${key}: ${compile(value, freeVars, 0)}`);
13205
+ }
13206
+ return `{${result.join(", ")}}`;
13207
+ }
13208
+ const h = expr.head;
13209
+ if (typeof h === "string") {
13210
+ if (h === "Negate") {
13211
+ const arg = expr.op1;
13212
+ if (arg === null)
13213
+ return "";
13214
+ return `-${compile(arg, freeVars, 3)}`;
13215
+ }
13216
+ if (h === "Error") {
13217
+ return "NaN";
13218
+ }
13219
+ if (h === "Sum")
13220
+ return compileLoop(expr, "+");
13221
+ if (h === "Product")
13222
+ return compileLoop(expr, "*");
13223
+ if (h === "Root") {
13224
+ const arg = expr.op1;
13225
+ if (arg === null)
13226
+ return "";
13227
+ const exp2 = expr.op2;
13228
+ if (exp2 === null)
13229
+ return `Math.sqrt(${compile(arg, freeVars, 0)})`;
13230
+ return `Math.pow(${compile(arg, freeVars)}, 1/${compile(exp2, freeVars)}`;
13231
+ }
13232
+ if (h === "Factorial") {
13233
+ const arg = expr.op1;
13234
+ if (arg === null)
13235
+ return "";
13236
+ return `${compile(arg, freeVars, 3)}!`;
13237
+ }
13238
+ const OPS = {
13239
+ Add: ["+", 1],
13240
+ Subtract: ["-", 1],
13241
+ Multiply: ["*", 2],
13242
+ Divide: ["/", 3]
13243
+ };
13244
+ const op3 = OPS[h];
13245
+ if (op3 !== void 0) {
13246
+ const args2 = expr.ops;
13247
+ if (args2 === null)
13248
+ return "";
13249
+ const result = [];
13250
+ for (const arg of args2)
13251
+ result.push(compile(arg, freeVars, op3[1]));
13252
+ return op3[1] < prec ? `(${result.join(op3[0])})` : result.join(op3[0]);
13253
+ }
13254
+ const fn = {
13255
+ Abs: "Math.abs",
13256
+ Arccos: "Math.acos",
13257
+ Arcosh: "Math.acosh",
13258
+ Arsin: "Math.asin",
13259
+ Arsinh: "Math.asinh",
13260
+ Arctan: "Math.atan",
13261
+ Artanh: "Math.atanh",
13262
+ // Math.cbrt
13263
+ Ceiling: "Math.ceil",
13264
+ Cos: "Math.cos",
13265
+ Cosh: "Math.cosh",
13266
+ Exp: "Math.exp",
13267
+ Floor: "Math.floor",
13268
+ // Math.hypot
13269
+ Ln: "Math.log",
13270
+ Log: "Math.log10",
13271
+ Lb: "Math.log2",
13272
+ Max: "Math.max",
13273
+ Min: "Math.min",
13274
+ Power: "Math.pow",
13275
+ Random: "Math.random",
13276
+ Round: "Math.round",
13277
+ Sgn: "Math.sign",
13278
+ Sin: "Math.sin",
13279
+ Sinh: "Math.sinh",
13280
+ Sqrt: "Math.sqrt",
13281
+ Tan: "Math.tan",
13282
+ Tanh: "Math.tanh"
13283
+ // Factorial: 'factorial', // TODO: implement
13284
+ // Hallucinated by Copilot, but interesting ideas...
13285
+ // Cot: 'Math.cot',
13286
+ // Sec: 'Math.sec',
13287
+ // Csc: 'Math.csc',
13288
+ // ArcCot: 'Math.acot',
13289
+ // ArcSec: 'Math.asec',
13290
+ // ArcCsc: 'Math.acsc',
13291
+ // Coth: 'Math.coth',
13292
+ // Sech: 'Math.sech',
13293
+ // Csch: 'Math.csch',
13294
+ // ArcCoth: 'Math.acoth',
13295
+ // ArcSech: 'Math.asech',
13296
+ // ArcCsch: 'Math.acsch',
13297
+ // Root: 'Math.root',
13298
+ // Gamma: 'Math.gamma',
13299
+ // Erf: 'Math.erf',
13300
+ // Erfc: 'Math.erfc',
13301
+ // Erfi: 'Math.erfi',
13302
+ // Zeta: 'Math.zeta',
13303
+ // PolyGamma: 'Math.polygamma',
13304
+ // HurwitzZeta: 'Math.hurwitzZeta',
13305
+ // DirichletEta: 'Math.dirichletEta',
13306
+ // Beta: 'Math.beta',
13307
+ // Binomial: 'Math.binomial',
13308
+ // Mod: 'Math.mod',
13309
+ // Quotient: 'Math.quotient',
13310
+ // GCD: 'Math.gcd',
13311
+ // LCM: 'Math.lcm',
13312
+ // Divisors: 'Math.divisors',
13313
+ // PrimeQ: 'Math.isPrime',
13314
+ // PrimePi: 'Math.primePi',
13315
+ // Prime: 'Math.prime',
13316
+ // NextPrime: 'Math.nextPrime',
13317
+ // PreviousPrime: 'Math.prevPrime',
13318
+ // PrimePowerQ: 'Math.isPrimePower',
13319
+ // PrimePowerPi: 'Math.primePowerPi',
13320
+ // PrimePower: 'Math.primePower',
13321
+ // NextPrimePower: 'Math.nextPrimePower',
13322
+ // PreviousPrimePower: 'Math.prevPrimePower',
13323
+ // PrimeFactors: 'Math.primeFactors',
13324
+ // DivisorSigma: 'Math.divisorSigma',
13325
+ // DivisorSigma0: 'Math.divisorSigma0',
13326
+ // DivisorSigma1: 'Math.divisorSigma1',
13327
+ // DivisorSigma2: 'Math.divisorSigma2',
13328
+ // DivisorSigma3: 'Math.divisorSigma3',
13329
+ // DivisorSigma4: 'Math.divisorSigma4',
13330
+ // DivisorCount: 'Math.divisorCount',
13331
+ // DivisorSum: 'Math.divisorSum',
13332
+ // MoebiusMu: 'Math.moebiusMu',
13333
+ // LiouvilleLambda: 'Math.liouvilleLambda',
13334
+ // CarmichaelLambda: 'Math.carmichaelLambda',
13335
+ // EulerPhi: 'Math.eulerPhi',
13336
+ // EulerPsi: 'Math.eulerPsi',
13337
+ // EulerGamma: 'Math.eulerGamma',
13338
+ // HarmonicNumber: 'Math.harmonicNumber',
13339
+ // BernoulliB: 'Math.bernoulliB',
13340
+ // StirlingS1: 'Math.stirlingS1',
13341
+ // StirlingS2: 'Math.stirlingS2',
13342
+ // BellB: 'Math.bellB',
13343
+ // BellNumber: 'Math.bellNumber',
13344
+ // LahS: 'Math.lahS',
13345
+ // LahL: 'Math.lahL',
13346
+ // RiemannR: 'Math.riemannR',
13347
+ // RiemannZeta: 'Math.riemannZeta',
13348
+ // RiemannXi: 'Math.riemannXi',
13349
+ // RiemannH: 'Math.riemannH',
13350
+ // RiemannZ: 'Math.riemannZ',
13351
+ // RiemannS: 'Math.riemannS',
13352
+ // RiemannXiZero: 'Math.riemannXiZero',
13353
+ // RiemannZetaZero: 'Math.riemannZetaZero',
13354
+ // RiemannHZero: 'Math.riemannHZero',
13355
+ // RiemannSZero: 'Math.riemannSZero',
13356
+ // RiemannPrimeCount: 'Math.riemannPrimeCount',
13357
+ // RiemannRLog: 'Math.riemannRLog',
13358
+ // RiemannRLogDerivative: 'Math.riemannRLogDerivative',
13359
+ // RiemannRLogZero: 'Math.riemannRLogZero',
13360
+ // RiemannRLogZeroDerivative: 'Math.riemannRLogZeroDerivative',
13361
+ // RiemannRZero: 'Math.riemannRZero',
13362
+ // RiemannRDerivative: 'Math.riemannRDerivative',
13363
+ // RiemannXiZeroDerivative: 'Math.riemannXiZeroDerivative',
13364
+ // RiemannZetaZeroDerivative: 'Math.riemannZetaZeroDerivative',
13365
+ // RiemannHZeroDerivative: 'Math.riemannHZeroDerivative',
13366
+ // RiemannSZeroDerivative: 'Math.riemannSZeroDerivative',
13367
+ // RiemannSZeroDerivative2: 'Math.riemannSZeroDerivative2',
13368
+ // RiemannSZeroDerivative3: 'Math.riemannSZeroDerivative3',
13369
+ // RiemannSZeroDerivative4: 'Math.riemannSZeroDerivative4',
13370
+ // RiemannSZeroDerivative5: 'Math.riemannSZeroDerivative5',
13371
+ // RiemannSZeroDerivative6: 'Math.riemannSZeroDerivative6',
13372
+ }[h] ?? h;
13373
+ const args = expr.ops;
13374
+ if (args !== null) {
13375
+ const result = [];
13376
+ for (const arg of args)
13377
+ result.push(compile(arg, freeVars, 0));
13378
+ return `${fn}(${result.join(", ")})`;
13379
+ }
13380
+ }
13381
+ return "";
13382
+ }
13383
+ function compileLoop(expr, op3) {
13384
+ const args = expr.ops;
13385
+ if (args === null)
13386
+ return "NaN";
13387
+ if (!expr.op1 || !expr.op2)
13388
+ return "NaN";
13389
+ const [index, lower, upper, isFinite2] = normalizeLimits(expr.op2);
13390
+ const fn = compile(expr.op1, [...expr.op1.freeVars, index], 0);
13391
+ return `((_) => {
13392
+ let acc = ${op3 === "+" ? "0" : "1"};
13393
+ const fn = (_) => ${fn};
13394
+ for (let i = ${lower}; i < ${upper}; i++)
13395
+ acc ${op3}= fn({ ..._, ${index}: i });
13396
+ return acc;
13397
+ })()`;
13398
+ }
13399
+
13400
+ // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13143
13401
  var AbstractBoxedExpression = class {
13144
13402
  constructor(ce, metadata) {
13145
13403
  this.engine = ce;
@@ -13464,6 +13722,9 @@ var AbstractBoxedExpression = class {
13464
13722
  N(_options) {
13465
13723
  return this.evaluate();
13466
13724
  }
13725
+ compile() {
13726
+ return compileToJavascript(this);
13727
+ }
13467
13728
  };
13468
13729
 
13469
13730
  // src/compute-engine/boxed-expression/serialize.ts
@@ -14926,7 +15187,7 @@ function validateNumericArgs(ce, ops2, count) {
14926
15187
  }
14927
15188
  }
14928
15189
  return xs.map(
14929
- (op3) => !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
15190
+ (op3) => op3 && !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
14930
15191
  );
14931
15192
  }
14932
15193
  function validateSignature(sig, ops2, codomain) {
@@ -15634,7 +15895,6 @@ var UNIVARIATE_ROOTS = [
15634
15895
  // ],
15635
15896
  ];
15636
15897
  function findUnivariateRoots(expr, x) {
15637
- /* @__PURE__ */ console.log("findUnivariateRoots", expr.toString(), x);
15638
15898
  const ce = expr.engine;
15639
15899
  if (expr.head === "Equal") {
15640
15900
  expr = ce.add([expr.op1.canonical, ce.neg(expr.op2.canonical)]).simplify();
@@ -19570,6 +19830,18 @@ function randomExpression(level) {
19570
19830
  level ?? (level = 1);
19571
19831
  if (level === 1) {
19572
19832
  const h = oneOf([
19833
+ [
19834
+ "Sqrt",
19835
+ [
19836
+ "Multiply",
19837
+ 6,
19838
+ [
19839
+ "Sum",
19840
+ ["Divide", 1, ["Power", "n", 2]],
19841
+ ["Triple", ["Hold", "n"], 1, { num: "+Infinity" }]
19842
+ ]
19843
+ ]
19844
+ ],
19573
19845
  "Add",
19574
19846
  "Add",
19575
19847
  "Add",
@@ -19588,13 +19860,16 @@ function randomExpression(level) {
19588
19860
  "Negate",
19589
19861
  "trig"
19590
19862
  ]);
19591
- return randomExpressionWithHead(h, 1);
19863
+ if (typeof h === "string")
19864
+ return randomExpressionWithHead(h, 1);
19865
+ return h;
19592
19866
  }
19593
19867
  if (level === 2) {
19594
- if (Math.random() > 0.5)
19595
- return randomExpression(3);
19596
- if (Math.random() > 0.75)
19868
+ const r = Math.random();
19869
+ if (r > 0.75)
19597
19870
  return randomExpression(1);
19871
+ if (r > 0.5)
19872
+ return randomExpression(3);
19598
19873
  const h = oneOf([
19599
19874
  "Multiply",
19600
19875
  "Multiply",
@@ -19648,7 +19923,7 @@ function randomExpression(level) {
19648
19923
  ["Rational", 12, 15],
19649
19924
  ["Rational", 15, 12],
19650
19925
  "ExponentialE",
19651
- "ImaginaryUnit",
19926
+ // 'ImaginaryUnit',
19652
19927
  ["Sqrt", 3],
19653
19928
  ["Sqrt", 5],
19654
19929
  ["Sqrt", 15],
@@ -19666,8 +19941,8 @@ function randomExpression(level) {
19666
19941
  ["Power", "x", 4],
19667
19942
  ["Subtract", "x", 1],
19668
19943
  ["Add", "x", 1],
19669
- "a",
19670
- "b",
19944
+ // 'a',
19945
+ // 'b',
19671
19946
  "Pi"
19672
19947
  ]);
19673
19948
  }
@@ -23713,6 +23988,12 @@ var ComputeEngine = class _ComputeEngine {
23713
23988
  static getStandardLibrary(categories = "all") {
23714
23989
  return getStandardLibrary(categories);
23715
23990
  }
23991
+ get latexDictionary() {
23992
+ return this.latexSyntax.dictionary;
23993
+ }
23994
+ set latexDictionary(dic) {
23995
+ this.latexSyntax.dictionary = dic;
23996
+ }
23716
23997
  /** After the configuration of the engine has changed, clear the caches
23717
23998
  * so that new values can be recalculated.
23718
23999
  *
@@ -23907,6 +24188,10 @@ var ComputeEngine = class _ComputeEngine {
23907
24188
  return new this._bignum(a);
23908
24189
  }
23909
24190
  complex(a, b) {
24191
+ if (a instanceof Decimal)
24192
+ a = a.toNumber();
24193
+ if (b instanceof Decimal)
24194
+ b = b.toNumber();
23910
24195
  return new import_complex19.Complex(a, b);
23911
24196
  }
23912
24197
  isBignum(a) {
@@ -24602,10 +24887,10 @@ var ComputeEngine = class _ComputeEngine {
24602
24887
  };
24603
24888
 
24604
24889
  // src/compute-engine.ts
24605
- var version = "0.12.6";
24890
+ var version = "0.13.0";
24606
24891
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
24607
24892
  ComputeEngine: ComputeEngine.prototype.constructor,
24608
- version: "0.12.6"
24893
+ version: "0.13.0"
24609
24894
  };
24610
24895
  export {
24611
24896
  ComputeEngine,