@cortex-js/compute-engine 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/dist/compute-engine.esm.js +28330 -24627
  2. package/dist/compute-engine.min.esm.js +70 -68
  3. package/dist/compute-engine.min.umd.js +131 -0
  4. package/dist/{compute-engine.cjs → compute-engine.umd.js} +28330 -24627
  5. package/dist/math-json.esm.js +22 -294
  6. package/dist/math-json.min.esm.js +22 -294
  7. package/dist/math-json.min.umd.js +4 -0
  8. package/dist/math-json.umd.js +141 -0
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/configuration-change.d.ts +28 -0
  11. package/dist/types/common/fuzzy-string-match.d.ts +1 -1
  12. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  13. package/dist/types/common/interruptible.d.ts +1 -1
  14. package/dist/types/common/one-of.d.ts +1 -1
  15. package/dist/types/common/signals.d.ts +1 -1
  16. package/dist/types/common/type/boxed-type.d.ts +20 -4
  17. package/dist/types/common/type/parse.d.ts +4 -4
  18. package/dist/types/common/type/primitive.d.ts +3 -2
  19. package/dist/types/common/type/serialize.d.ts +1 -1
  20. package/dist/types/common/type/subtype.d.ts +1 -1
  21. package/dist/types/common/type/types.d.ts +91 -25
  22. package/dist/types/common/type/utils.d.ts +2 -1
  23. package/dist/types/common/utils.d.ts +1 -1
  24. package/dist/types/compute-engine/assume.d.ts +2 -2
  25. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +72 -73
  26. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +2 -2
  29. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +19 -1
  30. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  31. package/dist/types/compute-engine/boxed-expression/box.d.ts +6 -6
  32. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +42 -0
  33. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +48 -27
  34. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +17 -5
  35. package/dist/types/compute-engine/boxed-expression/{boxed-function-definition.d.ts → boxed-operator-definition.d.ts} +12 -12
  36. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  37. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +7 -8
  38. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +91 -52
  39. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +22 -25
  40. package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +46 -0
  41. package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
  42. package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +5 -0
  43. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -2
  44. package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
  45. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  46. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  47. package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
  48. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  49. package/dist/types/compute-engine/boxed-expression/hold.d.ts +2 -2
  50. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  51. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  52. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +30 -3
  53. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  54. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/product.d.ts +2 -2
  56. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +41 -1
  59. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  60. package/dist/types/compute-engine/boxed-expression/solve.d.ts +3 -1
  61. package/dist/types/compute-engine/boxed-expression/terms.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/utils.d.ts +23 -23
  64. package/dist/types/compute-engine/boxed-expression/validate.d.ts +2 -1
  65. package/dist/types/compute-engine/collection-utils.d.ts +22 -57
  66. package/dist/types/compute-engine/compile.d.ts +61 -10
  67. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  68. package/dist/types/compute-engine/function-utils.d.ts +46 -29
  69. package/dist/types/compute-engine/global-types.d.ts +1432 -893
  70. package/dist/types/compute-engine/index.d.ts +154 -124
  71. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  72. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  73. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  74. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  75. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  76. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  77. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  78. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  79. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  80. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  81. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  82. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  83. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  84. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +3 -3
  85. package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +21 -0
  86. package/dist/types/compute-engine/latex-syntax/parse.d.ts +14 -12
  87. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  88. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  89. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +2 -2
  90. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  91. package/dist/types/compute-engine/latex-syntax/types.d.ts +51 -39
  92. package/dist/types/compute-engine/latex-syntax/utils.d.ts +5 -0
  93. package/dist/types/compute-engine/library/arithmetic.d.ts +2 -2
  94. package/dist/types/compute-engine/library/calculus.d.ts +2 -2
  95. package/dist/types/compute-engine/library/collections.d.ts +3 -3
  96. package/dist/types/compute-engine/library/combinatorics.d.ts +2 -0
  97. package/dist/types/compute-engine/library/complex.d.ts +2 -2
  98. package/dist/types/compute-engine/library/control-structures.d.ts +2 -2
  99. package/dist/types/compute-engine/library/core.d.ts +2 -2
  100. package/dist/types/compute-engine/library/invisible-operator.d.ts +1 -1
  101. package/dist/types/compute-engine/library/library.d.ts +5 -5
  102. package/dist/types/compute-engine/library/linear-algebra.d.ts +2 -2
  103. package/dist/types/compute-engine/library/logic.d.ts +2 -2
  104. package/dist/types/compute-engine/library/number-theory.d.ts +2 -0
  105. package/dist/types/compute-engine/library/polynomials.d.ts +2 -2
  106. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  107. package/dist/types/compute-engine/library/relational-operator.d.ts +2 -2
  108. package/dist/types/compute-engine/library/sets.d.ts +2 -2
  109. package/dist/types/compute-engine/library/statistics.d.ts +2 -2
  110. package/dist/types/compute-engine/library/trigonometry.d.ts +2 -2
  111. package/dist/types/compute-engine/library/utils.d.ts +24 -2
  112. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +3 -4
  113. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +3 -3
  114. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +3 -4
  115. package/dist/types/compute-engine/numeric-value/types.d.ts +7 -5
  116. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  117. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  118. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  119. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +4 -19
  120. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  121. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  122. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  123. package/dist/types/compute-engine/numerics/numeric.d.ts +9 -1
  124. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  125. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  126. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  127. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  128. package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
  129. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  130. package/dist/types/compute-engine/numerics/types.d.ts +1 -1
  131. package/dist/types/compute-engine/symbolic/antiderivative.d.ts +3 -0
  132. package/dist/types/compute-engine/symbolic/derivative.d.ts +2 -4
  133. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  134. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  135. package/dist/types/compute-engine/tensor/tensor-fields.d.ts +5 -46
  136. package/dist/types/compute-engine/tensor/tensors.d.ts +4 -14
  137. package/dist/types/compute-engine/types.d.ts +2 -5
  138. package/dist/types/compute-engine.d.ts +1 -1
  139. package/dist/types/math-json/symbols.d.ts +11 -0
  140. package/dist/types/math-json/types.d.ts +19 -11
  141. package/dist/types/math-json/utils.d.ts +18 -9
  142. package/dist/types/math-json.d.ts +2 -2
  143. package/package.json +9 -11
  144. package/dist/compute-engine.min.cjs +0 -129
  145. package/dist/math-json.cjs +0 -413
  146. package/dist/math-json.min.cjs +0 -6
  147. package/dist/types/common/json5.d.ts +0 -3
  148. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +0 -64
  149. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +0 -21
  150. package/dist/types/math-json/identifiers.d.ts +0 -11
@@ -1,15 +1,16 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  import { Decimal } from 'decimal.js';
3
- import { Expression, MathJsonIdentifier, MathJsonNumber } from '../math-json/types';
4
- import { SymbolDefinition, FunctionDefinition, AngularUnit, AssignValue, AssumeResult, BoxedExpression, BoxedFunctionDefinition, BoxedRule, BoxedRuleSet, BoxedSubstitution, BoxedSymbolDefinition, CanonicalOptions, ComputeEngineStats, EvaluateOptions, ExpressionMapInterface, IdentifierDefinitions, Metadata, Rule, RuntimeScope, Scope, SemiBoxedExpression, ComputeEngine as IComputeEngine } from './global-types';
5
- import type { BigNum, Rational } from './numerics/types';
3
+ import { Type, TypeResolver, TypeString } from '../common/type/types';
4
+ import { BoxedType } from '../common/type/boxed-type';
5
+ import type { OneOf } from '../common/one-of';
6
+ import { ConfigurationChangeListener } from '../common/configuration-change';
7
+ import type { Expression, MathJsonSymbol, MathJsonNumberObject } from '../math-json/types';
8
+ import type { ValueDefinition, OperatorDefinition, AngularUnit, AssignValue, AssumeResult, BoxedExpression, BoxedRule, BoxedRuleSet, BoxedSubstitution, CanonicalOptions, SymbolDefinitions, Metadata, Rule, Scope, EvalContext, SemiBoxedExpression, ComputeEngine as IComputeEngine, BoxedDefinition, SymbolDefinition } from './global-types';
9
+ import type { LatexDictionaryEntry, LatexString, LibraryCategory, ParseLatexOptions } from './latex-syntax/types';
6
10
  import { type IndexedLatexDictionary } from './latex-syntax/dictionary/definitions';
7
- import './boxed-expression/serialize';
11
+ import type { BigNum, Rational } from './numerics/types';
8
12
  import { ExactNumericValueData, NumericValue, NumericValueData } from './numeric-value/types';
9
- import type { LatexDictionaryEntry, LatexString, LibraryCategory, ParseLatexOptions } from './latex-syntax/types';
10
- import type { OneOf } from '../common/one-of';
11
- import { Type, TypeString } from '../common/type/types';
12
- import { BoxedType } from '../common/type/boxed-type';
13
+ import './boxed-expression/serialize';
13
14
  export * from './global-types';
14
15
  /**
15
16
  *
@@ -94,9 +95,8 @@ export declare class ComputeEngine implements IComputeEngine {
94
95
  private _bignumTolerance;
95
96
  private _negBignumTolerance;
96
97
  /** @internal */
97
- private _cache;
98
- /** @internal */
99
- private _stats;
98
+ private __cache;
99
+ private _configurationChangeTracker;
100
100
  /** @internal */
101
101
  private _cost?;
102
102
  /** @internal */
@@ -104,25 +104,39 @@ export declare class ComputeEngine implements IComputeEngine {
104
104
  /** @internal */
105
105
  private _commonNumbers;
106
106
  /**
107
- * The current scope.
108
- *
109
- * A **scope** stores the definition of symbols and assumptions.
110
- *
111
- * Scopes form a stack, and definitions in more recent
112
- * scopes can obscure definitions from older scopes.
107
+ * The stack of evaluation contexts.
113
108
  *
114
- * The `ce.context` property represents the current scope.
109
+ * An **evaluation context** contains bindings of symbols to their
110
+ * values, assumptions, and the matching scope.
115
111
  *
116
112
  */
117
- context: RuntimeScope | null;
113
+ _evalContextStack: EvalContext[];
114
+ /** The current evaluation context */
115
+ get context(): EvalContext;
116
+ get contextStack(): ReadonlyArray<EvalContext>;
117
+ set contextStack(stack: ReadonlyArray<EvalContext>);
118
+ /** @internal */
119
+ get _typeResolver(): TypeResolver;
118
120
  /**
119
- * Generation.
121
+ * Declare a new type in the current scope.
120
122
  *
123
+ * By default, types are nominal. To declare a structural type, set
124
+ * `alias` to `true`.
125
+ */
126
+ declareType(name: string, type: BoxedType | Type | TypeString, { alias }?: {
127
+ alias?: boolean;
128
+ }): void;
129
+ /**
130
+ * A list of the function calls to the current evaluation context,
131
+ * most recent first.
132
+ */
133
+ get trace(): ReadonlyArray<string>;
134
+ /**
121
135
  * The generation is incremented each time the context changes.
122
136
  * It is used to invalidate caches.
123
137
  * @internal
124
138
  */
125
- generation: number;
139
+ _generation: number;
126
140
  /** In strict mode (the default) the Compute Engine performs
127
141
  * validation of domains and signature and may report errors.
128
142
  *
@@ -138,21 +152,19 @@ export declare class ComputeEngine implements IComputeEngine {
138
152
  */
139
153
  deadline?: number;
140
154
  /**
141
- * Return identifier tables suitable for the specified categories, or `"all"`
155
+ * Return symbol tables suitable for the specified categories, or `"all"`
142
156
  * for all categories (`"arithmetic"`, `"algebra"`, etc...).
143
157
  *
144
- * An identifier table defines how the symbols and function names in a
145
- * MathJSON expression should be interpreted, i.e. how to evaluate and
146
- * manipulate them.
158
+ * A symbol table defines how to evaluate and manipulate symbols.
147
159
  *
148
160
  */
149
161
  /** @internal */
150
162
  private _latexDictionaryInput;
151
163
  /** @internal */
152
- _indexedLatexDictionary: IndexedLatexDictionary;
164
+ __indexedLatexDictionary: IndexedLatexDictionary;
153
165
  /** @internal */
154
166
  _bignum: Decimal.Constructor;
155
- static getStandardLibrary(categories?: LibraryCategory[] | LibraryCategory | 'all'): readonly IdentifierDefinitions[];
167
+ static getStandardLibrary(categories?: LibraryCategory[] | LibraryCategory | 'all'): readonly SymbolDefinitions[];
156
168
  /**
157
169
  * Return a LaTeX dictionary suitable for the specified category, or `"all"`
158
170
  * for all categories (`"arithmetic"`, `"algebra"`, etc...).
@@ -178,7 +190,7 @@ export declare class ComputeEngine implements IComputeEngine {
178
190
  * ...ce.getLatexDictionary("all"),
179
191
  * {
180
192
  * kind: "function",
181
- * identifierTrigger: "concat",
193
+ * symbolTrigger: "concat",
182
194
  * parse: "Concatenate"
183
195
  * }
184
196
  * ];
@@ -188,7 +200,7 @@ export declare class ComputeEngine implements IComputeEngine {
188
200
  /**
189
201
  * Construct a new `ComputeEngine` instance.
190
202
  *
191
- * Identifier tables define functions and symbols (in `options.ids`).
203
+ * Symbols tables define functions, constants and variables (in `options.ids`).
192
204
  * If no table is provided the MathJSON Standard Library is used (`ComputeEngine.getStandardLibrary()`)
193
205
  *
194
206
  * The LaTeX syntax dictionary is defined in `options.latexDictionary`.
@@ -207,14 +219,15 @@ export declare class ComputeEngine implements IComputeEngine {
207
219
  * `chop()` as well.
208
220
  */
209
221
  constructor(options?: {
210
- ids?: readonly IdentifierDefinitions[];
222
+ ids?: readonly SymbolDefinitions[];
211
223
  precision?: number | 'machine';
212
224
  tolerance?: number | 'auto';
213
225
  });
214
- toString(): string;
226
+ toJSON(): string;
227
+ [Symbol.toStringTag]: string;
215
228
  get latexDictionary(): Readonly<LatexDictionaryEntry[]>;
216
229
  set latexDictionary(dic: Readonly<LatexDictionaryEntry[]>);
217
- get indexedLatexDictionary(): IndexedLatexDictionary;
230
+ get _indexedLatexDictionary(): IndexedLatexDictionary;
218
231
  /** After the configuration of the engine has changed, clear the caches
219
232
  * so that new values can be recalculated.
220
233
  *
@@ -222,13 +235,9 @@ export declare class ComputeEngine implements IComputeEngine {
222
235
  *
223
236
  * @internal
224
237
  */
225
- reset(): void;
226
- /** @internal */
227
- _register(_expr: BoxedExpression): void;
238
+ _reset(): void;
228
239
  /** @internal */
229
- _unregister(_expr: BoxedExpression): void;
230
- /** @internal */
231
- get stats(): ComputeEngineStats;
240
+ listenToConfigurationChange(tracker: ConfigurationChangeListener): () => void;
232
241
  get precision(): number;
233
242
  /** The precision, or number of significant digits, of numeric
234
243
  * calculations.
@@ -238,6 +247,13 @@ export declare class ComputeEngine implements IComputeEngine {
238
247
  *
239
248
  * Trigonometric operations are accurate for precision up to 1,000.
240
249
  *
250
+ * If the precision is set to `machine`, floating point numbers
251
+ * are represented internally as a 64-bit floating point number (as
252
+ * per IEEE 754-2008), with a 52-bit mantissa, which gives about 15
253
+ * digits of precision.
254
+ *
255
+ * If the precision is set to `auto`, the precision is set to a default value.
256
+ *
241
257
  */
242
258
  set precision(p: number | 'machine' | 'auto');
243
259
  /**
@@ -252,15 +268,31 @@ export declare class ComputeEngine implements IComputeEngine {
252
268
  */
253
269
  get angularUnit(): AngularUnit;
254
270
  set angularUnit(u: AngularUnit);
271
+ /** Throw a `CancellationError` when the duration of an evaluation exceeds
272
+ * the time limit.
273
+ *
274
+ * Time in milliseconds, default 2000 ms = 2 seconds.
275
+ *
276
+ */
255
277
  get timeLimit(): number;
256
278
  set timeLimit(t: number);
257
279
  private _timeLimit;
258
280
  /** The time after which the time limit has been exceeded */
259
281
  _deadline: number | undefined;
260
282
  get _timeRemaining(): number;
283
+ /** Throw `CancellationError` `iteration-limit-exceeded` when the iteration limit
284
+ * in a loop is exceeded. Default: no limits.
285
+ *
286
+ * @experimental
287
+ */
261
288
  get iterationLimit(): number;
262
289
  set iterationLimit(t: number);
263
290
  private _iterationLimit;
291
+ /** Signal `recursion-depth-exceeded` when the recursion depth for this
292
+ * scope is exceeded.
293
+ *
294
+ * @experimental
295
+ */
264
296
  get recursionLimit(): number;
265
297
  set recursionLimit(t: number);
266
298
  private _recursionLimit;
@@ -388,118 +420,101 @@ export declare class ComputeEngine implements IComputeEngine {
388
420
  get costFunction(): (expr: BoxedExpression) => number;
389
421
  set costFunction(fn: ((expr: BoxedExpression) => number) | undefined);
390
422
  /**
391
- * Return a matching symbol definition, starting with the current
392
- * scope and going up the scope chain. Prioritize finding a match by
393
- * wikidata, if provided.
394
- */
395
- lookupSymbol(symbol: string, wikidata?: string, scope?: RuntimeScope): undefined | BoxedSymbolDefinition;
396
- /**
397
- * Return the definition for a function with this operator name.
398
- *
399
- * Start looking in the current context, than up the scope chain.
400
- *
401
- * This is a very rough lookup, since it doesn't account for the domain
402
- * of the argument or the codomain. However, it is useful during parsing
403
- * to differentiate between symbols that might represent a function application, e.g. `f` vs `x`.
423
+ * Return definition matching the symbol, starting with the current
424
+ * lexical scope and going up the scope chain.
404
425
  */
405
- lookupFunction(name: MathJsonIdentifier, scope?: RuntimeScope | null): undefined | BoxedFunctionDefinition;
426
+ lookupDefinition(id: MathJsonSymbol): undefined | BoxedDefinition;
406
427
  /**
407
428
  * Associate a new definition to a symbol in the current context.
408
429
  *
409
- * If a definition existed previously, it is replaced.
410
- *
411
- *
412
430
  * For internal use. Use `ce.declare()` instead.
413
431
  *
414
432
  * @internal
415
433
  */
416
- defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
417
- _defineSymbol(name: string, def: SymbolDefinition): BoxedSymbolDefinition;
434
+ _declareSymbolValue(name: MathJsonSymbol, def: Partial<ValueDefinition>, scope?: Scope): BoxedDefinition;
418
435
  /**
419
- * Associate a new FunctionDefinition to a function in the current context.
420
- *
421
- * If a definition existed previously, it is replaced.
436
+ * Associate a new OperatorDefinition to a function in the current context.
422
437
  *
423
438
  * For internal use. Use `ce.declare()` instead.
424
439
  *
425
440
  * @internal
426
441
  */
427
- defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
428
- _defineFunction(name: string, def: FunctionDefinition): BoxedFunctionDefinition;
442
+ _declareSymbolOperator(name: string, def: OperatorDefinition, scope?: Scope): BoxedDefinition;
429
443
  /**
430
444
  *
431
- * Create a new scope and add it to the top of the scope stack
445
+ * Create a new lexical scope and matching evaluation context and add it
446
+ * to the evaluation context stack.
432
447
  *
433
448
  */
434
- pushScope(scope?: Partial<Scope>): IComputeEngine;
435
- /** Remove the most recent scope from the scope stack, and set its
436
- * parent scope as the current scope. */
437
- popScope(): IComputeEngine;
438
- /** Set the current scope, return the previous scope. */
439
- swapScope(scope: RuntimeScope | null): RuntimeScope | null;
449
+ pushScope(scope?: Scope, name?: string): void;
450
+ /**
451
+ * Remove the most recent scope from the scope stack.
452
+ */
453
+ popScope(): void;
454
+ /** @internal */
455
+ _pushEvalContext(scope: Scope, name?: string): void;
456
+ /** @internal */
457
+ _popEvalContext(): void;
458
+ /** @internal */
459
+ _inScope<T>(scope: Scope | undefined, f: () => T): T;
440
460
  /** @internal */
441
- _printScope(options?: {
461
+ _printStack(options?: {
442
462
  details?: boolean;
443
463
  maxDepth?: number;
444
- }, scope?: RuntimeScope | null, depth?: number): RuntimeScope | null;
464
+ }): void;
445
465
  /**
446
- * Reset the value of any identifiers that have been assigned a value
447
- * in the current scope.
466
+ * Use `ce.box(name)` instead
448
467
  * @internal */
449
- resetContext(): void;
468
+ _getSymbolValue(id: MathJsonSymbol): BoxedExpression | undefined;
450
469
  /**
451
- * Declare an identifier: specify their type and other attributes,
452
- * including optionally a value.
470
+ * For internal use. Use `ce.assign(name, value)` instead.
471
+ * @internal
472
+ */
473
+ _setSymbolValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
474
+ /**
475
+ * Declare a symbol in the current lexical scope: specify their type and
476
+ * other attributes, including optionally a value.
453
477
  *
454
- * Once the type of an identifier has been declared, it cannot be changed.
478
+ * Once the type of a symbol has been declared, it cannot be changed.
455
479
  * The type information is used to calculate the canonical form of
456
480
  * expressions and ensure they are valid. If the type could be changed
457
481
  * after the fact, previously valid expressions could become invalid.
458
482
  *
459
- * Set the type to `any` type for a very generic type, or use `unknown`
460
- * if the type is not known yet. If `unknown`, the type will be inferred
461
- * based on usage.
483
+ * Set the type to `unknown` if the type is not known yet: it will be
484
+ * inferred based on usage. Use `any` for a very generic type.
462
485
  *
463
- * An identifier can be redeclared with a different type, but only if
464
- * the type of the identifier was inferred. If the domain was explicitly
465
- * set, the identifier cannot be redeclared.
466
486
  *
467
487
  */
468
- declare(id: string, def: TypeString | OneOf<[Type, SymbolDefinition, FunctionDefinition]>): IComputeEngine;
469
- declare(identifiers: {
470
- [id: string]: Type | TypeString | OneOf<[SymbolDefinition, FunctionDefinition]>;
488
+ declare(id: string, def: Type | TypeString | Partial<SymbolDefinition>, scope?: Scope): IComputeEngine;
489
+ declare(symbols: {
490
+ [id: string]: Type | TypeString | Partial<SymbolDefinition>;
471
491
  }): IComputeEngine;
472
- /** Assign a value to an identifier in the current scope.
473
- * Use `undefined` to reset the identifier to no value.
474
- *
475
- * The identifier should be a valid MathJSON identifier
476
- * not a LaTeX string.
492
+ /**
493
+ * Return an evaluation context in which the symbol is defined.
494
+ */
495
+ lookupContext(id: MathJsonSymbol): EvalContext | undefined;
496
+ /** Find the context in the stack frame, and set the stack frame to
497
+ * it. This is used to evaluate expressions in the context of
498
+ * a different scope.
499
+ */
500
+ _swapContext(context: EvalContext): void;
501
+ /**
502
+ * Assign a value to a symbol in the current scope.
503
+ * Use `undefined` to reset the symbol to no value.
477
504
  *
478
- * The identifier can take the form "f(x, y") to create a function
479
- * with two parameters, "x" and "y".
505
+ * The symbol should be a valid MathJSON symbol not a LaTeX string.
480
506
  *
481
- * If the id was not previously declared, assigning a value will declare it.
482
- * Its type is inferred from the value.
507
+ * If the symbol was not previously declared, it will be declared as a
508
+ * symbol of a type inferred from its value.
483
509
  *
484
- * To more precisely define the type of the identifier, use `ce.declare()`
510
+ * To more precisely define the type of the symbol, use `ce.declare()`
485
511
  * instead, which allows you to specify the type, value and other
486
- * attributes of the identifier.
512
+ * attributes of the symbol.
487
513
  */
488
514
  assign(id: string, value: AssignValue): IComputeEngine;
489
515
  assign(ids: {
490
516
  [id: string]: AssignValue;
491
517
  }): IComputeEngine;
492
- /**
493
- * Same as assign(), but for internal use:
494
- * - skips validity checks
495
- * - does not auto-declare
496
- * - if assigning to a function, must pass a JS function
497
- *
498
- * @internal
499
- */
500
- _assign(id: string, value: BoxedExpression | ((ops: ReadonlyArray<BoxedExpression>, options: Partial<EvaluateOptions> & {
501
- engine?: IComputeEngine;
502
- }) => BoxedExpression | undefined)): void;
503
518
  /**
504
519
  * Return false if the execution should stop.
505
520
  *
@@ -509,22 +524,24 @@ export declare class ComputeEngine implements IComputeEngine {
509
524
  *
510
525
  * @internal
511
526
  */
512
- shouldContinueExecution(): boolean;
527
+ _shouldContinueExecution(): boolean;
513
528
  /** @internal */
514
- checkContinueExecution(): void;
529
+ _checkContinueExecution(): void;
515
530
  /** @internal */
516
- cache<T>(cacheName: string, build: () => T, purge?: (t: T) => T | undefined): T;
531
+ _cache<T>(cacheName: string, build: () => T, purge?: (t: T) => T | undefined): T;
517
532
  /** Return a boxed expression from a number, string or semiboxed expression.
518
533
  * Calls `ce.function()`, `ce.number()` or `ce.symbol()` as appropriate.
519
534
  */
520
535
  box(expr: NumericValue | SemiBoxedExpression, options?: {
521
536
  canonical?: CanonicalOptions;
522
537
  structural?: boolean;
538
+ scope?: Scope | undefined;
523
539
  }): BoxedExpression;
524
540
  function(name: string, ops: ReadonlyArray<BoxedExpression> | ReadonlyArray<Expression>, options?: {
525
541
  metadata?: Metadata;
526
- canonical: CanonicalOptions;
527
- structural: boolean;
542
+ canonical?: CanonicalOptions;
543
+ structural?: boolean;
544
+ scope?: Scope | undefined;
528
545
  }): BoxedExpression;
529
546
  /**
530
547
  *
@@ -546,39 +563,45 @@ export declare class ComputeEngine implements IComputeEngine {
546
563
  tuple(...elements: ReadonlyArray<BoxedExpression>): BoxedExpression;
547
564
  type(type: Type | TypeString | BoxedType): BoxedType;
548
565
  string(s: string, metadata?: Metadata): BoxedExpression;
549
- /** Return a boxed symbol */
566
+ /** Create a boxed symbol */
550
567
  symbol(name: string, options?: {
551
- metadata?: Metadata;
552
568
  canonical?: CanonicalOptions;
553
569
  }): BoxedExpression;
554
570
  /**
555
571
  * This function tries to avoid creating a boxed number if `num` corresponds
556
572
  * to a common value for which we have a shared instance (-1, 0, NaN, etc...)
557
573
  */
558
- number(value: number | bigint | string | NumericValue | MathJsonNumber | Decimal | Complex | Rational, options?: {
574
+ number(value: number | bigint | string | NumericValue | MathJsonNumberObject | Decimal | Complex | Rational, options?: {
559
575
  metadata: Metadata;
560
576
  canonical: CanonicalOptions;
561
577
  }): BoxedExpression;
562
578
  rules(rules: Rule | ReadonlyArray<Rule | BoxedRule> | BoxedRuleSet | undefined | null, options?: {
563
579
  canonical?: boolean;
564
580
  }): BoxedRuleSet;
581
+ /**
582
+ * Return a set of built-in rules.
583
+ */
565
584
  getRuleSet(id?: string): BoxedRuleSet | undefined;
566
585
  /**
567
586
  * Return a function expression, but the caller is responsible for making
568
587
  * sure that the arguments are canonical.
569
588
  *
570
- * Unlike ce.function(), the operator of the result is the name argument.
589
+ * Unlike `ce.function()`, the operator of the result is the name argument.
571
590
  * Calling this function directly is potentially unsafe, as it bypasses
572
591
  * the canonicalization of the arguments.
573
592
  *
574
593
  * For example:
575
594
  *
576
- * - `ce._fn('Multiply', [1, 'x'])` returns `['Multiply', 1, 'x']` as a canonical expression, even though it doesn't follow the canonical form
577
- * - `ce.function('Multiply', [1, 'x']` returns `'x'` which is the correct canonical form
595
+ * - `ce._fn('Multiply', [1, 'x'])` returns `['Multiply', 1, 'x']` as a
596
+ * canonical expression, even though it doesn't follow the canonical form
597
+ * - `ce.function('Multiply', [1, 'x']` returns `'x'` which is the correct
598
+ * canonical form
578
599
  *
579
600
  * @internal */
580
- _fn(name: MathJsonIdentifier, ops: ReadonlyArray<BoxedExpression>, options?: Metadata & {
601
+ _fn(name: MathJsonSymbol, ops: ReadonlyArray<BoxedExpression>, options?: {
602
+ metadata?: Metadata;
581
603
  canonical?: boolean;
604
+ scope?: Scope;
582
605
  }): BoxedExpression;
583
606
  /**
584
607
  * Parse a string of LaTeX and return a corresponding `BoxedExpression`.
@@ -592,7 +615,6 @@ export declare class ComputeEngine implements IComputeEngine {
592
615
  parse(latex: LatexString, options?: Partial<ParseLatexOptions> & {
593
616
  canonical?: CanonicalOptions;
594
617
  }): BoxedExpression;
595
- get assumptions(): ExpressionMapInterface<boolean>;
596
618
  /**
597
619
  * Return a list of all the assumptions that match a pattern.
598
620
  *
@@ -622,6 +644,14 @@ export declare class ComputeEngine implements IComputeEngine {
622
644
  *
623
645
  */
624
646
  assume(predicate: BoxedExpression): AssumeResult;
625
- /** Remove all assumptions about one or more symbols */
626
- forget(symbol: undefined | string | string[]): void;
647
+ /**
648
+ * Remove all assumptions about one or more symbols.
649
+ *
650
+ * `ce.forget()` will remove all assumptions.
651
+ *
652
+ * Note that assumptions are scoped, so when exiting the current lexical
653
+ * scope, the previous assumptions will be restored.
654
+ *
655
+ * */
656
+ forget(symbol: undefined | MathJsonSymbol | MathJsonSymbol[]): void;
627
657
  }
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_ALGEBRA: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_ARITHMETIC: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_CALCULUS: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_COMPLEX: LatexDictionary;
@@ -1,4 +1,4 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_CORE: LatexDictionary;
3
3
  export declare const DELIMITERS_SHORTHAND: {
4
4
  '(': string;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_LINEAR_ALGEBRA: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_LOGIC: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_OTHERS: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_INEQUALITIES: LatexDictionaryEntry[];
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_SETS: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_STATISTICS: LatexDictionary;
@@ -1,3 +1,3 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const SYMBOLS: [string, string, number][];
3
3
  export declare const DEFINITIONS_SYMBOLS: LatexDictionary;
@@ -1,2 +1,2 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  export declare const DEFINITIONS_TRIGONOMETRY: LatexDictionary;
@@ -1,14 +1,14 @@
1
- /* 0.29.0 */
1
+ /* 0.30.0 */
2
2
  import { Delimiter, EnvironmentParseHandler, ExpressionParseHandler, InfixParseHandler, LatexDictionary, LatexDictionaryEntry, LatexString, LatexToken, LibraryCategory, MatchfixParseHandler, PostfixParseHandler, Precedence, SerializeHandler } from '../types';
3
3
  export type CommonEntry = {
4
4
  /** Note: a name is required if a serialize handler is provided */
5
5
  name?: string;
6
6
  serialize?: SerializeHandler;
7
- /** Note: not all kinds have a `latexTrigger` or `identifierTrigger`.
7
+ /** Note: not all kinds have a `latexTrigger` or `symbolTrigger`.
8
8
  * For example, matchfix operators use `openTrigger`/`closeTrigger`
9
9
  */
10
10
  latexTrigger?: LatexString;
11
- identifierTrigger?: string;
11
+ symbolTrigger?: string;
12
12
  };
13
13
  export type IndexedSymbolEntry = CommonEntry & {
14
14
  kind: 'symbol';
@@ -0,0 +1,21 @@
1
+ /* 0.30.0 */
2
+ import { Parser } from './types';
3
+ /** For error handling, if we have a symbol prefix, assume
4
+ * the symbol is invalid (it would have been captured by
5
+ * `matchSymbol()` otherwise) and return an error expression */
6
+ export declare function parseInvalidSymbol(parser: Parser): Expression | null;
7
+ /**
8
+ * Match a symbol.
9
+ *
10
+ * It can be:
11
+ * - a sequence of emojis: `👍🏻👍🏻👍🏻`
12
+ * - a single-letter: `a`
13
+ * - some LaTeX commands: `\alpha`
14
+ * - a multi-letter id with a prefix: `\operatorname{speed}`
15
+ * - an id with multiple prefixes:
16
+ * `\mathbin{\mathsf{T}}`
17
+ * - an id with modifiers:
18
+ * - `\mathrm{\alpha_{12}}` or
19
+ * - `\mathit{speed\unicode{"2012}of\unicode{"2012}sound}`
20
+ */
21
+ export declare function parseSymbol(parser: Parser): MathJsonSymbol | null;