@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
@@ -1,4 +1,4 @@
1
- /** Compute Engine 0.26.0 */
1
+ /** Compute Engine 0.26.3 */
2
2
  (function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ComputeEngine = {}));})(this, (function (exports) { 'use strict';
3
3
  var ComputeEngine = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -4053,6 +4053,7 @@ Invalid type
4053
4053
  function parseType(s) {
4054
4054
  if (s === void 0) return void 0;
4055
4055
  if (isValidType(s)) return s;
4056
+ if (typeof s !== "string") return void 0;
4056
4057
  if (PRIMITIVE_TYPES.includes(s)) return s;
4057
4058
  const parser = new TypeParser(s);
4058
4059
  return parser.parse();
@@ -4628,6 +4629,7 @@ Invalid type
4628
4629
  var ADDITION_PRECEDENCE = 275;
4629
4630
  var MULTIPLICATION_PRECEDENCE = 390;
4630
4631
  var DIVISION_PRECEDENCE = 600;
4632
+ var EXPONENTIATION_PRECEDENCE = 700;
4631
4633
  var POSTFIX_PRECEDENCE = 810;
4632
4634
  function isExpressionEntry(entry) {
4633
4635
  return !("kind" in entry) || entry.kind === "expression";
@@ -6489,20 +6491,20 @@ Invalid type
6489
6491
  if (def === void 0 || def === null || typeof def !== "object")
6490
6492
  return false;
6491
6493
  if (isBoxedExpression(def)) return false;
6492
- if ("type" in def || "value" in def || "constant" in def) {
6493
- if (typeof def.type === "function") {
6494
+ if ("value" in def || "constant" in def || "inferred" in def) {
6495
+ if ("type" in def && typeof def.type === "function") {
6494
6496
  throw new Error(
6495
- "The type field of a symbol definition should be a type string"
6497
+ "The `type` field of a symbol definition should be of type `string`"
6496
6498
  );
6497
6499
  }
6498
6500
  if ("signature" in def) {
6499
6501
  throw new Error(
6500
- "Symbol definition cannot have a signature field. Use a type field instead."
6502
+ "Symbol definition cannot have a `signature` field. Use a `type` field instead."
6501
6503
  );
6502
6504
  }
6503
6505
  if ("sgn" in def) {
6504
6506
  throw new Error(
6505
- "Symbol definition cannot have a sgn field. Use a flags field instead."
6507
+ "Symbol definition cannot have a `sgn` field. Use a `flags.sgn` field instead."
6506
6508
  );
6507
6509
  }
6508
6510
  return true;
@@ -6516,22 +6518,23 @@ Invalid type
6516
6518
  if ("signature" in def || "complexity" in def) {
6517
6519
  if ("constant" in def) {
6518
6520
  throw new Error(
6519
- "Function definition cannot have a constant field and symbol definition cannot have a signature field."
6521
+ "Function definition cannot have a `constant` field and symbol definition cannot have a `signature` field."
6520
6522
  );
6521
6523
  }
6522
- if ("type" in def && typeof def.type !== "function") {
6523
- throw new Error(
6524
- "The type field of a function definition should be a function"
6525
- );
6526
- }
6527
- if ("sgn" in def && typeof def.sgn !== "function") {
6528
- throw new Error(
6529
- "The sgn field of a function definition should be a function"
6530
- );
6531
- }
6532
- return true;
6533
6524
  }
6534
- return false;
6525
+ if (!("evaluate" in def) && !("signature" in def) && !("sgn" in def) && !("complexity" in def) && !("canonical" in def))
6526
+ return false;
6527
+ if ("type" in def && typeof def.type !== "function") {
6528
+ throw new Error(
6529
+ "The `type` field of a function definition should be a function"
6530
+ );
6531
+ }
6532
+ if ("sgn" in def && typeof def.sgn !== "function") {
6533
+ throw new Error(
6534
+ "The `sgn` field of a function definition should be a function"
6535
+ );
6536
+ }
6537
+ return true;
6535
6538
  }
6536
6539
  function semiCanonical(ce, xs) {
6537
6540
  if (!xs.every((x) => isBoxedExpression(x))) return xs.map((x) => ce.box(x));
@@ -7533,7 +7536,9 @@ Invalid type
7533
7536
  }
7534
7537
  return [value, ""];
7535
7538
  }
7536
- function numberToString(num) {
7539
+ function numberToString(num, fractionalDigits) {
7540
+ if (typeof fractionalDigits === "number" && typeof num === "number")
7541
+ return num.toFixed(fractionalDigits);
7537
7542
  const numStr = num.toString();
7538
7543
  if (typeof num === "number" && Number.isInteger(num) && numStr.includes("e")) {
7539
7544
  const fixedStr = BigInt(num).toString();
@@ -7565,11 +7570,13 @@ Invalid type
7565
7570
  if (s === "-infinity") return null;
7566
7571
  return bigint(s);
7567
7572
  }
7568
- function numberToExpression(num) {
7573
+ function numberToExpression(num, fractionalDigits) {
7569
7574
  if (typeof num === "number") {
7570
7575
  if (isNaN(num)) return "NaN";
7571
7576
  if (!Number.isFinite(num))
7572
7577
  return num < 0 ? "NegativeInfinity" : "PositiveInfinity";
7578
+ if (typeof fractionalDigits === "number")
7579
+ return { num: num.toFixed(fractionalDigits) };
7573
7580
  return num;
7574
7581
  }
7575
7582
  if (num >= Number.MIN_SAFE_INTEGER && num <= Number.MAX_SAFE_INTEGER)
@@ -7703,7 +7710,7 @@ Invalid type
7703
7710
  return result.mul(this.bignum(this.radical).sqrt());
7704
7711
  }
7705
7712
  get numerator() {
7706
- if (this.rational[1] === 1) return this;
7713
+ if (this.rational[1] == 1) return this;
7707
7714
  return this.clone({
7708
7715
  rational: isMachineRational(this.rational) ? [this.rational[0], 1] : [this.rational[0], BigInt(1)],
7709
7716
  radical: this.radical
@@ -7745,10 +7752,10 @@ Invalid type
7745
7752
  return Number.isNaN(this.rational[0]);
7746
7753
  }
7747
7754
  get isPositiveInfinity() {
7748
- return this.rational[0] === Infinity;
7755
+ return this.rational[0] == Infinity;
7749
7756
  }
7750
7757
  get isNegativeInfinity() {
7751
- return this.rational[0] === -Infinity;
7758
+ return this.rational[0] == -Infinity;
7752
7759
  }
7753
7760
  get isComplexInfinity() {
7754
7761
  return false;
@@ -7890,7 +7897,7 @@ Invalid type
7890
7897
  exponent = { re: exponent.re, im: exponent.im };
7891
7898
  } else {
7892
7899
  if (exponent instanceof _ExactNumericValue) {
7893
- if (exponent.radical === 1 && exponent.rational[0] === 1)
7900
+ if (exponent.radical === 1 && exponent.rational[0] == 1)
7894
7901
  return this.root(exponent.rational[0]);
7895
7902
  }
7896
7903
  exponent = exponent.re;
@@ -7972,7 +7979,7 @@ Invalid type
7972
7979
  return this.factory({ im: Math.pow(-this.re, 1 / exponent) });
7973
7980
  if (this.radical > SMALL_INTEGER || this.rational[0] > SMALL_INTEGER || this.rational[0] < -SMALL_INTEGER || this.rational[1] > SMALL_INTEGER)
7974
7981
  return this.factory(this.bignumRe).root(exponent);
7975
- if (this.rational[1] === 1) {
7982
+ if (this.rational[1] == 1) {
7976
7983
  const root2 = Math.pow(this.rational[0], 1 / exponent);
7977
7984
  if (Number.isInteger(root2)) return this.clone(root2);
7978
7985
  }
@@ -8038,7 +8045,7 @@ Invalid type
8038
8045
  }
8039
8046
  eq(other) {
8040
8047
  if (typeof other === "number")
8041
- return this.radical === 1 && isInteger(this.rational) && this.rational[0] === other;
8048
+ return this.radical === 1 && isInteger(this.rational) && this.rational[0] == other;
8042
8049
  if (other instanceof _ExactNumericValue) {
8043
8050
  return this.radical === other.radical && this.rational[0] == other.rational[0] && this.rational[1] == other.rational[1];
8044
8051
  }
@@ -8202,7 +8209,7 @@ Invalid type
8202
8209
  // src/compute-engine/boxed-expression/polynomials.ts
8203
8210
  function totalDegree(expr) {
8204
8211
  if (expr.symbol && !expr.isConstant) return 1;
8205
- if (expr.operator === "Power" && expr.op2.numericValue !== null) {
8212
+ if (expr.operator === "Power" && expr.op2.isNumberLiteral) {
8206
8213
  if (totalDegree(expr.op1) === 0) return 0;
8207
8214
  const deg = asSmallInteger(expr.op2);
8208
8215
  if (deg !== null && deg > 0) return deg;
@@ -8227,7 +8234,7 @@ Invalid type
8227
8234
  }
8228
8235
  function maxDegree(expr) {
8229
8236
  if (expr.symbol && !expr.isConstant) return 1;
8230
- if (expr.operator === "Power" && expr.op2.numericValue !== null) {
8237
+ if (expr.operator === "Power" && expr.op2.isNumberLiteral) {
8231
8238
  if (maxDegree(expr.op1) === 0) return 0;
8232
8239
  const deg = asSmallInteger(expr.op2);
8233
8240
  if (deg !== null && deg > 0) return deg;
@@ -9536,6 +9543,16 @@ Invalid type
9536
9543
  }
9537
9544
 
9538
9545
  // src/compute-engine/boxed-expression/negate.ts
9546
+ function canonicalNegate(expr) {
9547
+ let sign2 = -1;
9548
+ while (expr.operator === "Negate") {
9549
+ expr = expr.op1;
9550
+ sign2 = -sign2;
9551
+ }
9552
+ if (sign2 === 1) return expr;
9553
+ if (expr.isNumberLiteral) return expr.neg();
9554
+ return expr.engine._fn("Negate", [expr]);
9555
+ }
9539
9556
  function negate(expr) {
9540
9557
  let sign2 = -1;
9541
9558
  while (expr.operator === "Negate") {
@@ -9548,9 +9565,7 @@ Invalid type
9548
9565
  if (expr.operator === "Subtract") return expr.op2.sub(expr.op1);
9549
9566
  if (expr.operator === "Add") return add3(...expr.ops.map((x) => negate(x)));
9550
9567
  if (expr.operator === "Multiply") return negateProduct(ce, expr.ops);
9551
- if (expr.operator === "Divide") {
9552
- return negate(expr.op1).div(expr.op2);
9553
- }
9568
+ if (expr.operator === "Divide") return negate(expr.op1).div(expr.op2);
9554
9569
  return ce._fn("Negate", [expr]);
9555
9570
  }
9556
9571
  function negateProduct(ce, args) {
@@ -9612,7 +9627,10 @@ Invalid type
9612
9627
  if (next.operator === "Sqrt" && next.op1.isNumberLiteral && isSubtype(next.op1.type, "finite_integer")) {
9613
9628
  let radical = next.op1.numericValue;
9614
9629
  if (typeof radical !== "number") radical = radical.re;
9615
- if (radical >= SMALL_INTEGER) continue;
9630
+ if (radical >= SMALL_INTEGER) {
9631
+ ys.push(x);
9632
+ continue;
9633
+ }
9616
9634
  if (isSubtype(x.type, "finite_rational")) {
9617
9635
  const rational = x.numericValue;
9618
9636
  const [num, den] = typeof rational === "number" ? [rational, 1] : [rational.numerator.re, rational.denominator.re];
@@ -9705,6 +9723,7 @@ Invalid type
9705
9723
  function canonicalDivide(op1, op2) {
9706
9724
  const ce = op1.engine;
9707
9725
  if (!op1.isValid || !op2.isValid) return ce._fn("Divide", [op1, op2]);
9726
+ if (op1.isNaN || op2.isNaN) return ce.NaN;
9708
9727
  if (op2.is(0)) return op1.is(0) ? ce.NaN : ce.ComplexInfinity;
9709
9728
  if (op1.is(0)) return ce.Zero;
9710
9729
  if (op2.is(0) === false) {
@@ -9904,9 +9923,11 @@ Invalid type
9904
9923
  return;
9905
9924
  }
9906
9925
  if (term.isInfinity) {
9907
- this.coefficient = this.engine._numericValue(
9908
- term.isNegative ? -Infinity : Infinity
9909
- );
9926
+ if (isOne(exp2)) {
9927
+ this.coefficient = this.engine._numericValue(
9928
+ term.isNegative ? -Infinity : Infinity
9929
+ );
9930
+ } else this.terms.push({ term, exponent: exp2 });
9910
9931
  return;
9911
9932
  }
9912
9933
  if (isOne(exp2)) {
@@ -10061,8 +10082,15 @@ Invalid type
10061
10082
  const ce = this.engine;
10062
10083
  const coef = this.coefficient;
10063
10084
  if (coef.isZero) return [ce.Zero, ce.One];
10064
- if (coef.isPositiveInfinity || coef.isNegativeInfinity)
10085
+ if (coef.isPositiveInfinity || coef.isNegativeInfinity) {
10086
+ if (this.terms.length === 0) {
10087
+ return [
10088
+ coef.isPositiveInfinity ? ce.PositiveInfinity : ce.NegativeInfinity,
10089
+ ce.One
10090
+ ];
10091
+ }
10065
10092
  return [ce.NaN, ce.NaN];
10093
+ }
10066
10094
  const isNegativeOne2 = coef.isNegativeOne;
10067
10095
  if (isNegativeOne2) this.coefficient = ce._numericValue(1);
10068
10096
  const xs = this.groupedByDegrees({ mode: "rational" });
@@ -10629,13 +10657,23 @@ Invalid type
10629
10657
  (x) => x.sinh(),
10630
10658
  (x) => x.sinh()
10631
10659
  );
10632
- case "Tan":
10633
- return applyAngle(
10660
+ case "Tan": {
10661
+ const result = applyAngle(
10634
10662
  op,
10635
- Math.tan,
10636
- (x) => x.toSignificantDigits(ce.precision + 4).tan().toSignificantDigits(ce.precision),
10663
+ (x) => {
10664
+ const y = Math.tan(x);
10665
+ if (y > 1e6 || y < -1e6) return ce.ComplexInfinity;
10666
+ return y;
10667
+ },
10668
+ (x) => {
10669
+ const y = x.tan();
10670
+ if (y.greaterThan(1e6) || y.lessThan(-1e6)) return ce.ComplexInfinity;
10671
+ return y;
10672
+ },
10637
10673
  (x) => x.tan()
10638
10674
  );
10675
+ return result;
10676
+ }
10639
10677
  case "Tanh":
10640
10678
  return applyAngle(
10641
10679
  op,
@@ -10869,9 +10907,11 @@ Invalid type
10869
10907
  if (exp2 === 1) return a;
10870
10908
  if (exp2 === 2) {
10871
10909
  if (a.isNumberLiteral && isSubtype(a.type, "rational")) {
10872
- const v = a.sqrt();
10873
- if (typeof v.numericValue === "number") return v;
10874
- if (v.numericValue.isExact) return v;
10910
+ if (a.re < SMALL_INTEGER) {
10911
+ const v = a.sqrt();
10912
+ if (typeof v.numericValue === "number") return v;
10913
+ if (v.numericValue.isExact) return v;
10914
+ }
10875
10915
  }
10876
10916
  return ce._fn("Sqrt", [a]);
10877
10917
  }
@@ -11639,10 +11679,9 @@ Invalid type
11639
11679
  ce.resetContext();
11640
11680
  let i = 0;
11641
11681
  for (const param of params) ce.assign(param, args[i++]);
11642
- const result = fn.N() ?? fn.evaluate();
11682
+ const result = fn.evaluate();
11643
11683
  ce.swapScope(context);
11644
- if (!result.isValid) return void 0;
11645
- return result;
11684
+ return result.isValid ? result : void 0;
11646
11685
  };
11647
11686
  }
11648
11687
  function apply3(fn, args) {
@@ -13072,7 +13111,7 @@ Invalid type
13072
13111
  sgn: ([x]) => x.isNonNegative ? "positive" : x.isNegative || x.isReal === false ? "unsigned" : void 0,
13073
13112
  canonical: (args, { engine }) => {
13074
13113
  const x = args[0];
13075
- if (x.numericValue !== null && x.isNegative)
13114
+ if (x.isNumberLiteral && x.isNegative)
13076
13115
  return engine._fn("Factorial", [x.neg()]).neg();
13077
13116
  return engine._fn("Factorial", [x]);
13078
13117
  },
@@ -13439,7 +13478,7 @@ Invalid type
13439
13478
  const [base, exp2] = args;
13440
13479
  return canonicalRoot(base, exp2);
13441
13480
  },
13442
- evaluate: ([x, n], { numericApproximation }) => x.root(n)
13481
+ evaluate: ([x, n], { numericApproximation }) => root(x, n, { numericApproximation })
13443
13482
  },
13444
13483
  Round: {
13445
13484
  complexity: 1250,
@@ -18155,6 +18194,9 @@ ${e.message}`);
18155
18194
  fractionalPart += lastDigit;
18156
18195
  while (fractionalPart.endsWith("0"))
18157
18196
  fractionalPart = fractionalPart.slice(0, -1);
18197
+ if (typeof options.fractionalDigits === "number") {
18198
+ fractionalPart = fractionalPart.slice(0, options.fractionalDigits);
18199
+ }
18158
18200
  if (exponent)
18159
18201
  return `${wholepart}.${fractionalPart}${exponent.toLowerCase()}`;
18160
18202
  return `${wholepart}.${fractionalPart}`;
@@ -18309,7 +18351,7 @@ ${e.message}`);
18309
18351
  metadata.latex = metadata.latex ?? ce.box({ num: value.toString() }).latex;
18310
18352
  if (metadata.latex !== void 0)
18311
18353
  return { num: value.toString(), latex: metadata.latex };
18312
- return shorthandAllowed ? numberToExpression(value) : { num: numberToString(value) };
18354
+ return shorthandAllowed ? numberToExpression(value, options.fractionalDigits) : { num: numberToString(value, options.fractionalDigits) };
18313
18355
  }
18314
18356
  }
18315
18357
  let result;
@@ -18471,6 +18513,8 @@ ${e.message}`);
18471
18513
  }
18472
18514
  if (options.fractionalDigits === "auto")
18473
18515
  defaultOptions.fractionalDigits = -this.engine.precision;
18516
+ if (typeof options.fractionalDigits === "number")
18517
+ defaultOptions.fractionalDigits = options.fractionalDigits;
18474
18518
  }
18475
18519
  const opts = {
18476
18520
  ...defaultOptions,
@@ -18527,6 +18571,7 @@ ${e.message}`);
18527
18571
  };
18528
18572
  if (options?.fractionalDigits === "auto")
18529
18573
  effectiveOptions.fractionalDigits = -this.engine.precision;
18574
+ else effectiveOptions.fractionalDigits = options?.fractionalDigits ?? "max";
18530
18575
  if (typeof effectiveOptions.fractionalDigits === "number" && effectiveOptions.fractionalDigits > this.engine.precision)
18531
18576
  effectiveOptions.fractionalDigits = this.engine.precision;
18532
18577
  effectiveOptions = {
@@ -18655,7 +18700,7 @@ ${e.message}`);
18655
18700
  return null;
18656
18701
  }
18657
18702
  get isNumberLiteral() {
18658
- return this.numericValue !== null;
18703
+ return false;
18659
18704
  }
18660
18705
  get isFunctionExpression() {
18661
18706
  return false;
@@ -18828,6 +18873,9 @@ ${e.message}`);
18828
18873
  simplify(_options) {
18829
18874
  return this;
18830
18875
  }
18876
+ expand() {
18877
+ return expand2(this) ?? this;
18878
+ }
18831
18879
  evaluate(_options) {
18832
18880
  return this.simplify();
18833
18881
  }
@@ -21324,7 +21372,7 @@ ${e.message}`);
21324
21372
  return expr.engine._fn(expr.operator, expr.ops.map(invisibleOperatorForm));
21325
21373
  }
21326
21374
  function numberForm(expr) {
21327
- if (expr.numericValue !== null) return expr.canonical;
21375
+ if (expr.isNumberLiteral) return expr.canonical;
21328
21376
  if (expr.ops) return expr.engine._fn(expr.operator, expr.ops.map(numberForm));
21329
21377
  return expr;
21330
21378
  }
@@ -21455,10 +21503,10 @@ ${e.message}`);
21455
21503
  if (typeof op1 === "number") return ce.number(-op1, options);
21456
21504
  if (op1 instanceof Decimal) return ce.number(op1.neg(), options);
21457
21505
  const boxedop1 = ce.box(op1, options);
21458
- ops = [boxedop1];
21459
21506
  const num = boxedop1.numericValue;
21460
21507
  if (num !== null)
21461
21508
  return ce.number(typeof num === "number" ? -num : num.neg(), options);
21509
+ ops = [boxedop1];
21462
21510
  }
21463
21511
  }
21464
21512
  if (options.canonical === true)
@@ -21607,7 +21655,7 @@ ${e.message}`);
21607
21655
  } else return null;
21608
21656
  if (!ops.every((x) => x.isValid)) return ce._fn(name, ops, metadata);
21609
21657
  if (name === "Add") return canonicalAdd(ce, ops);
21610
- if (name === "Negate") return ops[0].neg();
21658
+ if (name === "Negate") return canonicalNegate(ops[0]);
21611
21659
  if (name === "Multiply") return canonicalMultiply(ce, ops);
21612
21660
  if (name === "Divide") {
21613
21661
  if (ops.length === 2)
@@ -21750,8 +21798,15 @@ ${e.message}`);
21750
21798
  Log: ["Power", ["Multiply", "_", ["Ln", "10"]], -1],
21751
21799
  Sqrt: ["Multiply", ["Power", "_", ["Negate", "Half"]], "Half"],
21752
21800
  Abs: [
21753
- "Piecewise",
21754
- ["Tuple", ["Multiply", "_", ["Power", "_", -1]], ["Greater", "_", 0]]
21801
+ "Which",
21802
+ ["Equal", "_", 0],
21803
+ NaN,
21804
+ ["Less", "_", 0],
21805
+ -1,
21806
+ ["Greater", "_", 0],
21807
+ 1,
21808
+ "True",
21809
+ ["D", ["Abs", "_"], "_"]
21755
21810
  ],
21756
21811
  // https://proofwiki.org/wiki/Derivative_of_Error_Function
21757
21812
  Erf: [
@@ -21825,7 +21880,7 @@ ${e.message}`);
21825
21880
  function differentiate(expr, v) {
21826
21881
  const ce = expr.engine;
21827
21882
  if (expr.string) return void 0;
21828
- if (expr.numericValue !== null) return expr.engine.Zero;
21883
+ if (expr.isNumberLiteral) return expr.engine.Zero;
21829
21884
  if (expr.symbol === v) return expr.engine.One;
21830
21885
  if (expr.symbol) return expr.engine.Zero;
21831
21886
  if (!expr.operator) return void 0;
@@ -23435,8 +23490,10 @@ ${e.message}`);
23435
23490
  evaluate: evaluateImplies
23436
23491
  },
23437
23492
  Exists: { signature: "function", hold: true },
23493
+ NotExists: { signature: "function", hold: true },
23438
23494
  ExistsUnique: { signature: "function", hold: true },
23439
23495
  ForAll: { signature: "function", hold: true },
23496
+ NotForAll: { signature: "function", hold: true },
23440
23497
  KroneckerDelta: {
23441
23498
  description: "Return 1 if the arguments are equal, 0 otherwise",
23442
23499
  signature: "(value, ...value) -> integer",
@@ -24606,9 +24663,9 @@ ${e.message}`);
24606
24663
  }
24607
24664
  return ce.number(fArg).div(180).mul(ce.Pi);
24608
24665
  },
24609
- evaluate: (ops, { engine: ce }) => {
24610
- if (ce.angularUnit === "deg") return ops[0];
24611
- return ops[0].mul(ce.Pi.div(180)).evaluate();
24666
+ evaluate: (ops, options) => {
24667
+ if (options.engine.angularUnit === "deg") return ops[0];
24668
+ return ops[0].mul(options.engine.Pi.div(180)).evaluate(options);
24612
24669
  }
24613
24670
  },
24614
24671
  // Hypot: sqrt(x*x + y*y)
@@ -25296,6 +25353,9 @@ Error in definition of "${name}"`,
25296
25353
  get numericValue() {
25297
25354
  return this._value;
25298
25355
  }
25356
+ get isNumberLiteral() {
25357
+ return true;
25358
+ }
25299
25359
  get re() {
25300
25360
  if (typeof this._value === "number") return this._value;
25301
25361
  return this._value.re;
@@ -25419,7 +25479,7 @@ Error in definition of "${name}"`,
25419
25479
  if (typeof this._value === "number") {
25420
25480
  if (this._value === 0 || this._value === 1) return this;
25421
25481
  if (this._value === -1) return this.engine.I;
25422
- if (this._value > 0 && Number.isInteger(this._value))
25482
+ if (this._value > 0 && Number.isInteger(this._value) && this._value < SMALL_INTEGER)
25423
25483
  return this.engine.number(
25424
25484
  this.engine._numericValue({ radical: this._value })
25425
25485
  );
@@ -25675,12 +25735,10 @@ Error in definition of "${name}"`,
25675
25735
  }
25676
25736
  const n = bigint(s);
25677
25737
  if (n !== null) {
25678
- if (n >= Number.MIN_SAFE_INTEGER && n <= Number.MAX_SAFE_INTEGER)
25679
- return Number(n);
25738
+ if (n >= -SMALL_INTEGER && n <= SMALL_INTEGER) return Number(n);
25680
25739
  return ce._numericValue(n);
25681
25740
  }
25682
- const b = ce.bignum(s);
25683
- return isInMachineRange(b) ? b.toNumber() : ce._numericValue(b);
25741
+ return ce._numericValue(ce.bignum(s));
25684
25742
  }
25685
25743
 
25686
25744
  // src/compute-engine/boxed-expression/boxed-symbol.ts
@@ -27039,14 +27097,12 @@ Error in definition of "${name}"`,
27039
27097
  name: "Negate",
27040
27098
  latexTrigger: ["-"],
27041
27099
  kind: "prefix",
27042
- precedence: ADDITION_PRECEDENCE + 2,
27100
+ precedence: EXPONENTIATION_PRECEDENCE + 1,
27043
27101
  parse: (parser, terminator) => {
27044
27102
  parser.skipSpace();
27045
- if (/\d/.test(parser.peek)) return null;
27046
- if (parser.parseNumber() !== null) return null;
27047
27103
  const rhs = parser.parseExpression({
27048
27104
  ...terminator,
27049
- minPrec: ADDITION_PRECEDENCE + 3
27105
+ minPrec: EXPONENTIATION_PRECEDENCE + 3
27050
27106
  });
27051
27107
  if (rhs === null) return null;
27052
27108
  return ["Negate", rhs];
@@ -27137,7 +27193,7 @@ Error in definition of "${name}"`,
27137
27193
  latexTrigger: ["^"],
27138
27194
  kind: "infix",
27139
27195
  serialize: serializePower
27140
- // Parsing is done as a special case in `parseExpression`
27196
+ // Parsing is done as a special case in `parseSupsub`
27141
27197
  },
27142
27198
  {
27143
27199
  latexTrigger: "\\prod",
@@ -27861,6 +27917,24 @@ Error in definition of "${name}"`,
27861
27917
  serialize: "\\exists!",
27862
27918
  parse: parseQuantifier("ExistsUnique")
27863
27919
  },
27920
+ {
27921
+ name: "NotForAll",
27922
+ kind: "prefix",
27923
+ latexTrigger: ["\\lnot", "\\forall"],
27924
+ precedence: 200,
27925
+ // Has to be lower than COMPARISON_PRECEDENCE
27926
+ serialize: "\\lnot\\forall",
27927
+ parse: parseQuantifier("NotForAll")
27928
+ },
27929
+ {
27930
+ name: "NotExists",
27931
+ kind: "prefix",
27932
+ latexTrigger: ["\\lnot", "\\exists"],
27933
+ precedence: 200,
27934
+ // Has to be lower than COMPARISON_PRECEDENCE,
27935
+ serialize: "\\lnot\\exists",
27936
+ parse: parseQuantifier("NotExists")
27937
+ },
27864
27938
  {
27865
27939
  name: "KroneckerDelta",
27866
27940
  kind: "prefix",
@@ -27928,14 +28002,11 @@ Error in definition of "${name}"`,
27928
28002
  if (id) {
27929
28003
  parser.skipSpace();
27930
28004
  if (parser.match(",") || parser.match("\\mid") || parser.match(".") || parser.match(":") || parser.match("\\colon")) {
27931
- const body = parser.parseExpression(terminator);
27932
- return [kind, id, missingIfEmpty(body)];
27933
- }
27934
- if (parser.match("(")) {
27935
- const body = parser.parseExpression(terminator);
27936
- if (!parser.match(")")) return null;
27937
- return [kind, id, missingIfEmpty(body)];
28005
+ const body2 = parser.parseExpression(terminator);
28006
+ return [kind, id, missingIfEmpty(body2)];
27938
28007
  }
28008
+ const body = parser.parseEnclosure();
28009
+ if (body) return [kind, id, missingIfEmpty(body)];
27939
28010
  }
27940
28011
  parser.index = index;
27941
28012
  const condition = parser.parseExpression(terminator);
@@ -32954,7 +33025,7 @@ Error in definition of "${name}"`,
32954
33025
  }
32955
33026
  if ("radical" in value || "rational" in value) {
32956
33027
  if (value.radical !== void 0 && (!Number.isInteger(value.radical) || value.radical >= SMALL_INTEGER)) {
32957
- return makeNumericValue(value);
33028
+ throw Error("Unexpected value for radical part:" + value.radical);
32958
33029
  }
32959
33030
  if (value.rational) {
32960
33031
  if (isMachineRational(value.rational)) {
@@ -33251,11 +33322,19 @@ Invalid definition for ${id}`, err.message].join("\n| ") + "\n"
33251
33322
  {
33252
33323
  const type2 = parseType(def);
33253
33324
  if (!isValidType(type2)) {
33325
+ if (typeof def === "object" && "N" in def) {
33326
+ throw Error(
33327
+ [
33328
+ `Invalid argument for "${id}"`,
33329
+ "Use `evaluate` handler instead of `N`"
33330
+ ].join("\n| ")
33331
+ );
33332
+ }
33254
33333
  throw Error(
33255
33334
  [
33256
33335
  `Invalid argument for "${id}"`,
33257
33336
  def.toString(),
33258
- `Use a type, a FunctionDefinition or a SymbolDefinition`
33337
+ `Use a type, a \`FunctionDefinition\` or a \`SymbolDefinition\``
33259
33338
  ].join("\n| ")
33260
33339
  );
33261
33340
  }
@@ -34346,10 +34425,10 @@ ${lineNo.toString().padStart(maxDigits, " ")}`;
34346
34425
  }
34347
34426
 
34348
34427
  // src/compute-engine.ts
34349
- var version = "0.26.0";
34428
+ var version = "0.26.3";
34350
34429
  globalThis[Symbol.for("io.cortexjs.compute-engine")] = {
34351
34430
  ComputeEngine: ComputeEngine.prototype.constructor,
34352
- version: "0.26.0"
34431
+ version: "0.26.3"
34353
34432
  };
34354
34433
  return __toCommonJS(compute_engine_exports);
34355
34434
  })();