@cortex-js/compute-engine 0.24.1 → 0.25.1

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 (112) hide show
  1. package/dist/compute-engine.esm.js +24046 -25451
  2. package/dist/compute-engine.js +23987 -25392
  3. package/dist/compute-engine.min.esm.js +13 -13
  4. package/dist/compute-engine.min.js +13 -13
  5. package/dist/math-json.esm.js +43 -77
  6. package/dist/math-json.js +43 -77
  7. package/dist/math-json.min.esm.js +43 -77
  8. package/dist/math-json.min.js +2 -2
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  11. package/dist/types/common/signals.d.ts +1 -1
  12. package/dist/types/common/utils.d.ts +1 -1
  13. package/dist/types/compute-engine/assume.d.ts +1 -1
  14. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +31 -19
  15. package/dist/types/compute-engine/boxed-expression/box.d.ts +9 -9
  16. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +4 -4
  17. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -2
  18. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +2 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +9 -10
  20. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +5 -4
  21. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +2 -15
  22. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -4
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +7 -8
  25. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +6 -7
  26. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -2
  27. package/dist/types/compute-engine/boxed-expression/coefficient-field.d.ts +56 -0
  28. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/factor.d.ts +11 -0
  30. package/dist/types/compute-engine/boxed-expression/match.d.ts +14 -0
  31. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +41 -0
  32. package/dist/types/compute-engine/boxed-expression/order.d.ts +6 -6
  33. package/dist/types/compute-engine/boxed-expression/public.d.ts +1983 -0
  34. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +3 -15
  35. package/dist/types/compute-engine/boxed-expression/utils.d.ts +5 -2
  36. package/dist/types/compute-engine/boxed-expression/validate.d.ts +7 -6
  37. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  38. package/dist/types/compute-engine/compile.d.ts +2 -2
  39. package/dist/types/compute-engine/compute-engine.d.ts +86 -62
  40. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  41. package/dist/types/compute-engine/domain-utils.d.ts +3 -2
  42. package/dist/types/compute-engine/function-utils.d.ts +2 -2
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  51. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -3
  52. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  53. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  54. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  55. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  56. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +5 -4
  57. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  58. package/dist/types/compute-engine/latex-syntax/parse.d.ts +71 -29
  59. package/dist/types/compute-engine/latex-syntax/public.d.ts +264 -252
  60. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +9 -3
  61. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  62. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +6 -11
  63. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +3 -2
  64. package/dist/types/compute-engine/library/arithmetic-add.d.ts +5 -5
  65. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +2 -2
  66. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +5 -5
  67. package/dist/types/compute-engine/library/arithmetic-power.d.ts +2 -1
  68. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  69. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  70. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  71. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  72. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  73. package/dist/types/compute-engine/library/core.d.ts +3 -2
  74. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  75. package/dist/types/compute-engine/library/library.d.ts +1 -1
  76. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  77. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  78. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  79. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  80. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -2
  81. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  82. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  83. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  84. package/dist/types/compute-engine/library/utils.d.ts +2 -2
  85. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +7 -1
  86. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  87. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  88. package/dist/types/compute-engine/numerics/numeric.d.ts +7 -17
  89. package/dist/types/compute-engine/numerics/primes.d.ts +6 -1
  90. package/dist/types/compute-engine/numerics/rationals.d.ts +4 -13
  91. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  92. package/dist/types/compute-engine/numerics/terms.d.ts +7 -4
  93. package/dist/types/compute-engine/public.d.ts +3 -1841
  94. package/dist/types/compute-engine/rules.d.ts +1 -1
  95. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  96. package/dist/types/compute-engine/solve.d.ts +4 -3
  97. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  98. package/dist/types/compute-engine/symbolic/expand.d.ts +2 -2
  99. package/dist/types/compute-engine/symbolic/flatten.d.ts +6 -4
  100. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  101. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  102. package/dist/types/compute-engine/symbolic/product.d.ts +4 -3
  103. package/dist/types/compute-engine/symbolic/tensor-fields.d.ts +1 -1
  104. package/dist/types/compute-engine/symbolic/tensors.d.ts +1 -1
  105. package/dist/types/compute-engine/symbolic/utils.d.ts +5 -10
  106. package/dist/types/compute-engine.d.ts +2 -2
  107. package/dist/types/math-json/math-json-format.d.ts +3 -3
  108. package/dist/types/math-json/utils.d.ts +4 -6
  109. package/dist/types/math-json.d.ts +2 -2
  110. package/package.json +10 -10
  111. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +0 -28
  112. package/dist/types/compute-engine/numerics/factor.d.ts +0 -27
@@ -1,4 +1,4 @@
1
- /* 0.24.1 */
1
+ /* 0.25.1 */
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,1089 +12,8 @@
12
12
  * @module "compute-engine"
13
13
  *
14
14
  */
15
- import type { Complex } from 'complex.js';
16
- import type { Decimal } from 'decimal.js';
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
- *
556
- * if the expression matches the `match` pattern, replace it with
557
- * the `replace` pattern.
558
- *
559
- * If no rules apply, return `null`.
560
- *
561
- * See also `subs` for a simple substitution.
562
- *
563
- *
564
- * **Note** applicable to canonical and non-canonical expressions. If the
565
- * expression is non-canonical, the result is also non-canonical.
566
- *
567
- */
568
- replace(rules: BoxedRuleSet | Rule | Rule[], options?: ReplaceOptions): null | BoxedExpression;
569
- /**
570
- * True if the expression includes a symbol `v` or a function head `v`.
571
- *
572
- * **Note** applicable to canonical and non-canonical expressions.
573
- */
574
- has(v: string | string[]): boolean;
575
- /** Structural/symbolic equality (weak equality).
576
- *
577
- * `ce.parse('1+x').isSame(ce.parse('x+1'))` is `false`
578
- *
579
- * **Note** applicable to canonical and non-canonical expressions.
580
- *
581
- * @category Relational Operator
582
- */
583
- isSame(rhs: BoxedExpression): boolean;
584
- /**
585
- * If this expression matches `pattern`, return a substitution that makes
586
- * `pattern` equal to `this`. Otherwise return `null`.
587
- *
588
- * If `pattern` includes wildcards (identifiers that starts
589
- * with `_`), the substitution will include a prop for each matching named
590
- * wildcard.
591
- *
592
- * If this expression matches `pattern` but there are no named wildcards,
593
- * return the empty substitution, `{}`.
594
- *
595
- * **Note** applicable to canonical and non-canonical expressions.
596
- *
597
- */
598
- match(pattern: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression | BoxedExpression, options?: PatternMatchOptions): BoxedSubstitution | null;
599
- /**
600
- * "Not a Number".
601
- *
602
- * A value representing undefined result of computations, such as `0/0`,
603
- * as per the floating point format standard IEEE-754.
604
- *
605
- * Note that if `isNaN` is true, `isNumber` is also true (yes, `NaN` is a
606
- * number).
607
- *
608
- * @category Expression Properties
609
- *
610
- */
611
- readonly isNaN: boolean | undefined;
612
- /**
613
- * The numeric value of this expression is 0.
614
- *
615
- * @category Expression Properties
616
- */
617
- readonly isZero: boolean | undefined;
618
- /**
619
- * The numeric value of this expression is not 0.
620
- * @category Expression Properties
621
- */
622
- readonly isNotZero: boolean | undefined;
623
- /**
624
- * The numeric value of this expression is not 1.
625
- * @category Expression Properties
626
- */
627
- readonly isOne: boolean | undefined;
628
- /**
629
- * The numeric value of this expression is not -1.
630
- * @category Expression Properties
631
- */
632
- readonly isNegativeOne: boolean | undefined;
633
- /** The numeric value of this expression is ±Infinity or Complex Infinity
634
- *
635
- * @category Expression Properties
636
- */
637
- readonly isInfinity: boolean | undefined;
638
- /** This expression is a number, but not ±Infinity and not `NaN`
639
- *
640
- * @category Expression Properties
641
- */
642
- readonly isFinite: boolean | undefined;
643
- /**
644
- * @category Expression Properties
645
- */
646
- readonly isEven: boolean | undefined;
647
- /**
648
- * @category Expression Properties
649
- */
650
- readonly isOdd: boolean | undefined;
651
- /**
652
- * @category Expression Properties
653
- */
654
- readonly isPrime: boolean | undefined;
655
- /**
656
- * @category Expression Properties
657
- */
658
- readonly isComposite: boolean | undefined;
659
- /**
660
- * Return the value of this expression, if a number literal.
661
- *
662
- * Note it is possible for `numericValue` to be `null`, and for `isNotZero`
663
- * to be true. For example, when a symbol has been defined with an assumption.
664
- *
665
- * Conversely, `isNumber` may be true even if `numericValue` is `null`,
666
- * example the symbol `Pi` return true for `isNumber` but `numericValue` is
667
- * `null`. It's value can be accessed with `.value.numericValue`
668
- *
669
- * @category Numeric Expression
670
- *
671
- */
672
- readonly numericValue: number | Decimal | Complex | Rational | null;
673
- /** The shape describes the axis of the expression.
674
- * When the expression is a scalar (number), the shape is `[]`.
675
- * When the expression is a vector, the shape is `[n]`.
676
- * When the expression is a matrix, the shape is `[n, m]`.
677
- */
678
- readonly shape: number[];
679
- /** 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` */
680
- readonly rank: number;
681
- /**
682
- * Return the following, depending on the value of this expression:
683
- *
684
- * * `-1` if it is < 0
685
- * * `0` if it is = 0
686
- * * `+1` if it is > 0
687
- * * `undefined` this value may be positive, negative or zero. We don't know
688
- * right now (a symbol with an Integer domain, but no currently assigned
689
- * value, for example)
690
- * * `null` this value will never be positive, negative or zero (`NaN`,
691
- * a string or a complex number for example)
692
- *
693
- * Note that complex numbers have no natural ordering,
694
- * so if the value is a complex number, `sgn` is either 0, or `null`
695
- *
696
- * If a symbol, this does take assumptions into account, that is `this.sgn`
697
- * will return `1` if `isPositive` is `true`, even if this expression has
698
- * no value
699
- *
700
- * @category Numeric Expression
701
- *
702
- */
703
- readonly sgn: -1 | 0 | 1 | undefined | null;
704
- /** If the expressions cannot be compared, return `undefined`
705
- *
706
- * The numeric value of both expressions are compared.
707
- *
708
- * @category Relational Operator
709
- */
710
- isLess(rhs: BoxedExpression): boolean | undefined;
711
- /**
712
- * The numeric value of both expressions are compared.
713
- * @category Relational Operator
714
- */
715
- isLessEqual(rhs: BoxedExpression): boolean | undefined;
716
- /**
717
- * The numeric value of both expressions are compared.
718
- * @category Relational Operator
719
- */
720
- isGreater(rhs: BoxedExpression): boolean | undefined;
721
- /**
722
- * The numeric value of both expressions are compared.
723
- * @category Relational Operator
724
- */
725
- isGreaterEqual(rhs: BoxedExpression): boolean | undefined;
726
- /** The numeric value of this expression is > 0, same as `isGreater(0)`
727
- *
728
- * @category Expression Properties
729
- */
730
- readonly isPositive: boolean | undefined;
731
- /** The numeric value of this expression is >= 0, same as `isGreaterEqual(0)`
732
- *
733
- * @category Expression Properties
734
- */
735
- readonly isNonNegative: boolean | undefined;
736
- /** The numeric value of this expression is < 0, same as `isLess(0)`
737
- *
738
- * @category Expression Properties
739
- */
740
- readonly isNegative: boolean | undefined;
741
- /** The numeric value of this expression is &lt;= 0, same as `isLessEqual(0)`
742
- *
743
- * @category Expression Properties
744
- */
745
- readonly isNonPositive: boolean | undefined;
746
- /** The keys of the dictionary.
747
- *
748
- * If this expression not a dictionary, return `null`
749
- *
750
- * @category Dictionary Expression
751
- *
752
- */
753
- readonly keys: IterableIterator<string> | null;
754
- /**
755
- *
756
- * @category Dictionary Expression
757
- */
758
- readonly keysCount: number;
759
- /**
760
- * If this expression is a dictionary, return the value of the `key` entry.
761
- *
762
- * @category Dictionary Expression
763
- *
764
- */
765
- getKey(key: string): BoxedExpression | undefined;
766
- /**
767
- * If this expression is a dictionary, return true if the
768
- * dictionary has a `key` entry.
769
- *
770
- * @category Dictionary Expression
771
- *
772
- */
773
- hasKey(key: string): boolean;
774
- /** Wikidata identifier.
775
- *
776
- * **Note** `undefined` if not a canonical expression.
777
- */
778
- readonly wikidata: string | undefined;
779
- /** An optional short description if a symbol or function expression.
780
- *
781
- * May include markdown. Each string is a paragraph.
782
- *
783
- * **Note** `undefined` if not a canonical expression.
784
- *
785
- */
786
- readonly description: undefined | string[];
787
- /** An optional URL pointing to more information about the symbol or
788
- * function head.
789
- *
790
- * **Note** `undefined` if not a canonical expression.
791
- *
792
- */
793
- readonly url: string | undefined;
794
- /** Expressions with a higher complexity score are sorted
795
- * first in commutative functions
796
- *
797
- * **Note** `undefined` if not a canonical expression.
798
- */
799
- readonly complexity: number | undefined;
800
- /**
801
- * For symbols and functions, a possible definition associated with the
802
- * expression. `baseDefinition` is the base class of symbol and function
803
- * definition.
804
- *
805
- * **Note** `undefined` if not a canonical expression.
806
- *
807
- */
808
- readonly baseDefinition: BoxedBaseDefinition | undefined;
809
- /**
810
- * For functions, a possible definition associated with the expression.
811
- *
812
- * **Note** `undefined` if not a canonical expression or not a function.
813
- *
814
- */
815
- readonly functionDefinition: BoxedFunctionDefinition | undefined;
816
- /**
817
- * For symbols, a possible definition associated with the expression.
818
- *
819
- * **Note** `undefined` if not a symbol
820
- *
821
- */
822
- readonly symbolDefinition: BoxedSymbolDefinition | undefined;
823
- /**
824
- *
825
- * Infer the domain of this expression.
826
- *
827
- * If the domain of this expression is already known, return `false`.
828
- *
829
- * If the domain was not set, set it to the inferred domain, return `true`
830
- * If the domain was previously inferred, adjust it by widening it,
831
- * return `true`
832
- *
833
- * @internal
834
- */
835
- infer(domain: BoxedDomain): boolean;
836
- /**
837
- * Update the definition associated with this expression, using the
838
- * current scope (`ce.context`).
839
- *
840
- * @internal
841
- */
842
- bind(): void;
843
- /**
844
- *
845
- * Reset the cached value associated with this expression.
846
- *
847
- * Use when the environment has changed, for example the numeric mode
848
- * or precision, to force the expression to be re-evaluated.
849
- *
850
- * @internal
851
- */
852
- reset(): void;
853
- /**
854
- * Return a simpler form of the canonical form of this expression.
855
- *
856
- * A series of rewriting rules are applied repeatedly, until no more rules
857
- * apply.
858
- *
859
- * If a custom `simplify` handler is associated with this function
860
- * definition, it is invoked.
861
- *
862
- * The values assigned to symbols and the assumptions about symbols may be
863
- * used, for example `arg.isInteger` or `arg.isPositive`.
864
- *
865
- * No calculations involving decimal numbers (numbers that are not
866
- * integers) are performed but exact calculations may be performed,
867
- * for example:
868
- *
869
- * \\( \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} \\).
870
- *
871
- * The result is in canonical form.
872
- *
873
- */
874
- simplify(options?: SimplifyOptions): BoxedExpression;
875
- /**
876
- * Return the value of the canonical form of this expression.
877
- *
878
- * A pure expression always return the same value and has no side effects.
879
- * If `expr.isPure` is `true`, `expr.value` and `expr.evaluate()` are
880
- * synonyms.
881
- *
882
- * For an impure expression, `expr.value` is undefined.
883
- *
884
- * Evaluating an impure expression may have some side effects, for
885
- * example modifying the `ComputeEngine` environment, such as its set of
886
- * assumptions.
887
- *
888
- * Only exact calculations are performed, no approximate calculations on
889
- * decimal numbers (non-integer numbers). Constants, rational numbers and
890
- * square root of rational numbers are preserved.
891
- *
892
- * To perform approximate calculations, use `expr.N()` instead.
893
- *
894
- * The result of `expr.evaluate()` may be the same as `expr.simplify()`.
895
- *
896
- * The result is in canonical form.
897
- *
898
- */
899
- evaluate(options?: EvaluateOptions): BoxedExpression;
900
- /** Return a numeric approximation of the canonical form of this expression.
901
- *
902
- * Any necessary calculations, including on decimal numbers (non-integers),
903
- * are performed.
904
- *
905
- * The calculations are performed according to the `numericMode` and
906
- * `precision` properties of the `ComputeEngine`.
907
- *
908
- * To only perform exact calculations, use `this.evaluate()` instead.
909
- *
910
- * If the function is not numeric, the result of `this.N()` is the same as
911
- * `this.evaluate()`.
912
- *
913
- * The result is in canonical form.
914
- */
915
- N(options?: NOptions): BoxedExpression;
916
- compile(to?: 'javascript', options?: {
917
- optimize: ('simplify' | 'evaluate')[];
918
- }): ((args: Record<string, any>) => any | undefined) | undefined;
919
- solve(vars: Iterable<string>): null | BoxedExpression[];
920
- /**
921
- * Return a JavaScript primitive representing the value of this expression.
922
- *
923
- * Equivalent to `expr.N().valueOf()`.
924
- *
925
- */
926
- get value(): number | boolean | string | number[] | undefined;
927
- /**
928
- * Only the value of variables can be changed (symbols that are not
929
- * constants).
930
- *
931
- * Throws a runtime error if a constant.
932
- *
933
- * **Note**: If non-canonical, does nothing.
934
- *
935
- */
936
- set value(value: boolean | string | Decimal | Complex | {
937
- re: number;
938
- im: number;
939
- } | {
940
- num: number;
941
- denom: number;
942
- } | number[] | BoxedExpression | number | undefined);
943
- /**
944
- *
945
- * The domain of the value of this expression.
946
- *
947
- * If a function expression, the domain of the value of the function
948
- * (the codomain of the function).
949
- *
950
- * If a symbol the domain of the value of the symbol.
951
- *
952
- * Use `expr.head` to determine if an expression is a symbol or function
953
- * expression.
954
- *
955
- * **Note**: if non-canonical or not valid, return `undefined`.
956
- *
957
- */
958
- get domain(): BoxedDomain | undefined;
959
- /** Modify the domain of a symbol.
960
- *
961
- * **Note**: If non-canonical, does nothing.
962
- *
963
- */
964
- set domain(domain: DomainExpression | BoxedDomain | undefined);
965
- /** `true` if the value of this expression is a number.
966
- *
967
- * `isExtendedComplex || isNaN` = `isReal || isImaginary || isInfinity || isNaN`
968
- *
969
- * Note that in a fateful twist of cosmic irony, `NaN` ("Not a Number")
970
- * **is** a number.
971
- *
972
- * @category Domain Properties
973
- */
974
- readonly isNumber: boolean | undefined;
975
- /** The value of this expression is an element of the set ℤ: ...,-2, -1, 0, 1, 2...
976
- *
977
- *
978
- * @category Domain Properties
979
- *
980
- */
981
- readonly isInteger: boolean | undefined;
982
- /** The value of this expression is an element of the set ℚ, p/q with p ∈ ℕ, q ∈ ℤ ⃰ q >= 1
983
- *
984
- * Note that every integer is also a rational.
985
- *
986
- *
987
- * @category Domain Properties
988
- *
989
- */
990
- readonly isRational: boolean | undefined;
991
- /**
992
- * The value of this expression is a number that is the root of a non-zero
993
- * univariate polynomial with rational coefficients.
994
- *
995
- * All integers and rational numbers are algebraic.
996
- *
997
- * Transcendental numbers, such as \\( \pi \\) or \\( e \\) are not algebraic.
998
- *
999
- *
1000
- * @category Domain Properties
1001
- *
1002
- */
1003
- readonly isAlgebraic: boolean | undefined;
1004
- /**
1005
- * The value of this expression is real number: finite and not imaginary.
1006
- *
1007
- * `isFinite && !isImaginary`
1008
- *
1009
- *
1010
- * @category Domain Properties
1011
- */
1012
- readonly isReal: boolean | undefined;
1013
- /** Real or ±Infinity
1014
- *
1015
- * `isReal || isInfinity`
1016
- *
1017
- *
1018
- * @category Domain Properties
1019
- */
1020
- readonly isExtendedReal: boolean | undefined;
1021
- /**
1022
- * The value of this expression is a number, but not `NaN` or any Infinity
1023
- *
1024
- * `isReal || isImaginary`
1025
- *
1026
- *
1027
- * @category Domain Properties
1028
- *
1029
- */
1030
- readonly isComplex: boolean | undefined;
1031
- /** `isReal || isImaginary || isInfinity`
1032
- *
1033
- *
1034
- * @category Domain Properties
1035
- */
1036
- readonly isExtendedComplex: boolean | undefined;
1037
- /** The value of this expression is a number with a imaginary part
1038
- *
1039
- *
1040
- * @category Domain Properties
1041
- */
1042
- readonly isImaginary: boolean | undefined;
1043
- /** Mathematical equality (strong equality), that is the value
1044
- * of this expression and of `rhs` are numerically equal.
1045
- *
1046
- * The numeric value of both expressions are compared.
1047
- *
1048
- * Numbers whose difference is less than `engine.tolerance` are
1049
- * considered equal. This tolerance is set when the `engine.precision` is
1050
- * changed to be such that the last two digits are ignored.
1051
- *
1052
- * @category Relational Operator
1053
- */
1054
- isEqual(rhs: BoxedExpression): boolean;
1055
- }
1056
- /** A semi boxed expression is a MathJSON expression which can include some
1057
- * boxed terms.
1058
- *
1059
- * This is convenient when creating new expressions from portions
1060
- * of an existing `BoxedExpression` while avoiding unboxing and reboxing.
1061
- *
1062
- * @category Boxed Expression
1063
- */
1064
- export type SemiBoxedExpression = number | string | Decimal | Complex | MathJsonNumber | MathJsonString | MathJsonSymbol | MathJsonFunction | MathJsonDictionary | SemiBoxedExpression[] | BoxedExpression;
1065
- /**
1066
- * Control how a pattern is matched to an expression.
1067
- *
1068
- * - `substitution`: if present, assumes these values for the named wildcards, and ensure that subsequent occurence of the same wildcard have the same value.
1069
- * - `recursive`: if true, match recursively, otherwise match only the top level.
1070
- * - `numericTolerance`: if present, the tolerance for numeric comparison.
1071
- * - `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.
1072
- *
1073
- * @category Pattern Matching
1074
- *
1075
- */
1076
- export type PatternMatchOptions = {
1077
- substitution?: BoxedSubstitution;
1078
- recursive?: boolean;
1079
- numericTolerance?: number;
1080
- exact?: boolean;
1081
- };
1082
- /**
1083
- * @noInheritDoc
1084
- *
1085
- * @category Pattern Matching
1086
- */
1087
- export type Pattern = BoxedExpression;
1088
- /** @category Assumptions */
1089
- export interface ExpressionMapInterface<U> {
1090
- has(expr: BoxedExpression): boolean;
1091
- get(expr: BoxedExpression): U | undefined;
1092
- set(expr: BoxedExpression, value: U): void;
1093
- delete(expr: BoxedExpression): void;
1094
- clear(): void;
1095
- [Symbol.iterator](): IterableIterator<[BoxedExpression, U]>;
1096
- entries(): IterableIterator<[BoxedExpression, U]>;
1097
- }
15
+ import { FunctionDefinition, SemiBoxedExpression, SymbolDefinition } from './boxed-expression/public';
16
+ export * from './boxed-expression/public';
1098
17
  /**
1099
18
  * A table mapping identifiers to their definition.
1100
19
  *
@@ -1117,760 +36,3 @@ export type IdentifierDefinition = SymbolDefinition | FunctionDefinition | SemiB
1117
36
  export type IdentifierDefinitions = Readonly<{
1118
37
  [id: string]: IdentifierDefinition;
1119
38
  }>;
1120
- /**
1121
- * The entries have been validated and optimized for faster evaluation.
1122
- *
1123
- * When a new scope is created with `pushScope()` or when creating a new
1124
- * engine instance, new instances of this type are created as needed.
1125
- *
1126
- * @category Definitions
1127
- */
1128
- export type RuntimeIdentifierDefinitions = Map<string, BoxedSymbolDefinition | BoxedFunctionDefinition>;
1129
- /**
1130
- * A scope is a set of names in a dictionary that are bound (defined) in
1131
- * a MathJSON expression.
1132
- *
1133
- * Scopes are arranged in a stack structure. When an expression that defined
1134
- * a new scope is evaluated, the new scope is added to the scope stack.
1135
- * Outside of the expression, the scope is removed from the scope stack.
1136
- *
1137
- * The scope stack is used to resolve symbols, and it is possible for
1138
- * a scope to 'mask' definitions from previous scopes.
1139
- *
1140
- * Scopes are lexical (also called a static scope): they are defined based on
1141
- * where they are in an expression, they are not determined at runtime.
1142
- *
1143
- * @category Compute Engine
1144
- */
1145
- export type Scope = {
1146
- /** Signal `timeout` when the execution time for this scope is exceeded.
1147
- *
1148
- * Time in seconds, default 2s.
1149
- *
1150
- * @experimental
1151
- */
1152
- timeLimit: number;
1153
- /** Signal `out-of-memory` when the memory usage for this scope is exceeded.
1154
- *
1155
- * Memory is in Megabytes, default: 1Mb.
1156
- *
1157
- * @experimental
1158
- */
1159
- memoryLimit: number;
1160
- /** Signal `recursion-depth-exceeded` when the recursion depth for this
1161
- * scope is exceeded.
1162
- *
1163
- * @experimental
1164
- */
1165
- recursionLimit: number;
1166
- /** Signal `iteration-limit-exceeded` when the iteration limit
1167
- * in a loop is exceeded. Default: no limits.
1168
- *
1169
- * @experimental
1170
- */
1171
- iterationLimit: number;
1172
- };
1173
- /** @category Compute Engine */
1174
- export type RuntimeScope = Scope & {
1175
- parentScope?: RuntimeScope;
1176
- ids?: RuntimeIdentifierDefinitions;
1177
- assumptions: undefined | ExpressionMapInterface<boolean>;
1178
- };
1179
- /**
1180
- * @category Definitions
1181
- *
1182
- */
1183
- export type BaseDefinition = {
1184
- /** A short (about 1 line) description. May contain Markdown. */
1185
- description?: string | string[];
1186
- /** A URL pointing to more information about this symbol or head. */
1187
- url?: string;
1188
- /**
1189
- * A short string representing an entry in a wikibase.
1190
- *
1191
- * For example `Q167` is the [wikidata entry](https://www.wikidata.org/wiki/Q167)
1192
- * for the `Pi` constant.
1193
- */
1194
- wikidata?: string;
1195
- };
1196
- /**
1197
- * @category Definitions
1198
- *
1199
- */
1200
- export interface BoxedBaseDefinition {
1201
- name: string;
1202
- wikidata?: string;
1203
- description?: string | string[];
1204
- url?: string;
1205
- /**
1206
- * The scope this definition belongs to.
1207
- *
1208
- * This field is usually undefined, but its value is set by `getDefinition()`
1209
- */
1210
- scope: RuntimeScope | undefined;
1211
- /** When the environment changes, for example the numerical precision,
1212
- * call `reset()` so that any cached values can be recalculated.
1213
- */
1214
- reset(): any;
1215
- }
1216
- /**
1217
- * A function definition can have some flags to indicate specific
1218
- * properties of the function.
1219
- * @category Definitions
1220
- */
1221
- export type FunctionDefinitionFlags = {
1222
- /** If `true`, the function is applied element by element to lists, matrices
1223
- * (`["List"]` or `["Tuple"]` expressions) and equations (relational
1224
- * operators).
1225
- *
1226
- * **Default**: `false`
1227
- */
1228
- threadable: boolean;
1229
- /** If `true`, `["f", ["f", a], b]` simplifies to `["f", a, b]`
1230
- *
1231
- * **Default**: `false`
1232
- */
1233
- associative: boolean;
1234
- /** If `true`, `["f", a, b]` equals `["f", b, a]`. The canonical
1235
- * version of the function will order the arguments.
1236
- *
1237
- * **Default**: `false`
1238
- */
1239
- commutative: boolean;
1240
- /** If `true`, when the function is univariate, `["f", ["Add", x, c]]` where `c`
1241
- * is constant, is simplified to `["Add", ["f", x], c]`.
1242
- *
1243
- * When the function is multivariate, additivity is considered only on the
1244
- * first argument: `["f", ["Add", x, c], y]` simplifies to `["Add", ["f", x, y], c]`.
1245
- *
1246
- * For example, `Log` is additive.
1247
- *
1248
- * **Default**: `false`
1249
- */
1250
- /** If `true`, when the function is univariate, `["f", ["Multiply", x, y]]`
1251
- * simplifies to `["Multiply", ["f", x], ["f", y]]`.
1252
- *
1253
- * When the function is multivariate, multiplicativity is considered only on the
1254
- * first argument: `["f", ["Multiply", x, y], z]` simplifies to
1255
- * `["Multiply", ["f", x, z], ["f", y, z]]`
1256
- *
1257
- * **Default**: `false`
1258
- */
1259
- /** If `true`, when the function is univariate, `["f", ["Multiply", x, c]]`
1260
- * simplifies to `["Multiply", ["f", x], c]` where `c` is constant
1261
- *
1262
- * When the function is multivariate, multiplicativity is considered only on
1263
- * the first argument: `["f", ["Multiply", x, y], z]` simplifies to
1264
- * `["Multiply", ["f", x, z], ["f", y, z]]`
1265
- *
1266
- * Default: `false`
1267
- */
1268
- /** If `true`, `["f", ["f", x]]` simplifies to `["f", x]`.
1269
- *
1270
- * **Default**: `false`
1271
- */
1272
- idempotent: boolean;
1273
- /** If `true`, `["f", ["f", x]]` simplifies to `x`.
1274
- *
1275
- * **Default**: `false`
1276
- */
1277
- involution: boolean;
1278
- /** If `true`, the value of this function is always the same for a given
1279
- * set of arguments and it has no side effects.
1280
- *
1281
- * An expression using this function is pure if the function and all its
1282
- * arguments are pure.
1283
- *
1284
- * For example `Sin` is pure, `Random` isn't.
1285
- *
1286
- * This information may be used to cache the value of expressions.
1287
- *
1288
- * **Default:** `true`
1289
- */
1290
- pure: boolean;
1291
- /**
1292
- * An inert function evaluates directly to one of its argument, typically
1293
- * the first one. They may be used to provide formating hints, but do
1294
- * not affect simplification or evaluation.
1295
- *
1296
- * **Default:** false
1297
- */
1298
- inert: boolean;
1299
- /**
1300
- * All the arguments of a numeric function are numeric,
1301
- * and its value is numeric.
1302
- */
1303
- numeric: boolean;
1304
- };
1305
- /**
1306
- * @category Definitions
1307
- *
1308
- */
1309
- export type FunctionSignature = {
1310
- /** The domain of this signature, a domain compatible with the `Functions`
1311
- * domain).
1312
- *
1313
- * @deprecated Use params, optParams, restParam and result instead
1314
- */
1315
- domain?: DomainExpression;
1316
- params?: DomainExpression[];
1317
- optParams?: DomainExpression[];
1318
- restParam?: DomainExpression;
1319
- /** The domain of the result of the function. Either a domain
1320
- * expression, or a function that returns a boxed domain.
1321
- */
1322
- result?: DomainExpression | ((ce: IComputeEngine, args: BoxedDomain[]) => BoxedDomain | null | undefined);
1323
- /**
1324
- * Return the canonical form of the expression with the arguments `args`.
1325
- *
1326
- * The arguments (`args`) may not be in canonical form. If necessary, they
1327
- * can be put in canonical form.
1328
- *
1329
- * This handler should validate the domain and number of the arguments.
1330
- *
1331
- * If a required argument is missing, it should be indicated with a
1332
- * `["Error", "'missing"]` expression. If more arguments than expected
1333
- * are present, this should be indicated with an
1334
- * ["Error", "'unexpected-argument'"]` error expression
1335
- *
1336
- * If the domain of an argument is not compatible, it should be indicated
1337
- * with an `incompatible-domain` error.
1338
- *
1339
- * `["Sequence"]` expressions are not folded and need to be handled
1340
- * explicitly.
1341
- *
1342
- * If the function is associative, idempotent or an involution,
1343
- * this handler should account for it. Notably, if it is commutative, the
1344
- * arguments should be sorted in canonical order.
1345
- *
1346
- * The handler can make transformations based on the value of the arguments
1347
- * that are exact and literal (i.e.
1348
- * `arg.numericValue !== null && arg.isExact`).
1349
- *
1350
- * Values of symbols should not be substituted, unless they have
1351
- * a `holdUntil` attribute of `"never"`.
1352
- *
1353
- * The handler should not consider the value or any assumptions about any
1354
- * of the arguments that are symbols or functions (i.e. `arg.isZero`,
1355
- * `arg.isInteger`, etc...) since those may change over time.
1356
- *
1357
- * The result of the handler should be a canonical expression.
1358
- *
1359
- * If the arguments do not match, they should be replaced with an appropriate
1360
- * `["Error"]` expression. If the expression cannot be put in canonical form,
1361
- * the handler should return `null`.
1362
- *
1363
- */
1364
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1365
- /**
1366
- * Rewrite an expression into a simpler form.
1367
- *
1368
- * The arguments are in canonical form and have been simplified.
1369
- *
1370
- * The handler can use the values assigned to symbols and the assumptions
1371
- * about symbols, for example with `arg.numericValue`, `arg.isInteger` or
1372
- * `arg.isPositive`.
1373
- *
1374
- * Even though a symbol may not have a value, there may be some information
1375
- * about it reflected for example in `this.isZero` or `this.isPrime`.
1376
- *
1377
- * The handler should not perform approximate numeric calculations, such
1378
- * as calculations involving decimal numbers (non-integers). Making exact
1379
- * calculations on integers or rationals is OK.
1380
- *
1381
- * Do not reduce constants with a `holdUntil` attribute of `"N"`
1382
- * or `"evaluate"`.
1383
- *
1384
- * This handler should not have any side-effects: do not modify
1385
- * the environment of the `ComputeEngine` instance, do not perform I/O,
1386
- * do not do calculations that depend on random values.
1387
- *
1388
- * If no simplification can be performed due to the values, domains or
1389
- * assumptions about its arguments, for example, return `undefined`.
1390
- *
1391
- */
1392
- simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1393
- /**
1394
- * Evaluate a function expression.
1395
- *
1396
- * The arguments have been evaluated, except the arguments to which a
1397
- * `hold` applied.
1398
- *
1399
- * It is not necessary to further simplify or evaluate the arguments.
1400
- *
1401
- * If performing numerical calculations, if all the arguments are exact,
1402
- * return an exact expression. If any of the arguments is not exact, that is
1403
- * if it is a literal decimal (non-integer) number, return an approximation.
1404
- * In this case, the value may be the same as `expr.N()`.
1405
- *
1406
- * When doing an exact calculation:
1407
- * - do not reduce rational numbers to decimal (floating point approximation)
1408
- * - do not down convert bignums to machine numbers
1409
- * - do not reduce square roots of rational numbers
1410
- * - do not reduce constants with a `holdUntil` attribute of `"N"`
1411
- *
1412
- * If the expression cannot be evaluated, due to the values, domains, or
1413
- * assumptions about its arguments, for example, return `undefined` or
1414
- * an `["Error"]` expression.
1415
- */
1416
- evaluate?: SemiBoxedExpression | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined);
1417
- /**
1418
- * Evaluate numerically a function expression.
1419
- *
1420
- * The arguments `args` have been simplified and evaluated, numerically
1421
- * if possible, except the arguments to which a `hold` apply.
1422
- *
1423
- * The arguments may be a combination of numbers, symbolic
1424
- * expressions and other expressions.
1425
- *
1426
- * Perform as many calculations as possible, and return the result.
1427
- *
1428
- * Return `undefined` if there isn't enough information to perform
1429
- * the evaluation, for example one of the arguments is a symbol with
1430
- * no value. If the handler returns `undefined`, symbolic evaluation of
1431
- * the expression will be returned instead to the caller.
1432
- *
1433
- * Return `NaN` if there is enough information to perform the
1434
- * evaluation, but a literal argument is out of range or
1435
- * not of the expected type.
1436
- *
1437
- * Use the value of `ce.numericMode` to determine how to perform
1438
- * the numeric evaluation.
1439
- *
1440
- * Note that regardless of the current value of `ce.numericMode`, the
1441
- * arguments may be boxed numbers representing machine numbers, bignum
1442
- * numbers, complex numbers, rationals or big rationals.
1443
- *
1444
- * If the numeric mode does not allow complex numbers (the
1445
- * `engine.numericMode` is not `"complex"` or `"auto"`) and the result of
1446
- * the evaluation would be a complex number, return `NaN` instead.
1447
- *
1448
- * If `ce.numericMode` is `"bignum"` or `"auto"` the evaluation should
1449
- * be done using bignums.
1450
- *
1451
- * Otherwise, `ce.numericMode` is `"machine", the evaluation should be
1452
- * performed using machine numbers.
1453
- *
1454
- * You may perform any necessary computations, including approximate
1455
- * calculations on floating point numbers.
1456
- *
1457
- */
1458
- N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1459
- /** Dimensional analysis
1460
- * @experimental
1461
- */
1462
- evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1463
- /** Return the sign of the function expression. */
1464
- sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
1465
- /** Return a compiled (optimized) expression. */
1466
- compile?: (expr: BoxedExpression) => CompiledExpression;
1467
- };
1468
- /**
1469
- * @category Definitions
1470
- *
1471
- */
1472
- export type BoxedFunctionSignature = {
1473
- inferredSignature: boolean;
1474
- params: BoxedDomain[];
1475
- optParams: BoxedDomain[];
1476
- restParam?: BoxedDomain;
1477
- result: BoxedDomain | ((ce: IComputeEngine, args: BoxedExpression[]) => BoxedDomain | null | undefined);
1478
- canonical?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | null;
1479
- simplify?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1480
- evaluate?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1481
- N?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression | undefined;
1482
- evalDimension?: (ce: IComputeEngine, args: BoxedExpression[]) => BoxedExpression;
1483
- sgn?: (ce: IComputeEngine, args: BoxedExpression[]) => -1 | 0 | 1 | undefined;
1484
- compile?: (expr: BoxedExpression) => CompiledExpression;
1485
- };
1486
- /**
1487
- * The handlers are the primitive operations that can be performed on
1488
- * collections.
1489
- *
1490
- * There are two types of collections:
1491
- * - finite collections, such as lists, tuples, sets, matrices, etc...
1492
- * The `size()` handler of finite collections returns the number of elements
1493
- * - infinite collections, such as sequences, ranges, etc...
1494
- * The `size()` handler of infinite collections returns `Infinity`
1495
- * Infinite collections are not indexable, they have no `at()` handler.
1496
- *
1497
- * @category Definitions
1498
- */
1499
- export type CollectionHandlers = {
1500
- /** Return an iterator
1501
- * - start is optional and is a 1-based index.
1502
- * - if start is not specified, start from index 1
1503
- * - count is optional and is the number of elements to return
1504
- * - if count is not specified or negative, return all the elements from start to the endna
1505
- *
1506
- * If there is a `keys()` handler, there is no `iterator()` handler.
1507
- *
1508
- * @category Definitions
1509
- */
1510
- iterator: (expr: BoxedExpression, start?: number, count?: number) => Iterator<BoxedExpression, undefined>;
1511
- /** Return the element at the specified index.
1512
- * The first element is `at(1)`, the last element is `at(-1)`.
1513
- * If the index is &lt;0, return the element at index `size() + index + 1`.
1514
- * The index can also be a string for example for dictionaries.
1515
- * If the index is invalid, return `undefined`.
1516
- */
1517
- at: (expr: BoxedExpression, index: number | string) => undefined | BoxedExpression;
1518
- /** Return the number of elements in the collection.
1519
- * An empty collection has a size of 0.
1520
- */
1521
- size: (expr: BoxedExpression) => number;
1522
- /**
1523
- * If the collection is indexed by strings, return the valid values
1524
- * for the index.
1525
- */
1526
- keys: (expr: BoxedExpression) => undefined | Iterator<string>;
1527
- /**
1528
- * Return the index of the first element that matches the target expression.
1529
- * The comparison is done using the `target.isEqual()` method.
1530
- * If the expression is not found, return `undefined`.
1531
- * If the expression is found, return the index, 1-based.
1532
- * If the expression is found multiple times, return the index of the first
1533
- * match.
1534
- *
1535
- * From is the starting index for the search. If negative, start from the end
1536
- * and search backwards.
1537
- */
1538
- indexOf: (expr: BoxedExpression, target: BoxedExpression, from?: number) => number | string | undefined;
1539
- };
1540
- /**
1541
- * Definition record for a function.
1542
- * @category Definitions
1543
- *
1544
- */
1545
- export type FunctionDefinition = BaseDefinition & Partial<CollectionHandlers> & Partial<FunctionDefinitionFlags> & {
1546
- /**
1547
- * A number used to order arguments.
1548
- *
1549
- * Argument with higher complexity are placed after arguments with lower
1550
- * complexity when ordered canonically in commutative functions.
1551
- *
1552
- * - Additive functions: 1000-1999
1553
- * - Multiplicative functions: 2000-2999
1554
- * - Root and power functions: 3000-3999
1555
- * - Log functions: 4000-4999
1556
- * - Trigonometric functions: 5000-5999
1557
- * - Hypertrigonometric functions: 6000-6999
1558
- * - Special functions (factorial, Gamma, ...): 7000-7999
1559
- * - Collections: 8000-8999
1560
- * - Inert and styling: 9000-9999
1561
- * - Logic: 10000-10999
1562
- * - Relational: 11000-11999
1563
- *
1564
- * **Default**: 100,000
1565
- */
1566
- complexity?: number;
1567
- /**
1568
- * - `"none"` Each of the arguments is evaluated (default)
1569
- * - `"all"` None of the arguments are evaluated and they are passed as is
1570
- * - `"first"` The first argument is not evaluated, the others are
1571
- * - `"rest"` The first argument is evaluated, the others aren't
1572
- * - `"last"`: The last argument is not evaluated, the others are
1573
- * - `"most"`: All the arguments are evaluated, except the last one
1574
- *
1575
- * **Default**: `"none"`
1576
- */
1577
- hold?: Hold;
1578
- signature: FunctionSignature;
1579
- };
1580
- /**
1581
- * @category Definitions
1582
- *
1583
- */
1584
- export type BoxedFunctionDefinition = BoxedBaseDefinition & Partial<CollectionHandlers> & FunctionDefinitionFlags & {
1585
- complexity: number;
1586
- hold: Hold;
1587
- signature: BoxedFunctionSignature;
1588
- };
1589
- /**
1590
- * When used in a `SymbolDefinition`, these flags are optional.
1591
- *
1592
- * If provided, they will override the value derived from
1593
- * the symbol's value.
1594
- *
1595
- * For example, it might be useful to override `algebraic = false`
1596
- * for a transcendental number.
1597
- *
1598
- * @category Definitions
1599
- */
1600
- export type NumericFlags = {
1601
- number: boolean | undefined;
1602
- integer: boolean | undefined;
1603
- rational: boolean | undefined;
1604
- algebraic: boolean | undefined;
1605
- real: boolean | undefined;
1606
- extendedReal: boolean | undefined;
1607
- complex: boolean | undefined;
1608
- extendedComplex: boolean | undefined;
1609
- imaginary: boolean | undefined;
1610
- positive: boolean | undefined;
1611
- nonPositive: boolean | undefined;
1612
- negative: boolean | undefined;
1613
- nonNegative: boolean | undefined;
1614
- zero: boolean | undefined;
1615
- notZero: boolean | undefined;
1616
- one: boolean | undefined;
1617
- negativeOne: boolean | undefined;
1618
- infinity: boolean | undefined;
1619
- NaN: boolean | undefined;
1620
- finite: boolean | undefined;
1621
- even: boolean | undefined;
1622
- odd: boolean | undefined;
1623
- prime: boolean | undefined;
1624
- composite: boolean | undefined;
1625
- };
1626
- /**
1627
- * @category Definitions
1628
- *
1629
- */
1630
- export type SymbolAttributes = {
1631
- /**
1632
- * If `true` the value of the symbol is constant. The value or domain of
1633
- * symbols with this attribute set to `true` cannot be changed.
1634
- *
1635
- * If `false`, the symbol is a variable.
1636
- *
1637
- * **Default**: `false`
1638
- */
1639
- constant: boolean;
1640
- /**
1641
- * If the symbol has a value, it is held as indicated in the table below.
1642
- * A green checkmark indicate that the symbol is substituted.
1643
-
1644
- <div className="symbols-table">
1645
-
1646
- | Operation | `"never"` | `"simplify"` | `"evaluate"` | `"N"` |
1647
- | :--- | :----- |
1648
- | `canonical()`| (X) | | | |
1649
- | `simplify()` | (X) | (X) | | |
1650
- | `evaluate()` | (X) | (X) | (X) | |
1651
- | `"N()"` | (X) | (X) | (X) | (X) |
1652
-
1653
- </div>
1654
-
1655
- * Some examples:
1656
- * - `i` has `holdUntil: 'never'`
1657
- * - `GoldenRatio` has `holdUntil: 'simplify'` (symbolic constant)
1658
- * - `x` has `holdUntil: 'evaluate'` (variables)
1659
- * - `Pi` has `holdUntil: 'N'` (special numeric constant)
1660
- *
1661
- * **Default:** `evaluate`
1662
- */
1663
- holdUntil: 'never' | 'simplify' | 'evaluate' | 'N';
1664
- };
1665
- /**
1666
- * A bound symbol (i.e. one with an associated definition) has either a domain
1667
- * (e.g. ∀ x ∈ ℝ), a value (x = 5) or both (π: value = 3.14... domain = TranscendentalNumbers)
1668
- * @category Definitions
1669
- */
1670
- export type SymbolDefinition = BaseDefinition & Partial<SymbolAttributes> & {
1671
- domain?: DomainLiteral | BoxedDomain;
1672
- /** If true, the domain is inferred, and could be adjusted later
1673
- * as more information becomes available or if the symbol is explicitly
1674
- * declared.
1675
- */
1676
- inferred?: boolean;
1677
- /** `value` can be a JS function since for some constants, such as
1678
- * `Pi`, the actual value depends on the `precision` setting of the
1679
- * `ComputeEngine` and possible other environment settings */
1680
- value?: LatexString | SemiBoxedExpression | ((ce: IComputeEngine) => SemiBoxedExpression | null);
1681
- flags?: Partial<NumericFlags>;
1682
- };
1683
- /**
1684
- * @noInheritDoc
1685
- * @category Definitions
1686
- */
1687
- export interface BoxedSymbolDefinition extends BoxedBaseDefinition, SymbolAttributes, Partial<NumericFlags> {
1688
- get value(): BoxedExpression | undefined;
1689
- set value(val: SemiBoxedExpression | number | undefined);
1690
- domain: BoxedDomain | undefined;
1691
- inferredDomain: boolean;
1692
- }
1693
- /** @category Assumptions */
1694
- export type AssumeResult = 'internal-error' | 'not-a-predicate' | 'contradiction' | 'tautology' | 'ok';
1695
- /** @category Compiling */
1696
- export type CompiledExpression = {
1697
- evaluate?: (scope: {
1698
- [symbol: string]: BoxedExpression;
1699
- }) => number | BoxedExpression;
1700
- };
1701
- /** @internal */
1702
- export interface ComputeEngineStats {
1703
- symbols: Set<BoxedExpression>;
1704
- expressions: null | Set<BoxedExpression>;
1705
- highwaterMark: number;
1706
- }
1707
- /** @category Compute Engine */
1708
- export type AssignValue = boolean | number | string | Decimal | Complex | LatexString | SemiBoxedExpression | ((ce: IComputeEngine, args: any) => BoxedExpression) | undefined;
1709
- /** @category Compute Engine */
1710
- export type ArrayValue = boolean | number | string | Decimal | Complex | BoxedExpression | undefined;
1711
- /** @internal */
1712
- export interface IComputeEngine {
1713
- latexDictionary: readonly LatexDictionaryEntry[];
1714
- readonly Anything: BoxedDomain;
1715
- readonly Void: BoxedDomain;
1716
- readonly Strings: BoxedDomain;
1717
- readonly Booleans: BoxedDomain;
1718
- readonly Numbers: BoxedDomain;
1719
- readonly True: BoxedExpression;
1720
- readonly False: BoxedExpression;
1721
- readonly Pi: BoxedExpression;
1722
- readonly E: BoxedExpression;
1723
- readonly Nothing: BoxedExpression;
1724
- readonly Zero: BoxedExpression;
1725
- readonly One: BoxedExpression;
1726
- readonly Half: BoxedExpression;
1727
- readonly NegativeOne: BoxedExpression;
1728
- readonly I: BoxedExpression;
1729
- readonly NaN: BoxedExpression;
1730
- readonly PositiveInfinity: BoxedExpression;
1731
- readonly NegativeInfinity: BoxedExpression;
1732
- readonly ComplexInfinity: BoxedExpression;
1733
- /** @internal */
1734
- readonly _BIGNUM_NAN: Decimal;
1735
- /** @internal */
1736
- readonly _BIGNUM_ZERO: Decimal;
1737
- /** @internal */
1738
- readonly _BIGNUM_ONE: Decimal;
1739
- /** @internal */
1740
- readonly _BIGNUM_TWO: Decimal;
1741
- /** @internal */
1742
- readonly _BIGNUM_HALF: Decimal;
1743
- /** @internal */
1744
- readonly _BIGNUM_PI: Decimal;
1745
- /** @internal */
1746
- readonly _BIGNUM_NEGATIVE_ONE: Decimal;
1747
- /** The current scope */
1748
- context: RuntimeScope | null;
1749
- /** Absolute time beyond which evaluation should not proceed
1750
- * @internal
1751
- */
1752
- deadline?: number;
1753
- /** @hidden */
1754
- readonly timeLimit: number;
1755
- /** @hidden */
1756
- readonly iterationLimit: number;
1757
- /** @hidden */
1758
- readonly recursionLimit: number;
1759
- numericMode: NumericMode;
1760
- tolerance: number;
1761
- chop(n: number): number;
1762
- chop(n: Decimal): Decimal | 0;
1763
- chop(n: Complex): Complex | 0;
1764
- chop(n: number | Decimal | Complex): number | Decimal | Complex;
1765
- bignum: (a: Decimal.Value | bigint) => Decimal;
1766
- isBignum(a: unknown): a is Decimal;
1767
- complex: (a: number | Complex, b?: number) => Complex;
1768
- isComplex(a: unknown): a is Complex;
1769
- set precision(p: number | 'machine');
1770
- get precision(): number;
1771
- costFunction: (expr: BoxedExpression) => number;
1772
- strict: boolean;
1773
- canonical(xs: SemiBoxedExpression[]): BoxedExpression[];
1774
- box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression, options?: {
1775
- canonical?: boolean | CanonicalForm | CanonicalForm[];
1776
- }): BoxedExpression;
1777
- function(head: string | BoxedExpression, ops: SemiBoxedExpression[], options?: {
1778
- metadata?: Metadata;
1779
- canonical?: boolean;
1780
- }): BoxedExpression;
1781
- number(value: number | bigint | string | MathJsonNumber | Decimal | Complex | Rational, options?: {
1782
- metadata?: Metadata;
1783
- canonical?: boolean;
1784
- }): BoxedExpression;
1785
- symbol(sym: string, options?: {
1786
- metadata?: Metadata;
1787
- canonical?: boolean;
1788
- }): BoxedExpression;
1789
- string(s: string, metadata?: Metadata): BoxedExpression;
1790
- domain(domain: BoxedDomain | DomainExpression, metadata?: Metadata): BoxedDomain;
1791
- error(message: string | [string, ...SemiBoxedExpression[]], where?: SemiBoxedExpression): BoxedExpression;
1792
- domainError(expectedDomain: BoxedDomain | DomainLiteral, actualDomain: undefined | BoxedDomain, where?: SemiBoxedExpression): BoxedExpression;
1793
- hold(expr: SemiBoxedExpression): BoxedExpression;
1794
- add(...ops: BoxedExpression[]): BoxedExpression;
1795
- mul(...ops: BoxedExpression[]): BoxedExpression;
1796
- pow(base: BoxedExpression, exponent: number | Rational | BoxedExpression): BoxedExpression;
1797
- sqrt(base: BoxedExpression): BoxedExpression;
1798
- inv(expr: BoxedExpression): BoxedExpression;
1799
- neg(expr: BoxedExpression): BoxedExpression;
1800
- div(num: BoxedExpression, denom: BoxedExpression): BoxedExpression;
1801
- pair(first: BoxedExpression, second: BoxedExpression, metadata?: Metadata): BoxedExpression;
1802
- tuple(elements: number[], metadata?: Metadata): BoxedExpression;
1803
- tuple(elements: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1804
- array(elements: ArrayValue[] | ArrayValue[][], metadata?: Metadata): BoxedExpression;
1805
- rules(rules: Rule[]): BoxedRuleSet;
1806
- /**
1807
- * This is a primitive to create a boxed function.
1808
- *
1809
- * In general, consider using `ce.box()` or `canonicalXXX()` instead.
1810
- *
1811
- * The caller must ensure that the arguments are in canonical form:
1812
- * - arguments are `canonical()`
1813
- * - arguments are sorted
1814
- * - arguments are flattened and desequenced
1815
- *
1816
- * @internal
1817
- */
1818
- _fn(head: string | BoxedExpression, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
1819
- parse(s: LatexString | string, options?: {
1820
- canonical?: boolean | CanonicalForm | CanonicalForm[];
1821
- }): BoxedExpression;
1822
- parse(s: null, options?: {
1823
- canonical?: boolean | CanonicalForm | CanonicalForm[];
1824
- }): null;
1825
- parse(s: LatexString | string | null, options?: {
1826
- canonical?: boolean | CanonicalForm | CanonicalForm[];
1827
- }): null | BoxedExpression;
1828
- serialize(expr: SemiBoxedExpression, options?: {
1829
- canonical?: boolean;
1830
- }): LatexString;
1831
- get latexOptions(): NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions;
1832
- set latexOptions(opts: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions> & Partial<SerializeLatexOptions>);
1833
- get jsonSerializationOptions(): Readonly<JsonSerializationOptions>;
1834
- set jsonSerializationOptions(val: Partial<JsonSerializationOptions>);
1835
- pushScope(scope?: Partial<Scope>): IComputeEngine;
1836
- popScope(): IComputeEngine;
1837
- swapScope(scope: RuntimeScope | null): RuntimeScope | null;
1838
- resetContext(): void;
1839
- defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
1840
- lookupSymbol(name: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
1841
- defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
1842
- lookupFunction(head: string | BoxedExpression, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
1843
- assign(ids: {
1844
- [id: string]: AssignValue;
1845
- }): IComputeEngine;
1846
- assign(id: string, value: AssignValue): IComputeEngine;
1847
- assign(arg1: string | {
1848
- [id: string]: AssignValue;
1849
- }, arg2?: AssignValue): IComputeEngine;
1850
- declare(identifiers: {
1851
- [id: string]: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition;
1852
- }): IComputeEngine;
1853
- declare(id: string, def: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition): IComputeEngine;
1854
- declare(arg1: string | {
1855
- [id: string]: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition;
1856
- }, arg2?: BoxedDomain | DomainExpression | SymbolDefinition | FunctionDefinition): IComputeEngine;
1857
- assume(predicate: SemiBoxedExpression): AssumeResult;
1858
- forget(symbol?: string | string[]): void;
1859
- get assumptions(): ExpressionMapInterface<boolean>;
1860
- ask(pattern: SemiBoxedExpression): BoxedSubstitution[];
1861
- verify(query: SemiBoxedExpression): boolean;
1862
- /** @internal */
1863
- shouldContinueExecution(): boolean;
1864
- /** @internal */
1865
- checkContinueExecution(): void;
1866
- /** @internal */
1867
- cache<T>(name: string, build: () => T, purge?: (T: any) => T | undefined): T;
1868
- /** @internal */
1869
- readonly stats: ComputeEngineStats;
1870
- /** @internal */
1871
- reset(): void;
1872
- /** @internal */
1873
- _register(expr: BoxedExpression): void;
1874
- /** @internal */
1875
- _unregister(expr: BoxedExpression): void;
1876
- }