@cortex-js/compute-engine 0.35.6 → 0.50.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.
- package/README.md +121 -5
- package/dist/compute-engine.esm.js +55594 -51135
- package/dist/compute-engine.min.esm.js +1081 -78
- package/dist/compute-engine.min.umd.js +1081 -78
- package/dist/compute-engine.umd.js +53638 -49162
- package/dist/math-json.esm.js +2 -2
- package/dist/math-json.min.esm.js +2 -2
- 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 +1 -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 +8 -1
- package/dist/types/common/type/primitive.d.ts +2 -1
- package/dist/types/common/type/reduce.d.ts +1 -1
- 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 +3 -2
- package/dist/types/common/utils.d.ts +1 -1
- package/dist/types/compute-engine/assume.d.ts +4 -22
- package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +66 -76
- package/dist/types/compute-engine/boxed-expression/apply.d.ts +4 -4
- package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +15 -6
- package/dist/types/compute-engine/boxed-expression/arithmetic-mul-div.d.ts +72 -6
- package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +8 -8
- package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +5 -8
- package/dist/types/compute-engine/boxed-expression/box.d.ts +63 -8
- package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +13 -12
- package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +44 -43
- package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +31 -30
- package/dist/types/compute-engine/boxed-expression/boxed-operator-definition.d.ts +18 -18
- package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +5 -37
- package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +5 -4
- package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +34 -33
- package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +34 -33
- package/dist/types/compute-engine/boxed-expression/boxed-value-definition.d.ts +8 -8
- package/dist/types/compute-engine/boxed-expression/cache.d.ts +1 -1
- package/dist/types/compute-engine/boxed-expression/canonical-utils.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/compare.d.ts +7 -4
- package/dist/types/compute-engine/boxed-expression/constants.d.ts +6 -0
- package/dist/types/compute-engine/boxed-expression/expand.d.ts +4 -5
- package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +9 -9
- package/dist/types/compute-engine/boxed-expression/factor.d.ts +7 -7
- package/dist/types/compute-engine/boxed-expression/flatten.d.ts +4 -13
- package/dist/types/compute-engine/boxed-expression/hold.d.ts +3 -3
- package/dist/types/compute-engine/boxed-expression/inequality-bounds.d.ts +21 -0
- package/dist/types/compute-engine/boxed-expression/init-lazy-refs.d.ts +1 -0
- package/dist/types/compute-engine/boxed-expression/invisible-operator.d.ts +4 -0
- package/dist/types/compute-engine/boxed-expression/match.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/negate.d.ts +4 -4
- package/dist/types/compute-engine/boxed-expression/numerics.d.ts +8 -8
- package/dist/types/compute-engine/boxed-expression/order.d.ts +14 -14
- package/dist/types/compute-engine/boxed-expression/pattern-utils.d.ts +41 -0
- package/dist/types/compute-engine/boxed-expression/polynomial-degree.d.ts +18 -0
- package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +12 -35
- package/dist/types/compute-engine/boxed-expression/predicates.d.ts +2 -0
- package/dist/types/compute-engine/boxed-expression/product.d.ts +1 -66
- package/dist/types/compute-engine/boxed-expression/rules.d.ts +51 -51
- package/dist/types/compute-engine/boxed-expression/serialize.d.ts +6 -3
- package/dist/types/compute-engine/boxed-expression/sgn.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/simplify.d.ts +2 -2
- package/dist/types/compute-engine/boxed-expression/solve-linear-system.d.ts +8 -8
- package/dist/types/compute-engine/boxed-expression/solve.d.ts +3 -11
- package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +7 -7
- package/dist/types/compute-engine/boxed-expression/type-guards.d.ts +31 -0
- package/dist/types/compute-engine/boxed-expression/utils.d.ts +13 -13
- package/dist/types/compute-engine/boxed-expression/validate.d.ts +10 -10
- package/dist/types/compute-engine/collection-utils.d.ts +4 -4
- package/dist/types/compute-engine/compilation/base-compiler.d.ts +3 -3
- package/dist/types/compute-engine/compilation/compile-expression.d.ts +25 -0
- package/dist/types/compute-engine/compilation/glsl-target.d.ts +12 -41
- package/dist/types/compute-engine/compilation/gpu-target.d.ts +50 -0
- package/dist/types/compute-engine/compilation/interval-glsl-target.d.ts +11 -11
- package/dist/types/compute-engine/compilation/interval-javascript-target.d.ts +7 -7
- package/dist/types/compute-engine/compilation/interval-wgsl-target.d.ts +54 -0
- package/dist/types/compute-engine/compilation/javascript-target.d.ts +6 -6
- package/dist/types/compute-engine/compilation/python-target.d.ts +61 -0
- package/dist/types/compute-engine/compilation/types.d.ts +38 -22
- package/dist/types/compute-engine/compilation/wgsl-target.d.ts +41 -0
- package/dist/types/compute-engine/cost-function.d.ts +3 -3
- package/dist/types/compute-engine/engine-assumptions.d.ts +6 -0
- package/dist/types/compute-engine/engine-cache.d.ts +6 -0
- package/dist/types/compute-engine/engine-common-symbols.d.ts +6 -0
- package/dist/types/compute-engine/engine-compilation-targets.d.ts +17 -0
- package/dist/types/compute-engine/engine-configuration-lifecycle.d.ts +15 -0
- package/dist/types/compute-engine/engine-declarations.d.ts +19 -0
- package/dist/types/compute-engine/engine-expression-entrypoints.d.ts +44 -0
- package/dist/types/compute-engine/engine-extension-contracts.d.ts +9 -0
- package/dist/types/compute-engine/engine-latex-dictionary-state.d.ts +13 -0
- package/dist/types/compute-engine/engine-library-bootstrap.d.ts +6 -0
- package/dist/types/compute-engine/engine-numeric-configuration.d.ts +34 -0
- package/dist/types/compute-engine/engine-parse-entrypoint.d.ts +7 -0
- package/dist/types/compute-engine/engine-runtime-state.d.ts +19 -0
- package/dist/types/compute-engine/engine-scope.d.ts +13 -0
- package/dist/types/compute-engine/engine-sequences.d.ts +14 -0
- package/dist/types/compute-engine/engine-simplification-rules.d.ts +15 -0
- package/dist/types/compute-engine/engine-startup-coordinator.d.ts +28 -0
- package/dist/types/compute-engine/engine-type-resolver.d.ts +12 -0
- package/dist/types/compute-engine/engine-validation-entrypoints.d.ts +13 -0
- package/dist/types/compute-engine/free-functions.d.ts +20 -0
- package/dist/types/compute-engine/function-utils.d.ts +10 -10
- package/dist/types/compute-engine/global-types.d.ts +5 -3301
- package/dist/types/compute-engine/index.d.ts +156 -112
- package/dist/types/compute-engine/interval/arithmetic.d.ts +1 -1
- package/dist/types/compute-engine/interval/comparison.d.ts +1 -1
- package/dist/types/compute-engine/interval/elementary.d.ts +1 -1
- package/dist/types/compute-engine/interval/index.d.ts +1 -1
- package/dist/types/compute-engine/interval/trigonometric.d.ts +1 -1
- package/dist/types/compute-engine/interval/types.d.ts +1 -1
- package/dist/types/compute-engine/interval/util.d.ts +1 -1
- 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 +4 -80
- package/dist/types/compute-engine/latex-syntax/dictionary/indexed-types.d.ts +83 -0
- package/dist/types/compute-engine/latex-syntax/parse-number.d.ts +28 -0
- package/dist/types/compute-engine/latex-syntax/parse-symbol.d.ts +2 -2
- package/dist/types/compute-engine/latex-syntax/parse.d.ts +23 -33
- package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +2 -2
- package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +8 -8
- package/dist/types/compute-engine/latex-syntax/serializer.d.ts +15 -15
- package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
- package/dist/types/compute-engine/latex-syntax/types.d.ts +73 -53
- package/dist/types/compute-engine/latex-syntax/utils.d.ts +1 -1
- package/dist/types/compute-engine/library/arithmetic.d.ts +1 -2
- package/dist/types/compute-engine/library/calculus.d.ts +1 -1
- package/dist/types/compute-engine/library/collections.d.ts +4 -4
- 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/library.d.ts +19 -6
- package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
- package/dist/types/compute-engine/library/logic-analysis.d.ts +19 -32
- package/dist/types/compute-engine/library/logic.d.ts +1 -5
- 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 +2 -2
- 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/type-handlers.d.ts +4 -0
- package/dist/types/compute-engine/library/utils.d.ts +12 -12
- package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +5 -5
- package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +5 -5
- package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +5 -5
- package/dist/types/compute-engine/numeric-value/types.d.ts +2 -3
- package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
- package/dist/types/compute-engine/numerics/expression.d.ts +3 -3
- package/dist/types/compute-engine/numerics/interval.d.ts +2 -2
- 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 +1 -1
- package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
- package/dist/types/compute-engine/numerics/rationals.d.ts +4 -4
- package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
- package/dist/types/compute-engine/numerics/special-functions.d.ts +120 -1
- package/dist/types/compute-engine/numerics/statistics.d.ts +1 -2
- package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
- package/dist/types/compute-engine/numerics/types.d.ts +3 -1
- package/dist/types/compute-engine/oeis.d.ts +3 -3
- package/dist/types/compute-engine/sequence.d.ts +13 -13
- package/dist/types/compute-engine/symbolic/antiderivative.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/derivative.d.ts +3 -3
- package/dist/types/compute-engine/symbolic/distribute.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/fu-cost.d.ts +6 -6
- package/dist/types/compute-engine/symbolic/fu-transforms.d.ts +46 -46
- package/dist/types/compute-engine/symbolic/fu.d.ts +4 -4
- package/dist/types/compute-engine/symbolic/logic-utils.d.ts +54 -0
- package/dist/types/compute-engine/symbolic/simplify-abs.d.ts +4 -4
- package/dist/types/compute-engine/symbolic/simplify-divide.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-hyperbolic.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-infinity.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-log.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-logic.d.ts +5 -0
- package/dist/types/compute-engine/symbolic/simplify-power.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-product.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +65 -4
- package/dist/types/compute-engine/symbolic/simplify-sum.d.ts +2 -2
- package/dist/types/compute-engine/symbolic/simplify-trig.d.ts +2 -2
- package/dist/types/compute-engine/tensor/tensor-fields.d.ts +42 -42
- package/dist/types/compute-engine/tensor/tensors.d.ts +4 -4
- package/dist/types/compute-engine/types-definitions.d.ts +900 -0
- package/dist/types/compute-engine/types-engine.d.ts +375 -0
- package/dist/types/compute-engine/types-evaluation.d.ts +67 -0
- package/dist/types/compute-engine/types-expression.d.ts +1655 -0
- package/dist/types/compute-engine/types-kernel-evaluation.d.ts +164 -0
- package/dist/types/compute-engine/types-kernel-serialization.d.ts +147 -0
- package/dist/types/compute-engine/types-serialization.d.ts +23 -0
- package/dist/types/compute-engine/types.d.ts +2 -2
- package/dist/types/compute-engine.d.ts +16 -3
- package/dist/types/math-json/symbols.d.ts +1 -1
- package/dist/types/math-json/types.d.ts +3 -3
- package/dist/types/math-json/utils.d.ts +31 -31
- package/dist/types/math-json.d.ts +2 -2
- package/package.json +8 -1
- package/dist/types/compute-engine/boxed-expression/terms.d.ts +0 -10
- package/dist/types/compute-engine/library/invisible-operator.d.ts +0 -4
- package/dist/types/compute-engine/library/logic-utils.d.ts +0 -54
|
@@ -0,0 +1,1655 @@
|
|
|
1
|
+
/* 0.50.1 */
|
|
2
|
+
import type { OneOf } from '../common/one-of';
|
|
3
|
+
import type { MathJsonExpression, MathJsonNumberObject, MathJsonStringObject, MathJsonSymbolObject, MathJsonFunctionObject, MathJsonSymbol, MathJsonDictionaryObject } from '../math-json';
|
|
4
|
+
import type { Type, TypeString } from '../common/type/types';
|
|
5
|
+
import type { BoxedType } from '../common/type/boxed-type';
|
|
6
|
+
import type { NumericValue } from './numeric-value/types';
|
|
7
|
+
import type { BigNum } from './numerics/types';
|
|
8
|
+
import type { LatexString, SerializeLatexOptions } from './latex-syntax/types';
|
|
9
|
+
import type { JsonSerializationOptions, PatternMatchOptions, ReplaceOptions, Substitution, BoxedSubstitution, CanonicalOptions } from './types-kernel-serialization';
|
|
10
|
+
import type { EvaluateOptions as KernelEvaluateOptions, BoxedRule as KernelBoxedRule, Rule as KernelRule, BoxedRuleSet as KernelBoxedRuleSet, Scope as KernelScope } from './types-kernel-evaluation';
|
|
11
|
+
/**
|
|
12
|
+
* Compute engine surface used by expression types.
|
|
13
|
+
*
|
|
14
|
+
* This interface is augmented by `types-engine.ts` with the concrete
|
|
15
|
+
* `IComputeEngine` members to avoid type-layer circular dependencies.
|
|
16
|
+
*
|
|
17
|
+
* @category Compute Engine
|
|
18
|
+
*/
|
|
19
|
+
export interface ExpressionComputeEngine {
|
|
20
|
+
}
|
|
21
|
+
type Sign = 'zero' | 'positive' | 'negative' | 'non-negative' | 'non-positive' | 'not-zero' | 'unsigned';
|
|
22
|
+
type BaseDefinition = {
|
|
23
|
+
description: string | string[];
|
|
24
|
+
examples: string | string[];
|
|
25
|
+
url: string;
|
|
26
|
+
wikidata: string;
|
|
27
|
+
readonly isConstant?: boolean;
|
|
28
|
+
};
|
|
29
|
+
interface BaseCollectionHandlers {
|
|
30
|
+
iterator: (collection: Expression) => Iterator<Expression, undefined> | undefined;
|
|
31
|
+
count: (collection: Expression) => number | undefined;
|
|
32
|
+
isEmpty?: (collection: Expression) => boolean | undefined;
|
|
33
|
+
isFinite?: (collection: Expression) => boolean | undefined;
|
|
34
|
+
isLazy?: (collection: Expression) => boolean;
|
|
35
|
+
contains?: (collection: Expression, target: Expression) => boolean | undefined;
|
|
36
|
+
subsetOf?: (collection: Expression, other: Expression, strict: boolean) => boolean | undefined;
|
|
37
|
+
eltsgn?: (collection: Expression) => Sign | undefined;
|
|
38
|
+
elttype?: (collection: Expression) => Type | undefined;
|
|
39
|
+
}
|
|
40
|
+
interface IndexedCollectionHandlers {
|
|
41
|
+
at: (collection: Expression, index: number | string) => undefined | Expression;
|
|
42
|
+
indexWhere: (collection: Expression, predicate: (element: Expression) => boolean) => number | undefined;
|
|
43
|
+
}
|
|
44
|
+
type CollectionHandlers = BaseCollectionHandlers & Partial<IndexedCollectionHandlers>;
|
|
45
|
+
interface BoxedBaseDefinition extends Partial<BaseDefinition> {
|
|
46
|
+
collection?: CollectionHandlers;
|
|
47
|
+
}
|
|
48
|
+
interface BoxedValueDefinition extends BoxedBaseDefinition {
|
|
49
|
+
holdUntil: 'never' | 'evaluate' | 'N';
|
|
50
|
+
readonly value: Expression | undefined;
|
|
51
|
+
eq?: (a: Expression) => boolean | undefined;
|
|
52
|
+
neq?: (a: Expression) => boolean | undefined;
|
|
53
|
+
cmp?: (a: Expression) => '=' | '>' | '<' | undefined;
|
|
54
|
+
inferredType: boolean;
|
|
55
|
+
type: BoxedType;
|
|
56
|
+
subscriptEvaluate?: (subscript: Expression, options: {
|
|
57
|
+
engine: ExpressionComputeEngine;
|
|
58
|
+
numericApproximation?: boolean;
|
|
59
|
+
}) => Expression | undefined;
|
|
60
|
+
}
|
|
61
|
+
type OperatorDefinitionFlags = {
|
|
62
|
+
lazy: boolean;
|
|
63
|
+
scoped: boolean;
|
|
64
|
+
broadcastable: boolean;
|
|
65
|
+
associative: boolean;
|
|
66
|
+
commutative: boolean;
|
|
67
|
+
commutativeOrder: ((a: Expression, b: Expression) => number) | undefined;
|
|
68
|
+
idempotent: boolean;
|
|
69
|
+
involution: boolean;
|
|
70
|
+
pure: boolean;
|
|
71
|
+
};
|
|
72
|
+
interface BoxedOperatorDefinition extends BoxedBaseDefinition, OperatorDefinitionFlags {
|
|
73
|
+
complexity: number;
|
|
74
|
+
inferredSignature: boolean;
|
|
75
|
+
signature: BoxedType;
|
|
76
|
+
type?: (ops: ReadonlyArray<Expression>, options: {
|
|
77
|
+
engine: ExpressionComputeEngine;
|
|
78
|
+
}) => Type | TypeString | BoxedType | undefined;
|
|
79
|
+
sgn?: (ops: ReadonlyArray<Expression>, options: {
|
|
80
|
+
engine: ExpressionComputeEngine;
|
|
81
|
+
}) => Sign | undefined;
|
|
82
|
+
eq?: (a: Expression, b: Expression) => boolean | undefined;
|
|
83
|
+
neq?: (a: Expression, b: Expression) => boolean | undefined;
|
|
84
|
+
canonical?: (ops: ReadonlyArray<Expression>, options: {
|
|
85
|
+
engine: ExpressionComputeEngine;
|
|
86
|
+
scope: Scope | undefined;
|
|
87
|
+
}) => Expression | null;
|
|
88
|
+
evaluate?: (ops: ReadonlyArray<Expression>, options: Partial<EvaluateOptions> & {
|
|
89
|
+
engine?: ExpressionComputeEngine;
|
|
90
|
+
}) => Expression | undefined;
|
|
91
|
+
evaluateAsync?: (ops: ReadonlyArray<Expression>, options?: Partial<EvaluateOptions> & {
|
|
92
|
+
engine?: ExpressionComputeEngine;
|
|
93
|
+
}) => Promise<Expression | undefined>;
|
|
94
|
+
evalDimension?: (ops: ReadonlyArray<Expression>, options: {
|
|
95
|
+
engine: ExpressionComputeEngine;
|
|
96
|
+
}) => Expression;
|
|
97
|
+
compile?: (expr: Expression) => CompiledExpression;
|
|
98
|
+
update(def: unknown): void;
|
|
99
|
+
}
|
|
100
|
+
type BoxedDefinition = {
|
|
101
|
+
value: BoxedValueDefinition;
|
|
102
|
+
} | {
|
|
103
|
+
operator: BoxedOperatorDefinition;
|
|
104
|
+
};
|
|
105
|
+
type Scope = KernelScope<BoxedDefinition>;
|
|
106
|
+
type EvaluateOptions = KernelEvaluateOptions<Expression>;
|
|
107
|
+
type Rule = KernelRule<Expression, ExpressionInput, ExpressionComputeEngine>;
|
|
108
|
+
type BoxedRule = KernelBoxedRule<Expression, ExpressionComputeEngine>;
|
|
109
|
+
type BoxedRuleSet = KernelBoxedRuleSet<Expression, ExpressionComputeEngine>;
|
|
110
|
+
type SimplifyOptions = {
|
|
111
|
+
rules?: null | Rule | ReadonlyArray<BoxedRule | Rule> | BoxedRuleSet;
|
|
112
|
+
costFunction?: (expr: Expression) => number;
|
|
113
|
+
strategy?: 'default' | 'fu';
|
|
114
|
+
};
|
|
115
|
+
/** @category Compiling */
|
|
116
|
+
export type CompiledType = boolean | number | string | object;
|
|
117
|
+
/** @category Compiling */
|
|
118
|
+
export type JSSource = string;
|
|
119
|
+
/** @category Compiling */
|
|
120
|
+
export type CompiledExpression = {
|
|
121
|
+
evaluate?: (scope: {
|
|
122
|
+
[symbol: string]: Expression;
|
|
123
|
+
}) => number | Expression;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Map of `TensorDataType` to JavaScript type.
|
|
127
|
+
*
|
|
128
|
+
* @category Tensors */
|
|
129
|
+
export type DataTypeMap = {
|
|
130
|
+
float64: number;
|
|
131
|
+
float32: number;
|
|
132
|
+
int32: number;
|
|
133
|
+
uint8: number;
|
|
134
|
+
complex128: Complex;
|
|
135
|
+
complex64: Complex;
|
|
136
|
+
bool: boolean;
|
|
137
|
+
expression: Expression;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The type of the cells in a tensor.
|
|
141
|
+
* @category Tensors */
|
|
142
|
+
export type TensorDataType = keyof DataTypeMap;
|
|
143
|
+
/** @internal */
|
|
144
|
+
export type NestedArray<T> = NestedArray_<T>[];
|
|
145
|
+
/** @internal */
|
|
146
|
+
export type NestedArray_<T> = T | NestedArray_<T>[];
|
|
147
|
+
/**
|
|
148
|
+
* A record representing the type, shape and data of a tensor.
|
|
149
|
+
* @category Tensors */
|
|
150
|
+
export interface TensorData<DT extends TensorDataType> {
|
|
151
|
+
dtype: DT;
|
|
152
|
+
shape: number[];
|
|
153
|
+
rank?: number;
|
|
154
|
+
data: DataTypeMap[DT][];
|
|
155
|
+
}
|
|
156
|
+
/** @category Tensors */
|
|
157
|
+
export interface TensorField<T extends number | Complex | Expression | boolean | string = number> {
|
|
158
|
+
readonly one: T;
|
|
159
|
+
readonly zero: T;
|
|
160
|
+
readonly nan: T;
|
|
161
|
+
cast(x: T, dtype: 'float64'): undefined | number;
|
|
162
|
+
cast(x: T, dtype: 'float32'): undefined | number;
|
|
163
|
+
cast(x: T, dtype: 'int32'): undefined | number;
|
|
164
|
+
cast(x: T, dtype: 'uint8'): undefined | number;
|
|
165
|
+
cast(x: T, dtype: 'complex128'): undefined | Complex;
|
|
166
|
+
cast(x: T, dtype: 'complex64'): undefined | Complex;
|
|
167
|
+
cast(x: T, dtype: 'bool'): undefined | boolean;
|
|
168
|
+
cast(x: T, dtype: 'expression'): undefined | Expression;
|
|
169
|
+
cast(x: T[], dtype: 'float64'): undefined | number[];
|
|
170
|
+
cast(x: T[], dtype: 'float32'): undefined | number[];
|
|
171
|
+
cast(x: T[], dtype: 'int32'): undefined | number[];
|
|
172
|
+
cast(x: T[], dtype: 'uint8'): undefined | number[];
|
|
173
|
+
cast(x: T[], dtype: 'complex128'): undefined | Complex[];
|
|
174
|
+
cast(x: T[], dtype: 'complex64'): undefined | Complex[];
|
|
175
|
+
cast(x: T[], dtype: 'bool'): undefined | boolean[];
|
|
176
|
+
cast(x: T[], dtype: 'expression'): undefined | Expression[];
|
|
177
|
+
cast(x: T | T[], dtype: TensorDataType): undefined | Complex | number | boolean | Expression | Complex[] | number[] | boolean[] | Expression[];
|
|
178
|
+
expression(x: T): Expression;
|
|
179
|
+
isZero(x: T): boolean;
|
|
180
|
+
isOne(x: T): boolean;
|
|
181
|
+
equals(lhs: T, rhs: T): boolean;
|
|
182
|
+
add(lhs: T, rhs: T): T;
|
|
183
|
+
addn(...xs: T[]): T;
|
|
184
|
+
neg(x: T): T;
|
|
185
|
+
sub(lhs: T, rhs: T): T;
|
|
186
|
+
mul(lhs: T, rhs: T): T;
|
|
187
|
+
muln(...xs: T[]): T;
|
|
188
|
+
div(lhs: T, rhs: T): T;
|
|
189
|
+
pow(rhs: T, n: number): T;
|
|
190
|
+
conjugate(x: T): T;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @category Tensors
|
|
194
|
+
*/
|
|
195
|
+
export interface Tensor<DT extends TensorDataType> extends TensorData<DT> {
|
|
196
|
+
dtype: DT;
|
|
197
|
+
shape: number[];
|
|
198
|
+
rank: number;
|
|
199
|
+
data: DataTypeMap[DT][];
|
|
200
|
+
readonly field: TensorField<DT>;
|
|
201
|
+
readonly expression: Expression;
|
|
202
|
+
readonly array: NestedArray<DataTypeMap[DT]>;
|
|
203
|
+
readonly isSquare: boolean;
|
|
204
|
+
readonly isSymmetric: boolean;
|
|
205
|
+
readonly isSkewSymmetric: boolean;
|
|
206
|
+
readonly isDiagonal: boolean;
|
|
207
|
+
readonly isUpperTriangular: boolean;
|
|
208
|
+
readonly isLowerTriangular: boolean;
|
|
209
|
+
readonly isTriangular: boolean;
|
|
210
|
+
readonly isIdentity: boolean;
|
|
211
|
+
readonly isZero: boolean;
|
|
212
|
+
at(...indices: number[]): DataTypeMap[DT] | undefined;
|
|
213
|
+
diagonal(axis1?: number, axis2?: number): undefined | DataTypeMap[DT][];
|
|
214
|
+
trace(axis1?: number, axis2?: number): undefined | DataTypeMap[DT] | Tensor<DT>;
|
|
215
|
+
reshape(...shape: number[]): Tensor<DT>;
|
|
216
|
+
slice(index: number): Tensor<DT>;
|
|
217
|
+
flatten(): DataTypeMap[DT][];
|
|
218
|
+
upcast<DT extends TensorDataType>(dtype: DT): Tensor<DT>;
|
|
219
|
+
transpose(axis1?: number, axis2?: number): undefined | Tensor<DT>;
|
|
220
|
+
conjugateTranspose(axis1?: number, axis2?: number): undefined | Tensor<DT>;
|
|
221
|
+
determinant(): undefined | DataTypeMap[DT];
|
|
222
|
+
inverse(): undefined | Tensor<DT>;
|
|
223
|
+
pseudoInverse(): undefined | Tensor<DT>;
|
|
224
|
+
adjugateMatrix(): undefined | Tensor<DT>;
|
|
225
|
+
minor(axis1: number, axis2: number): undefined | DataTypeMap[DT];
|
|
226
|
+
map1(fn: (lhs: DataTypeMap[DT], rhs: DataTypeMap[DT]) => DataTypeMap[DT], scalar: DataTypeMap[DT]): Tensor<DT>;
|
|
227
|
+
map2(fn: (lhs: DataTypeMap[DT], rhs: DataTypeMap[DT]) => DataTypeMap[DT], rhs: Tensor<DT>): Tensor<DT>;
|
|
228
|
+
add(other: Tensor<DT> | DataTypeMap[DT]): Tensor<DT>;
|
|
229
|
+
subtract(other: Tensor<DT> | DataTypeMap[DT]): Tensor<DT>;
|
|
230
|
+
multiply(other: Tensor<DT> | DataTypeMap[DT]): Tensor<DT>;
|
|
231
|
+
divide(other: Tensor<DT> | DataTypeMap[DT]): Tensor<DT>;
|
|
232
|
+
power(other: Tensor<DT> | DataTypeMap[DT]): Tensor<DT>;
|
|
233
|
+
equals(other: Tensor<DT>): boolean;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* :::info[THEORY OF OPERATIONS]
|
|
237
|
+
*
|
|
238
|
+
* The `Expression` interface includes the methods and properties
|
|
239
|
+
* applicable to all kinds of expression. For example it includes `expr.symbol`
|
|
240
|
+
* which only applies to symbols or `expr.ops` which only applies to
|
|
241
|
+
* function expressions.
|
|
242
|
+
*
|
|
243
|
+
* When a property is not applicable to this `Expression` its value is
|
|
244
|
+
* `undefined`. For example `expr.symbol` for a `BoxedNumber` is `undefined`.
|
|
245
|
+
*
|
|
246
|
+
* This convention makes it convenient to manipulate expressions without
|
|
247
|
+
* having to check what kind of instance they are before manipulating them.
|
|
248
|
+
* :::
|
|
249
|
+
*
|
|
250
|
+
* :::info[THEORY OF OPERATIONS]
|
|
251
|
+
* A boxed expression can represent a canonical or a non-canonical
|
|
252
|
+
* expression. A non-canonical expression is a "raw" form of the
|
|
253
|
+
* expression. For example, the non-canonical representation of `\frac{10}{20}`
|
|
254
|
+
* is `["Divide", 10, 20]`. The canonical representation of the same
|
|
255
|
+
* expression is the boxed number `1/2`.
|
|
256
|
+
*
|
|
257
|
+
* The canonical representation of symbols and function expressions are
|
|
258
|
+
* bound to a definition. The definition contains metadata about the symbol
|
|
259
|
+
* or function operator, such as its type, its signature, and other attributes.
|
|
260
|
+
* The value of symbols are tracked in a separate table for each
|
|
261
|
+
* evaluation context.
|
|
262
|
+
*
|
|
263
|
+
* The binding only occurs when the expression is constructed, if it is created
|
|
264
|
+
* as a canonical expression. If the expression is constructed as a
|
|
265
|
+
* non-canonical expression, no binding is done.
|
|
266
|
+
*
|
|
267
|
+
* <!--
|
|
268
|
+
* Rules:
|
|
269
|
+
* - nothing should cause the binding to occur outside of the constructor
|
|
270
|
+
* - if an operation require a canonical expression (e.g. evaluate()),
|
|
271
|
+
* it should return undefined or throw an error if the expression is not
|
|
272
|
+
* canonical
|
|
273
|
+
* -->
|
|
274
|
+
*
|
|
275
|
+
*
|
|
276
|
+
* :::
|
|
277
|
+
*
|
|
278
|
+
* :::info[THEORY OF OPERATIONS]
|
|
279
|
+
* The **value** of an expression is a number, a string, a boolean or a tensor.
|
|
280
|
+
*
|
|
281
|
+
* The value of number literals and strings are themselves.
|
|
282
|
+
*
|
|
283
|
+
* A symbol can have a value associated with it, in which case the value
|
|
284
|
+
* of the symbol is the value associated with it.
|
|
285
|
+
*
|
|
286
|
+
* Some symbols (unknowns) are purely symbolic and have no value associated
|
|
287
|
+
* with them.
|
|
288
|
+
*
|
|
289
|
+
* Function expressions do not have a value associated with them.
|
|
290
|
+
* For example, `["Add", 2, 3]` has no value associated with it, it is a
|
|
291
|
+
* symbolic expression.
|
|
292
|
+
*
|
|
293
|
+
* Some properties of a Boxed Expression are only applicable if the expression
|
|
294
|
+
* has a value associated with it. For example, `expr.isNumber` is only
|
|
295
|
+
* applicable if the value of the expression is a number, that is if the
|
|
296
|
+
* expression is a number literal or a symbol with a numeric value.
|
|
297
|
+
*
|
|
298
|
+
* The following properties are applicable to expressions with a value:
|
|
299
|
+
* - `expr.isNumber`
|
|
300
|
+
* :::
|
|
301
|
+
*
|
|
302
|
+
* To create a boxed expression:
|
|
303
|
+
*
|
|
304
|
+
* ### `ce.box()` and `ce.parse()`
|
|
305
|
+
*
|
|
306
|
+
* Use `ce.box()` or `ce.parse()`.
|
|
307
|
+
*
|
|
308
|
+
* Use `ce.parse()` to get a boxed expression from a LaTeX string.
|
|
309
|
+
* Use `ce.box()` to get a boxed expression from a MathJSON expression.
|
|
310
|
+
*
|
|
311
|
+
* By default, the result of these methods is a canonical expression. For
|
|
312
|
+
* example, if it is a rational literal, it is reduced to its canonical form.
|
|
313
|
+
* If it is a function expression:
|
|
314
|
+
* - the arguments are put in canonical form
|
|
315
|
+
* - the arguments of commutative functions are sorted
|
|
316
|
+
* - invisible operators are made explicit
|
|
317
|
+
* - a limited number of core simplifications are applied,
|
|
318
|
+
* for example rationals are reduced
|
|
319
|
+
* - sequences are flattened: `["Add", 1, ["Sequence", 2, 3]]` is
|
|
320
|
+
* transformed to `["Add", 1, 2, 3]`
|
|
321
|
+
* - associative functions are flattened: `["Add", 1, ["Add", 2, 3]]` is
|
|
322
|
+
* transformed to `["Add", 1, 2, 3]`
|
|
323
|
+
* - symbols are **not** replaced with their values (unless they have
|
|
324
|
+
* a `holdUntil` flag set to `never`).
|
|
325
|
+
*
|
|
326
|
+
* ### `ce.function()`
|
|
327
|
+
*
|
|
328
|
+
* This is a specialized version of `ce.box()` for creating a new function
|
|
329
|
+
* expression.
|
|
330
|
+
*
|
|
331
|
+
* The canonical handler of the operator is called.
|
|
332
|
+
*
|
|
333
|
+
*
|
|
334
|
+
* ### Algebraic methods (`expr.add()`, `expr.mul()`, etc...)
|
|
335
|
+
*
|
|
336
|
+
* The boxed expression have some algebraic methods, i.e. `add()`, `mul()`,
|
|
337
|
+
* `div()`, `pow()`, etc. These methods are suitable for
|
|
338
|
+
* internal calculations, although they may be used as part of the public
|
|
339
|
+
* API as well.
|
|
340
|
+
*
|
|
341
|
+
* - a runtime error is thrown if the expression is not canonical
|
|
342
|
+
* - the arguments are not evaluated
|
|
343
|
+
* - the canonical handler (of the corresponding operation) is not called
|
|
344
|
+
* - some additional simplifications over canonicalization are applied.
|
|
345
|
+
* For example number literals are combined.
|
|
346
|
+
* However, the result is exact, and no approximation is made. Use `.N()`
|
|
347
|
+
* to get an approximate value.
|
|
348
|
+
* This is equivalent to calling `simplify()` on the expression (but
|
|
349
|
+
* without simplifying the arguments).
|
|
350
|
+
* - sequences were already flattened as part of the canonicalization process
|
|
351
|
+
*
|
|
352
|
+
* For 'add()' and 'mul()', which take multiple arguments, separate functions
|
|
353
|
+
* are provided that take an array of arguments. They are equivalent
|
|
354
|
+
* to calling the boxed algebraic method, i.e. `ce.Zero.add(1, 2, 3)` and
|
|
355
|
+
* `add(1, 2, 3)` are equivalent.
|
|
356
|
+
*
|
|
357
|
+
* These methods are not equivalent to calling `expr.evaluate()` on the
|
|
358
|
+
* expression: evaluate will replace symbols with their values, and
|
|
359
|
+
* evaluate the expression.
|
|
360
|
+
*
|
|
361
|
+
* For algebraic functions (`add()`, `mul()`, etc..), use the corresponding
|
|
362
|
+
* canonicalization function, i.e. `canonicalAdd(a, b)` instead of
|
|
363
|
+
* `ce.function('Add', [a, b])`.
|
|
364
|
+
*
|
|
365
|
+
* Another option is to use the algebraic methods directly, i.e. `a.add(b)`
|
|
366
|
+
* instead of `ce.function('Add', [a, b])`. However, the algebraic methods will
|
|
367
|
+
* apply further simplifications which may or may not be desirable. For
|
|
368
|
+
* example, number literals will be combined.
|
|
369
|
+
*
|
|
370
|
+
* ### `ce._fn()`
|
|
371
|
+
*
|
|
372
|
+
* This method is a low level method to create a new function expression which
|
|
373
|
+
* is typically invoked in the canonical handler of an operator definition.
|
|
374
|
+
*
|
|
375
|
+
* The arguments are not modified. The expression is not put in canonical
|
|
376
|
+
* form. The canonical handler is *not* called.
|
|
377
|
+
*
|
|
378
|
+
* A canonical flag can be set when calling this method, but it only
|
|
379
|
+
* asserts that the function expression is canonical. The caller is responsible
|
|
380
|
+
* for ensuring that is the case.
|
|
381
|
+
*
|
|
382
|
+
*
|
|
383
|
+
*
|
|
384
|
+
* ### Canonical Handlers
|
|
385
|
+
*
|
|
386
|
+
* Canonical handlers are responsible for:
|
|
387
|
+
* - validating the signature: this can involve checking the
|
|
388
|
+
* number of arguments. It is recommended to avoid checking the
|
|
389
|
+
* type of non-literal arguments, since the type of symbols or
|
|
390
|
+
* function expressions may change. Similarly, the canonicalization
|
|
391
|
+
* process should not rely on the value of or assumptions about non-literal
|
|
392
|
+
* arguments.
|
|
393
|
+
* - flattening sequences
|
|
394
|
+
* - flattening arguments if the function is associative
|
|
395
|
+
* - sort the arguments (if the function is commutative)
|
|
396
|
+
* - calling `ce._fn()` to create a new function expression
|
|
397
|
+
*
|
|
398
|
+
* When the canonical handler is invoked, the arguments have been put in
|
|
399
|
+
* canonical form unless the `lazy` flag is set to `true`.
|
|
400
|
+
*
|
|
401
|
+
* Note that the result of a canonical handler should be a canonical expression,
|
|
402
|
+
* but not all arguments need to be canonical. For example, the arguments of
|
|
403
|
+
* `["Declare", "x", 2]` are not canonical, since `x` refers to the name
|
|
404
|
+
* of the symbol, not its value.
|
|
405
|
+
*
|
|
406
|
+
* @category Boxed Expression
|
|
407
|
+
*
|
|
408
|
+
*/
|
|
409
|
+
export interface Expression {
|
|
410
|
+
/** @internal */
|
|
411
|
+
readonly _kind: string;
|
|
412
|
+
/** @internal */
|
|
413
|
+
readonly hash: number;
|
|
414
|
+
/**
|
|
415
|
+
* The Compute Engine instance associated with this expression provides
|
|
416
|
+
* a context in which to interpret it, such as definition of symbols
|
|
417
|
+
* and functions.
|
|
418
|
+
*/
|
|
419
|
+
readonly engine: ExpressionComputeEngine;
|
|
420
|
+
/**
|
|
421
|
+
*
|
|
422
|
+
* Return a JavaScript primitive value for the expression, based on
|
|
423
|
+
* `Object.valueOf()`.
|
|
424
|
+
*
|
|
425
|
+
* This method is intended to make it easier to work with JavaScript
|
|
426
|
+
* primitives, for example when mixing JavaScript computations with
|
|
427
|
+
* symbolic computations from the Compute Engine.
|
|
428
|
+
*
|
|
429
|
+
* If the expression is a **machine number**, a **bignum**, or a **rational**
|
|
430
|
+
* that can be converted to a machine number, return a JavaScript `number`.
|
|
431
|
+
* This conversion may result in a loss of precision.
|
|
432
|
+
*
|
|
433
|
+
* If the expression is the **symbol `"True"`** or the **symbol `"False"`**,
|
|
434
|
+
* return `true` or `false`, respectively.
|
|
435
|
+
*
|
|
436
|
+
* If the expression is a **symbol with a numeric value**, return the numeric
|
|
437
|
+
* value of the symbol.
|
|
438
|
+
*
|
|
439
|
+
* If the expression is a **string literal**, return the string value.
|
|
440
|
+
*
|
|
441
|
+
* If the expression is a **tensor** (list of number or multidimensional
|
|
442
|
+
* array or matrix), return an array of numbers, or an array of
|
|
443
|
+
* arrays of numbers, or an array of arrays of arrays of numbers.
|
|
444
|
+
*
|
|
445
|
+
* If the expression is a function expression return a string representation
|
|
446
|
+
* of the expression.
|
|
447
|
+
*
|
|
448
|
+
* @category Primitive Methods
|
|
449
|
+
*/
|
|
450
|
+
valueOf(): number | number[] | number[][] | number[][][] | string | boolean;
|
|
451
|
+
/** Similar to`expr.valueOf()` but includes a hint.
|
|
452
|
+
*
|
|
453
|
+
* @category Primitive Methods
|
|
454
|
+
*/
|
|
455
|
+
[Symbol.toPrimitive](hint: 'number' | 'string' | 'default'): number | string | null;
|
|
456
|
+
/**
|
|
457
|
+
* Return an ASCIIMath representation of the expression. This string is
|
|
458
|
+
* suitable to be output to the console for debugging, for example.
|
|
459
|
+
*
|
|
460
|
+
* Based on `Object.toString()`.
|
|
461
|
+
*
|
|
462
|
+
* To get a LaTeX representation of the expression, use `expr.latex`.
|
|
463
|
+
*
|
|
464
|
+
* Note that lazy collections are eagerly evaluated.
|
|
465
|
+
*
|
|
466
|
+
* Used when coercing a `Expression` to a `String`.
|
|
467
|
+
*
|
|
468
|
+
* @category Primitive Methods
|
|
469
|
+
*/
|
|
470
|
+
toString(): string;
|
|
471
|
+
/** Serialize to a LaTeX string.
|
|
472
|
+
*
|
|
473
|
+
* Note that lazy collections are eagerly evaluated.
|
|
474
|
+
*
|
|
475
|
+
* Will ignore any LaTeX metadata.
|
|
476
|
+
*/
|
|
477
|
+
toLatex(options?: Partial<SerializeLatexOptions>): LatexString;
|
|
478
|
+
/** LaTeX representation of this expression.
|
|
479
|
+
*
|
|
480
|
+
* If the expression was parsed from LaTeX, the LaTeX representation is
|
|
481
|
+
* the same as the input LaTeX.
|
|
482
|
+
*
|
|
483
|
+
* To customize the serialization, use `expr.toLatex()`.
|
|
484
|
+
*
|
|
485
|
+
* Note that lazy collections are eagerly evaluated.
|
|
486
|
+
*
|
|
487
|
+
* :::info[Note]
|
|
488
|
+
* Applicable to canonical and non-canonical expressions.
|
|
489
|
+
* :::
|
|
490
|
+
*
|
|
491
|
+
*/
|
|
492
|
+
get latex(): LatexString;
|
|
493
|
+
/** Used by `JSON.stringify()` to serialize this object to JSON.
|
|
494
|
+
*
|
|
495
|
+
* Method version of `expr.json`.
|
|
496
|
+
*
|
|
497
|
+
* Based on `Object.toJSON()`.
|
|
498
|
+
*
|
|
499
|
+
* Note that lazy collections are *not* eagerly evaluated.
|
|
500
|
+
*
|
|
501
|
+
* @category Primitive Methods
|
|
502
|
+
*/
|
|
503
|
+
toJSON(): MathJsonExpression;
|
|
504
|
+
/** Serialize to a MathJSON expression with specified options */
|
|
505
|
+
toMathJson(options?: Readonly<Partial<JsonSerializationOptions>>): MathJsonExpression;
|
|
506
|
+
/** MathJSON representation of this expression.
|
|
507
|
+
*
|
|
508
|
+
* This representation always use shorthands when possible. Metadata is not
|
|
509
|
+
* included.
|
|
510
|
+
*
|
|
511
|
+
* Numbers are converted to JavaScript numbers and may lose precision.
|
|
512
|
+
*
|
|
513
|
+
* The expression is represented exactly and no sugaring is applied. For
|
|
514
|
+
* example, `["Power", "x", 2]` is not represented as `["Square", "x"]`.
|
|
515
|
+
*
|
|
516
|
+
* For more control over the serialization, use `expr.toMathJson()`.
|
|
517
|
+
*
|
|
518
|
+
* Note that lazy collections are *not* eagerly evaluated.
|
|
519
|
+
*
|
|
520
|
+
* :::info[Note]
|
|
521
|
+
* Applicable to canonical and non-canonical expressions.
|
|
522
|
+
* :::
|
|
523
|
+
*
|
|
524
|
+
*/
|
|
525
|
+
readonly json: MathJsonExpression;
|
|
526
|
+
/**
|
|
527
|
+
* Output to the console a string representation of the expression.
|
|
528
|
+
*
|
|
529
|
+
* Note that lazy collections are eagerly evaluated when printed.
|
|
530
|
+
*
|
|
531
|
+
*/
|
|
532
|
+
print(): void;
|
|
533
|
+
/** If the expression was constructed from a LaTeX string, the verbatim LaTeX
|
|
534
|
+
* string it was parsed from.
|
|
535
|
+
*/
|
|
536
|
+
verbatimLatex?: string;
|
|
537
|
+
/** If `true`, this expression is in a canonical form. */
|
|
538
|
+
get isCanonical(): boolean;
|
|
539
|
+
/** For internal use only, set when a canonical expression is created.
|
|
540
|
+
* @internal
|
|
541
|
+
*/
|
|
542
|
+
set isCanonical(val: boolean);
|
|
543
|
+
/** If `true`, this expression is in a structural form.
|
|
544
|
+
*
|
|
545
|
+
* The structural form of an expression is used when applying rules to
|
|
546
|
+
* an expression. For example, a rational number is represented as a
|
|
547
|
+
* function expression instead of a `Expression` object.
|
|
548
|
+
*
|
|
549
|
+
*/
|
|
550
|
+
get isStructural(): boolean;
|
|
551
|
+
/**
|
|
552
|
+
* Return the canonical form of this expression.
|
|
553
|
+
*
|
|
554
|
+
* If a function expression or symbol, they are first bound with a definition
|
|
555
|
+
* in the current scope.
|
|
556
|
+
*
|
|
557
|
+
* When determining the canonical form the following operator definition
|
|
558
|
+
* flags are applied:
|
|
559
|
+
* - `associative`: \\( f(a, f(b), c) \longrightarrow f(a, b, c) \\)
|
|
560
|
+
* - `idempotent`: \\( f(f(a)) \longrightarrow f(a) \\)
|
|
561
|
+
* - `involution`: \\( f(f(a)) \longrightarrow a \\)
|
|
562
|
+
* - `commutative`: sort the arguments.
|
|
563
|
+
*
|
|
564
|
+
* If this expression is already canonical, the value of canonical is
|
|
565
|
+
* `this`.
|
|
566
|
+
*
|
|
567
|
+
* The arguments of a canonical function expression may not all be
|
|
568
|
+
* canonical, for example in the `["Declare", "i", 2]` expression,
|
|
569
|
+
* `i` is not canonical since it is used only as the name of a symbol, not
|
|
570
|
+
* as a (potentially) existing symbol.
|
|
571
|
+
*
|
|
572
|
+
* :::info[Note]
|
|
573
|
+
* Partially canonical expressions, such as those produced through
|
|
574
|
+
* `CanonicalForm`, also yield an expression which is marked as `canonical`.
|
|
575
|
+
* This means that, likewise for partially canonical expressions, the
|
|
576
|
+
* `canonical` property will return the self-same expression (and
|
|
577
|
+
* 'isCanonical' will also be true).
|
|
578
|
+
* :::
|
|
579
|
+
*
|
|
580
|
+
*/
|
|
581
|
+
get canonical(): Expression;
|
|
582
|
+
/**
|
|
583
|
+
* Return the structural form of this expression.
|
|
584
|
+
*
|
|
585
|
+
* Some expressions, such as rational numbers, are represented with
|
|
586
|
+
* a `Expression` object. In some cases, for example when doing a
|
|
587
|
+
* structural comparison of two expressions, it is useful to have a
|
|
588
|
+
* structural representation of the expression where the rational numbers
|
|
589
|
+
* is represented by a function expression instead.
|
|
590
|
+
*
|
|
591
|
+
* If there is a structural representation of the expression, return it,
|
|
592
|
+
* otherwise return `this`.
|
|
593
|
+
*
|
|
594
|
+
*/
|
|
595
|
+
get structural(): Expression;
|
|
596
|
+
/** `false` if this expression or any of its subexpressions is an `["Error"]`
|
|
597
|
+
* expression.
|
|
598
|
+
*
|
|
599
|
+
* :::info[Note]
|
|
600
|
+
* Applicable to canonical and non-canonical expressions. For
|
|
601
|
+
* non-canonical expression, this may indicate a syntax error while parsing
|
|
602
|
+
* LaTeX. For canonical expression, this may indicate argument type
|
|
603
|
+
* mismatch, or missing or unexpected arguments.
|
|
604
|
+
* :::
|
|
605
|
+
*
|
|
606
|
+
*/
|
|
607
|
+
readonly isValid: boolean;
|
|
608
|
+
/** If *true*, evaluating this expression has no side-effects (does not
|
|
609
|
+
* change the state of the Compute Engine).
|
|
610
|
+
*
|
|
611
|
+
* If *false*, evaluating this expression may change the state of the
|
|
612
|
+
* Compute Engine or it may return a different value each time it is
|
|
613
|
+
* evaluated, even if the state of the Compute Engine is the same.
|
|
614
|
+
*
|
|
615
|
+
* As an example, the `["Add", 2, 3]` function expression is pure, but
|
|
616
|
+
* the `["Random"]` function expression is not pure.
|
|
617
|
+
*
|
|
618
|
+
* For a function expression to be pure, the function itself (its operator)
|
|
619
|
+
* must be pure, and all of its arguments must be pure too.
|
|
620
|
+
*
|
|
621
|
+
* A pure function expression may return a different value each time it is
|
|
622
|
+
* evaluated if its arguments are not constant. For example, the
|
|
623
|
+
* `["Add", "x", 1]` function expression is pure, but it is not
|
|
624
|
+
* constant, because `x` is not constant.
|
|
625
|
+
*
|
|
626
|
+
* :::info[Note]
|
|
627
|
+
* Applicable to canonical expressions only
|
|
628
|
+
* :::
|
|
629
|
+
*/
|
|
630
|
+
readonly isPure: boolean;
|
|
631
|
+
/**
|
|
632
|
+
* `True` if evaluating this expression always returns the same value.
|
|
633
|
+
*
|
|
634
|
+
* If *true* and a function expression, implies that it is *pure* and
|
|
635
|
+
* that all of its arguments are constant.
|
|
636
|
+
*
|
|
637
|
+
* Number literals, symbols with constant values, and pure numeric functions
|
|
638
|
+
* with constant arguments are all *constant*, i.e.:
|
|
639
|
+
* - `42` is constant
|
|
640
|
+
* - `Pi` is constant
|
|
641
|
+
* - `["Divide", "Pi", 2]` is constant
|
|
642
|
+
* - `x` is not constant, unless declared with a constant flag.
|
|
643
|
+
* - `["Add", "x", 2]` is either constant only if `x` is constant.
|
|
644
|
+
*/
|
|
645
|
+
readonly isConstant: boolean;
|
|
646
|
+
/** All the `["Error"]` subexpressions.
|
|
647
|
+
*
|
|
648
|
+
* If an expression includes an error, the expression is also an error.
|
|
649
|
+
* In that case, the `this.isValid` property is `false`.
|
|
650
|
+
*
|
|
651
|
+
* :::info[Note]
|
|
652
|
+
* Applicable to canonical and non-canonical expressions.
|
|
653
|
+
* :::
|
|
654
|
+
*
|
|
655
|
+
*/
|
|
656
|
+
readonly errors: ReadonlyArray<Expression>;
|
|
657
|
+
/** All the subexpressions matching the named operator, recursively.
|
|
658
|
+
*
|
|
659
|
+
* Example:
|
|
660
|
+
*
|
|
661
|
+
* ```js
|
|
662
|
+
* const expr = ce.parse('a + b * c + d');
|
|
663
|
+
* const subexpressions = expr.getSubexpressions('Add');
|
|
664
|
+
* // -> `[['Add', 'a', 'b'], ['Add', 'c', 'd']]`
|
|
665
|
+
* ```
|
|
666
|
+
*
|
|
667
|
+
* :::info[Note]
|
|
668
|
+
* Applicable to canonical and non-canonical expressions.
|
|
669
|
+
* :::
|
|
670
|
+
*
|
|
671
|
+
*/
|
|
672
|
+
getSubexpressions(operator: string): ReadonlyArray<Expression>;
|
|
673
|
+
/** All the subexpressions in this expression, recursively
|
|
674
|
+
*
|
|
675
|
+
* Example:
|
|
676
|
+
*
|
|
677
|
+
* ```js
|
|
678
|
+
* const expr = ce.parse('a + b * c + d');
|
|
679
|
+
* const subexpressions = expr.subexpressions;
|
|
680
|
+
* // -> `[['Add', 'a', 'b'], ['Add', 'c', 'd'], 'a', 'b', 'c', 'd']`
|
|
681
|
+
* ```
|
|
682
|
+
*
|
|
683
|
+
* :::info[Note]
|
|
684
|
+
* Applicable to canonical and non-canonical expressions.
|
|
685
|
+
* :::
|
|
686
|
+
*
|
|
687
|
+
*/
|
|
688
|
+
readonly subexpressions: ReadonlyArray<Expression>;
|
|
689
|
+
/**
|
|
690
|
+
*
|
|
691
|
+
* All the symbols in the expression, recursively
|
|
692
|
+
*
|
|
693
|
+
* ```js
|
|
694
|
+
* const expr = ce.parse('a + b * c + d');
|
|
695
|
+
* const symbols = expr.symbols;
|
|
696
|
+
* // -> ['a', 'b', 'c', 'd']
|
|
697
|
+
* ```
|
|
698
|
+
*
|
|
699
|
+
* :::info[Note]
|
|
700
|
+
* Applicable to canonical and non-canonical expressions.
|
|
701
|
+
* :::
|
|
702
|
+
*
|
|
703
|
+
*/
|
|
704
|
+
readonly symbols: ReadonlyArray<string>;
|
|
705
|
+
/**
|
|
706
|
+
* All the symbols used in the expression that do not have a value
|
|
707
|
+
* associated with them, i.e. they are declared but not defined.
|
|
708
|
+
*/
|
|
709
|
+
readonly unknowns: ReadonlyArray<string>;
|
|
710
|
+
/**
|
|
711
|
+
* Attempt to factor a numeric coefficient `c` and a `rest` out of a
|
|
712
|
+
* canonical expression such that `rest.mul(c)` is equal to `this`.
|
|
713
|
+
*
|
|
714
|
+
* Attempts to make `rest` a positive value (i.e. pulls out negative sign).
|
|
715
|
+
*
|
|
716
|
+
*```json
|
|
717
|
+
* ['Multiply', 2, 'x', 3, 'a']
|
|
718
|
+
* -> [NumericValue(6), ['Multiply', 'x', 'a']]
|
|
719
|
+
*
|
|
720
|
+
* ['Divide', ['Multiply', 2, 'x'], ['Multiply', 3, 'y', 'a']]
|
|
721
|
+
* -> [NumericValue({rational: [2, 3]}), ['Divide', 'x', ['Multiply, 'y', 'a']]]
|
|
722
|
+
* ```
|
|
723
|
+
*/
|
|
724
|
+
toNumericValue(): [NumericValue, Expression];
|
|
725
|
+
/**
|
|
726
|
+
* If the value of this expression is not an **integer** return `undefined`.
|
|
727
|
+
*
|
|
728
|
+
* @category Numeric Expression
|
|
729
|
+
*/
|
|
730
|
+
readonly isEven: boolean | undefined;
|
|
731
|
+
/**
|
|
732
|
+
* If the value of this expression is not an **integer** return `undefined`.
|
|
733
|
+
*
|
|
734
|
+
* @category Numeric Expression
|
|
735
|
+
*/
|
|
736
|
+
readonly isOdd: boolean | undefined;
|
|
737
|
+
/**
|
|
738
|
+
* Return the real part of the value of this expression, if a number.
|
|
739
|
+
*
|
|
740
|
+
* Otherwise, return `NaN` (not a number).
|
|
741
|
+
*
|
|
742
|
+
* @category Numeric Expression
|
|
743
|
+
*/
|
|
744
|
+
readonly re: number;
|
|
745
|
+
/**
|
|
746
|
+
* If value of this expression is a number, return the imaginary part of the
|
|
747
|
+
* value. If the value is a real number, the imaginary part is 0.
|
|
748
|
+
*
|
|
749
|
+
* Otherwise, return `NaN` (not a number).
|
|
750
|
+
*
|
|
751
|
+
* @category Numeric Expression
|
|
752
|
+
*/
|
|
753
|
+
readonly im: number;
|
|
754
|
+
/**
|
|
755
|
+
* If the value of this expression is a number, return the real part of the
|
|
756
|
+
* value as a `BigNum`.
|
|
757
|
+
*
|
|
758
|
+
* If the value is not available as a bignum return `undefined`. That is,
|
|
759
|
+
* the value is not upconverted to a bignum.
|
|
760
|
+
*
|
|
761
|
+
* To get the real value either as a bignum or a number, use
|
|
762
|
+
* `expr.bignumRe ?? expr.re`.
|
|
763
|
+
*
|
|
764
|
+
* When using this pattern, the value is returned as a bignum if available,
|
|
765
|
+
* otherwise as a number or `NaN` if the value is not a number.
|
|
766
|
+
*
|
|
767
|
+
* @category Numeric Expression
|
|
768
|
+
*
|
|
769
|
+
*/
|
|
770
|
+
readonly bignumRe: BigNum | undefined;
|
|
771
|
+
/**
|
|
772
|
+
* If the value of this expression is a number, return the imaginary part as
|
|
773
|
+
* a `BigNum`.
|
|
774
|
+
*
|
|
775
|
+
* It may be 0 if the number is real.
|
|
776
|
+
*
|
|
777
|
+
* If the value of the expression is not a number or the value is not
|
|
778
|
+
* available as a bignum return `undefined`. That is, the value is not
|
|
779
|
+
* upconverted to a bignum.
|
|
780
|
+
*
|
|
781
|
+
* To get the imaginary value either as a bignum or a number, use
|
|
782
|
+
* `expr.bignumIm ?? expr.im`.
|
|
783
|
+
*
|
|
784
|
+
* When using this pattern, the value is returned as a bignum if available, otherwise as a number or `NaN` if the value is not a number.
|
|
785
|
+
*
|
|
786
|
+
* @category Numeric Expression
|
|
787
|
+
*/
|
|
788
|
+
readonly bignumIm: BigNum | undefined;
|
|
789
|
+
/**
|
|
790
|
+
* Return the sign of the expression.
|
|
791
|
+
*
|
|
792
|
+
* Note that complex numbers have no natural ordering, so if the value is an
|
|
793
|
+
* imaginary number (a complex number with a non-zero imaginary part),
|
|
794
|
+
* `this.sgn` will return `unsigned`.
|
|
795
|
+
*
|
|
796
|
+
* If a symbol, this does take assumptions into account, that is `this.sgn`
|
|
797
|
+
* will return `positive` if the symbol is assumed to be positive
|
|
798
|
+
* using `ce.assume()`.
|
|
799
|
+
*
|
|
800
|
+
* Non-canonical expressions return `undefined`.
|
|
801
|
+
*
|
|
802
|
+
* @category Numeric Expression
|
|
803
|
+
*
|
|
804
|
+
*/
|
|
805
|
+
readonly sgn: Sign | undefined;
|
|
806
|
+
/** The value of this expression is > 0, same as `isGreaterEqual(0)`
|
|
807
|
+
*
|
|
808
|
+
* @category Numeric Expression
|
|
809
|
+
*/
|
|
810
|
+
readonly isPositive: boolean | undefined;
|
|
811
|
+
/** The value of this expression is >= 0, same as `isGreaterEqual(0)`
|
|
812
|
+
*
|
|
813
|
+
* @category Numeric Expression
|
|
814
|
+
*/
|
|
815
|
+
readonly isNonNegative: boolean | undefined;
|
|
816
|
+
/** The value of this expression is < 0, same as `isLess(0)`
|
|
817
|
+
*
|
|
818
|
+
* @category Numeric Expression
|
|
819
|
+
*/
|
|
820
|
+
readonly isNegative: boolean | undefined;
|
|
821
|
+
/** The value of this expression is <= 0, same as `isLessEqual(0)`
|
|
822
|
+
*
|
|
823
|
+
* @category Numeric Expression
|
|
824
|
+
*/
|
|
825
|
+
readonly isNonPositive: boolean | undefined;
|
|
826
|
+
/** Negate (additive inverse) */
|
|
827
|
+
neg(): Expression;
|
|
828
|
+
/** Inverse (multiplicative inverse) */
|
|
829
|
+
inv(): Expression;
|
|
830
|
+
/** Absolute value */
|
|
831
|
+
abs(): Expression;
|
|
832
|
+
/** Addition */
|
|
833
|
+
add(rhs: number | Expression): Expression;
|
|
834
|
+
/** Subtraction */
|
|
835
|
+
sub(rhs: Expression): Expression;
|
|
836
|
+
/** Multiplication */
|
|
837
|
+
mul(rhs: NumericValue | number | Expression): Expression;
|
|
838
|
+
/** Division */
|
|
839
|
+
div(rhs: number | Expression): Expression;
|
|
840
|
+
/** Power */
|
|
841
|
+
pow(exp: number | Expression): Expression;
|
|
842
|
+
/** Exponentiation */
|
|
843
|
+
root(exp: number | Expression): Expression;
|
|
844
|
+
/** Square root */
|
|
845
|
+
sqrt(): Expression;
|
|
846
|
+
/** Logarithm (natural by default) */
|
|
847
|
+
ln(base?: number | Expression): Expression;
|
|
848
|
+
/**
|
|
849
|
+
* Return this expression expressed as a numerator.
|
|
850
|
+
*/
|
|
851
|
+
get numerator(): Expression;
|
|
852
|
+
/**
|
|
853
|
+
* Return this expression expressed as a denominator.
|
|
854
|
+
*/
|
|
855
|
+
get denominator(): Expression;
|
|
856
|
+
/**
|
|
857
|
+
* Return this expression expressed as a numerator and denominator.
|
|
858
|
+
*/
|
|
859
|
+
get numeratorDenominator(): [Expression, Expression];
|
|
860
|
+
/**
|
|
861
|
+
* The name of the operator of the expression.
|
|
862
|
+
*
|
|
863
|
+
* For example, the name of the operator of `["Add", 2, 3]` is `"Add"`.
|
|
864
|
+
*
|
|
865
|
+
* A string literal has a `"String"` operator.
|
|
866
|
+
*
|
|
867
|
+
* A symbol has a `"Symbol"` operator.
|
|
868
|
+
*
|
|
869
|
+
* A number has a `"Number"`, `"Real"`, `"Rational"` or `"Integer"` operator; amongst some others.
|
|
870
|
+
* Practically speaking, for fully canonical and valid expressions, all of these are likely to
|
|
871
|
+
* collapse to `"Number"`.
|
|
872
|
+
*
|
|
873
|
+
* @category Function Expression
|
|
874
|
+
*/
|
|
875
|
+
readonly operator: string;
|
|
876
|
+
/** If true, the expression has its own local scope that can be used
|
|
877
|
+
* for local variables and arguments. Only true if the expression is a
|
|
878
|
+
* function expression.
|
|
879
|
+
*/
|
|
880
|
+
readonly isScoped: boolean;
|
|
881
|
+
/** If this expression has a local scope, return it. */
|
|
882
|
+
get localScope(): Scope | undefined;
|
|
883
|
+
/**
|
|
884
|
+
* Replace all the symbols in the expression as indicated.
|
|
885
|
+
*
|
|
886
|
+
* Note the same effect can be achieved with `this.replace()`, but
|
|
887
|
+
* using `this.subs()` is more efficient and simpler, but limited
|
|
888
|
+
* to replacing symbols.
|
|
889
|
+
*
|
|
890
|
+
* The result is bound to the current scope, not to `this.scope`.
|
|
891
|
+
*
|
|
892
|
+
* If `options.canonical` is not set, the result is canonical if `this`
|
|
893
|
+
* is canonical.
|
|
894
|
+
*
|
|
895
|
+
* :::info[Note]
|
|
896
|
+
* Applicable to canonical and non-canonical expressions.
|
|
897
|
+
*
|
|
898
|
+
* If this is a function, an empty substitution is given, and the computed value of `canonical`
|
|
899
|
+
* does not differ from that of this expr.: then a call this method is analagous to requesting a
|
|
900
|
+
* *clone*.
|
|
901
|
+
* :::
|
|
902
|
+
*
|
|
903
|
+
*/
|
|
904
|
+
subs(sub: Substitution<ExpressionInput>, options?: {
|
|
905
|
+
canonical?: CanonicalOptions;
|
|
906
|
+
}): Expression;
|
|
907
|
+
/**
|
|
908
|
+
* Recursively replace all the subexpressions in the expression as indicated.
|
|
909
|
+
*
|
|
910
|
+
* To remove a subexpression, return an empty `["Sequence"]` expression.
|
|
911
|
+
*
|
|
912
|
+
* The `canonical` option is applied to each function subexpression after
|
|
913
|
+
* the substitution is applied.
|
|
914
|
+
*
|
|
915
|
+
* If no `options.canonical` is set, the result is canonical if `this`
|
|
916
|
+
* is canonical.
|
|
917
|
+
*
|
|
918
|
+
* **Default**: `{ canonical: this.isCanonical, recursive: true }`
|
|
919
|
+
*
|
|
920
|
+
* :::info[Note]
|
|
921
|
+
* Applicable to canonical and non-canonical expressions.
|
|
922
|
+
* :::
|
|
923
|
+
*/
|
|
924
|
+
map(fn: (expr: Expression) => Expression, options?: {
|
|
925
|
+
canonical: CanonicalOptions;
|
|
926
|
+
recursive?: boolean;
|
|
927
|
+
}): Expression;
|
|
928
|
+
/**
|
|
929
|
+
* Transform the expression by applying one or more replacement rules:
|
|
930
|
+
*
|
|
931
|
+
* - If the expression matches the `match` pattern and the `condition`
|
|
932
|
+
* predicate is true, replace it with the `replace` pattern.
|
|
933
|
+
*
|
|
934
|
+
* - If no rules apply, return `null`.
|
|
935
|
+
*
|
|
936
|
+
* See also `expr.subs()` for a simple substitution of symbols.
|
|
937
|
+
*
|
|
938
|
+
* Procedure for the determining the canonical-status of the input expression and replacements:
|
|
939
|
+
*
|
|
940
|
+
* - If `options.canonical` is set, the *entire expr.* is canonicalized to this degree: whether
|
|
941
|
+
* the replacement occurs at the top-level, or within/recursively.
|
|
942
|
+
*
|
|
943
|
+
* - If otherwise, the *direct replacement will be canonical* if either the 'replaced' expression
|
|
944
|
+
* is canonical, or the given replacement (- is a Expression and -) is canonical.
|
|
945
|
+
* Notably also, if this replacement takes place recursively (not at the top-level), then exprs.
|
|
946
|
+
* containing the replaced expr. will still however have their (previous) canonical-status
|
|
947
|
+
* *preserved*... unless this expr. was previously non-canonical, and *replacements have resulted
|
|
948
|
+
* in canonical operands*. In this case, an expr. meeting this criteria will be updated to
|
|
949
|
+
* canonical status. (Canonicalization is opportunistic here, in other words).
|
|
950
|
+
*
|
|
951
|
+
* :::info[Note]
|
|
952
|
+
* Applicable to canonical and non-canonical expressions.
|
|
953
|
+
*
|
|
954
|
+
* To match a specific symbol (not a wildcard pattern), the `match` must be
|
|
955
|
+
* a `Expression` (e.g., `{ match: ce.box('x'), replace: ... }`).
|
|
956
|
+
* For simple symbol substitution, consider using `subs()` instead.
|
|
957
|
+
* :::
|
|
958
|
+
*/
|
|
959
|
+
replace(rules: BoxedRuleSet | Rule | Rule[], options?: Partial<ReplaceOptions>): null | Expression;
|
|
960
|
+
/**
|
|
961
|
+
* True if the expression includes a symbol `v` or a function operator `v`.
|
|
962
|
+
*
|
|
963
|
+
* :::info[Note]
|
|
964
|
+
* Applicable to canonical and non-canonical expressions.
|
|
965
|
+
* :::
|
|
966
|
+
*/
|
|
967
|
+
has(v: string | string[]): boolean;
|
|
968
|
+
/** Structural/symbolic equality (weak equality).
|
|
969
|
+
*
|
|
970
|
+
* `ce.parse('1+x', {form: 'raw'}).isSame(ce.parse('x+1', {form: 'raw'}))` is `false`.
|
|
971
|
+
*
|
|
972
|
+
* See `expr.isEqual()` for mathematical equality.
|
|
973
|
+
*
|
|
974
|
+
* :::info[Note]
|
|
975
|
+
* Applicable to canonical and non-canonical expressions.
|
|
976
|
+
* :::
|
|
977
|
+
*
|
|
978
|
+
* @category Relational Operator
|
|
979
|
+
*/
|
|
980
|
+
isSame(rhs: Expression): boolean;
|
|
981
|
+
/**
|
|
982
|
+
* Equivalent to `Expression.isSame()` but the argument can be
|
|
983
|
+
* a JavaScript primitive. For example, `expr.is(2)` is equivalent to
|
|
984
|
+
* `expr.isSame(ce.number(2))`.
|
|
985
|
+
*
|
|
986
|
+
* @category Primitive Methods
|
|
987
|
+
*
|
|
988
|
+
*/
|
|
989
|
+
is(other: Expression | number | bigint | boolean | string): boolean;
|
|
990
|
+
/**
|
|
991
|
+
* If this expression matches `pattern`, return a substitution that makes
|
|
992
|
+
* `pattern` equal to `this`. Otherwise return `null`.
|
|
993
|
+
*
|
|
994
|
+
* If `pattern` includes wildcards (symbols that start
|
|
995
|
+
* with `_`), the substitution will include a prop for each matching named
|
|
996
|
+
* wildcard.
|
|
997
|
+
*
|
|
998
|
+
* If this expression matches `pattern` but there are no named wildcards,
|
|
999
|
+
* return the empty substitution, `{}`.
|
|
1000
|
+
*
|
|
1001
|
+
* Read more about [**patterns and rules**](/compute-engine/guides/patterns-and-rules/).
|
|
1002
|
+
*
|
|
1003
|
+
* :::info[Note]
|
|
1004
|
+
* Applicable to canonical and non-canonical expressions.
|
|
1005
|
+
* :::
|
|
1006
|
+
*
|
|
1007
|
+
*/
|
|
1008
|
+
match(pattern: Expression, options?: PatternMatchOptions<Expression>): BoxedSubstitution<Expression> | null;
|
|
1009
|
+
/**
|
|
1010
|
+
*
|
|
1011
|
+
* The **shape** describes the **axes** of the expression, where each axis
|
|
1012
|
+
* represent a way to index the elements of the expression.
|
|
1013
|
+
*
|
|
1014
|
+
* When the expression is a scalar (number), the shape is `[]`.
|
|
1015
|
+
*
|
|
1016
|
+
* When the expression is a vector of length `n`, the shape is `[n]`.
|
|
1017
|
+
*
|
|
1018
|
+
* When the expression is a `n` by `m` matrix, the shape is `[n, m]`.
|
|
1019
|
+
*
|
|
1020
|
+
*
|
|
1021
|
+
* @category Tensor Expression
|
|
1022
|
+
*
|
|
1023
|
+
*/
|
|
1024
|
+
readonly shape: number[];
|
|
1025
|
+
/**
|
|
1026
|
+
* The **rank** refers to the number of dimensions (or axes) of the
|
|
1027
|
+
* expression.
|
|
1028
|
+
*
|
|
1029
|
+
* Return 0 for a scalar, 1 for a vector, 2 for a matrix, > 2 for
|
|
1030
|
+
* a multidimensional matrix.
|
|
1031
|
+
*
|
|
1032
|
+
* The rank is equivalent to the length of `expr.shape`
|
|
1033
|
+
*
|
|
1034
|
+
* :::info[Note]
|
|
1035
|
+
* There are several definitions of rank in the literature.
|
|
1036
|
+
* For example, the row rank of a matrix is the number of linearly
|
|
1037
|
+
* independent rows. The rank can also refer to the number of non-zero
|
|
1038
|
+
* singular values of a matrix.
|
|
1039
|
+
* :::
|
|
1040
|
+
*
|
|
1041
|
+
* @category Tensor Expression
|
|
1042
|
+
* */
|
|
1043
|
+
readonly rank: number;
|
|
1044
|
+
/**
|
|
1045
|
+
*
|
|
1046
|
+
* The value of both expressions are compared.
|
|
1047
|
+
*
|
|
1048
|
+
* If the expressions cannot be compared, return `undefined`
|
|
1049
|
+
*
|
|
1050
|
+
* @category Relational Operator
|
|
1051
|
+
*/
|
|
1052
|
+
isLess(other: number | Expression): boolean | undefined;
|
|
1053
|
+
/**
|
|
1054
|
+
* The value of both expressions are compared.
|
|
1055
|
+
*
|
|
1056
|
+
* If the expressions cannot be compared, return `undefined`
|
|
1057
|
+
* @category Relational Operator
|
|
1058
|
+
*/
|
|
1059
|
+
isLessEqual(other: number | Expression): boolean | undefined;
|
|
1060
|
+
/**
|
|
1061
|
+
* The value of both expressions are compared.
|
|
1062
|
+
*
|
|
1063
|
+
* If the expressions cannot be compared, return `undefined`
|
|
1064
|
+
* @category Relational Operator
|
|
1065
|
+
*/
|
|
1066
|
+
isGreater(other: number | Expression): boolean | undefined;
|
|
1067
|
+
/**
|
|
1068
|
+
* The value of both expressions are compared.
|
|
1069
|
+
*
|
|
1070
|
+
* If the expressions cannot be compared, return `undefined`
|
|
1071
|
+
* @category Relational Operator
|
|
1072
|
+
*/
|
|
1073
|
+
isGreaterEqual(other: number | Expression): boolean | undefined;
|
|
1074
|
+
/**
|
|
1075
|
+
* If true, the value of this expression is "Not a Number".
|
|
1076
|
+
*
|
|
1077
|
+
* A value representing undefined result of computations, such as `0/0`,
|
|
1078
|
+
* as per the floating point format standard IEEE-754.
|
|
1079
|
+
*
|
|
1080
|
+
* Note that if `isNaN` is true, `isNumber` is also true (yes, `NaN` is a
|
|
1081
|
+
* number).
|
|
1082
|
+
*
|
|
1083
|
+
* @category Numeric Expression
|
|
1084
|
+
*
|
|
1085
|
+
*/
|
|
1086
|
+
readonly isNaN: boolean | undefined;
|
|
1087
|
+
/**
|
|
1088
|
+
* The numeric value of this expression is `±Infinity` or ComplexInfinity.
|
|
1089
|
+
*
|
|
1090
|
+
* @category Numeric Expression
|
|
1091
|
+
*/
|
|
1092
|
+
readonly isInfinity: boolean | undefined;
|
|
1093
|
+
/** This expression is a number, but not `±Infinity`, `ComplexInfinity` or
|
|
1094
|
+
* `NaN`
|
|
1095
|
+
*
|
|
1096
|
+
* @category Numeric Expression
|
|
1097
|
+
*/
|
|
1098
|
+
readonly isFinite: boolean | undefined;
|
|
1099
|
+
/**
|
|
1100
|
+
* Wikidata identifier.
|
|
1101
|
+
*
|
|
1102
|
+
* If not a canonical expression, return `undefined`.
|
|
1103
|
+
*
|
|
1104
|
+
*/
|
|
1105
|
+
readonly wikidata: string | undefined;
|
|
1106
|
+
/** An optional short description if a symbol or function expression.
|
|
1107
|
+
*
|
|
1108
|
+
* May include markdown. Each string is a paragraph.
|
|
1109
|
+
*
|
|
1110
|
+
* If not a canonical expression, return `undefined`.
|
|
1111
|
+
*
|
|
1112
|
+
*/
|
|
1113
|
+
readonly description: undefined | string[];
|
|
1114
|
+
/** An optional URL pointing to more information about the symbol or
|
|
1115
|
+
* function operator.
|
|
1116
|
+
*
|
|
1117
|
+
* If not a canonical expression, return `undefined`.
|
|
1118
|
+
*
|
|
1119
|
+
*/
|
|
1120
|
+
readonly url: string | undefined;
|
|
1121
|
+
/** Expressions with a higher complexity score are sorted
|
|
1122
|
+
* first in commutative functions
|
|
1123
|
+
*
|
|
1124
|
+
* If not a canonical expression, return `undefined`.
|
|
1125
|
+
*/
|
|
1126
|
+
readonly complexity: number | undefined;
|
|
1127
|
+
/**
|
|
1128
|
+
* For symbols and functions, a definition associated with the
|
|
1129
|
+
* expression. `this.baseDefinition` is the base class of symbol and function
|
|
1130
|
+
* definition.
|
|
1131
|
+
*
|
|
1132
|
+
* If not a canonical expression, return `undefined`.
|
|
1133
|
+
*
|
|
1134
|
+
*/
|
|
1135
|
+
readonly baseDefinition: BoxedBaseDefinition | undefined;
|
|
1136
|
+
/**
|
|
1137
|
+
* For function expressions, the definition of the operator associated with
|
|
1138
|
+
* the expression. For symbols, the definition of the symbol if it is an
|
|
1139
|
+
* operator, for example `"Sin"`.
|
|
1140
|
+
*
|
|
1141
|
+
* If not a canonical expression or not a function expression,
|
|
1142
|
+
* its value is `undefined`.
|
|
1143
|
+
*
|
|
1144
|
+
*/
|
|
1145
|
+
readonly operatorDefinition: BoxedOperatorDefinition | undefined;
|
|
1146
|
+
/**
|
|
1147
|
+
* For symbols, a definition associated with the expression, if it is
|
|
1148
|
+
* not an operator.
|
|
1149
|
+
*
|
|
1150
|
+
* If not a canonical expression, or not a value, its value is `undefined`.
|
|
1151
|
+
*
|
|
1152
|
+
*/
|
|
1153
|
+
readonly valueDefinition: BoxedValueDefinition | undefined;
|
|
1154
|
+
/**
|
|
1155
|
+
*
|
|
1156
|
+
* Infer the type of this expression.
|
|
1157
|
+
*
|
|
1158
|
+
* For symbols, inference may take place for undeclared symbols,
|
|
1159
|
+
* symbols with an `unknown` type, or symbols with an inferred type.
|
|
1160
|
+
*
|
|
1161
|
+
* Constant symbols always have a defined type, and will return `false`.
|
|
1162
|
+
*
|
|
1163
|
+
* For functions, inference only takes place if it has an *inferred
|
|
1164
|
+
* signature*.
|
|
1165
|
+
*
|
|
1166
|
+
*
|
|
1167
|
+
* For a successful inference, *narrows* the type for symbols,
|
|
1168
|
+
* and for functions, narrows the *(return) type*.
|
|
1169
|
+
*
|
|
1170
|
+
* Subsequent inferences can be made and will refine previous ones if valid.
|
|
1171
|
+
*
|
|
1172
|
+
* If the given type is incompatible with the declared or previously inferred
|
|
1173
|
+
* type, return `false`.
|
|
1174
|
+
*
|
|
1175
|
+
*
|
|
1176
|
+
* @internal
|
|
1177
|
+
*/
|
|
1178
|
+
infer(t: Type, inferenceMode?: 'narrow' | 'widen'): boolean;
|
|
1179
|
+
/**
|
|
1180
|
+
* Update the definition associated with this expression, using the
|
|
1181
|
+
* current scope (`ce.context`).
|
|
1182
|
+
*
|
|
1183
|
+
* @internal
|
|
1184
|
+
*/
|
|
1185
|
+
bind(): void;
|
|
1186
|
+
/**
|
|
1187
|
+
*
|
|
1188
|
+
* Reset the cached value associated with this expression.
|
|
1189
|
+
*
|
|
1190
|
+
* Use when the environment, for example the precision, has changed to
|
|
1191
|
+
* force the expression to be re-evaluated.
|
|
1192
|
+
*
|
|
1193
|
+
* @internal
|
|
1194
|
+
*/
|
|
1195
|
+
reset(): void;
|
|
1196
|
+
/**
|
|
1197
|
+
* Return a simpler form of this expression.
|
|
1198
|
+
*
|
|
1199
|
+
* A series of rewriting rules are applied repeatedly, until no more rules
|
|
1200
|
+
* apply.
|
|
1201
|
+
*
|
|
1202
|
+
* The values assigned to symbols and the assumptions about symbols may be
|
|
1203
|
+
* used, for example `expr.isInteger` or `expr.isPositive`.
|
|
1204
|
+
*
|
|
1205
|
+
* No calculations involving decimal numbers (numbers that are not
|
|
1206
|
+
* integers) are performed but exact calculations may be performed,
|
|
1207
|
+
* for example:
|
|
1208
|
+
*
|
|
1209
|
+
* $$ \sin(\frac{\pi}{4}) \longrightarrow \frac{\sqrt{2}}{2} $$.
|
|
1210
|
+
*
|
|
1211
|
+
* The result is canonical.
|
|
1212
|
+
*
|
|
1213
|
+
* To manipulate symbolically non-canonical expressions, use `expr.replace()`.
|
|
1214
|
+
*
|
|
1215
|
+
*/
|
|
1216
|
+
simplify(options?: Partial<SimplifyOptions>): Expression;
|
|
1217
|
+
/**
|
|
1218
|
+
* Return the value of the canonical form of this expression.
|
|
1219
|
+
*
|
|
1220
|
+
* A pure expression always returns the same value (provided that it
|
|
1221
|
+
* remains constant / values of sub-expressions or symbols do not change),
|
|
1222
|
+
* and has no side effects.
|
|
1223
|
+
*
|
|
1224
|
+
* Evaluating an impure expression may return a varying value, and may have
|
|
1225
|
+
* some side effects such as adjusting symbol assumptions.
|
|
1226
|
+
*
|
|
1227
|
+
* To perform approximate calculations, use `expr.N()` instead,
|
|
1228
|
+
* or call with `options.numericApproximation` to `true`.
|
|
1229
|
+
*
|
|
1230
|
+
* It is possible that the result of `expr.evaluate()` may be the same as
|
|
1231
|
+
* `expr.simplify()`.
|
|
1232
|
+
*
|
|
1233
|
+
* The result is in canonical form.
|
|
1234
|
+
*
|
|
1235
|
+
*/
|
|
1236
|
+
evaluate(options?: Partial<EvaluateOptions>): Expression;
|
|
1237
|
+
/** Asynchronous version of `evaluate()`.
|
|
1238
|
+
*
|
|
1239
|
+
* The `options` argument can include a `signal` property, which is an
|
|
1240
|
+
* `AbortSignal` object. If the signal is aborted, a `CancellationError` is thrown.
|
|
1241
|
+
*
|
|
1242
|
+
*/
|
|
1243
|
+
evaluateAsync(options?: Partial<EvaluateOptions>): Promise<Expression>;
|
|
1244
|
+
/** Return a numeric approximation of the canonical form of this expression.
|
|
1245
|
+
*
|
|
1246
|
+
* Any necessary calculations, including on decimal numbers (non-integers),
|
|
1247
|
+
* are performed.
|
|
1248
|
+
*
|
|
1249
|
+
* The calculations are performed according to the
|
|
1250
|
+
* `precision` property of the `ComputeEngine`.
|
|
1251
|
+
*
|
|
1252
|
+
* To only perform exact calculations, use `this.evaluate()` instead.
|
|
1253
|
+
*
|
|
1254
|
+
* If the function is not numeric, the result of `this.N()` is the same as
|
|
1255
|
+
* `this.evaluate()`.
|
|
1256
|
+
*
|
|
1257
|
+
* The result is in canonical form.
|
|
1258
|
+
*/
|
|
1259
|
+
N(): Expression;
|
|
1260
|
+
/**
|
|
1261
|
+
* If this is an equation, solve the equation for the variables in vars.
|
|
1262
|
+
* Otherwise, solve the equation `this = 0` for the variables in vars.
|
|
1263
|
+
*
|
|
1264
|
+
* For univariate equations, returns an array of solutions (roots).
|
|
1265
|
+
* For systems of linear equations (List of Equal expressions), returns
|
|
1266
|
+
* an object mapping variable names to their values.
|
|
1267
|
+
* For non-linear polynomial systems (like xy=6, x+y=5), returns an array
|
|
1268
|
+
* of solution objects (multiple solutions possible).
|
|
1269
|
+
*
|
|
1270
|
+
* ```javascript
|
|
1271
|
+
* // Univariate equation
|
|
1272
|
+
* const expr = ce.parse("x^2 + 2*x + 1 = 0");
|
|
1273
|
+
* console.log(expr.solve("x")); // Returns array of roots
|
|
1274
|
+
*
|
|
1275
|
+
* // System of linear equations
|
|
1276
|
+
* const system = ce.parse("\\begin{cases}x+y=70\\\\2x-4y=80\\end{cases}");
|
|
1277
|
+
* console.log(system.solve(["x", "y"])); // Returns { x: 60, y: 10 }
|
|
1278
|
+
*
|
|
1279
|
+
* // Non-linear polynomial system (product + sum)
|
|
1280
|
+
* const nonlinear = ce.parse("\\begin{cases}xy=6\\\\x+y=5\\end{cases}");
|
|
1281
|
+
* console.log(nonlinear.solve(["x", "y"])); // Returns [{ x: 2, y: 3 }, { x: 3, y: 2 }]
|
|
1282
|
+
* ```
|
|
1283
|
+
*/
|
|
1284
|
+
solve(vars?: Iterable<string> | string | Expression | Iterable<Expression>): null | ReadonlyArray<Expression> | Record<string, Expression> | Array<Record<string, Expression>>;
|
|
1285
|
+
/**
|
|
1286
|
+
* If this expression is a number literal, a string literal or a function
|
|
1287
|
+
* literal, return the expression.
|
|
1288
|
+
*
|
|
1289
|
+
* If the expression is a symbol, return the value of the symbol.
|
|
1290
|
+
*
|
|
1291
|
+
* Otherwise, the expression is a symbolic expression, including an unknown
|
|
1292
|
+
* symbol, i.e. a symbol with no value, return `undefined`.
|
|
1293
|
+
*
|
|
1294
|
+
*/
|
|
1295
|
+
get value(): Expression | undefined;
|
|
1296
|
+
/**
|
|
1297
|
+
* If the expression is a symbol, set the value of the symbol.
|
|
1298
|
+
*
|
|
1299
|
+
* Will throw a runtime error if either not a symbol, or a symbol with the
|
|
1300
|
+
* `constant` flag set to `true`.
|
|
1301
|
+
*
|
|
1302
|
+
* Setting the value of a symbol results in the forgetting of all assumptions
|
|
1303
|
+
* about it in the current scope.
|
|
1304
|
+
*
|
|
1305
|
+
*/
|
|
1306
|
+
set value(value: boolean | string | BigNum | OneOf<[
|
|
1307
|
+
{
|
|
1308
|
+
re: number;
|
|
1309
|
+
im: number;
|
|
1310
|
+
},
|
|
1311
|
+
{
|
|
1312
|
+
num: number;
|
|
1313
|
+
denom: number;
|
|
1314
|
+
},
|
|
1315
|
+
Expression
|
|
1316
|
+
]> | number[] | number | undefined);
|
|
1317
|
+
/**
|
|
1318
|
+
*
|
|
1319
|
+
* The type of the value of this expression.
|
|
1320
|
+
*
|
|
1321
|
+
* If a symbol the type of the value of the symbol.
|
|
1322
|
+
*
|
|
1323
|
+
* If a function expression, the type of the value of the function
|
|
1324
|
+
* (the result type).
|
|
1325
|
+
*
|
|
1326
|
+
* If a symbol with a `"function"` type (a function literal), returns the
|
|
1327
|
+
* signature.
|
|
1328
|
+
*
|
|
1329
|
+
* If not valid, return `"error"`.
|
|
1330
|
+
*
|
|
1331
|
+
* If the type is not known, return `"unknown"`.
|
|
1332
|
+
*
|
|
1333
|
+
* @category Type Properties
|
|
1334
|
+
*/
|
|
1335
|
+
get type(): BoxedType;
|
|
1336
|
+
set type(type: Type | TypeString | BoxedType);
|
|
1337
|
+
/** `true` if the value of this expression is a number.
|
|
1338
|
+
*
|
|
1339
|
+
*
|
|
1340
|
+
* Note that in a fateful twist of cosmic irony, `NaN` ("Not a Number")
|
|
1341
|
+
* **is** a number.
|
|
1342
|
+
*
|
|
1343
|
+
* If `isNumber` is `true`, this indicates that evaluating the expression
|
|
1344
|
+
* will return a number.
|
|
1345
|
+
*
|
|
1346
|
+
* This does not indicate that the expression is a number literal. To check
|
|
1347
|
+
* if the expression is a number literal, use `expr.isNumberLiteral`.
|
|
1348
|
+
*
|
|
1349
|
+
* For example, the expression `["Add", 1, "x"]` is a number if "x" is a
|
|
1350
|
+
* number and `expr.isNumber` is `true`, but `isNumberLiteral` is `false`.
|
|
1351
|
+
*
|
|
1352
|
+
* @category Type Properties
|
|
1353
|
+
*/
|
|
1354
|
+
readonly isNumber: boolean | undefined;
|
|
1355
|
+
/**
|
|
1356
|
+
*
|
|
1357
|
+
* The value of this expression is an element of the set ℤ: ...,-2, -1, 0, 1, 2...
|
|
1358
|
+
*
|
|
1359
|
+
* Note that ±∞ and NaN are not integers.
|
|
1360
|
+
*
|
|
1361
|
+
* @category Type Properties
|
|
1362
|
+
*
|
|
1363
|
+
*/
|
|
1364
|
+
readonly isInteger: boolean | undefined;
|
|
1365
|
+
/** The value of this expression is an element of the set ℚ, p/q with p ∈ ℕ, q ∈ ℤ ⃰ q >= 1
|
|
1366
|
+
*
|
|
1367
|
+
* Note that every integer is also a rational.
|
|
1368
|
+
*
|
|
1369
|
+
* This is equivalent to `this.type === "rational" || this.type === "integer"`
|
|
1370
|
+
*
|
|
1371
|
+
* Note that ±∞ and NaN are not rationals.
|
|
1372
|
+
*
|
|
1373
|
+
* @category Type Properties
|
|
1374
|
+
*
|
|
1375
|
+
*/
|
|
1376
|
+
readonly isRational: boolean | undefined;
|
|
1377
|
+
/**
|
|
1378
|
+
* The value of this expression is a real number.
|
|
1379
|
+
*
|
|
1380
|
+
* This is equivalent to `this.type === "rational" || this.type === "integer" || this.type === "real"`
|
|
1381
|
+
*
|
|
1382
|
+
* Note that ±∞ and NaN are not real numbers.
|
|
1383
|
+
*
|
|
1384
|
+
* @category Type Properties
|
|
1385
|
+
*/
|
|
1386
|
+
readonly isReal: boolean | undefined;
|
|
1387
|
+
/** Mathematical equality (strong equality), that is the value
|
|
1388
|
+
* of this expression and the value of `other` are numerically equal.
|
|
1389
|
+
*
|
|
1390
|
+
* Both expressions are evaluated and the result is compared numerically.
|
|
1391
|
+
*
|
|
1392
|
+
* Numbers whose difference is less than `engine.tolerance` are
|
|
1393
|
+
* considered equal. This tolerance is set when the `engine.precision` is
|
|
1394
|
+
* changed to be such that the last two digits are ignored.
|
|
1395
|
+
*
|
|
1396
|
+
* Evaluating the expressions may be expensive. Other options to consider
|
|
1397
|
+
* to compare two expressions include:
|
|
1398
|
+
* - `expr.isSame(other)` for a structural comparison which does not involve
|
|
1399
|
+
* evaluating the expressions.
|
|
1400
|
+
* - `expr.is(other)` for a comparison of a number literal
|
|
1401
|
+
*
|
|
1402
|
+
* **Examples**
|
|
1403
|
+
*
|
|
1404
|
+
* ```js
|
|
1405
|
+
* let expr = ce.parse('2 + 2');
|
|
1406
|
+
* console.log(expr.isEqual(4)); // true
|
|
1407
|
+
* console.log(expr.isSame(ce.parse(4))); // false
|
|
1408
|
+
* console.log(expr.is(4)); // false
|
|
1409
|
+
*
|
|
1410
|
+
* expr = ce.parse('4');
|
|
1411
|
+
* console.log(expr.isEqual(4)); // true
|
|
1412
|
+
* console.log(expr.isSame(ce.parse(4))); // true
|
|
1413
|
+
* console.log(expr.is(4)); // true (fastest)
|
|
1414
|
+
*
|
|
1415
|
+
* ```
|
|
1416
|
+
*
|
|
1417
|
+
* @category Relational Operator
|
|
1418
|
+
*/
|
|
1419
|
+
isEqual(other: number | Expression): boolean | undefined;
|
|
1420
|
+
/**
|
|
1421
|
+
* Is `true` if the expression is a collection.
|
|
1422
|
+
*
|
|
1423
|
+
* When `isCollection` is `true`, the expression:
|
|
1424
|
+
*
|
|
1425
|
+
* - has an `each()` method that returns a generator over the elements
|
|
1426
|
+
* of the collection.
|
|
1427
|
+
* - has a `size` property that returns the number of elements in the
|
|
1428
|
+
* collection.
|
|
1429
|
+
* - has a `contains(other)` method that returns `true` if the `other`
|
|
1430
|
+
* expression is in the collection.
|
|
1431
|
+
*
|
|
1432
|
+
*/
|
|
1433
|
+
isCollection: boolean;
|
|
1434
|
+
/**
|
|
1435
|
+
* Is `true` if this is an indexed collection, such as a list, a vector,
|
|
1436
|
+
* a matrix, a tuple, etc...
|
|
1437
|
+
*
|
|
1438
|
+
* The elements of an indexed collection can be accessed by a one-based
|
|
1439
|
+
* index.
|
|
1440
|
+
*
|
|
1441
|
+
* When `isIndexedCollection` is `true`, the expression:
|
|
1442
|
+
* - has an `each()`, `size()` and `contains(rhs)` methods
|
|
1443
|
+
* as for a collection.
|
|
1444
|
+
* - has an `at(index: number)` method that returns the element at the
|
|
1445
|
+
* specified index.
|
|
1446
|
+
* - has an `indexWhere(predicate: (element: Expression) => boolean)`
|
|
1447
|
+
* method that returns the index of the first element that matches the
|
|
1448
|
+
* predicate.
|
|
1449
|
+
*/
|
|
1450
|
+
isIndexedCollection: boolean;
|
|
1451
|
+
/**
|
|
1452
|
+
* False if not a collection, or if the elements of the collection
|
|
1453
|
+
* are not computed lazily.
|
|
1454
|
+
*
|
|
1455
|
+
* The elements of a lazy collection are computed on demand, when
|
|
1456
|
+
* iterating over the collection using `each()`.
|
|
1457
|
+
*
|
|
1458
|
+
* Use `ListFrom` and related functions to create eager collections from
|
|
1459
|
+
* lazy collections.
|
|
1460
|
+
*
|
|
1461
|
+
*/
|
|
1462
|
+
isLazyCollection: boolean;
|
|
1463
|
+
/**
|
|
1464
|
+
* If this is a collection, return an iterator over the elements of the
|
|
1465
|
+
* collection.
|
|
1466
|
+
*
|
|
1467
|
+
* ```js
|
|
1468
|
+
* const expr = ce.parse('[1, 2, 3, 4]');
|
|
1469
|
+
* for (const e of expr.each()) {
|
|
1470
|
+
* console.log(e);
|
|
1471
|
+
* }
|
|
1472
|
+
* ```
|
|
1473
|
+
*/
|
|
1474
|
+
each(): Generator<Expression>;
|
|
1475
|
+
/**
|
|
1476
|
+
* If this is a collection, return true if the `rhs` expression is in the
|
|
1477
|
+
* collection.
|
|
1478
|
+
*
|
|
1479
|
+
* Return `undefined` if the membership cannot be determined without
|
|
1480
|
+
* iterating over the collection.
|
|
1481
|
+
*/
|
|
1482
|
+
contains(rhs: Expression): boolean | undefined;
|
|
1483
|
+
/**
|
|
1484
|
+
* Check if this collection is a subset of another collection.
|
|
1485
|
+
*
|
|
1486
|
+
* @param other The other collection to check against.
|
|
1487
|
+
* @param strict If true, the subset relation is strict (i.e., proper subset).
|
|
1488
|
+
*/
|
|
1489
|
+
subsetOf(other: Expression, strict: boolean): boolean | undefined;
|
|
1490
|
+
/**
|
|
1491
|
+
* If this is a collection, return the number of elements in the collection.
|
|
1492
|
+
*
|
|
1493
|
+
* If the collection is infinite, return `Infinity`.
|
|
1494
|
+
*
|
|
1495
|
+
* If the number of elements cannot be determined, return `undefined`, for
|
|
1496
|
+
* example, if the collection is lazy and not finite and the size cannot
|
|
1497
|
+
* be determined without iterating over the collection.
|
|
1498
|
+
*
|
|
1499
|
+
*/
|
|
1500
|
+
get count(): number | undefined;
|
|
1501
|
+
/** If this is a finite collection, return true. */
|
|
1502
|
+
isFiniteCollection: boolean | undefined;
|
|
1503
|
+
/** If this is an empty collection, return true.
|
|
1504
|
+
*
|
|
1505
|
+
* An empty collection has a size of 0.
|
|
1506
|
+
*/
|
|
1507
|
+
isEmptyCollection: boolean | undefined;
|
|
1508
|
+
/** If this is an indexed collection, return the element at the specified
|
|
1509
|
+
* index. The first element is at index 1.
|
|
1510
|
+
*
|
|
1511
|
+
* If the index is negative, return the element at index `size() + index + 1`.
|
|
1512
|
+
*
|
|
1513
|
+
* The last element is at index -1.
|
|
1514
|
+
*
|
|
1515
|
+
*/
|
|
1516
|
+
at(index: number): Expression | undefined;
|
|
1517
|
+
/** If this is a keyed collection (map, record, tuple), return the value of
|
|
1518
|
+
* the corresponding key.
|
|
1519
|
+
*
|
|
1520
|
+
* If `key` is a `Expression`, it should be a string.
|
|
1521
|
+
*
|
|
1522
|
+
*/
|
|
1523
|
+
get(key: string | Expression): Expression | undefined;
|
|
1524
|
+
/**
|
|
1525
|
+
* If this is an indexed collection, return the index of the first element
|
|
1526
|
+
* that matches the predicate.
|
|
1527
|
+
*
|
|
1528
|
+
*/
|
|
1529
|
+
indexWhere(predicate: (element: Expression) => boolean): number | undefined;
|
|
1530
|
+
}
|
|
1531
|
+
/**
|
|
1532
|
+
* Narrowed interface for number literal expressions.
|
|
1533
|
+
*
|
|
1534
|
+
* Obtained via `isNumber()`.
|
|
1535
|
+
*
|
|
1536
|
+
* @category Boxed Expression
|
|
1537
|
+
*/
|
|
1538
|
+
export interface NumberLiteralInterface {
|
|
1539
|
+
readonly numericValue: number | NumericValue;
|
|
1540
|
+
readonly isNumberLiteral: true;
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* Narrowed interface for symbol expressions.
|
|
1544
|
+
*
|
|
1545
|
+
* Obtained via `isSymbol()`.
|
|
1546
|
+
*
|
|
1547
|
+
* @category Boxed Expression
|
|
1548
|
+
*/
|
|
1549
|
+
export interface SymbolInterface {
|
|
1550
|
+
readonly symbol: string;
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
* Narrowed interface for function expressions.
|
|
1554
|
+
*
|
|
1555
|
+
* Obtained via `isFunction()`.
|
|
1556
|
+
*
|
|
1557
|
+
* @category Boxed Expression
|
|
1558
|
+
*/
|
|
1559
|
+
export interface FunctionInterface {
|
|
1560
|
+
readonly isFunctionExpression: true;
|
|
1561
|
+
readonly ops: ReadonlyArray<Expression>;
|
|
1562
|
+
readonly nops: number;
|
|
1563
|
+
readonly op1: Expression;
|
|
1564
|
+
readonly op2: Expression;
|
|
1565
|
+
readonly op3: Expression;
|
|
1566
|
+
}
|
|
1567
|
+
/**
|
|
1568
|
+
* Narrowed interface for string expressions.
|
|
1569
|
+
*
|
|
1570
|
+
* Obtained via `isString()`.
|
|
1571
|
+
*
|
|
1572
|
+
* @category Boxed Expression
|
|
1573
|
+
*/
|
|
1574
|
+
export interface StringInterface {
|
|
1575
|
+
readonly string: string;
|
|
1576
|
+
}
|
|
1577
|
+
/**
|
|
1578
|
+
* Narrowed interface for tensor expressions.
|
|
1579
|
+
*
|
|
1580
|
+
* Obtained via `isTensor()`.
|
|
1581
|
+
*
|
|
1582
|
+
* @category Boxed Expression
|
|
1583
|
+
*/
|
|
1584
|
+
export interface TensorInterface {
|
|
1585
|
+
readonly tensor: Tensor<TensorDataType>;
|
|
1586
|
+
readonly shape: number[];
|
|
1587
|
+
readonly rank: number;
|
|
1588
|
+
}
|
|
1589
|
+
/**
|
|
1590
|
+
* Narrowed interface for collection expressions.
|
|
1591
|
+
*
|
|
1592
|
+
* Obtained via `isCollection()`.
|
|
1593
|
+
*
|
|
1594
|
+
* @category Boxed Expression
|
|
1595
|
+
*/
|
|
1596
|
+
export interface CollectionInterface {
|
|
1597
|
+
readonly isCollection: true;
|
|
1598
|
+
each(): Generator<Expression>;
|
|
1599
|
+
contains(rhs: Expression): boolean | undefined;
|
|
1600
|
+
subsetOf(other: Expression, strict: boolean): boolean | undefined;
|
|
1601
|
+
readonly count: number | undefined;
|
|
1602
|
+
readonly isFiniteCollection: boolean | undefined;
|
|
1603
|
+
readonly isEmptyCollection: boolean | undefined;
|
|
1604
|
+
}
|
|
1605
|
+
/**
|
|
1606
|
+
* Narrowed interface for indexed collection expressions (lists, vectors,
|
|
1607
|
+
* matrices, tuples).
|
|
1608
|
+
*
|
|
1609
|
+
* Obtained via `isIndexedCollection()`.
|
|
1610
|
+
*
|
|
1611
|
+
* @category Boxed Expression
|
|
1612
|
+
*/
|
|
1613
|
+
export interface IndexedCollectionInterface extends CollectionInterface {
|
|
1614
|
+
readonly isIndexedCollection: true;
|
|
1615
|
+
at(index: number): Expression | undefined;
|
|
1616
|
+
indexWhere(predicate: (element: Expression) => boolean): number | undefined;
|
|
1617
|
+
}
|
|
1618
|
+
/** An expression input is a MathJSON expression which can include some
|
|
1619
|
+
* engine expression terms.
|
|
1620
|
+
*
|
|
1621
|
+
* This is convenient when creating new expressions from portions
|
|
1622
|
+
* of an existing `Expression` while avoiding unboxing and reboxing.
|
|
1623
|
+
*
|
|
1624
|
+
* @category Boxed Expression
|
|
1625
|
+
*/
|
|
1626
|
+
export type ExpressionInput = number | bigint | string | BigNum | MathJsonNumberObject | MathJsonStringObject | MathJsonSymbolObject | MathJsonFunctionObject | MathJsonDictionaryObject | readonly [MathJsonSymbol, ...ExpressionInput[]] | Expression;
|
|
1627
|
+
/** Interface for dictionary-like structures.
|
|
1628
|
+
* Use `isDictionary()` to check if an expression is a dictionary.
|
|
1629
|
+
*/
|
|
1630
|
+
export interface DictionaryInterface {
|
|
1631
|
+
get(key: string): Expression | undefined;
|
|
1632
|
+
has(key: string): boolean;
|
|
1633
|
+
get keys(): string[];
|
|
1634
|
+
get entries(): [string, Expression][];
|
|
1635
|
+
get values(): Expression[];
|
|
1636
|
+
}
|
|
1637
|
+
/**
|
|
1638
|
+
* These handlers compare two expressions.
|
|
1639
|
+
*
|
|
1640
|
+
* If only one of the handlers is provided, the other is derived from it.
|
|
1641
|
+
*
|
|
1642
|
+
* Having both may be useful if comparing non-equality is faster than equality.
|
|
1643
|
+
*
|
|
1644
|
+
* @category Definitions
|
|
1645
|
+
*
|
|
1646
|
+
*/
|
|
1647
|
+
export interface EqHandlers {
|
|
1648
|
+
eq: (a: Expression, b: Expression) => boolean | undefined;
|
|
1649
|
+
neq: (a: Expression, b: Expression) => boolean | undefined;
|
|
1650
|
+
}
|
|
1651
|
+
/** @deprecated Use `Expression` instead. */
|
|
1652
|
+
export type BoxedExpression = Expression;
|
|
1653
|
+
/** @deprecated Use `ExpressionInput` instead. */
|
|
1654
|
+
export type SemiBoxedExpression = ExpressionInput;
|
|
1655
|
+
export {};
|