@cortex-js/compute-engine 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/compute-engine.esm.js +8491 -7432
  2. package/dist/compute-engine.min.esm.js +2 -2
  3. package/dist/compute-engine.min.js +2 -2
  4. package/dist/math-json.esm.js +41 -88
  5. package/dist/math-json.min.esm.js +2 -2
  6. package/dist/math-json.min.js +2 -2
  7. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  8. package/dist/types/common/signals.d.ts +1 -5
  9. package/dist/types/common/utils.d.ts +1 -0
  10. package/dist/types/compute-engine/assume.d.ts +6 -1
  11. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +10 -15
  12. package/dist/types/compute-engine/boxed-expression/box.d.ts +74 -16
  13. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +10 -5
  14. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -4
  15. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +16 -15
  17. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +20 -20
  18. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +8 -7
  19. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -4
  20. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +2 -3
  21. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +18 -13
  22. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +3 -3
  25. package/dist/types/compute-engine/boxed-expression/utils.d.ts +7 -7
  26. package/dist/types/compute-engine/boxed-expression/validate.d.ts +18 -0
  27. package/dist/types/compute-engine/compute-engine.d.ts +65 -33
  28. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  29. package/dist/types/compute-engine/domain-utils.d.ts +2 -8
  30. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  31. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  32. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  33. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  34. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/parse.d.ts +25 -9
  43. package/dist/types/compute-engine/latex-syntax/public.d.ts +14 -6
  44. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +2 -2
  46. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +2 -2
  47. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  48. package/dist/types/compute-engine/library/arithmetic-add.d.ts +3 -3
  49. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  50. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +5 -6
  51. package/dist/types/compute-engine/library/arithmetic-power.d.ts +3 -2
  52. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  53. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  54. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  55. package/dist/types/compute-engine/library/core.d.ts +1 -1
  56. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  57. package/dist/types/compute-engine/library/library.d.ts +1 -1
  58. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  59. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  60. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  61. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  62. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  63. package/dist/types/compute-engine/numerics/{numeric-decimal.d.ts → numeric-bignum.d.ts} +10 -6
  64. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  65. package/dist/types/compute-engine/numerics/numeric.d.ts +10 -6
  66. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  67. package/dist/types/compute-engine/numerics/rationals.d.ts +56 -0
  68. package/dist/types/compute-engine/public.d.ts +205 -254
  69. package/dist/types/compute-engine/rules.d.ts +7 -1
  70. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  71. package/dist/types/compute-engine/solve.d.ts +17 -0
  72. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  73. package/dist/types/compute-engine/symbolic/flatten.d.ts +2 -1
  74. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  75. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  76. package/dist/types/compute-engine/symbolic/product.d.ts +30 -12
  77. package/dist/types/compute-engine/symbolic/sum.d.ts +13 -7
  78. package/dist/types/compute-engine/symbolic/utils.d.ts +7 -29
  79. package/dist/types/compute-engine.d.ts +2 -2
  80. package/dist/types/math-json/math-json-format.d.ts +2 -2
  81. package/dist/types/math-json/utils.d.ts +11 -41
  82. package/dist/types/math-json.d.ts +3 -3
  83. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- /* 0.8.0 */
1
+ /* 0.10.0 */
2
2
  * The most important classes are {@link ComputeEngine} and
3
3
  * {@link BoxedExpression}.
4
4
  *
@@ -13,6 +13,7 @@ import type { SignalMessage, WarningSignal, WarningSignalHandler } from '../comm
13
13
  import type { Expression, MathJsonDictionary, MathJsonFunction, MathJsonNumber, MathJsonString, MathJsonSymbol } from '../math-json/math-json-format';
14
14
  import type { NumberFormattingOptions, ParseLatexOptions, SerializeLatexOptions } from './latex-syntax/public';
15
15
  export * from './latex-syntax/public';
16
+ export declare type Rational = [number, number] | [Decimal, Decimal];
16
17
  /**
17
18
  * Metadata that can be associated with a `BoxedExpression`
18
19
  */
@@ -23,18 +24,19 @@ export declare type Metadata = {
23
24
  /**
24
25
  * The numeric evaluation mode:
25
26
  *
26
- * - `"auto"`: use machine number if precision is 15 or less, allow complex numbers.
27
- * - `"machine"`: 64-bit float, **IEEE 754-2008**, 64-bit float, 52-bit mantissa,
28
- * about 15 digits of precision
29
- * - `"decimal"`: arbitrary precision floating point numbers, as provided by the
30
- * "decimal.js" library
31
- * - `"complex"`: complex number represented by two machine numbers, a real and
32
- * an imaginary part, as provided by the "complex.js" library
33
- */
34
- export declare type NumericMode = 'auto' | 'machine' | 'decimal' | 'complex';
35
- /** Options for `BoxedExpression.simplify()`
36
- *
27
+ <div class=symbols-table>
28
+
29
+ | Mode | |
30
+ | :--- | :----- |
31
+ | `"auto"`| Use bignum or complex numbers. |
32
+ | `"machine"` | **IEEE 754-2008**, 64-bit floating point numbers: 52-bit mantissa, about 15 digits of precision |
33
+ | `"bignum"` | Arbitrary precision floating point numbers, as provided by the "decimal.js" library |
34
+ | `"complex"` | Complex number represented by two machine numbers, a real and an imaginary part, as provided by the "complex.js" library |
35
+
36
+ </div>
37
37
  */
38
+ export declare type NumericMode = 'auto' | 'machine' | 'bignum' | 'complex';
39
+ /** Options for `BoxedExpression.simplify()` */
38
40
  export declare type SimplifyOptions = EvaluateOptions & {
39
41
  recursive?: boolean;
40
42
  rules?: BoxedRuleSet;
@@ -207,7 +209,14 @@ export declare type JsonSerializationOptions = {
207
209
  *
208
210
  * **Default**: `true`
209
211
  */
210
- repeatingDecimal: boolean;
212
+ repeatingDecimals: boolean;
213
+ /** Number literals are serialized with this precision.
214
+ * If `"auto"`, the same precision as the compute engine calculations is used
215
+ * If `"max"`, all available digits are serialized
216
+ *
217
+ * **Default**: `"auto"`
218
+ */
219
+ precision: 'auto' | 'max' | number;
211
220
  };
212
221
  /**
213
222
  * **Theory of Operations**
@@ -231,7 +240,7 @@ export interface BoxedExpression {
231
240
  readonly engine: IComputeEngine;
232
241
  /** From `Object.valueOf()`, return a primitive value for the expression.
233
242
  *
234
- * If the expression is a machine number, or a Decimal or rational that can be
243
+ * If the expression is a machine number, or bignum or rational that can be
235
244
  * converted to a machine number, return a `number`.
236
245
  *
237
246
  * If the expression is a symbol, return the name of the symbol as a `string`.
@@ -275,6 +284,11 @@ export interface BoxedExpression {
275
284
  *
276
285
  */
277
286
  readonly json: Expression;
287
+ /**
288
+ * The scope in which this expression has been defined.
289
+ * Is null when the expression is not canonical.
290
+ */
291
+ readonly scope: RuntimeScope | null;
278
292
  /** From `Object.is()`. Equivalent to `BoxedExpression.isSame()`
279
293
  *
280
294
  * @category Primitive Methods
@@ -418,15 +432,15 @@ export interface BoxedExpression {
418
432
  */
419
433
  readonly isValid: boolean;
420
434
  /**
421
- * If `true`, this expression represents a value that was not calculated
422
- * and that does not reference another expression.
435
+ * An exact value is not further transformed when evaluated. To get an
436
+ * approximate evaluation of an exact value, use `.N()`.
423
437
  *
424
- * This means the expression is either a number, a string or a dictionary.
425
- * Functions and symbols are not literals.
438
+ * Non-exact values includes:
439
+ * - numbers with a fractional part
440
+ * - complex numbers with a real or imaginary fractional part
426
441
  *
427
- * **Note** applicable to canonical and non-canonical expressions.
428
442
  */
429
- readonly isLiteral: boolean;
443
+ readonly isExact: boolean;
430
444
  /** If true, the value of the expression never changes and evaluating it has
431
445
  * no side-effects.
432
446
  * If false, the value of the expression may change, if the
@@ -447,28 +461,18 @@ export interface BoxedExpression {
447
461
  /**
448
462
  * Return the canonical form of this expression.
449
463
  *
450
- * If a function, after putting all the arguments in canonical form, find
451
- * a corresponding function definition in the current context.
464
+ * If this is a function expressin, a definition is associated with the
465
+ * canonical expression.
452
466
  *
453
- * Apply the function definition flags:
467
+ * When determining the canonical form the following function definition
468
+ * flags are applied:
454
469
  * - `associative`: \\( f(a, f(b), c) \longrightarrow f(a, b, c) \\)
455
470
  * - `idempotent`: \\( f(f(a)) \longrightarrow f(a) \\)
456
471
  * - `involution`: \\( f(f(a)) \longrightarrow a \\)
457
472
  * - `commutative`: sort the arguments.
458
473
  *
459
- * Additionally, some simplifications involving exact computations on
460
- * small integers may be performed.
461
- *
462
- * For example:
463
- * - \\( 2 + x + 1 \longrightarrow x + 3 \\)
464
- * - \\( \sqrt{4} \longrightarrow 2 \\)
465
- * - \\(\frac{4}{10} \longrightarrow \frac{2}{5} \\).
466
- *
467
- * However, no calculation is performed involving floating point numbers, so
468
- * \\( \sqrt(2) \longrightarrow \sqrt(2) \\).
469
- *
470
- * **Note** applicable to canonical and non-canonical expressions.
471
- * Expressions that are already canonical return themselves.
474
+ * If his expression is already canonical, the value of canonical is
475
+ * `this`.
472
476
  *
473
477
  */
474
478
  get canonical(): BoxedExpression;
@@ -496,7 +500,9 @@ export interface BoxedExpression {
496
500
  * **Note** applicable to canonical and non-canonical expressions.
497
501
  *
498
502
  */
499
- subs(sub: Substitution): BoxedExpression;
503
+ subs(sub: Substitution, options?: {
504
+ canonical?: boolean;
505
+ }): BoxedExpression;
500
506
  /**
501
507
  * Transform the expression by applying the rules:
502
508
  * if the `lhs` of a rule matches, it is replaced by its `rhs`.
@@ -541,7 +547,7 @@ export interface BoxedExpression {
541
547
  * **Note** applicable to canonical and non-canonical expressions.
542
548
  *
543
549
  */
544
- match(rhs: BoxedExpression, options?: PatternMatchOption): Substitution | null;
550
+ match(rhs: BoxedExpression, options?: PatternMatchOptions): BoxedSubstitution | null;
545
551
  /**
546
552
  * "Not a Number".
547
553
  *
@@ -603,94 +609,15 @@ export interface BoxedExpression {
603
609
  */
604
610
  readonly isComposite: boolean | undefined;
605
611
  /**
606
- * Return the value of this expression, if stored as a machine
607
- * number.
612
+ * Return the value of this expression, if a number literal.
608
613
  *
609
- * Note it is possible for `machineValue` to be `null`, and for `isNotZero`
614
+ * Note it is possible for `numericValue` to be `null`, and for `isNotZero`
610
615
  * to be true. For example, when a symbol has been defined with an assumption.
611
616
  *
612
- * If `machineValue` is not `null`, then `decimalValue`, `rationalValue`
613
- * and `complexValue` are `null.
614
- *
615
- * @category Numeric Expression
616
- *
617
- */
618
- readonly machineValue: number | null;
619
- /** If the value of this expression is a rational number, return it.
620
- * Otherwise, return `[null, null]`.
621
- *
622
- * If `rationalValue` is not `[null, null]`, then `machineValue`, `decimalValue`
623
- * and `complexValue` are `null.
624
- *
625
- * @category Numeric Expression
626
- *
627
- */
628
- readonly rationalValue: [numer: number, denom: number] | [null, null];
629
- /** If the value of this expression is a `Decimal` number, return it.
630
- * Otherwise, return `null`.
631
- *
632
- * A `Decimal` number is an arbitrarily long floating point number.
633
- *
634
- * If `decimalValue` is not `null`, then `machineValue`
635
- * and `complexValue` are `null` and `rationalValue` is `[null, null]`.
636
- *
637
617
  * @category Numeric Expression
638
618
  *
639
619
  */
640
- readonly decimalValue: Decimal | null;
641
- /** If the value of this expression is a `Complex` number, return it.
642
- * Otherwise, return `null`.
643
- *
644
- * If `complexValue` is not `null`, then `machineValue`, `rationalValue`
645
- * and `decimalValue` are `null.
646
- *
647
- * @category Numeric Expression
648
- *
649
- *
650
- */
651
- readonly complexValue: Complex | null;
652
- /** Return an approximation of the numeric value of this expression as
653
- * a 64-bit floating point number.
654
- *
655
- * If the value is a machine number, return it exactly.
656
- *
657
- * If the value is a rational number, return the numerator divided by the
658
- * denominator.
659
- *
660
- * If the value is a Decimal number return an approximation of the decimal
661
- * number to a machine number. There might be a loss of precision or a
662
- * round to 0 or Infinity, depending on the value.
663
- *
664
- * If the value of this expression cannot be represented by a float,
665
- * return `null`.
666
- *
667
- * @category Numeric Expression
668
- *
669
- *
670
- */
671
- readonly asFloat: number | null;
672
- /**
673
- * If the value of this expression is an integer with a 'small' absolute
674
- * value, return this value. Otherwise, return `null`.
675
- *
676
- * Some calculations, for example to put in canonical forms, are only
677
- * performed if they are safe from overflow. This method makes it easy
678
- * to check for this, whether the value is a Decimal or a number.
679
- *
680
- * By default, "small" is less than 1,000,000.
681
- *
682
- * @category Numeric Expression
683
- *
684
- */
685
- readonly asSmallInteger: number | null;
686
- /**
687
- * If the value of this an expression is a small integer or a rational,
688
- * return this value. Otherwise, return `[null, null]`.
689
- *
690
- * @category Numeric Expression
691
- *
692
- */
693
- readonly asRational: [number, number] | [null, null];
620
+ readonly numericValue: number | Decimal | Complex | Rational | null;
694
621
  /**
695
622
  * Return the following, depending on the value of this expression:
696
623
  *
@@ -864,19 +791,21 @@ export interface BoxedExpression {
864
791
  */
865
792
  unbind(): void;
866
793
  /**
867
- * Return a simpler form of this expression.
794
+ * Return a simpler form of the canonical form of this expression.
868
795
  *
869
- * The expression is first converted to canonical form. Then a series of
870
- * rewriting rules are applied repeatedly, until no rules apply.
796
+ * A series of rewriting rules are applied repeatedly, until no more rules
797
+ * apply.
871
798
  *
872
- * If a custom `simplify` handler is associated with this function definition,
873
- * it is invoked.
799
+ * If a custom `simplify` handler is associated with this function
800
+ * definition, it is invoked.
874
801
  *
875
802
  * The values assigned to symbols and the assumptions about symbols may be
876
803
  * used, for example `arg.isInteger` or `arg.isPositive`.
877
804
  *
878
- * No calculations involving floating point numbers are performed but exact
879
- * calculations may be performed, for example
805
+ * No calculations involving decimal numbers (numbers that are not
806
+ * integers) are performed but exact calculations may be performed,
807
+ * for example:
808
+ *
880
809
  * \\( \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} \\).
881
810
  *
882
811
  * The result is in canonical form.
@@ -884,33 +813,37 @@ export interface BoxedExpression {
884
813
  */
885
814
  simplify(options?: SimplifyOptions): BoxedExpression;
886
815
  /**
887
- * Return the value of this expression.
888
- *
889
- * The expression is first converted to canonical form.
816
+ * Return the value of the canonical form of this expression.
890
817
  *
891
818
  * A pure expression always return the same value and has no side effects.
892
- * If `this.isPure` is `true`, `this.value` and `this.evaluate()` are synonyms.
893
- * For an impure expression, `this.value` is undefined.
819
+ * If `expr.isPure` is `true`, `expr.value` and `expr.evaluate()` are
820
+ * synonyms.
821
+ *
822
+ * For an impure expression, `expr.value` is undefined.
894
823
  *
895
824
  * Evaluating an impure expression may have some side effects, for
896
- * example modifying the `ComputeEngine` environment, such as its set of assumptions.
825
+ * example modifying the `ComputeEngine` environment, such as its set of
826
+ * assumptions.
827
+ *
828
+ * Only exact calculations are performed, no approximate calculations on
829
+ * decimal numbers (non-integer numbers). Constants, rational numbers and
830
+ * square root of rational numbers are preserved.
897
831
  *
898
- * Only exact calculations are performed, no floating point calculations.
899
- * To perform approximate floating point calculations, use `this.N()` instead.
832
+ * To perform approximate calculations, use `expr.N()` instead.
900
833
  *
901
- * The result of `this.evaluate()` may be the same as `this.simplify()`.
834
+ * The result of `expr.evaluate()` may be the same as `expr.simplify()`.
902
835
  *
903
836
  * The result is in canonical form.
904
837
  *
905
838
  */
906
839
  evaluate(options?: EvaluateOptions): BoxedExpression;
907
- /** Return a numeric approximation of this expression.
840
+ /** Return a numeric approximation of the canonical form of this expression.
908
841
  *
909
- * The expression is first converted to canonical form.
842
+ * Any necessary calculations, including on decimal numbers (non-integers),
843
+ * are performed.
910
844
  *
911
- * Any necessary calculations, including on floating point numbers,
912
- * are performed. The calculations are performed according
913
- * to the `numericMode` and `precision` properties of the `ComputeEngine`.
845
+ * The calculations are performed according to the `numericMode` and
846
+ * `precision` properties of the `ComputeEngine`.
914
847
  *
915
848
  * To only perform exact calculations, use `this.evaluate()` instead.
916
849
  *
@@ -938,16 +871,6 @@ export interface BoxedExpression {
938
871
  *
939
872
  */
940
873
  set value(value: BoxedExpression | number | undefined);
941
- /** An approximation of the value of this expression. Floating-point
942
- * operations may be performed.
943
- *
944
- * Just like `this.value`, it returns `undefined` for expressions that are
945
- * not pure.
946
- *
947
- * **Note**: If non-canonical, return the numeric value of its canonical
948
- * counterpart
949
- */
950
- readonly numericValue: BoxedExpression | undefined;
951
874
  /** The domain of the value of this expression.
952
875
  *
953
876
  * If a function expression, the domain of the value of the function (the codomain of the function).
@@ -966,7 +889,7 @@ export interface BoxedExpression {
966
889
  * **Note**: If non-canonical, does nothing.
967
890
  *
968
891
  */
969
- set domain(domain: BoxedDomain | string);
892
+ set domain(domain: BoxedExpression | DomainExpression | BoxedDomain);
970
893
  /** `true` if the value of this expression is a number.
971
894
  *
972
895
  * `isExtendedComplex || isNaN` = `isReal || isImaginary || isInfinity || isNaN`
@@ -1064,29 +987,18 @@ export interface BoxedExpression {
1064
987
  * This is convenient when creating new expressions from portions
1065
988
  * of an existing `BoxedExpression` while avoiding unboxing and reboxing.
1066
989
  */
1067
- export declare type SemiBoxedExpression = BoxedExpression | number | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | string | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[];
1068
- export declare type LambdaExpression = SemiBoxedExpression;
1069
- export declare type BoxedLambdaExpression = BoxedExpression;
1070
- export declare type PatternMatchOption = {
990
+ export declare type SemiBoxedExpression = number | string | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[] | BoxedExpression;
991
+ export declare type PatternMatchOptions = {
992
+ substitution?: BoxedSubstitution;
1071
993
  recursive?: boolean;
1072
994
  numericTolerance?: number;
1073
995
  exact?: boolean;
1074
996
  };
1075
997
  export interface Pattern extends BoxedExpression {
1076
- /**
1077
- * If `expr` does not match the pattern, return `null`.
1078
- *
1079
- * Otherwise, return a substitution describing the values that the named
1080
- * wildcard in the pattern should be changed to in order for the pattern to be
1081
- * equal to the expression. If there are no named wildcards and the expression
1082
- * matches the pattern, and empty object literal `{}` is returned.
1083
- */
1084
- match(expr: BoxedExpression, options?: PatternMatchOption): BoxedSubstitution | null;
1085
998
  /** If `expr` matches the pattern, return `true`, otherwise `false` */
1086
- test(expr: BoxedExpression, options?: PatternMatchOption): boolean;
999
+ test(expr: BoxedExpression, options?: PatternMatchOptions): boolean;
1087
1000
  /** Return the number of exprs that matched the pattern */
1088
- count(exprs: Iterable<BoxedExpression>, options?: PatternMatchOption): number;
1089
- subs(sub: Substitution): Pattern;
1001
+ count(exprs: Iterable<BoxedExpression>, options?: PatternMatchOptions): number;
1090
1002
  }
1091
1003
  export interface ExpressionMapInterface<U> {
1092
1004
  has(expr: BoxedExpression): boolean;
@@ -1163,7 +1075,7 @@ export declare type Scope = {
1163
1075
  iterationLimit?: number;
1164
1076
  };
1165
1077
  export declare type RuntimeScope = Scope & {
1166
- parentScope: RuntimeScope;
1078
+ parentScope?: RuntimeScope;
1167
1079
  symbolTable?: RuntimeSymbolTable;
1168
1080
  assumptions: undefined | ExpressionMapInterface<boolean>;
1169
1081
  /** The location of the call site that created this scope */
@@ -1304,12 +1216,6 @@ export declare type FunctionDefinitionFlags = {
1304
1216
  * and its value is numeric.
1305
1217
  */
1306
1218
  numeric: boolean;
1307
- /**
1308
- * When true, evaluating the function create a temporary scope.
1309
- * This is used for example by the `Lambda` function to keep track of the
1310
- * inferred domain of its wildcard `_` arguments
1311
- */
1312
- scoped: boolean;
1313
1219
  };
1314
1220
  /**
1315
1221
  *
@@ -1326,76 +1232,89 @@ export declare type FunctionSignature = {
1326
1232
  /**
1327
1233
  * Return the canonical form of the expression with the arguments `args`.
1328
1234
  *
1329
- * All the arguments that are not subject to a hold are in canonical form.
1330
- * Any `Nothing` argument has been removed.
1235
+ * The arguments (`args`) may not be in canonical form. If necessary, they
1236
+ * can be put in canonical form.
1331
1237
  *
1332
- * If the function is associative, idempotent or an involution,
1333
- * it should handle its arguments accordingly. Notably, if it
1334
- * is commutative, the arguments should be sorted in canonical order.
1238
+ * This handler should validate the domain and number of the arguments.
1239
+ * If a required argument is missing, it should be indicated with a
1240
+ * `["Error", "'missing"]` expression. If more arguments than expected
1241
+ * are present, this should be indicated with a `unexpected-argument` error.
1242
+ * If the domain of an argument is not compatible, it should be indicated with
1243
+ * a `incompatible-domain` error.
1335
1244
  *
1336
- * The handler can make transformations based on the value of the arguments
1337
- * that are literal and either rational numbers (i.e.
1338
- * `arg.isLiteral && arg.isRational`) or integers (i.e.
1339
- * `isLiteral && arg.isInteger`).
1245
+ * `["Sequence"]` expressions are not folded and need to be handled
1246
+ * explicitly.
1340
1247
  *
1341
- * The handler should not consider the value of the arguments
1342
- * that are symbols or functions.
1248
+ * If the function is associative, idempotent or an involution,
1249
+ * this handler should account for it. Notably, if it is commutative, the
1250
+ * arguments should be sorted in canonical order.
1343
1251
  *
1344
- * The handler should not consider any assumptions about any of the
1345
- * arguments that are symbols or functions i.e. `arg.isZero`,
1346
- * `arg.isInteger`, etc...
1252
+ * The handler can make transformations based on the value of the arguments
1253
+ * that are exact (i.e. `arg.isExact`).
1347
1254
  *
1348
- * The handler should not make transformations based on the value of
1349
- * floating point numbers.
1255
+ * The handler should not consider the value or any assumptions about any
1256
+ * of the arguments that are symbols or functions (i.e. `arg.isZero`,
1257
+ * `arg.isInteger`, etc...) since those may change over time.
1350
1258
  *
1351
1259
  * The result of the handler should be a canonical expression.
1352
1260
  *
1261
+ * If the arguments do not match, they should be replaced with an appropriate
1262
+ * `["Error"]` expression. If the expression cannot be put in canonical form,
1263
+ * the handler should return `null`.
1264
+ *
1353
1265
  */
1354
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1266
+ canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1355
1267
  /**
1356
1268
  * Rewrite an expression into a simpler form.
1357
1269
  *
1358
1270
  * The arguments are in canonical form and have been simplified.
1359
1271
  *
1360
- * The handler can use the values assigned to symbols and the assumptions about
1361
- * symbols, for example with `arg.machineValue`, `arg.isInteger` or
1272
+ * The handler can use the values assigned to symbols and the assumptions
1273
+ * about symbols, for example with `arg.numericValue`, `arg.isInteger` or
1362
1274
  * `arg.isPositive`.
1363
1275
  *
1364
1276
  * Even though a symbol may not have a value, there may be some information
1365
1277
  * about it reflected for example in `this.isZero` or `this.isPrime`.
1366
1278
  *
1367
1279
  * The handler should not perform approximate numeric calculations, such
1368
- * as calculations involving floating point numbers. Making exact
1369
- * calculations on integers or rationals is OK. It is recommended, but not
1370
- * required, that the calculations be limited to `this.smallIntegerValue`
1371
- * (i.e. numeric representations of the expression as an integer of small
1372
- * magnitude).
1280
+ * as calculations involving decimal numbers (non-integers). Making exact
1281
+ * calculations on integers or rationals is OK.
1373
1282
  *
1374
1283
  * This handler should not have any side-effects: do not modify
1375
1284
  * the environment of the `ComputeEngine` instance, do not perform I/O,
1376
1285
  * do not do calculations that depend on random values.
1377
1286
  *
1378
- * If no simplification can be performed due to the values, domains or assumptions
1379
- * about its arguments, for example, return `undefined`.
1287
+ * If no simplification can be performed due to the values, domains or
1288
+ * assumptions about its arguments, for example, return `undefined`.
1380
1289
  *
1381
1290
  */
1382
1291
  simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1383
1292
  /**
1384
- * Evaluate symbolically an expression.
1293
+ * Evaluate symbolically a function expression.
1385
1294
  *
1386
1295
  * The arguments have been symbolically evaluated, except the arguments to
1387
1296
  * which a `hold` apply.
1388
1297
  *
1389
1298
  * It is not necessary to further simplify or evaluate the arguments.
1390
1299
  *
1391
- * If the expression cannot be evaluated, due to the values, domains, or
1392
- * assumptions about its arguments, for example, return `undefined`.
1300
+ * If performing numerical calculations, if all the arguments are exact,
1301
+ * return an exact expression. If any of the arguments is not exact, that is
1302
+ * if it is a literal decimal (non-integer) number, return an approximation.
1303
+ * In this case, the value may be the same as `expr.N()`.
1393
1304
  *
1305
+ * When doing an exact calculation:
1306
+ * - do not reduce rational numbers to decimal (floating point approximation)
1307
+ * - do not down convert bignums to machine numbers
1308
+ * - do not reduce square roots of rational numbers
1309
+ * - do not reduce constants with a `hold` attribute
1394
1310
  *
1311
+ * If the expression cannot be evaluated, due to the values, domains, or
1312
+ * assumptions about its arguments, for example, return `undefined` or
1313
+ * an `["Error"]` expression.
1395
1314
  */
1396
- evaluate?: LambdaExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1315
+ evaluate?: SemiBoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1397
1316
  /**
1398
- * Evaluate numerically an expression.
1317
+ * Evaluate numerically a function expression.
1399
1318
  *
1400
1319
  * The arguments `args` have been simplified and evaluated, numerically
1401
1320
  * if possible, except the arguments to which a `hold` apply.
@@ -1414,19 +1333,22 @@ export declare type FunctionSignature = {
1414
1333
  * evaluation, but a literal argument is out of range or
1415
1334
  * not of the expected type.
1416
1335
  *
1417
- * Also return `NaN` if the result of the evaluation would be a complex
1418
- * number, but complex numbers are not allowed (the `engine.numericMode`
1419
- * is not `complex` or `auto`).
1336
+ * Note that regardless of the current value of `ce.numericMode`, the
1337
+ * arguments may be boxed numbers representing machine numbers, bignum
1338
+ * numbers, complex numbers, rationals or big rationals.
1420
1339
  *
1421
- * If the `ce.numericMode` is `auto` or `complex`, you may return
1422
- * a Complex number as a result. Otherwise, if the result is a complex
1423
- * value, return `NaN`. If Complex are not allowed, none of the arguments
1424
- * will be complex literals.
1340
+ * Use the value of `ce.numericMode` to determine how to perform
1341
+ * the numeric evaluation.
1425
1342
  *
1426
- * If the `ce.numericMode` is `decimal` or `auto` and
1427
- * `this.engine.precision` is > 15, you may return a Decimal number.
1428
- * Otherwise, return a `machine` number approximation. If Decimal are
1429
- * not allowed, none of the arguments will be Decimal literal.
1343
+ * If the numeric mode does not allow complex numbers (the
1344
+ * `engine.numericMode` is not `"complex"` or `"auto"`) and the result of
1345
+ * the evaluation would be a complex number, return `NaN` instead.
1346
+ *
1347
+ * If `ce.numericMode` is `"bignum"` or `"auto"` the evaluation should be done
1348
+ * using bignums.
1349
+ *
1350
+ * Otherwise, `ce.numericMode` is `"machine", the evaluation should be
1351
+ * performed using machine numbers.
1430
1352
  *
1431
1353
  * You may perform any necessary computations, including approximate
1432
1354
  * calculations on floating point numbers.
@@ -1437,17 +1359,17 @@ export declare type FunctionSignature = {
1437
1359
  * @experimental
1438
1360
  */
1439
1361
  evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1440
- /** Return the sign of the function given a list of arguments. */
1362
+ /** Return the sign of the function expression. */
1441
1363
  sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
1442
1364
  /** Return a compiled (optimized) expression. */
1443
1365
  compile?: (expr: BoxedExpression) => CompiledExpression;
1444
1366
  };
1445
1367
  export declare type BoxedFunctionSignature = {
1446
1368
  domain: BoxedDomain;
1447
- codomain?: BoxedDomain | ((ce: IComputeEngine, args: BoxedDomain[]) => BoxedDomain | null);
1448
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1369
+ codomain?: BoxedDomain | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedDomain | null);
1370
+ canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1449
1371
  simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1450
- evaluate?: BoxedLambdaExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1372
+ evaluate?: BoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1451
1373
  N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1452
1374
  evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1453
1375
  sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
@@ -1562,7 +1484,7 @@ export declare type SymbolDefinition = BaseDefinition & Partial<SymbolFlags> & P
1562
1484
  };
1563
1485
  export interface BoxedSymbolDefinition extends BoxedBaseDefinition, Partial<SymbolFlags>, SymbolDefinitionFlags {
1564
1486
  get value(): BoxedExpression | undefined;
1565
- set value(val: BoxedExpression | undefined);
1487
+ set value(val: SemiBoxedExpression | number | undefined);
1566
1488
  domain: BoxedDomain | undefined;
1567
1489
  at?: (index: string | number) => undefined | BoxedExpression;
1568
1490
  }
@@ -1584,8 +1506,6 @@ export interface IComputeEngine {
1584
1506
  /** @internal */
1585
1507
  readonly _ONE: BoxedExpression;
1586
1508
  /** @internal */
1587
- readonly _TWO: BoxedExpression;
1588
- /** @internal */
1589
1509
  readonly _HALF: BoxedExpression;
1590
1510
  /** @internal */
1591
1511
  readonly _NEGATIVE_ONE: BoxedExpression;
@@ -1600,19 +1520,19 @@ export interface IComputeEngine {
1600
1520
  /** @internal */
1601
1521
  readonly _COMPLEX_INFINITY: BoxedExpression;
1602
1522
  /** @internal */
1603
- readonly _DECIMAL_NAN: Decimal;
1523
+ readonly _BIGNUM_NAN: Decimal;
1604
1524
  /** @internal */
1605
- readonly _DECIMAL_ZERO: Decimal;
1525
+ readonly _BIGNUM_ZERO: Decimal;
1606
1526
  /** @internal */
1607
- readonly _DECIMAL_ONE: Decimal;
1527
+ readonly _BIGNUM_ONE: Decimal;
1608
1528
  /** @internal */
1609
- readonly _DECIMAL_TWO: Decimal;
1529
+ readonly _BIGNUM_TWO: Decimal;
1610
1530
  /** @internal */
1611
- readonly _DECIMAL_HALF: Decimal;
1531
+ readonly _BIGNUM_HALF: Decimal;
1612
1532
  /** @internal */
1613
- readonly _DECIMAL_PI: Decimal;
1533
+ readonly _BIGNUM_PI: Decimal;
1614
1534
  /** @internal */
1615
- readonly _DECIMAL_NEGATIVE_ONE: Decimal;
1535
+ readonly _BIGNUM_NEGATIVE_ONE: Decimal;
1616
1536
  /** The current scope */
1617
1537
  context: RuntimeScope | null;
1618
1538
  /** Absolute time beyond which evaluation should not proceed
@@ -1634,12 +1554,20 @@ export interface IComputeEngine {
1634
1554
  chop(n: Complex): Complex | 0;
1635
1555
  chop(n: number | Decimal | Complex): number | Decimal | Complex;
1636
1556
  /** @internal */
1637
- decimal: (a: Decimal.Value) => Decimal;
1557
+ bignum: (a: Decimal.Value) => Decimal;
1638
1558
  /** @internal */
1639
1559
  complex: (a: number | Complex, b?: number) => Complex;
1640
1560
  set precision(p: number | 'machine');
1641
1561
  get precision(): number;
1642
1562
  costFunction: (expr: BoxedExpression) => number;
1563
+ /** In strict mode (the default) the Compute Engine performs
1564
+ * validation of domains and signature and may report errors.
1565
+ *
1566
+ * When strict mode is off, results may be incorrect or generate JavaScript
1567
+ * errors if the input is not valid.
1568
+ *
1569
+ */
1570
+ strict: boolean;
1643
1571
  /**
1644
1572
  * Associate a new definition to a symbol in the current context.
1645
1573
  *
@@ -1654,17 +1582,23 @@ export interface IComputeEngine {
1654
1582
  defineFunction(def: FunctionDefinition): BoxedFunctionDefinition;
1655
1583
  lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
1656
1584
  /** Return `undefined` if no definition exist for this `head` */
1657
- lookupFunction(head: string, scope?: RuntimeScope): undefined | BoxedFunctionDefinition;
1585
+ lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
1658
1586
  /**
1659
1587
  * Return a boxed expression from the input.
1660
- *
1661
- * The result may not be canonical.
1662
1588
  */
1663
- box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression): BoxedExpression;
1664
- /** Return a canonical boxed number */
1665
- number(value: number | MathJsonNumber | Decimal | Complex | [num: number, denom: number], metadata?: Metadata): BoxedExpression;
1589
+ box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression, options?: {
1590
+ canonical?: boolean;
1591
+ }): BoxedExpression;
1592
+ /** Return a boxed number */
1593
+ number(value: number | string | MathJsonNumber | Decimal | Complex | [num: number, denom: number] | [num: Decimal, denom: Decimal], options?: {
1594
+ metadata?: Metadata;
1595
+ canonical?: boolean;
1596
+ }): BoxedExpression;
1666
1597
  /** Return a canonical boxed symbol */
1667
- symbol(sym: string, metadata?: Metadata): BoxedExpression;
1598
+ symbol(sym: string, options?: {
1599
+ metadata?: Metadata;
1600
+ canonical?: boolean;
1601
+ }): BoxedExpression;
1668
1602
  /** Return a canonical boxed string */
1669
1603
  string(s: string, metadata?: Metadata): BoxedExpression;
1670
1604
  /** Return a canonical boxed domain.
@@ -1673,8 +1607,6 @@ export interface IComputeEngine {
1673
1607
  *
1674
1608
  */
1675
1609
  domain(domain: SemiBoxedExpression | BoxedDomain | string, metadata?: Metadata): BoxedDomain;
1676
- /** Return a canonical lambda expression */
1677
- lambda(expr: SemiBoxedExpression, sig: BoxedDomain): BoxedLambdaExpression;
1678
1610
  /**
1679
1611
  * Return a canonical expression.
1680
1612
  *
@@ -1690,7 +1622,7 @@ export interface IComputeEngine {
1690
1622
  * This is a primitive to create a boxed function. It doesn't perform
1691
1623
  * any checks or normalization on its arguments.
1692
1624
  *
1693
- * In general, consider using `fn()` or `box()` instead.
1625
+ * In general, consider using `ce.fn()` or `ce.box()` instead.
1694
1626
  *
1695
1627
  * The result is canonical, but the caller has to ensure that all the
1696
1628
  * conditions are met (i.e. `ops` properly normalized and sorted, all
@@ -1702,6 +1634,10 @@ export interface IComputeEngine {
1702
1634
  * The result is canonical.
1703
1635
  */
1704
1636
  error(message: string | [string, ...SemiBoxedExpression[]], where?: SemiBoxedExpression): BoxedExpression;
1637
+ /**
1638
+ * Add a`["Hold"]` wrapper to `expr.
1639
+ */
1640
+ hold(expr: SemiBoxedExpression): BoxedExpression;
1705
1641
  /** Shortcut for `this.fn("Add"...)`.
1706
1642
  *
1707
1643
  * The result is canonical.
@@ -1716,7 +1652,8 @@ export interface IComputeEngine {
1716
1652
  *
1717
1653
  * The result is canonical.
1718
1654
  */
1719
- power(base: BoxedExpression, exponent: number | [number, number] | BoxedExpression, metadata?: Metadata): BoxedExpression;
1655
+ power(base: BoxedExpression, exponent: number | Rational | BoxedExpression, metadata?: Metadata): BoxedExpression;
1656
+ sqrt(base: BoxedExpression, metadata?: Metadata): any;
1720
1657
  /** Shortcut for `this.fn("Divide", [1, expr])`
1721
1658
  *
1722
1659
  * The result is canonical.
@@ -1750,9 +1687,15 @@ export interface IComputeEngine {
1750
1687
  * The result may not be canonical.
1751
1688
  *
1752
1689
  */
1753
- parse(s: LatexString | string): BoxedExpression;
1754
- parse(s: null): null;
1755
- parse(s: LatexString | string | null): null | BoxedExpression;
1690
+ parse(s: LatexString | string, options?: {
1691
+ canonical?: boolean;
1692
+ }): BoxedExpression;
1693
+ parse(s: null, options?: {
1694
+ canonical?: boolean;
1695
+ }): null;
1696
+ parse(s: LatexString | string | null, options?: {
1697
+ canonical?: boolean;
1698
+ }): null | BoxedExpression;
1756
1699
  /** Serialize a `BoxedExpression` or a `MathJSON` expression to
1757
1700
  * a LaTeX string
1758
1701
  */
@@ -1770,8 +1713,22 @@ export interface IComputeEngine {
1770
1713
  get latexOptions(): NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions;
1771
1714
  set latexOptions(opts: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions> & Partial<SerializeLatexOptions>);
1772
1715
  /** {@inheritDoc JsonSerializationOptions} */
1773
- get jsonSerializationOptions(): JsonSerializationOptions;
1716
+ get jsonSerializationOptions(): Readonly<JsonSerializationOptions>;
1774
1717
  set jsonSerializationOptions(val: Partial<JsonSerializationOptions>);
1718
+ pushScope(options?: {
1719
+ symbolTable?: Readonly<SymbolTable> | Readonly<SymbolTable>[];
1720
+ assumptions?: (LatexString | Expression | BoxedExpression)[];
1721
+ scope?: Partial<Scope>;
1722
+ }): void;
1723
+ popScope(): void;
1724
+ /** Assign a value to an identifier in the current scope. Use `null` to reset the identifier to no value */
1725
+ set(identifiers: {
1726
+ [identifier: string]: SemiBoxedExpression | null;
1727
+ }): void;
1728
+ /** Declare identifiers (specify their domain without necessarily assigning them a value in the current scope*/
1729
+ let(identifiers: {
1730
+ [identifier: string]: SymbolDefinition | FunctionDefinition;
1731
+ }): void;
1775
1732
  /**
1776
1733
  * Add an assumption.
1777
1734
  *
@@ -1793,13 +1750,7 @@ export interface IComputeEngine {
1793
1750
  /** Remove all assumptions about one or more symbols */
1794
1751
  forget(symbol?: string | string[]): void;
1795
1752
  get assumptions(): ExpressionMapInterface<boolean>;
1796
- ask(pattern: LatexString | SemiBoxedExpression): Substitution[];
1797
- pushScope(options?: {
1798
- symbolTable?: Readonly<SymbolTable> | Readonly<SymbolTable>[];
1799
- assumptions?: (LatexString | Expression | BoxedExpression)[];
1800
- scope?: Partial<Scope>;
1801
- }): void;
1802
- popScope(): void;
1753
+ ask(pattern: LatexString | SemiBoxedExpression): BoxedSubstitution[];
1803
1754
  /**
1804
1755
  * When `condition` is false, signal.
1805
1756
  *