@cortex-js/compute-engine 0.17.0 → 0.18.1

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 (100) hide show
  1. package/dist/compute-engine.esm.js +496 -152
  2. package/dist/compute-engine.js +496 -152
  3. package/dist/compute-engine.min.esm.js +11 -8
  4. package/dist/compute-engine.min.js +11 -8
  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/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  11. package/dist/types/common/signals.d.ts +1 -1
  12. package/dist/types/common/utils.d.ts +1 -1
  13. package/dist/types/compute-engine/assume.d.ts +1 -1
  14. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  17. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +1 -1
  18. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  20. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  21. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  26. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  30. package/dist/types/compute-engine/compile.d.ts +8 -4
  31. package/dist/types/compute-engine/compute-engine.d.ts +1 -1
  32. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  33. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  34. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/public.d.ts +15 -6
  52. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  53. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  54. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  55. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  56. package/dist/types/compute-engine/library/arithmetic-add.d.ts +1 -1
  57. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  58. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +1 -1
  59. package/dist/types/compute-engine/library/arithmetic-power.d.ts +1 -1
  60. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  61. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  62. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  63. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  64. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  65. package/dist/types/compute-engine/library/core.d.ts +1 -1
  66. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  67. package/dist/types/compute-engine/library/library.d.ts +1 -1
  68. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  69. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  70. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  71. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  72. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  73. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  74. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  75. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  76. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  77. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  78. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  79. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  80. package/dist/types/compute-engine/numerics/numeric.d.ts +12 -2
  81. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  82. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  83. package/dist/types/compute-engine/numerics/richardson.d.ts +80 -0
  84. package/dist/types/compute-engine/public.d.ts +1 -1
  85. package/dist/types/compute-engine/rules.d.ts +1 -1
  86. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  87. package/dist/types/compute-engine/solve.d.ts +1 -1
  88. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  89. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  90. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  91. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  92. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  93. package/dist/types/compute-engine/symbolic/product.d.ts +1 -1
  94. package/dist/types/compute-engine/symbolic/sum.d.ts +1 -1
  95. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  96. package/dist/types/compute-engine.d.ts +2 -2
  97. package/dist/types/math-json/math-json-format.d.ts +1 -1
  98. package/dist/types/math-json/utils.d.ts +1 -1
  99. package/dist/types/math-json.d.ts +2 -2
  100. package/package.json +10 -3
@@ -1,4 +1,4 @@
1
- /** CortexJS Compute Engine 0.17.0 */
1
+ /** CortexJS Compute Engine 0.18.1 */
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -1063,7 +1063,7 @@ function isEnvironmentEntry(entry) {
1063
1063
  }
1064
1064
 
1065
1065
  // src/compute-engine/compute-engine.ts
1066
- var import_complex20 = __toESM(require_complex(), 1);
1066
+ var import_complex20 = __toESM(require_complex());
1067
1067
 
1068
1068
  // node_modules/decimal.js/decimal.mjs
1069
1069
  var EXP_LIMIT = 9e15;
@@ -4859,8 +4859,41 @@ var DEFINITIONS_ARITHMETIC = [
4859
4859
  identifierTrigger: "lcm",
4860
4860
  kind: "function"
4861
4861
  },
4862
- { name: "Max", identifierTrigger: "max", kind: "function" },
4863
- { name: "Min", identifierTrigger: "min", kind: "function" },
4862
+ { identifierTrigger: "max", kind: "function", parse: "Max" },
4863
+ { identifierTrigger: "min", kind: "function", parse: "Min" },
4864
+ { name: "Max", latexTrigger: "\\max", kind: "function" },
4865
+ { name: "Min", latexTrigger: "\\min", kind: "function" },
4866
+ { name: "Supremum", latexTrigger: "\\sup", kind: "function" },
4867
+ { name: "Infimum", latexTrigger: "\\inf", kind: "function" },
4868
+ {
4869
+ name: "Limit",
4870
+ latexTrigger: "\\lim",
4871
+ kind: "expression",
4872
+ parse: (parser) => {
4873
+ if (!parser.match("_"))
4874
+ return void 0;
4875
+ const base = parser.parseGroup();
4876
+ if (head(base) !== "To")
4877
+ return void 0;
4878
+ const expr = parser.parseArguments("implicit");
4879
+ if (!expr)
4880
+ return void 0;
4881
+ return ["Limit", ["Function", expr[0], op(base, 1)], op(base, 2)];
4882
+ },
4883
+ serialize: (serializer, expr) => {
4884
+ const fn = op(expr, 1);
4885
+ const fnVar = op(fn, 2);
4886
+ const to = op(expr, 2);
4887
+ return joinLatex([
4888
+ "\\lim_{",
4889
+ serializer.serialize(fnVar),
4890
+ "\\to",
4891
+ serializer.serialize(to),
4892
+ "}",
4893
+ serializer.serialize(op(fn, 1))
4894
+ ]);
4895
+ }
4896
+ },
4864
4897
  {
4865
4898
  name: "MinusPlus",
4866
4899
  latexTrigger: ["\\mp"],
@@ -5298,13 +5331,14 @@ var DEFINITIONS_CORE = [
5298
5331
  },
5299
5332
  {
5300
5333
  name: "Assign",
5301
- kind: "function",
5302
5334
  latexTrigger: "\\coloneqq",
5335
+ kind: "infix",
5336
+ associativity: "right",
5303
5337
  precedence: 260,
5304
- parse: (parser, lhs) => {
5305
- const rhs = parser.parseExpression({ minPrec: 260 }) ?? "Nothing";
5306
- return ["Assign", lhs, rhs];
5307
- },
5338
+ // parse: (parser: Parser, lhs: Expression) => {
5339
+ // const rhs = parser.parseExpression({ minPrec: 260 }) ?? 'Nothing';
5340
+ // return ['Assign', lhs, rhs];
5341
+ // },
5308
5342
  serialize: (serializer, expr) => {
5309
5343
  return joinLatex([
5310
5344
  serializer.serialize(op(expr, 1)),
@@ -5314,22 +5348,31 @@ var DEFINITIONS_CORE = [
5314
5348
  }
5315
5349
  },
5316
5350
  {
5317
- kind: "function",
5318
- latexTrigger: ":=",
5319
- // \coloneqq
5320
- parse: (parser, lhs) => {
5321
- const rhs = parser.parseExpression({ minPrec: 270 }) ?? "Nothing";
5322
- return ["Assign", lhs, rhs];
5323
- }
5351
+ latexTrigger: [":", "="],
5352
+ kind: "infix",
5353
+ associativity: "right",
5354
+ precedence: 260,
5355
+ parse: "Assign"
5324
5356
  },
5357
+ // {
5358
+ // kind: 'function',
5359
+ // latexTrigger: ':=', // \coloneqq
5360
+ // parse: (parser: Parser, lhs: Expression) => {
5361
+ // const rhs = parser.parseExpression({ minPrec: 270 }) ?? 'Nothing';
5362
+ // return ['Assign', lhs, rhs];
5363
+ // },
5364
+ // },
5325
5365
  {
5326
- kind: "function",
5327
5366
  latexTrigger: "\\colonequals",
5328
5367
  // \coloneqq
5329
- parse: (parser, lhs) => {
5330
- const rhs = parser.parseExpression({ minPrec: 270 }) ?? "Nothing";
5331
- return ["Assign", lhs, rhs];
5332
- }
5368
+ kind: "infix",
5369
+ associativity: "right",
5370
+ precedence: 260,
5371
+ parse: "Assign"
5372
+ // parse: (parser: Parser, lhs: Expression) => {
5373
+ // const rhs = parser.parseExpression({ minPrec: 270 }) ?? 'Nothing';
5374
+ // return ['Assign', lhs, rhs];
5375
+ // },
5333
5376
  },
5334
5377
  {
5335
5378
  name: "BaseForm",
@@ -6074,13 +6117,6 @@ var DEFINITIONS_INEQUALITIES = [
6074
6117
  associativity: "right",
6075
6118
  precedence: 260
6076
6119
  },
6077
- {
6078
- latexTrigger: [":", "="],
6079
- kind: "infix",
6080
- associativity: "right",
6081
- precedence: 260,
6082
- parse: "Assign"
6083
- },
6084
6120
  {
6085
6121
  name: "Approx",
6086
6122
  // Note: Mathematica TildeTilde
@@ -8672,7 +8708,7 @@ var DEFAULT_PARSE_LATEX_OPTIONS = {
8672
8708
  applyInvisibleOperator: "auto",
8673
8709
  skipSpace: true,
8674
8710
  parseArgumentsOfUnknownLatexCommands: true,
8675
- parseNumbers: true,
8711
+ parseNumbers: "auto",
8676
8712
  parseUnknownIdentifier: (id, parser) => parser.computeEngine?.lookupFunction(id) !== void 0 ? "function" : "symbol",
8677
8713
  preserveLatex: false
8678
8714
  };
@@ -9282,27 +9318,27 @@ var _Parser = class {
9282
9318
  if (this.matchAny(["e", "E"])) {
9283
9319
  const exponent = this.parseSignedInteger({ withGrouping: false });
9284
9320
  if (exponent)
9285
- return "e" + exponent;
9321
+ return exponent;
9286
9322
  }
9287
9323
  this.index = start;
9288
9324
  if (this.match("\\times")) {
9289
9325
  this.skipSpaceTokens();
9290
9326
  if (this.match("1") && this.match("0") && this.match("^")) {
9291
9327
  if (/^[0-9]$/.test(this.peek))
9292
- return "e" + this.nextToken();
9328
+ return this.nextToken();
9293
9329
  if (this.match("<{>")) {
9294
9330
  this.skipSpaceTokens();
9295
9331
  const exponent = this.parseSignedInteger();
9296
9332
  this.skipSpaceTokens();
9297
9333
  if (this.match("<}>") && exponent)
9298
- return "e" + exponent;
9334
+ return exponent;
9299
9335
  }
9300
9336
  }
9301
9337
  }
9302
9338
  this.index = start;
9303
9339
  this.skipSpaceTokens();
9304
9340
  if (this.match("\\%"))
9305
- return `e-2`;
9341
+ return `-2`;
9306
9342
  this.index = start;
9307
9343
  if (this.matchAll(this._exponentProductTokens)) {
9308
9344
  this.skipSpaceTokens();
@@ -9311,7 +9347,7 @@ var _Parser = class {
9311
9347
  const exponent = this.parseSignedInteger();
9312
9348
  this.skipSpaceTokens();
9313
9349
  if (this.matchAll(this._endExponentMarkerTokens) && exponent)
9314
- return "e" + exponent;
9350
+ return exponent;
9315
9351
  }
9316
9352
  }
9317
9353
  this.index = start;
@@ -9359,43 +9395,65 @@ var _Parser = class {
9359
9395
  * repeating decimals, etc...
9360
9396
  */
9361
9397
  parseNumber() {
9362
- if (!this.options.parseNumbers)
9398
+ if (this.options.parseNumbers === false || this.options.parseNumbers === "never")
9363
9399
  return null;
9364
9400
  const start = this.index;
9365
9401
  this.skipVisualSpace();
9366
9402
  this.match("+");
9367
- let result = "";
9368
- let dotPrefix = false;
9403
+ let wholePart = "";
9404
+ let fractionalPart = "";
9405
+ let startsWithDecimalMarker = false;
9369
9406
  if (this.match(".") || this.matchAll(this._decimalMarkerTokens)) {
9370
9407
  const peek = this.peek;
9371
- if (peek !== "\\overline" && peek !== this._beginRepeatingDigitsTokens[0] && !/[0-9\(]/.test(peek)) {
9372
- this.index = start;
9373
- return null;
9374
- }
9375
- dotPrefix = true;
9376
- } else {
9377
- result = this.parseDecimalDigits({ withGrouping: true });
9378
- if (!result) {
9379
- this.index = start;
9380
- return null;
9408
+ if (peek === "\\overline" || peek === this._beginRepeatingDigitsTokens[0] || /[0-9\(]/.test(peek)) {
9409
+ startsWithDecimalMarker = true;
9410
+ wholePart = "0";
9381
9411
  }
9412
+ } else
9413
+ wholePart = this.parseDecimalDigits({ withGrouping: true });
9414
+ if (!wholePart) {
9415
+ this.index = start;
9416
+ return null;
9382
9417
  }
9383
- let hasDecimal = true;
9384
- if (!dotPrefix && (this.match(".") || this.matchAll(this._decimalMarkerTokens)))
9385
- result += "." + this.parseDecimalDigits({ withGrouping: true });
9386
- else if (dotPrefix)
9387
- result = "0." + this.parseDecimalDigits({ withGrouping: true });
9418
+ let hasFractionalPart = true;
9419
+ if (startsWithDecimalMarker || this.match(".") || this.matchAll(this._decimalMarkerTokens))
9420
+ fractionalPart = this.parseDecimalDigits({ withGrouping: true });
9388
9421
  else
9389
- hasDecimal = false;
9390
- if (hasDecimal) {
9422
+ hasFractionalPart = false;
9423
+ let hasRepeatingPart = false;
9424
+ if (hasFractionalPart) {
9391
9425
  const repeat = this.parseRepeatingDecimal();
9392
- if (repeat)
9393
- result += repeat;
9394
- else if (this.match("\\ldots") || this.matchAll(this._truncationMarkerTokens)) {
9426
+ if (repeat) {
9427
+ fractionalPart += repeat;
9428
+ hasRepeatingPart = true;
9429
+ } else if (this.match("\\ldots") || this.matchAll(this._truncationMarkerTokens)) {
9395
9430
  }
9396
9431
  }
9397
9432
  this.skipVisualSpace();
9398
- return result + this.parseExponent();
9433
+ const exponent = this.parseExponent();
9434
+ if (!hasRepeatingPart && this.options.parseNumbers === "rational") {
9435
+ const whole = parseInt(wholePart, 10);
9436
+ if (!fractionalPart) {
9437
+ if (exponent)
9438
+ return ["Multiply", whole, ["Power", 10, exponent]];
9439
+ return whole;
9440
+ }
9441
+ const fraction = parseInt(fractionalPart, 10);
9442
+ const n = fractionalPart.length;
9443
+ const numerator = whole * Math.pow(10, n) + fraction;
9444
+ const denominator = Math.pow(10, n);
9445
+ if (exponent) {
9446
+ return [
9447
+ "Multiply",
9448
+ ["Rational", numerator, denominator],
9449
+ ["Power", 10, exponent]
9450
+ ];
9451
+ }
9452
+ return ["Rational", numerator, denominator];
9453
+ }
9454
+ return {
9455
+ num: wholePart + (hasFractionalPart ? "." + fractionalPart : "") + (exponent ? "e" + exponent : "")
9456
+ };
9399
9457
  }
9400
9458
  /**
9401
9459
  * A Latex number can be a decimal, hex or octal number.
@@ -9843,18 +9901,24 @@ var _Parser = class {
9843
9901
  (x) => x.latexTrigger === "^"
9844
9902
  );
9845
9903
  if (defs) {
9846
- const arg = [
9847
- "Superscript",
9848
- result,
9849
- superscripts.length === 1 ? superscripts[0] : ["List", ...superscripts]
9850
- ];
9851
- for (const def of defs) {
9852
- if (typeof def.parse === "function")
9853
- result = def.parse(this, arg, { minPrec: 0 });
9854
- else
9855
- result = arg;
9856
- if (result)
9857
- break;
9904
+ let nonEmptySuperscripts = superscripts.filter(
9905
+ (x) => head(x) !== "Sequence"
9906
+ );
9907
+ if (nonEmptySuperscripts.length !== 0) {
9908
+ const superscriptExpression = nonEmptySuperscripts.length === 1 ? nonEmptySuperscripts[0] : ["List", ...nonEmptySuperscripts];
9909
+ const arg = [
9910
+ "Superscript",
9911
+ result,
9912
+ superscriptExpression
9913
+ ];
9914
+ for (const def of defs) {
9915
+ if (typeof def.parse === "function")
9916
+ result = def.parse(this, arg, { minPrec: 0 });
9917
+ else
9918
+ result = arg;
9919
+ if (result)
9920
+ break;
9921
+ }
9858
9922
  }
9859
9923
  }
9860
9924
  }
@@ -10021,8 +10085,11 @@ var _Parser = class {
10021
10085
  const [def, n] = opDefs[0];
10022
10086
  if (this.peek === "^") {
10023
10087
  this.index += 1;
10024
- this.parseGroup();
10025
- this.error("unexpected-operator", start);
10088
+ return [
10089
+ "Superscript",
10090
+ this.error("missing", start),
10091
+ missingIfEmpty(this.parseGroup())
10092
+ ];
10026
10093
  }
10027
10094
  this.index += n;
10028
10095
  if (typeof def.parse === "function") {
@@ -10129,11 +10196,7 @@ var _Parser = class {
10129
10196
  );
10130
10197
  }
10131
10198
  }
10132
- if (result === null) {
10133
- const num = this.parseNumber();
10134
- if (num !== null)
10135
- result = { num };
10136
- }
10199
+ result ?? (result = this.parseNumber());
10137
10200
  result ?? (result = this.parseEnclosure());
10138
10201
  result ?? (result = this.parseEnvironment(until));
10139
10202
  if (result === null && this.matchAll(this._positiveInfinityTokens))
@@ -11061,10 +11124,65 @@ var LatexSyntax = class _LatexSyntax {
11061
11124
  };
11062
11125
 
11063
11126
  // src/compute-engine/boxed-expression/utils.ts
11064
- var import_complex2 = __toESM(require_complex(), 1);
11127
+ var import_complex2 = __toESM(require_complex());
11128
+
11129
+ // src/compute-engine/numerics/numeric.ts
11130
+ var import_complex = __toESM(require_complex());
11131
+
11132
+ // src/compute-engine/numerics/richardson.ts
11133
+ function extrapolate(f, x0, options = {}) {
11134
+ const {
11135
+ contract = 0.125,
11136
+ step = 1,
11137
+ power = 2,
11138
+ atol = 1e-16,
11139
+ rtol = atol > 0 ? 0 : Math.sqrt(Number.EPSILON),
11140
+ maxeval = 1e6,
11141
+ // Number.MAX_SAFE_INTEGER
11142
+ breaktol = 2
11143
+ } = options;
11144
+ if (!isFinite(x0)) {
11145
+ return extrapolate((u) => f(1 / u), 1 / x0, {
11146
+ rtol,
11147
+ atol,
11148
+ maxeval,
11149
+ contract: Math.abs(contract) > 1 ? 1 / contract : contract,
11150
+ step: 1 / step,
11151
+ power
11152
+ });
11153
+ }
11154
+ let h = step;
11155
+ let invcontract = Math.pow(1 / contract, power);
11156
+ let f0 = f(x0 + h);
11157
+ let neville = [f0];
11158
+ let err = Infinity;
11159
+ let numeval = 1;
11160
+ while (numeval < maxeval) {
11161
+ numeval += 1;
11162
+ h *= contract;
11163
+ neville.push(f(x0 + h));
11164
+ let c = invcontract;
11165
+ let minerr = Infinity;
11166
+ for (let i = neville.length - 2; i >= 0; i--) {
11167
+ const old = neville[i];
11168
+ neville[i] = neville[i + 1] + (neville[i + 1] - neville[i]) / (c - 1);
11169
+ const err_ = Math.abs(neville[i] - old);
11170
+ minerr = Math.min(minerr, err_);
11171
+ if (err_ < err) {
11172
+ f0 = neville[i];
11173
+ err = err_;
11174
+ }
11175
+ c *= invcontract;
11176
+ }
11177
+ if (minerr > breaktol * err || !isFinite(minerr))
11178
+ break;
11179
+ if (err <= Math.max(rtol * Math.abs(f0), atol))
11180
+ break;
11181
+ }
11182
+ return [f0, err];
11183
+ }
11065
11184
 
11066
11185
  // src/compute-engine/numerics/numeric.ts
11067
- var import_complex = __toESM(require_complex(), 1);
11068
11186
  var MACHINE_PRECISION_BITS = 53;
11069
11187
  var MACHINE_PRECISION = Math.log10(
11070
11188
  Math.pow(2, MACHINE_PRECISION_BITS)
@@ -12396,10 +12514,10 @@ function erfInv(x) {
12396
12514
  const x7 = x5 * x2;
12397
12515
  return Math.sqrt(pi) / 2 * (x + pi / 12 * x3 + 7 * pi2 / 480 * x5 + 127 * pi3 / 40320 * x7 + 4369 * pi2 * pi2 / 5806080 * x7 * x2 + 34807 * pi3 * pi2 / 182476800 * x7 * x2 * x2);
12398
12516
  }
12399
- function centeredDiff8thOrder(f, x, h = 1e-4) {
12400
- return (-f(x - 4 * h) + 9 * f(x - 3 * h) - 45 * f(x - 2 * h) + 186 * f(x - h) - 186 * f(x + h) + 45 * f(x + 2 * h) - 9 * f(x + 3 * h) + f(x + 4 * h)) / (60 * h);
12517
+ function centeredDiff8thOrder(f, x, h = 0.1) {
12518
+ return (f(x - 4 * h) / 280 - 4 * f(x - 3 * h) / 105 + f(x - 2 * h) / 5 - 4 * f(x - h) / 5 + 4 * f(x + h) / 5 - f(x + 2 * h) / 5 + 4 * f(x + 3 * h) / 105 - f(x + 4 * h) / 280) / h;
12401
12519
  }
12402
- function monteCarloEstimate(f, a, b, n = 1e4) {
12520
+ function monteCarloEstimate(f, a, b, n = 1e5) {
12403
12521
  let sum2 = 0;
12404
12522
  if (a === -Infinity && b === Infinity) {
12405
12523
  for (let i = 0; i < n; i++) {
@@ -12428,6 +12546,19 @@ function monteCarloEstimate(f, a, b, n = 1e4) {
12428
12546
  }
12429
12547
  return sum2 / n * (b - a);
12430
12548
  }
12549
+ function limit(f, x, dir = 1) {
12550
+ if (dir === 0) {
12551
+ const left = limit(f, x, -1);
12552
+ const right = limit(f, x, 1);
12553
+ if (left === void 0 || right === void 0)
12554
+ return NaN;
12555
+ if (Math.abs(left - right) > 1e-5)
12556
+ return NaN;
12557
+ return (left + right) / 2;
12558
+ }
12559
+ const [val, err] = extrapolate(f, x, { step: dir > 0 ? 1 : -1 });
12560
+ return val;
12561
+ }
12431
12562
 
12432
12563
  // src/compute-engine/numerics/numeric-bigint.ts
12433
12564
  function bigint(a) {
@@ -12748,7 +12879,7 @@ function getWildcardName(s) {
12748
12879
  }
12749
12880
 
12750
12881
  // src/compute-engine/boxed-expression/order.ts
12751
- var import_complex3 = __toESM(require_complex(), 1);
12882
+ var import_complex3 = __toESM(require_complex());
12752
12883
 
12753
12884
  // src/compute-engine/symbolic/polynomials.ts
12754
12885
  function totalDegree(expr) {
@@ -12958,7 +13089,7 @@ function flattenSequence(xs) {
12958
13089
  }
12959
13090
 
12960
13091
  // src/compute-engine/numerics/rationals.ts
12961
- var import_complex4 = __toESM(require_complex(), 1);
13092
+ var import_complex4 = __toESM(require_complex());
12962
13093
  function isRational(x) {
12963
13094
  return x !== null && Array.isArray(x);
12964
13095
  }
@@ -13268,7 +13399,7 @@ function signDiff(lhs, rhs, tolerance) {
13268
13399
  }
13269
13400
 
13270
13401
  // src/compute-engine/symbolic/sum.ts
13271
- var import_complex5 = __toESM(require_complex(), 1);
13402
+ var import_complex5 = __toESM(require_complex());
13272
13403
  var Sum = class {
13273
13404
  constructor(ce, xs, options) {
13274
13405
  // If `false`, the running sums are not calculated
@@ -13704,7 +13835,7 @@ function domainSetsLibrary() {
13704
13835
  }
13705
13836
 
13706
13837
  // src/compute-engine/boxed-expression/abstract-boxed-expression.ts
13707
- var import_complex6 = __toESM(require_complex(), 1);
13838
+ var import_complex6 = __toESM(require_complex());
13708
13839
 
13709
13840
  // src/compute-engine/library/utils.ts
13710
13841
  function normalizeLimits(range) {
@@ -13775,6 +13906,7 @@ var NATIVE_JS_FUNCTIONS = {
13775
13906
  Gcd: "_SYS.gcd",
13776
13907
  // Math.hypot
13777
13908
  Lcm: "_SYS.lcm",
13909
+ Limit: (args, compile2) => `_SYS.limit(${compile2(args[0])}, ${compile2(args[1])})`,
13778
13910
  Ln: "Math.log",
13779
13911
  Log: "Math.log10",
13780
13912
  LogGamma: "_SYS.lngamma",
@@ -13809,7 +13941,7 @@ var NATIVE_JS_FUNCTIONS = {
13809
13941
  },
13810
13942
  Random: "Math.random",
13811
13943
  Round: "Math.round",
13812
- Sqaure: (args, compile2) => {
13944
+ Square: (args, compile2) => {
13813
13945
  const arg = args[0];
13814
13946
  if (arg === null)
13815
13947
  throw new Error("Square: no argument");
@@ -13920,7 +14052,8 @@ var ComputeEngineFunction = class extends Function {
13920
14052
  lngamma: gammaln,
13921
14053
  gcd,
13922
14054
  lcm,
13923
- chop
14055
+ chop,
14056
+ limit
13924
14057
  };
13925
14058
  return new Proxy(this, {
13926
14059
  apply: (target, thisArg, argumentsList) => super.apply(thisArg, [this.sys, ...argumentsList]),
@@ -13967,7 +14100,9 @@ function compileToJavascript(expr) {
13967
14100
  return void 0;
13968
14101
  },
13969
14102
  string: (str) => JSON.stringify(str),
13970
- number: (n) => n.toString()
14103
+ number: (n) => n.toString(),
14104
+ indent: 0,
14105
+ ws: (s) => s ?? ""
13971
14106
  });
13972
14107
  }
13973
14108
  function compileExpr(h, args, prec, target) {
@@ -13992,6 +14127,50 @@ function compileExpr(h, args, prec, target) {
13992
14127
  }
13993
14128
  return op3[1] < prec ? `(${resultStr})` : resultStr;
13994
14129
  }
14130
+ if (h === "Function") {
14131
+ const params = args.slice(1).map((x) => x.symbol);
14132
+ return `((${params.join(", ")}) => ${compile(args[0], {
14133
+ ...target,
14134
+ var: (id) => params.includes(id) ? id : target.var(id)
14135
+ })})`;
14136
+ }
14137
+ if (h === "Declare")
14138
+ return `let ${args[0].symbol}`;
14139
+ if (h === "Assign")
14140
+ return `${args[0].symbol} = ${compile(args[1], target)}`;
14141
+ if (h === "Return")
14142
+ return `return ${compile(args[0], target)}`;
14143
+ if (h === "If") {
14144
+ if (args.length !== 3)
14145
+ throw new Error("If: wrong number of arguments");
14146
+ return `((${compile(args[0], target)}) ? (${compile(
14147
+ args[1],
14148
+ target
14149
+ )}) : (${compile(args[2], target)}))`;
14150
+ }
14151
+ if (h === "Block") {
14152
+ const locals = [];
14153
+ for (const arg of args) {
14154
+ if (arg.head === "Declare")
14155
+ locals.push(arg.ops[0].symbol);
14156
+ }
14157
+ if (args.length === 1 && locals.length === 0)
14158
+ return compile(args[0], target);
14159
+ const result = args.map(
14160
+ (arg) => compile(arg, {
14161
+ ...target,
14162
+ var: (id) => {
14163
+ if (locals.includes(id))
14164
+ return id;
14165
+ return target.var(id);
14166
+ }
14167
+ })
14168
+ );
14169
+ result[result.length - 1] = `return ${result[result.length - 1]}`;
14170
+ return `(() => {${target.ws("\n")}${result.join(
14171
+ `;${target.ws("\n")}`
14172
+ )}${target.ws("\n")}})()`;
14173
+ }
13995
14174
  const fn = target.functions?.(h);
13996
14175
  if (!fn)
13997
14176
  throw new Error(`Unknown function ${h}`);
@@ -14383,7 +14562,7 @@ var _BoxedExpression = class {
14383
14562
  compile(to = "javascript", options) {
14384
14563
  if (to !== "javascript")
14385
14564
  return void 0;
14386
- options ?? (options = { optimize: ["simplify", "evaluate"] });
14565
+ options ?? (options = { optimize: ["simplify"] });
14387
14566
  let expr = this;
14388
14567
  if (options.optimize.includes("simplify"))
14389
14568
  expr = expr.simplify();
@@ -14757,7 +14936,7 @@ function narrow(a, b) {
14757
14936
  }
14758
14937
 
14759
14938
  // src/compute-engine/symbolic/utils.ts
14760
- var import_complex7 = __toESM(require_complex(), 1);
14939
+ var import_complex7 = __toESM(require_complex());
14761
14940
  function makePositive(expr) {
14762
14941
  if (expr.head === "Negate")
14763
14942
  return [-1, expr.op1];
@@ -15312,7 +15491,7 @@ function evalSummation(ce, expr, range, mode) {
15312
15491
  }
15313
15492
 
15314
15493
  // src/compute-engine/symbolic/negate.ts
15315
- var import_complex8 = __toESM(require_complex(), 1);
15494
+ var import_complex8 = __toESM(require_complex());
15316
15495
  function negateLiteral(expr, metadata) {
15317
15496
  let n = expr.numericValue;
15318
15497
  if (n === null)
@@ -15844,10 +16023,10 @@ function isInequality(expr) {
15844
16023
  }
15845
16024
 
15846
16025
  // src/compute-engine/boxed-expression/box.ts
15847
- var import_complex14 = __toESM(require_complex(), 1);
16026
+ var import_complex14 = __toESM(require_complex());
15848
16027
 
15849
16028
  // src/compute-engine/boxed-expression/serialize.ts
15850
- var import_complex10 = __toESM(require_complex(), 1);
16029
+ var import_complex10 = __toESM(require_complex());
15851
16030
 
15852
16031
  // src/compute-engine/numerics/numeric-bignum.ts
15853
16032
  function factorial3(ce, n) {
@@ -15963,7 +16142,7 @@ function isInMachineRange(d) {
15963
16142
  }
15964
16143
 
15965
16144
  // src/compute-engine/symbolic/product.ts
15966
- var import_complex9 = __toESM(require_complex(), 1);
16145
+ var import_complex9 = __toESM(require_complex());
15967
16146
  var Product = class {
15968
16147
  constructor(ce, xs, options) {
15969
16148
  this.options = options;
@@ -16902,7 +17081,7 @@ var BoxedDictionary = class _BoxedDictionary extends _BoxedExpression {
16902
17081
  };
16903
17082
 
16904
17083
  // src/compute-engine/boxed-expression/boxed-function.ts
16905
- var import_complex11 = __toESM(require_complex(), 1);
17084
+ var import_complex11 = __toESM(require_complex());
16906
17085
 
16907
17086
  // src/compute-engine/simplify-rules.ts
16908
17087
  var SIMPLIFY_RULES = [];
@@ -17595,10 +17774,12 @@ var BoxedFunction = class _BoxedFunction extends _BoxedExpression {
17595
17774
  const sig = def?.signature;
17596
17775
  if (!result && sig) {
17597
17776
  const numericMode = options?.numericMode ?? false;
17777
+ const context = this.engine.swapScope(this.scope);
17598
17778
  if (numericMode && sig.N)
17599
17779
  result = sig.N(this.engine, tail);
17600
17780
  if (!result && sig.evaluate)
17601
17781
  result = sig.evaluate(this.engine, tail);
17782
+ this.engine.swapScope(context);
17602
17783
  }
17603
17784
  if (result) {
17604
17785
  const num = result.numericValue;
@@ -17791,10 +17972,10 @@ function cheapest(oldExpr, newExpr) {
17791
17972
  }
17792
17973
 
17793
17974
  // src/compute-engine/boxed-expression/boxed-number.ts
17794
- var import_complex13 = __toESM(require_complex(), 1);
17975
+ var import_complex13 = __toESM(require_complex());
17795
17976
 
17796
17977
  // src/compute-engine/domain-utils.ts
17797
- var import_complex12 = __toESM(require_complex(), 1);
17978
+ var import_complex12 = __toESM(require_complex());
17798
17979
  function inferNumericDomain(value) {
17799
17980
  if (typeof value === "number" && !isNaN(value)) {
17800
17981
  if (!isFinite(value))
@@ -18081,7 +18262,12 @@ var BoxedNumber = class _BoxedNumber extends _BoxedExpression {
18081
18262
  return false;
18082
18263
  }
18083
18264
  isEqual(rhs) {
18084
- return this.isSame(rhs);
18265
+ if (this === rhs)
18266
+ return true;
18267
+ if (!(rhs instanceof _BoxedNumber))
18268
+ return false;
18269
+ const s = signDiff(this, rhs);
18270
+ return s === 0;
18085
18271
  }
18086
18272
  match(rhs, options) {
18087
18273
  if (this.isEqualWithTolerance(rhs, options?.numericTolerance ?? 0))
@@ -18736,7 +18922,7 @@ function asString(expr) {
18736
18922
  }
18737
18923
 
18738
18924
  // src/compute-engine/library/arithmetic.ts
18739
- var import_complex16 = __toESM(require_complex(), 1);
18925
+ var import_complex16 = __toESM(require_complex());
18740
18926
 
18741
18927
  // src/compute-engine/numerics/numeric-complex.ts
18742
18928
  function gamma3(c) {
@@ -18747,7 +18933,7 @@ function gammaln3(c) {
18747
18933
  }
18748
18934
 
18749
18935
  // src/compute-engine/library/arithmetic-power.ts
18750
- var import_complex15 = __toESM(require_complex(), 1);
18936
+ var import_complex15 = __toESM(require_complex());
18751
18937
  function canonicalPower(ce, base, exponent, metadata) {
18752
18938
  if (exponent.symbol === "ComplexInfinity")
18753
18939
  return ce.NaN;
@@ -20162,21 +20348,7 @@ var ARITHMETIC_LIBRARY = [
20162
20348
  return ops2[0];
20163
20349
  return ce.fn("Max", ops2);
20164
20350
  },
20165
- evaluate: (ce, ops2) => {
20166
- if (ops2.length === 0)
20167
- return ce.NegativeInfinity;
20168
- let result = void 0;
20169
- const rest = [];
20170
- for (const op3 of ops2) {
20171
- if (!op3.isNumber || op3.numericValue === void 0)
20172
- rest.push(op3);
20173
- else if (!result || op3.isGreater(result))
20174
- result = op3;
20175
- }
20176
- if (rest.length > 0)
20177
- return ce.box(result ? ["Max", result, ...rest] : ["Max", ...rest]);
20178
- return result ?? ce.NaN;
20179
- }
20351
+ evaluate: (ce, ops2) => processMinMax(ce, ops2, "Max")
20180
20352
  }
20181
20353
  },
20182
20354
  Min: {
@@ -20186,26 +20358,42 @@ var ARITHMETIC_LIBRARY = [
20186
20358
  domain: ["FunctionOf", ["VarArg", "Values"], "Numbers"],
20187
20359
  simplify: (ce, ops2) => {
20188
20360
  if (ops2.length === 0)
20189
- return ce.NegativeInfinity;
20361
+ return ce.PositiveInfinity;
20190
20362
  if (ops2.length === 1)
20191
20363
  return ops2[0];
20192
20364
  return ce.fn("Min", ops2);
20193
20365
  },
20194
- evaluate: (ce, ops2) => {
20366
+ evaluate: (ce, ops2) => processMinMax(ce, ops2, "Min")
20367
+ }
20368
+ },
20369
+ Supremum: {
20370
+ description: "Like Max, but defined for open sets",
20371
+ complexity: 1200,
20372
+ signature: {
20373
+ domain: ["FunctionOf", ["VarArg", "Values"], "Numbers"],
20374
+ simplify: (ce, ops2) => {
20195
20375
  if (ops2.length === 0)
20196
20376
  return ce.NegativeInfinity;
20197
- let result = void 0;
20198
- const rest = [];
20199
- for (const op3 of ops2) {
20200
- if (!op3.isNumber || op3.numericValue === void 0)
20201
- rest.push(op3);
20202
- else if (!result || op3.isLess(result))
20203
- result = op3;
20204
- }
20205
- if (rest.length > 0)
20206
- return ce.box(result ? ["Min", result, ...rest] : ["Min", ...rest]);
20207
- return result ?? ce.NaN;
20208
- }
20377
+ if (ops2.length === 1)
20378
+ return ops2[0];
20379
+ return ce.fn("Min", ops2);
20380
+ },
20381
+ evaluate: (ce, ops2) => processMinMax(ce, ops2, "Supremum")
20382
+ }
20383
+ },
20384
+ Infimum: {
20385
+ description: "Like Min, but defined for open sets",
20386
+ complexity: 1200,
20387
+ signature: {
20388
+ domain: ["FunctionOf", ["VarArg", "Values"], "Numbers"],
20389
+ simplify: (ce, ops2) => {
20390
+ if (ops2.length === 0)
20391
+ return ce.PositiveInfinity;
20392
+ if (ops2.length === 1)
20393
+ return ops2[0];
20394
+ return ce.fn("Min", ops2);
20395
+ },
20396
+ evaluate: (ce, ops2) => processMinMax(ce, ops2, "Infimum")
20209
20397
  }
20210
20398
  },
20211
20399
  Product: {
@@ -20251,6 +20439,69 @@ var ARITHMETIC_LIBRARY = [
20251
20439
  evaluate: (ce, ops2) => evalSummation(ce, ops2[0], ops2[1], "evaluate"),
20252
20440
  N: (ce, ops2) => evalSummation(ce, ops2[0], ops2[1], "N")
20253
20441
  }
20442
+ },
20443
+ // Limits
20444
+ Limit: {
20445
+ description: "Limit of a function",
20446
+ complexity: 5e3,
20447
+ hold: "all",
20448
+ signature: {
20449
+ domain: [
20450
+ "FunctionOf",
20451
+ "Anything",
20452
+ "Numbers",
20453
+ ["OptArg", "Numbers"],
20454
+ "Numbers"
20455
+ ],
20456
+ N: (ce, ops2) => {
20457
+ const [f, x, dir] = ops2;
20458
+ const target = asFloat(x.N());
20459
+ if (target === null)
20460
+ return void 0;
20461
+ const fn = applicable(f);
20462
+ return ce.number(
20463
+ limit(
20464
+ (x2) => {
20465
+ let y = fn([ce.number(x2)])?.valueOf();
20466
+ return typeof y === "number" ? y : Number.NaN;
20467
+ },
20468
+ target,
20469
+ dir ? asFloat(dir) ?? 1 : 1
20470
+ )
20471
+ );
20472
+ }
20473
+ }
20474
+ },
20475
+ NLimit: {
20476
+ description: "Numerical approximation of the limit of a function",
20477
+ complexity: 5e3,
20478
+ hold: "all",
20479
+ signature: {
20480
+ domain: [
20481
+ "FunctionOf",
20482
+ "Anything",
20483
+ "Numbers",
20484
+ ["OptArg", "Numbers"],
20485
+ "Numbers"
20486
+ ],
20487
+ evaluate: (ce, ops2) => {
20488
+ const [f, x, dir] = ops2;
20489
+ const target = asFloat(x.N());
20490
+ if (target === null)
20491
+ return void 0;
20492
+ const fn = applicable(f);
20493
+ return ce.number(
20494
+ limit(
20495
+ (x2) => {
20496
+ let y = fn([ce.number(x2)])?.valueOf();
20497
+ return typeof y === "number" ? y : Number.NaN;
20498
+ },
20499
+ target,
20500
+ dir ? asFloat(dir) ?? 1 : 1
20501
+ )
20502
+ );
20503
+ }
20504
+ }
20254
20505
  }
20255
20506
  },
20256
20507
  //
@@ -20357,6 +20608,24 @@ function processAbs(ce, arg, mode) {
20357
20608
  return ce.neg(arg);
20358
20609
  return void 0;
20359
20610
  }
20611
+ function processMinMax(ce, ops2, mode) {
20612
+ const upper = mode === "Max" || mode === "Supremum";
20613
+ if (ops2.length === 0)
20614
+ return upper ? ce.NegativeInfinity : ce.PositiveInfinity;
20615
+ let result = void 0;
20616
+ const rest = [];
20617
+ for (const op3 of ops2) {
20618
+ if (!op3.isNumber || op3.numericValue === void 0)
20619
+ rest.push(op3);
20620
+ else if (!result)
20621
+ result = op3;
20622
+ else if (upper && op3.isGreater(result) || !upper && op3.isLess(result))
20623
+ result = op3;
20624
+ }
20625
+ if (rest.length > 0)
20626
+ return ce.box(result ? [mode, result, ...rest] : [mode, ...rest]);
20627
+ return result ?? (upper ? ce.NegativeInfinity : ce.PositiveInfinity);
20628
+ }
20360
20629
 
20361
20630
  // src/compute-engine/symbolic/derivative.ts
20362
20631
  var DERIVATIVES_TABLE = {
@@ -20681,8 +20950,9 @@ var CALCULUS_LIBRARY = [
20681
20950
  const params = ops2.slice(1);
20682
20951
  f.bind();
20683
20952
  f = f.canonical;
20953
+ const result = ce._fn("D", [f, ...params]);
20684
20954
  ce.popScope();
20685
- return ce._fn("D", [f, ...params]);
20955
+ return result;
20686
20956
  },
20687
20957
  evaluate: (ce, ops2) => {
20688
20958
  let f = ops2[0].canonical;
@@ -20716,7 +20986,7 @@ var CALCULUS_LIBRARY = [
20716
20986
  if (typeof x !== "number")
20717
20987
  return void 0;
20718
20988
  const f = applicableN1(ce.box(ops2[0]));
20719
- return ce.number(centeredDiff8thOrder(f, x, 1e-6));
20989
+ return ce.number(centeredDiff8thOrder(f, x));
20720
20990
  }
20721
20991
  }
20722
20992
  },
@@ -20772,11 +21042,17 @@ var CALCULUS_LIBRARY = [
20772
21042
  signature: {
20773
21043
  domain: ["FunctionOf", "Functions", "Numbers", "Numbers", "Numbers"],
20774
21044
  evaluate: (ce, ops2) => {
21045
+ const numericMode = ce.numericMode;
21046
+ const precision = ce.precision;
21047
+ ce.numericMode = "machine";
20775
21048
  const f = applicableN1(ops2[0]);
20776
21049
  const [a, b] = ops2.slice(1).map((op3) => op3.valueOf());
20777
- if (typeof a !== "number" || typeof b !== "number")
20778
- return void 0;
20779
- return ce.number(monteCarloEstimate(f, a, b));
21050
+ let result = void 0;
21051
+ if (typeof a === "number" && typeof b === "number")
21052
+ result = ce.number(monteCarloEstimate(f, a, b));
21053
+ ce.numericMode = numericMode;
21054
+ ce.precision = precision;
21055
+ return result;
20780
21056
  }
20781
21057
  }
20782
21058
  }
@@ -21541,6 +21817,7 @@ function indexes(ranges) {
21541
21817
  var CONTROL_STRUCTURES_LIBRARY = [
21542
21818
  {
21543
21819
  Block: {
21820
+ hold: "all",
21544
21821
  signature: {
21545
21822
  domain: "Functions",
21546
21823
  canonical: canonicalBlock,
@@ -21642,33 +21919,37 @@ function whichEvaluate(ce, args, mode) {
21642
21919
  function evaluateBlock(ce, ops2) {
21643
21920
  if (ops2.length === 0)
21644
21921
  return ce.Nothing;
21922
+ ce.resetContext();
21645
21923
  let result = void 0;
21646
21924
  for (const op3 of ops2) {
21647
- result = op3.evaluate();
21648
- const h = result.head;
21649
- if (h === "Return" || h === "Break" || h === "Continue")
21925
+ const h = op3.head;
21926
+ if (h === "Return") {
21927
+ result = op3.op1.evaluate();
21650
21928
  break;
21929
+ }
21930
+ if (h === "Break" || h === "Continue") {
21931
+ result = ce.fn(h, [op3.op1.evaluate()]);
21932
+ break;
21933
+ }
21934
+ result = op3.evaluate();
21651
21935
  }
21652
21936
  return result ?? ce.Nothing;
21653
21937
  }
21654
21938
  function canonicalBlock(ce, ops2) {
21655
21939
  if (ops2.length === 0)
21656
21940
  return null;
21941
+ ce.pushScope();
21657
21942
  const declarations = [];
21658
21943
  const body = [];
21659
21944
  for (const op3 of ops2) {
21660
- if (op3.head === "Declare") {
21945
+ if (op3.head === "Declare")
21661
21946
  declarations.push(op3);
21662
- } else if (op3.head === "Assign") {
21663
- const id = op3.op1.symbol;
21664
- const def = ce.lookupSymbol(id) ?? ce.lookupFunction(id);
21665
- if (!def) {
21666
- declarations.push(ce._fn("Declare", [op3.op1, ce.symbol("Undefined")]));
21667
- }
21668
- } else
21947
+ else
21669
21948
  body.push(invalidateDeclare(op3));
21670
21949
  }
21671
- return ce._fn("Block", [...declarations, ...body]);
21950
+ const result = ce._fn("Block", [...declarations, ...body]);
21951
+ ce.popScope();
21952
+ return result;
21672
21953
  }
21673
21954
  function invalidateDeclare(expr) {
21674
21955
  if (expr.head === "Declare")
@@ -22033,7 +22314,8 @@ var CORE_LIBRARY = [
22033
22314
  },
22034
22315
  // By definition, for arguments of the canonical expression of
22035
22316
  // `Hold` are not canonicalized.
22036
- canonical: (ce, args) => args.length !== 1 ? null : ce._fn("Hold", args)
22317
+ canonical: (ce, args) => args.length !== 1 ? null : ce._fn("Hold", args),
22318
+ evaluate: (ce, ops2) => ops2[0]
22037
22319
  }
22038
22320
  },
22039
22321
  HorizontalSpacing: {
@@ -22073,6 +22355,32 @@ var CORE_LIBRARY = [
22073
22355
  evaluate: (_ce, ops2) => apply3(ops2[0], ops2.slice(1))
22074
22356
  }
22075
22357
  },
22358
+ Assign: {
22359
+ hold: "all",
22360
+ signature: {
22361
+ domain: ["FunctionOf", "Symbols", "Anything", "Anything"],
22362
+ canonical: (ce, args) => {
22363
+ if (args.length !== 2)
22364
+ return null;
22365
+ const op12 = args[0];
22366
+ const op22 = args[1];
22367
+ if (!op12.symbol)
22368
+ return null;
22369
+ if (op22.symbol)
22370
+ return ce._fn("Assign", args);
22371
+ return ce._fn("Assign", [op12, ce._fn("Hold", [op22])]);
22372
+ },
22373
+ evaluate: (ce, ops2) => {
22374
+ const op12 = ops2[0];
22375
+ const op22 = ops2[1];
22376
+ if (!op12.symbol)
22377
+ return ce.Nothing;
22378
+ const val = op22.evaluate();
22379
+ ce.assign(op12.symbol, val);
22380
+ return val;
22381
+ }
22382
+ }
22383
+ },
22076
22384
  Assume: {
22077
22385
  hold: "all",
22078
22386
  signature: {
@@ -22082,6 +22390,32 @@ var CORE_LIBRARY = [
22082
22390
  },
22083
22391
  // @todo
22084
22392
  About: { signature: { domain: "Functions" } },
22393
+ Declare: {
22394
+ hold: "all",
22395
+ signature: {
22396
+ domain: ["FunctionOf", "Symbols", "Anything"],
22397
+ canonical: (ce, args) => {
22398
+ if (args.length !== 2)
22399
+ return null;
22400
+ const op12 = args[0];
22401
+ const op22 = args[1];
22402
+ if (!op12.symbol)
22403
+ return null;
22404
+ if (op22.symbol)
22405
+ return ce._fn("Declare", args);
22406
+ return ce._fn("Declare", [op12, ce._fn("Hold", [op22])]);
22407
+ },
22408
+ evaluate: (ce, ops2) => {
22409
+ const op12 = ops2[0];
22410
+ const op22 = ops2[1];
22411
+ if (!op12.symbol)
22412
+ return ce.Nothing;
22413
+ const val = op22.evaluate();
22414
+ ce.declare(op12.symbol, val);
22415
+ return val;
22416
+ }
22417
+ }
22418
+ },
22085
22419
  Domain: {
22086
22420
  /** Return the domain of an expression */
22087
22421
  signature: {
@@ -24188,7 +24522,7 @@ function evalTrig(ce, mode, head2, op3) {
24188
24522
  }
24189
24523
 
24190
24524
  // src/compute-engine/boxed-expression/boxed-symbol-definition.ts
24191
- var import_complex17 = __toESM(require_complex(), 1);
24525
+ var import_complex17 = __toESM(require_complex());
24192
24526
  var _BoxedSymbolDefinition = class {
24193
24527
  // @todo
24194
24528
  constructor(ce, name, def) {
@@ -25138,7 +25472,7 @@ function isFunctionDefinition(def) {
25138
25472
  }
25139
25473
 
25140
25474
  // src/compute-engine/cost-function.ts
25141
- var import_complex19 = __toESM(require_complex(), 1);
25475
+ var import_complex19 = __toESM(require_complex());
25142
25476
  function numericCostFunction(n) {
25143
25477
  if (Number.isInteger(n) && n !== 0) {
25144
25478
  return Math.floor(Math.log2(Math.abs(n)) / Math.log2(10)) + (n > 0 ? 1 : 2);
@@ -25530,6 +25864,16 @@ function match(subject, pattern, options) {
25530
25864
  }
25531
25865
  if (!options.recursive)
25532
25866
  return null;
25867
+ if (subject.ops) {
25868
+ const ops2 = subject.ops;
25869
+ const result = {};
25870
+ for (let i = 0; i < ops2.length; i++) {
25871
+ const sub2 = match(ops2[i], pattern, options);
25872
+ if (sub2 !== null)
25873
+ return sub2;
25874
+ }
25875
+ return result;
25876
+ }
25533
25877
  return null;
25534
25878
  }
25535
25879
 
@@ -27295,10 +27639,10 @@ var ComputeEngine = class _ComputeEngine {
27295
27639
  };
27296
27640
 
27297
27641
  // src/compute-engine.ts
27298
- var version = "0.17.0";
27642
+ var version = "0.18.1";
27299
27643
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
27300
27644
  ComputeEngine: ComputeEngine.prototype.constructor,
27301
- version: "0.17.0"
27645
+ version: "0.18.1"
27302
27646
  };
27303
27647
  export {
27304
27648
  ADDITION_PRECEDENCE,