@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
  (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 __create = Object.create;
@@ -3971,6 +3971,14 @@ var ComputeEngine = (() => {
3971
3971
  return null;
3972
3972
  return s;
3973
3973
  }
3974
+ function isListLike(expr) {
3975
+ if (expr === null)
3976
+ return false;
3977
+ const h = head(expr);
3978
+ if (!h || typeof h !== "string")
3979
+ return false;
3980
+ return /^(List|Sequence|Tuple|Single|Pair|Triple)$/.test(h);
3981
+ }
3974
3982
  function keyValuePair(expr) {
3975
3983
  const h = head(expr);
3976
3984
  if (h === "KeyValuePair" || h === "Tuple" || h === "Pair") {
@@ -4606,6 +4614,10 @@ var ComputeEngine = (() => {
4606
4614
  parse: "ExponentialE",
4607
4615
  serialize: "\\exponentialE"
4608
4616
  },
4617
+ {
4618
+ trigger: "\\mathrm{e}",
4619
+ parse: "ExponentialE"
4620
+ },
4609
4621
  {
4610
4622
  kind: "function",
4611
4623
  trigger: "exp",
@@ -4619,6 +4631,10 @@ var ComputeEngine = (() => {
4619
4631
  name: "ImaginaryUnit",
4620
4632
  trigger: ["\\imaginaryI"]
4621
4633
  },
4634
+ {
4635
+ trigger: "\\mathrm{i}",
4636
+ parse: "ImaginaryUnit"
4637
+ },
4622
4638
  // Operations
4623
4639
  {
4624
4640
  /** Could be the determinant if the argument is a matrix */
@@ -5178,7 +5194,7 @@ var ComputeEngine = (() => {
5178
5194
  }
5179
5195
  return result;
5180
5196
  }
5181
- function serializeSequence(sep = "") {
5197
+ function serializeOps(sep = "") {
5182
5198
  return (serializer, expr) => (ops(expr) ?? []).map((x) => serializer.serialize(x)).join(sep);
5183
5199
  }
5184
5200
  var DEFINITIONS_CORE = [
@@ -5246,22 +5262,19 @@ var ComputeEngine = (() => {
5246
5262
  return "";
5247
5263
  const style = serializer.options.groupStyle(expr, serializer.level + 1);
5248
5264
  const arg1 = op(expr, 1);
5249
- if (argCount === 1)
5250
- return serializer.wrapString(serializer.serialize(arg1), style);
5251
- let sep = "";
5252
- let open = "";
5253
- let close = "";
5265
+ const h1 = head(arg1);
5266
+ const defaultFence = { List: "[],", Sequence: "" }[typeof h1 === "string" ? h1 : ""] ?? "(),";
5267
+ let open = defaultFence[0] ?? "";
5268
+ let close = defaultFence[1] ?? "";
5269
+ let sep = defaultFence[2] ?? "";
5254
5270
  if (argCount > 1) {
5255
5271
  const op22 = stringValue(op(expr, 2)) ?? "";
5256
- open = op22[0] ?? "(";
5257
- close = op22[1] ?? ")";
5258
- sep = op22[2] ?? ",";
5272
+ open = op22[0] ?? defaultFence[0];
5273
+ close = op22[1] ?? defaultFence[1];
5274
+ sep = op22[2] ?? defaultFence[2];
5259
5275
  }
5260
- const body = head(arg1) === "List" ? serializeSequence(sep)(serializer, arg1) : serializer.serialize(arg1);
5261
- serializer.wrapString(body, style, stringValue(op(expr, 2)) ?? void 0);
5262
- if (!open || !close)
5263
- return serializer.wrapString(body, style);
5264
- return `${open} ${body} ${close}`;
5276
+ const body = isListLike(arg1) ? serializeOps(sep)(serializer, arg1) : serializer.serialize(arg1);
5277
+ return serializer.wrapString(body, style, open + close);
5265
5278
  }
5266
5279
  },
5267
5280
  {
@@ -5362,7 +5375,7 @@ var ComputeEngine = (() => {
5362
5375
  serialize: (serializer, expr) => {
5363
5376
  return joinLatex([
5364
5377
  "\\lbrack",
5365
- serializeSequence(", ")(serializer, expr),
5378
+ serializeOps(", ")(serializer, expr),
5366
5379
  "\\rbrack"
5367
5380
  ]);
5368
5381
  }
@@ -5400,7 +5413,7 @@ var ComputeEngine = (() => {
5400
5413
  },
5401
5414
  {
5402
5415
  name: "Sequence",
5403
- serialize: serializeSequence("")
5416
+ serialize: serializeOps("")
5404
5417
  },
5405
5418
  {
5406
5419
  trigger: [";"],
@@ -5530,24 +5543,17 @@ var ComputeEngine = (() => {
5530
5543
  return result;
5531
5544
  },
5532
5545
  serialize: (serialize2, expr) => {
5533
- if (head(op(expr, 1)) !== "List")
5534
- return "";
5535
- const rows = ops(op(expr, 1)) ?? [];
5536
- const body = [];
5537
- let rowSep = "";
5538
- for (const row of rows) {
5539
- if (head(row) === "Tuple" || head(row) === "Pair") {
5540
- body.push(rowSep);
5541
- if (op(row, 2)) {
5542
- body.push(serialize2.serialize(op(row, 2)));
5543
- const condition = op(row, 1);
5544
- if (condition !== null)
5545
- body.push("&", serialize2.serialize(condition));
5546
- }
5546
+ const rows = [];
5547
+ const args = ops(expr);
5548
+ if (args) {
5549
+ for (let i = 0; i <= args.length - 2; i += 2) {
5550
+ const row = [];
5551
+ row.push(serialize2.serialize(args[i + 1]));
5552
+ row.push(serialize2.serialize(args[i]));
5553
+ rows.push(row.join("&"));
5547
5554
  }
5548
- rowSep = "\\\\";
5549
5555
  }
5550
- return joinLatex(["\\begin{cases}", ...body, "\\end{cases}"]);
5556
+ return joinLatex(["\\begin{cases}", rows.join("\\\\"), "\\end{cases}"]);
5551
5557
  }
5552
5558
  }
5553
5559
  ];
@@ -7449,11 +7455,6 @@ var ComputeEngine = (() => {
7449
7455
  "\\rceil": "\\rceil",
7450
7456
  "\\rfloor": "\\rfloor"
7451
7457
  };
7452
- function triggerLength(trigger) {
7453
- if (Array.isArray(trigger))
7454
- return trigger.length;
7455
- return 1;
7456
- }
7457
7458
  function addEntry(result, entry, onError) {
7458
7459
  const [trigger, indexedEntry] = makeIndexedEntry(entry, onError);
7459
7460
  if (indexedEntry === null)
@@ -7495,7 +7496,7 @@ var ComputeEngine = (() => {
7495
7496
  if (indexedEntry.kind === "matchfix") {
7496
7497
  result.matchfix.push(indexedEntry);
7497
7498
  } else if (indexedEntry.kind === "environment") {
7498
- const triggerString = tokensToString(entry.trigger ?? "");
7499
+ const triggerString = tokensToString(entry.trigger ?? []);
7499
7500
  if (result.environment.has(triggerString)) {
7500
7501
  onError({
7501
7502
  severity: "warning",
@@ -7509,22 +7510,31 @@ var ComputeEngine = (() => {
7509
7510
  result.environment.set(triggerString, indexedEntry);
7510
7511
  } else if (trigger) {
7511
7512
  /* @__PURE__ */ console.assert(entry.trigger);
7512
- const triggerString = tokensToString(entry.trigger ?? "");
7513
- const n = triggerLength(trigger);
7514
- result.lookahead = Math.max(result.lookahead, n);
7515
7513
  if (indexedEntry.kind === "function") {
7516
- if (!result.function.has(triggerString))
7517
- result.function.set(triggerString, [indexedEntry]);
7514
+ /* @__PURE__ */ console.assert(typeof entry.trigger === "string");
7515
+ const fnName = entry.trigger;
7516
+ if (!result.function.has(fnName))
7517
+ result.function.set(fnName, [indexedEntry]);
7518
7518
  else
7519
- result.function.set(triggerString, [
7520
- ...result.function.get(triggerString),
7519
+ result.function.set(fnName, [
7520
+ ...result.function.get(fnName),
7521
7521
  indexedEntry
7522
7522
  ]);
7523
7523
  } else {
7524
+ let triggerString;
7525
+ let tokenCount;
7526
+ if (typeof entry.trigger === "string") {
7527
+ triggerString = entry.trigger;
7528
+ tokenCount = countTokens(triggerString);
7529
+ } else {
7530
+ triggerString = tokensToString(entry.trigger ?? []);
7531
+ tokenCount = entry.trigger.length;
7532
+ }
7533
+ result.lookahead = Math.max(result.lookahead, tokenCount);
7524
7534
  const kind2 = indexedEntry.kind;
7525
- if (result[kind2][n] === void 0)
7526
- result[kind2][n] = /* @__PURE__ */ new Map();
7527
- const list = result[kind2][n];
7535
+ if (result[kind2][tokenCount] === void 0)
7536
+ result[kind2][tokenCount] = /* @__PURE__ */ new Map();
7537
+ const list = result[kind2][tokenCount];
7528
7538
  if (list.has(triggerString))
7529
7539
  list.get(triggerString).push(indexedEntry);
7530
7540
  else
@@ -7536,12 +7546,12 @@ var ComputeEngine = (() => {
7536
7546
  const result = {
7537
7547
  lookahead: 1,
7538
7548
  name: /* @__PURE__ */ new Map(),
7539
- expression: /* @__PURE__ */ new Map(),
7540
- function: /* @__PURE__ */ new Map(),
7549
+ expression: [],
7541
7550
  symbol: [],
7542
7551
  infix: [],
7543
7552
  prefix: [],
7544
7553
  postfix: [],
7554
+ function: /* @__PURE__ */ new Map(),
7545
7555
  environment: /* @__PURE__ */ new Map(),
7546
7556
  matchfix: []
7547
7557
  };
@@ -7604,13 +7614,11 @@ var ComputeEngine = (() => {
7604
7614
  if (result.kind === "expression" && isExpressionEntry(entry)) {
7605
7615
  result.serialize = entry.serialize ?? triggerString;
7606
7616
  if (typeof result.serialize === "string") {
7607
- const serializeExpr = result.serialize;
7617
+ const serializeStr = result.serialize;
7608
7618
  result.serialize = (serializer, expr) => {
7609
7619
  if (!head(expr))
7610
- return serializer.serialize(serializeExpr);
7611
- return `${serializer.serialize(
7612
- serializeExpr
7613
- )}${serializer.wrapArguments(expr)}`;
7620
+ return serializeStr;
7621
+ return `${serializeStr}${serializer.wrapArguments(expr)}`;
7614
7622
  };
7615
7623
  }
7616
7624
  {
@@ -8357,7 +8365,7 @@ var ComputeEngine = (() => {
8357
8365
  defs = this.lookAhead().map((x, n) => this._dictionary[kind][n]?.get(x));
8358
8366
  }
8359
8367
  const result = [];
8360
- for (let i = defs.length; i > 0; i--) {
8368
+ for (let i = defs.length - 1; i > 0; i--) {
8361
8369
  if (defs[i] !== void 0) {
8362
8370
  /* @__PURE__ */ console.assert(Array.isArray(defs[i]));
8363
8371
  for (const def of defs[i])
@@ -10111,14 +10119,16 @@ var ComputeEngine = (() => {
10111
10119
  wrapString(s, style, fence) {
10112
10120
  if (style === "none")
10113
10121
  return s;
10114
- const openFence = fence?.[0] ?? "(";
10115
- const closeFence = fence?.[1] ?? ")";
10122
+ if (fence === void 0)
10123
+ fence = "()";
10124
+ const openFence = fence?.[0] ?? ".";
10125
+ const closeFence = fence?.[1] ?? ".";
10116
10126
  if ((openFence === "." || closeFence === ".") && style === "paren")
10117
10127
  style = "leftright";
10118
10128
  if (style === "leftright")
10119
- return `${openFence === "." ? "" : `\\left(${openFence}`}${s}${closeFence === "." ? "" : `\\right(${closeFence}`})`;
10129
+ return `\\left${openFence}${s}\\right${closeFence}}`;
10120
10130
  if (style === "big")
10121
- return `${openFence === "." ? "" : `\\Bigl(${openFence}`}${s}${closeFence === "." ? "" : `\\Bigr(${closeFence}`})`;
10131
+ return `${openFence === "." ? "" : `\\Bigl${openFence}`}${s}${closeFence === "." ? "" : `\\Bigr${closeFence}`})`;
10122
10132
  return openFence + s + closeFence;
10123
10133
  }
10124
10134
  wrapArguments(expr) {
@@ -10467,11 +10477,19 @@ var ComputeEngine = (() => {
10467
10477
  ...DEFAULT_SERIALIZE_LATEX_OPTIONS,
10468
10478
  ...opts
10469
10479
  };
10480
+ this._dictionaryInput = options.dictionary ?? _LatexSyntax.getDictionary();
10470
10481
  this._dictionary = indexLatexDictionary(
10471
- options.dictionary ?? _LatexSyntax.getDictionary(),
10482
+ this._dictionaryInput,
10472
10483
  (sig) => this.onError([sig])
10473
10484
  );
10474
10485
  }
10486
+ get dictionary() {
10487
+ return this._dictionaryInput;
10488
+ }
10489
+ set dictionary(val) {
10490
+ this._dictionaryInput = val;
10491
+ this._dictionary = indexLatexDictionary(val, (sig) => this.onError([sig]));
10492
+ }
10475
10493
  updateOptions(opt) {
10476
10494
  for (const k of Object.keys(this.options))
10477
10495
  if (k in opt)
@@ -13162,6 +13180,246 @@ var ComputeEngine = (() => {
13162
13180
 
13163
13181
  // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13164
13182
  var import_complex6 = __toESM(require_complex());
13183
+
13184
+ // src/compute-engine/compile.ts
13185
+ function compileToJavascript(expr) {
13186
+ const js = compile(expr, expr.freeVars);
13187
+ try {
13188
+ return new Function("_", `return ${js}`);
13189
+ } catch (e) {
13190
+ console.error(`${e}
13191
+ ${expr.latex}
13192
+ ${js}`);
13193
+ }
13194
+ return void 0;
13195
+ }
13196
+ function compile(expr, freeVars = [], prec = 0) {
13197
+ const f = asFloat(expr);
13198
+ if (f !== null)
13199
+ return f.toString();
13200
+ const s = expr.symbol;
13201
+ if (s !== null) {
13202
+ const result = {
13203
+ True: "true",
13204
+ False: "false",
13205
+ Pi: "Math.PI",
13206
+ ExponentialE: "Math.E",
13207
+ I: "Math.I",
13208
+ NaN: "Number.NaN",
13209
+ ImaginaryUnit: "NaN"
13210
+ }[s];
13211
+ if (result !== void 0)
13212
+ return result;
13213
+ if (freeVars.includes(s))
13214
+ return `_.${s}`;
13215
+ return s;
13216
+ }
13217
+ const str = expr.string;
13218
+ if (str !== null)
13219
+ return JSON.stringify(str);
13220
+ const keys = expr.keys;
13221
+ if (keys !== null) {
13222
+ const result = [];
13223
+ for (const key of keys) {
13224
+ const value = expr.getKey(key);
13225
+ if (value)
13226
+ result.push(`${key}: ${compile(value, freeVars, 0)}`);
13227
+ }
13228
+ return `{${result.join(", ")}}`;
13229
+ }
13230
+ const h = expr.head;
13231
+ if (typeof h === "string") {
13232
+ if (h === "Negate") {
13233
+ const arg = expr.op1;
13234
+ if (arg === null)
13235
+ return "";
13236
+ return `-${compile(arg, freeVars, 3)}`;
13237
+ }
13238
+ if (h === "Error") {
13239
+ return "NaN";
13240
+ }
13241
+ if (h === "Sum")
13242
+ return compileLoop(expr, "+");
13243
+ if (h === "Product")
13244
+ return compileLoop(expr, "*");
13245
+ if (h === "Root") {
13246
+ const arg = expr.op1;
13247
+ if (arg === null)
13248
+ return "";
13249
+ const exp2 = expr.op2;
13250
+ if (exp2 === null)
13251
+ return `Math.sqrt(${compile(arg, freeVars, 0)})`;
13252
+ return `Math.pow(${compile(arg, freeVars)}, 1/${compile(exp2, freeVars)}`;
13253
+ }
13254
+ if (h === "Factorial") {
13255
+ const arg = expr.op1;
13256
+ if (arg === null)
13257
+ return "";
13258
+ return `${compile(arg, freeVars, 3)}!`;
13259
+ }
13260
+ const OPS = {
13261
+ Add: ["+", 1],
13262
+ Subtract: ["-", 1],
13263
+ Multiply: ["*", 2],
13264
+ Divide: ["/", 3]
13265
+ };
13266
+ const op3 = OPS[h];
13267
+ if (op3 !== void 0) {
13268
+ const args2 = expr.ops;
13269
+ if (args2 === null)
13270
+ return "";
13271
+ const result = [];
13272
+ for (const arg of args2)
13273
+ result.push(compile(arg, freeVars, op3[1]));
13274
+ return op3[1] < prec ? `(${result.join(op3[0])})` : result.join(op3[0]);
13275
+ }
13276
+ const fn = {
13277
+ Abs: "Math.abs",
13278
+ Arccos: "Math.acos",
13279
+ Arcosh: "Math.acosh",
13280
+ Arsin: "Math.asin",
13281
+ Arsinh: "Math.asinh",
13282
+ Arctan: "Math.atan",
13283
+ Artanh: "Math.atanh",
13284
+ // Math.cbrt
13285
+ Ceiling: "Math.ceil",
13286
+ Cos: "Math.cos",
13287
+ Cosh: "Math.cosh",
13288
+ Exp: "Math.exp",
13289
+ Floor: "Math.floor",
13290
+ // Math.hypot
13291
+ Ln: "Math.log",
13292
+ Log: "Math.log10",
13293
+ Lb: "Math.log2",
13294
+ Max: "Math.max",
13295
+ Min: "Math.min",
13296
+ Power: "Math.pow",
13297
+ Random: "Math.random",
13298
+ Round: "Math.round",
13299
+ Sgn: "Math.sign",
13300
+ Sin: "Math.sin",
13301
+ Sinh: "Math.sinh",
13302
+ Sqrt: "Math.sqrt",
13303
+ Tan: "Math.tan",
13304
+ Tanh: "Math.tanh"
13305
+ // Factorial: 'factorial', // TODO: implement
13306
+ // Hallucinated by Copilot, but interesting ideas...
13307
+ // Cot: 'Math.cot',
13308
+ // Sec: 'Math.sec',
13309
+ // Csc: 'Math.csc',
13310
+ // ArcCot: 'Math.acot',
13311
+ // ArcSec: 'Math.asec',
13312
+ // ArcCsc: 'Math.acsc',
13313
+ // Coth: 'Math.coth',
13314
+ // Sech: 'Math.sech',
13315
+ // Csch: 'Math.csch',
13316
+ // ArcCoth: 'Math.acoth',
13317
+ // ArcSech: 'Math.asech',
13318
+ // ArcCsch: 'Math.acsch',
13319
+ // Root: 'Math.root',
13320
+ // Gamma: 'Math.gamma',
13321
+ // Erf: 'Math.erf',
13322
+ // Erfc: 'Math.erfc',
13323
+ // Erfi: 'Math.erfi',
13324
+ // Zeta: 'Math.zeta',
13325
+ // PolyGamma: 'Math.polygamma',
13326
+ // HurwitzZeta: 'Math.hurwitzZeta',
13327
+ // DirichletEta: 'Math.dirichletEta',
13328
+ // Beta: 'Math.beta',
13329
+ // Binomial: 'Math.binomial',
13330
+ // Mod: 'Math.mod',
13331
+ // Quotient: 'Math.quotient',
13332
+ // GCD: 'Math.gcd',
13333
+ // LCM: 'Math.lcm',
13334
+ // Divisors: 'Math.divisors',
13335
+ // PrimeQ: 'Math.isPrime',
13336
+ // PrimePi: 'Math.primePi',
13337
+ // Prime: 'Math.prime',
13338
+ // NextPrime: 'Math.nextPrime',
13339
+ // PreviousPrime: 'Math.prevPrime',
13340
+ // PrimePowerQ: 'Math.isPrimePower',
13341
+ // PrimePowerPi: 'Math.primePowerPi',
13342
+ // PrimePower: 'Math.primePower',
13343
+ // NextPrimePower: 'Math.nextPrimePower',
13344
+ // PreviousPrimePower: 'Math.prevPrimePower',
13345
+ // PrimeFactors: 'Math.primeFactors',
13346
+ // DivisorSigma: 'Math.divisorSigma',
13347
+ // DivisorSigma0: 'Math.divisorSigma0',
13348
+ // DivisorSigma1: 'Math.divisorSigma1',
13349
+ // DivisorSigma2: 'Math.divisorSigma2',
13350
+ // DivisorSigma3: 'Math.divisorSigma3',
13351
+ // DivisorSigma4: 'Math.divisorSigma4',
13352
+ // DivisorCount: 'Math.divisorCount',
13353
+ // DivisorSum: 'Math.divisorSum',
13354
+ // MoebiusMu: 'Math.moebiusMu',
13355
+ // LiouvilleLambda: 'Math.liouvilleLambda',
13356
+ // CarmichaelLambda: 'Math.carmichaelLambda',
13357
+ // EulerPhi: 'Math.eulerPhi',
13358
+ // EulerPsi: 'Math.eulerPsi',
13359
+ // EulerGamma: 'Math.eulerGamma',
13360
+ // HarmonicNumber: 'Math.harmonicNumber',
13361
+ // BernoulliB: 'Math.bernoulliB',
13362
+ // StirlingS1: 'Math.stirlingS1',
13363
+ // StirlingS2: 'Math.stirlingS2',
13364
+ // BellB: 'Math.bellB',
13365
+ // BellNumber: 'Math.bellNumber',
13366
+ // LahS: 'Math.lahS',
13367
+ // LahL: 'Math.lahL',
13368
+ // RiemannR: 'Math.riemannR',
13369
+ // RiemannZeta: 'Math.riemannZeta',
13370
+ // RiemannXi: 'Math.riemannXi',
13371
+ // RiemannH: 'Math.riemannH',
13372
+ // RiemannZ: 'Math.riemannZ',
13373
+ // RiemannS: 'Math.riemannS',
13374
+ // RiemannXiZero: 'Math.riemannXiZero',
13375
+ // RiemannZetaZero: 'Math.riemannZetaZero',
13376
+ // RiemannHZero: 'Math.riemannHZero',
13377
+ // RiemannSZero: 'Math.riemannSZero',
13378
+ // RiemannPrimeCount: 'Math.riemannPrimeCount',
13379
+ // RiemannRLog: 'Math.riemannRLog',
13380
+ // RiemannRLogDerivative: 'Math.riemannRLogDerivative',
13381
+ // RiemannRLogZero: 'Math.riemannRLogZero',
13382
+ // RiemannRLogZeroDerivative: 'Math.riemannRLogZeroDerivative',
13383
+ // RiemannRZero: 'Math.riemannRZero',
13384
+ // RiemannRDerivative: 'Math.riemannRDerivative',
13385
+ // RiemannXiZeroDerivative: 'Math.riemannXiZeroDerivative',
13386
+ // RiemannZetaZeroDerivative: 'Math.riemannZetaZeroDerivative',
13387
+ // RiemannHZeroDerivative: 'Math.riemannHZeroDerivative',
13388
+ // RiemannSZeroDerivative: 'Math.riemannSZeroDerivative',
13389
+ // RiemannSZeroDerivative2: 'Math.riemannSZeroDerivative2',
13390
+ // RiemannSZeroDerivative3: 'Math.riemannSZeroDerivative3',
13391
+ // RiemannSZeroDerivative4: 'Math.riemannSZeroDerivative4',
13392
+ // RiemannSZeroDerivative5: 'Math.riemannSZeroDerivative5',
13393
+ // RiemannSZeroDerivative6: 'Math.riemannSZeroDerivative6',
13394
+ }[h] ?? h;
13395
+ const args = expr.ops;
13396
+ if (args !== null) {
13397
+ const result = [];
13398
+ for (const arg of args)
13399
+ result.push(compile(arg, freeVars, 0));
13400
+ return `${fn}(${result.join(", ")})`;
13401
+ }
13402
+ }
13403
+ return "";
13404
+ }
13405
+ function compileLoop(expr, op3) {
13406
+ const args = expr.ops;
13407
+ if (args === null)
13408
+ return "NaN";
13409
+ if (!expr.op1 || !expr.op2)
13410
+ return "NaN";
13411
+ const [index, lower, upper, isFinite2] = normalizeLimits(expr.op2);
13412
+ const fn = compile(expr.op1, [...expr.op1.freeVars, index], 0);
13413
+ return `((_) => {
13414
+ let acc = ${op3 === "+" ? "0" : "1"};
13415
+ const fn = (_) => ${fn};
13416
+ for (let i = ${lower}; i < ${upper}; i++)
13417
+ acc ${op3}= fn({ ..._, ${index}: i });
13418
+ return acc;
13419
+ })()`;
13420
+ }
13421
+
13422
+ // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13165
13423
  var AbstractBoxedExpression = class {
13166
13424
  constructor(ce, metadata) {
13167
13425
  this.engine = ce;
@@ -13486,6 +13744,9 @@ var ComputeEngine = (() => {
13486
13744
  N(_options) {
13487
13745
  return this.evaluate();
13488
13746
  }
13747
+ compile() {
13748
+ return compileToJavascript(this);
13749
+ }
13489
13750
  };
13490
13751
 
13491
13752
  // src/compute-engine/boxed-expression/serialize.ts
@@ -14948,7 +15209,7 @@ var ComputeEngine = (() => {
14948
15209
  }
14949
15210
  }
14950
15211
  return xs.map(
14951
- (op3) => !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
15212
+ (op3) => op3 && !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
14952
15213
  );
14953
15214
  }
14954
15215
  function validateSignature(sig, ops2, codomain) {
@@ -15656,7 +15917,6 @@ var ComputeEngine = (() => {
15656
15917
  // ],
15657
15918
  ];
15658
15919
  function findUnivariateRoots(expr, x) {
15659
- /* @__PURE__ */ console.log("findUnivariateRoots", expr.toString(), x);
15660
15920
  const ce = expr.engine;
15661
15921
  if (expr.head === "Equal") {
15662
15922
  expr = ce.add([expr.op1.canonical, ce.neg(expr.op2.canonical)]).simplify();
@@ -19592,6 +19852,18 @@ var ComputeEngine = (() => {
19592
19852
  level ?? (level = 1);
19593
19853
  if (level === 1) {
19594
19854
  const h = oneOf([
19855
+ [
19856
+ "Sqrt",
19857
+ [
19858
+ "Multiply",
19859
+ 6,
19860
+ [
19861
+ "Sum",
19862
+ ["Divide", 1, ["Power", "n", 2]],
19863
+ ["Triple", ["Hold", "n"], 1, { num: "+Infinity" }]
19864
+ ]
19865
+ ]
19866
+ ],
19595
19867
  "Add",
19596
19868
  "Add",
19597
19869
  "Add",
@@ -19610,13 +19882,16 @@ var ComputeEngine = (() => {
19610
19882
  "Negate",
19611
19883
  "trig"
19612
19884
  ]);
19613
- return randomExpressionWithHead(h, 1);
19885
+ if (typeof h === "string")
19886
+ return randomExpressionWithHead(h, 1);
19887
+ return h;
19614
19888
  }
19615
19889
  if (level === 2) {
19616
- if (Math.random() > 0.5)
19617
- return randomExpression(3);
19618
- if (Math.random() > 0.75)
19890
+ const r = Math.random();
19891
+ if (r > 0.75)
19619
19892
  return randomExpression(1);
19893
+ if (r > 0.5)
19894
+ return randomExpression(3);
19620
19895
  const h = oneOf([
19621
19896
  "Multiply",
19622
19897
  "Multiply",
@@ -19670,7 +19945,7 @@ var ComputeEngine = (() => {
19670
19945
  ["Rational", 12, 15],
19671
19946
  ["Rational", 15, 12],
19672
19947
  "ExponentialE",
19673
- "ImaginaryUnit",
19948
+ // 'ImaginaryUnit',
19674
19949
  ["Sqrt", 3],
19675
19950
  ["Sqrt", 5],
19676
19951
  ["Sqrt", 15],
@@ -19688,8 +19963,8 @@ var ComputeEngine = (() => {
19688
19963
  ["Power", "x", 4],
19689
19964
  ["Subtract", "x", 1],
19690
19965
  ["Add", "x", 1],
19691
- "a",
19692
- "b",
19966
+ // 'a',
19967
+ // 'b',
19693
19968
  "Pi"
19694
19969
  ]);
19695
19970
  }
@@ -23735,6 +24010,12 @@ ${JSON.stringify(entry)}`
23735
24010
  static getStandardLibrary(categories = "all") {
23736
24011
  return getStandardLibrary(categories);
23737
24012
  }
24013
+ get latexDictionary() {
24014
+ return this.latexSyntax.dictionary;
24015
+ }
24016
+ set latexDictionary(dic) {
24017
+ this.latexSyntax.dictionary = dic;
24018
+ }
23738
24019
  /** After the configuration of the engine has changed, clear the caches
23739
24020
  * so that new values can be recalculated.
23740
24021
  *
@@ -23929,6 +24210,10 @@ ${JSON.stringify(entry)}`
23929
24210
  return new this._bignum(a);
23930
24211
  }
23931
24212
  complex(a, b) {
24213
+ if (a instanceof Decimal)
24214
+ a = a.toNumber();
24215
+ if (b instanceof Decimal)
24216
+ b = b.toNumber();
23932
24217
  return new import_complex19.Complex(a, b);
23933
24218
  }
23934
24219
  isBignum(a) {
@@ -24624,10 +24909,10 @@ ${JSON.stringify(entry)}`
24624
24909
  };
24625
24910
 
24626
24911
  // src/compute-engine.ts
24627
- var version = "0.12.6";
24912
+ var version = "0.13.0";
24628
24913
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
24629
24914
  ComputeEngine: ComputeEngine.prototype.constructor,
24630
- version: "0.12.6"
24915
+ version: "0.13.0"
24631
24916
  };
24632
24917
  return __toCommonJS(compute_engine_exports);
24633
24918
  })();
@@ -1,4 +1,4 @@
1
- /** CortexJS Compute Engine 0.12.6 */
1
+ /** CortexJS Compute Engine 0.13.0 */
2
2
 
3
3
  // src/math-json/utils.ts
4
4
  function isSymbolObject(expr) {
@@ -239,7 +239,7 @@ function mapArgs(expr, fn) {
239
239
  }
240
240
 
241
241
  // src/math-json.ts
242
- var version = "0.12.6";
242
+ var version = "0.13.0";
243
243
  export {
244
244
  applyRecursively,
245
245
  dictionary as getDictionary,
package/dist/math-json.js CHANGED
@@ -1,4 +1,4 @@
1
- /** CortexJS MathJSON 0.12.6 */
1
+ /** CortexJS MathJSON 0.13.0 */
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.MathJson = {}));})(this, (function (exports) { 'use strict';
3
3
  var MathJson = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -277,7 +277,7 @@ var MathJson = (() => {
277
277
  }
278
278
 
279
279
  // src/math-json.ts
280
- var version = "0.12.6";
280
+ var version = "0.13.0";
281
281
  return __toCommonJS(math_json_exports);
282
282
  })();
283
283
  Object.assign(exports, MathJson); Object.defineProperty(exports, '__esModule', { value: true });}));