@cortex-js/compute-engine 0.4.1 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/README.md +4 -5
  2. package/dist/compute-engine.esm.js +22496 -16245
  3. package/dist/compute-engine.min.esm.js +2 -7
  4. package/dist/compute-engine.min.js +2 -7
  5. package/dist/math-json.esm.js +4338 -10523
  6. package/dist/math-json.min.esm.js +2 -7
  7. package/dist/math-json.min.js +2 -7
  8. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  9. package/dist/types/common/signals.d.ts +90 -0
  10. package/dist/types/compute-engine/assume.d.ts +21 -6
  11. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +107 -0
  12. package/dist/types/compute-engine/boxed-expression/box.d.ts +56 -0
  13. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +39 -0
  14. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +8 -0
  15. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +2 -0
  16. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +93 -0
  17. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +83 -0
  18. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +21 -0
  19. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +23 -0
  20. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +98 -0
  21. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +74 -0
  22. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +12 -0
  23. package/dist/types/compute-engine/boxed-expression/order.d.ts +53 -0
  24. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +15 -0
  25. package/dist/types/compute-engine/boxed-expression/utils.d.ts +37 -0
  26. package/dist/types/compute-engine/compute-engine.d.ts +238 -97
  27. package/dist/types/compute-engine/cost-function.d.ts +3 -0
  28. package/dist/types/compute-engine/dictionary/arithmetic-add.d.ts +11 -0
  29. package/dist/types/compute-engine/dictionary/arithmetic-divide.d.ts +9 -0
  30. package/dist/types/compute-engine/dictionary/arithmetic-multiply.d.ts +17 -0
  31. package/dist/types/compute-engine/dictionary/arithmetic-power.d.ts +9 -0
  32. package/dist/types/compute-engine/dictionary/arithmetic.d.ts +2 -12
  33. package/dist/types/compute-engine/dictionary/collections.d.ts +1 -1
  34. package/dist/types/compute-engine/dictionary/core.d.ts +2 -2
  35. package/dist/types/compute-engine/dictionary/dictionary.d.ts +18 -11
  36. package/dist/types/compute-engine/dictionary/logic.d.ts +1 -1
  37. package/dist/types/compute-engine/dictionary/polynomials.d.ts +2 -0
  38. package/dist/types/compute-engine/dictionary/relational-operator.d.ts +2 -0
  39. package/dist/types/compute-engine/dictionary/sets.d.ts +1 -1
  40. package/dist/types/compute-engine/dictionary/trigonometry.d.ts +2 -2
  41. package/dist/types/compute-engine/domain-utils.d.ts +30 -0
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +2 -0
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +2 -0
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +2 -0
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +4 -0
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +2 -0
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +2 -0
  48. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +2 -0
  49. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +2 -0
  50. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +2 -0
  51. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +2 -0
  52. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +57 -0
  53. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +23 -0
  54. package/dist/types/compute-engine/latex-syntax/parse.d.ts +198 -0
  55. package/dist/types/compute-engine/latex-syntax/public.d.ts +550 -0
  56. package/dist/types/{common → compute-engine/latex-syntax}/serialize-number.d.ts +4 -5
  57. package/dist/types/{latex-syntax → compute-engine/latex-syntax}/serializer-style.d.ts +2 -1
  58. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +43 -0
  59. package/dist/types/{latex-syntax/core → compute-engine/latex-syntax}/tokenizer.d.ts +2 -2
  60. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +3 -0
  61. package/dist/types/compute-engine/numerics/numeric-decimal.d.ts +12 -0
  62. package/dist/types/compute-engine/numerics/numeric.d.ts +35 -0
  63. package/dist/types/compute-engine/numerics/primes.d.ts +2 -0
  64. package/dist/types/compute-engine/public.d.ts +1109 -434
  65. package/dist/types/compute-engine/rules.d.ts +16 -6
  66. package/dist/types/compute-engine/simplify-rules.d.ts +17 -0
  67. package/dist/types/compute-engine/symbolic/expand.d.ts +11 -0
  68. package/dist/types/compute-engine/symbolic/flatten.d.ts +7 -0
  69. package/dist/types/compute-engine/symbolic/negate.d.ts +12 -0
  70. package/dist/types/compute-engine/symbolic/polynomials.d.ts +52 -0
  71. package/dist/types/compute-engine/symbolic/product.d.ts +45 -0
  72. package/dist/types/compute-engine/symbolic/sum.d.ts +25 -0
  73. package/dist/types/compute-engine/symbolic/utils.d.ts +47 -0
  74. package/dist/types/compute-engine.d.ts +3 -5
  75. package/dist/types/math-json/math-json-format.d.ts +101 -0
  76. package/dist/types/math-json/utils.d.ts +97 -0
  77. package/dist/types/math-json.d.ts +5 -3
  78. package/package.json +29 -29
  79. package/dist/compute-engine.js +0 -17296
  80. package/dist/cortex.esm.js +0 -20989
  81. package/dist/cortex.js +0 -21011
  82. package/dist/cortex.min.esm.js +0 -7
  83. package/dist/cortex.min.js +0 -7
  84. package/dist/math-json.js +0 -12517
  85. package/dist/types/common/debug.d.ts +0 -28
  86. package/dist/types/common/utils.d.ts +0 -153
  87. package/dist/types/compute-engine/canonical-forms.d.ts +0 -74
  88. package/dist/types/compute-engine/dictionary/domains.d.ts +0 -26
  89. package/dist/types/compute-engine/dictionary/utils.d.ts +0 -5
  90. package/dist/types/compute-engine/domains.d.ts +0 -4
  91. package/dist/types/compute-engine/evaluate.d.ts +0 -13
  92. package/dist/types/compute-engine/expression-map.d.ts +0 -11
  93. package/dist/types/compute-engine/internal-compute-engine.d.ts +0 -108
  94. package/dist/types/compute-engine/numeric-complex.d.ts +0 -2
  95. package/dist/types/compute-engine/numeric-decimal.d.ts +0 -12
  96. package/dist/types/compute-engine/numeric.d.ts +0 -21
  97. package/dist/types/compute-engine/numerical-eval.d.ts +0 -5
  98. package/dist/types/compute-engine/order.d.ts +0 -18
  99. package/dist/types/compute-engine/patterns.d.ts +0 -22
  100. package/dist/types/compute-engine/predicates.d.ts +0 -42
  101. package/dist/types/compute-engine/simplify.d.ts +0 -6
  102. package/dist/types/compute-engine/utils.d.ts +0 -15
  103. package/dist/types/cortex/formatter.d.ts +0 -165
  104. package/dist/types/cortex/parse-cortex.d.ts +0 -5
  105. package/dist/types/cortex/reserved-words.d.ts +0 -1
  106. package/dist/types/cortex/serialize-cortex.d.ts +0 -14
  107. package/dist/types/cortex.d.ts +0 -8
  108. package/dist/types/latex-syntax/definitions-algebra.d.ts +0 -2
  109. package/dist/types/latex-syntax/definitions-arithmetic.d.ts +0 -3
  110. package/dist/types/latex-syntax/definitions-calculus.d.ts +0 -2
  111. package/dist/types/latex-syntax/definitions-core.d.ts +0 -3
  112. package/dist/types/latex-syntax/definitions-inequalities.d.ts +0 -2
  113. package/dist/types/latex-syntax/definitions-other.d.ts +0 -3
  114. package/dist/types/latex-syntax/definitions-sets.d.ts +0 -2
  115. package/dist/types/latex-syntax/definitions-symbols.d.ts +0 -3
  116. package/dist/types/latex-syntax/definitions-trigonometry.d.ts +0 -3
  117. package/dist/types/latex-syntax/definitions.d.ts +0 -40
  118. package/dist/types/latex-syntax/latex-syntax.d.ts +0 -27
  119. package/dist/types/latex-syntax/parse.d.ts +0 -138
  120. package/dist/types/latex-syntax/public.d.ts +0 -456
  121. package/dist/types/latex-syntax/serializer.d.ts +0 -26
  122. package/dist/types/latex-syntax/utils.d.ts +0 -10
  123. package/dist/types/point-free-parser/characters.d.ts +0 -25
  124. package/dist/types/point-free-parser/combinators.d.ts +0 -46
  125. package/dist/types/point-free-parser/core-combinators.d.ts +0 -78
  126. package/dist/types/point-free-parser/grammar.d.ts +0 -12
  127. package/dist/types/point-free-parser/identifier-parsers.d.ts +0 -7
  128. package/dist/types/point-free-parser/numeric-parsers.d.ts +0 -8
  129. package/dist/types/point-free-parser/parsers.d.ts +0 -130
  130. package/dist/types/point-free-parser/string-parsers.d.ts +0 -29
  131. package/dist/types/point-free-parser/whitespace-parsers.d.ts +0 -17
  132. package/dist/types/public.d.ts +0 -163
@@ -0,0 +1,74 @@
1
+ /* 0.4.4 */
2
+ import { AbstractBoxedExpression } from './abstract-boxed-expression';
3
+ import { BoxedExpression, BoxedRuleSet, BoxedSymbolDefinition, IComputeEngine, EvaluateOptions, NOptions, ReplaceOptions, SimplifyOptions, Substitution, Metadata, PatternMatchOption } from '../public';
4
+ /**
5
+ * BoxedSymbol
6
+ *
7
+ * A boxed symbol is a reference to a BoxedSymbolDefinition.
8
+ * The BoxedSymbolDefinition "owns" all the information
9
+ * about the symbol, its value, domain and various attributes.
10
+ */
11
+ export declare class BoxedSymbol extends AbstractBoxedExpression {
12
+ protected _name: string;
13
+ private _hash;
14
+ private _def;
15
+ constructor(ce: IComputeEngine, name: string, metadata?: Metadata);
16
+ get hash(): number;
17
+ _purge(): undefined;
18
+ get isPure(): boolean;
19
+ get isCanonical(): boolean;
20
+ set isCanonical(_va: boolean);
21
+ get wikidata(): string;
22
+ get description(): string[];
23
+ get url(): string;
24
+ get complexity(): number;
25
+ get head(): string;
26
+ get symbol(): string;
27
+ get isMissing(): boolean;
28
+ get isLiteral(): boolean;
29
+ get symbolDefinition(): BoxedSymbolDefinition | undefined;
30
+ _repairDefinition(): void;
31
+ get value(): BoxedExpression | undefined;
32
+ set value(value: BoxedExpression | number | undefined);
33
+ get numericValue(): BoxedExpression | undefined;
34
+ get domain(): BoxedExpression;
35
+ set domain(d: BoxedExpression);
36
+ get json(): Expression;
37
+ get sgn(): -1 | 0 | 1 | undefined | null;
38
+ has(x: string | string[]): boolean;
39
+ isSame(rhs: BoxedExpression): boolean;
40
+ match(rhs: BoxedExpression, _options?: PatternMatchOption): Substitution | null;
41
+ isEqual(rhs: BoxedExpression): boolean;
42
+ isLess(rhs: BoxedExpression): boolean | undefined;
43
+ isLessEqual(rhs: BoxedExpression): boolean | undefined;
44
+ isGreater(rhs: BoxedExpression): boolean | undefined;
45
+ isGreaterEqual(rhs: BoxedExpression): boolean | undefined;
46
+ get isZero(): boolean | undefined;
47
+ get isNotZero(): boolean | undefined;
48
+ get isOne(): boolean | undefined;
49
+ get isNegativeOne(): boolean | undefined;
50
+ get isOdd(): boolean | undefined;
51
+ get isEven(): boolean | undefined;
52
+ get isPrime(): boolean | undefined;
53
+ get isComposite(): boolean | undefined;
54
+ get isInfinity(): boolean | undefined;
55
+ get isNaN(): boolean | undefined;
56
+ get isPositive(): boolean | undefined;
57
+ get isNonPositive(): boolean | undefined;
58
+ get isNegative(): boolean | undefined;
59
+ get isNonNegative(): boolean | undefined;
60
+ get isNumber(): boolean | undefined;
61
+ get isInteger(): boolean | undefined;
62
+ get isRational(): boolean | undefined;
63
+ get isAlgebraic(): boolean | undefined;
64
+ get isReal(): boolean | undefined;
65
+ get isExtendedReal(): boolean | undefined;
66
+ get isComplex(): boolean | undefined;
67
+ get isImaginary(): boolean | undefined;
68
+ get canonical(): BoxedExpression;
69
+ simplify(options?: SimplifyOptions): BoxedExpression;
70
+ evaluate(options?: EvaluateOptions): BoxedExpression;
71
+ N(options?: NOptions): BoxedExpression;
72
+ replace(rules: BoxedRuleSet, options?: ReplaceOptions): BoxedExpression | null;
73
+ subs(sub: Substitution): BoxedExpression;
74
+ }
@@ -0,0 +1,12 @@
1
+ /* 0.4.4 */
2
+ export declare class ExpressionMap<U> implements ExpressionMapInterface<U> {
3
+ readonly _items: Map<BoxedExpression, U>;
4
+ constructor(source?: ExpressionMapInterface<U> | readonly (readonly [BoxedExpression, U])[]);
5
+ has(expr: BoxedExpression): boolean;
6
+ get(expr: BoxedExpression): U | undefined;
7
+ clear(): void;
8
+ set(expr: BoxedExpression, value: U): void;
9
+ delete(expr: BoxedExpression): void;
10
+ [Symbol.iterator](): IterableIterator<[BoxedExpression, U]>;
11
+ entries(): IterableIterator<[BoxedExpression, U]>;
12
+ }
@@ -0,0 +1,53 @@
1
+ /* 0.4.4 */
2
+ export declare type Order = 'lex' | 'dexlex' | 'grevlex' | 'elim';
3
+ export declare const DEFAULT_COMPLEXITY = 100000;
4
+ /**
5
+ * Sort by higher total degree (sum of degree), if tied, sort by max degree,
6
+ * if tied,
7
+ */
8
+ export declare function sortAdd(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression[];
9
+ /**
10
+ * Given two expressions `a` and `b`, return:
11
+ * - `-1` if `a` should be ordered before `b`
12
+ * - `+1` if `b` should be ordered before `a`
13
+ * - `0` if they have the same order (they are structurally equal)
14
+ *
15
+ * The default order is as follow:
16
+ *
17
+ * 1/ Literal numeric values (rational, machine numbers and Decimal numbers),
18
+ * ordered by they numeric value (smaller numbers before larger numbers)
19
+ *
20
+ * 2/ Literal complex numbers, ordered by their real parts. In case of a tie,
21
+ * ordered by the absolute value of their imaginary parts. In case of a tie,
22
+ * ordered by the value of their imaginary parts.
23
+ *
24
+ * 3/ Symbols, ordered by their name as strings
25
+ *
26
+ * 4/ Addition, ordered as a polynom, with higher degree terms first
27
+ *
28
+ * 5/ Other functions, ordered by their `complexity` property. In case
29
+ * of a tie, ordered by the head of the expression as a string. In case of a
30
+ * tie, by the leaf count of each expression. In case of a tie, by the order
31
+ * of each argument, left to right.
32
+ *
33
+ * 6/ Strings, ordered by comparing their Unicode code point values. While this
34
+ * sort order is quick to calculate, it can produce unexpected results, for
35
+ * example "E" < "e" < "È" and "11" < "2". This ordering is not suitable to
36
+ * collate natural language strings.
37
+ *
38
+ * 7/ Dictionaries, ordered by the number of keys. If there is a tie, by the
39
+ * sum of the complexities of the values of the dictionary
40
+ *
41
+ *
42
+ */
43
+ export declare function order(a: BoxedExpression, b: BoxedExpression): number;
44
+ /**
45
+ * Sort the terms of a polynomial expression (`Add` expression) according
46
+ * to the deglex polynomial ordering
47
+ *
48
+ */
49
+ export declare function polynomialOrder(expr: BoxedExpression): SemiBoxedExpression;
50
+ export declare function lexicographicOrder(expr: BoxedExpression, vars?: string[]): SemiBoxedExpression;
51
+ export declare function degreeLexicographicOrder(expr: BoxedExpression, vars?: string[]): SemiBoxedExpression;
52
+ export declare function degreeReverseLexicographicOrder(expr: BoxedExpression, vars?: string[]): SemiBoxedExpression;
53
+ export declare function eliminationOrder(expr: BoxedExpression, vars?: string[]): SemiBoxedExpression;
@@ -0,0 +1,15 @@
1
+ /* 0.4.4 */
2
+ import Decimal from 'decimal.js';
3
+ import { Expression } from '../../math-json/math-json-format';
4
+ import { BoxedExpression, IComputeEngine, Metadata } from '../public';
5
+ /**
6
+ * The canonical version of `serializeJsonFunction()` applies
7
+ * additional transformations to "reverse" some of the effects
8
+ * of canonicalization (or boxing), for example it uses `Divide`
9
+ * instead of `Multiply`/`Power` when applicable.
10
+ */
11
+ export declare function serializeJsonCanonicalFunction(ce: IComputeEngine, head: string | BoxedExpression, args: BoxedExpression[], metadata?: Metadata): Expression;
12
+ export declare function serializeJsonFunction(ce: IComputeEngine, head: string | BoxedExpression, args: BoxedExpression[], metadata?: Metadata): Expression;
13
+ export declare function serializeJsonString(ce: IComputeEngine, s: string): Expression;
14
+ export declare function serializeJsonSymbol(ce: IComputeEngine, sym: string, metadata?: Metadata): Expression;
15
+ export declare function serializeJsonNumber(ce: IComputeEngine, value: number | Decimal | Complex | [number, number], metadata?: Metadata): Expression;
@@ -0,0 +1,37 @@
1
+ /* 0.4.4 */
2
+ export declare function isLatexString(s: any): s is string;
3
+ export declare function latexString(s: any): string | null;
4
+ /**
5
+ * Return a multiple of the imaginary unit, e.g.
6
+ * - 'ImaginaryUnit'
7
+ * - ['Negate', 'ImaginaryUnit']
8
+ * - ['Negate', ['Multiply', 3, 'ImaginaryUnit']]
9
+ * - ['Multiply', 5, 'ImaginaryUnit']
10
+ * - ['Multiply', 'ImaginaryUnit', 5]
11
+ */
12
+ export declare function getImaginaryCoef(expr: BoxedExpression): number;
13
+ /**
14
+ * Return the free symbols in the expression, recursively.
15
+ * A variable, or free symbol,is a symbol that is not bound to a value.
16
+ */
17
+ export declare function getVars(expr: BoxedExpression): string[];
18
+ /**
19
+ * For any numeric result, or when boxing numbers,
20
+ * if `ce.useDecimal` is true, create them as Decimal number
21
+ * if `ce.useDecimal` is false, create them as machine number
22
+ */
23
+ export declare function useDecimal(ce: IComputeEngine): boolean;
24
+ /** If result of a numeric evaluation is a complex number,
25
+ * return `NaN` if `ce.useComplex` is false
26
+ */
27
+ export declare function complexAllowed(ce: IComputeEngine): boolean;
28
+ /**
29
+ * Assert that `expr` is in fact canonical.
30
+ *
31
+ * Called for example from within a `canonical` handler.
32
+ *
33
+ * To make an expression whose canonical status is unknown, canonical, call
34
+ * `expr.canonical`.
35
+ */
36
+ export declare function asCanonical(expr: BoxedExpression): BoxedExpression;
37
+ export declare function hashCode(s: string): number;
@@ -1,116 +1,257 @@
1
- /* 0.4.1 */
2
- import { CollectionDefinition, ComputeEngine as ComputeEngineInterface, Definition, Dictionary, Domain, Form, FunctionDefinition, Numeric, NumericFormat, RuleSet, RuntimeScope, Scope, SetDefinition, Simplification, SymbolDefinition } from './public';
3
- import { InternalComputeEngine } from './internal-compute-engine';
4
- import { ExpressionMap } from './expression-map';
5
- export declare class ComputeEngine<T extends number = number> implements ComputeEngineInterface {
6
- readonly internal: InternalComputeEngine;
7
- static getDictionaries(categories?: DictionaryCategory[] | 'all'): Readonly<Dictionary<Numeric>>[];
1
+ /* 0.4.4 */
2
+ import { DictionaryCategory, LatexDictionaryEntry, LatexString, NumberFormattingOptions, ParseLatexOptions, SerializeLatexOptions } from './latex-syntax/public';
3
+ import { Decimal } from 'decimal.js';
4
+ import { Complex } from 'complex.js';
5
+ import { SignalMessage, WarningSignal } from '../common/signals';
6
+ import { AssumeResult, BoxedExpression, BoxedFunctionDefinition, BoxedSymbolDefinition, IComputeEngine, Dictionary, ExpressionMapInterface, NumericMode as NumericMode, Pattern, RuntimeScope, Scope, SemiBoxedExpression, Substitution, SymbolDefinition, BoxedRuleSet, Rule, JsonSerializationOptions, ComputeEngineStats, Metadata } from './public';
7
+ /**
8
+ * Create a `CustomEngine` instance to customize its behavior and the syntax
9
+ * and operation dictionaries it uses.
10
+ *
11
+ * The constructor of `ComputeEngine` will compile and optimize the dictionary
12
+ * upfront.
13
+ */
14
+ export declare class ComputeEngine implements IComputeEngine {
15
+ readonly ZERO: BoxedExpression;
16
+ readonly ONE: BoxedExpression;
17
+ readonly TWO: BoxedExpression;
18
+ readonly HALF: BoxedExpression;
19
+ readonly NEGATIVE_ONE: BoxedExpression;
20
+ readonly I: BoxedExpression;
21
+ readonly NAN: BoxedExpression;
22
+ readonly POSITIVE_INFINITY: BoxedExpression;
23
+ readonly NEGATIVE_INFINITY: BoxedExpression;
24
+ readonly COMPLEX_INFINITY: BoxedExpression;
25
+ DECIMAL_NAN: Decimal;
26
+ DECIMAL_ZERO: Decimal;
27
+ DECIMAL_ONE: Decimal;
28
+ DECIMAL_TWO: Decimal;
29
+ DECIMAL_HALF: Decimal;
30
+ DECIMAL_PI: Decimal;
31
+ DECIMAL_NEGATIVE_ONE: Decimal;
32
+ private _precision;
33
+ private _numericMode;
34
+ private _latexSyntax?;
35
+ private _tolerance;
36
+ private _decimalTolerance;
37
+ private _cache;
38
+ private _stats;
39
+ private _cost?;
40
+ private _jsonSerializationOptions;
41
+ /** The domain of unknown symbols. If `null` unknown symbols do not have a
42
+ * definition automatically associated with them.
43
+ */
44
+ private _defaultDomain;
45
+ private _commonSymbols;
46
+ private _commonNumbers;
47
+ private _commonDomains;
48
+ private _latexDictionary?;
49
+ /**
50
+ * The current scope.
51
+ *
52
+ * A scope is a dictionary that contains the definition of local symbols.
53
+ *
54
+ * Scopes form a stack, and definitions in more recent
55
+ * scopes can obscure definitions from older scopes.
56
+ *
57
+ */
58
+ context: RuntimeScope;
59
+ /** Absolute time beyond which evaluation should not proceed */
60
+ deadline?: number;
61
+ /**
62
+ * Return dictionaries suitable for the specified categories, or `"all"`
63
+ * for all categories (`"arithmetic"`, `"algebra"`, etc...).
64
+ *
65
+ * A symbol dictionary defines how the symbols and function names in a MathJSON
66
+ * expression should be interpreted, i.e. how to evaluate and manipulate them.
67
+ *
68
+ */
69
+ static getDictionaries(categories?: DictionaryCategory[] | 'all'): Readonly<Dictionary>[];
70
+ /**
71
+ * Construct a new `ComputeEngine` environment.
72
+ *
73
+ * If no `options.dictionaries` is provided a default set of dictionaries
74
+ * is used.
75
+ *
76
+ * The `ComputeEngine.getDictionaries()` method can be called
77
+ * to access a subset of dictionaries, e.g. for arithmetic, calculus, etc...
78
+ *
79
+ * The order of the dictionaries matter: the definitions from the later ones
80
+ * override the definitions from earlier ones. The first dictionary should
81
+ * be the `'core'` dictionary which include some basic definitions such
82
+ * as domains (`Boolean`, `Number`, etc...) that are used by later dictionaries.
83
+ */
8
84
  constructor(options?: {
9
- dictionaries?: Readonly<Dictionary<Numeric>>[];
85
+ dictionaries?: Readonly<Dictionary>[];
86
+ latexDictionary?: readonly LatexDictionaryEntry[];
87
+ numericMode?: NumericMode;
88
+ numericPrecision?: number;
89
+ tolerance?: number;
90
+ assumptions?: (LatexString | Expression)[];
91
+ defaultDomain?: string;
10
92
  });
93
+ /** After the configuration of the engine has changed, clear the caches
94
+ * so that new values can be recalculated.
95
+ *
96
+ * This needs to happen for example when the numeric precision changes.
97
+ *
98
+ */
99
+ purge(): void;
100
+ _register(expr: BoxedExpression): void;
101
+ _unregister(expr: BoxedExpression): void;
102
+ get stats(): ComputeEngineStats;
103
+ _decimal: Decimal.Constructor;
104
+ decimal: (a: Decimal.Value) => Decimal;
105
+ complex: (a: number | Complex, b?: number) => Complex;
106
+ /** The precision, or number of significant digits, for numerical calculations
107
+ * such as when calling `ce.N()`.
108
+ *
109
+ * To make calculations using more digits, at the cost of expended memory
110
+ * usage and slower computations, set the `precision` higher.
111
+ *
112
+ * Trigonometric operations are accurate for precision up to 1,000.
113
+ *
114
+ */
11
115
  get precision(): number;
12
116
  set precision(p: number | 'machine');
13
- get numericFormat(): NumericFormat;
14
- set numericFormat(f: NumericFormat);
117
+ /**
118
+ * Mode to use for numerical calculations:
119
+ *
120
+ * - `auto`: use machine number if the precision is 15 or less, Decimal
121
+ * otherwise. Complex numbers are allowed.
122
+ * - `number`: use the machine format (64-bit float, 52-bit mantissa, about
123
+ * 15 digits of precision).
124
+ * - `decimal`: arbitrary precision floating-point numbers, as provided by the
125
+ * "decimal.js" library
126
+ * - `complex`: machine and complex numbers: two 64-bit float, as provided by the
127
+ * "complex.js" library
128
+ *
129
+ */
130
+ get numericMode(): NumericMode;
131
+ set numericMode(f: NumericMode);
132
+ get timeLimit(): number;
133
+ get iterationLimit(): number;
134
+ get recursionLimit(): number;
135
+ get defaultDomain(): BoxedExpression | null;
136
+ set defaultDomain(domain: BoxedExpression | string | null);
137
+ /**
138
+ * Values smaller than the tolerance are considered to be zero for the
139
+ * purpose of comparison, i.e. if `|b - a| <= tolerance`, `b` is considered
140
+ * equal to `a`.
141
+ */
15
142
  get tolerance(): number;
16
143
  set tolerance(val: number);
17
- get context(): RuntimeScope<Numeric>;
18
- pushScope(dictionary: Readonly<Dictionary<Numeric>>, scope?: Partial<Scope>): void;
144
+ chop(n: number): number;
145
+ chop(n: Decimal): Decimal | 0;
146
+ chop(n: Complex): Complex | 0;
147
+ private get latexSyntax();
148
+ set costFunction(fn: ((expr: BoxedExpression) => number) | undefined);
149
+ get costFunction(): (expr: BoxedExpression) => number;
150
+ /**
151
+ * Return a matching symbol definition, starting with the current
152
+ * scope and going up the scope chain.
153
+ */
154
+ getSymbolDefinition(symbol: string, wikidata?: string): undefined | BoxedSymbolDefinition;
155
+ /**
156
+ * Return the definition for a function matching this head.
157
+ *
158
+ * Start looking in the current scope, than up the scope chain.
159
+ */
160
+ getFunctionDefinition(head: string): undefined | BoxedFunctionDefinition;
161
+ /**
162
+ * Add (or replace) a definition for a symbol in the current scope.
163
+ */
164
+ defineSymbol(def: SymbolDefinition): BoxedSymbolDefinition;
165
+ /**
166
+ *
167
+ * Create a new scope and add it to the top of the scope stack
168
+ *
169
+ * The `options.scope` property can be used to specify custom precision,
170
+ * etc... for this scope
171
+ *
172
+ */
173
+ pushScope(options?: {
174
+ dictionary?: Readonly<Dictionary> | Readonly<Dictionary>[];
175
+ assumptions?: (LatexString | Expression)[];
176
+ scope?: Partial<Scope>;
177
+ }): void;
178
+ /** Remove the topmost scope from the scope stack.
179
+ */
19
180
  popScope(): void;
20
- get assumptions(): ExpressionMap<T, boolean>;
181
+ get assumptions(): ExpressionMapInterface<boolean>;
182
+ /**
183
+ * Return false if the execution should stop.
184
+ *
185
+ * This can occur if:
186
+ * - an error has been signaled
187
+ * - the time limit or memory limit has been exceeded
188
+ */
21
189
  shouldContinueExecution(): boolean;
22
190
  checkContinueExecution(): void;
23
- signal(sig: ErrorSignal | WarningSignal): void;
24
- getFunctionDefinition(name: string): FunctionDefinition | null;
25
- getSymbolDefinition(name: string): SymbolDefinition<Numeric> | null;
26
- getSetDefinition(name: string): SetDefinition<Numeric> | null;
27
- getCollectionDefinition(name: string): CollectionDefinition<Numeric> | null;
28
- getDefinition(name: string): Definition<Numeric> | null;
29
- getRules(topic: string | string[]): RuleSet;
30
- format(expr: Expression | null, forms?: Form | Form[]): Expression | null;
191
+ assert(condition: boolean, expr: BoxedExpression, msg: string, code?: SignalMessage): void;
31
192
  /**
32
- * Return the canonical form of an expression.
193
+ * Call this function if an unexpected condition occurs during execution of a
194
+ * function in the engine.
195
+ *
196
+ * An `ErrorSignal` is a problem that cannot be recovered from.
197
+ *
198
+ * A `WarningSignal` indicates a minor problem that does not prevent the
199
+ * execution to continue.
200
+ *
33
201
  */
34
- canonical(expr: Expression | null): Expression | null;
202
+ signal(expr: BoxedExpression, msg: string, code?: SignalMessage): void;
203
+ signal(sig: WarningSignal): void;
204
+ cache<T>(cacheName: string, build: () => T, purge: (T: any) => T | undefined): T;
205
+ box(expr: Decimal | Complex | [num: number, denom: number] | SemiBoxedExpression): BoxedExpression;
206
+ fn(head: string, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
207
+ _fn(head: string | BoxedExpression, ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
208
+ error(val: BoxedExpression, message: string, messageArg: SemiBoxedExpression): BoxedExpression;
209
+ add(ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
210
+ mul(ops: BoxedExpression[], metadata?: Metadata): BoxedExpression;
211
+ power(base: BoxedExpression, exponent: number | [number, number] | BoxedExpression, metadata?: Metadata): BoxedExpression;
212
+ inverse(expr: BoxedExpression, metadata?: Metadata): BoxedExpression;
213
+ negate(expr: BoxedExpression, metadata?: Metadata): BoxedExpression;
214
+ divide(num: BoxedExpression, denom: BoxedExpression, metadata?: Metadata): BoxedExpression;
215
+ pair(first: BoxedExpression, second: BoxedExpression, metadata?: Metadata): BoxedExpression;
216
+ tuple(elements: BoxedExpression[], metadata?: Metadata): BoxedExpression;
217
+ string(s: string, metadata?: Metadata): BoxedExpression;
218
+ symbol(sym: string, metadata?: Metadata): BoxedExpression;
219
+ domain(domain: BoxedExpression | string, metadata?: Metadata): BoxedExpression;
220
+ number(value: number | MathJsonNumber | Decimal | Complex | [num: number, denom: number], metadata?: Metadata): BoxedExpression;
221
+ rules(rules: Rule[]): BoxedRuleSet;
222
+ pattern(expr: LatexString | SemiBoxedExpression): Pattern;
223
+ parse(s: null): null;
224
+ parse(s: LatexString | string): BoxedExpression;
225
+ parse(s: LatexString | string | null): null | BoxedExpression;
226
+ serialize(x: Expression | BoxedExpression): string;
227
+ get latexOptions(): NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions;
228
+ set latexOptions(opts: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions> & Partial<SerializeLatexOptions>);
229
+ get jsonSerializationOptions(): JsonSerializationOptions;
230
+ set jsonSerializationOptions(val: Partial<JsonSerializationOptions>);
35
231
  /**
36
- * Return a numerical approximation of an expression.
232
+ * Return a list of all the assumptions that match a pattern.
233
+ *
234
+ * ```js
235
+ * ce.assume(x, 'PositiveInteger');
236
+ * ce.ask(['Greater', 'x', '_val'])
237
+ * // -> [{'val': 0}]
238
+ * ```
37
239
  */
38
- N(expr: Expression): Expression | null;
240
+ ask(pattern: LatexString | SemiBoxedExpression): Substitution[];
39
241
  /**
40
- * Attempt to simplify an expression, that is rewrite it in a simpler form,
41
- * making use of the available assumptions.
242
+ * Add an assumption.
42
243
  *
43
- * The simplification steps will proceed multiple times until either:
44
- * 1/ the expression stop changing
45
- * 2/ the number of iteration exceeds `iterationLimit`
46
- * 3/ the time to compute exceeds `timeLimit`, expressed in seconds
244
+ * Return `contradiction` if the new assumption is incompatible with previous
245
+ * ones.
47
246
  *
48
- * If no `timeLimit` or `iterationLimit` are provided, the values
49
- * from the current ComputeEngine context are used. By default those
50
- * values are an infinite amount of iterations and a 2s time limit.
247
+ * Return `tautology` if the new assumption is redundant with previous ones.
248
+ *
249
+ * Return `ok` if the assumption was successfully added to the assumption set.
250
+ *
251
+ * Note that the assumption is put into canonical form before being added.
51
252
  *
52
253
  */
53
- simplify(expr: Expression, options?: {
54
- timeLimit?: number;
55
- iterationLimit?: number;
56
- simplifications?: Simplification[];
57
- }): Expression | null;
58
- /**
59
- * Return a simplified and numerically approximation of an expression
60
- * in canonical form.
61
- *
62
- * The simplification steps will proceed multiple times until either:
63
- * 1/ the expression stop changing
64
- * 2/ the number of iteration exceeds `iterationLimit`
65
- * 3/ the time to compute exceeds `timeLimit`, expressed in seconds
66
- *
67
- * If no `timeLimit` or `iterationLimit` are provided, the values
68
- * from the current ComputeEngine context are used. By default those
69
- * values are an infinite amount of iterations and a 2s time limit.
70
- */
71
- evaluate(expr: Expression, options?: {
72
- timeLimit?: number;
73
- iterationLimit?: number;
74
- }): Promise<Expression | null>;
75
- parse(s: string): Expression;
76
- serialize(x: Expression): string;
77
- domain(expr: Expression): Expression | null;
78
- /** Query the assumption database
79
- */
80
- is(symbol: Expression, domain: Domain): boolean | undefined;
81
- is(predicate: Expression): boolean | undefined;
82
- ask(pattern: Expression): {
83
- [symbol: string]: Expression;
84
- }[];
85
- assume(symbol: Expression, domain: Domain): 'contradiction' | 'tautology' | 'ok';
86
- assume(predicate: Expression): 'contradiction' | 'tautology' | 'ok';
87
- replace(rules: RuleSet<T>, expr: Expression<T>): Expression<T>;
88
- getVars(expr: Expression): Set<string>;
89
- chop(n: Numeric): Numeric;
90
- isZero(x: Expression<T>): boolean | undefined;
91
- isNotZero(x: Expression<T>): boolean | undefined;
92
- isNumeric(x: Expression<T>): boolean | undefined;
93
- isInfinity(x: Expression<T>): boolean | undefined;
94
- isFinite(x: Expression<T>): boolean | undefined;
95
- isNonNegative(x: Expression<T>): boolean | undefined;
96
- isPositive(x: Expression<T>): boolean | undefined;
97
- isNegative(x: Expression<T>): boolean | undefined;
98
- isNonPositive(x: Expression<T>): boolean | undefined;
99
- isInteger(x: Expression<T>): boolean | undefined;
100
- isRational(x: Expression<T>): boolean | undefined;
101
- isAlgebraic(x: Expression<T>): boolean | undefined;
102
- isReal(x: Expression<T>): boolean | undefined;
103
- isExtendedReal(x: Expression<T>): boolean | undefined;
104
- isComplex(x: Expression<T>): boolean | undefined;
105
- isOne(x: Expression<T>): boolean | undefined;
106
- isNegativeOne(x: Expression<T>): boolean | undefined;
107
- isElement(x: Expression<T>, set: Expression<T>): boolean | undefined;
108
- isSubsetOf(lhs: Domain | null, rhs: Domain | null): boolean;
109
- isEqual(lhs: Expression, rhs: Expression): boolean | undefined;
110
- isLess(lhs: Expression, rhs: Expression): boolean | undefined;
111
- isLessEqual(lhs: Expression, rhs: Expression): boolean | undefined;
112
- isGreater(lhs: Expression, rhs: Expression): boolean | undefined;
113
- isGreaterEqual(lhs: Expression, rhs: Expression): boolean | undefined;
254
+ assume(symbol: LatexString | SemiBoxedExpression, domain: string | BoxedExpression): AssumeResult;
255
+ assume(predicate: LatexString | SemiBoxedExpression): AssumeResult;
256
+ forget(symbol: undefined | string | string[]): void;
114
257
  }
115
- export declare function format(expr: Expression, forms: Form | Form[]): Expression | null;
116
- export declare function evaluate(expr: Expression): Promise<Expression | null>;
@@ -0,0 +1,3 @@
1
+ /* 0.4.4 */
2
+ export declare function costFunction(expr: BoxedExpression): number;
3
+ export declare const DEFAULT_COST_FUNCTION: typeof costFunction;
@@ -0,0 +1,11 @@
1
+ /* 0.4.4 */
2
+ /** The canonical form of `Add`:
3
+ * - removes `0`
4
+ * - adds up small integers and rational numbers
5
+ * - capture complex numbers (a + ib or ai +b)
6
+ * - groups repeated terms (a + a -> 2a)
7
+ * */
8
+ export declare function canonicalAdd(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
9
+ export declare function domainAdd(ce: IComputeEngine, args: BoxedExpression[]): BoxedExpression | string;
10
+ export declare function numEvalAdd(ce: IComputeEngine, args: BoxedExpression[]): BoxedExpression;
11
+ export declare function processAdd(ce: IComputeEngine, args: BoxedExpression[], _mode: 'simplify' | 'evaluate'): BoxedExpression | undefined;
@@ -0,0 +1,9 @@
1
+ /* 0.4.4 */
2
+ /**
3
+ * Canonical form of 'Divide' (and 'Rational')
4
+ * - remove denominator of 1
5
+ * - simplify the signs
6
+ * - factor out negate (make the numerator and denominator positive)
7
+ * - if Divide, transform into Multiply/Power
8
+ */
9
+ export declare function canonicalDivide(ce: IComputeEngine, op1: BoxedExpression, op2: BoxedExpression): BoxedExpression;
@@ -0,0 +1,17 @@
1
+ /* 0.4.4 */
2
+ /** The canonical form of `Multiply`:
3
+ * - remove `1`
4
+ * - combine literal small integers and rationals
5
+ * - any arg is literal 0 -> return 0
6
+ * - simplify signs
7
+ * - combine terms with same base
8
+ * `a a^3` -> `a^4`
9
+ * - simplify the signs:
10
+ * - i.e. `-2 \times -3` -> `2 \times 3`
11
+ * - `2 \times -x` -> `-2 \times x`
12
+ *
13
+ * The ops must be canonical, the result is canonical.
14
+ */
15
+ export declare function canonicalMultiply(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
16
+ export declare function processMultiply(ce: IComputeEngine, ops: BoxedExpression[], _mode: 'simplify' | 'evaluate'): BoxedExpression | undefined;
17
+ export declare function numEvalMultiply(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression | undefined;
@@ -0,0 +1,9 @@
1
+ /* 0.4.4 */
2
+ /**
3
+ *
4
+ * Return `null` if there is no canonicalization necessary and the result is
5
+ * simply `ce._fn('Power', [base, exponent])`
6
+ */
7
+ export declare function canonicalPower(ce: IComputeEngine, base: BoxedExpression, exponent: BoxedExpression, metadata?: Metadata): BoxedExpression | null;
8
+ export declare function square(ce: IComputeEngine, base: BoxedExpression): BoxedExpression;
9
+ export declare function processPower(ce: IComputeEngine, base: BoxedExpression, exponent: BoxedExpression, mode: 'simplify' | 'evaluate' | 'N'): BoxedExpression | undefined;
@@ -1,12 +1,2 @@
1
- /* 0.4.1 */
2
- import type { ComputeEngine, Dictionary, Numeric } from '../public';
3
- export declare const ARITHMETIC_DICTIONARY: Dictionary<Numeric>;
4
- /** Apply some simplifications for `Negate`.
5
- * Used by `canonical-negate` and `simplify`
6
- */
7
- export declare function applyNegate(expr: Expression): Expression;
8
- export declare function ungroup(expr: Expression | null): Expression;
9
- export declare function applyPower(engine: ComputeEngine, expr: Expression): Expression;
10
- /** Used by `simplify` and `canonical` to simplify some arithmetic
11
- * and trigonometric constants */
12
- export declare function applyConstants(expr: Expression): Expression;
1
+ /* 0.4.4 */
2
+ export declare const ARITHMETIC_DICTIONARY: Dictionary[];
@@ -1,2 +1,2 @@
1
- /* 0.4.1 */
1
+ /* 0.4.4 */
2
2
  export declare const COLLECTIONS_DICTIONARY: Dictionary;
@@ -1,2 +1,2 @@
1
- /* 0.4.1 */
2
- export declare const CORE_DICTIONARY: Dictionary;
1
+ /* 0.4.4 */
2
+ export declare const CORE_DICTIONARY: Dictionary[];