@cortex-js/compute-engine 0.24.0 → 0.25.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.
- package/dist/compute-engine.esm.js +25479 -24875
- package/dist/compute-engine.js +25480 -24876
- package/dist/compute-engine.min.esm.js +13 -13
- package/dist/compute-engine.min.js +13 -13
- package/dist/math-json.esm.js +6 -3
- package/dist/math-json.js +6 -3
- package/dist/math-json.min.esm.js +6 -3
- package/dist/math-json.min.js +2 -2
- package/dist/types/common/ansi-codes.d.ts +1 -1
- package/dist/types/common/grapheme-splitter.d.ts +1 -1
- package/dist/types/common/signals.d.ts +1 -1
- package/dist/types/common/utils.d.ts +1 -1
- package/dist/types/compute-engine/assume.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +30 -19
- package/dist/types/compute-engine/boxed-expression/box.d.ts +9 -9
- package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +4 -4
- package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -2
- package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +2 -1
- package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +9 -10
- package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +5 -4
- package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +2 -15
- package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -4
- package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +7 -8
- package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +6 -7
- package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/factor.d.ts +11 -0
- package/dist/types/compute-engine/boxed-expression/match.d.ts +14 -0
- package/dist/types/compute-engine/boxed-expression/numerics.d.ts +41 -0
- package/dist/types/compute-engine/boxed-expression/order.d.ts +6 -6
- package/dist/types/compute-engine/boxed-expression/public.d.ts +1964 -0
- package/dist/types/compute-engine/boxed-expression/serialize.d.ts +3 -15
- package/dist/types/compute-engine/boxed-expression/utils.d.ts +5 -2
- package/dist/types/compute-engine/boxed-expression/validate.d.ts +7 -6
- package/dist/types/compute-engine/collection-utils.d.ts +1 -1
- package/dist/types/compute-engine/compile.d.ts +2 -2
- package/dist/types/compute-engine/compute-engine.d.ts +83 -60
- package/dist/types/compute-engine/cost-function.d.ts +1 -1
- package/dist/types/compute-engine/domain-utils.d.ts +3 -2
- package/dist/types/compute-engine/function-utils.d.ts +2 -2
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -3
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +5 -4
- package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/parse.d.ts +71 -29
- package/dist/types/compute-engine/latex-syntax/public.d.ts +264 -252
- package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +9 -3
- package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serializer.d.ts +6 -11
- package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +3 -2
- package/dist/types/compute-engine/library/arithmetic-add.d.ts +5 -5
- package/dist/types/compute-engine/library/arithmetic-divide.d.ts +2 -1
- package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +4 -4
- package/dist/types/compute-engine/library/arithmetic-power.d.ts +2 -1
- package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/library/calculus.d.ts +1 -1
- package/dist/types/compute-engine/library/collections.d.ts +1 -1
- package/dist/types/compute-engine/library/complex.d.ts +1 -1
- package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
- package/dist/types/compute-engine/library/core.d.ts +3 -2
- package/dist/types/compute-engine/library/domains.d.ts +1 -1
- package/dist/types/compute-engine/library/library.d.ts +1 -1
- package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/library/logic.d.ts +1 -1
- package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
- package/dist/types/compute-engine/library/relational-operator.d.ts +1 -2
- package/dist/types/compute-engine/library/sets.d.ts +1 -1
- package/dist/types/compute-engine/library/statistics.d.ts +1 -1
- package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/library/utils.d.ts +2 -2
- package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +7 -1
- package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric.d.ts +7 -17
- package/dist/types/compute-engine/numerics/primes.d.ts +6 -1
- package/dist/types/compute-engine/numerics/rationals.d.ts +4 -13
- package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
- package/dist/types/compute-engine/numerics/terms.d.ts +7 -4
- package/dist/types/compute-engine/public.d.ts +3 -1839
- package/dist/types/compute-engine/rules.d.ts +1 -1
- package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
- package/dist/types/compute-engine/solve.d.ts +4 -3
- package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/expand.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/flatten.d.ts +6 -4
- package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/product.d.ts +4 -3
- package/dist/types/compute-engine/symbolic/tensor-fields.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/tensors.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/utils.d.ts +5 -10
- package/dist/types/compute-engine.d.ts +2 -2
- package/dist/types/math-json/math-json-format.d.ts +3 -3
- package/dist/types/math-json/utils.d.ts +4 -6
- package/dist/types/math-json.d.ts +2 -2
- package/package.json +1 -1
- package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +0 -28
- package/dist/types/compute-engine/numerics/factor.d.ts +0 -27
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* 0.
|
|
1
|
+
/* 0.25.0 */
|
|
2
2
|
*
|
|
3
3
|
* The Compute Engine is a symbolic computation engine that can be used to
|
|
4
4
|
* manipulate and evaluate mathematical expressions.
|
|
@@ -12,1087 +12,8 @@
|
|
|
12
12
|
* @module "compute-engine"
|
|
13
13
|
*
|
|
14
14
|
*/
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
import type { Expression, MathJsonDictionary, MathJsonFunction, MathJsonNumber, MathJsonString, MathJsonSymbol } from '../math-json/math-json-format';
|
|
18
|
-
import type { LatexDictionaryEntry, NumberFormattingOptions, ParseLatexOptions, SerializeLatexOptions } from './latex-syntax/public';
|
|
19
|
-
export * from './latex-syntax/public';
|
|
20
|
-
export * from './compute-engine';
|
|
21
|
-
/**
|
|
22
|
-
* @category Boxed Expression
|
|
23
|
-
*/
|
|
24
|
-
export type Rational = [number, number] | [bigint, bigint];
|
|
25
|
-
/**
|
|
26
|
-
* Metadata that can be associated with a `BoxedExpression`
|
|
27
|
-
*
|
|
28
|
-
* @category Boxed Expression
|
|
29
|
-
*/
|
|
30
|
-
export type Metadata = {
|
|
31
|
-
latex?: string | undefined;
|
|
32
|
-
wikidata?: string | undefined;
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* The numeric evaluation mode:
|
|
36
|
-
*
|
|
37
|
-
<div className="symbols-table">
|
|
38
|
-
|
|
39
|
-
| Mode | |
|
|
40
|
-
| :--- | :----- |
|
|
41
|
-
| `"auto"`| Use bignum or complex numbers. |
|
|
42
|
-
| `"machine"` | **IEEE 754-2008**, 64-bit floating point numbers: 52-bit mantissa, about 15 digits of precision |
|
|
43
|
-
| `"bignum"` | Arbitrary precision floating point numbers, as provided by the "decimal.js" library |
|
|
44
|
-
| `"complex"` | Complex number represented by two machine numbers, a real and an imaginary part, as provided by the "complex.js" library |
|
|
45
|
-
|
|
46
|
-
</div>
|
|
47
|
-
|
|
48
|
-
* @category Compute Engine
|
|
49
|
-
*/
|
|
50
|
-
export type NumericMode = 'auto' | 'machine' | 'bignum' | 'complex';
|
|
51
|
-
/** @category Compute Engine */
|
|
52
|
-
export type Hold = 'none' | 'all' | 'first' | 'rest' | 'last' | 'most';
|
|
53
|
-
/** Options for `BoxedExpression.simplify()`
|
|
54
|
-
*
|
|
55
|
-
* @category Compute Engine
|
|
56
|
-
*/
|
|
57
|
-
export type SimplifyOptions = {
|
|
58
|
-
recursive?: boolean;
|
|
59
|
-
rules?: BoxedRuleSet;
|
|
60
|
-
};
|
|
61
|
-
/** Options for `BoxedExpression.evaluate()`
|
|
62
|
-
*
|
|
63
|
-
* @category Boxed Expression
|
|
64
|
-
*/
|
|
65
|
-
export type EvaluateOptions = {
|
|
66
|
-
numericMode?: boolean;
|
|
67
|
-
};
|
|
68
|
-
/** Options for `BoxedExpression.N()`
|
|
69
|
-
*
|
|
70
|
-
* @category Boxed Expression
|
|
71
|
-
*/
|
|
72
|
-
export type NOptions = {};
|
|
73
|
-
/**
|
|
74
|
-
* @category Boxed Expression
|
|
75
|
-
*
|
|
76
|
-
*/
|
|
77
|
-
export type ReplaceOptions = {
|
|
78
|
-
/** If `true`, apply replacement rules to all sub-expressions.
|
|
79
|
-
* If `false`, only consider the top-level expression.
|
|
80
|
-
*
|
|
81
|
-
* **Default**: `false`
|
|
82
|
-
*/
|
|
83
|
-
recursive?: boolean;
|
|
84
|
-
/**
|
|
85
|
-
* If `true`, stop after the first rule that matches.
|
|
86
|
-
*
|
|
87
|
-
* If `false`, apply all the remaining rules even after the first match.
|
|
88
|
-
*
|
|
89
|
-
* **Default**: `false`
|
|
90
|
-
*/
|
|
91
|
-
once?: boolean;
|
|
92
|
-
/**
|
|
93
|
-
* If `iterationLimit` > 1, the rules will be repeatedly applied
|
|
94
|
-
* until no rules apply, up to `maxIterations` times.
|
|
95
|
-
*
|
|
96
|
-
* Note that if `once` is true, `maxIterations` has no effect.
|
|
97
|
-
*
|
|
98
|
-
* **Default**: `1`
|
|
99
|
-
*/
|
|
100
|
-
iterationLimit?: number;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* A substitution describes the values of the wildcards in a pattern so that
|
|
104
|
-
* the pattern is equal to a target expression.
|
|
105
|
-
*
|
|
106
|
-
* A substitution can also be considered a more constrained version of a
|
|
107
|
-
* rule whose `lhs` is always a symbol.
|
|
108
|
-
|
|
109
|
-
* @category Boxed Expression
|
|
110
|
-
*/
|
|
111
|
-
export type Substitution<T = SemiBoxedExpression> = {
|
|
112
|
-
[symbol: string]: T;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* @category Boxed Expression
|
|
116
|
-
*
|
|
117
|
-
*/
|
|
118
|
-
export type BoxedSubstitution = Substitution<BoxedExpression>;
|
|
119
|
-
/** A LaTeX string starts and end with `$`, for example
|
|
120
|
-
* `"$\frac{\pi}{2}$"`.
|
|
121
|
-
*
|
|
122
|
-
* @category Latex Parsing and Serialization
|
|
123
|
-
*/
|
|
124
|
-
export type LatexString = string;
|
|
125
|
-
/** @category Rules */
|
|
126
|
-
export type PatternReplaceFunction = (expr: BoxedExpression, wildcards: BoxedSubstitution) => BoxedExpression;
|
|
127
|
-
/** @category Rules */
|
|
128
|
-
export type PatternConditionFunction = (wildcards: BoxedSubstitution, ce: IComputeEngine) => boolean;
|
|
129
|
-
/**
|
|
130
|
-
* A rule describes how to modify an expressions that matches a pattern `match`
|
|
131
|
-
* into a new expression `replace`.
|
|
132
|
-
*
|
|
133
|
-
* `x-1` \( \to \) `1-x`
|
|
134
|
-
* `(x+1)(x-1)` \( \to \) `x^2-1
|
|
135
|
-
*
|
|
136
|
-
* The `match` pattern can be expressed as a LaTeX string or a
|
|
137
|
-
* MathJSON expression.
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* Anonymous wildcards (`_`) will match any
|
|
141
|
-
* expression. Named wildcards (`_x`, `_a`, etc...) will match any expression
|
|
142
|
-
* and bind the expression to the wildcard name.
|
|
143
|
-
*
|
|
144
|
-
* In addition the sequence wildcard (`__1`, `__a`, etc...) will match
|
|
145
|
-
* a sequence of one or more expressions, and bind the sequence to the
|
|
146
|
-
* wildcard name.
|
|
147
|
-
*
|
|
148
|
-
* @category Rules
|
|
149
|
-
*/
|
|
150
|
-
export type Rule = {
|
|
151
|
-
match: LatexString | SemiBoxedExpression | Pattern;
|
|
152
|
-
replace: LatexString | SemiBoxedExpression | PatternReplaceFunction;
|
|
153
|
-
condition?: LatexString | PatternConditionFunction;
|
|
154
|
-
priority?: number;
|
|
155
|
-
id?: string;
|
|
156
|
-
};
|
|
157
|
-
/** @category Rules */
|
|
158
|
-
export type BoxedRule = {
|
|
159
|
-
match: Pattern;
|
|
160
|
-
replace: BoxedExpression | PatternReplaceFunction;
|
|
161
|
-
condition: undefined | PatternConditionFunction;
|
|
162
|
-
priority: number;
|
|
163
|
-
id?: string;
|
|
164
|
-
};
|
|
165
|
-
/** @category Rules */
|
|
166
|
-
export type BoxedRuleSet = Set<BoxedRule>;
|
|
167
|
-
/** @category Boxed Expression */
|
|
168
|
-
export type DomainCompatibility = 'covariant' | 'contravariant' | 'bivariant' | 'invariant';
|
|
169
|
-
/** A domain constructor is the head of a domain expression.
|
|
170
|
-
*
|
|
171
|
-
* @category Boxed Expression
|
|
172
|
-
*
|
|
173
|
-
*/
|
|
174
|
-
export type DomainConstructor = 'FunctionOf' | 'ListOf' | 'DictionaryOf' | 'TupleOf' | 'Intersection' | 'Union' | 'OptArg' | 'VarArg' | 'Covariant' | 'Contravariant' | 'Bivariant' | 'Invariant';
|
|
175
|
-
/** @category Boxed Expression */
|
|
176
|
-
export type DomainLiteral = 'Anything' | 'Values' | 'Domains' | 'Void' | 'NothingDomain' | 'Booleans' | 'Strings' | 'Symbols' | 'Collections' | 'Lists' | 'Dictionaries' | 'Sequences' | 'Tuples' | 'Sets' | 'Functions' | 'Predicates' | 'LogicOperators' | 'RelationalOperators' | 'NumericFunctions' | 'RealFunctions' | 'Numbers' | 'ComplexNumbers' | 'ExtendedRealNumbers' | 'ImaginaryNumbers' | 'Integers' | 'Rationals' | 'PositiveNumbers' | 'PositiveIntegers' | 'NegativeNumbers' | 'NegativeIntegers' | 'NonNegativeNumbers' | 'NonNegativeIntegers' | 'NonPositiveNumbers' | 'NonPositiveIntegers' | 'ExtendedComplexNumbers' | 'TranscendentalNumbers' | 'AlgebraicNumbers' | 'RationalNumbers' | 'RealNumbers';
|
|
177
|
-
/** @category Boxed Expression */
|
|
178
|
-
export type DomainExpression<T = SemiBoxedExpression> = DomainLiteral | ['Union', ...DomainExpression<T>[]] | ['Intersection', ...DomainExpression<T>[]] | ['ListOf', DomainExpression<T>] | ['DictionaryOf', DomainExpression<T>] | ['TupleOf', ...DomainExpression<T>[]] | ['OptArg', ...DomainExpression<T>[]] | ['VarArg', DomainExpression<T>] | ['Covariant', DomainExpression<T>] | ['Contravariant', DomainExpression<T>] | ['Bivariant', DomainExpression<T>] | ['Invariant', DomainExpression<T>] | ['FunctionOf', ...DomainExpression<T>[]];
|
|
179
|
-
/**
|
|
180
|
-
* @noInheritDoc
|
|
181
|
-
*
|
|
182
|
-
* @category Boxed Expression
|
|
183
|
-
*/
|
|
184
|
-
export interface BoxedDomain extends BoxedExpression {
|
|
185
|
-
get canonical(): BoxedDomain;
|
|
186
|
-
get json(): Expression;
|
|
187
|
-
/** True if a valid domain, and compatible with `dom`
|
|
188
|
-
* `kind` is '"covariant"' by default, i.e. `this <: dom`
|
|
189
|
-
*/
|
|
190
|
-
isCompatible(dom: BoxedDomain | DomainLiteral, kind?: DomainCompatibility): boolean;
|
|
191
|
-
get base(): DomainLiteral;
|
|
192
|
-
get ctor(): DomainConstructor | null;
|
|
193
|
-
get params(): DomainExpression[];
|
|
194
|
-
readonly isNumeric: boolean;
|
|
195
|
-
readonly isFunction: boolean;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Options to control the serialization to MathJSON when using `BoxedExpression.json`.
|
|
199
|
-
*
|
|
200
|
-
* @category Compute Engine
|
|
201
|
-
*/
|
|
202
|
-
export type JsonSerializationOptions = {
|
|
203
|
-
/** A list of space separated function names that should be excluded from
|
|
204
|
-
* the JSON output.
|
|
205
|
-
*
|
|
206
|
-
* Those functions are replaced with an equivalent, for example, `Square` with
|
|
207
|
-
* `Power`, etc...
|
|
208
|
-
*
|
|
209
|
-
* Possible values include `Sqrt`, `Root`, `Square`, `Exp`, `Subtract`,
|
|
210
|
-
* `Rational`, `Complex`
|
|
211
|
-
*
|
|
212
|
-
* **Default**: `[]` (none)
|
|
213
|
-
*/
|
|
214
|
-
exclude: string[];
|
|
215
|
-
/** A list of space separated keywords indicating which MathJSON expressions
|
|
216
|
-
* can use a shorthand.
|
|
217
|
-
*
|
|
218
|
-
* **Default**: `["all"]`
|
|
219
|
-
*/
|
|
220
|
-
shorthands: ('all' | 'number' | 'symbol' | 'function' | 'dictionary' | 'string')[];
|
|
221
|
-
/** A list of space separated keywords indicating which metadata should be
|
|
222
|
-
* included in the MathJSON. If metadata is included, shorthand notation
|
|
223
|
-
* is not used.
|
|
224
|
-
*
|
|
225
|
-
* **Default**: `[]` (none)
|
|
226
|
-
*/
|
|
227
|
-
metadata: ('all' | 'wikidata' | 'latex')[];
|
|
228
|
-
/** If true, repeating decimals are detected and serialized accordingly
|
|
229
|
-
* For example:
|
|
230
|
-
* - `1.3333333333333333` \( \to \) `1.(3)`
|
|
231
|
-
* - `0.142857142857142857142857142857142857142857142857142` \( \to \) `0.(1428571)`
|
|
232
|
-
*
|
|
233
|
-
* **Default**: `true`
|
|
234
|
-
*/
|
|
235
|
-
repeatingDecimals: boolean;
|
|
236
|
-
/** Number literals are serialized with this precision.
|
|
237
|
-
* If `"auto"`, the same precision as the compute engine calculations is used
|
|
238
|
-
* If `"max"`, all available digits are serialized
|
|
239
|
-
*
|
|
240
|
-
* **Default**: `"auto"`
|
|
241
|
-
*/
|
|
242
|
-
precision: 'auto' | 'max' | number;
|
|
243
|
-
};
|
|
244
|
-
/**
|
|
245
|
-
* @category Boxed Expression
|
|
246
|
-
*/
|
|
247
|
-
export type CanonicalForm = 'InvisibleOperator' | 'Number' | 'Multiply' | 'Add' | 'Power' | 'Divide' | 'Flatten' | 'Order';
|
|
248
|
-
/**
|
|
249
|
-
* :::info[THEORY OF OPERATIONS]
|
|
250
|
-
*
|
|
251
|
-
* The `BoxedExpression` interface includes most of the member functions
|
|
252
|
-
* applicable to any kind of expression, for example `get symbol()` or
|
|
253
|
-
* `get ops()`.
|
|
254
|
-
*
|
|
255
|
-
* When a member function is not applicable to this `BoxedExpression`,
|
|
256
|
-
* for example `get symbol()` on a `BoxedNumber`, it returns `null`.
|
|
257
|
-
*
|
|
258
|
-
* This convention makes it convenient to manipulate expressions without
|
|
259
|
-
* having to check what kind of instance they are before manipulating them.
|
|
260
|
-
* :::
|
|
261
|
-
*
|
|
262
|
-
* To get a boxed expression, use `ce.box()` or `ce.parse()`.
|
|
263
|
-
*
|
|
264
|
-
* @category Boxed Expression
|
|
265
|
-
*
|
|
266
|
-
*/
|
|
267
|
-
export interface BoxedExpression {
|
|
268
|
-
/** The Compute Engine associated with this expression provides
|
|
269
|
-
* a context in which to interpret it, such as definition of symbols
|
|
270
|
-
* and functions.
|
|
271
|
-
*
|
|
272
|
-
*/
|
|
273
|
-
readonly engine: IComputeEngine;
|
|
274
|
-
/** From `Object.valueOf()`, return a primitive value for the expression.
|
|
275
|
-
*
|
|
276
|
-
* If the expression is a machine number, or bignum or rational that can be
|
|
277
|
-
* converted to a machine number, return a JavaScript `number`.
|
|
278
|
-
*
|
|
279
|
-
* If the expression is a symbol, return the name of the symbol as a `string`.
|
|
280
|
-
*
|
|
281
|
-
* Otherwise return a JavaScript primitive representation of the expression.
|
|
282
|
-
*
|
|
283
|
-
* @category Primitive Methods
|
|
284
|
-
*/
|
|
285
|
-
valueOf(): number | any[] | string | boolean;
|
|
286
|
-
/** From `Object.toString()`, return a string representation of the
|
|
287
|
-
* expression. This string is suitable to be output to the console
|
|
288
|
-
* for debugging, for example. To get a LaTeX representation of the
|
|
289
|
-
* expression, use `expr.latex`.
|
|
290
|
-
*
|
|
291
|
-
* Used when coercing a `BoxedExpression` to a `String`.
|
|
292
|
-
*
|
|
293
|
-
* @category Primitive Methods
|
|
294
|
-
*/
|
|
295
|
-
toString(): string;
|
|
296
|
-
/**
|
|
297
|
-
* Output to the console a string representation of the expression.
|
|
298
|
-
*
|
|
299
|
-
* @category Primitive Methods
|
|
300
|
-
*/
|
|
301
|
-
print(): void;
|
|
302
|
-
/** Similar to`expr.valueOf()` but includes a hint.
|
|
303
|
-
* @category Primitive Methods
|
|
304
|
-
*/
|
|
305
|
-
[Symbol.toPrimitive](hint: 'number' | 'string' | 'default'): number | string | null;
|
|
306
|
-
/** Used by `JSON.stringify()` to serialize this object to JSON.
|
|
307
|
-
*
|
|
308
|
-
* Method version of `expr.json`.
|
|
309
|
-
*
|
|
310
|
-
* @category Primitive Methods
|
|
311
|
-
*/
|
|
312
|
-
toJSON(): Expression;
|
|
313
|
-
/** If `true`, this expression is in a canonical form.
|
|
314
|
-
*
|
|
315
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
316
|
-
*
|
|
317
|
-
*/
|
|
318
|
-
get isCanonical(): boolean;
|
|
319
|
-
/** For internal use only, set when a canonical expression is created.
|
|
320
|
-
* @internal
|
|
321
|
-
*/
|
|
322
|
-
set isCanonical(val: boolean);
|
|
323
|
-
/** MathJSON representation of this expression.
|
|
324
|
-
*
|
|
325
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
326
|
-
*
|
|
327
|
-
*/
|
|
328
|
-
readonly json: Expression;
|
|
329
|
-
/** For debugging, the raw MathJSON representation of this expression without decanonicalization.
|
|
330
|
-
* @internal */
|
|
331
|
-
readonly rawJson: Expression;
|
|
332
|
-
/**
|
|
333
|
-
* The scope in which this expression has been defined.
|
|
334
|
-
* Is null when the expression is not canonical.
|
|
335
|
-
*/
|
|
336
|
-
readonly scope: RuntimeScope | null;
|
|
337
|
-
/** From `Object.is()`. Equivalent to `BoxedExpression.isSame()`
|
|
338
|
-
*
|
|
339
|
-
* @category Primitive Methods
|
|
340
|
-
*
|
|
341
|
-
*/
|
|
342
|
-
is(rhs: unknown): boolean;
|
|
343
|
-
/** @internal */
|
|
344
|
-
readonly hash: number;
|
|
345
|
-
/** LaTeX representation of this expression.
|
|
346
|
-
*
|
|
347
|
-
* The serialization can be customized with `ComputeEngine.latexOptions`
|
|
348
|
-
*
|
|
349
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
350
|
-
*
|
|
351
|
-
*/
|
|
352
|
-
get latex(): LatexString;
|
|
353
|
-
/**
|
|
354
|
-
*
|
|
355
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
356
|
-
* @internal
|
|
357
|
-
*/
|
|
358
|
-
set latex(val: string);
|
|
359
|
-
/** If this expression is a symbol, return the name of the symbol as a string.
|
|
360
|
-
* Otherwise, return `null`.
|
|
361
|
-
*
|
|
362
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
363
|
-
|
|
364
|
-
* @category Symbol Expression
|
|
365
|
-
*
|
|
366
|
-
*/
|
|
367
|
-
readonly symbol: string | null;
|
|
368
|
-
/**
|
|
369
|
-
* If this is the `Nothing` symbol, return `true`.
|
|
370
|
-
*
|
|
371
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
372
|
-
*/
|
|
373
|
-
readonly isNothing: boolean;
|
|
374
|
-
/** If this expression is a string, return the value of the string.
|
|
375
|
-
* Otherwise, return `null`.
|
|
376
|
-
*
|
|
377
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
378
|
-
|
|
379
|
-
* @category String Expression
|
|
380
|
-
*
|
|
381
|
-
*/
|
|
382
|
-
readonly string: string | null;
|
|
383
|
-
/** All the subexpressions matching the head
|
|
384
|
-
*
|
|
385
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
386
|
-
*
|
|
387
|
-
*/
|
|
388
|
-
getSubexpressions(head: string): BoxedExpression[];
|
|
389
|
-
/** All the subexpressions in this expression, recursively
|
|
390
|
-
*
|
|
391
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
392
|
-
*
|
|
393
|
-
*/
|
|
394
|
-
readonly subexpressions: BoxedExpression[];
|
|
395
|
-
/**
|
|
396
|
-
*
|
|
397
|
-
* All the symbols in the expression, recursively
|
|
398
|
-
*
|
|
399
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
400
|
-
*
|
|
401
|
-
*/
|
|
402
|
-
readonly symbols: string[];
|
|
403
|
-
/**
|
|
404
|
-
* All the identifiers used in the expression that do not have a value
|
|
405
|
-
* associated with them, i.e. they are declared but not defined.
|
|
406
|
-
*/
|
|
407
|
-
readonly unknowns: string[];
|
|
408
|
-
/**
|
|
409
|
-
*
|
|
410
|
-
* All the identifiers (symbols and functions) in the expression that are
|
|
411
|
-
* not a local variable or a parameter of that function.
|
|
412
|
-
*
|
|
413
|
-
*/
|
|
414
|
-
readonly freeVariables: string[];
|
|
415
|
-
/** All the `["Error"]` subexpressions
|
|
416
|
-
*
|
|
417
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
418
|
-
*
|
|
419
|
-
*/
|
|
420
|
-
readonly errors: BoxedExpression[];
|
|
421
|
-
/** All boxed expressions have a head.
|
|
422
|
-
*
|
|
423
|
-
* If not a function this can be `Symbol`, `String`, `Number` or `Dictionary`.
|
|
424
|
-
*
|
|
425
|
-
* If the head expression can be represented as a string, it is returned
|
|
426
|
-
* as a string.
|
|
427
|
-
*
|
|
428
|
-
* **Note** applicable to canonical and non-canonical expressions. The head
|
|
429
|
-
* of a non-canonical expression may be different than the head of its
|
|
430
|
-
* canonical counterpart. For example the canonical counterpart of `["Divide", 5, 7]` is `["Rational", 5, 5]`.
|
|
431
|
-
*/
|
|
432
|
-
readonly head: BoxedExpression | string;
|
|
433
|
-
/** The list of arguments of the function, its "tail".
|
|
434
|
-
*
|
|
435
|
-
* If the expression is not a function, return `null`.
|
|
436
|
-
*
|
|
437
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
438
|
-
*
|
|
439
|
-
* @category Function Expression
|
|
440
|
-
*
|
|
441
|
-
*/
|
|
442
|
-
readonly ops: null | BoxedExpression[];
|
|
443
|
-
/** If this expression is a function, the number of operands, otherwise 0.
|
|
444
|
-
*
|
|
445
|
-
* Note that a function can have 0 operands, so to check if this expression
|
|
446
|
-
* is a function, check if `this.ops !== null` instead.
|
|
447
|
-
*
|
|
448
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
449
|
-
*
|
|
450
|
-
* @category Function Expression
|
|
451
|
-
*
|
|
452
|
-
*/
|
|
453
|
-
readonly nops: number;
|
|
454
|
-
/** First operand, i.e.`this.ops[0]`
|
|
455
|
-
*
|
|
456
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
457
|
-
*
|
|
458
|
-
* @category Function Expression
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*/
|
|
462
|
-
readonly op1: BoxedExpression;
|
|
463
|
-
/** Second operand, i.e.`this.ops[1]`
|
|
464
|
-
*
|
|
465
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
466
|
-
*
|
|
467
|
-
* @category Function Expression
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*/
|
|
471
|
-
readonly op2: BoxedExpression;
|
|
472
|
-
/** Third operand, i.e. `this.ops[2]`
|
|
473
|
-
*
|
|
474
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
475
|
-
*
|
|
476
|
-
* @category Function Expression
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*/
|
|
480
|
-
readonly op3: BoxedExpression;
|
|
481
|
-
/** `true` if this expression or any of its subexpressions is an `["Error"]`
|
|
482
|
-
* expression.
|
|
483
|
-
*
|
|
484
|
-
* **Note** applicable to canonical and non-canonical expressions. For
|
|
485
|
-
* non-canonical expression, this may indicate a syntax error while parsing
|
|
486
|
-
* LaTeX. For canonical expression, this may indicate argument domain
|
|
487
|
-
* mismatch, or missing or unexpected arguments.
|
|
488
|
-
*
|
|
489
|
-
* @category Symbol Expression
|
|
490
|
-
*
|
|
491
|
-
*/
|
|
492
|
-
readonly isValid: boolean;
|
|
493
|
-
/**
|
|
494
|
-
* An exact value is not further transformed when evaluated. To get an
|
|
495
|
-
* approximate evaluation of an exact value, use `.N()`.
|
|
496
|
-
*
|
|
497
|
-
* Exact numbers are:
|
|
498
|
-
* - rationals (including integers)
|
|
499
|
-
* - complex numbers with integer real and imaginary parts (Gaussian integers)
|
|
500
|
-
* - square root of rationals
|
|
501
|
-
*
|
|
502
|
-
* Non-exact values includes:
|
|
503
|
-
* - numbers with a fractional part
|
|
504
|
-
* - complex numbers with a real or imaginary fractional part
|
|
505
|
-
*
|
|
506
|
-
*/
|
|
507
|
-
readonly isExact: boolean;
|
|
508
|
-
/** If true, the value of the expression never changes and evaluating it has
|
|
509
|
-
* no side-effects.
|
|
510
|
-
* If false, the value of the expression may change, if the
|
|
511
|
-
* value of other expression changes or for other reasons.
|
|
512
|
-
*
|
|
513
|
-
* If `this.isPure` is `false`, `this.value` is undefined. Call
|
|
514
|
-
* `this.evaluate()` to determine the value of the expression instead.
|
|
515
|
-
*
|
|
516
|
-
* As an example, the `Random` function is not pure.
|
|
517
|
-
*
|
|
518
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
519
|
-
*/
|
|
520
|
-
readonly isPure: boolean;
|
|
521
|
-
/** True if the expression is a constant, that is a symbol with an immutable value */
|
|
522
|
-
readonly isConstant: boolean;
|
|
523
|
-
/**
|
|
524
|
-
* Return the canonical form of this expression.
|
|
525
|
-
*
|
|
526
|
-
* If this is a function expressin, a definition is associated with the
|
|
527
|
-
* canonical expression.
|
|
528
|
-
*
|
|
529
|
-
* When determining the canonical form the following function definition
|
|
530
|
-
* flags are applied:
|
|
531
|
-
* - `associative`: \\( f(a, f(b), c) \longrightarrow f(a, b, c) \\)
|
|
532
|
-
* - `idempotent`: \\( f(f(a)) \longrightarrow f(a) \\)
|
|
533
|
-
* - `involution`: \\( f(f(a)) \longrightarrow a \\)
|
|
534
|
-
* - `commutative`: sort the arguments.
|
|
535
|
-
*
|
|
536
|
-
* If his expression is already canonical, the value of canonical is
|
|
537
|
-
* `this`.
|
|
538
|
-
*
|
|
539
|
-
*/
|
|
540
|
-
get canonical(): BoxedExpression;
|
|
541
|
-
/**
|
|
542
|
-
* Replace all the symbols in the expression as indicated.
|
|
543
|
-
*
|
|
544
|
-
* Note the same effect can be achieved with `this.replace()`, but
|
|
545
|
-
* using `this.subs()` is more efficient, and simpler.
|
|
546
|
-
*
|
|
547
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
548
|
-
*
|
|
549
|
-
*/
|
|
550
|
-
subs(sub: Substitution, options?: {
|
|
551
|
-
canonical?: boolean;
|
|
552
|
-
}): BoxedExpression;
|
|
553
|
-
/**
|
|
554
|
-
* Transform the expression by applying the rules:
|
|
555
|
-
* if an expression matches the `match` pattern, replace it with
|
|
556
|
-
* the `replace` pattern.
|
|
557
|
-
*
|
|
558
|
-
* If no rules apply, return `null`.
|
|
559
|
-
*
|
|
560
|
-
* See also `subs` for a simple substitution.
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
564
|
-
*
|
|
565
|
-
*/
|
|
566
|
-
replace(rules: BoxedRuleSet | Rule | Rule[], options?: ReplaceOptions): null | BoxedExpression;
|
|
567
|
-
/**
|
|
568
|
-
* True if the expression includes a symbol `v` or a function head `v`.
|
|
569
|
-
*
|
|
570
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
571
|
-
*/
|
|
572
|
-
has(v: string | string[]): boolean;
|
|
573
|
-
/** Structural/symbolic equality (weak equality).
|
|
574
|
-
*
|
|
575
|
-
* `ce.parse('1+x').isSame(ce.parse('x+1'))` is `false`
|
|
576
|
-
*
|
|
577
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
578
|
-
*
|
|
579
|
-
* @category Relational Operator
|
|
580
|
-
*/
|
|
581
|
-
isSame(rhs: BoxedExpression): boolean;
|
|
582
|
-
/**
|
|
583
|
-
* If this expression matches `pattern`, return a substitution that makes
|
|
584
|
-
* `pattern` equal to `this`. Otherwise return `null`.
|
|
585
|
-
*
|
|
586
|
-
* If `pattern` includes wildcards (identifiers that starts
|
|
587
|
-
* with `_`), the substitution will include a prop for each matching named
|
|
588
|
-
* wildcard.
|
|
589
|
-
*
|
|
590
|
-
* If this expression matches `pattern` but there are no named wildcards,
|
|
591
|
-
* return the empty substitution, `{}`.
|
|
592
|
-
*
|
|
593
|
-
* **Note** applicable to canonical and non-canonical expressions.
|
|
594
|
-
*
|
|
595
|
-
*/
|
|
596
|
-
match(pattern: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression | BoxedExpression, options?: PatternMatchOptions): BoxedSubstitution | null;
|
|
597
|
-
/**
|
|
598
|
-
* "Not a Number".
|
|
599
|
-
*
|
|
600
|
-
* A value representing undefined result of computations, such as `0/0`,
|
|
601
|
-
* as per the floating point format standard IEEE-754.
|
|
602
|
-
*
|
|
603
|
-
* Note that if `isNaN` is true, `isNumber` is also true (yes, `NaN` is a
|
|
604
|
-
* number).
|
|
605
|
-
*
|
|
606
|
-
* @category Expression Properties
|
|
607
|
-
*
|
|
608
|
-
*/
|
|
609
|
-
readonly isNaN: boolean | undefined;
|
|
610
|
-
/**
|
|
611
|
-
* The numeric value of this expression is 0.
|
|
612
|
-
*
|
|
613
|
-
* @category Expression Properties
|
|
614
|
-
*/
|
|
615
|
-
readonly isZero: boolean | undefined;
|
|
616
|
-
/**
|
|
617
|
-
* The numeric value of this expression is not 0.
|
|
618
|
-
* @category Expression Properties
|
|
619
|
-
*/
|
|
620
|
-
readonly isNotZero: boolean | undefined;
|
|
621
|
-
/**
|
|
622
|
-
* The numeric value of this expression is not 1.
|
|
623
|
-
* @category Expression Properties
|
|
624
|
-
*/
|
|
625
|
-
readonly isOne: boolean | undefined;
|
|
626
|
-
/**
|
|
627
|
-
* The numeric value of this expression is not -1.
|
|
628
|
-
* @category Expression Properties
|
|
629
|
-
*/
|
|
630
|
-
readonly isNegativeOne: boolean | undefined;
|
|
631
|
-
/** The numeric value of this expression is ±Infinity or Complex Infinity
|
|
632
|
-
*
|
|
633
|
-
* @category Expression Properties
|
|
634
|
-
*/
|
|
635
|
-
readonly isInfinity: boolean | undefined;
|
|
636
|
-
/** This expression is a number, but not ±Infinity and not `NaN`
|
|
637
|
-
*
|
|
638
|
-
* @category Expression Properties
|
|
639
|
-
*/
|
|
640
|
-
readonly isFinite: boolean | undefined;
|
|
641
|
-
/**
|
|
642
|
-
* @category Expression Properties
|
|
643
|
-
*/
|
|
644
|
-
readonly isEven: boolean | undefined;
|
|
645
|
-
/**
|
|
646
|
-
* @category Expression Properties
|
|
647
|
-
*/
|
|
648
|
-
readonly isOdd: boolean | undefined;
|
|
649
|
-
/**
|
|
650
|
-
* @category Expression Properties
|
|
651
|
-
*/
|
|
652
|
-
readonly isPrime: boolean | undefined;
|
|
653
|
-
/**
|
|
654
|
-
* @category Expression Properties
|
|
655
|
-
*/
|
|
656
|
-
readonly isComposite: boolean | undefined;
|
|
657
|
-
/**
|
|
658
|
-
* Return the value of this expression, if a number literal.
|
|
659
|
-
*
|
|
660
|
-
* Note it is possible for `numericValue` to be `null`, and for `isNotZero`
|
|
661
|
-
* to be true. For example, when a symbol has been defined with an assumption.
|
|
662
|
-
*
|
|
663
|
-
* Conversely, `isNumber` may be true even if `numericValue` is `null`,
|
|
664
|
-
* example the symbol `Pi` return true for `isNumber` but `numericValue` is
|
|
665
|
-
* `null`. It's value can be accessed with `.value.numericValue`
|
|
666
|
-
*
|
|
667
|
-
* @category Numeric Expression
|
|
668
|
-
*
|
|
669
|
-
*/
|
|
670
|
-
readonly numericValue: number | Decimal | Complex | Rational | null;
|
|
671
|
-
/** The shape describes the axis of the expression.
|
|
672
|
-
* When the expression is a scalar (number), the shape is `[]`.
|
|
673
|
-
* When the expression is a vector, the shape is `[n]`.
|
|
674
|
-
* When the expression is a matrix, the shape is `[n, m]`.
|
|
675
|
-
*/
|
|
676
|
-
readonly shape: number[];
|
|
677
|
-
/** 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` */
|
|
678
|
-
readonly rank: number;
|
|
679
|
-
/**
|
|
680
|
-
* Return the following, depending on the value of this expression:
|
|
681
|
-
*
|
|
682
|
-
* * `-1` if it is < 0
|
|
683
|
-
* * `0` if it is = 0
|
|
684
|
-
* * `+1` if it is > 0
|
|
685
|
-
* * `undefined` this value may be positive, negative or zero. We don't know
|
|
686
|
-
* right now (a symbol with an Integer domain, but no currently assigned
|
|
687
|
-
* value, for example)
|
|
688
|
-
* * `null` this value will never be positive, negative or zero (`NaN`,
|
|
689
|
-
* a string or a complex number for example)
|
|
690
|
-
*
|
|
691
|
-
* Note that complex numbers have no natural ordering,
|
|
692
|
-
* so if the value is a complex number, `sgn` is either 0, or `null`
|
|
693
|
-
*
|
|
694
|
-
* If a symbol, this does take assumptions into account, that is `this.sgn`
|
|
695
|
-
* will return `1` if `isPositive` is `true`, even if this expression has
|
|
696
|
-
* no value
|
|
697
|
-
*
|
|
698
|
-
* @category Numeric Expression
|
|
699
|
-
*
|
|
700
|
-
*/
|
|
701
|
-
readonly sgn: -1 | 0 | 1 | undefined | null;
|
|
702
|
-
/** If the expressions cannot be compared, return `undefined`
|
|
703
|
-
*
|
|
704
|
-
* The numeric value of both expressions are compared.
|
|
705
|
-
*
|
|
706
|
-
* @category Relational Operator
|
|
707
|
-
*/
|
|
708
|
-
isLess(rhs: BoxedExpression): boolean | undefined;
|
|
709
|
-
/**
|
|
710
|
-
* The numeric value of both expressions are compared.
|
|
711
|
-
* @category Relational Operator
|
|
712
|
-
*/
|
|
713
|
-
isLessEqual(rhs: BoxedExpression): boolean | undefined;
|
|
714
|
-
/**
|
|
715
|
-
* The numeric value of both expressions are compared.
|
|
716
|
-
* @category Relational Operator
|
|
717
|
-
*/
|
|
718
|
-
isGreater(rhs: BoxedExpression): boolean | undefined;
|
|
719
|
-
/**
|
|
720
|
-
* The numeric value of both expressions are compared.
|
|
721
|
-
* @category Relational Operator
|
|
722
|
-
*/
|
|
723
|
-
isGreaterEqual(rhs: BoxedExpression): boolean | undefined;
|
|
724
|
-
/** The numeric value of this expression is > 0, same as `isGreater(0)`
|
|
725
|
-
*
|
|
726
|
-
* @category Expression Properties
|
|
727
|
-
*/
|
|
728
|
-
readonly isPositive: boolean | undefined;
|
|
729
|
-
/** The numeric value of this expression is >= 0, same as `isGreaterEqual(0)`
|
|
730
|
-
*
|
|
731
|
-
* @category Expression Properties
|
|
732
|
-
*/
|
|
733
|
-
readonly isNonNegative: boolean | undefined;
|
|
734
|
-
/** The numeric value of this expression is < 0, same as `isLess(0)`
|
|
735
|
-
*
|
|
736
|
-
* @category Expression Properties
|
|
737
|
-
*/
|
|
738
|
-
readonly isNegative: boolean | undefined;
|
|
739
|
-
/** The numeric value of this expression is <= 0, same as `isLessEqual(0)`
|
|
740
|
-
*
|
|
741
|
-
* @category Expression Properties
|
|
742
|
-
*/
|
|
743
|
-
readonly isNonPositive: boolean | undefined;
|
|
744
|
-
/** The keys of the dictionary.
|
|
745
|
-
*
|
|
746
|
-
* If this expression not a dictionary, return `null`
|
|
747
|
-
*
|
|
748
|
-
* @category Dictionary Expression
|
|
749
|
-
*
|
|
750
|
-
*/
|
|
751
|
-
readonly keys: IterableIterator<string> | null;
|
|
752
|
-
/**
|
|
753
|
-
*
|
|
754
|
-
* @category Dictionary Expression
|
|
755
|
-
*/
|
|
756
|
-
readonly keysCount: number;
|
|
757
|
-
/**
|
|
758
|
-
* If this expression is a dictionary, return the value of the `key` entry.
|
|
759
|
-
*
|
|
760
|
-
* @category Dictionary Expression
|
|
761
|
-
*
|
|
762
|
-
*/
|
|
763
|
-
getKey(key: string): BoxedExpression | undefined;
|
|
764
|
-
/**
|
|
765
|
-
* If this expression is a dictionary, return true if the
|
|
766
|
-
* dictionary has a `key` entry.
|
|
767
|
-
*
|
|
768
|
-
* @category Dictionary Expression
|
|
769
|
-
*
|
|
770
|
-
*/
|
|
771
|
-
hasKey(key: string): boolean;
|
|
772
|
-
/** Wikidata identifier.
|
|
773
|
-
*
|
|
774
|
-
* **Note** `undefined` if not a canonical expression.
|
|
775
|
-
*/
|
|
776
|
-
readonly wikidata: string | undefined;
|
|
777
|
-
/** An optional short description if a symbol or function expression.
|
|
778
|
-
*
|
|
779
|
-
* May include markdown. Each string is a paragraph.
|
|
780
|
-
*
|
|
781
|
-
* **Note** `undefined` if not a canonical expression.
|
|
782
|
-
*
|
|
783
|
-
*/
|
|
784
|
-
readonly description: undefined | string[];
|
|
785
|
-
/** An optional URL pointing to more information about the symbol or
|
|
786
|
-
* function head.
|
|
787
|
-
*
|
|
788
|
-
* **Note** `undefined` if not a canonical expression.
|
|
789
|
-
*
|
|
790
|
-
*/
|
|
791
|
-
readonly url: string | undefined;
|
|
792
|
-
/** Expressions with a higher complexity score are sorted
|
|
793
|
-
* first in commutative functions
|
|
794
|
-
*
|
|
795
|
-
* **Note** `undefined` if not a canonical expression.
|
|
796
|
-
*/
|
|
797
|
-
readonly complexity: number | undefined;
|
|
798
|
-
/**
|
|
799
|
-
* For symbols and functions, a possible definition associated with the
|
|
800
|
-
* expression. `baseDefinition` is the base class of symbol and function
|
|
801
|
-
* definition.
|
|
802
|
-
*
|
|
803
|
-
* **Note** `undefined` if not a canonical expression.
|
|
804
|
-
*
|
|
805
|
-
*/
|
|
806
|
-
readonly baseDefinition: BoxedBaseDefinition | undefined;
|
|
807
|
-
/**
|
|
808
|
-
* For functions, a possible definition associated with the expression.
|
|
809
|
-
*
|
|
810
|
-
* **Note** `undefined` if not a canonical expression or not a function.
|
|
811
|
-
*
|
|
812
|
-
*/
|
|
813
|
-
readonly functionDefinition: BoxedFunctionDefinition | undefined;
|
|
814
|
-
/**
|
|
815
|
-
* For symbols, a possible definition associated with the expression.
|
|
816
|
-
*
|
|
817
|
-
* **Note** `undefined` if not a symbol
|
|
818
|
-
*
|
|
819
|
-
*/
|
|
820
|
-
readonly symbolDefinition: BoxedSymbolDefinition | undefined;
|
|
821
|
-
/**
|
|
822
|
-
*
|
|
823
|
-
* Infer the domain of this expression.
|
|
824
|
-
*
|
|
825
|
-
* If the domain of this expression is already known, return `false`.
|
|
826
|
-
*
|
|
827
|
-
* If the domain was not set, set it to the inferred domain, return `true`
|
|
828
|
-
* If the domain was previously inferred, adjust it by widening it,
|
|
829
|
-
* return `true`
|
|
830
|
-
*
|
|
831
|
-
* @internal
|
|
832
|
-
*/
|
|
833
|
-
infer(domain: BoxedDomain): boolean;
|
|
834
|
-
/**
|
|
835
|
-
* Update the definition associated with this expression, using the
|
|
836
|
-
* current scope (`ce.context`).
|
|
837
|
-
*
|
|
838
|
-
* @internal
|
|
839
|
-
*/
|
|
840
|
-
bind(): void;
|
|
841
|
-
/**
|
|
842
|
-
*
|
|
843
|
-
* Reset the cached value associated with this expression.
|
|
844
|
-
*
|
|
845
|
-
* Use when the environment has changed, for example the numeric mode
|
|
846
|
-
* or precision, to force the expression to be re-evaluated.
|
|
847
|
-
*
|
|
848
|
-
* @internal
|
|
849
|
-
*/
|
|
850
|
-
reset(): void;
|
|
851
|
-
/**
|
|
852
|
-
* Return a simpler form of the canonical form of this expression.
|
|
853
|
-
*
|
|
854
|
-
* A series of rewriting rules are applied repeatedly, until no more rules
|
|
855
|
-
* apply.
|
|
856
|
-
*
|
|
857
|
-
* If a custom `simplify` handler is associated with this function
|
|
858
|
-
* definition, it is invoked.
|
|
859
|
-
*
|
|
860
|
-
* The values assigned to symbols and the assumptions about symbols may be
|
|
861
|
-
* used, for example `arg.isInteger` or `arg.isPositive`.
|
|
862
|
-
*
|
|
863
|
-
* No calculations involving decimal numbers (numbers that are not
|
|
864
|
-
* integers) are performed but exact calculations may be performed,
|
|
865
|
-
* for example:
|
|
866
|
-
*
|
|
867
|
-
* \\( \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} \\).
|
|
868
|
-
*
|
|
869
|
-
* The result is in canonical form.
|
|
870
|
-
*
|
|
871
|
-
*/
|
|
872
|
-
simplify(options?: SimplifyOptions): BoxedExpression;
|
|
873
|
-
/**
|
|
874
|
-
* Return the value of the canonical form of this expression.
|
|
875
|
-
*
|
|
876
|
-
* A pure expression always return the same value and has no side effects.
|
|
877
|
-
* If `expr.isPure` is `true`, `expr.value` and `expr.evaluate()` are
|
|
878
|
-
* synonyms.
|
|
879
|
-
*
|
|
880
|
-
* For an impure expression, `expr.value` is undefined.
|
|
881
|
-
*
|
|
882
|
-
* Evaluating an impure expression may have some side effects, for
|
|
883
|
-
* example modifying the `ComputeEngine` environment, such as its set of
|
|
884
|
-
* assumptions.
|
|
885
|
-
*
|
|
886
|
-
* Only exact calculations are performed, no approximate calculations on
|
|
887
|
-
* decimal numbers (non-integer numbers). Constants, rational numbers and
|
|
888
|
-
* square root of rational numbers are preserved.
|
|
889
|
-
*
|
|
890
|
-
* To perform approximate calculations, use `expr.N()` instead.
|
|
891
|
-
*
|
|
892
|
-
* The result of `expr.evaluate()` may be the same as `expr.simplify()`.
|
|
893
|
-
*
|
|
894
|
-
* The result is in canonical form.
|
|
895
|
-
*
|
|
896
|
-
*/
|
|
897
|
-
evaluate(options?: EvaluateOptions): BoxedExpression;
|
|
898
|
-
/** Return a numeric approximation of the canonical form of this expression.
|
|
899
|
-
*
|
|
900
|
-
* Any necessary calculations, including on decimal numbers (non-integers),
|
|
901
|
-
* are performed.
|
|
902
|
-
*
|
|
903
|
-
* The calculations are performed according to the `numericMode` and
|
|
904
|
-
* `precision` properties of the `ComputeEngine`.
|
|
905
|
-
*
|
|
906
|
-
* To only perform exact calculations, use `this.evaluate()` instead.
|
|
907
|
-
*
|
|
908
|
-
* If the function is not numeric, the result of `this.N()` is the same as
|
|
909
|
-
* `this.evaluate()`.
|
|
910
|
-
*
|
|
911
|
-
* The result is in canonical form.
|
|
912
|
-
*/
|
|
913
|
-
N(options?: NOptions): BoxedExpression;
|
|
914
|
-
compile(to?: 'javascript', options?: {
|
|
915
|
-
optimize: ('simplify' | 'evaluate')[];
|
|
916
|
-
}): ((args: Record<string, any>) => any | undefined) | undefined;
|
|
917
|
-
solve(vars: Iterable<string>): null | BoxedExpression[];
|
|
918
|
-
/**
|
|
919
|
-
* Return a JavaScript primitive representing the value of this expression.
|
|
920
|
-
*
|
|
921
|
-
* Equivalent to `expr.N().valueOf()`.
|
|
922
|
-
*
|
|
923
|
-
*/
|
|
924
|
-
get value(): number | boolean | string | number[] | undefined;
|
|
925
|
-
/**
|
|
926
|
-
* Only the value of variables can be changed (symbols that are not
|
|
927
|
-
* constants).
|
|
928
|
-
*
|
|
929
|
-
* Throws a runtime error if a constant.
|
|
930
|
-
*
|
|
931
|
-
* **Note**: If non-canonical, does nothing.
|
|
932
|
-
*
|
|
933
|
-
*/
|
|
934
|
-
set value(value: boolean | string | Decimal | Complex | {
|
|
935
|
-
re: number;
|
|
936
|
-
im: number;
|
|
937
|
-
} | {
|
|
938
|
-
num: number;
|
|
939
|
-
denom: number;
|
|
940
|
-
} | number[] | BoxedExpression | number | undefined);
|
|
941
|
-
/**
|
|
942
|
-
*
|
|
943
|
-
* The domain of the value of this expression.
|
|
944
|
-
*
|
|
945
|
-
* If a function expression, the domain of the value of the function
|
|
946
|
-
* (the codomain of the function).
|
|
947
|
-
*
|
|
948
|
-
* If a symbol the domain of the value of the symbol.
|
|
949
|
-
*
|
|
950
|
-
* Use `expr.head` to determine if an expression is a symbol or function
|
|
951
|
-
* expression.
|
|
952
|
-
*
|
|
953
|
-
* **Note**: if non-canonical or not valid, return `undefined`.
|
|
954
|
-
*
|
|
955
|
-
*/
|
|
956
|
-
get domain(): BoxedDomain | undefined;
|
|
957
|
-
/** Modify the domain of a symbol.
|
|
958
|
-
*
|
|
959
|
-
* **Note**: If non-canonical, does nothing.
|
|
960
|
-
*
|
|
961
|
-
*/
|
|
962
|
-
set domain(domain: DomainExpression | BoxedDomain | undefined);
|
|
963
|
-
/** `true` if the value of this expression is a number.
|
|
964
|
-
*
|
|
965
|
-
* `isExtendedComplex || isNaN` = `isReal || isImaginary || isInfinity || isNaN`
|
|
966
|
-
*
|
|
967
|
-
* Note that in a fateful twist of cosmic irony, `NaN` ("Not a Number")
|
|
968
|
-
* **is** a number.
|
|
969
|
-
*
|
|
970
|
-
* @category Domain Properties
|
|
971
|
-
*/
|
|
972
|
-
readonly isNumber: boolean | undefined;
|
|
973
|
-
/** The value of this expression is an element of the set ℤ: ...,-2, -1, 0, 1, 2...
|
|
974
|
-
*
|
|
975
|
-
*
|
|
976
|
-
* @category Domain Properties
|
|
977
|
-
*
|
|
978
|
-
*/
|
|
979
|
-
readonly isInteger: boolean | undefined;
|
|
980
|
-
/** The value of this expression is an element of the set ℚ, p/q with p ∈ ℕ, q ∈ ℤ ⃰ q >= 1
|
|
981
|
-
*
|
|
982
|
-
* Note that every integer is also a rational.
|
|
983
|
-
*
|
|
984
|
-
*
|
|
985
|
-
* @category Domain Properties
|
|
986
|
-
*
|
|
987
|
-
*/
|
|
988
|
-
readonly isRational: boolean | undefined;
|
|
989
|
-
/**
|
|
990
|
-
* The value of this expression is a number that is the root of a non-zero
|
|
991
|
-
* univariate polynomial with rational coefficients.
|
|
992
|
-
*
|
|
993
|
-
* All integers and rational numbers are algebraic.
|
|
994
|
-
*
|
|
995
|
-
* Transcendental numbers, such as \\( \pi \\) or \\( e \\) are not algebraic.
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
* @category Domain Properties
|
|
999
|
-
*
|
|
1000
|
-
*/
|
|
1001
|
-
readonly isAlgebraic: boolean | undefined;
|
|
1002
|
-
/**
|
|
1003
|
-
* The value of this expression is real number: finite and not imaginary.
|
|
1004
|
-
*
|
|
1005
|
-
* `isFinite && !isImaginary`
|
|
1006
|
-
*
|
|
1007
|
-
*
|
|
1008
|
-
* @category Domain Properties
|
|
1009
|
-
*/
|
|
1010
|
-
readonly isReal: boolean | undefined;
|
|
1011
|
-
/** Real or ±Infinity
|
|
1012
|
-
*
|
|
1013
|
-
* `isReal || isInfinity`
|
|
1014
|
-
*
|
|
1015
|
-
*
|
|
1016
|
-
* @category Domain Properties
|
|
1017
|
-
*/
|
|
1018
|
-
readonly isExtendedReal: boolean | undefined;
|
|
1019
|
-
/**
|
|
1020
|
-
* The value of this expression is a number, but not `NaN` or any Infinity
|
|
1021
|
-
*
|
|
1022
|
-
* `isReal || isImaginary`
|
|
1023
|
-
*
|
|
1024
|
-
*
|
|
1025
|
-
* @category Domain Properties
|
|
1026
|
-
*
|
|
1027
|
-
*/
|
|
1028
|
-
readonly isComplex: boolean | undefined;
|
|
1029
|
-
/** `isReal || isImaginary || isInfinity`
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
* @category Domain Properties
|
|
1033
|
-
*/
|
|
1034
|
-
readonly isExtendedComplex: boolean | undefined;
|
|
1035
|
-
/** The value of this expression is a number with a imaginary part
|
|
1036
|
-
*
|
|
1037
|
-
*
|
|
1038
|
-
* @category Domain Properties
|
|
1039
|
-
*/
|
|
1040
|
-
readonly isImaginary: boolean | undefined;
|
|
1041
|
-
/** Mathematical equality (strong equality), that is the value
|
|
1042
|
-
* of this expression and of `rhs` are numerically equal.
|
|
1043
|
-
*
|
|
1044
|
-
* The numeric value of both expressions are compared.
|
|
1045
|
-
*
|
|
1046
|
-
* Numbers whose difference is less than `engine.tolerance` are
|
|
1047
|
-
* considered equal. This tolerance is set when the `engine.precision` is
|
|
1048
|
-
* changed to be such that the last two digits are ignored.
|
|
1049
|
-
*
|
|
1050
|
-
* @category Relational Operator
|
|
1051
|
-
*/
|
|
1052
|
-
isEqual(rhs: BoxedExpression): boolean;
|
|
1053
|
-
}
|
|
1054
|
-
/** A semi boxed expression is a MathJSON expression which can include some
|
|
1055
|
-
* boxed terms.
|
|
1056
|
-
*
|
|
1057
|
-
* This is convenient when creating new expressions from portions
|
|
1058
|
-
* of an existing `BoxedExpression` while avoiding unboxing and reboxing.
|
|
1059
|
-
*
|
|
1060
|
-
* @category Boxed Expression
|
|
1061
|
-
*/
|
|
1062
|
-
export type SemiBoxedExpression = number | string | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[] | BoxedExpression;
|
|
1063
|
-
/**
|
|
1064
|
-
* Control how a pattern is matched to an expression.
|
|
1065
|
-
*
|
|
1066
|
-
* - `substitution`: if present, assumes these values for the named wildcards, and ensure that subsequent occurence of the same wildcard have the same value.
|
|
1067
|
-
* - `recursive`: if true, match recursively, otherwise match only the top level.
|
|
1068
|
-
* - `numericTolerance`: if present, the tolerance for numeric comparison.
|
|
1069
|
-
* - `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.
|
|
1070
|
-
*
|
|
1071
|
-
* @category Pattern Matching
|
|
1072
|
-
*
|
|
1073
|
-
*/
|
|
1074
|
-
export type PatternMatchOptions = {
|
|
1075
|
-
substitution?: BoxedSubstitution;
|
|
1076
|
-
recursive?: boolean;
|
|
1077
|
-
numericTolerance?: number;
|
|
1078
|
-
exact?: boolean;
|
|
1079
|
-
};
|
|
1080
|
-
/**
|
|
1081
|
-
* @noInheritDoc
|
|
1082
|
-
*
|
|
1083
|
-
* @category Pattern Matching
|
|
1084
|
-
*/
|
|
1085
|
-
export type Pattern = BoxedExpression;
|
|
1086
|
-
/** @category Assumptions */
|
|
1087
|
-
export interface ExpressionMapInterface<U> {
|
|
1088
|
-
has(expr: BoxedExpression): boolean;
|
|
1089
|
-
get(expr: BoxedExpression): U | undefined;
|
|
1090
|
-
set(expr: BoxedExpression, value: U): void;
|
|
1091
|
-
delete(expr: BoxedExpression): void;
|
|
1092
|
-
clear(): void;
|
|
1093
|
-
[Symbol.iterator](): IterableIterator<[BoxedExpression, U]>;
|
|
1094
|
-
entries(): IterableIterator<[BoxedExpression, U]>;
|
|
1095
|
-
}
|
|
15
|
+
import { FunctionDefinition, SemiBoxedExpression, SymbolDefinition } from './boxed-expression/public';
|
|
16
|
+
export * from './boxed-expression/public';
|
|
1096
17
|
/**
|
|
1097
18
|
* A table mapping identifiers to their definition.
|
|
1098
19
|
*
|
|
@@ -1115,760 +36,3 @@ export type IdentifierDefinition = SymbolDefinition | FunctionDefinition | SemiB
|
|
|
1115
36
|
export type IdentifierDefinitions = Readonly<{
|
|
1116
37
|
[id: string]: IdentifierDefinition;
|
|
1117
38
|
}>;
|
|
1118
|
-
/**
|
|
1119
|
-
* The entries have been validated and optimized for faster evaluation.
|
|
1120
|
-
*
|
|
1121
|
-
* When a new scope is created with `pushScope()` or when creating a new
|
|
1122
|
-
* engine instance, new instances of this type are created as needed.
|
|
1123
|
-
*
|
|
1124
|
-
* @category Definitions
|
|
1125
|
-
*/
|
|
1126
|
-
export type RuntimeIdentifierDefinitions = Map<string, BoxedSymbolDefinition | BoxedFunctionDefinition>;
|
|
1127
|
-
/**
|
|
1128
|
-
* A scope is a set of names in a dictionary that are bound (defined) in
|
|
1129
|
-
* a MathJSON expression.
|
|
1130
|
-
*
|
|
1131
|
-
* Scopes are arranged in a stack structure. When an expression that defined
|
|
1132
|
-
* a new scope is evaluated, the new scope is added to the scope stack.
|
|
1133
|
-
* Outside of the expression, the scope is removed from the scope stack.
|
|
1134
|
-
*
|
|
1135
|
-
* The scope stack is used to resolve symbols, and it is possible for
|
|
1136
|
-
* a scope to 'mask' definitions from previous scopes.
|
|
1137
|
-
*
|
|
1138
|
-
* Scopes are lexical (also called a static scope): they are defined based on
|
|
1139
|
-
* where they are in an expression, they are not determined at runtime.
|
|
1140
|
-
*
|
|
1141
|
-
* @category Compute Engine
|
|
1142
|
-
*/
|
|
1143
|
-
export type Scope = {
|
|
1144
|
-
/** Signal `timeout` when the execution time for this scope is exceeded.
|
|
1145
|
-
*
|
|
1146
|
-
* Time in seconds, default 2s.
|
|
1147
|
-
*
|
|
1148
|
-
* @experimental
|
|
1149
|
-
*/
|
|
1150
|
-
timeLimit: number;
|
|
1151
|
-
/** Signal `out-of-memory` when the memory usage for this scope is exceeded.
|
|
1152
|
-
*
|
|
1153
|
-
* Memory is in Megabytes, default: 1Mb.
|
|
1154
|
-
*
|
|
1155
|
-
* @experimental
|
|
1156
|
-
*/
|
|
1157
|
-
memoryLimit: number;
|
|
1158
|
-
/** Signal `recursion-depth-exceeded` when the recursion depth for this
|
|
1159
|
-
* scope is exceeded.
|
|
1160
|
-
*
|
|
1161
|
-
* @experimental
|
|
1162
|
-
*/
|
|
1163
|
-
recursionLimit: number;
|
|
1164
|
-
/** Signal `iteration-limit-exceeded` when the iteration limit
|
|
1165
|
-
* in a loop is exceeded. Default: no limits.
|
|
1166
|
-
*
|
|
1167
|
-
* @experimental
|
|
1168
|
-
*/
|
|
1169
|
-
iterationLimit: number;
|
|
1170
|
-
};
|
|
1171
|
-
/** @category Compute Engine */
|
|
1172
|
-
export type RuntimeScope = Scope & {
|
|
1173
|
-
parentScope?: RuntimeScope;
|
|
1174
|
-
ids?: RuntimeIdentifierDefinitions;
|
|
1175
|
-
assumptions: undefined | ExpressionMapInterface<boolean>;
|
|
1176
|
-
};
|
|
1177
|
-
/**
|
|
1178
|
-
* @category Definitions
|
|
1179
|
-
*
|
|
1180
|
-
*/
|
|
1181
|
-
export type BaseDefinition = {
|
|
1182
|
-
/** A short (about 1 line) description. May contain Markdown. */
|
|
1183
|
-
description?: string | string[];
|
|
1184
|
-
/** A URL pointing to more information about this symbol or head. */
|
|
1185
|
-
url?: string;
|
|
1186
|
-
/**
|
|
1187
|
-
* A short string representing an entry in a wikibase.
|
|
1188
|
-
*
|
|
1189
|
-
* For example `Q167` is the [wikidata entry](https://www.wikidata.org/wiki/Q167)
|
|
1190
|
-
* for the `Pi` constant.
|
|
1191
|
-
*/
|
|
1192
|
-
wikidata?: string;
|
|
1193
|
-
};
|
|
1194
|
-
/**
|
|
1195
|
-
* @category Definitions
|
|
1196
|
-
*
|
|
1197
|
-
*/
|
|
1198
|
-
export interface BoxedBaseDefinition {
|
|
1199
|
-
name: string;
|
|
1200
|
-
wikidata?: string;
|
|
1201
|
-
description?: string | string[];
|
|
1202
|
-
url?: string;
|
|
1203
|
-
/**
|
|
1204
|
-
* The scope this definition belongs to.
|
|
1205
|
-
*
|
|
1206
|
-
* This field is usually undefined, but its value is set by `getDefinition()`
|
|
1207
|
-
*/
|
|
1208
|
-
scope: RuntimeScope | undefined;
|
|
1209
|
-
/** When the environment changes, for example the numerical precision,
|
|
1210
|
-
* call `reset()` so that any cached values can be recalculated.
|
|
1211
|
-
*/
|
|
1212
|
-
reset(): any;
|
|
1213
|
-
}
|
|
1214
|
-
/**
|
|
1215
|
-
* A function definition can have some flags to indicate specific
|
|
1216
|
-
* properties of the function.
|
|
1217
|
-
* @category Definitions
|
|
1218
|
-
*/
|
|
1219
|
-
export type FunctionDefinitionFlags = {
|
|
1220
|
-
/** If `true`, the function is applied element by element to lists, matrices
|
|
1221
|
-
* (`["List"]` or `["Tuple"]` expressions) and equations (relational
|
|
1222
|
-
* operators).
|
|
1223
|
-
*
|
|
1224
|
-
* **Default**: `false`
|
|
1225
|
-
*/
|
|
1226
|
-
threadable: boolean;
|
|
1227
|
-
/** If `true`, `["f", ["f", a], b]` simplifies to `["f", a, b]`
|
|
1228
|
-
*
|
|
1229
|
-
* **Default**: `false`
|
|
1230
|
-
*/
|
|
1231
|
-
associative: boolean;
|
|
1232
|
-
/** If `true`, `["f", a, b]` equals `["f", b, a]`. The canonical
|
|
1233
|
-
* version of the function will order the arguments.
|
|
1234
|
-
*
|
|
1235
|
-
* **Default**: `false`
|
|
1236
|
-
*/
|
|
1237
|
-
commutative: boolean;
|
|
1238
|
-
/** If `true`, when the function is univariate, `["f", ["Add", x, c]]` where `c`
|
|
1239
|
-
* is constant, is simplified to `["Add", ["f", x], c]`.
|
|
1240
|
-
*
|
|
1241
|
-
* When the function is multivariate, additivity is considered only on the
|
|
1242
|
-
* first argument: `["f", ["Add", x, c], y]` simplifies to `["Add", ["f", x, y], c]`.
|
|
1243
|
-
*
|
|
1244
|
-
* For example, `Log` is additive.
|
|
1245
|
-
*
|
|
1246
|
-
* **Default**: `false`
|
|
1247
|
-
*/
|
|
1248
|
-
/** If `true`, when the function is univariate, `["f", ["Multiply", x, y]]`
|
|
1249
|
-
* simplifies to `["Multiply", ["f", x], ["f", y]]`.
|
|
1250
|
-
*
|
|
1251
|
-
* When the function is multivariate, multiplicativity is considered only on the
|
|
1252
|
-
* first argument: `["f", ["Multiply", x, y], z]` simplifies to
|
|
1253
|
-
* `["Multiply", ["f", x, z], ["f", y, z]]`
|
|
1254
|
-
*
|
|
1255
|
-
* **Default**: `false`
|
|
1256
|
-
*/
|
|
1257
|
-
/** If `true`, when the function is univariate, `["f", ["Multiply", x, c]]`
|
|
1258
|
-
* simplifies to `["Multiply", ["f", x], c]` where `c` is constant
|
|
1259
|
-
*
|
|
1260
|
-
* When the function is multivariate, multiplicativity is considered only on
|
|
1261
|
-
* the first argument: `["f", ["Multiply", x, y], z]` simplifies to
|
|
1262
|
-
* `["Multiply", ["f", x, z], ["f", y, z]]`
|
|
1263
|
-
*
|
|
1264
|
-
* Default: `false`
|
|
1265
|
-
*/
|
|
1266
|
-
/** If `true`, `["f", ["f", x]]` simplifies to `["f", x]`.
|
|
1267
|
-
*
|
|
1268
|
-
* **Default**: `false`
|
|
1269
|
-
*/
|
|
1270
|
-
idempotent: boolean;
|
|
1271
|
-
/** If `true`, `["f", ["f", x]]` simplifies to `x`.
|
|
1272
|
-
*
|
|
1273
|
-
* **Default**: `false`
|
|
1274
|
-
*/
|
|
1275
|
-
involution: boolean;
|
|
1276
|
-
/** If `true`, the value of this function is always the same for a given
|
|
1277
|
-
* set of arguments and it has no side effects.
|
|
1278
|
-
*
|
|
1279
|
-
* An expression using this function is pure if the function and all its
|
|
1280
|
-
* arguments are pure.
|
|
1281
|
-
*
|
|
1282
|
-
* For example `Sin` is pure, `Random` isn't.
|
|
1283
|
-
*
|
|
1284
|
-
* This information may be used to cache the value of expressions.
|
|
1285
|
-
*
|
|
1286
|
-
* **Default:** `true`
|
|
1287
|
-
*/
|
|
1288
|
-
pure: boolean;
|
|
1289
|
-
/**
|
|
1290
|
-
* An inert function evaluates directly to one of its argument, typically
|
|
1291
|
-
* the first one. They may be used to provide formating hints, but do
|
|
1292
|
-
* not affect simplification or evaluation.
|
|
1293
|
-
*
|
|
1294
|
-
* **Default:** false
|
|
1295
|
-
*/
|
|
1296
|
-
inert: boolean;
|
|
1297
|
-
/**
|
|
1298
|
-
* All the arguments of a numeric function are numeric,
|
|
1299
|
-
* and its value is numeric.
|
|
1300
|
-
*/
|
|
1301
|
-
numeric: boolean;
|
|
1302
|
-
};
|
|
1303
|
-
/**
|
|
1304
|
-
* @category Definitions
|
|
1305
|
-
*
|
|
1306
|
-
*/
|
|
1307
|
-
export type FunctionSignature = {
|
|
1308
|
-
/** The domain of this signature, a domain compatible with the `Functions`
|
|
1309
|
-
* domain).
|
|
1310
|
-
*
|
|
1311
|
-
* @deprecated Use params, optParams, restParam and result instead
|
|
1312
|
-
*/
|
|
1313
|
-
domain?: DomainExpression;
|
|
1314
|
-
params?: DomainExpression[];
|
|
1315
|
-
optParams?: DomainExpression[];
|
|
1316
|
-
restParam?: DomainExpression;
|
|
1317
|
-
/** The domain of the result of the function. Either a domain
|
|
1318
|
-
* expression, or a function that returns a boxed domain.
|
|
1319
|
-
*/
|
|
1320
|
-
result?: DomainExpression | ((ce: IComputeEngine, args: BoxedDomain[]) => BoxedDomain | null | undefined);
|
|
1321
|
-
/**
|
|
1322
|
-
* Return the canonical form of the expression with the arguments `args`.
|
|
1323
|
-
*
|
|
1324
|
-
* The arguments (`args`) may not be in canonical form. If necessary, they
|
|
1325
|
-
* can be put in canonical form.
|
|
1326
|
-
*
|
|
1327
|
-
* This handler should validate the domain and number of the arguments.
|
|
1328
|
-
*
|
|
1329
|
-
* If a required argument is missing, it should be indicated with a
|
|
1330
|
-
* `["Error", "'missing"]` expression. If more arguments than expected
|
|
1331
|
-
* are present, this should be indicated with an
|
|
1332
|
-
* ["Error", "'unexpected-argument'"]` error expression
|
|
1333
|
-
*
|
|
1334
|
-
* If the domain of an argument is not compatible, it should be indicated
|
|
1335
|
-
* with an `incompatible-domain` error.
|
|
1336
|
-
*
|
|
1337
|
-
* `["Sequence"]` expressions are not folded and need to be handled
|
|
1338
|
-
* explicitly.
|
|
1339
|
-
*
|
|
1340
|
-
* If the function is associative, idempotent or an involution,
|
|
1341
|
-
* this handler should account for it. Notably, if it is commutative, the
|
|
1342
|
-
* arguments should be sorted in canonical order.
|
|
1343
|
-
*
|
|
1344
|
-
* The handler can make transformations based on the value of the arguments
|
|
1345
|
-
* that are exact and literal (i.e.
|
|
1346
|
-
* `arg.numericValue !== null && arg.isExact`).
|
|
1347
|
-
*
|
|
1348
|
-
* Values of symbols should not be substituted, unless they have
|
|
1349
|
-
* a `holdUntil` attribute of `"never"`.
|
|
1350
|
-
*
|
|
1351
|
-
* The handler should not consider the value or any assumptions about any
|
|
1352
|
-
* of the arguments that are symbols or functions (i.e. `arg.isZero`,
|
|
1353
|
-
* `arg.isInteger`, etc...) since those may change over time.
|
|
1354
|
-
*
|
|
1355
|
-
* The result of the handler should be a canonical expression.
|
|
1356
|
-
*
|
|
1357
|
-
* If the arguments do not match, they should be replaced with an appropriate
|
|
1358
|
-
* `["Error"]` expression. If the expression cannot be put in canonical form,
|
|
1359
|
-
* the handler should return `null`.
|
|
1360
|
-
*
|
|
1361
|
-
*/
|
|
1362
|
-
canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
|
|
1363
|
-
/**
|
|
1364
|
-
* Rewrite an expression into a simpler form.
|
|
1365
|
-
*
|
|
1366
|
-
* The arguments are in canonical form and have been simplified.
|
|
1367
|
-
*
|
|
1368
|
-
* The handler can use the values assigned to symbols and the assumptions
|
|
1369
|
-
* about symbols, for example with `arg.numericValue`, `arg.isInteger` or
|
|
1370
|
-
* `arg.isPositive`.
|
|
1371
|
-
*
|
|
1372
|
-
* Even though a symbol may not have a value, there may be some information
|
|
1373
|
-
* about it reflected for example in `this.isZero` or `this.isPrime`.
|
|
1374
|
-
*
|
|
1375
|
-
* The handler should not perform approximate numeric calculations, such
|
|
1376
|
-
* as calculations involving decimal numbers (non-integers). Making exact
|
|
1377
|
-
* calculations on integers or rationals is OK.
|
|
1378
|
-
*
|
|
1379
|
-
* Do not reduce constants with a `holdUntil` attribute of `"N"`
|
|
1380
|
-
* or `"evaluate"`.
|
|
1381
|
-
*
|
|
1382
|
-
* This handler should not have any side-effects: do not modify
|
|
1383
|
-
* the environment of the `ComputeEngine` instance, do not perform I/O,
|
|
1384
|
-
* do not do calculations that depend on random values.
|
|
1385
|
-
*
|
|
1386
|
-
* If no simplification can be performed due to the values, domains or
|
|
1387
|
-
* assumptions about its arguments, for example, return `undefined`.
|
|
1388
|
-
*
|
|
1389
|
-
*/
|
|
1390
|
-
simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
|
|
1391
|
-
/**
|
|
1392
|
-
* Evaluate a function expression.
|
|
1393
|
-
*
|
|
1394
|
-
* The arguments have been evaluated, except the arguments to which a
|
|
1395
|
-
* `hold` applied.
|
|
1396
|
-
*
|
|
1397
|
-
* It is not necessary to further simplify or evaluate the arguments.
|
|
1398
|
-
*
|
|
1399
|
-
* If performing numerical calculations, if all the arguments are exact,
|
|
1400
|
-
* return an exact expression. If any of the arguments is not exact, that is
|
|
1401
|
-
* if it is a literal decimal (non-integer) number, return an approximation.
|
|
1402
|
-
* In this case, the value may be the same as `expr.N()`.
|
|
1403
|
-
*
|
|
1404
|
-
* When doing an exact calculation:
|
|
1405
|
-
* - do not reduce rational numbers to decimal (floating point approximation)
|
|
1406
|
-
* - do not down convert bignums to machine numbers
|
|
1407
|
-
* - do not reduce square roots of rational numbers
|
|
1408
|
-
* - do not reduce constants with a `holdUntil` attribute of `"N"`
|
|
1409
|
-
*
|
|
1410
|
-
* If the expression cannot be evaluated, due to the values, domains, or
|
|
1411
|
-
* assumptions about its arguments, for example, return `undefined` or
|
|
1412
|
-
* an `["Error"]` expression.
|
|
1413
|
-
*/
|
|
1414
|
-
evaluate?: SemiBoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
|
|
1415
|
-
/**
|
|
1416
|
-
* Evaluate numerically a function expression.
|
|
1417
|
-
*
|
|
1418
|
-
* The arguments `args` have been simplified and evaluated, numerically
|
|
1419
|
-
* if possible, except the arguments to which a `hold` apply.
|
|
1420
|
-
*
|
|
1421
|
-
* The arguments may be a combination of numbers, symbolic
|
|
1422
|
-
* expressions and other expressions.
|
|
1423
|
-
*
|
|
1424
|
-
* Perform as many calculations as possible, and return the result.
|
|
1425
|
-
*
|
|
1426
|
-
* Return `undefined` if there isn't enough information to perform
|
|
1427
|
-
* the evaluation, for example one of the arguments is a symbol with
|
|
1428
|
-
* no value. If the handler returns `undefined`, symbolic evaluation of
|
|
1429
|
-
* the expression will be returned instead to the caller.
|
|
1430
|
-
*
|
|
1431
|
-
* Return `NaN` if there is enough information to perform the
|
|
1432
|
-
* evaluation, but a literal argument is out of range or
|
|
1433
|
-
* not of the expected type.
|
|
1434
|
-
*
|
|
1435
|
-
* Use the value of `ce.numericMode` to determine how to perform
|
|
1436
|
-
* the numeric evaluation.
|
|
1437
|
-
*
|
|
1438
|
-
* Note that regardless of the current value of `ce.numericMode`, the
|
|
1439
|
-
* arguments may be boxed numbers representing machine numbers, bignum
|
|
1440
|
-
* numbers, complex numbers, rationals or big rationals.
|
|
1441
|
-
*
|
|
1442
|
-
* If the numeric mode does not allow complex numbers (the
|
|
1443
|
-
* `engine.numericMode` is not `"complex"` or `"auto"`) and the result of
|
|
1444
|
-
* the evaluation would be a complex number, return `NaN` instead.
|
|
1445
|
-
*
|
|
1446
|
-
* If `ce.numericMode` is `"bignum"` or `"auto"` the evaluation should
|
|
1447
|
-
* be done using bignums.
|
|
1448
|
-
*
|
|
1449
|
-
* Otherwise, `ce.numericMode` is `"machine", the evaluation should be
|
|
1450
|
-
* performed using machine numbers.
|
|
1451
|
-
*
|
|
1452
|
-
* You may perform any necessary computations, including approximate
|
|
1453
|
-
* calculations on floating point numbers.
|
|
1454
|
-
*
|
|
1455
|
-
*/
|
|
1456
|
-
N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
|
|
1457
|
-
/** Dimensional analysis
|
|
1458
|
-
* @experimental
|
|
1459
|
-
*/
|
|
1460
|
-
evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
|
|
1461
|
-
/** Return the sign of the function expression. */
|
|
1462
|
-
sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
|
|
1463
|
-
/** Return a compiled (optimized) expression. */
|
|
1464
|
-
compile?: (expr: BoxedExpression) => CompiledExpression;
|
|
1465
|
-
};
|
|
1466
|
-
/**
|
|
1467
|
-
* @category Definitions
|
|
1468
|
-
*
|
|
1469
|
-
*/
|
|
1470
|
-
export type BoxedFunctionSignature = {
|
|
1471
|
-
inferredSignature: boolean;
|
|
1472
|
-
params: BoxedDomain[];
|
|
1473
|
-
optParams: BoxedDomain[];
|
|
1474
|
-
restParam?: BoxedDomain;
|
|
1475
|
-
result: BoxedDomain | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedDomain | null | undefined);
|
|
1476
|
-
canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
|
|
1477
|
-
simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
|
|
1478
|
-
evaluate?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
|
|
1479
|
-
N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
|
|
1480
|
-
evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
|
|
1481
|
-
sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
|
|
1482
|
-
compile?: (expr: BoxedExpression) => CompiledExpression;
|
|
1483
|
-
};
|
|
1484
|
-
/**
|
|
1485
|
-
* The handlers are the primitive operations that can be performed on
|
|
1486
|
-
* collections.
|
|
1487
|
-
*
|
|
1488
|
-
* There are two types of collections:
|
|
1489
|
-
* - finite collections, such as lists, tuples, sets, matrices, etc...
|
|
1490
|
-
* The `size()` handler of finite collections returns the number of elements
|
|
1491
|
-
* - infinite collections, such as sequences, ranges, etc...
|
|
1492
|
-
* The `size()` handler of infinite collections returns `Infinity`
|
|
1493
|
-
* Infinite collections are not indexable, they have no `at()` handler.
|
|
1494
|
-
*
|
|
1495
|
-
* @category Definitions
|
|
1496
|
-
*/
|
|
1497
|
-
export type CollectionHandlers = {
|
|
1498
|
-
/** Return an iterator
|
|
1499
|
-
* - start is optional and is a 1-based index.
|
|
1500
|
-
* - if start is not specified, start from index 1
|
|
1501
|
-
* - count is optional and is the number of elements to return
|
|
1502
|
-
* - if count is not specified or negative, return all the elements from start to the endna
|
|
1503
|
-
*
|
|
1504
|
-
* If there is a `keys()` handler, there is no `iterator()` handler.
|
|
1505
|
-
*
|
|
1506
|
-
* @category Definitions
|
|
1507
|
-
*/
|
|
1508
|
-
iterator: (expr: BoxedExpression, start?: number, count?: number) => Iterator<BoxedExpression, undefined>;
|
|
1509
|
-
/** Return the element at the specified index.
|
|
1510
|
-
* The first element is `at(1)`, the last element is `at(-1)`.
|
|
1511
|
-
* If the index is <0, return the element at index `size() + index + 1`.
|
|
1512
|
-
* The index can also be a string for example for dictionaries.
|
|
1513
|
-
* If the index is invalid, return `undefined`.
|
|
1514
|
-
*/
|
|
1515
|
-
at: (expr: BoxedExpression, index: number | string) => undefined | BoxedExpression;
|
|
1516
|
-
/** Return the number of elements in the collection.
|
|
1517
|
-
* An empty collection has a size of 0.
|
|
1518
|
-
*/
|
|
1519
|
-
size: (expr: BoxedExpression) => number;
|
|
1520
|
-
/**
|
|
1521
|
-
* If the collection is indexed by strings, return the valid values
|
|
1522
|
-
* for the index.
|
|
1523
|
-
*/
|
|
1524
|
-
keys: (expr: BoxedExpression) => undefined | Iterator<string>;
|
|
1525
|
-
/**
|
|
1526
|
-
* Return the index of the first element that matches the target expression.
|
|
1527
|
-
* The comparison is done using the `target.isEqual()` method.
|
|
1528
|
-
* If the expression is not found, return `undefined`.
|
|
1529
|
-
* If the expression is found, return the index, 1-based.
|
|
1530
|
-
* If the expression is found multiple times, return the index of the first
|
|
1531
|
-
* match.
|
|
1532
|
-
*
|
|
1533
|
-
* From is the starting index for the search. If negative, start from the end
|
|
1534
|
-
* and search backwards.
|
|
1535
|
-
*/
|
|
1536
|
-
indexOf: (expr: BoxedExpression, target: BoxedExpression, from?: number) => number | string | undefined;
|
|
1537
|
-
};
|
|
1538
|
-
/**
|
|
1539
|
-
* Definition record for a function.
|
|
1540
|
-
* @category Definitions
|
|
1541
|
-
*
|
|
1542
|
-
*/
|
|
1543
|
-
export type FunctionDefinition = BaseDefinition & Partial<CollectionHandlers> & Partial<FunctionDefinitionFlags> & {
|
|
1544
|
-
/**
|
|
1545
|
-
* A number used to order arguments.
|
|
1546
|
-
*
|
|
1547
|
-
* Argument with higher complexity are placed after arguments with lower
|
|
1548
|
-
* complexity when ordered canonically in commutative functions.
|
|
1549
|
-
*
|
|
1550
|
-
* - Additive functions: 1000-1999
|
|
1551
|
-
* - Multiplicative functions: 2000-2999
|
|
1552
|
-
* - Root and power functions: 3000-3999
|
|
1553
|
-
* - Log functions: 4000-4999
|
|
1554
|
-
* - Trigonometric functions: 5000-5999
|
|
1555
|
-
* - Hypertrigonometric functions: 6000-6999
|
|
1556
|
-
* - Special functions (factorial, Gamma, ...): 7000-7999
|
|
1557
|
-
* - Collections: 8000-8999
|
|
1558
|
-
* - Inert and styling: 9000-9999
|
|
1559
|
-
* - Logic: 10000-10999
|
|
1560
|
-
* - Relational: 11000-11999
|
|
1561
|
-
*
|
|
1562
|
-
* **Default**: 100,000
|
|
1563
|
-
*/
|
|
1564
|
-
complexity?: number;
|
|
1565
|
-
/**
|
|
1566
|
-
* - `"none"` Each of the arguments is evaluated (default)
|
|
1567
|
-
* - `"all"` None of the arguments are evaluated and they are passed as is
|
|
1568
|
-
* - `"first"` The first argument is not evaluated, the others are
|
|
1569
|
-
* - `"rest"` The first argument is evaluated, the others aren't
|
|
1570
|
-
* - `"last"`: The last argument is not evaluated, the others are
|
|
1571
|
-
* - `"most"`: All the arguments are evaluated, except the last one
|
|
1572
|
-
*
|
|
1573
|
-
* **Default**: `"none"`
|
|
1574
|
-
*/
|
|
1575
|
-
hold?: Hold;
|
|
1576
|
-
signature: FunctionSignature;
|
|
1577
|
-
};
|
|
1578
|
-
/**
|
|
1579
|
-
* @category Definitions
|
|
1580
|
-
*
|
|
1581
|
-
*/
|
|
1582
|
-
export type BoxedFunctionDefinition = BoxedBaseDefinition & Partial<CollectionHandlers> & FunctionDefinitionFlags & {
|
|
1583
|
-
complexity: number;
|
|
1584
|
-
hold: Hold;
|
|
1585
|
-
signature: BoxedFunctionSignature;
|
|
1586
|
-
};
|
|
1587
|
-
/**
|
|
1588
|
-
* When used in a `SymbolDefinition`, these flags are optional.
|
|
1589
|
-
*
|
|
1590
|
-
* If provided, they will override the value derived from
|
|
1591
|
-
* the symbol's value.
|
|
1592
|
-
*
|
|
1593
|
-
* For example, it might be useful to override `algebraic = false`
|
|
1594
|
-
* for a transcendental number.
|
|
1595
|
-
*
|
|
1596
|
-
* @category Definitions
|
|
1597
|
-
*/
|
|
1598
|
-
export type NumericFlags = {
|
|
1599
|
-
number: boolean | undefined;
|
|
1600
|
-
integer: boolean | undefined;
|
|
1601
|
-
rational: boolean | undefined;
|
|
1602
|
-
algebraic: boolean | undefined;
|
|
1603
|
-
real: boolean | undefined;
|
|
1604
|
-
extendedReal: boolean | undefined;
|
|
1605
|
-
complex: boolean | undefined;
|
|
1606
|
-
extendedComplex: boolean | undefined;
|
|
1607
|
-
imaginary: boolean | undefined;
|
|
1608
|
-
positive: boolean | undefined;
|
|
1609
|
-
nonPositive: boolean | undefined;
|
|
1610
|
-
negative: boolean | undefined;
|
|
1611
|
-
nonNegative: boolean | undefined;
|
|
1612
|
-
zero: boolean | undefined;
|
|
1613
|
-
notZero: boolean | undefined;
|
|
1614
|
-
one: boolean | undefined;
|
|
1615
|
-
negativeOne: boolean | undefined;
|
|
1616
|
-
infinity: boolean | undefined;
|
|
1617
|
-
NaN: boolean | undefined;
|
|
1618
|
-
finite: boolean | undefined;
|
|
1619
|
-
even: boolean | undefined;
|
|
1620
|
-
odd: boolean | undefined;
|
|
1621
|
-
prime: boolean | undefined;
|
|
1622
|
-
composite: boolean | undefined;
|
|
1623
|
-
};
|
|
1624
|
-
/**
|
|
1625
|
-
* @category Definitions
|
|
1626
|
-
*
|
|
1627
|
-
*/
|
|
1628
|
-
export type SymbolAttributes = {
|
|
1629
|
-
/**
|
|
1630
|
-
* If `true` the value of the symbol is constant. The value or domain of
|
|
1631
|
-
* symbols with this attribute set to `true` cannot be changed.
|
|
1632
|
-
*
|
|
1633
|
-
* If `false`, the symbol is a variable.
|
|
1634
|
-
*
|
|
1635
|
-
* **Default**: `false`
|
|
1636
|
-
*/
|
|
1637
|
-
constant: boolean;
|
|
1638
|
-
/**
|
|
1639
|
-
* If the symbol has a value, it is held as indicated in the table below.
|
|
1640
|
-
* A green checkmark indicate that the symbol is substituted.
|
|
1641
|
-
|
|
1642
|
-
<div className="symbols-table">
|
|
1643
|
-
|
|
1644
|
-
| Operation | `"never"` | `"simplify"` | `"evaluate"` | `"N"` |
|
|
1645
|
-
| :--- | :----- |
|
|
1646
|
-
| `canonical()`| (X) | | | |
|
|
1647
|
-
| `simplify()` | (X) | (X) | | |
|
|
1648
|
-
| `evaluate()` | (X) | (X) | (X) | |
|
|
1649
|
-
| `"N()"` | (X) | (X) | (X) | (X) |
|
|
1650
|
-
|
|
1651
|
-
</div>
|
|
1652
|
-
|
|
1653
|
-
* Some examples:
|
|
1654
|
-
* - `i` has `holdUntil: 'never'`
|
|
1655
|
-
* - `GoldenRatio` has `holdUntil: 'simplify'` (symbolic constant)
|
|
1656
|
-
* - `x` has `holdUntil: 'evaluate'` (variables)
|
|
1657
|
-
* - `Pi` has `holdUntil: 'N'` (special numeric constant)
|
|
1658
|
-
*
|
|
1659
|
-
* **Default:** `evaluate`
|
|
1660
|
-
*/
|
|
1661
|
-
holdUntil: 'never' | 'simplify' | 'evaluate' | 'N';
|
|
1662
|
-
};
|
|
1663
|
-
/**
|
|
1664
|
-
* A bound symbol (i.e. one with an associated definition) has either a domain
|
|
1665
|
-
* (e.g. ∀ x ∈ ℝ), a value (x = 5) or both (π: value = 3.14... domain = TranscendentalNumbers)
|
|
1666
|
-
* @category Definitions
|
|
1667
|
-
*/
|
|
1668
|
-
export type SymbolDefinition = BaseDefinition & Partial<SymbolAttributes> & {
|
|
1669
|
-
domain?: DomainLiteral | BoxedDomain;
|
|
1670
|
-
/** If true, the domain is inferred, and could be adjusted later
|
|
1671
|
-
* as more information becomes available or if the symbol is explicitly
|
|
1672
|
-
* declared.
|
|
1673
|
-
*/
|
|
1674
|
-
inferred?: boolean;
|
|
1675
|
-
/** `value` can be a JS function since for some constants, such as
|
|
1676
|
-
* `Pi`, the actual value depends on the `precision` setting of the
|
|
1677
|
-
* `ComputeEngine` and possible other environment settings */
|
|
1678
|
-
value?: LatexString | SemiBoxedExpression | ((ce: IComputeEngine) => SemiBoxedExpression | null);
|
|
1679
|
-
flags?: Partial<NumericFlags>;
|
|
1680
|
-
};
|
|
1681
|
-
/**
|
|
1682
|
-
* @noInheritDoc
|
|
1683
|
-
* @category Definitions
|
|
1684
|
-
*/
|
|
1685
|
-
export interface BoxedSymbolDefinition extends BoxedBaseDefinition, SymbolAttributes, Partial<NumericFlags> {
|
|
1686
|
-
get value(): BoxedExpression | undefined;
|
|
1687
|
-
set value(val: SemiBoxedExpression | number | undefined);
|
|
1688
|
-
domain: BoxedDomain | undefined;
|
|
1689
|
-
inferredDomain: boolean;
|
|
1690
|
-
}
|
|
1691
|
-
/** @category Assumptions */
|
|
1692
|
-
export type AssumeResult = 'internal-error' | 'not-a-predicate' | 'contradiction' | 'tautology' | 'ok';
|
|
1693
|
-
/** @category Compiling */
|
|
1694
|
-
export type CompiledExpression = {
|
|
1695
|
-
evaluate?: (scope: {
|
|
1696
|
-
[symbol: string]: BoxedExpression;
|
|
1697
|
-
}) => number | BoxedExpression;
|
|
1698
|
-
};
|
|
1699
|
-
/** @internal */
|
|
1700
|
-
export interface ComputeEngineStats {
|
|
1701
|
-
symbols: Set<BoxedExpression>;
|
|
1702
|
-
expressions: null | Set<BoxedExpression>;
|
|
1703
|
-
highwaterMark: number;
|
|
1704
|
-
}
|
|
1705
|
-
/** @category Compute Engine */
|
|
1706
|
-
export type AssignValue = boolean | number | string | Decimal | Complex | LatexString | SemiBoxedExpression | ((ce: IComputeEngine, args: any) => BoxedExpression) | undefined;
|
|
1707
|
-
/** @category Compute Engine */
|
|
1708
|
-
export type ArrayValue = boolean | number | string | Decimal | Complex | BoxedExpression | undefined;
|
|
1709
|
-
/** @internal */
|
|
1710
|
-
export interface IComputeEngine {
|
|
1711
|
-
latexDictionary: readonly LatexDictionaryEntry[];
|
|
1712
|
-
readonly Anything: BoxedDomain;
|
|
1713
|
-
readonly Void: BoxedDomain;
|
|
1714
|
-
readonly Strings: BoxedDomain;
|
|
1715
|
-
readonly Booleans: BoxedDomain;
|
|
1716
|
-
readonly Numbers: BoxedDomain;
|
|
1717
|
-
readonly True: BoxedExpression;
|
|
1718
|
-
readonly False: BoxedExpression;
|
|
1719
|
-
readonly Pi: BoxedExpression;
|
|
1720
|
-
readonly E: BoxedExpression;
|
|
1721
|
-
readonly Nothing: BoxedExpression;
|
|
1722
|
-
readonly Zero: BoxedExpression;
|
|
1723
|
-
readonly One: BoxedExpression;
|
|
1724
|
-
readonly Half: BoxedExpression;
|
|
1725
|
-
readonly NegativeOne: BoxedExpression;
|
|
1726
|
-
readonly I: BoxedExpression;
|
|
1727
|
-
readonly NaN: BoxedExpression;
|
|
1728
|
-
readonly PositiveInfinity: BoxedExpression;
|
|
1729
|
-
readonly NegativeInfinity: BoxedExpression;
|
|
1730
|
-
readonly ComplexInfinity: BoxedExpression;
|
|
1731
|
-
/** @internal */
|
|
1732
|
-
readonly _BIGNUM_NAN: Decimal;
|
|
1733
|
-
/** @internal */
|
|
1734
|
-
readonly _BIGNUM_ZERO: Decimal;
|
|
1735
|
-
/** @internal */
|
|
1736
|
-
readonly _BIGNUM_ONE: Decimal;
|
|
1737
|
-
/** @internal */
|
|
1738
|
-
readonly _BIGNUM_TWO: Decimal;
|
|
1739
|
-
/** @internal */
|
|
1740
|
-
readonly _BIGNUM_HALF: Decimal;
|
|
1741
|
-
/** @internal */
|
|
1742
|
-
readonly _BIGNUM_PI: Decimal;
|
|
1743
|
-
/** @internal */
|
|
1744
|
-
readonly _BIGNUM_NEGATIVE_ONE: Decimal;
|
|
1745
|
-
/** The current scope */
|
|
1746
|
-
context: RuntimeScope | null;
|
|
1747
|
-
/** Absolute time beyond which evaluation should not proceed
|
|
1748
|
-
* @internal
|
|
1749
|
-
*/
|
|
1750
|
-
deadline?: number;
|
|
1751
|
-
/** @hidden */
|
|
1752
|
-
readonly timeLimit: number;
|
|
1753
|
-
/** @hidden */
|
|
1754
|
-
readonly iterationLimit: number;
|
|
1755
|
-
/** @hidden */
|
|
1756
|
-
readonly recursionLimit: number;
|
|
1757
|
-
numericMode: NumericMode;
|
|
1758
|
-
tolerance: number;
|
|
1759
|
-
chop(n: number): number;
|
|
1760
|
-
chop(n: Decimal): Decimal | 0;
|
|
1761
|
-
chop(n: Complex): Complex | 0;
|
|
1762
|
-
chop(n: number | Decimal | Complex): number | Decimal | Complex;
|
|
1763
|
-
bignum: (a: Decimal.Value | bigint) => Decimal;
|
|
1764
|
-
isBignum(a: unknown): a is Decimal;
|
|
1765
|
-
complex: (a: number | Complex, b?: number) => Complex;
|
|
1766
|
-
isComplex(a: unknown): a is Complex;
|
|
1767
|
-
set precision(p: number | 'machine');
|
|
1768
|
-
get precision(): number;
|
|
1769
|
-
costFunction: (expr: BoxedExpression) => number;
|
|
1770
|
-
strict: boolean;
|
|
1771
|
-
canonical(xs: SemiBoxedExpression[]): BoxedExpression[];
|
|
1772
|
-
box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression, options?: {
|
|
1773
|
-
canonical?: boolean | CanonicalForm | CanonicalForm[];
|
|
1774
|
-
}): BoxedExpression;
|
|
1775
|
-
function(head: string | BoxedExpression, ops: SemiBoxedExpression[], options?: {
|
|
1776
|
-
metadata?: Metadata;
|
|
1777
|
-
canonical?: boolean;
|
|
1778
|
-
}): BoxedExpression;
|
|
1779
|
-
number(value: number | bigint | string | MathJsonNumber | Decimal | Complex | Rational, options?: {
|
|
1780
|
-
metadata?: Metadata;
|
|
1781
|
-
canonical?: boolean;
|
|
1782
|
-
}): BoxedExpression;
|
|
1783
|
-
symbol(sym: string, options?: {
|
|
1784
|
-
metadata?: Metadata;
|
|
1785
|
-
canonical?: boolean;
|
|
1786
|
-
}): BoxedExpression;
|
|
1787
|
-
string(s: string, metadata?: Metadata): BoxedExpression;
|
|
1788
|
-
domain(domain: BoxedDomain | DomainExpression, metadata?: Metadata): BoxedDomain;
|
|
1789
|
-
error(message: string | [string, ...SemiBoxedExpression[]], where?: SemiBoxedExpression): BoxedExpression;
|
|
1790
|
-
domainError(expectedDomain: BoxedDomain | DomainLiteral, actualDomain: undefined | BoxedDomain, where?: SemiBoxedExpression): BoxedExpression;
|
|
1791
|
-
hold(expr: SemiBoxedExpression): BoxedExpression;
|
|
1792
|
-
add(...ops: BoxedExpression[]): BoxedExpression;
|
|
1793
|
-
mul(...ops: BoxedExpression[]): BoxedExpression;
|
|
1794
|
-
pow(base: BoxedExpression, exponent: number | Rational | BoxedExpression): BoxedExpression;
|
|
1795
|
-
sqrt(base: BoxedExpression): BoxedExpression;
|
|
1796
|
-
inv(expr: BoxedExpression): BoxedExpression;
|
|
1797
|
-
neg(expr: BoxedExpression): BoxedExpression;
|
|
1798
|
-
div(num: BoxedExpression, denom: BoxedExpression): BoxedExpression;
|
|
1799
|
-
pair(first: BoxedExpression, second: BoxedExpression, metadata?: Metadata): BoxedExpression;
|
|
1800
|
-
tuple(elements: number[], metadata?: Metadata): BoxedExpression;
|
|
1801
|
-
tuple(elements: BoxedExpression[], metadata?: Metadata): BoxedExpression;
|
|
1802
|
-
array(elements: ArrayValue[] | ArrayValue[][], metadata?: Metadata): BoxedExpression;
|
|
1803
|
-
rules(rules: Rule[]): BoxedRuleSet;
|
|
1804
|
-
/**
|
|
1805
|
-
* This is a primitive to create a boxed function.
|
|
1806
|
-
*
|
|
1807
|
-
* In general, consider using `ce.box()` or `canonicalXXX()` instead.
|
|
1808
|
-
*
|
|
1809
|
-
* The caller must ensure that the arguments are in canonical form:
|
|
1810
|
-
* - arguments are `canonical()`
|
|
1811
|
-
* - arguments are sorted
|
|
1812
|
-
* - arguments are flattened and desequenced
|
|
1813
|
-
*
|
|
1814
|
-
* @internal
|
|
1815
|
-
*/
|
|
1816
|
-
_fn(head: string | BoxedExpression, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
|
|
1817
|
-
parse(s: LatexString | string, options?: {
|
|
1818
|
-
canonical?: boolean | CanonicalForm | CanonicalForm[];
|
|
1819
|
-
}): BoxedExpression;
|
|
1820
|
-
parse(s: null, options?: {
|
|
1821
|
-
canonical?: boolean | CanonicalForm | CanonicalForm[];
|
|
1822
|
-
}): null;
|
|
1823
|
-
parse(s: LatexString | string | null, options?: {
|
|
1824
|
-
canonical?: boolean | CanonicalForm | CanonicalForm[];
|
|
1825
|
-
}): null | BoxedExpression;
|
|
1826
|
-
serialize(expr: SemiBoxedExpression, options?: {
|
|
1827
|
-
canonical?: boolean;
|
|
1828
|
-
}): LatexString;
|
|
1829
|
-
get latexOptions(): NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions;
|
|
1830
|
-
set latexOptions(opts: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions> & Partial<SerializeLatexOptions>);
|
|
1831
|
-
get jsonSerializationOptions(): Readonly<JsonSerializationOptions>;
|
|
1832
|
-
set jsonSerializationOptions(val: Partial<JsonSerializationOptions>);
|
|
1833
|
-
pushScope(scope?: Partial<Scope>): IComputeEngine;
|
|
1834
|
-
popScope(): IComputeEngine;
|
|
1835
|
-
swapScope(scope: RuntimeScope | null): RuntimeScope | null;
|
|
1836
|
-
resetContext(): void;
|
|
1837
|
-
defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
|
|
1838
|
-
lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
|
|
1839
|
-
defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
|
|
1840
|
-
lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
|
|
1841
|
-
assign(ids: {
|
|
1842
|
-
[id: string]: AssignValue;
|
|
1843
|
-
}): IComputeEngine;
|
|
1844
|
-
assign(id: string, value: AssignValue): IComputeEngine;
|
|
1845
|
-
assign(arg1: string | {
|
|
1846
|
-
[id: string]: AssignValue;
|
|
1847
|
-
}, arg2?: AssignValue): IComputeEngine;
|
|
1848
|
-
declare(identifiers: {
|
|
1849
|
-
[id: string]: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition;
|
|
1850
|
-
}): IComputeEngine;
|
|
1851
|
-
declare(id: string, def: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition): IComputeEngine;
|
|
1852
|
-
declare(arg1: string | {
|
|
1853
|
-
[id: string]: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition;
|
|
1854
|
-
}, arg2?: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition): IComputeEngine;
|
|
1855
|
-
assume(predicate: SemiBoxedExpression): AssumeResult;
|
|
1856
|
-
forget(symbol?: string | string[]): void;
|
|
1857
|
-
get assumptions(): ExpressionMapInterface<boolean>;
|
|
1858
|
-
ask(pattern: SemiBoxedExpression): BoxedSubstitution[];
|
|
1859
|
-
verify(query: SemiBoxedExpression): boolean;
|
|
1860
|
-
/** @internal */
|
|
1861
|
-
shouldContinueExecution(): boolean;
|
|
1862
|
-
/** @internal */
|
|
1863
|
-
checkContinueExecution(): void;
|
|
1864
|
-
/** @internal */
|
|
1865
|
-
cache<T>(name: string, build: () => T, purge?: (T: any) => T | undefined): T;
|
|
1866
|
-
/** @internal */
|
|
1867
|
-
readonly stats: ComputeEngineStats;
|
|
1868
|
-
/** @internal */
|
|
1869
|
-
reset(): void;
|
|
1870
|
-
/** @internal */
|
|
1871
|
-
_register(expr: BoxedExpression): void;
|
|
1872
|
-
/** @internal */
|
|
1873
|
-
_unregister(expr: BoxedExpression): void;
|
|
1874
|
-
}
|