@cortex-js/compute-engine 0.32.1 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compute-engine.esm.js +39456 -28560
- package/dist/compute-engine.min.esm.js +80 -59
- package/dist/compute-engine.min.umd.js +80 -59
- package/dist/compute-engine.umd.js +39464 -28585
- package/dist/math-json.esm.js +2 -2
- package/dist/math-json.min.esm.js +2 -130
- package/dist/math-json.min.umd.js +2 -2
- package/dist/math-json.umd.js +2 -2
- package/dist/types/common/ansi-codes.d.ts +1 -1
- package/dist/types/common/configuration-change.d.ts +1 -1
- package/dist/types/common/fuzzy-string-match.d.ts +1 -1
- package/dist/types/common/grapheme-splitter.d.ts +1 -1
- package/dist/types/common/interruptible.d.ts +1 -1
- package/dist/types/common/one-of.d.ts +2 -1
- package/dist/types/common/signals.d.ts +1 -1
- package/dist/types/common/type/ast-nodes.d.ts +1 -1
- package/dist/types/common/type/boxed-type.d.ts +1 -1
- package/dist/types/common/type/lexer.d.ts +1 -1
- package/dist/types/common/type/parse.d.ts +1 -1
- package/dist/types/common/type/parser.d.ts +1 -1
- package/dist/types/common/type/primitive.d.ts +1 -1
- package/dist/types/common/type/reduce.d.ts +12 -0
- package/dist/types/common/type/serialize.d.ts +1 -1
- package/dist/types/common/type/subtype.d.ts +1 -1
- package/dist/types/common/type/type-builder.d.ts +1 -1
- package/dist/types/common/type/types.d.ts +1 -1
- package/dist/types/common/type/utils.d.ts +1 -1
- package/dist/types/common/utils.d.ts +1 -1
- package/dist/types/compute-engine/assume.d.ts +37 -1
- package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +6 -3
- package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +6 -2
- package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +5 -1
- package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +2 -1
- package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +5 -1
- package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/compare.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/factor.d.ts +47 -1
- package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/hold.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/product.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +60 -0
- package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/terms.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +1 -2
- package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
- package/dist/types/compute-engine/collection-utils.d.ts +1 -1
- package/dist/types/compute-engine/compilation/base-compiler.d.ts +1 -1
- package/dist/types/compute-engine/compilation/glsl-target.d.ts +63 -0
- package/dist/types/compute-engine/compilation/javascript-target.d.ts +1 -1
- package/dist/types/compute-engine/compilation/types.d.ts +58 -1
- package/dist/types/compute-engine/cost-function.d.ts +1 -1
- package/dist/types/compute-engine/function-utils.d.ts +1 -1
- package/dist/types/compute-engine/global-types.d.ts +381 -10
- package/dist/types/compute-engine/index.d.ts +186 -3
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +10 -1
- package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/parse.d.ts +21 -1
- package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/serializer.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/types.d.ts +33 -3
- package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
- package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/library/calculus.d.ts +1 -1
- package/dist/types/compute-engine/library/collections.d.ts +1 -1
- package/dist/types/compute-engine/library/combinatorics.d.ts +1 -1
- package/dist/types/compute-engine/library/complex.d.ts +1 -1
- package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
- package/dist/types/compute-engine/library/core.d.ts +1 -1
- package/dist/types/compute-engine/library/invisible-operator.d.ts +1 -1
- package/dist/types/compute-engine/library/library.d.ts +1 -1
- package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/library/logic-analysis.d.ts +189 -4
- package/dist/types/compute-engine/library/logic-utils.d.ts +1 -5
- package/dist/types/compute-engine/library/logic.d.ts +1 -1
- package/dist/types/compute-engine/library/number-theory.d.ts +1 -1
- package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
- package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
- package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
- package/dist/types/compute-engine/library/sets.d.ts +1 -1
- package/dist/types/compute-engine/library/statistics.d.ts +1 -1
- package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
- package/dist/types/compute-engine/library/utils.d.ts +60 -2
- package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
- package/dist/types/compute-engine/numeric-value/types.d.ts +1 -1
- package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
- package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
- package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
- package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
- package/dist/types/compute-engine/numerics/numeric.d.ts +2 -2
- package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
- package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
- package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
- package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
- package/dist/types/compute-engine/numerics/statistics.d.ts +1 -1
- package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
- package/dist/types/compute-engine/numerics/types.d.ts +2 -1
- package/dist/types/compute-engine/oeis.d.ts +85 -0
- package/dist/types/compute-engine/sequence.d.ts +120 -0
- package/dist/types/compute-engine/symbolic/antiderivative.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/derivative.d.ts +17 -2
- package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/fu-cost.d.ts +33 -0
- package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +130 -0
- package/dist/types/compute-engine/symbolic/fu.d.ts +48 -0
- package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +12 -0
- package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +15 -0
- package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +2 -0
- package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +14 -0
- package/dist/types/compute-engine/symbolic/simplify-log.d.ts +15 -0
- package/dist/types/compute-engine/symbolic/simplify-power.d.ts +15 -0
- package/dist/types/compute-engine/symbolic/simplify-product.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -11
- package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +1 -1
- package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +2 -0
- package/dist/types/compute-engine/tensor/tensor-fields.d.ts +1 -1
- package/dist/types/compute-engine/tensor/tensors.d.ts +3 -3
- package/dist/types/compute-engine/types.d.ts +1 -1
- package/dist/types/compute-engine.d.ts +5 -1
- package/dist/types/math-json/symbols.d.ts +1 -1
- package/dist/types/math-json/types.d.ts +1 -1
- package/dist/types/math-json/utils.d.ts +1 -1
- package/dist/types/math-json.d.ts +2 -2
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* 0.
|
|
1
|
+
/* 0.34.0 */
|
|
2
2
|
import type { OneOf } from '../common/one-of';
|
|
3
3
|
import type { Expression, MathJsonNumberObject, MathJsonStringObject, MathJsonFunctionObject, MathJsonSymbolObject, MathJsonSymbol, MathJsonDictionaryObject } from '../math-json';
|
|
4
4
|
import type { Type, TypeReference, TypeResolver, TypeString } from '../common/type/types';
|
|
@@ -107,7 +107,7 @@ export interface Tensor<DT extends TensorDataType> extends TensorData<DT> {
|
|
|
107
107
|
readonly isZero: boolean;
|
|
108
108
|
at(...indices: number[]): DataTypeMap[DT] | undefined;
|
|
109
109
|
diagonal(axis1?: number, axis2?: number): undefined | DataTypeMap[DT][];
|
|
110
|
-
trace(axis1?: number, axis2?: number): undefined | DataTypeMap[DT]
|
|
110
|
+
trace(axis1?: number, axis2?: number): undefined | DataTypeMap[DT] | Tensor<DT>;
|
|
111
111
|
reshape(...shape: number[]): Tensor<DT>;
|
|
112
112
|
slice(index: number): Tensor<DT>;
|
|
113
113
|
flatten(): DataTypeMap[DT][];
|
|
@@ -506,7 +506,7 @@ export interface BoxedExpression {
|
|
|
506
506
|
* Compute Engine or it may return a different value each time it is
|
|
507
507
|
* evaluated, even if the state of the Compute Engine is the same.
|
|
508
508
|
*
|
|
509
|
-
* As an example, the ["Add", 2, 3]` function expression is pure, but
|
|
509
|
+
* As an example, the `["Add", 2, 3]` function expression is pure, but
|
|
510
510
|
* the `["Random"]` function expression is not pure.
|
|
511
511
|
*
|
|
512
512
|
* For a function expression to be pure, the function itself (its operator)
|
|
@@ -615,7 +615,7 @@ export interface BoxedExpression {
|
|
|
615
615
|
* Return the value of this expression, if a number literal.
|
|
616
616
|
*
|
|
617
617
|
* Note it is possible for `expr.numericValue` to be `null`, and for
|
|
618
|
-
* `expr.
|
|
618
|
+
* `expr.is(0)` to be false. For example, when a symbol has been
|
|
619
619
|
* defined with an assumption.
|
|
620
620
|
*
|
|
621
621
|
* Conversely, `expr.isNumber` may be true even if `expr.numericValue` is
|
|
@@ -1242,6 +1242,30 @@ export interface BoxedExpression {
|
|
|
1242
1242
|
*
|
|
1243
1243
|
*/
|
|
1244
1244
|
simplify(options?: Partial<SimplifyOptions>): BoxedExpression;
|
|
1245
|
+
/**
|
|
1246
|
+
* Apply the Fu algorithm to simplify trigonometric expressions.
|
|
1247
|
+
*
|
|
1248
|
+
* The Fu algorithm is a systematic approach to trigonometric simplification
|
|
1249
|
+
* that uses transformation rules (TR1-TR22), combination transforms (CTR),
|
|
1250
|
+
* and rule lists (RL) to reduce the number of trigonometric functions.
|
|
1251
|
+
*
|
|
1252
|
+
* This is equivalent to calling `simplify({ strategy: 'fu' })` but is
|
|
1253
|
+
* more convenient for trig-heavy expressions.
|
|
1254
|
+
*
|
|
1255
|
+
* Reference: Fu, Hongguang, Xiuqin Zhong, and Zhenbing Zeng.
|
|
1256
|
+
* "Automated and readable simplification of trigonometric expressions."
|
|
1257
|
+
* Mathematical and Computer Modelling 44.11 (2006): 1169-1177.
|
|
1258
|
+
*
|
|
1259
|
+
* @example
|
|
1260
|
+
* ```typescript
|
|
1261
|
+
* ce.parse('\\sin(x)\\cos(x)').trigSimplify()
|
|
1262
|
+
* // => sin(2x)/2
|
|
1263
|
+
*
|
|
1264
|
+
* ce.parse('\\sin^2(x) + \\cos^2(x)').trigSimplify()
|
|
1265
|
+
* // => 1
|
|
1266
|
+
* ```
|
|
1267
|
+
*/
|
|
1268
|
+
trigSimplify(): BoxedExpression;
|
|
1245
1269
|
/**
|
|
1246
1270
|
* Expand the expression: distribute multiplications over additions,
|
|
1247
1271
|
* and expand powers.
|
|
@@ -1320,9 +1344,25 @@ export interface BoxedExpression {
|
|
|
1320
1344
|
* `options.fallback` is set to `false`. If it is set to `false`, the
|
|
1321
1345
|
* function will throw an error if it cannot be compiled.
|
|
1322
1346
|
*
|
|
1347
|
+
* **Custom operators**: You can override operators to use function calls
|
|
1348
|
+
* instead of native operators, useful for vector/matrix operations:
|
|
1349
|
+
*
|
|
1350
|
+
* ```javascript
|
|
1351
|
+
* const expr = ce.parse("v + w");
|
|
1352
|
+
* const f = expr.compile({
|
|
1353
|
+
* operators: {
|
|
1354
|
+
* Add: ['add', 11], // Convert + to add()
|
|
1355
|
+
* Multiply: ['mul', 12] // Convert * to mul()
|
|
1356
|
+
* }
|
|
1357
|
+
* });
|
|
1358
|
+
* // Result: add(v, w) instead of v + w
|
|
1359
|
+
* ```
|
|
1360
|
+
*
|
|
1323
1361
|
*/
|
|
1324
1362
|
compile(options?: {
|
|
1325
|
-
to?:
|
|
1363
|
+
to?: string;
|
|
1364
|
+
target?: any;
|
|
1365
|
+
operators?: Partial<Record<MathJsonSymbol, [op: string, prec: number]>> | ((op: MathJsonSymbol) => [op: string, prec: number] | undefined);
|
|
1326
1366
|
functions?: Record<MathJsonSymbol, JSSource | ((...any: any[]) => any)>;
|
|
1327
1367
|
vars?: Record<MathJsonSymbol, JSSource>;
|
|
1328
1368
|
imports?: ((...any: any[]) => any)[];
|
|
@@ -1335,15 +1375,27 @@ export interface BoxedExpression {
|
|
|
1335
1375
|
* If this is an equation, solve the equation for the variables in vars.
|
|
1336
1376
|
* Otherwise, solve the equation `this = 0` for the variables in vars.
|
|
1337
1377
|
*
|
|
1378
|
+
* For univariate equations, returns an array of solutions (roots).
|
|
1379
|
+
* For systems of linear equations (List of Equal expressions), returns
|
|
1380
|
+
* an object mapping variable names to their values.
|
|
1381
|
+
* For non-linear polynomial systems (like xy=6, x+y=5), returns an array
|
|
1382
|
+
* of solution objects (multiple solutions possible).
|
|
1338
1383
|
*
|
|
1339
1384
|
* ```javascript
|
|
1385
|
+
* // Univariate equation
|
|
1340
1386
|
* const expr = ce.parse("x^2 + 2*x + 1 = 0");
|
|
1341
|
-
* console.log(expr.solve("x"));
|
|
1342
|
-
* ```
|
|
1387
|
+
* console.log(expr.solve("x")); // Returns array of roots
|
|
1343
1388
|
*
|
|
1389
|
+
* // System of linear equations
|
|
1390
|
+
* const system = ce.parse("\\begin{cases}x+y=70\\\\2x-4y=80\\end{cases}");
|
|
1391
|
+
* console.log(system.solve(["x", "y"])); // Returns { x: 60, y: 10 }
|
|
1344
1392
|
*
|
|
1393
|
+
* // Non-linear polynomial system (product + sum)
|
|
1394
|
+
* const nonlinear = ce.parse("\\begin{cases}xy=6\\\\x+y=5\\end{cases}");
|
|
1395
|
+
* console.log(nonlinear.solve(["x", "y"])); // Returns [{ x: 2, y: 3 }, { x: 3, y: 2 }]
|
|
1396
|
+
* ```
|
|
1345
1397
|
*/
|
|
1346
|
-
solve(vars?: Iterable<string> | string | BoxedExpression | Iterable<BoxedExpression>): null | ReadonlyArray<BoxedExpression
|
|
1398
|
+
solve(vars?: Iterable<string> | string | BoxedExpression | Iterable<BoxedExpression>): null | ReadonlyArray<BoxedExpression> | Record<string, BoxedExpression> | Array<Record<string, BoxedExpression>>;
|
|
1347
1399
|
/**
|
|
1348
1400
|
* If this expression is a number literal, a string literal or a function
|
|
1349
1401
|
* literal, return the expression.
|
|
@@ -1809,7 +1861,194 @@ export type ValueDefinition = BaseDefinition & {
|
|
|
1809
1861
|
neq: (a: BoxedExpression) => boolean | undefined;
|
|
1810
1862
|
cmp: (a: BoxedExpression) => '=' | '>' | '<' | undefined;
|
|
1811
1863
|
collection: CollectionHandlers;
|
|
1864
|
+
/**
|
|
1865
|
+
* Custom evaluation handler for subscripted expressions of this symbol.
|
|
1866
|
+
* Called when evaluating `Subscript(symbol, index)`.
|
|
1867
|
+
*
|
|
1868
|
+
* @param subscript - The subscript expression (already evaluated)
|
|
1869
|
+
* @param options - Contains the compute engine and evaluation options
|
|
1870
|
+
* @returns The evaluated result, or `undefined` to fall back to symbolic form
|
|
1871
|
+
*/
|
|
1872
|
+
subscriptEvaluate?: (subscript: BoxedExpression, options: {
|
|
1873
|
+
engine: ComputeEngine;
|
|
1874
|
+
numericApproximation?: boolean;
|
|
1875
|
+
}) => BoxedExpression | undefined;
|
|
1812
1876
|
};
|
|
1877
|
+
/**
|
|
1878
|
+
* Definition for a sequence declared with `ce.declareSequence()`.
|
|
1879
|
+
*
|
|
1880
|
+
* A sequence is defined by base cases and a recurrence relation.
|
|
1881
|
+
*
|
|
1882
|
+
* @example
|
|
1883
|
+
* ```typescript
|
|
1884
|
+
* // Fibonacci sequence
|
|
1885
|
+
* ce.declareSequence('F', {
|
|
1886
|
+
* base: { 0: 0, 1: 1 },
|
|
1887
|
+
* recurrence: 'F_{n-1} + F_{n-2}',
|
|
1888
|
+
* });
|
|
1889
|
+
* ce.parse('F_{10}').evaluate(); // → 55
|
|
1890
|
+
* ```
|
|
1891
|
+
*
|
|
1892
|
+
* @category Definitions
|
|
1893
|
+
*/
|
|
1894
|
+
export interface SequenceDefinition {
|
|
1895
|
+
/**
|
|
1896
|
+
* Index variable name for single-index sequences, default 'n'.
|
|
1897
|
+
* For multi-index sequences, use `variables` instead.
|
|
1898
|
+
*/
|
|
1899
|
+
variable?: string;
|
|
1900
|
+
/**
|
|
1901
|
+
* Index variable names for multi-index sequences.
|
|
1902
|
+
* Example: `['n', 'k']` for Pascal's triangle P_{n,k}
|
|
1903
|
+
*
|
|
1904
|
+
* If provided, this takes precedence over `variable`.
|
|
1905
|
+
*/
|
|
1906
|
+
variables?: string[];
|
|
1907
|
+
/**
|
|
1908
|
+
* Base cases as index → value mapping.
|
|
1909
|
+
*
|
|
1910
|
+
* For single-index sequences, use numeric keys:
|
|
1911
|
+
* ```typescript
|
|
1912
|
+
* base: { 0: 0, 1: 1 } // F_0 = 0, F_1 = 1
|
|
1913
|
+
* ```
|
|
1914
|
+
*
|
|
1915
|
+
* For multi-index sequences, use comma-separated string keys:
|
|
1916
|
+
* ```typescript
|
|
1917
|
+
* base: {
|
|
1918
|
+
* '0,0': 1, // Exact: P_{0,0} = 1
|
|
1919
|
+
* 'n,0': 1, // Pattern: P_{n,0} = 1 for all n
|
|
1920
|
+
* 'n,n': 1, // Pattern: P_{n,n} = 1 (diagonal)
|
|
1921
|
+
* }
|
|
1922
|
+
* ```
|
|
1923
|
+
*
|
|
1924
|
+
* Pattern keys use variable names to match any value. When the same
|
|
1925
|
+
* variable appears multiple times (e.g., 'n,n'), the indices must be equal.
|
|
1926
|
+
*/
|
|
1927
|
+
base: Record<number | string, number | BoxedExpression>;
|
|
1928
|
+
/** Recurrence relation as LaTeX string or BoxedExpression */
|
|
1929
|
+
recurrence: string | BoxedExpression;
|
|
1930
|
+
/** Whether to memoize computed values (default: true) */
|
|
1931
|
+
memoize?: boolean;
|
|
1932
|
+
/**
|
|
1933
|
+
* Valid index domain constraints.
|
|
1934
|
+
*
|
|
1935
|
+
* For single-index sequences:
|
|
1936
|
+
* ```typescript
|
|
1937
|
+
* domain: { min: 0, max: 100 }
|
|
1938
|
+
* ```
|
|
1939
|
+
*
|
|
1940
|
+
* For multi-index sequences, use per-variable constraints:
|
|
1941
|
+
* ```typescript
|
|
1942
|
+
* domain: { n: { min: 0 }, k: { min: 0 } }
|
|
1943
|
+
* ```
|
|
1944
|
+
*/
|
|
1945
|
+
domain?: {
|
|
1946
|
+
min?: number;
|
|
1947
|
+
max?: number;
|
|
1948
|
+
} | Record<string, {
|
|
1949
|
+
min?: number;
|
|
1950
|
+
max?: number;
|
|
1951
|
+
}>;
|
|
1952
|
+
/**
|
|
1953
|
+
* Constraint expression for multi-index sequences.
|
|
1954
|
+
* The expression should evaluate to a boolean/numeric value.
|
|
1955
|
+
* If it evaluates to false or 0, the subscript is considered out of domain.
|
|
1956
|
+
*
|
|
1957
|
+
* Example: `'k <= n'` for Pascal's triangle (only valid when k ≤ n)
|
|
1958
|
+
*/
|
|
1959
|
+
constraints?: string | BoxedExpression;
|
|
1960
|
+
}
|
|
1961
|
+
/**
|
|
1962
|
+
* Status of a sequence definition.
|
|
1963
|
+
* @category Definitions
|
|
1964
|
+
*/
|
|
1965
|
+
export interface SequenceStatus {
|
|
1966
|
+
/**
|
|
1967
|
+
* Status of the sequence:
|
|
1968
|
+
* - 'complete': Both base case(s) and recurrence defined
|
|
1969
|
+
* - 'pending': Waiting for base case(s) or recurrence
|
|
1970
|
+
* - 'not-a-sequence': Symbol is not a sequence
|
|
1971
|
+
*/
|
|
1972
|
+
status: 'complete' | 'pending' | 'not-a-sequence';
|
|
1973
|
+
/** Whether at least one base case is defined */
|
|
1974
|
+
hasBase: boolean;
|
|
1975
|
+
/** Whether a recurrence relation is defined */
|
|
1976
|
+
hasRecurrence: boolean;
|
|
1977
|
+
/**
|
|
1978
|
+
* Keys of defined base cases.
|
|
1979
|
+
* For single-index: numeric indices (e.g., [0, 1])
|
|
1980
|
+
* For multi-index: string keys including patterns (e.g., ['0,0', 'n,0', 'n,n'])
|
|
1981
|
+
*/
|
|
1982
|
+
baseIndices: (number | string)[];
|
|
1983
|
+
/** Index variable name if recurrence is defined (single-index) */
|
|
1984
|
+
variable?: string;
|
|
1985
|
+
/** Index variable names if recurrence is defined (multi-index) */
|
|
1986
|
+
variables?: string[];
|
|
1987
|
+
}
|
|
1988
|
+
/**
|
|
1989
|
+
* Information about a defined sequence for introspection.
|
|
1990
|
+
* @category Definitions
|
|
1991
|
+
*/
|
|
1992
|
+
export interface SequenceInfo {
|
|
1993
|
+
/** The sequence name */
|
|
1994
|
+
name: string;
|
|
1995
|
+
/** Index variable name for single-index sequences (e.g., `"n"`) */
|
|
1996
|
+
variable?: string;
|
|
1997
|
+
/** Index variable names for multi-index sequences (e.g., `["n", "k"]`) */
|
|
1998
|
+
variables?: string[];
|
|
1999
|
+
/**
|
|
2000
|
+
* Base case keys.
|
|
2001
|
+
* For single-index: numeric indices
|
|
2002
|
+
* For multi-index: string keys including patterns
|
|
2003
|
+
*/
|
|
2004
|
+
baseIndices: (number | string)[];
|
|
2005
|
+
/** Whether memoization is enabled */
|
|
2006
|
+
memoize: boolean;
|
|
2007
|
+
/**
|
|
2008
|
+
* Domain constraints.
|
|
2009
|
+
* For single-index: `{ min?, max? }`
|
|
2010
|
+
* For multi-index: per-variable constraints
|
|
2011
|
+
*/
|
|
2012
|
+
domain: {
|
|
2013
|
+
min?: number;
|
|
2014
|
+
max?: number;
|
|
2015
|
+
} | Record<string, {
|
|
2016
|
+
min?: number;
|
|
2017
|
+
max?: number;
|
|
2018
|
+
}>;
|
|
2019
|
+
/** Number of cached values */
|
|
2020
|
+
cacheSize: number;
|
|
2021
|
+
/** Whether this is a multi-index sequence */
|
|
2022
|
+
isMultiIndex: boolean;
|
|
2023
|
+
}
|
|
2024
|
+
/**
|
|
2025
|
+
* Result from an OEIS lookup operation.
|
|
2026
|
+
* @category OEIS
|
|
2027
|
+
*/
|
|
2028
|
+
export interface OEISSequenceInfo {
|
|
2029
|
+
/** OEIS sequence ID (e.g., 'A000045') */
|
|
2030
|
+
id: string;
|
|
2031
|
+
/** Sequence name/description */
|
|
2032
|
+
name: string;
|
|
2033
|
+
/** First several terms of the sequence */
|
|
2034
|
+
terms: number[];
|
|
2035
|
+
/** Formula or recurrence (if available) */
|
|
2036
|
+
formula?: string;
|
|
2037
|
+
/** Comments about the sequence */
|
|
2038
|
+
comments?: string[];
|
|
2039
|
+
/** URL to the OEIS page */
|
|
2040
|
+
url: string;
|
|
2041
|
+
}
|
|
2042
|
+
/**
|
|
2043
|
+
* Options for OEIS operations.
|
|
2044
|
+
* @category OEIS
|
|
2045
|
+
*/
|
|
2046
|
+
export interface OEISOptions {
|
|
2047
|
+
/** Request timeout in milliseconds (default: 10000) */
|
|
2048
|
+
timeout?: number;
|
|
2049
|
+
/** Maximum number of results to return for lookups (default: 5) */
|
|
2050
|
+
maxResults?: number;
|
|
2051
|
+
}
|
|
1813
2052
|
/**
|
|
1814
2053
|
* Definition record for a function.
|
|
1815
2054
|
* @category Definitions
|
|
@@ -2041,6 +2280,21 @@ export type SimplifyOptions = {
|
|
|
2041
2280
|
* used.
|
|
2042
2281
|
*/
|
|
2043
2282
|
costFunction?: (expr: BoxedExpression) => number;
|
|
2283
|
+
/**
|
|
2284
|
+
* The simplification strategy to use.
|
|
2285
|
+
*
|
|
2286
|
+
* - `'default'`: Use standard simplification rules (default)
|
|
2287
|
+
* - `'fu'`: Use the Fu algorithm for trigonometric simplification.
|
|
2288
|
+
* This is more aggressive for trig expressions and may produce
|
|
2289
|
+
* different results than the default strategy.
|
|
2290
|
+
*
|
|
2291
|
+
* **Note:** When using the `'fu'` strategy, the `costFunction` and `rules`
|
|
2292
|
+
* options are ignored. The Fu algorithm uses its own specialized cost
|
|
2293
|
+
* function that prioritizes minimizing the number of trigonometric
|
|
2294
|
+
* functions. Standard simplification is applied before and after the
|
|
2295
|
+
* Fu transformations using the engine's default rules.
|
|
2296
|
+
*/
|
|
2297
|
+
strategy?: 'default' | 'fu';
|
|
2044
2298
|
};
|
|
2045
2299
|
/**
|
|
2046
2300
|
* A table mapping symbols to their definition.
|
|
@@ -2277,6 +2531,14 @@ export interface BoxedValueDefinition extends BoxedBaseDefinition {
|
|
|
2277
2531
|
*/
|
|
2278
2532
|
inferredType: boolean;
|
|
2279
2533
|
type: BoxedType;
|
|
2534
|
+
/**
|
|
2535
|
+
* Custom evaluation handler for subscripted expressions of this symbol.
|
|
2536
|
+
* Called when evaluating `Subscript(symbol, index)`.
|
|
2537
|
+
*/
|
|
2538
|
+
subscriptEvaluate?: (subscript: BoxedExpression, options: {
|
|
2539
|
+
engine: ComputeEngine;
|
|
2540
|
+
numericApproximation?: boolean;
|
|
2541
|
+
}) => BoxedExpression | undefined;
|
|
2280
2542
|
}
|
|
2281
2543
|
/**
|
|
2282
2544
|
* An operator definition can have some flags to indicate specific
|
|
@@ -2577,7 +2839,7 @@ export type RuleSteps = RuleStep[];
|
|
|
2577
2839
|
* into a new expression `replace`.
|
|
2578
2840
|
*
|
|
2579
2841
|
* - `x-1` \( \to \) `1-x`
|
|
2580
|
-
* - `(x+1)(x-1)` \( \to \) `x^2-1
|
|
2842
|
+
* - `(x+1)(x-1)` \( \to \) `x^2-1`
|
|
2581
2843
|
*
|
|
2582
2844
|
* The patterns can be expressed as LaTeX strings or `SemiBoxedExpression`'s.
|
|
2583
2845
|
* Alternatively, match/replace logic may be specified by a `RuleFunction`, allowing both custom
|
|
@@ -2757,6 +3019,8 @@ export interface ComputeEngine extends IBigNum {
|
|
|
2757
3019
|
readonly context: EvalContext;
|
|
2758
3020
|
contextStack: ReadonlyArray<EvalContext>;
|
|
2759
3021
|
/** @internal */
|
|
3022
|
+
readonly isVerifying: boolean;
|
|
3023
|
+
/** @internal */
|
|
2760
3024
|
readonly _typeResolver: TypeResolver;
|
|
2761
3025
|
/** Absolute time beyond which evaluation should not proceed
|
|
2762
3026
|
* @internal
|
|
@@ -2890,6 +3154,11 @@ export interface ComputeEngine extends IBigNum {
|
|
|
2890
3154
|
* Use `ce.assign(id, value)` instead.
|
|
2891
3155
|
* @internal */
|
|
2892
3156
|
_setSymbolValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
|
|
3157
|
+
/**
|
|
3158
|
+
* Set a value directly in the current context's values map.
|
|
3159
|
+
* Used for assumptions so values are properly scoped.
|
|
3160
|
+
* @internal */
|
|
3161
|
+
_setCurrentContextValue(id: MathJsonSymbol, value: BoxedExpression | boolean | number | undefined): void;
|
|
2893
3162
|
/** A list of the function calls to the current evaluation context */
|
|
2894
3163
|
trace: ReadonlyArray<string>;
|
|
2895
3164
|
lookupContext(id: MathJsonSymbol): undefined | EvalContext;
|
|
@@ -2914,9 +3183,111 @@ export interface ComputeEngine extends IBigNum {
|
|
|
2914
3183
|
[id: MathJsonSymbol]: Type | TypeString | Partial<SymbolDefinition>;
|
|
2915
3184
|
}, arg2?: Type | TypeString | Partial<SymbolDefinition>, arg3?: Scope): ComputeEngine;
|
|
2916
3185
|
assume(predicate: BoxedExpression): AssumeResult;
|
|
3186
|
+
/**
|
|
3187
|
+
* Declare a sequence with a recurrence relation.
|
|
3188
|
+
*
|
|
3189
|
+
* @example
|
|
3190
|
+
* ```typescript
|
|
3191
|
+
* // Fibonacci sequence
|
|
3192
|
+
* ce.declareSequence('F', {
|
|
3193
|
+
* base: { 0: 0, 1: 1 },
|
|
3194
|
+
* recurrence: 'F_{n-1} + F_{n-2}',
|
|
3195
|
+
* });
|
|
3196
|
+
* ce.parse('F_{10}').evaluate(); // → 55
|
|
3197
|
+
* ```
|
|
3198
|
+
*/
|
|
3199
|
+
declareSequence(name: string, def: SequenceDefinition): ComputeEngine;
|
|
3200
|
+
/**
|
|
3201
|
+
* Get the status of a sequence definition.
|
|
3202
|
+
*
|
|
3203
|
+
* @example
|
|
3204
|
+
* ```typescript
|
|
3205
|
+
* ce.parse('F_0 := 0').evaluate();
|
|
3206
|
+
* ce.getSequenceStatus('F');
|
|
3207
|
+
* // → { status: 'pending', hasBase: true, hasRecurrence: false, baseIndices: [0] }
|
|
3208
|
+
* ```
|
|
3209
|
+
*/
|
|
3210
|
+
getSequenceStatus(name: string): SequenceStatus;
|
|
3211
|
+
/**
|
|
3212
|
+
* Get information about a defined sequence.
|
|
3213
|
+
* Returns `undefined` if the symbol is not a sequence.
|
|
3214
|
+
*/
|
|
3215
|
+
getSequence(name: string): SequenceInfo | undefined;
|
|
3216
|
+
/**
|
|
3217
|
+
* List all defined sequences.
|
|
3218
|
+
* Returns an array of sequence names.
|
|
3219
|
+
*/
|
|
3220
|
+
listSequences(): string[];
|
|
3221
|
+
/**
|
|
3222
|
+
* Check if a symbol is a defined sequence.
|
|
3223
|
+
*/
|
|
3224
|
+
isSequence(name: string): boolean;
|
|
3225
|
+
/**
|
|
3226
|
+
* Clear the memoization cache for a sequence.
|
|
3227
|
+
* If no name is provided, clears caches for all sequences.
|
|
3228
|
+
*/
|
|
3229
|
+
clearSequenceCache(name?: string): void;
|
|
3230
|
+
/**
|
|
3231
|
+
* Get the memoization cache for a sequence.
|
|
3232
|
+
* Returns a Map of index → value, or `undefined` if not a sequence or memoization is disabled.
|
|
3233
|
+
*
|
|
3234
|
+
* For single-index sequences, keys are numbers.
|
|
3235
|
+
* For multi-index sequences, keys are comma-separated strings (e.g., '5,2').
|
|
3236
|
+
*/
|
|
3237
|
+
getSequenceCache(name: string): Map<number | string, BoxedExpression> | undefined;
|
|
3238
|
+
/**
|
|
3239
|
+
* Generate a list of sequence terms from start to end (inclusive).
|
|
3240
|
+
*
|
|
3241
|
+
* @param name - The sequence name
|
|
3242
|
+
* @param start - Starting index (inclusive)
|
|
3243
|
+
* @param end - Ending index (inclusive)
|
|
3244
|
+
* @param step - Step size (default: 1)
|
|
3245
|
+
* @returns Array of BoxedExpressions, or undefined if not a sequence
|
|
3246
|
+
*
|
|
3247
|
+
* @example
|
|
3248
|
+
* ```typescript
|
|
3249
|
+
* ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
|
|
3250
|
+
* ce.getSequenceTerms('F', 0, 10);
|
|
3251
|
+
* // → [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
|
3252
|
+
* ```
|
|
3253
|
+
*/
|
|
3254
|
+
getSequenceTerms(name: string, start: number, end: number, step?: number): BoxedExpression[] | undefined;
|
|
3255
|
+
/**
|
|
3256
|
+
* Look up sequences in OEIS by their terms.
|
|
3257
|
+
*
|
|
3258
|
+
* @param terms - Array of sequence terms to search for
|
|
3259
|
+
* @param options - Optional configuration (timeout, maxResults)
|
|
3260
|
+
* @returns Promise resolving to array of matching sequences
|
|
3261
|
+
*
|
|
3262
|
+
* @example
|
|
3263
|
+
* ```typescript
|
|
3264
|
+
* const results = await ce.lookupOEIS([0, 1, 1, 2, 3, 5, 8, 13]);
|
|
3265
|
+
* // → [{ id: 'A000045', name: 'Fibonacci numbers', ... }]
|
|
3266
|
+
* ```
|
|
3267
|
+
*/
|
|
3268
|
+
lookupOEIS(terms: (number | BoxedExpression)[], options?: OEISOptions): Promise<OEISSequenceInfo[]>;
|
|
3269
|
+
/**
|
|
3270
|
+
* Check if a defined sequence matches an OEIS sequence.
|
|
3271
|
+
*
|
|
3272
|
+
* @param name - Name of the defined sequence
|
|
3273
|
+
* @param count - Number of terms to check (default: 10)
|
|
3274
|
+
* @param options - Optional configuration
|
|
3275
|
+
* @returns Promise with match results including OEIS matches and generated terms
|
|
3276
|
+
*
|
|
3277
|
+
* @example
|
|
3278
|
+
* ```typescript
|
|
3279
|
+
* ce.declareSequence('F', { base: { 0: 0, 1: 1 }, recurrence: 'F_{n-1} + F_{n-2}' });
|
|
3280
|
+
* const result = await ce.checkSequenceOEIS('F', 10);
|
|
3281
|
+
* // → { matches: [{ id: 'A000045', name: 'Fibonacci numbers', ... }], terms: [0, 1, 1, ...] }
|
|
3282
|
+
* ```
|
|
3283
|
+
*/
|
|
3284
|
+
checkSequenceOEIS(name: string, count?: number, options?: OEISOptions): Promise<{
|
|
3285
|
+
matches: OEISSequenceInfo[];
|
|
3286
|
+
terms: number[];
|
|
3287
|
+
}>;
|
|
2917
3288
|
forget(symbol?: MathJsonSymbol | MathJsonSymbol[]): void;
|
|
2918
3289
|
ask(pattern: BoxedExpression): BoxedSubstitution[];
|
|
2919
|
-
verify(query: BoxedExpression): boolean;
|
|
3290
|
+
verify(query: BoxedExpression): boolean | undefined;
|
|
2920
3291
|
/** @internal */
|
|
2921
3292
|
_shouldContinueExecution(): boolean;
|
|
2922
3293
|
/** @internal */
|