@cortex-js/compute-engine 0.12.7 → 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 +341 -70
  2. package/dist/compute-engine.js +341 -70
  3. package/dist/compute-engine.min.esm.js +341 -70
  4. package/dist/compute-engine.min.js +341 -70
  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 +2 -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 +1 -1
  44. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -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 +1 -1
  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.7 */
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
  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
+ 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
@@ -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
  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) {
@@ -13171,6 +13181,246 @@ var ComputeEngine = (() => {
13171
13181
 
13172
13182
  // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13173
13183
  var import_complex6 = __toESM(require_complex());
13184
+
13185
+ // src/compute-engine/compile.ts
13186
+ function compileToJavascript(expr) {
13187
+ const js = compile(expr, expr.freeVars);
13188
+ try {
13189
+ return new Function("_", `return ${js}`);
13190
+ } catch (e) {
13191
+ console.error(`${e}
13192
+ ${expr.latex}
13193
+ ${js}`);
13194
+ }
13195
+ return void 0;
13196
+ }
13197
+ function compile(expr, freeVars = [], prec = 0) {
13198
+ const f = asFloat(expr);
13199
+ if (f !== null)
13200
+ return f.toString();
13201
+ const s = expr.symbol;
13202
+ if (s !== null) {
13203
+ const result = {
13204
+ True: "true",
13205
+ False: "false",
13206
+ Pi: "Math.PI",
13207
+ ExponentialE: "Math.E",
13208
+ I: "Math.I",
13209
+ NaN: "Number.NaN",
13210
+ ImaginaryUnit: "NaN"
13211
+ }[s];
13212
+ if (result !== void 0)
13213
+ return result;
13214
+ if (freeVars.includes(s))
13215
+ return `_.${s}`;
13216
+ return s;
13217
+ }
13218
+ const str = expr.string;
13219
+ if (str !== null)
13220
+ return JSON.stringify(str);
13221
+ const keys = expr.keys;
13222
+ if (keys !== null) {
13223
+ const result = [];
13224
+ for (const key of keys) {
13225
+ const value = expr.getKey(key);
13226
+ if (value)
13227
+ result.push(`${key}: ${compile(value, freeVars, 0)}`);
13228
+ }
13229
+ return `{${result.join(", ")}}`;
13230
+ }
13231
+ const h = expr.head;
13232
+ if (typeof h === "string") {
13233
+ if (h === "Negate") {
13234
+ const arg = expr.op1;
13235
+ if (arg === null)
13236
+ return "";
13237
+ return `-${compile(arg, freeVars, 3)}`;
13238
+ }
13239
+ if (h === "Error") {
13240
+ return "NaN";
13241
+ }
13242
+ if (h === "Sum")
13243
+ return compileLoop(expr, "+");
13244
+ if (h === "Product")
13245
+ return compileLoop(expr, "*");
13246
+ if (h === "Root") {
13247
+ const arg = expr.op1;
13248
+ if (arg === null)
13249
+ return "";
13250
+ const exp2 = expr.op2;
13251
+ if (exp2 === null)
13252
+ return `Math.sqrt(${compile(arg, freeVars, 0)})`;
13253
+ return `Math.pow(${compile(arg, freeVars)}, 1/${compile(exp2, freeVars)}`;
13254
+ }
13255
+ if (h === "Factorial") {
13256
+ const arg = expr.op1;
13257
+ if (arg === null)
13258
+ return "";
13259
+ return `${compile(arg, freeVars, 3)}!`;
13260
+ }
13261
+ const OPS = {
13262
+ Add: ["+", 1],
13263
+ Subtract: ["-", 1],
13264
+ Multiply: ["*", 2],
13265
+ Divide: ["/", 3]
13266
+ };
13267
+ const op3 = OPS[h];
13268
+ if (op3 !== void 0) {
13269
+ const args2 = expr.ops;
13270
+ if (args2 === null)
13271
+ return "";
13272
+ const result = [];
13273
+ for (const arg of args2)
13274
+ result.push(compile(arg, freeVars, op3[1]));
13275
+ return op3[1] < prec ? `(${result.join(op3[0])})` : result.join(op3[0]);
13276
+ }
13277
+ const fn = {
13278
+ Abs: "Math.abs",
13279
+ Arccos: "Math.acos",
13280
+ Arcosh: "Math.acosh",
13281
+ Arsin: "Math.asin",
13282
+ Arsinh: "Math.asinh",
13283
+ Arctan: "Math.atan",
13284
+ Artanh: "Math.atanh",
13285
+ // Math.cbrt
13286
+ Ceiling: "Math.ceil",
13287
+ Cos: "Math.cos",
13288
+ Cosh: "Math.cosh",
13289
+ Exp: "Math.exp",
13290
+ Floor: "Math.floor",
13291
+ // Math.hypot
13292
+ Ln: "Math.log",
13293
+ Log: "Math.log10",
13294
+ Lb: "Math.log2",
13295
+ Max: "Math.max",
13296
+ Min: "Math.min",
13297
+ Power: "Math.pow",
13298
+ Random: "Math.random",
13299
+ Round: "Math.round",
13300
+ Sgn: "Math.sign",
13301
+ Sin: "Math.sin",
13302
+ Sinh: "Math.sinh",
13303
+ Sqrt: "Math.sqrt",
13304
+ Tan: "Math.tan",
13305
+ Tanh: "Math.tanh"
13306
+ // Factorial: 'factorial', // TODO: implement
13307
+ // Hallucinated by Copilot, but interesting ideas...
13308
+ // Cot: 'Math.cot',
13309
+ // Sec: 'Math.sec',
13310
+ // Csc: 'Math.csc',
13311
+ // ArcCot: 'Math.acot',
13312
+ // ArcSec: 'Math.asec',
13313
+ // ArcCsc: 'Math.acsc',
13314
+ // Coth: 'Math.coth',
13315
+ // Sech: 'Math.sech',
13316
+ // Csch: 'Math.csch',
13317
+ // ArcCoth: 'Math.acoth',
13318
+ // ArcSech: 'Math.asech',
13319
+ // ArcCsch: 'Math.acsch',
13320
+ // Root: 'Math.root',
13321
+ // Gamma: 'Math.gamma',
13322
+ // Erf: 'Math.erf',
13323
+ // Erfc: 'Math.erfc',
13324
+ // Erfi: 'Math.erfi',
13325
+ // Zeta: 'Math.zeta',
13326
+ // PolyGamma: 'Math.polygamma',
13327
+ // HurwitzZeta: 'Math.hurwitzZeta',
13328
+ // DirichletEta: 'Math.dirichletEta',
13329
+ // Beta: 'Math.beta',
13330
+ // Binomial: 'Math.binomial',
13331
+ // Mod: 'Math.mod',
13332
+ // Quotient: 'Math.quotient',
13333
+ // GCD: 'Math.gcd',
13334
+ // LCM: 'Math.lcm',
13335
+ // Divisors: 'Math.divisors',
13336
+ // PrimeQ: 'Math.isPrime',
13337
+ // PrimePi: 'Math.primePi',
13338
+ // Prime: 'Math.prime',
13339
+ // NextPrime: 'Math.nextPrime',
13340
+ // PreviousPrime: 'Math.prevPrime',
13341
+ // PrimePowerQ: 'Math.isPrimePower',
13342
+ // PrimePowerPi: 'Math.primePowerPi',
13343
+ // PrimePower: 'Math.primePower',
13344
+ // NextPrimePower: 'Math.nextPrimePower',
13345
+ // PreviousPrimePower: 'Math.prevPrimePower',
13346
+ // PrimeFactors: 'Math.primeFactors',
13347
+ // DivisorSigma: 'Math.divisorSigma',
13348
+ // DivisorSigma0: 'Math.divisorSigma0',
13349
+ // DivisorSigma1: 'Math.divisorSigma1',
13350
+ // DivisorSigma2: 'Math.divisorSigma2',
13351
+ // DivisorSigma3: 'Math.divisorSigma3',
13352
+ // DivisorSigma4: 'Math.divisorSigma4',
13353
+ // DivisorCount: 'Math.divisorCount',
13354
+ // DivisorSum: 'Math.divisorSum',
13355
+ // MoebiusMu: 'Math.moebiusMu',
13356
+ // LiouvilleLambda: 'Math.liouvilleLambda',
13357
+ // CarmichaelLambda: 'Math.carmichaelLambda',
13358
+ // EulerPhi: 'Math.eulerPhi',
13359
+ // EulerPsi: 'Math.eulerPsi',
13360
+ // EulerGamma: 'Math.eulerGamma',
13361
+ // HarmonicNumber: 'Math.harmonicNumber',
13362
+ // BernoulliB: 'Math.bernoulliB',
13363
+ // StirlingS1: 'Math.stirlingS1',
13364
+ // StirlingS2: 'Math.stirlingS2',
13365
+ // BellB: 'Math.bellB',
13366
+ // BellNumber: 'Math.bellNumber',
13367
+ // LahS: 'Math.lahS',
13368
+ // LahL: 'Math.lahL',
13369
+ // RiemannR: 'Math.riemannR',
13370
+ // RiemannZeta: 'Math.riemannZeta',
13371
+ // RiemannXi: 'Math.riemannXi',
13372
+ // RiemannH: 'Math.riemannH',
13373
+ // RiemannZ: 'Math.riemannZ',
13374
+ // RiemannS: 'Math.riemannS',
13375
+ // RiemannXiZero: 'Math.riemannXiZero',
13376
+ // RiemannZetaZero: 'Math.riemannZetaZero',
13377
+ // RiemannHZero: 'Math.riemannHZero',
13378
+ // RiemannSZero: 'Math.riemannSZero',
13379
+ // RiemannPrimeCount: 'Math.riemannPrimeCount',
13380
+ // RiemannRLog: 'Math.riemannRLog',
13381
+ // RiemannRLogDerivative: 'Math.riemannRLogDerivative',
13382
+ // RiemannRLogZero: 'Math.riemannRLogZero',
13383
+ // RiemannRLogZeroDerivative: 'Math.riemannRLogZeroDerivative',
13384
+ // RiemannRZero: 'Math.riemannRZero',
13385
+ // RiemannRDerivative: 'Math.riemannRDerivative',
13386
+ // RiemannXiZeroDerivative: 'Math.riemannXiZeroDerivative',
13387
+ // RiemannZetaZeroDerivative: 'Math.riemannZetaZeroDerivative',
13388
+ // RiemannHZeroDerivative: 'Math.riemannHZeroDerivative',
13389
+ // RiemannSZeroDerivative: 'Math.riemannSZeroDerivative',
13390
+ // RiemannSZeroDerivative2: 'Math.riemannSZeroDerivative2',
13391
+ // RiemannSZeroDerivative3: 'Math.riemannSZeroDerivative3',
13392
+ // RiemannSZeroDerivative4: 'Math.riemannSZeroDerivative4',
13393
+ // RiemannSZeroDerivative5: 'Math.riemannSZeroDerivative5',
13394
+ // RiemannSZeroDerivative6: 'Math.riemannSZeroDerivative6',
13395
+ }[h] ?? h;
13396
+ const args = expr.ops;
13397
+ if (args !== null) {
13398
+ const result = [];
13399
+ for (const arg of args)
13400
+ result.push(compile(arg, freeVars, 0));
13401
+ return `${fn}(${result.join(", ")})`;
13402
+ }
13403
+ }
13404
+ return "";
13405
+ }
13406
+ function compileLoop(expr, op3) {
13407
+ const args = expr.ops;
13408
+ if (args === null)
13409
+ return "NaN";
13410
+ if (!expr.op1 || !expr.op2)
13411
+ return "NaN";
13412
+ const [index, lower, upper, isFinite2] = normalizeLimits(expr.op2);
13413
+ const fn = compile(expr.op1, [...expr.op1.freeVars, index], 0);
13414
+ return `((_) => {
13415
+ let acc = ${op3 === "+" ? "0" : "1"};
13416
+ const fn = (_) => ${fn};
13417
+ for (let i = ${lower}; i < ${upper}; i++)
13418
+ acc ${op3}= fn({ ..._, ${index}: i });
13419
+ return acc;
13420
+ })()`;
13421
+ }
13422
+
13423
+ // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13174
13424
  var AbstractBoxedExpression = class {
13175
13425
  constructor(ce, metadata) {
13176
13426
  this.engine = ce;
@@ -13495,6 +13745,9 @@ var ComputeEngine = (() => {
13495
13745
  N(_options) {
13496
13746
  return this.evaluate();
13497
13747
  }
13748
+ compile() {
13749
+ return compileToJavascript(this);
13750
+ }
13498
13751
  };
13499
13752
 
13500
13753
  // src/compute-engine/boxed-expression/serialize.ts
@@ -14957,7 +15210,7 @@ var ComputeEngine = (() => {
14957
15210
  }
14958
15211
  }
14959
15212
  return xs.map(
14960
- (op3) => !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
15213
+ (op3) => op3 && !op3.isValid || op3.isNumber ? op3 : ce.error(["incompatible-domain", "Number", op3.domain], op3)
14961
15214
  );
14962
15215
  }
14963
15216
  function validateSignature(sig, ops2, codomain) {
@@ -15665,7 +15918,6 @@ var ComputeEngine = (() => {
15665
15918
  // ],
15666
15919
  ];
15667
15920
  function findUnivariateRoots(expr, x) {
15668
- console.log("findUnivariateRoots", expr.toString(), x);
15669
15921
  const ce = expr.engine;
15670
15922
  if (expr.head === "Equal") {
15671
15923
  expr = ce.add([expr.op1.canonical, ce.neg(expr.op2.canonical)]).simplify();
@@ -19604,6 +19856,18 @@ var ComputeEngine = (() => {
19604
19856
  level ?? (level = 1);
19605
19857
  if (level === 1) {
19606
19858
  const h = oneOf([
19859
+ [
19860
+ "Sqrt",
19861
+ [
19862
+ "Multiply",
19863
+ 6,
19864
+ [
19865
+ "Sum",
19866
+ ["Divide", 1, ["Power", "n", 2]],
19867
+ ["Triple", ["Hold", "n"], 1, { num: "+Infinity" }]
19868
+ ]
19869
+ ]
19870
+ ],
19607
19871
  "Add",
19608
19872
  "Add",
19609
19873
  "Add",
@@ -19622,13 +19886,16 @@ var ComputeEngine = (() => {
19622
19886
  "Negate",
19623
19887
  "trig"
19624
19888
  ]);
19625
- return randomExpressionWithHead(h, 1);
19889
+ if (typeof h === "string")
19890
+ return randomExpressionWithHead(h, 1);
19891
+ return h;
19626
19892
  }
19627
19893
  if (level === 2) {
19628
- if (Math.random() > 0.5)
19629
- return randomExpression(3);
19630
- if (Math.random() > 0.75)
19894
+ const r = Math.random();
19895
+ if (r > 0.75)
19631
19896
  return randomExpression(1);
19897
+ if (r > 0.5)
19898
+ return randomExpression(3);
19632
19899
  const h = oneOf([
19633
19900
  "Multiply",
19634
19901
  "Multiply",
@@ -19682,7 +19949,7 @@ var ComputeEngine = (() => {
19682
19949
  ["Rational", 12, 15],
19683
19950
  ["Rational", 15, 12],
19684
19951
  "ExponentialE",
19685
- "ImaginaryUnit",
19952
+ // 'ImaginaryUnit',
19686
19953
  ["Sqrt", 3],
19687
19954
  ["Sqrt", 5],
19688
19955
  ["Sqrt", 15],
@@ -19700,8 +19967,8 @@ var ComputeEngine = (() => {
19700
19967
  ["Power", "x", 4],
19701
19968
  ["Subtract", "x", 1],
19702
19969
  ["Add", "x", 1],
19703
- "a",
19704
- "b",
19970
+ // 'a',
19971
+ // 'b',
19705
19972
  "Pi"
19706
19973
  ]);
19707
19974
  }
@@ -23947,6 +24214,10 @@ ${JSON.stringify(entry)}`
23947
24214
  return new this._bignum(a);
23948
24215
  }
23949
24216
  complex(a, b) {
24217
+ if (a instanceof Decimal)
24218
+ a = a.toNumber();
24219
+ if (b instanceof Decimal)
24220
+ b = b.toNumber();
23950
24221
  return new import_complex19.Complex(a, b);
23951
24222
  }
23952
24223
  isBignum(a) {
@@ -24642,10 +24913,10 @@ ${JSON.stringify(entry)}`
24642
24913
  };
24643
24914
 
24644
24915
  // src/compute-engine.ts
24645
- var version = "0.12.7";
24916
+ var version = "0.13.0";
24646
24917
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
24647
24918
  ComputeEngine: ComputeEngine.prototype.constructor,
24648
- version: "0.12.7"
24919
+ version: "0.13.0"
24649
24920
  };
24650
24921
  return __toCommonJS(compute_engine_exports);
24651
24922
  })();