@cortex-js/compute-engine 0.14.0 → 0.15.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 (94) hide show
  1. package/dist/compute-engine.esm.js +730 -131
  2. package/dist/compute-engine.js +730 -131
  3. package/dist/compute-engine.min.esm.js +730 -131
  4. package/dist/compute-engine.min.js +730 -131
  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 +1 -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 +1 -1
  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-complex.d.ts +2 -0
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +2 -0
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +4 -2
  47. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/parse.d.ts +37 -1
  49. package/dist/types/compute-engine/latex-syntax/public.d.ts +6 -2
  50. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  52. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +5 -2
  53. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  54. package/dist/types/compute-engine/library/arithmetic-add.d.ts +1 -1
  55. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  56. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +1 -1
  57. package/dist/types/compute-engine/library/arithmetic-power.d.ts +1 -1
  58. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  59. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  60. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  61. package/dist/types/compute-engine/library/complex.d.ts +2 -0
  62. package/dist/types/compute-engine/library/core.d.ts +1 -1
  63. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  64. package/dist/types/compute-engine/library/library.d.ts +1 -1
  65. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  66. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  67. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  68. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  69. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  70. package/dist/types/compute-engine/library/statistics.d.ts +2 -0
  71. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  72. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  73. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  74. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  75. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  76. package/dist/types/compute-engine/numerics/numeric.d.ts +2 -1
  77. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  78. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  79. package/dist/types/compute-engine/public.d.ts +1 -1
  80. package/dist/types/compute-engine/rules.d.ts +1 -1
  81. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  82. package/dist/types/compute-engine/solve.d.ts +1 -1
  83. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  84. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  85. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  86. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  87. package/dist/types/compute-engine/symbolic/product.d.ts +1 -1
  88. package/dist/types/compute-engine/symbolic/sum.d.ts +1 -1
  89. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  90. package/dist/types/compute-engine.d.ts +2 -2
  91. package/dist/types/math-json/math-json-format.d.ts +1 -1
  92. package/dist/types/math-json/utils.d.ts +2 -1
  93. package/dist/types/math-json.d.ts +2 -2
  94. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- /** CortexJS Compute Engine 0.14.0 */
1
+ /** CortexJS Compute Engine 0.15.0 */
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -3405,7 +3405,7 @@ PI = new Decimal(PI);
3405
3405
  var decimal_default = Decimal;
3406
3406
 
3407
3407
  // src/compute-engine/compute-engine.ts
3408
- var import_complex19 = __toESM(require_complex());
3408
+ var import_complex20 = __toESM(require_complex());
3409
3409
 
3410
3410
  // src/common/grapheme-splitter.ts
3411
3411
  function stringToCodepoints(string) {
@@ -4269,7 +4269,7 @@ function serializeAdd(serializer, expr) {
4269
4269
  if (name === "Negate") {
4270
4270
  result = "-" + serializer.wrap(arg, 276);
4271
4271
  } else if (name === "Add") {
4272
- if (nops(expr) === 2 && serializer.options.invisiblePlus !== "+") {
4272
+ if (serializer.canonical && nops(expr) === 2 && serializer.options.invisiblePlus !== "+") {
4273
4273
  const [op12, op22] = [op(expr, 1), op(expr, 2)];
4274
4274
  let [lhs, rhs] = [op12, op22];
4275
4275
  let lhsValue = machineValue(lhs);
@@ -4330,21 +4330,23 @@ function serializeMultiply(serializer, expr) {
4330
4330
  return "";
4331
4331
  serializer.level -= 1;
4332
4332
  let result = "";
4333
- const [numer, denom] = numeratorDenominator(expr);
4334
- if (denom.length > 0) {
4335
- if (denom.length === 1 && denom[0] === 1) {
4336
- if (numer.length === 0)
4337
- result = "1";
4338
- else if (numer.length === 1)
4339
- result = serializer.serialize(numer[0]);
4340
- else
4341
- result = serializeMultiply(serializer, ["Multiply", ...numer]);
4342
- } else {
4343
- result = serializer.serialize([
4344
- "Divide",
4345
- numer.length === 1 ? numer[0] : ["Multiply", ...numer],
4346
- denom.length === 1 ? denom[0] : ["Multiply", ...denom]
4347
- ]);
4333
+ if (serializer.canonical === true) {
4334
+ const [numer, denom] = numeratorDenominator(expr);
4335
+ if (denom.length > 0) {
4336
+ if (denom.length === 1 && denom[0] === 1) {
4337
+ if (numer.length === 0)
4338
+ result = "1";
4339
+ else if (numer.length === 1)
4340
+ result = serializer.serialize(numer[0]);
4341
+ else
4342
+ result = serializeMultiply(serializer, ["Multiply", ...numer]);
4343
+ } else {
4344
+ result = serializer.serialize([
4345
+ "Divide",
4346
+ numer.length === 1 ? numer[0] : ["Multiply", ...numer],
4347
+ denom.length === 1 ? denom[0] : ["Multiply", ...denom]
4348
+ ]);
4349
+ }
4348
4350
  }
4349
4351
  }
4350
4352
  if (result) {
@@ -4465,7 +4467,7 @@ function serializeFraction(serializer, expr) {
4465
4467
  return "";
4466
4468
  const numer = missingIfEmpty(op(expr, 1));
4467
4469
  const denom = missingIfEmpty(op(expr, 2));
4468
- const style = getFractionStyle(expr, serializer.level);
4470
+ const style = serializer.canonical ? getFractionStyle(expr, serializer.level) : "quotient";
4469
4471
  if (style === "inline-solidus" || style === "nice-solidus") {
4470
4472
  const numerStr = serializer.wrapShort(numer);
4471
4473
  const denomStr = serializer.wrapShort(denom);
@@ -4504,25 +4506,27 @@ function serializePower(serializer, expr) {
4504
4506
  base,
4505
4507
  exp2
4506
4508
  );
4507
- const val2 = machineValue(exp2) ?? 1;
4508
- if (val2 === -1) {
4509
- return serializer.serialize(["Divide", "1", base]);
4510
- } else if (val2 < 0) {
4511
- return serializer.serialize(["Divide", "1", ["Power", base, -val2]]);
4512
- } else if (head(exp2) === "Divide" || head(exp2) === "Rational") {
4513
- if (machineValue(op(exp2, 1)) === 1) {
4514
- const style = getRootStyle(expr, serializer.level);
4515
- return serializeRoot(serializer, style, base, op(exp2, 2));
4516
- }
4517
- if (machineValue(op(exp2, 2)) === 2) {
4518
- return `${serializer.serialize(["Sqrt", base])}^{${serializer.serialize(
4519
- op(exp2, 1)
4520
- )}}`;
4521
- }
4522
- } else if (head(exp2) === "Power") {
4523
- if (machineValue(op(exp2, 2)) === -1) {
4524
- const style = getRootStyle(expr, serializer.level);
4525
- return serializeRoot(serializer, style, base, op(exp2, 1));
4509
+ if (serializer.canonical) {
4510
+ const val2 = machineValue(exp2) ?? 1;
4511
+ if (val2 === -1) {
4512
+ return serializer.serialize(["Divide", "1", base]);
4513
+ } else if (val2 < 0) {
4514
+ return serializer.serialize(["Divide", "1", ["Power", base, -val2]]);
4515
+ } else if (head(exp2) === "Divide" || head(exp2) === "Rational") {
4516
+ if (machineValue(op(exp2, 1)) === 1) {
4517
+ const style = getRootStyle(expr, serializer.level);
4518
+ return serializeRoot(serializer, style, base, op(exp2, 2));
4519
+ }
4520
+ if (machineValue(op(exp2, 2)) === 2) {
4521
+ return `${serializer.serialize(["Sqrt", base])}^{${serializer.serialize(
4522
+ op(exp2, 1)
4523
+ )}}`;
4524
+ }
4525
+ } else if (head(exp2) === "Power") {
4526
+ if (machineValue(op(exp2, 2)) === -1) {
4527
+ const style = getRootStyle(expr, serializer.level);
4528
+ return serializeRoot(serializer, style, base, op(exp2, 1));
4529
+ }
4526
4530
  }
4527
4531
  }
4528
4532
  return serializer.wrapShort(base) + "^{" + serializer.serialize(exp2) + "}";
@@ -5366,15 +5370,9 @@ var DEFINITIONS_CORE = [
5366
5370
  {
5367
5371
  name: "List",
5368
5372
  kind: "matchfix",
5369
- openTrigger: "[",
5370
- closeTrigger: "]",
5371
- parse: (_parser, body) => {
5372
- if (body === null || isEmptySequence(body))
5373
- return ["List"];
5374
- if (head(body) !== "Sequence" && head(body) !== "List")
5375
- return ["List", body];
5376
- return ["List", ...ops(body) ?? []];
5377
- },
5373
+ openTrigger: "\\lbrack",
5374
+ closeTrigger: "\\rbrack",
5375
+ parse: parseList,
5378
5376
  serialize: (serializer, expr) => {
5379
5377
  return joinLatex([
5380
5378
  "\\lbrack",
@@ -5383,20 +5381,31 @@ var DEFINITIONS_CORE = [
5383
5381
  ]);
5384
5382
  }
5385
5383
  },
5384
+ // Synonyms for List
5385
+ {
5386
+ kind: "matchfix",
5387
+ openTrigger: "[",
5388
+ closeTrigger: "]",
5389
+ parse: parseList
5390
+ },
5391
+ {
5392
+ kind: "matchfix",
5393
+ openTrigger: "\\[",
5394
+ closeTrigger: "\\]",
5395
+ parse: parseList
5396
+ },
5397
+ // Synonyms for Delimiter
5386
5398
  {
5387
5399
  kind: "matchfix",
5388
5400
  openTrigger: "(",
5389
5401
  closeTrigger: ")",
5390
- parse: (_parser, body) => {
5391
- if (body === null || isEmptySequence(body))
5392
- return ["Sequence"];
5393
- if (head(body) === "Sequence" || head(body) === "List") {
5394
- if (nops(body) === 0)
5395
- return ["Delimiter"];
5396
- return ["Delimiter", ["Sequence", ...ops(body) ?? []]];
5397
- }
5398
- return ["Delimiter", body];
5399
- }
5402
+ parse: parseDelimiter
5403
+ },
5404
+ {
5405
+ kind: "matchfix",
5406
+ openTrigger: "\\lparen",
5407
+ closeTrigger: "\\rparen",
5408
+ parse: parseDelimiter
5400
5409
  },
5401
5410
  {
5402
5411
  latexTrigger: [","],
@@ -5469,9 +5478,7 @@ var DEFINITIONS_CORE = [
5469
5478
  kind: "postfix",
5470
5479
  parse: (_parser, lhs) => ["Superstar", lhs]
5471
5480
  },
5472
- // @todo: when lhs is a complex number, 'Conjugate'
5473
- // { name: 'Conjugate', trigger: ['\\star'], kind: 'infix' },
5474
- { name: "Superstar", latexTrigger: ["^", "\\star"], kind: "postfix" },
5481
+ // { name: 'Superstar', latexTrigger: ['^', '\\star'], kind: 'postfix' },
5475
5482
  {
5476
5483
  latexTrigger: ["_", "*"],
5477
5484
  kind: "postfix",
@@ -5753,6 +5760,23 @@ function parsePrime(parser, lhs, order2) {
5753
5760
  return ["Prime", missingIfEmpty(lhs)];
5754
5761
  return ["Prime", missingIfEmpty(lhs), order2];
5755
5762
  }
5763
+ function parseDelimiter(parser, body) {
5764
+ if (body === null || isEmptySequence(body))
5765
+ return ["Sequence"];
5766
+ if (head(body) === "Sequence") {
5767
+ if (nops(body) === 0)
5768
+ return ["Delimiter"];
5769
+ return ["Delimiter", ["Sequence", ...ops(body) ?? []]];
5770
+ }
5771
+ return ["Delimiter", body];
5772
+ }
5773
+ function parseList(_parser, body) {
5774
+ if (body === null || isEmptySequence(body))
5775
+ return ["List"];
5776
+ if (head(body) !== "Sequence" && head(body) !== "List")
5777
+ return ["List", body];
5778
+ return ["List", ...ops(body) ?? []];
5779
+ }
5756
5780
 
5757
5781
  // src/compute-engine/latex-syntax/dictionary/definitions-inequalities.ts
5758
5782
  var DEFINITIONS_INEQUALITIES = [
@@ -7681,6 +7705,59 @@ var DEFINITIONS_SYMBOLS = [
7681
7705
  })
7682
7706
  ];
7683
7707
 
7708
+ // src/compute-engine/latex-syntax/dictionary/definitions-complex.ts
7709
+ var DEFINITIONS_COMPLEX = [
7710
+ {
7711
+ name: "Real",
7712
+ kind: "function",
7713
+ latexTrigger: ["\\Re"]
7714
+ },
7715
+ {
7716
+ name: "Imaginary",
7717
+ kind: "function",
7718
+ latexTrigger: ["\\Im"]
7719
+ },
7720
+ {
7721
+ name: "Argument",
7722
+ kind: "function",
7723
+ latexTrigger: ["\\arg"]
7724
+ },
7725
+ {
7726
+ name: "Conjugate",
7727
+ latexTrigger: ["^", "\\star"],
7728
+ kind: "postfix"
7729
+ }
7730
+ ];
7731
+
7732
+ // src/compute-engine/latex-syntax/dictionary/definitions-statistics.ts
7733
+ var DEFINITIONS_STATISTICS = [
7734
+ {
7735
+ name: "Mean",
7736
+ kind: "function",
7737
+ identifierTrigger: "mean"
7738
+ },
7739
+ {
7740
+ name: "Median",
7741
+ kind: "function",
7742
+ identifierTrigger: "median"
7743
+ },
7744
+ {
7745
+ name: "StandarDeviation",
7746
+ kind: "function",
7747
+ identifierTrigger: "stddev"
7748
+ },
7749
+ {
7750
+ latexTrigger: ["\\bar"],
7751
+ kind: "expression",
7752
+ parse: (parser, _until) => {
7753
+ const expr = parser.parseGroup() ?? parser.parseToken();
7754
+ if (!expr || !symbol(expr))
7755
+ return null;
7756
+ return ["Mean", expr];
7757
+ }
7758
+ }
7759
+ ];
7760
+
7684
7761
  // src/compute-engine/latex-syntax/dictionary/definitions.ts
7685
7762
  var DEFAULT_DELIMITER = {
7686
7763
  "(": "(",
@@ -7870,7 +7947,7 @@ function makeParseHandler(entry, latexTrigger, idTrigger) {
7870
7947
  return entry.parse;
7871
7948
  const kind = "kind" in entry ? entry.kind : "expression";
7872
7949
  if (kind === "environment") {
7873
- const envName = entry.parse ?? idTrigger ?? entry.name;
7950
+ const envName = entry.parse ?? entry.name ?? idTrigger;
7874
7951
  if (envName)
7875
7952
  return (parser, _until) => {
7876
7953
  const array = parser.parseTabular();
@@ -7880,7 +7957,7 @@ function makeParseHandler(entry, latexTrigger, idTrigger) {
7880
7957
  };
7881
7958
  }
7882
7959
  if (kind === "function") {
7883
- const fnName = entry.parse ?? idTrigger ?? entry.name;
7960
+ const fnName = entry.parse ?? entry.name ?? idTrigger;
7884
7961
  if (fnName)
7885
7962
  return (parser, until) => {
7886
7963
  const args = parser.parseArguments("enclosure", until);
@@ -7888,12 +7965,12 @@ function makeParseHandler(entry, latexTrigger, idTrigger) {
7888
7965
  };
7889
7966
  }
7890
7967
  if (kind === "symbol") {
7891
- const symName = entry.parse ?? idTrigger ?? entry.name;
7968
+ const symName = entry.parse ?? entry.name ?? idTrigger;
7892
7969
  if (symName)
7893
7970
  return (_parser, _terminator) => symName;
7894
7971
  }
7895
7972
  if (kind === "prefix") {
7896
- const h = entry.parse ?? idTrigger ?? entry.name;
7973
+ const h = entry.parse ?? entry.name ?? idTrigger;
7897
7974
  if (h) {
7898
7975
  const prec = entry["precedence"] ?? 1e4;
7899
7976
  return (parser, until) => {
@@ -7919,7 +7996,7 @@ function makeParseHandler(entry, latexTrigger, idTrigger) {
7919
7996
  missingIfEmpty(op(arg, 2))
7920
7997
  ];
7921
7998
  }
7922
- const h = entry.parse ?? idTrigger ?? entry.name;
7999
+ const h = entry.parse ?? entry.name ?? idTrigger;
7923
8000
  const prec = entry["precedence"] ?? 1e4;
7924
8001
  const associativity = entry["associativity"] ?? "non";
7925
8002
  if (h)
@@ -7944,7 +8021,7 @@ function makeParseHandler(entry, latexTrigger, idTrigger) {
7944
8021
  };
7945
8022
  }
7946
8023
  if (kind === "expression") {
7947
- const parseResult = entry.parse ?? idTrigger ?? entry.name;
8024
+ const parseResult = entry.parse ?? entry.name ?? idTrigger;
7948
8025
  if (parseResult)
7949
8026
  return () => parseResult;
7950
8027
  }
@@ -7965,6 +8042,16 @@ function isValidEntry(entry, onError) {
7965
8042
  }
7966
8043
  if (Array.isArray(subject))
7967
8044
  subject = tokensToString(subject);
8045
+ if ("trigger" in entry) {
8046
+ onError({
8047
+ severity: "warning",
8048
+ message: [
8049
+ "invalid-dictionary-entry",
8050
+ subject,
8051
+ `The 'trigger' property is deprecated. Use 'latexTrigger' or 'identifierTrigger' instead`
8052
+ ]
8053
+ });
8054
+ }
7968
8055
  if ("kind" in entry && ![
7969
8056
  "expression",
7970
8057
  "symbol",
@@ -8131,6 +8218,7 @@ var DEFAULT_LATEX_DICTIONARY = {
8131
8218
  algebra: DEFINITIONS_ALGEBRA,
8132
8219
  arithmetic: DEFINITIONS_ARITHMETIC,
8133
8220
  calculus: DEFINITIONS_CALCULUS,
8221
+ complex: DEFINITIONS_COMPLEX,
8134
8222
  core: DEFINITIONS_CORE,
8135
8223
  logic: DEFINITIONS_LOGIC,
8136
8224
  relop: DEFINITIONS_INEQUALITIES,
@@ -8143,6 +8231,7 @@ var DEFAULT_LATEX_DICTIONARY = {
8143
8231
  }
8144
8232
  ],
8145
8233
  sets: DEFINITIONS_SETS,
8234
+ statistics: DEFINITIONS_STATISTICS,
8146
8235
  symbols: DEFINITIONS_SYMBOLS,
8147
8236
  trigonometry: DEFINITIONS_TRIGONOMETRY
8148
8237
  };
@@ -8666,7 +8755,7 @@ var _Parser = class {
8666
8755
  }
8667
8756
  for (const def of defs) {
8668
8757
  if (def.identifierTrigger) {
8669
- const n = peekComplexId(this, def.identifierTrigger);
8758
+ const n = parseComplexId(this, def.identifierTrigger);
8670
8759
  if (n > 0)
8671
8760
  result.push([def, n]);
8672
8761
  }
@@ -8809,6 +8898,65 @@ var _Parser = class {
8809
8898
  this.index = index;
8810
8899
  return null;
8811
8900
  }
8901
+ /** If the next token matches the open delimiter, set a boundary with
8902
+ * the close token and return true.
8903
+ *
8904
+ * Note this method handles generic delimiters, i.e. '(' will math both
8905
+ * '(', '\left(', '\bigl(', etc...
8906
+ *
8907
+ * Note that the definitions for matchfix may need to include synonyms
8908
+ * for example:
8909
+ *
8910
+ * {
8911
+ * openDelimiter: '(',
8912
+ * closeDelimiter: ')'
8913
+ * }
8914
+ *
8915
+ * and
8916
+ *
8917
+ * {
8918
+ * openDelimiter: '\\lparen',
8919
+ * closeDelimiter: '\\rparen'
8920
+ * }
8921
+ *
8922
+ * For:
8923
+ * - '[': '\\lbrack' and '\\['
8924
+ * - ']': '\\rbrack' and '\\]'
8925
+ * - '{': '\\lbrace' and '\\}'
8926
+ * - '}': '\\rbrace' and '\\}'
8927
+ * - '<': '\\langle'
8928
+ * - '>': '\\rangle'
8929
+ * - '|': '\\vert'
8930
+ * - '||': '\\Vert'
8931
+ * - '|': '\\lvert' and '\\rvert'
8932
+ * - '||': '\\lVert' and '\\rVert'
8933
+ */
8934
+ matchDelimiter(open, close) {
8935
+ if (this.peek === "[")
8936
+ return false;
8937
+ if (Array.isArray(open)) {
8938
+ console.assert(Array.isArray(close));
8939
+ if (this.matchAll(open)) {
8940
+ this.addBoundary(close);
8941
+ return true;
8942
+ }
8943
+ return false;
8944
+ }
8945
+ const start = this.index;
8946
+ const closePrefix = OPEN_DELIMITER_PREFIX[this.peek];
8947
+ if (closePrefix)
8948
+ this.nextToken();
8949
+ if (open === "||" && this.matchAll(["|", "|"])) {
8950
+ this.addBoundary(["|", "|"]);
8951
+ return true;
8952
+ }
8953
+ if (!this.match(open)) {
8954
+ this.index = start;
8955
+ return false;
8956
+ }
8957
+ this.addBoundary(closePrefix ? [closePrefix, close] : [close]);
8958
+ return true;
8959
+ }
8812
8960
  parseGroup() {
8813
8961
  const start = this.index;
8814
8962
  this.skipSpaceTokens();
@@ -9265,6 +9413,8 @@ var _Parser = class {
9265
9413
  * return `['\right', '\rparen']`, which can be matched with `matchAll()`
9266
9414
  *
9267
9415
  * If you need to match several tokens, use `matchAll()`
9416
+ *
9417
+ * @internal
9268
9418
  */
9269
9419
  matchOpenDelimiter(openDelim, closeDelim) {
9270
9420
  const index = this.index;
@@ -9379,42 +9529,20 @@ var _Parser = class {
9379
9529
  const start = this.index;
9380
9530
  for (const def of defs) {
9381
9531
  this.index = start;
9382
- if (Array.isArray(def.openDelimiter)) {
9383
- if (!this.matchAll(def.openDelimiter))
9384
- continue;
9385
- this.addBoundary(def.closeDelimiter);
9386
- const body2 = this.parseExpression();
9387
- this.skipSpace();
9388
- if (!this.matchBoundary()) {
9389
- this.removeBoundary();
9390
- continue;
9391
- }
9392
- const rhs = def.parse(this, body2 ?? ["Sequence"]);
9393
- if (rhs === null)
9394
- continue;
9395
- return rhs;
9396
- }
9397
- const closeDelimiter = this.matchOpenDelimiter(
9398
- def.openDelimiter,
9399
- def.closeDelimiter
9400
- );
9401
- if (closeDelimiter === null)
9532
+ if (!this.matchDelimiter(def.openDelimiter, def.closeDelimiter))
9402
9533
  continue;
9403
- if (this.matchAll(closeDelimiter)) {
9404
- const result2 = def.parse(this, ["Sequence"]);
9405
- if (result2 === null)
9406
- continue;
9407
- return result2;
9408
- }
9409
- this.addBoundary(closeDelimiter);
9410
9534
  const bodyStart = this.index;
9535
+ this.skipSpace();
9411
9536
  let body = this.parseExpression();
9412
9537
  this.skipSpace();
9413
9538
  if (!this.matchBoundary()) {
9539
+ const boundary = this._boundaries[this._boundaries.length - 1].tokens;
9414
9540
  this.removeBoundary();
9415
9541
  this.index = bodyStart;
9542
+ this.skipSpace();
9416
9543
  body = this.parseExpression();
9417
- if (!this.matchAll(closeDelimiter)) {
9544
+ this.skipSpace();
9545
+ if (!this.matchAll(boundary)) {
9418
9546
  if (!this.atEnd)
9419
9547
  continue;
9420
9548
  this.index = start;
@@ -10001,7 +10129,7 @@ var _Parser = class {
10001
10129
  }
10002
10130
  }
10003
10131
  };
10004
- function peekComplexId(parser, id) {
10132
+ function parseComplexId(parser, id) {
10005
10133
  const start = parser.index;
10006
10134
  const candidate = parseIdentifier(parser)?.trim();
10007
10135
  if (candidate === null)
@@ -10270,6 +10398,7 @@ var Serializer = class {
10270
10398
  }
10271
10399
  this.onError = onError;
10272
10400
  this.dictionary = dictionary2;
10401
+ this.canonical = void 0;
10273
10402
  }
10274
10403
  updateOptions(opt) {
10275
10404
  for (const k of Object.keys(this.options))
@@ -10389,9 +10518,16 @@ var Serializer = class {
10389
10518
  return `\\textbf{${x}} & \\rightarrow & ${this.serialize(dict[x])}`;
10390
10519
  }).join("\\\\")}\\end{array}\\right\\rbrack`;
10391
10520
  }
10392
- serialize(expr) {
10521
+ serialize(expr, options) {
10393
10522
  if (expr === null || expr === void 0)
10394
10523
  return "";
10524
+ options ?? (options = {});
10525
+ options = { ...options };
10526
+ if (!("canonical" in options))
10527
+ options.canonical = true;
10528
+ const savedCanonical = this.canonical;
10529
+ if (this.canonical === void 0)
10530
+ this.canonical = options.canonical;
10395
10531
  this.level += 1;
10396
10532
  try {
10397
10533
  const result = (() => {
@@ -10428,10 +10564,12 @@ var Serializer = class {
10428
10564
  ]);
10429
10565
  })();
10430
10566
  this.level -= 1;
10567
+ this.canonical = savedCanonical;
10431
10568
  return result ?? "";
10432
10569
  } catch (e) {
10433
10570
  }
10434
10571
  this.level -= 1;
10572
+ this.canonical = savedCanonical;
10435
10573
  return "";
10436
10574
  }
10437
10575
  applyFunctionStyle(expr, level) {
@@ -10769,8 +10907,8 @@ var LatexSyntax = class _LatexSyntax {
10769
10907
  }
10770
10908
  return expr ?? ["Sequence"];
10771
10909
  }
10772
- serialize(expr) {
10773
- return this.serializer.serialize(expr);
10910
+ serialize(expr, options) {
10911
+ return this.serializer.serialize(expr, options);
10774
10912
  }
10775
10913
  get serializer() {
10776
10914
  if (this._serializer)
@@ -12103,6 +12241,19 @@ function chop(n, tolerance) {
12103
12241
  return 0;
12104
12242
  return n;
12105
12243
  }
12244
+ function erf(x) {
12245
+ const a1 = 0.254829592;
12246
+ const a2 = -0.284496736;
12247
+ const a3 = 1.421413741;
12248
+ const a4 = -1.453152027;
12249
+ const a5 = 1.061405429;
12250
+ const p = 0.3275911;
12251
+ const sign2 = x < 0 ? -1 : 1;
12252
+ x = Math.abs(x);
12253
+ const t = 1 / (1 + p * x);
12254
+ const y = ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t;
12255
+ return sign2 * (1 - y * Math.exp(-x * x));
12256
+ }
12106
12257
 
12107
12258
  // src/compute-engine/numerics/numeric-bigint.ts
12108
12259
  function bigint(a) {
@@ -18930,6 +19081,24 @@ function multiply2(op12, op22, metadata) {
18930
19081
  console.assert(op12.isCanonical);
18931
19082
  console.assert(op22.isCanonical);
18932
19083
  const ce = op12.engine;
19084
+ if (op12.symbol === "ImaginaryUnit") {
19085
+ const f = asFloat(op22);
19086
+ if (f !== null)
19087
+ return ce.number(ce.complex(0, f));
19088
+ }
19089
+ if (op22.symbol === "ImaginaryUnit") {
19090
+ const f = asFloat(op12);
19091
+ if (f !== null)
19092
+ return ce.number(ce.complex(0, f));
19093
+ }
19094
+ if (op12.numericValue !== null && op22.numericValue !== null) {
19095
+ const f1 = asFloat(op12);
19096
+ const f2 = asFloat(op22);
19097
+ if (f1 !== null && ce.isComplex(op22))
19098
+ return ce.number(ce.complex(f1 * op22.re, f1 * op22.im));
19099
+ if (f2 !== null && ce.isComplex(op12))
19100
+ return ce.number(ce.complex(f2 * op12.re, f2 * op12.im));
19101
+ }
18933
19102
  if (op12.numericValue !== null && op22.numericValue !== null && op12.isInteger && op22.isInteger) {
18934
19103
  return apply2N(
18935
19104
  op12,
@@ -19345,14 +19514,6 @@ var ARITHMETIC_LIBRARY = [
19345
19514
  }
19346
19515
  }
19347
19516
  },
19348
- Erf: {
19349
- description: "Error Function",
19350
- complexity: 7500
19351
- },
19352
- Erfc: {
19353
- description: "Complementary Error Function",
19354
- complexity: 7500
19355
- },
19356
19517
  Factorial: {
19357
19518
  description: "Factorial Function",
19358
19519
  wikidata: "Q120976",
@@ -19497,7 +19658,7 @@ var ARITHMETIC_LIBRARY = [
19497
19658
  description: "Maximum of two or more numbers",
19498
19659
  complexity: 1200,
19499
19660
  signature: {
19500
- domain: ["Function", ["Sequence", "Number"], "Number"],
19661
+ domain: ["Function", ["Sequence", "Value"], "Number"],
19501
19662
  simplify: (ce, ops2) => {
19502
19663
  if (ops2.length === 0)
19503
19664
  return ce._NEGATIVE_INFINITY;
@@ -19526,7 +19687,7 @@ var ARITHMETIC_LIBRARY = [
19526
19687
  description: "Minimum of two or more numbers",
19527
19688
  complexity: 1200,
19528
19689
  signature: {
19529
- domain: ["Function", ["Sequence", "Number"], "Number"],
19690
+ domain: ["Function", ["Sequence", "Value"], "Number"],
19530
19691
  simplify: (ce, ops2) => {
19531
19692
  if (ops2.length === 0)
19532
19693
  return ce._NEGATIVE_INFINITY;
@@ -20084,6 +20245,125 @@ var COLLECTIONS_LIBRARY = {
20084
20245
  }
20085
20246
  };
20086
20247
 
20248
+ // src/compute-engine/library/complex.ts
20249
+ var COMPLEX_LIBRARY = [
20250
+ {
20251
+ Real: {
20252
+ threadable: true,
20253
+ complexity: 1200,
20254
+ signature: {
20255
+ domain: ["Function", "Number", "Number"],
20256
+ evaluate: (ce, ops2) => {
20257
+ const op3 = ops2[0].numericValue;
20258
+ if (op3 === null)
20259
+ return void 0;
20260
+ if (ce.isComplex(op3))
20261
+ return ce.number(op3.re);
20262
+ return ops2[0];
20263
+ }
20264
+ }
20265
+ },
20266
+ Imaginary: {
20267
+ threadable: true,
20268
+ complexity: 1200,
20269
+ signature: {
20270
+ domain: ["Function", "Number", "Number"],
20271
+ evaluate: (ce, ops2) => {
20272
+ const op3 = ops2[0].numericValue;
20273
+ if (op3 === null)
20274
+ return void 0;
20275
+ if (ce.isComplex(op3))
20276
+ return ce.number(op3.im);
20277
+ return ce._ZERO;
20278
+ }
20279
+ }
20280
+ },
20281
+ Argument: {
20282
+ threadable: true,
20283
+ complexity: 1200,
20284
+ signature: {
20285
+ domain: ["Function", "Number", "Number"],
20286
+ evaluate: (ce, ops2) => {
20287
+ const op3 = ops2[0].numericValue;
20288
+ if (op3 === null)
20289
+ return void 0;
20290
+ if (ce.isComplex(op3))
20291
+ return ce.number(op3.arg());
20292
+ const f = asFloat(ops2[0]);
20293
+ if (f === null)
20294
+ return void 0;
20295
+ if (f >= 0)
20296
+ return ce.number(0);
20297
+ return ce.number(Math.PI);
20298
+ }
20299
+ }
20300
+ },
20301
+ AbsArg: {
20302
+ threadable: true,
20303
+ complexity: 1200,
20304
+ signature: {
20305
+ domain: ["Function", "Number", "Tuple"],
20306
+ evaluate: (ce, ops2) => {
20307
+ const op3 = ops2[0].numericValue;
20308
+ if (op3 === null)
20309
+ return void 0;
20310
+ if (ce.isComplex(op3))
20311
+ return ce.tuple([ce.number(op3.abs()), ce.number(op3.arg())]);
20312
+ const f = asFloat(ops2[0]);
20313
+ if (f === null)
20314
+ return void 0;
20315
+ return ce.tuple([
20316
+ ce.number(Math.abs(f)),
20317
+ ce.number(f >= 0 ? 0 : Math.PI)
20318
+ ]);
20319
+ }
20320
+ }
20321
+ },
20322
+ Conjugate: {
20323
+ threadable: true,
20324
+ complexity: 1200,
20325
+ signature: {
20326
+ domain: ["Function", "Number", "Number"],
20327
+ evaluate: (ce, ops2) => {
20328
+ const op3 = ops2[0].numericValue;
20329
+ if (op3 === null || !ce.isComplex(op3))
20330
+ return void 0;
20331
+ return ce.number(op3.conjugate());
20332
+ }
20333
+ }
20334
+ },
20335
+ ComplexRoots: {
20336
+ threadable: true,
20337
+ complexity: 1200,
20338
+ signature: {
20339
+ domain: ["Function", "Number", "Number", "List"],
20340
+ evaluate: (ce, ops2) => {
20341
+ const x = asFloat(ops2[0]);
20342
+ const n = asFloat(ops2[1]);
20343
+ if (x === null || n === null || !Number.isInteger(n) || n <= 0)
20344
+ return void 0;
20345
+ const roots = [];
20346
+ const [re, im] = ce.isComplex(x) ? [x.re, x.im] : [x, 0];
20347
+ const arg = Math.atan2(im, re);
20348
+ const mod2 = Math.sqrt(re * re + im * im);
20349
+ for (let k = 0; k < n; k++) {
20350
+ const theta = (arg + 2 * Math.PI * k) / n;
20351
+ const r = Math.pow(mod2, 1 / n);
20352
+ roots.push([r * Math.cos(theta), r * Math.sin(theta)]);
20353
+ }
20354
+ return ce.box([
20355
+ "List",
20356
+ ...roots.map(
20357
+ (r) => ce.number(r[1] !== 0 ? ce.complex(r[0], r[1]) : r[0])
20358
+ )
20359
+ ]);
20360
+ }
20361
+ }
20362
+ }
20363
+ // For Abs (magnitude) see src/compute-engine/library/processAbs
20364
+ }
20365
+ ];
20366
+
20087
20367
  // src/compute-engine/library/random-expression.ts
20088
20368
  function oneOf(xs) {
20089
20369
  return xs[Math.floor(Math.random() * xs.length)];
@@ -21582,6 +21862,328 @@ function evaluateElement(ce, ops2) {
21582
21862
  return ce._fn("Element", [lhs, rhs]);
21583
21863
  }
21584
21864
 
21865
+ // src/compute-engine/library/statistics.ts
21866
+ var STATISTICS_LIBRARY = [
21867
+ {
21868
+ Mean: {
21869
+ complexity: 1200,
21870
+ signature: {
21871
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21872
+ evaluate: (ce, ops2) => {
21873
+ let sum2 = 0;
21874
+ let count = 0;
21875
+ for (const op3 of each(ops2)) {
21876
+ const v = asFloat(op3);
21877
+ if (v === null)
21878
+ return void 0;
21879
+ sum2 += v;
21880
+ count++;
21881
+ }
21882
+ if (count === 0)
21883
+ return ce._NAN;
21884
+ return ce.number(sum2 / count);
21885
+ }
21886
+ }
21887
+ },
21888
+ Median: {
21889
+ complexity: 1200,
21890
+ signature: {
21891
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21892
+ evaluate: (ce, ops2) => {
21893
+ const values = [];
21894
+ for (const op3 of each(ops2)) {
21895
+ const v = asFloat(op3);
21896
+ if (v === null)
21897
+ return void 0;
21898
+ values.push(v);
21899
+ }
21900
+ if (values.length === 0)
21901
+ return ce._NAN;
21902
+ values.sort((a, b) => a - b);
21903
+ const mid = Math.floor(values.length / 2);
21904
+ if (values.length % 2 === 0)
21905
+ return ce.number((values[mid - 1] + values[mid]) / 2);
21906
+ return ce.number(values[mid]);
21907
+ }
21908
+ }
21909
+ },
21910
+ Variance: {
21911
+ complexity: 1200,
21912
+ signature: {
21913
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21914
+ evaluate: (ce, ops2) => {
21915
+ let sum2 = 0;
21916
+ let sum22 = 0;
21917
+ let count = 0;
21918
+ for (const op3 of each(ops2)) {
21919
+ const v = asFloat(op3);
21920
+ if (v === null)
21921
+ return void 0;
21922
+ sum2 += v;
21923
+ sum22 += v * v;
21924
+ count++;
21925
+ }
21926
+ if (count === 0)
21927
+ return ce._NAN;
21928
+ return ce.number((sum22 - sum2 * sum2 / count) / (count - 1));
21929
+ }
21930
+ }
21931
+ },
21932
+ StandardDeviation: {
21933
+ complexity: 1200,
21934
+ signature: {
21935
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21936
+ evaluate: (ce, ops2) => {
21937
+ let sum2 = 0;
21938
+ let sum22 = 0;
21939
+ let count = 0;
21940
+ for (const op3 of each(ops2)) {
21941
+ const v = asFloat(op3);
21942
+ if (v === null)
21943
+ return void 0;
21944
+ sum2 += v;
21945
+ sum22 += v * v;
21946
+ count++;
21947
+ }
21948
+ if (count === 0)
21949
+ return ce._NAN;
21950
+ return ce.number(
21951
+ Math.sqrt((sum22 - sum2 * sum2 / count) / (count - 1))
21952
+ );
21953
+ }
21954
+ }
21955
+ },
21956
+ Kurtosis: {
21957
+ complexity: 1200,
21958
+ signature: {
21959
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21960
+ evaluate: (ce, ops2) => {
21961
+ let sum2 = 0;
21962
+ let sum22 = 0;
21963
+ let sum4 = 0;
21964
+ let count = 0;
21965
+ for (const op3 of each(ops2)) {
21966
+ const v = asFloat(op3);
21967
+ if (v === null)
21968
+ return void 0;
21969
+ sum2 += v;
21970
+ sum22 += v * v;
21971
+ sum4 += v * v * v * v;
21972
+ count++;
21973
+ }
21974
+ if (count === 0)
21975
+ return ce._NAN;
21976
+ const s2 = (sum22 - sum2 * sum2 / count) / (count - 1);
21977
+ const s4 = (sum4 - sum22 * sum22 / count) / (count - 1);
21978
+ return ce.number((s4 / (s2 * s2) - 3) * (count * (count + 1)) / 6);
21979
+ }
21980
+ }
21981
+ },
21982
+ Skewness: {
21983
+ complexity: 1200,
21984
+ signature: {
21985
+ domain: ["Function", ["Sequence", "Value"], "Number"],
21986
+ evaluate: (ce, ops2) => {
21987
+ let sum2 = 0;
21988
+ let sum22 = 0;
21989
+ let sum3 = 0;
21990
+ let count = 0;
21991
+ for (const op3 of each(ops2)) {
21992
+ const v = asFloat(op3);
21993
+ if (v === null)
21994
+ return void 0;
21995
+ sum2 += v;
21996
+ sum22 += v * v;
21997
+ sum3 += v * v * v;
21998
+ count++;
21999
+ }
22000
+ if (count === 0)
22001
+ return ce._NAN;
22002
+ const s2 = (sum22 - sum2 * sum2 / count) / (count - 1);
22003
+ const s3 = (sum3 - sum22 * sum2 / count) / (count - 1);
22004
+ return ce.number(s3 / Math.pow(s2, 3 / 2) * Math.sqrt(count * 1));
22005
+ }
22006
+ }
22007
+ },
22008
+ Mode: {
22009
+ complexity: 1200,
22010
+ signature: {
22011
+ domain: ["Function", ["Sequence", "Value"], "Number"],
22012
+ evaluate: (ce, ops2) => {
22013
+ const values = [];
22014
+ for (const op3 of each(ops2)) {
22015
+ const v = asFloat(op3);
22016
+ if (v === null)
22017
+ return void 0;
22018
+ values.push(v);
22019
+ }
22020
+ if (values.length === 0)
22021
+ return ce._NAN;
22022
+ values.sort((a, b) => a - b);
22023
+ const counts = {};
22024
+ for (const v of values) {
22025
+ counts[v] = (counts[v] ?? 0) + 1;
22026
+ }
22027
+ let max2 = 0;
22028
+ let mode = values[0];
22029
+ for (const v of values) {
22030
+ const c = counts[v];
22031
+ if (c > max2) {
22032
+ max2 = c;
22033
+ mode = v;
22034
+ }
22035
+ }
22036
+ return ce.number(mode);
22037
+ }
22038
+ }
22039
+ },
22040
+ Quartiles: {
22041
+ complexity: 1200,
22042
+ signature: {
22043
+ domain: ["Function", ["Sequence", "Value"], "List"],
22044
+ evaluate: (ce, ops2) => {
22045
+ const values = [];
22046
+ for (const op3 of each(ops2)) {
22047
+ const v = asFloat(op3);
22048
+ if (v === null)
22049
+ return void 0;
22050
+ values.push(v);
22051
+ }
22052
+ if (values.length === 0)
22053
+ return ce._NAN;
22054
+ values.sort((a, b) => a - b);
22055
+ const mid = Math.floor(values.length / 2);
22056
+ const lower = values.slice(0, mid);
22057
+ const upper = values.slice(mid + 1);
22058
+ return ce.box([
22059
+ "List",
22060
+ ce.number(values[mid]),
22061
+ ce.number(lower[Math.floor(lower.length / 2)]),
22062
+ ce.number(upper[Math.floor(upper.length / 2)])
22063
+ ]);
22064
+ }
22065
+ }
22066
+ },
22067
+ InterquartileRange: {
22068
+ complexity: 1200,
22069
+ signature: {
22070
+ domain: ["Function", ["Sequence", "Value"], "Number"],
22071
+ evaluate: (ce, ops2) => {
22072
+ const values = [];
22073
+ for (const op3 of each(ops2)) {
22074
+ const v = asFloat(op3);
22075
+ if (v === null)
22076
+ return void 0;
22077
+ values.push(v);
22078
+ }
22079
+ if (values.length === 0)
22080
+ return ce._NAN;
22081
+ values.sort((a, b) => a - b);
22082
+ const mid = Math.floor(values.length / 2);
22083
+ const lower = values.slice(0, mid);
22084
+ const upper = values.slice(mid + 1);
22085
+ return ce.number(
22086
+ upper[Math.floor(upper.length / 2)] - lower[Math.floor(lower.length / 2)]
22087
+ );
22088
+ }
22089
+ }
22090
+ },
22091
+ Count: {
22092
+ threadable: true,
22093
+ complexity: 1200,
22094
+ signature: {
22095
+ domain: ["Function", ["Sequence", "Value"], "Number"],
22096
+ evaluate: (ce, ops2) => {
22097
+ let count = 0;
22098
+ for (const _op of each(ops2))
22099
+ count++;
22100
+ return ce.number(count);
22101
+ }
22102
+ }
22103
+ },
22104
+ Erf: {
22105
+ complexity: 7500,
22106
+ signature: {
22107
+ domain: ["Function", "Number", "Number"],
22108
+ evaluate: (ce, ops2) => {
22109
+ const x = asFloat(ops2[0]);
22110
+ if (x === null)
22111
+ return void 0;
22112
+ return ce.number(erf(x));
22113
+ }
22114
+ }
22115
+ },
22116
+ Erfc: {
22117
+ complexity: 7500,
22118
+ signature: {
22119
+ domain: ["Function", "Number", "Number"],
22120
+ evaluate: (ce, ops2) => {
22121
+ const x = asFloat(ops2[0]);
22122
+ if (x === null)
22123
+ return void 0;
22124
+ return ce.number(1 - erf(x));
22125
+ }
22126
+ }
22127
+ }
22128
+ }
22129
+ ];
22130
+ function* each(ops2) {
22131
+ if (ops2.length === 0)
22132
+ return;
22133
+ const ce = ops2[0].engine;
22134
+ for (const op3 of ops2) {
22135
+ const h = op3.head;
22136
+ if (h === "Range") {
22137
+ let lower = asFloat(op3[1]);
22138
+ if (lower === null)
22139
+ return;
22140
+ let upper = asFloat(op3[2]);
22141
+ if (upper === null) {
22142
+ upper = lower;
22143
+ lower = 1;
22144
+ }
22145
+ if (lower > upper) {
22146
+ const step2 = asFloat(op3[3] ?? -1) ?? -1;
22147
+ if (step2 >= 0)
22148
+ return;
22149
+ for (let i = lower; i <= upper; i += step2)
22150
+ yield ce.number(i);
22151
+ return;
22152
+ }
22153
+ const step = asFloat(op3[3] ?? 1) ?? 1;
22154
+ if (step <= 0)
22155
+ return;
22156
+ for (let i = lower; i <= upper; i += step)
22157
+ yield ce.number(i);
22158
+ return;
22159
+ }
22160
+ if (h === "Linspace") {
22161
+ let start = asFloat(op3[1]);
22162
+ if (start === null)
22163
+ return;
22164
+ let stop = asFloat(op3[2]);
22165
+ if (stop === null) {
22166
+ stop = start;
22167
+ start = 0;
22168
+ }
22169
+ const num = asFloat(op3[3]) ?? 50;
22170
+ if (!Number.isInteger(num))
22171
+ return;
22172
+ if (num <= 0)
22173
+ return;
22174
+ const step = (stop - start) / (num - 1);
22175
+ for (let i = start; i <= stop; i += step)
22176
+ yield ce.number(i);
22177
+ return;
22178
+ }
22179
+ if (typeof h === "string" && /^(List|Sequence|Tuple|Single|Pair|Triple)$/.test(h)) {
22180
+ yield* each(op3.ops);
22181
+ return;
22182
+ }
22183
+ yield op3;
22184
+ }
22185
+ }
22186
+
21585
22187
  // src/compute-engine/library/trigonometry.ts
21586
22188
  var domainNumberToRealNumber = (_head) => {
21587
22189
  return ["Function", "Number", "ExtendedRealNumber"];
@@ -23148,7 +23750,7 @@ var LIBRARIES = {
23148
23750
  // // - factor
23149
23751
  // // - simplify
23150
23752
  // ],
23151
- "arithmetic": ARITHMETIC_LIBRARY,
23753
+ "arithmetic": [...ARITHMETIC_LIBRARY, ...COMPLEX_LIBRARY],
23152
23754
  "calculus": CALCULUS_LIBRARY,
23153
23755
  "combinatorics": [],
23154
23756
  // @todo fibonacci, binomial, etc...
@@ -23209,15 +23811,7 @@ var LIBRARIES = {
23209
23811
  // unit: ['Divide', 'N', ['Square', 'A']],
23210
23812
  }
23211
23813
  },
23212
- "statistics": [],
23213
- // @todo statistics: [
23214
- // // average
23215
- // // mean
23216
- // // variance = size(l) * stddev(l)^2 / (size(l) - 1)
23217
- // // stddev
23218
- // // median
23219
- // // quantile
23220
- // ],
23814
+ "statistics": STATISTICS_LIBRARY,
23221
23815
  "trigonometry": TRIGONOMETRY_LIBRARY,
23222
23816
  "units": []
23223
23817
  };
@@ -23273,7 +23867,7 @@ ${JSON.stringify(entry)}`
23273
23867
  }
23274
23868
 
23275
23869
  // src/compute-engine/cost-function.ts
23276
- var import_complex18 = __toESM(require_complex());
23870
+ var import_complex19 = __toESM(require_complex());
23277
23871
  function numericCostFunction(n) {
23278
23872
  if (Number.isInteger(n) && n !== 0) {
23279
23873
  return Math.floor(Math.log2(Math.abs(n)) / Math.log2(10)) + (n > 0 ? 1 : 2);
@@ -23295,7 +23889,7 @@ function costFunction(expr) {
23295
23889
  else
23296
23890
  return numericCostFunction(Number(num[0])) + numericCostFunction(Number(num[1])) + 1;
23297
23891
  }
23298
- if (num instanceof import_complex18.default)
23892
+ if (num instanceof import_complex19.default)
23299
23893
  return numericCostFunction(num.re) + numericCostFunction(num.im) + 1;
23300
23894
  if (expr.isNumber)
23301
23895
  return 2;
@@ -24237,11 +24831,11 @@ var ComputeEngine = class _ComputeEngine {
24237
24831
  this._ONE = new BoxedNumber(this, 1);
24238
24832
  this._HALF = new BoxedNumber(this, [1, 2]);
24239
24833
  this._NEGATIVE_ONE = new BoxedNumber(this, -1);
24240
- this._I = new BoxedNumber(this, import_complex19.Complex.I);
24834
+ this._I = new BoxedNumber(this, import_complex20.Complex.I);
24241
24835
  this._NAN = new BoxedNumber(this, Number.NaN);
24242
24836
  this._POSITIVE_INFINITY = new BoxedNumber(this, Number.POSITIVE_INFINITY);
24243
24837
  this._NEGATIVE_INFINITY = new BoxedNumber(this, Number.NEGATIVE_INFINITY);
24244
- this._COMPLEX_INFINITY = new BoxedNumber(this, import_complex19.Complex.INFINITY);
24838
+ this._COMPLEX_INFINITY = new BoxedNumber(this, import_complex20.Complex.INFINITY);
24245
24839
  this.reset();
24246
24840
  this.context = {
24247
24841
  assumptions: new ExpressionMap(),
@@ -24479,7 +25073,7 @@ var ComputeEngine = class _ComputeEngine {
24479
25073
  return 0;
24480
25074
  if (n instanceof Decimal && n.abs().lte(this._bignumTolerance))
24481
25075
  return 0;
24482
- if (n instanceof import_complex19.Complex && Math.abs(n.re) <= this._tolerance && Math.abs(n.im) <= this._tolerance)
25076
+ if (n instanceof import_complex20.Complex && Math.abs(n.re) <= this._tolerance && Math.abs(n.im) <= this._tolerance)
24483
25077
  return 0;
24484
25078
  return n;
24485
25079
  }
@@ -24493,13 +25087,13 @@ var ComputeEngine = class _ComputeEngine {
24493
25087
  a = a.toNumber();
24494
25088
  if (b instanceof Decimal)
24495
25089
  b = b.toNumber();
24496
- return new import_complex19.Complex(a, b);
25090
+ return new import_complex20.Complex(a, b);
24497
25091
  }
24498
25092
  isBignum(a) {
24499
25093
  return a instanceof Decimal;
24500
25094
  }
24501
25095
  isComplex(a) {
24502
- return a instanceof import_complex19.Complex;
25096
+ return a instanceof import_complex20.Complex;
24503
25097
  }
24504
25098
  get latexSyntax() {
24505
25099
  if (!this._latexSyntax)
@@ -24859,6 +25453,10 @@ var ComputeEngine = class _ComputeEngine {
24859
25453
  return canonicalPower(this, base, this._HALF, metadata);
24860
25454
  }
24861
25455
  pow(base, exponent, metadata) {
25456
+ if (base.symbol === "ExponentialE" && exponent instanceof import_complex20.Complex && exponent.re === 0) {
25457
+ const im = exponent.im;
25458
+ return this.number(this.complex(Math.cos(im), Math.sin(im)));
25459
+ }
24862
25460
  if (exponent instanceof AbstractBoxedExpression) {
24863
25461
  const num = exponent.numericValue;
24864
25462
  if (num !== null) {
@@ -25040,14 +25638,15 @@ var ComputeEngine = class _ComputeEngine {
25040
25638
  const result = this.latexSyntax.parse(latexString(latex) ?? latex);
25041
25639
  return this.box(result, options);
25042
25640
  }
25043
- serialize(x) {
25641
+ serialize(x, options) {
25044
25642
  if (typeof x === "object" && "json" in x) {
25045
25643
  const ce = "engine" in x ? x.engine : this;
25046
25644
  return this.latexSyntax.serialize(
25047
- this.rawJson(ce.box(x, { canonical: false }))
25645
+ this.rawJson(ce.box(x, { canonical: false })),
25646
+ options
25048
25647
  );
25049
25648
  }
25050
- return this.latexSyntax.serialize(x);
25649
+ return this.latexSyntax.serialize(x, options);
25051
25650
  }
25052
25651
  get latexOptions() {
25053
25652
  const latexSyntax = this.latexSyntax;
@@ -25208,10 +25807,10 @@ var ComputeEngine = class _ComputeEngine {
25208
25807
  };
25209
25808
 
25210
25809
  // src/compute-engine.ts
25211
- var version = "0.14.0";
25810
+ var version = "0.15.0";
25212
25811
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
25213
25812
  ComputeEngine: ComputeEngine.prototype.constructor,
25214
- version: "0.14.0"
25813
+ version: "0.15.0"
25215
25814
  };
25216
25815
  export {
25217
25816
  ComputeEngine,