@cortex-js/compute-engine 0.8.0 → 0.9.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 +7800 -6891
  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 -14
  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 -14
  17. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +20 -19
  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 -3
  20. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  21. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +18 -12
  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 +64 -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 +200 -247
  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.9.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
@@ -427,6 +441,16 @@ export interface BoxedExpression {
427
441
  * **Note** applicable to canonical and non-canonical expressions.
428
442
  */
429
443
  readonly isLiteral: boolean;
444
+ /**
445
+ * An exact value is not further transformed when evaluated. To get an
446
+ * approximate evaluation of an exact value, use `.N()`.
447
+ *
448
+ * Non-exact values includes:
449
+ * - numbers with a fractional part
450
+ * - complex numbers with a real or imaginary fractional part
451
+ *
452
+ */
453
+ readonly isExact: boolean;
430
454
  /** If true, the value of the expression never changes and evaluating it has
431
455
  * no side-effects.
432
456
  * If false, the value of the expression may change, if the
@@ -447,28 +471,18 @@ export interface BoxedExpression {
447
471
  /**
448
472
  * Return the canonical form of this expression.
449
473
  *
450
- * If a function, after putting all the arguments in canonical form, find
451
- * a corresponding function definition in the current context.
474
+ * If this is a function expressin, a definition is associated with the
475
+ * canonical expression.
452
476
  *
453
- * Apply the function definition flags:
477
+ * When determining the canonical form the following function definition
478
+ * flags are applied:
454
479
  * - `associative`: \\( f(a, f(b), c) \longrightarrow f(a, b, c) \\)
455
480
  * - `idempotent`: \\( f(f(a)) \longrightarrow f(a) \\)
456
481
  * - `involution`: \\( f(f(a)) \longrightarrow a \\)
457
482
  * - `commutative`: sort the arguments.
458
483
  *
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.
484
+ * If his expression is already canonical, the value of canonical is
485
+ * `this`.
472
486
  *
473
487
  */
474
488
  get canonical(): BoxedExpression;
@@ -496,7 +510,9 @@ export interface BoxedExpression {
496
510
  * **Note** applicable to canonical and non-canonical expressions.
497
511
  *
498
512
  */
499
- subs(sub: Substitution): BoxedExpression;
513
+ subs(sub: Substitution, options?: {
514
+ canonical?: boolean;
515
+ }): BoxedExpression;
500
516
  /**
501
517
  * Transform the expression by applying the rules:
502
518
  * if the `lhs` of a rule matches, it is replaced by its `rhs`.
@@ -541,7 +557,7 @@ export interface BoxedExpression {
541
557
  * **Note** applicable to canonical and non-canonical expressions.
542
558
  *
543
559
  */
544
- match(rhs: BoxedExpression, options?: PatternMatchOption): Substitution | null;
560
+ match(rhs: BoxedExpression, options?: PatternMatchOptions): BoxedSubstitution | null;
545
561
  /**
546
562
  * "Not a Number".
547
563
  *
@@ -603,94 +619,15 @@ export interface BoxedExpression {
603
619
  */
604
620
  readonly isComposite: boolean | undefined;
605
621
  /**
606
- * Return the value of this expression, if stored as a machine
607
- * number.
622
+ * Return the value of this expression, if a number literal.
608
623
  *
609
- * Note it is possible for `machineValue` to be `null`, and for `isNotZero`
624
+ * Note it is possible for `numericValue` to be `null`, and for `isNotZero`
610
625
  * to be true. For example, when a symbol has been defined with an assumption.
611
626
  *
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
627
  * @category Numeric Expression
638
628
  *
639
629
  */
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];
630
+ readonly numericValue: number | Decimal | Complex | Rational | null;
694
631
  /**
695
632
  * Return the following, depending on the value of this expression:
696
633
  *
@@ -864,19 +801,21 @@ export interface BoxedExpression {
864
801
  */
865
802
  unbind(): void;
866
803
  /**
867
- * Return a simpler form of this expression.
804
+ * Return a simpler form of the canonical form of this expression.
868
805
  *
869
- * The expression is first converted to canonical form. Then a series of
870
- * rewriting rules are applied repeatedly, until no rules apply.
806
+ * A series of rewriting rules are applied repeatedly, until no more rules
807
+ * apply.
871
808
  *
872
- * If a custom `simplify` handler is associated with this function definition,
873
- * it is invoked.
809
+ * If a custom `simplify` handler is associated with this function
810
+ * definition, it is invoked.
874
811
  *
875
812
  * The values assigned to symbols and the assumptions about symbols may be
876
813
  * used, for example `arg.isInteger` or `arg.isPositive`.
877
814
  *
878
- * No calculations involving floating point numbers are performed but exact
879
- * calculations may be performed, for example
815
+ * No calculations involving decimal numbers (numbers that are not
816
+ * integers) are performed but exact calculations may be performed,
817
+ * for example:
818
+ *
880
819
  * \\( \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} \\).
881
820
  *
882
821
  * The result is in canonical form.
@@ -884,33 +823,37 @@ export interface BoxedExpression {
884
823
  */
885
824
  simplify(options?: SimplifyOptions): BoxedExpression;
886
825
  /**
887
- * Return the value of this expression.
888
- *
889
- * The expression is first converted to canonical form.
826
+ * Return the value of the canonical form of this expression.
890
827
  *
891
828
  * 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.
829
+ * If `expr.isPure` is `true`, `expr.value` and `expr.evaluate()` are
830
+ * synonyms.
831
+ *
832
+ * For an impure expression, `expr.value` is undefined.
894
833
  *
895
834
  * Evaluating an impure expression may have some side effects, for
896
- * example modifying the `ComputeEngine` environment, such as its set of assumptions.
835
+ * example modifying the `ComputeEngine` environment, such as its set of
836
+ * assumptions.
837
+ *
838
+ * Only exact calculations are performed, no approximate calculations on
839
+ * decimal numbers (non-integer numbers). Constants, rational numbers and
840
+ * square root of rational numbers are preserved.
897
841
  *
898
- * Only exact calculations are performed, no floating point calculations.
899
- * To perform approximate floating point calculations, use `this.N()` instead.
842
+ * To perform approximate calculations, use `expr.N()` instead.
900
843
  *
901
- * The result of `this.evaluate()` may be the same as `this.simplify()`.
844
+ * The result of `expr.evaluate()` may be the same as `expr.simplify()`.
902
845
  *
903
846
  * The result is in canonical form.
904
847
  *
905
848
  */
906
849
  evaluate(options?: EvaluateOptions): BoxedExpression;
907
- /** Return a numeric approximation of this expression.
850
+ /** Return a numeric approximation of the canonical form of this expression.
908
851
  *
909
- * The expression is first converted to canonical form.
852
+ * Any necessary calculations, including on decimal numbers (non-integers),
853
+ * are performed.
910
854
  *
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`.
855
+ * The calculations are performed according to the `numericMode` and
856
+ * `precision` properties of the `ComputeEngine`.
914
857
  *
915
858
  * To only perform exact calculations, use `this.evaluate()` instead.
916
859
  *
@@ -938,16 +881,6 @@ export interface BoxedExpression {
938
881
  *
939
882
  */
940
883
  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
884
  /** The domain of the value of this expression.
952
885
  *
953
886
  * If a function expression, the domain of the value of the function (the codomain of the function).
@@ -966,7 +899,7 @@ export interface BoxedExpression {
966
899
  * **Note**: If non-canonical, does nothing.
967
900
  *
968
901
  */
969
- set domain(domain: BoxedDomain | string);
902
+ set domain(domain: BoxedExpression | DomainExpression | BoxedDomain);
970
903
  /** `true` if the value of this expression is a number.
971
904
  *
972
905
  * `isExtendedComplex || isNaN` = `isReal || isImaginary || isInfinity || isNaN`
@@ -1064,29 +997,18 @@ export interface BoxedExpression {
1064
997
  * This is convenient when creating new expressions from portions
1065
998
  * of an existing `BoxedExpression` while avoiding unboxing and reboxing.
1066
999
  */
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 = {
1000
+ export declare type SemiBoxedExpression = number | string | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[] | BoxedExpression;
1001
+ export declare type PatternMatchOptions = {
1002
+ substitution?: BoxedSubstitution;
1071
1003
  recursive?: boolean;
1072
1004
  numericTolerance?: number;
1073
1005
  exact?: boolean;
1074
1006
  };
1075
1007
  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
1008
  /** If `expr` matches the pattern, return `true`, otherwise `false` */
1086
- test(expr: BoxedExpression, options?: PatternMatchOption): boolean;
1009
+ test(expr: BoxedExpression, options?: PatternMatchOptions): boolean;
1087
1010
  /** Return the number of exprs that matched the pattern */
1088
- count(exprs: Iterable<BoxedExpression>, options?: PatternMatchOption): number;
1089
- subs(sub: Substitution): Pattern;
1011
+ count(exprs: Iterable<BoxedExpression>, options?: PatternMatchOptions): number;
1090
1012
  }
1091
1013
  export interface ExpressionMapInterface<U> {
1092
1014
  has(expr: BoxedExpression): boolean;
@@ -1163,7 +1085,7 @@ export declare type Scope = {
1163
1085
  iterationLimit?: number;
1164
1086
  };
1165
1087
  export declare type RuntimeScope = Scope & {
1166
- parentScope: RuntimeScope;
1088
+ parentScope?: RuntimeScope;
1167
1089
  symbolTable?: RuntimeSymbolTable;
1168
1090
  assumptions: undefined | ExpressionMapInterface<boolean>;
1169
1091
  /** The location of the call site that created this scope */
@@ -1304,12 +1226,6 @@ export declare type FunctionDefinitionFlags = {
1304
1226
  * and its value is numeric.
1305
1227
  */
1306
1228
  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
1229
  };
1314
1230
  /**
1315
1231
  *
@@ -1326,76 +1242,89 @@ export declare type FunctionSignature = {
1326
1242
  /**
1327
1243
  * Return the canonical form of the expression with the arguments `args`.
1328
1244
  *
1329
- * All the arguments that are not subject to a hold are in canonical form.
1330
- * Any `Nothing` argument has been removed.
1245
+ * The arguments (`args`) may not be in canonical form. If necessary, they
1246
+ * can be put in canonical form.
1331
1247
  *
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.
1248
+ * This handler should validate the domain and number of the arguments.
1249
+ * If a required argument is missing, it should be indicated with a
1250
+ * `["Error", "'missing"]` expression. If more arguments than expected
1251
+ * are present, this should be indicated with a `unexpected-argument` error.
1252
+ * If the domain of an argument is not compatible, it should be indicated with
1253
+ * a `incompatible-domain` error.
1335
1254
  *
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`).
1255
+ * `["Sequence"]` expressions are not folded and need to be handled
1256
+ * explicitly.
1340
1257
  *
1341
- * The handler should not consider the value of the arguments
1342
- * that are symbols or functions.
1258
+ * If the function is associative, idempotent or an involution,
1259
+ * this handler should account for it. Notably, if it is commutative, the
1260
+ * arguments should be sorted in canonical order.
1343
1261
  *
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...
1262
+ * The handler can make transformations based on the value of the arguments
1263
+ * that are exact (i.e. `arg.isExact`).
1347
1264
  *
1348
- * The handler should not make transformations based on the value of
1349
- * floating point numbers.
1265
+ * The handler should not consider the value or any assumptions about any
1266
+ * of the arguments that are symbols or functions (i.e. `arg.isZero`,
1267
+ * `arg.isInteger`, etc...) since those may change over time.
1350
1268
  *
1351
1269
  * The result of the handler should be a canonical expression.
1352
1270
  *
1271
+ * If the arguments do not match, they should be replaced with an appropriate
1272
+ * `["Error"]` expression. If the expression cannot be put in canonical form,
1273
+ * the handler should return `null`.
1274
+ *
1353
1275
  */
1354
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1276
+ canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1355
1277
  /**
1356
1278
  * Rewrite an expression into a simpler form.
1357
1279
  *
1358
1280
  * The arguments are in canonical form and have been simplified.
1359
1281
  *
1360
- * The handler can use the values assigned to symbols and the assumptions about
1361
- * symbols, for example with `arg.machineValue`, `arg.isInteger` or
1282
+ * The handler can use the values assigned to symbols and the assumptions
1283
+ * about symbols, for example with `arg.numericValue`, `arg.isInteger` or
1362
1284
  * `arg.isPositive`.
1363
1285
  *
1364
1286
  * Even though a symbol may not have a value, there may be some information
1365
1287
  * about it reflected for example in `this.isZero` or `this.isPrime`.
1366
1288
  *
1367
1289
  * 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).
1290
+ * as calculations involving decimal numbers (non-integers). Making exact
1291
+ * calculations on integers or rationals is OK.
1373
1292
  *
1374
1293
  * This handler should not have any side-effects: do not modify
1375
1294
  * the environment of the `ComputeEngine` instance, do not perform I/O,
1376
1295
  * do not do calculations that depend on random values.
1377
1296
  *
1378
- * If no simplification can be performed due to the values, domains or assumptions
1379
- * about its arguments, for example, return `undefined`.
1297
+ * If no simplification can be performed due to the values, domains or
1298
+ * assumptions about its arguments, for example, return `undefined`.
1380
1299
  *
1381
1300
  */
1382
1301
  simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1383
1302
  /**
1384
- * Evaluate symbolically an expression.
1303
+ * Evaluate symbolically a function expression.
1385
1304
  *
1386
1305
  * The arguments have been symbolically evaluated, except the arguments to
1387
1306
  * which a `hold` apply.
1388
1307
  *
1389
1308
  * It is not necessary to further simplify or evaluate the arguments.
1390
1309
  *
1391
- * If the expression cannot be evaluated, due to the values, domains, or
1392
- * assumptions about its arguments, for example, return `undefined`.
1310
+ * If performing numerical calculations, if all the arguments are exact,
1311
+ * return an exact expression. If any of the arguments is not exact, that is
1312
+ * if it is a literal decimal (non-integer) number, return an approximation.
1313
+ * In this case, the value may be the same as `expr.N()`.
1393
1314
  *
1315
+ * When doing an exact calculation:
1316
+ * - do not reduce rational numbers to decimal (floating point approximation)
1317
+ * - do not down convert bignums to machine numbers
1318
+ * - do not reduce square roots of rational numbers
1319
+ * - do not reduce constants with a `hold` attribute
1394
1320
  *
1321
+ * If the expression cannot be evaluated, due to the values, domains, or
1322
+ * assumptions about its arguments, for example, return `undefined` or
1323
+ * an `["Error"]` expression.
1395
1324
  */
1396
- evaluate?: LambdaExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1325
+ evaluate?: SemiBoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1397
1326
  /**
1398
- * Evaluate numerically an expression.
1327
+ * Evaluate numerically a function expression.
1399
1328
  *
1400
1329
  * The arguments `args` have been simplified and evaluated, numerically
1401
1330
  * if possible, except the arguments to which a `hold` apply.
@@ -1414,19 +1343,22 @@ export declare type FunctionSignature = {
1414
1343
  * evaluation, but a literal argument is out of range or
1415
1344
  * not of the expected type.
1416
1345
  *
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`).
1346
+ * Note that regardless of the current value of `ce.numericMode`, the
1347
+ * arguments may be boxed numbers representing machine numbers, bignum
1348
+ * numbers, complex numbers, rationals or big rationals.
1420
1349
  *
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.
1350
+ * Use the value of `ce.numericMode` to determine how to perform
1351
+ * the numeric evaluation.
1425
1352
  *
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.
1353
+ * If the numeric mode does not allow complex numbers (the
1354
+ * `engine.numericMode` is not `"complex"` or `"auto"`) and the result of
1355
+ * the evaluation would be a complex number, return `NaN` instead.
1356
+ *
1357
+ * If `ce.numericMode` is `"bignum"` or `"auto"` the evaluation should be done
1358
+ * using bignums.
1359
+ *
1360
+ * Otherwise, `ce.numericMode` is `"machine", the evaluation should be
1361
+ * performed using machine numbers.
1430
1362
  *
1431
1363
  * You may perform any necessary computations, including approximate
1432
1364
  * calculations on floating point numbers.
@@ -1437,17 +1369,17 @@ export declare type FunctionSignature = {
1437
1369
  * @experimental
1438
1370
  */
1439
1371
  evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1440
- /** Return the sign of the function given a list of arguments. */
1372
+ /** Return the sign of the function expression. */
1441
1373
  sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
1442
1374
  /** Return a compiled (optimized) expression. */
1443
1375
  compile?: (expr: BoxedExpression) => CompiledExpression;
1444
1376
  };
1445
1377
  export declare type BoxedFunctionSignature = {
1446
1378
  domain: BoxedDomain;
1447
- codomain?: BoxedDomain | ((ce: IComputeEngine, args: BoxedDomain[]) => BoxedDomain | null);
1448
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1379
+ codomain?: BoxedDomain | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedDomain | null);
1380
+ canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1449
1381
  simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1450
- evaluate?: BoxedLambdaExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1382
+ evaluate?: BoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1451
1383
  N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1452
1384
  evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1453
1385
  sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
@@ -1584,8 +1516,6 @@ export interface IComputeEngine {
1584
1516
  /** @internal */
1585
1517
  readonly _ONE: BoxedExpression;
1586
1518
  /** @internal */
1587
- readonly _TWO: BoxedExpression;
1588
- /** @internal */
1589
1519
  readonly _HALF: BoxedExpression;
1590
1520
  /** @internal */
1591
1521
  readonly _NEGATIVE_ONE: BoxedExpression;
@@ -1600,19 +1530,19 @@ export interface IComputeEngine {
1600
1530
  /** @internal */
1601
1531
  readonly _COMPLEX_INFINITY: BoxedExpression;
1602
1532
  /** @internal */
1603
- readonly _DECIMAL_NAN: Decimal;
1533
+ readonly _BIGNUM_NAN: Decimal;
1604
1534
  /** @internal */
1605
- readonly _DECIMAL_ZERO: Decimal;
1535
+ readonly _BIGNUM_ZERO: Decimal;
1606
1536
  /** @internal */
1607
- readonly _DECIMAL_ONE: Decimal;
1537
+ readonly _BIGNUM_ONE: Decimal;
1608
1538
  /** @internal */
1609
- readonly _DECIMAL_TWO: Decimal;
1539
+ readonly _BIGNUM_TWO: Decimal;
1610
1540
  /** @internal */
1611
- readonly _DECIMAL_HALF: Decimal;
1541
+ readonly _BIGNUM_HALF: Decimal;
1612
1542
  /** @internal */
1613
- readonly _DECIMAL_PI: Decimal;
1543
+ readonly _BIGNUM_PI: Decimal;
1614
1544
  /** @internal */
1615
- readonly _DECIMAL_NEGATIVE_ONE: Decimal;
1545
+ readonly _BIGNUM_NEGATIVE_ONE: Decimal;
1616
1546
  /** The current scope */
1617
1547
  context: RuntimeScope | null;
1618
1548
  /** Absolute time beyond which evaluation should not proceed
@@ -1634,7 +1564,7 @@ export interface IComputeEngine {
1634
1564
  chop(n: Complex): Complex | 0;
1635
1565
  chop(n: number | Decimal | Complex): number | Decimal | Complex;
1636
1566
  /** @internal */
1637
- decimal: (a: Decimal.Value) => Decimal;
1567
+ bignum: (a: Decimal.Value) => Decimal;
1638
1568
  /** @internal */
1639
1569
  complex: (a: number | Complex, b?: number) => Complex;
1640
1570
  set precision(p: number | 'machine');
@@ -1654,17 +1584,23 @@ export interface IComputeEngine {
1654
1584
  defineFunction(def: FunctionDefinition): BoxedFunctionDefinition;
1655
1585
  lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
1656
1586
  /** Return `undefined` if no definition exist for this `head` */
1657
- lookupFunction(head: string, scope?: RuntimeScope): undefined | BoxedFunctionDefinition;
1587
+ lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
1658
1588
  /**
1659
1589
  * Return a boxed expression from the input.
1660
- *
1661
- * The result may not be canonical.
1662
1590
  */
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;
1591
+ box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression, options?: {
1592
+ canonical?: boolean;
1593
+ }): BoxedExpression;
1594
+ /** Return a boxed number */
1595
+ number(value: number | string | MathJsonNumber | Decimal | Complex | [num: number, denom: number] | [num: Decimal, denom: Decimal], options?: {
1596
+ metadata?: Metadata;
1597
+ canonical?: boolean;
1598
+ }): BoxedExpression;
1666
1599
  /** Return a canonical boxed symbol */
1667
- symbol(sym: string, metadata?: Metadata): BoxedExpression;
1600
+ symbol(sym: string, options?: {
1601
+ metadata?: Metadata;
1602
+ canonical?: boolean;
1603
+ }): BoxedExpression;
1668
1604
  /** Return a canonical boxed string */
1669
1605
  string(s: string, metadata?: Metadata): BoxedExpression;
1670
1606
  /** Return a canonical boxed domain.
@@ -1673,8 +1609,6 @@ export interface IComputeEngine {
1673
1609
  *
1674
1610
  */
1675
1611
  domain(domain: SemiBoxedExpression | BoxedDomain | string, metadata?: Metadata): BoxedDomain;
1676
- /** Return a canonical lambda expression */
1677
- lambda(expr: SemiBoxedExpression, sig: BoxedDomain): BoxedLambdaExpression;
1678
1612
  /**
1679
1613
  * Return a canonical expression.
1680
1614
  *
@@ -1690,7 +1624,7 @@ export interface IComputeEngine {
1690
1624
  * This is a primitive to create a boxed function. It doesn't perform
1691
1625
  * any checks or normalization on its arguments.
1692
1626
  *
1693
- * In general, consider using `fn()` or `box()` instead.
1627
+ * In general, consider using `ce.fn()` or `ce.box()` instead.
1694
1628
  *
1695
1629
  * The result is canonical, but the caller has to ensure that all the
1696
1630
  * conditions are met (i.e. `ops` properly normalized and sorted, all
@@ -1702,6 +1636,10 @@ export interface IComputeEngine {
1702
1636
  * The result is canonical.
1703
1637
  */
1704
1638
  error(message: string | [string, ...SemiBoxedExpression[]], where?: SemiBoxedExpression): BoxedExpression;
1639
+ /**
1640
+ * Add a`["Hold"]` wrapper to `expr.
1641
+ */
1642
+ hold(expr: SemiBoxedExpression): BoxedExpression;
1705
1643
  /** Shortcut for `this.fn("Add"...)`.
1706
1644
  *
1707
1645
  * The result is canonical.
@@ -1716,7 +1654,8 @@ export interface IComputeEngine {
1716
1654
  *
1717
1655
  * The result is canonical.
1718
1656
  */
1719
- power(base: BoxedExpression, exponent: number | [number, number] | BoxedExpression, metadata?: Metadata): BoxedExpression;
1657
+ power(base: BoxedExpression, exponent: number | Rational | BoxedExpression, metadata?: Metadata): BoxedExpression;
1658
+ sqrt(base: BoxedExpression, metadata?: Metadata): any;
1720
1659
  /** Shortcut for `this.fn("Divide", [1, expr])`
1721
1660
  *
1722
1661
  * The result is canonical.
@@ -1750,9 +1689,15 @@ export interface IComputeEngine {
1750
1689
  * The result may not be canonical.
1751
1690
  *
1752
1691
  */
1753
- parse(s: LatexString | string): BoxedExpression;
1754
- parse(s: null): null;
1755
- parse(s: LatexString | string | null): null | BoxedExpression;
1692
+ parse(s: LatexString | string, options?: {
1693
+ canonical?: boolean;
1694
+ }): BoxedExpression;
1695
+ parse(s: null, options?: {
1696
+ canonical?: boolean;
1697
+ }): null;
1698
+ parse(s: LatexString | string | null, options?: {
1699
+ canonical?: boolean;
1700
+ }): null | BoxedExpression;
1756
1701
  /** Serialize a `BoxedExpression` or a `MathJSON` expression to
1757
1702
  * a LaTeX string
1758
1703
  */
@@ -1770,8 +1715,22 @@ export interface IComputeEngine {
1770
1715
  get latexOptions(): NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions;
1771
1716
  set latexOptions(opts: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions> & Partial<SerializeLatexOptions>);
1772
1717
  /** {@inheritDoc JsonSerializationOptions} */
1773
- get jsonSerializationOptions(): JsonSerializationOptions;
1718
+ get jsonSerializationOptions(): Readonly<JsonSerializationOptions>;
1774
1719
  set jsonSerializationOptions(val: Partial<JsonSerializationOptions>);
1720
+ pushScope(options?: {
1721
+ symbolTable?: Readonly<SymbolTable> | Readonly<SymbolTable>[];
1722
+ assumptions?: (LatexString | Expression | BoxedExpression)[];
1723
+ scope?: Partial<Scope>;
1724
+ }): void;
1725
+ popScope(): void;
1726
+ /** Assign a value to an identifier in the current scope */
1727
+ set(identifiers: {
1728
+ [identifier: string]: SemiBoxedExpression;
1729
+ }): void;
1730
+ /** Declare identifiers (specify their domain without necessarily assigning them a value in the current scope*/
1731
+ let(identifiers: {
1732
+ [identifier: string]: SymbolDefinition | FunctionDefinition;
1733
+ }): void;
1775
1734
  /**
1776
1735
  * Add an assumption.
1777
1736
  *
@@ -1793,13 +1752,7 @@ export interface IComputeEngine {
1793
1752
  /** Remove all assumptions about one or more symbols */
1794
1753
  forget(symbol?: string | string[]): void;
1795
1754
  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;
1755
+ ask(pattern: LatexString | SemiBoxedExpression): BoxedSubstitution[];
1803
1756
  /**
1804
1757
  * When `condition` is false, signal.
1805
1758
  *