@cortex-js/compute-engine 0.21.0 → 0.23.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 (110) hide show
  1. package/README.md +6 -3
  2. package/dist/compute-engine.esm.js +9120 -6769
  3. package/dist/compute-engine.js +9120 -6769
  4. package/dist/compute-engine.min.esm.js +10 -10
  5. package/dist/compute-engine.min.js +10 -10
  6. package/dist/math-json.esm.js +12 -11
  7. package/dist/math-json.js +12 -11
  8. package/dist/math-json.min.esm.js +12 -11
  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/grapheme-splitter.d.ts +1 -1
  12. package/dist/types/common/signals.d.ts +1 -1
  13. package/dist/types/common/utils.d.ts +1 -1
  14. package/dist/types/compute-engine/assume.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +13 -9
  16. package/dist/types/compute-engine/boxed-expression/box.d.ts +4 -4
  17. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  18. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +1 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  20. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +12 -3
  21. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +2 -1
  26. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +69 -0
  27. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -0
  28. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  30. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  31. package/dist/types/compute-engine/boxed-expression/utils.d.ts +16 -51
  32. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  33. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  34. package/dist/types/compute-engine/compile.d.ts +1 -1
  35. package/dist/types/compute-engine/compute-engine.d.ts +47 -44
  36. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  37. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  38. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +4 -0
  48. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  52. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +2 -1
  53. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  54. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  55. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  56. package/dist/types/compute-engine/latex-syntax/public.d.ts +13 -6
  57. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  58. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +4 -3
  59. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +6 -6
  60. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  61. package/dist/types/compute-engine/library/arithmetic-add.d.ts +13 -3
  62. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +2 -1
  63. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +6 -8
  64. package/dist/types/compute-engine/library/arithmetic-power.d.ts +3 -1
  65. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  66. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  67. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  68. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  69. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  70. package/dist/types/compute-engine/library/core.d.ts +2 -1
  71. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  72. package/dist/types/compute-engine/library/library.d.ts +1 -1
  73. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  74. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  75. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  76. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  77. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  78. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  79. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  80. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  81. package/dist/types/compute-engine/library/utils.d.ts +27 -5
  82. package/dist/types/compute-engine/numerics/factor.d.ts +27 -0
  83. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +3 -1
  84. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  85. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  86. package/dist/types/compute-engine/numerics/numeric.d.ts +8 -1
  87. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  88. package/dist/types/compute-engine/numerics/rationals.d.ts +8 -22
  89. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  90. package/dist/types/compute-engine/numerics/terms.d.ts +14 -0
  91. package/dist/types/compute-engine/public.d.ts +100 -58
  92. package/dist/types/compute-engine/rules.d.ts +9 -13
  93. package/dist/types/compute-engine/simplify-rules.d.ts +11 -1
  94. package/dist/types/compute-engine/solve.d.ts +2 -1
  95. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  96. package/dist/types/compute-engine/symbolic/expand.d.ts +5 -1
  97. package/dist/types/compute-engine/symbolic/flatten.d.ts +2 -1
  98. package/dist/types/compute-engine/symbolic/negate.d.ts +3 -2
  99. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  100. package/dist/types/compute-engine/symbolic/product.d.ts +8 -3
  101. package/dist/types/compute-engine/symbolic/tensor-fields.d.ts +168 -0
  102. package/dist/types/compute-engine/symbolic/tensors.d.ts +103 -0
  103. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  104. package/dist/types/compute-engine.d.ts +2 -2
  105. package/dist/types/math-json/math-json-format.d.ts +1 -1
  106. package/dist/types/math-json/utils.d.ts +1 -1
  107. package/dist/types/math-json.d.ts +2 -2
  108. package/package.json +1 -1
  109. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +0 -2
  110. package/dist/types/compute-engine/symbolic/sum.d.ts +0 -33
@@ -1,14 +1,34 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  * Assume the caller has setup a scope. The index
4
4
  * variable will be declared in that scope.
5
5
  *
6
- * @param limits
6
+ * @param indexingSet
7
+
8
+ * IndexingSet is an expression describing an index variable
9
+ * and a range of values for that variable.
10
+ *
11
+ * The MultiIndexingSet function takes an expression of the form
12
+ * \sum_{i=1,j=1}^{10,10} x and returns an array of expressions
13
+ * ["Sum","x",["Triple","i",1,10],["Triple","j",1,10]
14
+ */
15
+ export declare function MultiIndexingSet(indexingSet: BoxedExpression | undefined): BoxedExpression[] | undefined;
16
+ /**
17
+ * Assume the caller has setup a scope. The index
18
+ * variable will be declared in that scope.
19
+ *
20
+ * @param indexingSet
21
+
22
+ * IndexingSet is an expression describing an index variable
23
+ * and a range of values for that variable.
7
24
  *
25
+ * The SingleIndexingSet function takes an expression of the form
26
+ * \sum_{i=1}^{10} x and returns an array of expressions
27
+ * ["Sum","x",["Triple","i",1,10]
8
28
  */
9
- export declare function canonicalLimits(limits: BoxedExpression | undefined): BoxedExpression | undefined;
29
+ export declare function SingleIndexingSet(indexingSet: BoxedExpression | undefined): BoxedExpression | undefined;
10
30
  /**
11
- * Limits is an expression describing an index variable
31
+ * IndexingSet is an expression describing an index variable
12
32
  * and a range of values for that variable.
13
33
  *
14
34
  * This can take several valid forms:
@@ -21,9 +41,11 @@ export declare function canonicalLimits(limits: BoxedExpression | undefined): Bo
21
41
  * @param limits
22
42
  * @returns
23
43
  */
24
- export declare function normalizeLimits(limits: BoxedExpression | undefined): [
44
+ export declare function normalizeIndexingSet(limits: BoxedExpression | undefined): [
25
45
  index: string | undefined,
26
46
  lower: number,
27
47
  upper: number,
28
48
  isFinite: boolean
29
49
  ];
50
+ export declare function cartesianProduct(array1: number[], array2: number[]): number[][];
51
+ export declare function range(start: number, end: number): number[];
@@ -0,0 +1,27 @@
1
+ /* 0.23.0 */
2
+ import Decimal from 'decimal.js';
3
+ import { BoxedExpression, Rational } from '../public';
4
+ /**
5
+ * Attempt to factor a numeric coefficient `c` and a `rest` out of a
6
+ * canonical expression `expr` such that `ce.mul(c, rest)` is equal to `expr`.
7
+ *
8
+ * Attempts to make `rest` a positive value (i.e. pulls out negative sign).
9
+ *
10
+ * The numeric coefficient could be an expression, for example:
11
+ * ['Multiply', 2, ['Sqrt', 5], 'x']
12
+ * -> [['Multiply', 2, ['Sqrt', 5]], 'x']
13
+ *
14
+ * ['Multiply', 2, 'x', 3, 'a']
15
+ * -> [6, ['Multiply', 'x', 'a']]
16
+ *
17
+ * ['Divide', ['Multiply', 2, 'x'], ['Multiply', 3, 'y', 'a']]
18
+ * -> [['Rational', 2, 3], ['Divide', 'x', ['Multiply, 'y', 'a']]]
19
+ */
20
+ export declare function asCoefficient(expr: BoxedExpression): [coef: Rational, rest: BoxedExpression];
21
+ export declare function applyCoefficient(value: number | Decimal | Complex | Rational | null, coef: Rational): number | Decimal | Complex | Rational | null;
22
+ /** Combine rational expressions into a single fraction */
23
+ export declare function together(op: BoxedExpression): BoxedExpression;
24
+ /**
25
+ * Return a list of expressions that multiply together to form `expr`.
26
+ */
27
+ export declare function factor(expr: BoxedExpression): BoxedExpression;
@@ -1,4 +1,6 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
+ import { Expression } from '../../math-json.js';
3
+ export declare function bigintValue(expr: Expression | null | undefined): bigint | null;
2
4
  export declare function bigint(a: Decimal | number | bigint | string): bigint;
3
5
  export declare function gcd(a: bigint, b: bigint): bigint;
4
6
  export declare function lcm(a: bigint, b: bigint): bigint;
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  import { IComputeEngine } from '../public';
3
3
  export declare function gcd(a: Decimal, b: Decimal): Decimal;
4
4
  export declare function lcm(a: Decimal, b: Decimal): Decimal;
@@ -1,3 +1,3 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  export declare function gamma(c: Complex): Complex;
3
3
  export declare function gammaln(c: Complex): Complex;
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  import { Decimal } from 'decimal.js';
3
3
  import { BoxedExpression } from '../public';
4
4
  export declare const MACHINE_PRECISION_BITS = 53;
@@ -109,3 +109,10 @@ export declare function monteCarloEstimate(f: (x: number) => number, a: number,
109
109
  export declare function limit(f: (x: number) => number, x: number, dir?: number): number;
110
110
  export declare function fromRoman(roman: string): [result: number, rest: string];
111
111
  export declare function fromDigits(s: string, baseInput?: string | number): [result: number, rest: string];
112
+ /**
113
+ *
114
+ * @param lhs
115
+ * @param rhs
116
+ * @returns the sign (-1, 0, 1) of the difference between `lhs` and `rhs`
117
+ */
118
+ export declare function signDiff(lhs: BoxedExpression, rhs: BoxedExpression, tolerance?: number): -1 | 0 | 1 | undefined;
@@ -1,2 +1,2 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  export declare function isPrime(n: number): boolean | undefined;
@@ -1,9 +1,10 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  export declare function isRational(x: any | null): x is Rational;
3
3
  export declare function isMachineRational(x: any | null): x is [number, number];
4
4
  export declare function isBigRational(x: any | null): x is [bigint, bigint];
5
5
  export declare function isRationalZero(x: Rational): boolean;
6
6
  export declare function isRationalOne(x: Rational): boolean;
7
+ export declare function isRationalInteger(x: Rational): boolean;
7
8
  export declare function isRationalNegativeOne(x: Rational): boolean;
8
9
  export declare function machineNumerator(x: Rational): number;
9
10
  export declare function machineDenominator(x: Rational): number;
@@ -17,7 +18,7 @@ export declare function inverse(x: Rational): Rational;
17
18
  export declare function asRational(expr: BoxedExpression): Rational | undefined;
18
19
  /**
19
20
  * Add a literal numeric value to a rational.
20
- * If the rational is a bignum, this is a hint to do the calculation in bignum
21
+ * If the rational is a bigint, this is a hint to do the calculation in bigint
21
22
  * (no need to check `bignumPreferred()`).
22
23
  * @param lhs
23
24
  * @param rhs
@@ -25,30 +26,15 @@ export declare function asRational(expr: BoxedExpression): Rational | undefined;
25
26
  */
26
27
  export declare function add(lhs: Rational, rhs: BoxedExpression | Rational): Rational;
27
28
  export declare function mul(lhs: Rational, rhs: BoxedExpression | Rational): Rational;
29
+ export declare function div(lhs: Rational, rhs: Rational): Rational;
28
30
  export declare function pow(r: Rational, exp: number): Rational;
31
+ export declare function sqrt(r: Rational): Rational | undefined;
29
32
  export declare function reducedRational(r: [number, number]): [number, number];
30
33
  export declare function reducedRational(r: [bigint, bigint]): [bigint, bigint];
31
34
  export declare function reducedRational(r: Rational): Rational;
32
35
  /** Return a rational approximation of x */
33
36
  export declare function rationalize(x: number): [n: number, d: number] | number;
34
- /**
35
- * Attempt to factor a rational coefficient `c` and a `rest` out of a
36
- * canonical expression `expr` such that `ce.mul(c, rest)` is equal to `expr`.
37
- *
38
- * Attempts to make `rest` a positive value (i.e. pulls out negative sign).
39
- *
40
- *
41
- * ['Multiply', 2, 'x', 3, 'a', ['Sqrt', 5]]
42
- * -> [[6, 1], ['Multiply', 'x', 'a', ['Sqrt', 5]]]
43
- *
44
- * ['Divide', ['Multiply', 2, 'x'], ['Multiply', 3, 'y', 'a']]
45
- * -> [[2, 3], ['Divide', 'x', ['Multiply, 'y', 'a']]]
46
- */
47
- export declare function asCoefficient(expr: BoxedExpression): [coef: Rational, rest: BoxedExpression];
48
- /**
49
- *
50
- * @param lhs
51
- * @param rhs
52
- * @returns the sign (-1, 0, 1) of the difference between `lhs` and `rhs`
37
+ /** Return [factor, root] such that factor * sqrt(root) = n
38
+ * when factor and root are rationals
53
39
  */
54
- export declare function signDiff(lhs: BoxedExpression, rhs: BoxedExpression, tolerance?: number): -1 | 0 | 1 | undefined;
40
+ export declare function reduceRationalSquareRoot(n: Rational): [factor: Rational, root: Rational];
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
 
3
3
  Translated from https://github.com/JuliaMath/Richardson.jl/blob/master/src/Richardson.jl
4
4
 
@@ -0,0 +1,14 @@
1
+ /* 0.23.0 */
2
+ export declare class Terms {
3
+ private engine;
4
+ private terms;
5
+ constructor(ce: IComputeEngine, terms: BoxedExpression[]);
6
+ sub(term: BoxedExpression): void;
7
+ add(term2: BoxedExpression, coef2?: Rational): void;
8
+ find(term: BoxedExpression): number;
9
+ /** If `exact` is true, keep exact numbers */
10
+ reduceNumbers({ exact }?: {
11
+ exact: boolean;
12
+ }): void;
13
+ asExpression(): BoxedExpression;
14
+ }
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  * The most important classes are {@link ComputeEngine} and
3
3
  * {@link BoxedExpression}.
4
4
  *
@@ -91,40 +91,39 @@ export type BoxedSubstitution = Substitution<BoxedExpression>;
91
91
  * `"$\frac{\pi}{2}$"`.
92
92
  */
93
93
  export type LatexString = string;
94
+ export type PatternReplaceFunction = (expr: BoxedExpression, wildcards: BoxedSubstitution) => BoxedExpression;
95
+ export type PatternConditionFunction = (wildcards: BoxedSubstitution, ce: IComputeEngine) => boolean;
94
96
  /**
95
- * A rule describes how to modify an expressions that matches a `lhs` pattern
96
- * into a new expressions matching `rhs`.
97
+ * A rule describes how to modify an expressions that matches a pattern `match`
98
+ * into a new expression `replace`.
97
99
  *
98
100
  * `x-1` \( \to \) `1-x`
99
101
  * `(x+1)(x-1)` \( \to \) `x^2-1
100
102
  *
101
- * The `lhs` can be expressed as a LaTeX string or a MathJSON expression.
103
+ * The `match` pattern can be expressed as a LaTeX string or a MathJSON expression.
102
104
  *
103
- * Unbound variables (`x`, but not `Pi`) are matched structurally with a
104
- * a target expression, then the expression is rewritten as the `rhs`, with
105
- * the corresponding unbound variables in the `rhs` replaced by their values
106
- * in the `lhs.
107
105
  *
108
- * Pattern symbols (e.g. `_1`, `_a`) can be used as well.
106
+ * Anonymous wildcards (`_`) will match any
107
+ * expression. Named wildcards (`_x`, `_a`, etc...) will match any expression
108
+ * and bind the expression to the wildcard name.
109
109
  *
110
- * In addition:
111
- * - `__1` (`__a`, etc..) match a sequence of one or more expressions
112
- * - `___1` (`___a`, etc...) match a sequence of zero or more expressions
110
+ * In addition the sequence wildcard (`__1`, `__a`, etc...) will match a sequence
111
+ * of one or more expressions, and bind the sequence to the wildcard name.
113
112
  */
114
- export type Rule = [
115
- lhs: LatexString | SemiBoxedExpression | Pattern,
116
- rhs: LatexString | SemiBoxedExpression,
117
- options?: {
118
- condition?: LatexString | ((wildcards: BoxedSubstitution) => boolean);
119
- priority?: number;
120
- }
121
- ];
122
- export type BoxedRule = [
123
- lhs: Pattern,
124
- rhs: BoxedExpression,
125
- priority: number,
126
- condition: undefined | ((wildcards: BoxedSubstitution) => boolean)
127
- ];
113
+ export type Rule = {
114
+ match: LatexString | SemiBoxedExpression | Pattern;
115
+ replace: LatexString | SemiBoxedExpression | PatternReplaceFunction;
116
+ condition?: LatexString | PatternConditionFunction;
117
+ priority?: number;
118
+ id?: string;
119
+ };
120
+ export type BoxedRule = {
121
+ match: Pattern;
122
+ replace: BoxedExpression | PatternReplaceFunction;
123
+ condition: undefined | PatternConditionFunction;
124
+ priority: number;
125
+ id?: string;
126
+ };
128
127
  export type BoxedRuleSet = Set<BoxedRule>;
129
128
  export type DomainCompatibility = 'covariant' | 'contravariant' | 'bivariant' | 'invariant';
130
129
  /** A domain constructor is the head of a domain expression. */
@@ -189,6 +188,7 @@ export type JsonSerializationOptions = {
189
188
  */
190
189
  precision: 'auto' | 'max' | number;
191
190
  };
191
+ export type CanonicalForm = 'InvisibleOperator' | 'Number' | 'Multiply' | 'Add' | 'Power' | 'Divide' | 'Flatten' | 'Order';
192
192
  /**
193
193
  * ## THEORY OF OPERATIONS
194
194
  *
@@ -233,6 +233,12 @@ export interface BoxedExpression {
233
233
  * @category Primitive Methods
234
234
  */
235
235
  toString(): string;
236
+ /**
237
+ * Output to the console a string representation of the expression.
238
+ *
239
+ * @category Primitive Methods
240
+ */
241
+ print(): void;
236
242
  /** Similar to`expr.valueOf()` but includes a hint.
237
243
  * @category Primitive Methods
238
244
  */
@@ -428,6 +434,11 @@ export interface BoxedExpression {
428
434
  * An exact value is not further transformed when evaluated. To get an
429
435
  * approximate evaluation of an exact value, use `.N()`.
430
436
  *
437
+ * Exact numbers are:
438
+ * - rationals (including integers)
439
+ * - complex numbers with integer real and imaginary parts (Gaussian integers)
440
+ * - square root of rationals
441
+ *
431
442
  * Non-exact values includes:
432
443
  * - numbers with a fractional part
433
444
  * - complex numbers with a real or imaginary fractional part
@@ -590,10 +601,22 @@ export interface BoxedExpression {
590
601
  * Note it is possible for `numericValue` to be `null`, and for `isNotZero`
591
602
  * to be true. For example, when a symbol has been defined with an assumption.
592
603
  *
604
+ * Conversely, `isNumber` may be true even if `numericValue` is `null`,
605
+ * example the symbol `Pi` return true for `isNumber` but `numericValue` is
606
+ * `null`. It's value can be accessed with `.value.numericValue`
607
+ *
593
608
  * @category Numeric Expression
594
609
  *
595
610
  */
596
611
  readonly numericValue: number | Decimal | Complex | Rational | null;
612
+ /** The shape describes the axis of the expression.
613
+ * When the expression is a scalar (number), the shape is `[]`.
614
+ * When the expression is a vector, the shape is `[n]`.
615
+ * When the expression is a matrix, the shape is `[n, m]`.
616
+ */
617
+ readonly shape: number[];
618
+ /** Return 0 for a scalar, 1 for a vector, 2 for a matrix, > 2 for a multidimensional matrix. It's the length of `expr.shape` */
619
+ readonly rank: number;
597
620
  /**
598
621
  * Return the following, depending on the value of this expression:
599
622
  *
@@ -976,6 +999,14 @@ export interface BoxedExpression {
976
999
  * of an existing `BoxedExpression` while avoiding unboxing and reboxing.
977
1000
  */
978
1001
  export type SemiBoxedExpression = number | string | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[] | BoxedExpression;
1002
+ /**
1003
+ * Control how a pattern is matched to an expression.
1004
+ *
1005
+ * - `substitution`: if present, assumes these values for the named wildcards, and ensure that subsequent occurence of the same wildcard have the same value.
1006
+ * - `recursive`: if true, match recursively, otherwise match only the top level.
1007
+ * - `numericTolerance`: if present, the tolerance for numeric comparison.
1008
+ * - `exact`: if true, only match expressions that are structurally identical. If false, match expressions that are structurally identical or equivalent. For example, when false, `["Add", '_a', 2]` matches `2`, with a value of `_a` of `0`. If true, the expression does not match.
1009
+ */
979
1010
  export type PatternMatchOptions = {
980
1011
  substitution?: BoxedSubstitution;
981
1012
  recursive?: boolean;
@@ -1619,21 +1650,29 @@ export interface IComputeEngine {
1619
1650
  chop(n: Complex): Complex | 0;
1620
1651
  chop(n: number | Decimal | Complex): number | Decimal | Complex;
1621
1652
  bignum: (a: Decimal.Value | bigint) => Decimal;
1622
- complex: (a: number | Complex, b?: number) => Complex;
1623
1653
  isBignum(a: unknown): a is Decimal;
1654
+ complex: (a: number | Complex, b?: number) => Complex;
1624
1655
  isComplex(a: unknown): a is Complex;
1625
1656
  set precision(p: number | 'machine');
1626
1657
  get precision(): number;
1627
1658
  costFunction: (expr: BoxedExpression) => number;
1659
+ /** In strict mode the compute engine performs additional checks,
1660
+ * for example to ensure the correct number and domain of arguments. These
1661
+ * checks may impact performance.
1662
+ */
1628
1663
  strict: boolean;
1629
- defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
1630
- defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
1631
- lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
1632
- lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
1664
+ /** Return a canonical version of an array of semi-boxed-expressions. */
1665
+ canonical(xs: SemiBoxedExpression[]): BoxedExpression[];
1666
+ /** Return as boxed expression from a number, string or semiboxed expression.
1667
+ * Calls `function()`, `number()` or `symbol()` as appropriate.
1668
+ */
1633
1669
  box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression, options?: {
1670
+ canonical?: boolean | CanonicalForm | CanonicalForm[];
1671
+ }): BoxedExpression;
1672
+ function(head: string | BoxedExpression, ops: SemiBoxedExpression[], options?: {
1673
+ metadata?: Metadata;
1634
1674
  canonical?: boolean;
1635
1675
  }): BoxedExpression;
1636
- canonical(xs: SemiBoxedExpression[]): BoxedExpression[];
1637
1676
  number(value: number | bigint | string | MathJsonNumber | Decimal | Complex | Rational, options?: {
1638
1677
  metadata?: Metadata;
1639
1678
  canonical?: boolean;
@@ -1644,45 +1683,43 @@ export interface IComputeEngine {
1644
1683
  }): BoxedExpression;
1645
1684
  string(s: string, metadata?: Metadata): BoxedExpression;
1646
1685
  domain(domain: BoxedDomain | DomainExpression, metadata?: Metadata): BoxedDomain;
1647
- fn(head: string | SemiBoxedExpression, ops: SemiBoxedExpression[], options?: {
1648
- canonical: boolean;
1649
- }): BoxedExpression;
1650
- /**
1651
- * This is a primitive to create a boxed function. It doesn't perform
1652
- * any checks or normalization on its arguments.
1653
- *
1654
- * In general, consider using `ce.fn()` or `ce.box()` instead.
1655
- *
1656
- * The result is canonical, but the caller has to ensure that all the
1657
- * conditions are met (i.e. `ops` properly normalized and sorted, all
1658
- * `ops` canonical, etc..) so that the result is actually canonical.
1659
- *
1660
- * @internal
1661
- */
1662
- _fn(head: string | BoxedExpression, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1663
1686
  error(message: string | [string, ...SemiBoxedExpression[]], where?: SemiBoxedExpression): BoxedExpression;
1664
1687
  domainError(expectedDomain: BoxedDomain | DomainLiteral, actualDomain: undefined | BoxedDomain, where?: SemiBoxedExpression): BoxedExpression;
1665
1688
  hold(expr: SemiBoxedExpression): BoxedExpression;
1666
- add(ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1667
- mul(ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1668
- pow(base: BoxedExpression, exponent: number | Rational | BoxedExpression, metadata?: Metadata): BoxedExpression;
1669
- sqrt(base: BoxedExpression, metadata?: Metadata): BoxedExpression;
1670
- inv(expr: BoxedExpression, metadata?: Metadata): BoxedExpression;
1671
- neg(expr: BoxedExpression, metadata?: Metadata): BoxedExpression;
1672
- div(num: BoxedExpression, denom: BoxedExpression, metadata?: Metadata): BoxedExpression;
1689
+ add(...ops: BoxedExpression[]): BoxedExpression;
1690
+ mul(...ops: BoxedExpression[]): BoxedExpression;
1691
+ pow(base: BoxedExpression, exponent: number | Rational | BoxedExpression): BoxedExpression;
1692
+ sqrt(base: BoxedExpression): BoxedExpression;
1693
+ inv(expr: BoxedExpression): BoxedExpression;
1694
+ neg(expr: BoxedExpression): BoxedExpression;
1695
+ div(num: BoxedExpression, denom: BoxedExpression): BoxedExpression;
1673
1696
  pair(first: BoxedExpression, second: BoxedExpression, metadata?: Metadata): BoxedExpression;
1697
+ tuple(elements: number[], metadata?: Metadata): BoxedExpression;
1674
1698
  tuple(elements: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1675
1699
  array(elements: ArrayValue[] | ArrayValue[][], metadata?: Metadata): BoxedExpression;
1676
1700
  rules(rules: Rule[]): BoxedRuleSet;
1677
1701
  pattern(expr: LatexString | SemiBoxedExpression): Pattern;
1702
+ /**
1703
+ * This is a primitive to create a boxed function.
1704
+ *
1705
+ * In general, consider using `ce.box()` or `canonicalXXX()` instead.
1706
+ *
1707
+ * The caller must ensure that the arguments are in canonical form:
1708
+ * - arguments are `canonical()`
1709
+ * - arguments are sorted
1710
+ * - arguments are flattened and desequenced
1711
+ *
1712
+ * @internal
1713
+ */
1714
+ _fn(head: string | BoxedExpression, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1678
1715
  parse(s: LatexString | string, options?: {
1679
- canonical?: boolean;
1716
+ canonical?: boolean | CanonicalForm | CanonicalForm[];
1680
1717
  }): BoxedExpression;
1681
1718
  parse(s: null, options?: {
1682
- canonical?: boolean;
1719
+ canonical?: boolean | CanonicalForm | CanonicalForm[];
1683
1720
  }): null;
1684
1721
  parse(s: LatexString | string | null, options?: {
1685
- canonical?: boolean;
1722
+ canonical?: boolean | CanonicalForm | CanonicalForm[];
1686
1723
  }): null | BoxedExpression;
1687
1724
  serialize(expr: SemiBoxedExpression, options?: {
1688
1725
  canonical?: boolean;
@@ -1695,6 +1732,10 @@ export interface IComputeEngine {
1695
1732
  popScope(): IComputeEngine;
1696
1733
  swapScope(scope: RuntimeScope | null): RuntimeScope | null;
1697
1734
  resetContext(): void;
1735
+ defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
1736
+ lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
1737
+ defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
1738
+ lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
1698
1739
  assign(ids: {
1699
1740
  [id: string]: AssignValue;
1700
1741
  }): IComputeEngine;
@@ -1720,6 +1761,7 @@ export interface IComputeEngine {
1720
1761
  checkContinueExecution(): void;
1721
1762
  /** @internal */
1722
1763
  cache<T>(name: string, build: () => T, purge?: (T: any) => T | undefined): T;
1764
+ /** @internal */
1723
1765
  readonly stats: ComputeEngineStats;
1724
1766
  /** @internal */
1725
1767
  reset(): void;
@@ -1,10 +1,16 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
- * Go through all the rules in the rule set, and for all the rules that match
4
- * return the rhs of the rule applied to `expr`.
3
+ * For each rules in the rule set that match, return the `replace` of the rule
4
+ *
5
5
  * @param rules
6
6
  */
7
7
  export declare function matchRules(expr: BoxedExpression, rules: BoxedRuleSet, sub: BoxedSubstitution): BoxedExpression[];
8
+ /**
9
+ * Create a boxed rule set from a non-boxed rule set
10
+ * @param ce
11
+ * @param rs
12
+ * @returns
13
+ */
8
14
  export declare function boxRules(ce: IComputeEngine, rs: Iterable<Rule>): BoxedRuleSet;
9
15
  /**
10
16
  * Apply the rules in the ruleset and return a modified expression.
@@ -12,13 +18,3 @@ export declare function boxRules(ce: IComputeEngine, rs: Iterable<Rule>): BoxedR
12
18
  * If no rule applied, return `null`.
13
19
  */
14
20
  export declare function replace(expr: BoxedExpression, ruleSet: BoxedRuleSet, options?: ReplaceOptions): BoxedExpression | null;
15
- /**
16
- * Substitute some symbols with an expression.
17
- *
18
- * This is applied recursively to all subexpressions.
19
- *
20
- * While `replace()` applies a rule which may include expressions in
21
- * its `lhs` to an expression, `substitute` is a specialized version
22
- * that only apply rules that have a `lhs` made of a symbol.
23
- */
24
- export declare function getWildcardName(s: string): string;
@@ -1,4 +1,14 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
+ /**
3
+ * @todo: a set to "tidy" an expression. Different from a canonical form, but
4
+ * inline with the user's expectations.
5
+ *
6
+ * Example:
7
+ *
8
+ * - a^n * a^m -> a^(n+m)
9
+ * - a / √b -> (a * √b) / b
10
+ *
11
+ */
2
12
  /**
3
13
  * A set of simplification rules.
4
14
  *
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  export declare const UNIVARIATE_ROOTS: Rule[];
3
3
  /**
4
4
  * Expression is a function of a single variable (`x`) or an Equality
@@ -15,3 +15,4 @@ export declare function findUnivariateRoots(expr: BoxedExpression, x: string): B
15
15
  * `2x < 4` => `x < 2`
16
16
  */
17
17
  export declare function univariateSolve(expr: BoxedExpression, x: string): SemiBoxedExpression[] | null;
18
+ export declare const HARMONIZATION_RULES: Rule[];
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  *
4
4
  * @param fn The function to differentiate, a `["Function"]` expression or
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  export declare function distribute(expr: BoxedExpression[]): BoxedExpression;
3
3
  /** Use the multinomial theorem (https://en.wikipedia.org/wiki/Multinomial_theorem) to expand the expression.
4
4
  * The expression must be a power of a sum of terms.
@@ -9,6 +9,10 @@ export declare function distribute(expr: BoxedExpression[]): BoxedExpression;
9
9
  * -> 'a^3 + 3a^2b + 3ab^2 + b^3'
10
10
  */
11
11
  export declare function expandMultinomial(expr: BoxedExpression): BoxedExpression | null;
12
+ /** Expand all
13
+ * Recursive expand of all terms in the expression
14
+ */
15
+ export declare function expandAll(expr: BoxedExpression): BoxedExpression | null;
12
16
  /** Apply the distributive law if the expression is a product of sums.
13
17
  * For example, a(b + c) = ab + ac
14
18
  * Expand the expression if it is a power of a sum.
@@ -1,6 +1,7 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  * Flatten the arguments.
4
4
  */
5
5
  export declare function flattenOps(ops: BoxedExpression[], head: string): BoxedExpression[];
6
6
  export declare function flattenSequence(xs: BoxedExpression[]): BoxedExpression[];
7
+ export declare function flattenDelimiter(ce: IComputeEngine, body: undefined | BoxedExpression): BoxedExpression;
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  * Distribute `Negate` (multiply by -1) if expr is a number literal, an
4
4
  * addition or multiplication or another `Negate`.
@@ -6,5 +6,6 @@
6
6
  * It is important to do all these to handle cases like
7
7
  * `-3x` -> ["Negate, ["Multiply", 3, "x"]] -> ["Multiply, -3, x]
8
8
  */
9
- export declare function canonicalNegate(expr: BoxedExpression, metadata?: Metadata): BoxedExpression;
9
+ export declare function evalNegate(expr: BoxedExpression): BoxedExpression;
10
+ export declare function canonicalNegate(expr: BoxedExpression): BoxedExpression;
10
11
  export declare function processNegate(_ce: IComputeEngine, x: BoxedExpression, _mode?: 'simplify' | 'evaluate' | 'N'): BoxedExpression;
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  * Coefficient of a univariate (single variable) polynomial.
4
4
  *
@@ -1,4 +1,4 @@
1
- /* 0.21.0 */
1
+ /* 0.23.0 */
2
2
  /**
3
3
  * Group terms in a product by common term.
4
4
  *
@@ -21,10 +21,11 @@ export declare class Product {
21
21
  };
22
22
  engine: IComputeEngine;
23
23
  private _sign;
24
- private _rational;
24
+ private _number;
25
25
  private _complex;
26
26
  private _bignum;
27
- private _number;
27
+ private _rational;
28
+ private _rationalSqrt;
28
29
  private _terms;
29
30
  private _hasInfinity;
30
31
  private _hasZero;
@@ -40,6 +41,10 @@ export declare class Product {
40
41
  * Otherwise, terms and their exponent are tallied.
41
42
  */
42
43
  addTerm(term: BoxedExpression): void;
44
+ /** Return all ther terms with an exponent of 1 and
45
+ * the square root of rationals (which technically have an
46
+ * exponent of 1/2, but are considered as degree 1 terms)
47
+ */
43
48
  unitTerms(mode: 'rational' | 'expression' | 'numeric'): {
44
49
  exponent: Rational;
45
50
  terms: BoxedExpression[];