@cortex-js/compute-engine 0.26.0 → 0.26.3

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 (139) hide show
  1. package/README.md +9 -10
  2. package/dist/compute-engine.esm.js +160 -81
  3. package/dist/compute-engine.js +160 -81
  4. package/dist/compute-engine.min.esm.js +37 -36
  5. package/dist/compute-engine.min.js +37 -36
  6. package/dist/math-json.esm.js +2 -2
  7. package/dist/math-json.js +2 -2
  8. package/dist/math-json.min.esm.js +2 -2
  9. package/dist/math-json.min.js +2 -2
  10. package/dist/types/common/ansi-codes.d.ts +1 -1
  11. package/dist/types/common/buffer.d.ts +1 -1
  12. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  13. package/dist/types/common/one-of.d.ts +1 -1
  14. package/dist/types/common/signals.d.ts +1 -1
  15. package/dist/types/common/styled-text.d.ts +1 -1
  16. package/dist/types/common/suggest.d.ts +1 -1
  17. package/dist/types/common/syntax-highlighter.d.ts +1 -1
  18. package/dist/types/common/terminal.d.ts +1 -1
  19. package/dist/types/common/type/parse.d.ts +1 -1
  20. package/dist/types/common/type/primitive.d.ts +1 -1
  21. package/dist/types/common/type/serialize.d.ts +1 -1
  22. package/dist/types/common/type/subtype.d.ts +1 -1
  23. package/dist/types/common/type/types.d.ts +1 -1
  24. package/dist/types/common/type/utils.d.ts +1 -1
  25. package/dist/types/common/utils.d.ts +1 -1
  26. package/dist/types/compute-engine/assume.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +2 -1
  28. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  30. package/dist/types/compute-engine/boxed-expression/arithmetic-divide.d.ts +1 -1
  31. package/dist/types/compute-engine/boxed-expression/arithmetic-multiply.d.ts +1 -1
  32. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  33. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  34. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  35. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  36. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +1 -1
  37. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +2 -1
  38. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  39. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  40. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  41. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  42. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +1 -1
  43. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  44. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  45. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  46. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  47. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  48. package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
  49. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  50. package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
  51. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  52. package/dist/types/compute-engine/boxed-expression/negate.d.ts +2 -1
  53. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  54. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/product.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/public.d.ts +7 -2
  58. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  60. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/tensor-fields.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/terms.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  66. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  68. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  69. package/dist/types/compute-engine/compile.d.ts +1 -1
  70. package/dist/types/compute-engine/compute-engine.d.ts +2 -2
  71. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  72. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  73. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  74. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  75. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  76. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  77. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  78. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  79. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  80. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  81. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  82. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  83. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  84. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  85. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  86. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  87. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  88. package/dist/types/compute-engine/latex-syntax/parse.d.ts +2 -2
  89. package/dist/types/compute-engine/latex-syntax/public.d.ts +3 -1
  90. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  91. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  92. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
  93. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  94. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  95. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  96. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  97. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  98. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  99. package/dist/types/compute-engine/library/core.d.ts +1 -1
  100. package/dist/types/compute-engine/library/invisible-operator.d.ts +1 -1
  101. package/dist/types/compute-engine/library/library.d.ts +1 -1
  102. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  103. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  104. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  105. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  106. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  107. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  108. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  109. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  110. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  111. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  112. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  113. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  114. package/dist/types/compute-engine/numeric-value/public.d.ts +1 -1
  115. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  116. package/dist/types/compute-engine/numerics/bignum.d.ts +1 -1
  117. package/dist/types/compute-engine/numerics/expression.d.ts +2 -2
  118. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  119. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  120. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  121. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  122. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  123. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  124. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  125. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  126. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  127. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  128. package/dist/types/compute-engine/numerics/strings.d.ts +2 -2
  129. package/dist/types/compute-engine/public.d.ts +1 -1
  130. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  131. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  132. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  133. package/dist/types/compute-engine/tensor/tensors.d.ts +1 -1
  134. package/dist/types/compute-engine.d.ts +2 -2
  135. package/dist/types/math-json/identifiers.d.ts +1 -1
  136. package/dist/types/math-json/types.d.ts +1 -1
  137. package/dist/types/math-json/utils.d.ts +1 -1
  138. package/dist/types/math-json.d.ts +2 -2
  139. package/package.json +1 -1
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  notation interchange format based on JSON.
10
10
 
11
11
  The Cortex Compute Engine can parse LaTeX to MathJSON, serialize MathJSON to
12
- LaTeX, format, simplify and evaluate MathJSON expressions.
12
+ LaTeX or MathASCII, format, simplify and evaluate MathJSON expressions.
13
13
 
14
14
  Reference documentation and guides at
15
15
  [cortexjs.io/compute-engine](https://cortexjs.io/compute-engine/).
@@ -23,20 +23,19 @@ $ npm install --save @cortex-js/compute-engine
23
23
  ```
24
24
 
25
25
  ```js
26
- import { parse, evaluate } from "@cortex-js/compute-engine";
26
+ import { ComputeEngine } from "@cortex-js/compute-engine";
27
27
 
28
- const expr = parse("2^{11}-1 \\in \\P");
28
+ const ce = new ComputeEngine();
29
29
 
30
- console.log(expr);
31
- // ➔ ["Element", ["Subtract", ["Power", 2, 11] , 1], "PrimeNumber"]
32
-
33
- console.log(evaluate(expr));
34
- // ➔ "False"
30
+ ce.parse("2^{11}-1 \\in \\Z").evaluate().print()
31
+ // ➔ "True"
35
32
  ```
36
33
 
37
- ## More
34
+ ## FAQ
35
+
36
+ **Q** How do I build the project?
38
37
 
39
- - [Build](BUILD.md) instructions
38
+ [Build](BUILD.md) instructions
40
39
 
41
40
  ## Related Projects
42
41
 
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.26.0 */
1
+ /** Compute Engine 0.26.3 */
2
2
 
3
3
  // src/compute-engine/boxed-expression/public.ts
4
4
  function isRuleStep(x) {
@@ -4022,6 +4022,7 @@ Invalid type
4022
4022
  function parseType(s) {
4023
4023
  if (s === void 0) return void 0;
4024
4024
  if (isValidType(s)) return s;
4025
+ if (typeof s !== "string") return void 0;
4025
4026
  if (PRIMITIVE_TYPES.includes(s)) return s;
4026
4027
  const parser = new TypeParser(s);
4027
4028
  return parser.parse();
@@ -4597,6 +4598,7 @@ var ARROW_PRECEDENCE = 270;
4597
4598
  var ADDITION_PRECEDENCE = 275;
4598
4599
  var MULTIPLICATION_PRECEDENCE = 390;
4599
4600
  var DIVISION_PRECEDENCE = 600;
4601
+ var EXPONENTIATION_PRECEDENCE = 700;
4600
4602
  var POSTFIX_PRECEDENCE = 810;
4601
4603
  function isExpressionEntry(entry) {
4602
4604
  return !("kind" in entry) || entry.kind === "expression";
@@ -6458,20 +6460,20 @@ function isSymbolDefinition(def) {
6458
6460
  if (def === void 0 || def === null || typeof def !== "object")
6459
6461
  return false;
6460
6462
  if (isBoxedExpression(def)) return false;
6461
- if ("type" in def || "value" in def || "constant" in def) {
6462
- if (typeof def.type === "function") {
6463
+ if ("value" in def || "constant" in def || "inferred" in def) {
6464
+ if ("type" in def && typeof def.type === "function") {
6463
6465
  throw new Error(
6464
- "The type field of a symbol definition should be a type string"
6466
+ "The `type` field of a symbol definition should be of type `string`"
6465
6467
  );
6466
6468
  }
6467
6469
  if ("signature" in def) {
6468
6470
  throw new Error(
6469
- "Symbol definition cannot have a signature field. Use a type field instead."
6471
+ "Symbol definition cannot have a `signature` field. Use a `type` field instead."
6470
6472
  );
6471
6473
  }
6472
6474
  if ("sgn" in def) {
6473
6475
  throw new Error(
6474
- "Symbol definition cannot have a sgn field. Use a flags field instead."
6476
+ "Symbol definition cannot have a `sgn` field. Use a `flags.sgn` field instead."
6475
6477
  );
6476
6478
  }
6477
6479
  return true;
@@ -6485,22 +6487,23 @@ function isFunctionDefinition(def) {
6485
6487
  if ("signature" in def || "complexity" in def) {
6486
6488
  if ("constant" in def) {
6487
6489
  throw new Error(
6488
- "Function definition cannot have a constant field and symbol definition cannot have a signature field."
6490
+ "Function definition cannot have a `constant` field and symbol definition cannot have a `signature` field."
6489
6491
  );
6490
6492
  }
6491
- if ("type" in def && typeof def.type !== "function") {
6492
- throw new Error(
6493
- "The type field of a function definition should be a function"
6494
- );
6495
- }
6496
- if ("sgn" in def && typeof def.sgn !== "function") {
6497
- throw new Error(
6498
- "The sgn field of a function definition should be a function"
6499
- );
6500
- }
6501
- return true;
6502
6493
  }
6503
- return false;
6494
+ if (!("evaluate" in def) && !("signature" in def) && !("sgn" in def) && !("complexity" in def) && !("canonical" in def))
6495
+ return false;
6496
+ if ("type" in def && typeof def.type !== "function") {
6497
+ throw new Error(
6498
+ "The `type` field of a function definition should be a function"
6499
+ );
6500
+ }
6501
+ if ("sgn" in def && typeof def.sgn !== "function") {
6502
+ throw new Error(
6503
+ "The `sgn` field of a function definition should be a function"
6504
+ );
6505
+ }
6506
+ return true;
6504
6507
  }
6505
6508
  function semiCanonical(ce, xs) {
6506
6509
  if (!xs.every((x) => isBoxedExpression(x))) return xs.map((x) => ce.box(x));
@@ -7502,7 +7505,9 @@ function fromDigits(s, baseInput) {
7502
7505
  }
7503
7506
  return [value, ""];
7504
7507
  }
7505
- function numberToString(num) {
7508
+ function numberToString(num, fractionalDigits) {
7509
+ if (typeof fractionalDigits === "number" && typeof num === "number")
7510
+ return num.toFixed(fractionalDigits);
7506
7511
  const numStr = num.toString();
7507
7512
  if (typeof num === "number" && Number.isInteger(num) && numStr.includes("e")) {
7508
7513
  const fixedStr = BigInt(num).toString();
@@ -7534,11 +7539,13 @@ function bigintValue(expr) {
7534
7539
  if (s === "-infinity") return null;
7535
7540
  return bigint(s);
7536
7541
  }
7537
- function numberToExpression(num) {
7542
+ function numberToExpression(num, fractionalDigits) {
7538
7543
  if (typeof num === "number") {
7539
7544
  if (isNaN(num)) return "NaN";
7540
7545
  if (!Number.isFinite(num))
7541
7546
  return num < 0 ? "NegativeInfinity" : "PositiveInfinity";
7547
+ if (typeof fractionalDigits === "number")
7548
+ return { num: num.toFixed(fractionalDigits) };
7542
7549
  return num;
7543
7550
  }
7544
7551
  if (num >= Number.MIN_SAFE_INTEGER && num <= Number.MAX_SAFE_INTEGER)
@@ -7672,7 +7679,7 @@ var ExactNumericValue = class _ExactNumericValue extends NumericValue {
7672
7679
  return result.mul(this.bignum(this.radical).sqrt());
7673
7680
  }
7674
7681
  get numerator() {
7675
- if (this.rational[1] === 1) return this;
7682
+ if (this.rational[1] == 1) return this;
7676
7683
  return this.clone({
7677
7684
  rational: isMachineRational(this.rational) ? [this.rational[0], 1] : [this.rational[0], BigInt(1)],
7678
7685
  radical: this.radical
@@ -7714,10 +7721,10 @@ var ExactNumericValue = class _ExactNumericValue extends NumericValue {
7714
7721
  return Number.isNaN(this.rational[0]);
7715
7722
  }
7716
7723
  get isPositiveInfinity() {
7717
- return this.rational[0] === Infinity;
7724
+ return this.rational[0] == Infinity;
7718
7725
  }
7719
7726
  get isNegativeInfinity() {
7720
- return this.rational[0] === -Infinity;
7727
+ return this.rational[0] == -Infinity;
7721
7728
  }
7722
7729
  get isComplexInfinity() {
7723
7730
  return false;
@@ -7859,7 +7866,7 @@ var ExactNumericValue = class _ExactNumericValue extends NumericValue {
7859
7866
  exponent = { re: exponent.re, im: exponent.im };
7860
7867
  } else {
7861
7868
  if (exponent instanceof _ExactNumericValue) {
7862
- if (exponent.radical === 1 && exponent.rational[0] === 1)
7869
+ if (exponent.radical === 1 && exponent.rational[0] == 1)
7863
7870
  return this.root(exponent.rational[0]);
7864
7871
  }
7865
7872
  exponent = exponent.re;
@@ -7941,7 +7948,7 @@ var ExactNumericValue = class _ExactNumericValue extends NumericValue {
7941
7948
  return this.factory({ im: Math.pow(-this.re, 1 / exponent) });
7942
7949
  if (this.radical > SMALL_INTEGER || this.rational[0] > SMALL_INTEGER || this.rational[0] < -SMALL_INTEGER || this.rational[1] > SMALL_INTEGER)
7943
7950
  return this.factory(this.bignumRe).root(exponent);
7944
- if (this.rational[1] === 1) {
7951
+ if (this.rational[1] == 1) {
7945
7952
  const root2 = Math.pow(this.rational[0], 1 / exponent);
7946
7953
  if (Number.isInteger(root2)) return this.clone(root2);
7947
7954
  }
@@ -8007,7 +8014,7 @@ var ExactNumericValue = class _ExactNumericValue extends NumericValue {
8007
8014
  }
8008
8015
  eq(other) {
8009
8016
  if (typeof other === "number")
8010
- return this.radical === 1 && isInteger(this.rational) && this.rational[0] === other;
8017
+ return this.radical === 1 && isInteger(this.rational) && this.rational[0] == other;
8011
8018
  if (other instanceof _ExactNumericValue) {
8012
8019
  return this.radical === other.radical && this.rational[0] == other.rational[0] && this.rational[1] == other.rational[1];
8013
8020
  }
@@ -8171,7 +8178,7 @@ function asSmallInteger(expr) {
8171
8178
  // src/compute-engine/boxed-expression/polynomials.ts
8172
8179
  function totalDegree(expr) {
8173
8180
  if (expr.symbol && !expr.isConstant) return 1;
8174
- if (expr.operator === "Power" && expr.op2.numericValue !== null) {
8181
+ if (expr.operator === "Power" && expr.op2.isNumberLiteral) {
8175
8182
  if (totalDegree(expr.op1) === 0) return 0;
8176
8183
  const deg = asSmallInteger(expr.op2);
8177
8184
  if (deg !== null && deg > 0) return deg;
@@ -8196,7 +8203,7 @@ function totalDegree(expr) {
8196
8203
  }
8197
8204
  function maxDegree(expr) {
8198
8205
  if (expr.symbol && !expr.isConstant) return 1;
8199
- if (expr.operator === "Power" && expr.op2.numericValue !== null) {
8206
+ if (expr.operator === "Power" && expr.op2.isNumberLiteral) {
8200
8207
  if (maxDegree(expr.op1) === 0) return 0;
8201
8208
  const deg = asSmallInteger(expr.op2);
8202
8209
  if (deg !== null && deg > 0) return deg;
@@ -9505,6 +9512,16 @@ function expandAll(expr) {
9505
9512
  }
9506
9513
 
9507
9514
  // src/compute-engine/boxed-expression/negate.ts
9515
+ function canonicalNegate(expr) {
9516
+ let sign2 = -1;
9517
+ while (expr.operator === "Negate") {
9518
+ expr = expr.op1;
9519
+ sign2 = -sign2;
9520
+ }
9521
+ if (sign2 === 1) return expr;
9522
+ if (expr.isNumberLiteral) return expr.neg();
9523
+ return expr.engine._fn("Negate", [expr]);
9524
+ }
9508
9525
  function negate(expr) {
9509
9526
  let sign2 = -1;
9510
9527
  while (expr.operator === "Negate") {
@@ -9517,9 +9534,7 @@ function negate(expr) {
9517
9534
  if (expr.operator === "Subtract") return expr.op2.sub(expr.op1);
9518
9535
  if (expr.operator === "Add") return add3(...expr.ops.map((x) => negate(x)));
9519
9536
  if (expr.operator === "Multiply") return negateProduct(ce, expr.ops);
9520
- if (expr.operator === "Divide") {
9521
- return negate(expr.op1).div(expr.op2);
9522
- }
9537
+ if (expr.operator === "Divide") return negate(expr.op1).div(expr.op2);
9523
9538
  return ce._fn("Negate", [expr]);
9524
9539
  }
9525
9540
  function negateProduct(ce, args) {
@@ -9581,7 +9596,10 @@ function canonicalMultiply(ce, ops) {
9581
9596
  if (next.operator === "Sqrt" && next.op1.isNumberLiteral && isSubtype(next.op1.type, "finite_integer")) {
9582
9597
  let radical = next.op1.numericValue;
9583
9598
  if (typeof radical !== "number") radical = radical.re;
9584
- if (radical >= SMALL_INTEGER) continue;
9599
+ if (radical >= SMALL_INTEGER) {
9600
+ ys.push(x);
9601
+ continue;
9602
+ }
9585
9603
  if (isSubtype(x.type, "finite_rational")) {
9586
9604
  const rational = x.numericValue;
9587
9605
  const [num, den] = typeof rational === "number" ? [rational, 1] : [rational.numerator.re, rational.denominator.re];
@@ -9674,6 +9692,7 @@ function mulN(...xs) {
9674
9692
  function canonicalDivide(op1, op2) {
9675
9693
  const ce = op1.engine;
9676
9694
  if (!op1.isValid || !op2.isValid) return ce._fn("Divide", [op1, op2]);
9695
+ if (op1.isNaN || op2.isNaN) return ce.NaN;
9677
9696
  if (op2.is(0)) return op1.is(0) ? ce.NaN : ce.ComplexInfinity;
9678
9697
  if (op1.is(0)) return ce.Zero;
9679
9698
  if (op2.is(0) === false) {
@@ -9873,9 +9892,11 @@ var Product = class _Product {
9873
9892
  return;
9874
9893
  }
9875
9894
  if (term.isInfinity) {
9876
- this.coefficient = this.engine._numericValue(
9877
- term.isNegative ? -Infinity : Infinity
9878
- );
9895
+ if (isOne(exp2)) {
9896
+ this.coefficient = this.engine._numericValue(
9897
+ term.isNegative ? -Infinity : Infinity
9898
+ );
9899
+ } else this.terms.push({ term, exponent: exp2 });
9879
9900
  return;
9880
9901
  }
9881
9902
  if (isOne(exp2)) {
@@ -10030,8 +10051,15 @@ var Product = class _Product {
10030
10051
  const ce = this.engine;
10031
10052
  const coef = this.coefficient;
10032
10053
  if (coef.isZero) return [ce.Zero, ce.One];
10033
- if (coef.isPositiveInfinity || coef.isNegativeInfinity)
10054
+ if (coef.isPositiveInfinity || coef.isNegativeInfinity) {
10055
+ if (this.terms.length === 0) {
10056
+ return [
10057
+ coef.isPositiveInfinity ? ce.PositiveInfinity : ce.NegativeInfinity,
10058
+ ce.One
10059
+ ];
10060
+ }
10034
10061
  return [ce.NaN, ce.NaN];
10062
+ }
10035
10063
  const isNegativeOne2 = coef.isNegativeOne;
10036
10064
  if (isNegativeOne2) this.coefficient = ce._numericValue(1);
10037
10065
  const xs = this.groupedByDegrees({ mode: "rational" });
@@ -10598,13 +10626,23 @@ function evalTrig(name, op) {
10598
10626
  (x) => x.sinh(),
10599
10627
  (x) => x.sinh()
10600
10628
  );
10601
- case "Tan":
10602
- return applyAngle(
10629
+ case "Tan": {
10630
+ const result = applyAngle(
10603
10631
  op,
10604
- Math.tan,
10605
- (x) => x.toSignificantDigits(ce.precision + 4).tan().toSignificantDigits(ce.precision),
10632
+ (x) => {
10633
+ const y = Math.tan(x);
10634
+ if (y > 1e6 || y < -1e6) return ce.ComplexInfinity;
10635
+ return y;
10636
+ },
10637
+ (x) => {
10638
+ const y = x.tan();
10639
+ if (y.greaterThan(1e6) || y.lessThan(-1e6)) return ce.ComplexInfinity;
10640
+ return y;
10641
+ },
10606
10642
  (x) => x.tan()
10607
10643
  );
10644
+ return result;
10645
+ }
10608
10646
  case "Tanh":
10609
10647
  return applyAngle(
10610
10648
  op,
@@ -10838,9 +10876,11 @@ function canonicalRoot(a, b) {
10838
10876
  if (exp2 === 1) return a;
10839
10877
  if (exp2 === 2) {
10840
10878
  if (a.isNumberLiteral && isSubtype(a.type, "rational")) {
10841
- const v = a.sqrt();
10842
- if (typeof v.numericValue === "number") return v;
10843
- if (v.numericValue.isExact) return v;
10879
+ if (a.re < SMALL_INTEGER) {
10880
+ const v = a.sqrt();
10881
+ if (typeof v.numericValue === "number") return v;
10882
+ if (v.numericValue.isExact) return v;
10883
+ }
10844
10884
  }
10845
10885
  return ce._fn("Sqrt", [a]);
10846
10886
  }
@@ -11608,10 +11648,9 @@ function makeLambda(expr) {
11608
11648
  ce.resetContext();
11609
11649
  let i = 0;
11610
11650
  for (const param of params) ce.assign(param, args[i++]);
11611
- const result = fn.N() ?? fn.evaluate();
11651
+ const result = fn.evaluate();
11612
11652
  ce.swapScope(context);
11613
- if (!result.isValid) return void 0;
11614
- return result;
11653
+ return result.isValid ? result : void 0;
11615
11654
  };
11616
11655
  }
11617
11656
  function apply3(fn, args) {
@@ -13041,7 +13080,7 @@ var ARITHMETIC_LIBRARY = [
13041
13080
  sgn: ([x]) => x.isNonNegative ? "positive" : x.isNegative || x.isReal === false ? "unsigned" : void 0,
13042
13081
  canonical: (args, { engine }) => {
13043
13082
  const x = args[0];
13044
- if (x.numericValue !== null && x.isNegative)
13083
+ if (x.isNumberLiteral && x.isNegative)
13045
13084
  return engine._fn("Factorial", [x.neg()]).neg();
13046
13085
  return engine._fn("Factorial", [x]);
13047
13086
  },
@@ -13408,7 +13447,7 @@ var ARITHMETIC_LIBRARY = [
13408
13447
  const [base, exp2] = args;
13409
13448
  return canonicalRoot(base, exp2);
13410
13449
  },
13411
- evaluate: ([x, n], { numericApproximation }) => x.root(n)
13450
+ evaluate: ([x, n], { numericApproximation }) => root(x, n, { numericApproximation })
13412
13451
  },
13413
13452
  Round: {
13414
13453
  complexity: 1250,
@@ -18124,6 +18163,9 @@ function serializeRepeatingDecimals(s, options) {
18124
18163
  fractionalPart += lastDigit;
18125
18164
  while (fractionalPart.endsWith("0"))
18126
18165
  fractionalPart = fractionalPart.slice(0, -1);
18166
+ if (typeof options.fractionalDigits === "number") {
18167
+ fractionalPart = fractionalPart.slice(0, options.fractionalDigits);
18168
+ }
18127
18169
  if (exponent)
18128
18170
  return `${wholepart}.${fractionalPart}${exponent.toLowerCase()}`;
18129
18171
  return `${wholepart}.${fractionalPart}`;
@@ -18278,7 +18320,7 @@ function serializeJsonNumber(ce, value, options, metadata) {
18278
18320
  metadata.latex = metadata.latex ?? ce.box({ num: value.toString() }).latex;
18279
18321
  if (metadata.latex !== void 0)
18280
18322
  return { num: value.toString(), latex: metadata.latex };
18281
- return shorthandAllowed ? numberToExpression(value) : { num: numberToString(value) };
18323
+ return shorthandAllowed ? numberToExpression(value, options.fractionalDigits) : { num: numberToString(value, options.fractionalDigits) };
18282
18324
  }
18283
18325
  }
18284
18326
  let result;
@@ -18440,6 +18482,8 @@ var _BoxedExpression = class {
18440
18482
  }
18441
18483
  if (options.fractionalDigits === "auto")
18442
18484
  defaultOptions.fractionalDigits = -this.engine.precision;
18485
+ if (typeof options.fractionalDigits === "number")
18486
+ defaultOptions.fractionalDigits = options.fractionalDigits;
18443
18487
  }
18444
18488
  const opts = {
18445
18489
  ...defaultOptions,
@@ -18496,6 +18540,7 @@ var _BoxedExpression = class {
18496
18540
  };
18497
18541
  if (options?.fractionalDigits === "auto")
18498
18542
  effectiveOptions.fractionalDigits = -this.engine.precision;
18543
+ else effectiveOptions.fractionalDigits = options?.fractionalDigits ?? "max";
18499
18544
  if (typeof effectiveOptions.fractionalDigits === "number" && effectiveOptions.fractionalDigits > this.engine.precision)
18500
18545
  effectiveOptions.fractionalDigits = this.engine.precision;
18501
18546
  effectiveOptions = {
@@ -18624,7 +18669,7 @@ var _BoxedExpression = class {
18624
18669
  return null;
18625
18670
  }
18626
18671
  get isNumberLiteral() {
18627
- return this.numericValue !== null;
18672
+ return false;
18628
18673
  }
18629
18674
  get isFunctionExpression() {
18630
18675
  return false;
@@ -18797,6 +18842,9 @@ var _BoxedExpression = class {
18797
18842
  simplify(_options) {
18798
18843
  return this;
18799
18844
  }
18845
+ expand() {
18846
+ return expand2(this) ?? this;
18847
+ }
18800
18848
  evaluate(_options) {
18801
18849
  return this.simplify();
18802
18850
  }
@@ -21293,7 +21341,7 @@ function invisibleOperatorForm(expr) {
21293
21341
  return expr.engine._fn(expr.operator, expr.ops.map(invisibleOperatorForm));
21294
21342
  }
21295
21343
  function numberForm(expr) {
21296
- if (expr.numericValue !== null) return expr.canonical;
21344
+ if (expr.isNumberLiteral) return expr.canonical;
21297
21345
  if (expr.ops) return expr.engine._fn(expr.operator, expr.ops.map(numberForm));
21298
21346
  return expr;
21299
21347
  }
@@ -21424,10 +21472,10 @@ function boxFunction(ce, name, ops, options) {
21424
21472
  if (typeof op1 === "number") return ce.number(-op1, options);
21425
21473
  if (op1 instanceof Decimal) return ce.number(op1.neg(), options);
21426
21474
  const boxedop1 = ce.box(op1, options);
21427
- ops = [boxedop1];
21428
21475
  const num = boxedop1.numericValue;
21429
21476
  if (num !== null)
21430
21477
  return ce.number(typeof num === "number" ? -num : num.neg(), options);
21478
+ ops = [boxedop1];
21431
21479
  }
21432
21480
  }
21433
21481
  if (options.canonical === true)
@@ -21576,7 +21624,7 @@ function makeNumericFunction(ce, name, semiOps, metadata) {
21576
21624
  } else return null;
21577
21625
  if (!ops.every((x) => x.isValid)) return ce._fn(name, ops, metadata);
21578
21626
  if (name === "Add") return canonicalAdd(ce, ops);
21579
- if (name === "Negate") return ops[0].neg();
21627
+ if (name === "Negate") return canonicalNegate(ops[0]);
21580
21628
  if (name === "Multiply") return canonicalMultiply(ce, ops);
21581
21629
  if (name === "Divide") {
21582
21630
  if (ops.length === 2)
@@ -21719,8 +21767,15 @@ var DERIVATIVES_TABLE = {
21719
21767
  Log: ["Power", ["Multiply", "_", ["Ln", "10"]], -1],
21720
21768
  Sqrt: ["Multiply", ["Power", "_", ["Negate", "Half"]], "Half"],
21721
21769
  Abs: [
21722
- "Piecewise",
21723
- ["Tuple", ["Multiply", "_", ["Power", "_", -1]], ["Greater", "_", 0]]
21770
+ "Which",
21771
+ ["Equal", "_", 0],
21772
+ NaN,
21773
+ ["Less", "_", 0],
21774
+ -1,
21775
+ ["Greater", "_", 0],
21776
+ 1,
21777
+ "True",
21778
+ ["D", ["Abs", "_"], "_"]
21724
21779
  ],
21725
21780
  // https://proofwiki.org/wiki/Derivative_of_Error_Function
21726
21781
  Erf: [
@@ -21794,7 +21849,7 @@ function derivative(fn, order2) {
21794
21849
  function differentiate(expr, v) {
21795
21850
  const ce = expr.engine;
21796
21851
  if (expr.string) return void 0;
21797
- if (expr.numericValue !== null) return expr.engine.Zero;
21852
+ if (expr.isNumberLiteral) return expr.engine.Zero;
21798
21853
  if (expr.symbol === v) return expr.engine.One;
21799
21854
  if (expr.symbol) return expr.engine.Zero;
21800
21855
  if (!expr.operator) return void 0;
@@ -23404,8 +23459,10 @@ var LOGIC_LIBRARY = {
23404
23459
  evaluate: evaluateImplies
23405
23460
  },
23406
23461
  Exists: { signature: "function", hold: true },
23462
+ NotExists: { signature: "function", hold: true },
23407
23463
  ExistsUnique: { signature: "function", hold: true },
23408
23464
  ForAll: { signature: "function", hold: true },
23465
+ NotForAll: { signature: "function", hold: true },
23409
23466
  KroneckerDelta: {
23410
23467
  description: "Return 1 if the arguments are equal, 0 otherwise",
23411
23468
  signature: "(value, ...value) -> integer",
@@ -24575,9 +24632,9 @@ var TRIGONOMETRY_LIBRARY = [
24575
24632
  }
24576
24633
  return ce.number(fArg).div(180).mul(ce.Pi);
24577
24634
  },
24578
- evaluate: (ops, { engine: ce }) => {
24579
- if (ce.angularUnit === "deg") return ops[0];
24580
- return ops[0].mul(ce.Pi.div(180)).evaluate();
24635
+ evaluate: (ops, options) => {
24636
+ if (options.engine.angularUnit === "deg") return ops[0];
24637
+ return ops[0].mul(options.engine.Pi.div(180)).evaluate(options);
24581
24638
  }
24582
24639
  },
24583
24640
  // Hypot: sqrt(x*x + y*y)
@@ -25265,6 +25322,9 @@ var BoxedNumber = class extends _BoxedExpression {
25265
25322
  get numericValue() {
25266
25323
  return this._value;
25267
25324
  }
25325
+ get isNumberLiteral() {
25326
+ return true;
25327
+ }
25268
25328
  get re() {
25269
25329
  if (typeof this._value === "number") return this._value;
25270
25330
  return this._value.re;
@@ -25388,7 +25448,7 @@ var BoxedNumber = class extends _BoxedExpression {
25388
25448
  if (typeof this._value === "number") {
25389
25449
  if (this._value === 0 || this._value === 1) return this;
25390
25450
  if (this._value === -1) return this.engine.I;
25391
- if (this._value > 0 && Number.isInteger(this._value))
25451
+ if (this._value > 0 && Number.isInteger(this._value) && this._value < SMALL_INTEGER)
25392
25452
  return this.engine.number(
25393
25453
  this.engine._numericValue({ radical: this._value })
25394
25454
  );
@@ -25644,12 +25704,10 @@ function canonicalNumberString(ce, s) {
25644
25704
  }
25645
25705
  const n = bigint(s);
25646
25706
  if (n !== null) {
25647
- if (n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER)
25648
- return Number(n);
25707
+ if (n >= -SMALL_INTEGER && n <= SMALL_INTEGER) return Number(n);
25649
25708
  return ce._numericValue(n);
25650
25709
  }
25651
- const b = ce.bignum(s);
25652
- return isInMachineRange(b) ? b.toNumber() : ce._numericValue(b);
25710
+ return ce._numericValue(ce.bignum(s));
25653
25711
  }
25654
25712
 
25655
25713
  // src/compute-engine/boxed-expression/boxed-symbol.ts
@@ -27008,14 +27066,12 @@ var DEFINITIONS_ARITHMETIC = [
27008
27066
  name: "Negate",
27009
27067
  latexTrigger: ["-"],
27010
27068
  kind: "prefix",
27011
- precedence: ADDITION_PRECEDENCE + 2,
27069
+ precedence: EXPONENTIATION_PRECEDENCE + 1,
27012
27070
  parse: (parser, terminator) => {
27013
27071
  parser.skipSpace();
27014
- if (/\d/.test(parser.peek)) return null;
27015
- if (parser.parseNumber() !== null) return null;
27016
27072
  const rhs = parser.parseExpression({
27017
27073
  ...terminator,
27018
- minPrec: ADDITION_PRECEDENCE + 3
27074
+ minPrec: EXPONENTIATION_PRECEDENCE + 3
27019
27075
  });
27020
27076
  if (rhs === null) return null;
27021
27077
  return ["Negate", rhs];
@@ -27106,7 +27162,7 @@ var DEFINITIONS_ARITHMETIC = [
27106
27162
  latexTrigger: ["^"],
27107
27163
  kind: "infix",
27108
27164
  serialize: serializePower
27109
- // Parsing is done as a special case in `parseExpression`
27165
+ // Parsing is done as a special case in `parseSupsub`
27110
27166
  },
27111
27167
  {
27112
27168
  latexTrigger: "\\prod",
@@ -27830,6 +27886,24 @@ var DEFINITIONS_LOGIC = [
27830
27886
  serialize: "\\exists!",
27831
27887
  parse: parseQuantifier("ExistsUnique")
27832
27888
  },
27889
+ {
27890
+ name: "NotForAll",
27891
+ kind: "prefix",
27892
+ latexTrigger: ["\\lnot", "\\forall"],
27893
+ precedence: 200,
27894
+ // Has to be lower than COMPARISON_PRECEDENCE
27895
+ serialize: "\\lnot\\forall",
27896
+ parse: parseQuantifier("NotForAll")
27897
+ },
27898
+ {
27899
+ name: "NotExists",
27900
+ kind: "prefix",
27901
+ latexTrigger: ["\\lnot", "\\exists"],
27902
+ precedence: 200,
27903
+ // Has to be lower than COMPARISON_PRECEDENCE,
27904
+ serialize: "\\lnot\\exists",
27905
+ parse: parseQuantifier("NotExists")
27906
+ },
27833
27907
  {
27834
27908
  name: "KroneckerDelta",
27835
27909
  kind: "prefix",
@@ -27897,14 +27971,11 @@ function parseQuantifier(kind) {
27897
27971
  if (id) {
27898
27972
  parser.skipSpace();
27899
27973
  if (parser.match(",") || parser.match("\\mid") || parser.match(".") || parser.match(":") || parser.match("\\colon")) {
27900
- const body = parser.parseExpression(terminator);
27901
- return [kind, id, missingIfEmpty(body)];
27902
- }
27903
- if (parser.match("(")) {
27904
- const body = parser.parseExpression(terminator);
27905
- if (!parser.match(")")) return null;
27906
- return [kind, id, missingIfEmpty(body)];
27974
+ const body2 = parser.parseExpression(terminator);
27975
+ return [kind, id, missingIfEmpty(body2)];
27907
27976
  }
27977
+ const body = parser.parseEnclosure();
27978
+ if (body) return [kind, id, missingIfEmpty(body)];
27908
27979
  }
27909
27980
  parser.index = index;
27910
27981
  const condition = parser.parseExpression(terminator);
@@ -32923,7 +32994,7 @@ var ComputeEngine = class _ComputeEngine {
32923
32994
  }
32924
32995
  if ("radical" in value || "rational" in value) {
32925
32996
  if (value.radical !== void 0 && (!Number.isInteger(value.radical) || value.radical >= SMALL_INTEGER)) {
32926
- return makeNumericValue(value);
32997
+ throw Error("Unexpected value for radical part:" + value.radical);
32927
32998
  }
32928
32999
  if (value.rational) {
32929
33000
  if (isMachineRational(value.rational)) {
@@ -33220,11 +33291,19 @@ Invalid definition for ${id}`, err.message].join("\n| ") + "\n"
33220
33291
  {
33221
33292
  const type2 = parseType(def);
33222
33293
  if (!isValidType(type2)) {
33294
+ if (typeof def === "object" && "N" in def) {
33295
+ throw Error(
33296
+ [
33297
+ `Invalid argument for "${id}"`,
33298
+ "Use `evaluate` handler instead of `N`"
33299
+ ].join("\n| ")
33300
+ );
33301
+ }
33223
33302
  throw Error(
33224
33303
  [
33225
33304
  `Invalid argument for "${id}"`,
33226
33305
  def.toString(),
33227
- `Use a type, a FunctionDefinition or a SymbolDefinition`
33306
+ `Use a type, a \`FunctionDefinition\` or a \`SymbolDefinition\``
33228
33307
  ].join("\n| ")
33229
33308
  );
33230
33309
  }
@@ -34315,10 +34394,10 @@ function mark(line, mark2) {
34315
34394
  }
34316
34395
 
34317
34396
  // src/compute-engine.ts
34318
- var version = "0.26.0";
34397
+ var version = "0.26.3";
34319
34398
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
34320
34399
  ComputeEngine: ComputeEngine.prototype.constructor,
34321
- version: "0.26.0"
34400
+ version: "0.26.3"
34322
34401
  };
34323
34402
  export {
34324
34403
  ComputeEngine,