@cortex-js/compute-engine 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/compute-engine.esm.js +15779 -12531
  2. package/dist/compute-engine.min.esm.js +2 -2
  3. package/dist/compute-engine.min.js +2 -2
  4. package/dist/math-json.esm.js +62 -43
  5. package/dist/math-json.min.esm.js +2 -2
  6. package/dist/math-json.min.js +2 -2
  7. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  8. package/dist/types/common/signals.d.ts +1 -1
  9. package/dist/types/compute-engine/assume.d.ts +2 -2
  10. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +22 -10
  11. package/dist/types/compute-engine/boxed-expression/box.d.ts +2 -14
  12. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +4 -4
  13. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +36 -19
  14. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +14 -13
  16. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +3 -3
  17. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +5 -5
  18. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -3
  19. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +9 -6
  20. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +24 -10
  21. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/utils.d.ts +23 -6
  25. package/dist/types/compute-engine/compute-engine.d.ts +74 -50
  26. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  27. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  28. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  29. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  30. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  31. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -3
  32. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  33. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  34. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +8 -7
  39. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +3 -3
  40. package/dist/types/compute-engine/latex-syntax/parse.d.ts +87 -62
  41. package/dist/types/compute-engine/latex-syntax/public.d.ts +65 -46
  42. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +6 -4
  45. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +1 -1
  46. package/dist/types/compute-engine/library/arithmetic-add.d.ts +11 -0
  47. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +15 -0
  48. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +19 -0
  49. package/dist/types/compute-engine/{dictionary → library}/arithmetic-power.d.ts +1 -1
  50. package/dist/types/compute-engine/library/arithmetic.d.ts +2 -0
  51. package/dist/types/compute-engine/library/calculus.d.ts +2 -0
  52. package/dist/types/compute-engine/library/collections.d.ts +2 -0
  53. package/dist/types/compute-engine/library/core.d.ts +2 -0
  54. package/dist/types/compute-engine/library/domains.d.ts +15 -0
  55. package/dist/types/compute-engine/library/library.d.ts +17 -0
  56. package/dist/types/compute-engine/library/logic.d.ts +2 -0
  57. package/dist/types/compute-engine/library/polynomials.d.ts +2 -0
  58. package/dist/types/compute-engine/library/relational-operator.d.ts +2 -0
  59. package/dist/types/compute-engine/library/sets.d.ts +2 -0
  60. package/dist/types/compute-engine/library/trigonometry.d.ts +2 -0
  61. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  62. package/dist/types/compute-engine/numerics/numeric-decimal.d.ts +5 -1
  63. package/dist/types/compute-engine/numerics/numeric.d.ts +4 -2
  64. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  65. package/dist/types/compute-engine/public.d.ts +237 -187
  66. package/dist/types/compute-engine/rules.d.ts +1 -1
  67. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  68. package/dist/types/compute-engine/symbolic/expand.d.ts +1 -1
  69. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  70. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  71. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  72. package/dist/types/compute-engine/symbolic/product.d.ts +1 -1
  73. package/dist/types/compute-engine/symbolic/sum.d.ts +2 -2
  74. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  75. package/dist/types/compute-engine.d.ts +2 -2
  76. package/dist/types/math-json/math-json-format.d.ts +27 -23
  77. package/dist/types/math-json/utils.d.ts +22 -16
  78. package/dist/types/math-json.d.ts +3 -3
  79. package/package.json +20 -20
  80. package/dist/types/compute-engine/dictionary/arithmetic-add.d.ts +0 -11
  81. package/dist/types/compute-engine/dictionary/arithmetic-divide.d.ts +0 -9
  82. package/dist/types/compute-engine/dictionary/arithmetic-multiply.d.ts +0 -17
  83. package/dist/types/compute-engine/dictionary/arithmetic.d.ts +0 -2
  84. package/dist/types/compute-engine/dictionary/collections.d.ts +0 -2
  85. package/dist/types/compute-engine/dictionary/core.d.ts +0 -2
  86. package/dist/types/compute-engine/dictionary/dictionary.d.ts +0 -26
  87. package/dist/types/compute-engine/dictionary/logic.d.ts +0 -2
  88. package/dist/types/compute-engine/dictionary/polynomials.d.ts +0 -2
  89. package/dist/types/compute-engine/dictionary/relational-operator.d.ts +0 -2
  90. package/dist/types/compute-engine/dictionary/sets.d.ts +0 -2
  91. package/dist/types/compute-engine/dictionary/trigonometry.d.ts +0 -2
@@ -1,24 +1,35 @@
1
- /* 0.6.0 */
2
- import { ParseLatexOptions, LatexToken, NumberFormattingOptions, Delimiter, Terminator, Parser } from './public';
1
+ /* 0.7.0 */
3
2
  import { IndexedLatexDictionary, InfixEntry, PostfixEntry, PrefixEntry, SymbolEntry } from './dictionary/definitions';
4
- import { WarningSignalHandler } from '../../common/signals';
5
3
  import { IComputeEngine } from '../public';
4
+ import { Expression } from '../../math-json/math-json-format';
6
5
  export declare const DEFAULT_LATEX_NUMBER_OPTIONS: NumberFormattingOptions;
7
6
  export declare const DEFAULT_PARSE_LATEX_OPTIONS: ParseLatexOptions;
8
7
  export declare class _Parser implements Parser {
9
- readonly onError: WarningSignalHandler;
8
+ readonly computeEngine: IComputeEngine;
10
9
  readonly options: NumberFormattingOptions & ParseLatexOptions;
11
- readonly engine: IComputeEngine;
12
- readonly _dictionary: IndexedLatexDictionary;
13
10
  index: number;
14
- _lastPeek: string;
15
- _peekCounter: number;
16
- readonly _tokens: LatexToken[];
17
- constructor(tokens: LatexToken[], options: NumberFormattingOptions & ParseLatexOptions, dictionary: IndexedLatexDictionary, computeEngine: IComputeEngine, onError: WarningSignalHandler);
11
+ private readonly _tokens;
12
+ private _positiveInfinityTokens;
13
+ private _negativeInfinityTokens;
14
+ private _notANumberTokens;
15
+ private _decimalMarkerTokens;
16
+ private _groupSeparatorTokens;
17
+ private _exponentProductTokens;
18
+ private _beginExponentMarkerTokens;
19
+ private _endExponentMarkerTokens;
20
+ private _truncationMarkerTokens;
21
+ private _beginRepeatingDigitsTokens;
22
+ private _endRepeatingDigitsTokens;
23
+ private _imaginaryNumberTokens;
24
+ private readonly _dictionary;
25
+ private _boundaries;
26
+ private _lastPeek;
27
+ private _peekCounter;
28
+ constructor(tokens: LatexToken[], options: NumberFormattingOptions & ParseLatexOptions, dictionary: IndexedLatexDictionary, computeEngine: IComputeEngine);
18
29
  updateOptions(opt: Partial<NumberFormattingOptions> & Partial<ParseLatexOptions>): void;
19
- clone(start: number, end: number): Parser;
20
30
  get atEnd(): boolean;
21
31
  get peek(): LatexToken;
32
+ next(): LatexToken;
22
33
  /**
23
34
  * Return true if
24
35
  * - at end of the token stream
@@ -27,12 +38,18 @@ export declare class _Parser implements Parser {
27
38
  * Note: the `minPrec` condition is not checked. It should be checked separately.
28
39
  */
29
40
  atTerminator(t?: Terminator): boolean;
41
+ /** True if the current token matches any of the boundaries we are waiting for */
42
+ get atBoundary(): boolean;
43
+ addBoundary(boundary: LatexToken[]): void;
44
+ removeBoundary(): void;
45
+ matchBoundary(): boolean;
46
+ boundaryError(msg: string | [string, ...Expression[]]): Expression;
30
47
  latex(start: number, end?: number): string;
31
48
  latexAhead(n: number): string;
32
49
  latexBefore(): string;
33
50
  latexAfter(): string;
34
51
  /**
35
- * Return at most `lookahead` strings made from the tokens
52
+ * Return at most `this._dictionary.lookahead` strings made from the tokens
36
53
  * ahead.
37
54
  *
38
55
  * The index in the returned array correspond to the number of tokens.
@@ -44,13 +61,16 @@ export declare class _Parser implements Parser {
44
61
  */
45
62
  lookAhead(): string[];
46
63
  /** Return all the definitions that potentially match the tokens ahead */
64
+ peekDefinitions(kind: 'function'): [FunctionEntry, number][] | null;
47
65
  peekDefinitions(kind: 'symbol'): [SymbolEntry, number][] | null;
48
66
  peekDefinitions(kind: 'postfix'): [PostfixEntry, number][] | null;
49
- peekDefinitions(kind: 'postfix'): [PostfixEntry, number][] | null;
50
- peekDefinitions(kind: 'operator'): [InfixEntry | PrefixEntry | PostfixEntry, number][] | null;
51
67
  peekDefinitions(kind: 'infix'): [InfixEntry, number][] | null;
52
68
  peekDefinitions(kind: 'prefix'): [PrefixEntry, number][] | null;
53
- next(): LatexToken;
69
+ peekDefinitions(kind: 'operator'): [InfixEntry | PrefixEntry | PostfixEntry, number][] | null;
70
+ skipSpaceTokens(): void;
71
+ /** While parsing in math mode, skip applicable spaces.
72
+ * Do not use to skip spaces e.g. while parsing a string. See `skipSpaceTokens()` instead.
73
+ */
54
74
  skipSpace(): boolean;
55
75
  matchChar(): string | null;
56
76
  matchColor(_background?: boolean): string | null;
@@ -58,11 +78,16 @@ export declare class _Parser implements Parser {
58
78
  match(token: LatexToken): boolean;
59
79
  matchAll(tokens: LatexToken | LatexToken[]): boolean;
60
80
  matchAny(tokens: LatexToken[]): LatexToken;
61
- matchWhile(tokens: LatexToken[]): LatexToken[];
62
- matchSign(): string;
63
- matchDecimalDigits(): string;
64
- matchSignedInteger(): string;
81
+ matchSequence(tokens: LatexToken[]): LatexToken[];
82
+ matchOptionalSign(): string;
83
+ matchDecimalDigits(options?: {
84
+ withGrouping?: boolean;
85
+ }): string;
86
+ matchSignedInteger(options?: {
87
+ withGrouping?: boolean;
88
+ }): string;
65
89
  matchExponent(): string;
90
+ matchRepeatingDecimal(): string;
66
91
  matchNumber(): string;
67
92
  /**
68
93
  * A Latex number can be a decimal, hex or octal number.
@@ -80,13 +105,11 @@ export declare class _Parser implements Parser {
80
105
  matchInfixOperator(lhs: Expression, until?: Terminator): Expression | null;
81
106
  /**
82
107
  * - 'enclosure' : will look for an argument inside an enclosure (open/close fence)
83
- * - 'implicit': either an expression inside a pair of `()`, or just a primary
84
- * (i.e. we interpret `\cos x + 1` as `\cos(x) + 1`)
85
- * - 'group': the arguments follow until an end of group, `<}>`. This is the
86
- * case for example for `\displaystyle`
108
+ * - 'implicit': either an expression inside a pair of `()`, or just a product
109
+ * (i.e. we interpret `\cos 2x + 1` as `\cos(2x) + 1`)
87
110
  */
88
- matchArguments(kind: undefined | '' | 'enclosure' | 'implicit' | 'group'): Expression[] | null;
89
- /** If matches the normalized open delimiter, returns the
111
+ matchArguments(kind: undefined | '' | 'enclosure' | 'implicit'): Expression[] | null;
112
+ /** If matches the normalized open delimiter, return the
90
113
  * expected closing delimiter.
91
114
  *
92
115
  * For example, if `delimiter` is `(`, it would match `\left\lparen` and
@@ -97,55 +120,56 @@ export declare class _Parser implements Parser {
97
120
  matchOpenDelimiter(openDelim: Delimiter, closeDelim: Delimiter): LatexToken[] | null;
98
121
  matchMiddleDelimiter(delimiter: '|' | ':' | LatexToken): boolean;
99
122
  /**
100
- * An enclosure is an opening matchfix operator, an expression, optionally
101
- * followed multiple times by a separator and another expression,
123
+ * An enclosure is an opening matchfix operator, an optional expression,
124
+ * optionally followed multiple times by a separator and another expression,
102
125
  * and finally a closing matching operator.
103
126
  */
104
127
  matchEnclosure(): Expression | null;
105
128
  /**
106
- * A symbol can be:
107
- * - a constant: `\pi`
108
- * - a single-letter variable: `x`
109
- * - a multi-letter variable: `\mathit{speed}` or `\mathrm{speed}`
110
- * - a function with explicit arguments `f(x)`
111
- * - a function with implicit arguments: `\cos x`
112
- * - a command: `\frac{2}{3}`
129
+ * Match a single variable name. It can be:
130
+ * - a symbol
131
+ * - a simple multi-letter identifier: `\mathrm{speed}`
132
+ * - a complex multi-letter identifier: `\alpha_12` or `\mathit{speed\unicode{"2012}of\unicode{"2012}sound}`
133
+ * - a command: `\alpha` @todo
134
+ * - a complex name such as `\alpha_12` or `\mathit{speed\unicode{"2012}of\unicode{"2012}sound}` (see serializer.ts) @todo:
135
+ * @todo: matchSymbol should use matchIdentifier
113
136
  */
114
- matchSymbol(): Expression | null;
115
- matchOptionalLatexArgument(): Expression | null;
137
+ matchIdentifier(): string | null;
116
138
  /**
117
- * Match a required LaTeX argument:
118
- * - either enclosed in `{}`
119
- * - or a single token.
139
+ * A function is a function identifier followed by arguments
140
+ * - a function with explicit arguments `f(x)`
141
+ * - a function with explicit arguments `\mathrm{floor}(x)`
142
+ * - a function name: `\mathrm{floor}`
143
+ * - a function with implicit arguments: `\cos x` (via a custom parser)
120
144
  *
121
- * Return null if an argument was not found
122
- * Return 'Nothing' if an empty argument `{}` was found
123
145
  */
124
- matchRequiredLatexArgument(): Expression | null;
146
+ matchFunction(): Expression | null;
125
147
  /**
126
- * Match a sequence superfix/subfix operator, e.g. `^{*}`
127
- *
128
- * Superfix and subfix need special handling:
129
- *
130
- * - they act mostly like an infix operator, but they are commutative, i.e.
131
- * `x_a^b` should be parsed identically to `x^b_a`.
132
- *
133
- * - furthermore, in LaTeX, consecutive `^` or `_` are treated as concatenated,
134
- * that is `x^a^b` parses the same as `x^{ab}`.
135
- *
148
+ * A symbol can be:
149
+ * - a single-letter variable: `x`
150
+ * - a single LaTeX command: `\pi`
136
151
  */
152
+ matchSymbol(): Expression | null;
153
+ matchOptionalLatexArgument(): Expression | null;
154
+ matchRequiredLatexArgument(): Expression | null;
137
155
  matchSupsub(lhs: Expression | null): Expression | null;
138
156
  matchPostfix(lhs: Expression | null): Expression | null;
139
- matchString(until: Partial<Terminator>): string;
140
- matchEnvironmentName(command: '\\begin' | '\\end', envName: string): boolean;
157
+ /** Match a string used as a LaTeX identifier, for example an environment
158
+ * name.
159
+ * Not suitable for general purpose text, e.g. argument of a `\text{}
160
+ * command. See `matchChar()` instead.
161
+ */
162
+ matchString(): string;
163
+ /** Match a string as an argument (in a `{}` pair) */
164
+ matchStringArgument(): string | null;
141
165
  /**
142
- * Match an expression in a tabular format,
143
- * where row are separated by `\\` and columns by `&`
166
+ * Match an expression in a tabular format, where rows are separated by `\\`
167
+ * and columns by `&`.
144
168
  *
145
- * Return rows of sparse columns as a list: empty rows are indicated with NOTHING,
146
- * and empty cells are also indicated with NOTHING.
169
+ * Return rows of sparse columns: empty rows are indicated with `Nothing`,
170
+ * and empty cells are also indicated with `Nothing`.
147
171
  */
148
- matchTabular(endName: string): null | Expression;
172
+ matchTabular(): null | Expression[][];
149
173
  matchEnvironment(): Expression | null;
150
174
  /**
151
175
  * Apply an invisible operator between two expressions.
@@ -168,8 +192,8 @@ export declare class _Parser implements Parser {
168
192
  * - x2 -> no
169
193
  * => lhs is a number, rhs is a number, but not a literal
170
194
  */
171
- applyInvisibleOperator(terminator: Terminator, rawLhs: Expression | null): Expression | null;
172
- matchUnknownLatexCommand(): Expression | null;
195
+ applyInvisibleOperator(terminator: Terminator, lhs: Expression | null): Expression | null;
196
+ matchUnexpectedLatexCommand(): Expression | null;
173
197
  /**
174
198
  * <primary> :=
175
199
  * (<number> | <symbol> | <environment> | <matchfix-expr>) <subsup>* <postfix-operator>*
@@ -189,11 +213,12 @@ export declare class _Parser implements Parser {
189
213
  * | <prefix-op> <primary>
190
214
  * | <primary> <infix-op> <expression>
191
215
  *
192
- * Stop when an operator of precedence less than `minPrec` is encountered
216
+ * Stop when an operator of precedence less than `until.minPrec` is encountered
193
217
  */
194
218
  matchExpression(until?: Partial<Terminator>): Expression | null;
195
219
  /**
196
220
  * Add LaTeX or other requested metadata to the expression
197
221
  */
198
222
  decorate(expr: Expression | null, start: number): Expression | null;
223
+ error(code: string | [string, ...Expression[]], fromToken: number): Expression;
199
224
  }
@@ -1,4 +1,4 @@
1
- /* 0.6.0 */
1
+ /* 0.7.0 */
2
2
  import { Expression } from '../../math-json/math-json-format';
3
3
  import type { IComputeEngine } from '../public';
4
4
  /**
@@ -18,17 +18,15 @@ export declare type LatexString = string;
18
18
  * record to define new LaTeX dictionary entries.
19
19
  */
20
20
  export declare type Delimiter = ')' | '(' | ']' | '[' | '{' /** \lbrace */ | '}' /** \rbrace */ | '<' /** \langle */ | '>' /** \rangle */ | '|' | '||' | '\\lceil' | '\\rceil' | '\\lfloor' | '\\rfloor';
21
- export declare type DictionaryCategory = 'algebra' | 'arithmetic' | 'calculus' | 'collections' | 'combinatorics' | 'core' | 'dimensions' | 'domains' | 'linear-algebra' | 'logic' | 'numeric' | 'other' | 'physics' | 'polynomials' | 'relop' | 'sets' | 'statistics' | 'symbols' | 'trigonometry' | 'units';
21
+ export declare type LibraryCategory = 'algebra' | 'arithmetic' | 'calculus' | 'collections' | 'control-structures' | 'combinatorics' | 'core' | 'dimensions' | 'domains' | 'linear-algebra' | 'logic' | 'numeric' | 'other' | 'physics' | 'polynomials' | 'relop' | 'sets' | 'statistics' | 'styling' | 'symbols' | 'trigonometry' | 'units';
22
22
  /**
23
23
  * This indicates a condition under which parsing should stop:
24
- * - a specific string of tokens has been encountered
25
24
  * - an operator of a precedence higher than specified has been encountered
26
25
  * - the last token has been reached
27
26
  * - or if a function is provided, the function returns true;
28
27
  */
29
28
  export declare type Terminator = {
30
29
  minPrec: number;
31
- tokens?: LatexToken[];
32
30
  condition?: (parser: Parser) => boolean;
33
31
  };
34
32
  /**
@@ -54,11 +52,13 @@ export declare type Terminator = {
54
52
  */
55
53
  export declare type EnvironmentParseHandler = (parser: Parser, reqArgs: Expression[], optArgs: Expression[]) => Expression | null;
56
54
  export declare type SymbolParseHandler = (parser: Parser) => Expression | null;
55
+ export declare type FunctionParseHandler = (parser: Parser) => Expression | null;
57
56
  export declare type PostfixParseHandler = (parser: Parser, lhs: Expression) => Expression | null;
58
57
  export declare type PrefixParseHandler = (parser: Parser, until: Terminator) => Expression | null;
59
58
  export declare type InfixParseHandler = (parser: Parser, until: Terminator, lhs: Expression) => Expression | null;
60
59
  export declare type MatchfixParseHandler = (parser: Parser, body: Expression) => Expression | null;
61
- export declare type ParseHandler = SymbolParseHandler | EnvironmentParseHandler | PostfixParseHandler | PrefixParseHandler | InfixParseHandler | MatchfixParseHandler;
60
+ export declare type ParseHandler = SymbolParseHandler | FunctionParseHandler | EnvironmentParseHandler | PostfixParseHandler | PrefixParseHandler | InfixParseHandler | MatchfixParseHandler;
61
+ export declare type LatexArgumentType = '{expression}' /** A required math mode expression */ | '[expression]' /** An optional math mode expression */ | '{text}' /** A required expression in text mode */ | '[text]' /** An optional expression in text mode */ | '{unit}' /** A required unit expression, e.g. `3em` */ | '[unit]' /** An optional unit expression, e.g. `3em` */ | '{glue}' /** A required glue expression, e.g. `25 mu plus 3em ` */ | '[glue]' /** An optional glue expression, e.g. `25 mu plus 3em ` */ | '{string}' /** A required text string, terminated by a non-literal token */ | '[string]' /** An optional text string, terminated by a non-literal token */ | '{color}' /** A required color expression, e.g. `red` or `#00ff00` */ | '[color]'; /** An optional color expression, e.g. `red` or `#00ff00` */
62
62
  /**
63
63
  * Maps a string of LaTeX tokens to a function or symbol and vice-versa.
64
64
  *
@@ -163,6 +163,10 @@ export declare type SymbolEntry = BaseEntry & {
163
163
  kind: 'symbol';
164
164
  /** Used for appropriate wrapping (i.e. when to surround it with parens) */
165
165
  precedence?: number;
166
+ parse: Expression | SymbolParseHandler;
167
+ };
168
+ export declare type FunctionEntry = BaseEntry & {
169
+ kind: 'function';
166
170
  /**
167
171
  * Indicate if this symbol can be followed by arguments.
168
172
  *
@@ -188,41 +192,21 @@ export declare type SymbolEntry = BaseEntry & {
188
192
  * "f x" -> `["f", "x"]`
189
193
  *
190
194
  */
191
- /**
192
- *
193
- * If a LaTeX command (i.e. the trigger starts with `\`, e.g. `\sqrt`)
194
- * indicates the number of optional arguments expected (indicate with
195
- * square brackets).
196
- *
197
- * For example, for the `\sqrt` command, 1: `\sqrt[3]{x}`
198
- *
199
- */
200
- optionalLatexArg?: number;
201
- /**
202
- *
203
- * If a LaTeX command (i.e. the trigger starts with `\`, e.g. `\frac`)
204
- * indicates the number of required arguments expected (indicated with
205
- * curly braces).
206
- *
207
- * For example, for the `\frac` command, 2: `\frac{1}{n}`
208
- *
209
- */
210
- requiredLatexArg?: number;
211
- parse: Expression | SymbolParseHandler;
195
+ parse: Expression | FunctionParseHandler;
212
196
  };
213
197
  /**
214
198
  * A simple LaTeX dictionary entry, for example for a command like `\pi`.
215
199
  */
216
200
  export declare type DefaultEntry = BaseEntry & {
217
201
  precedence?: number;
218
- optionalLatexArg?: number;
219
- requiredLatexArg?: number;
220
202
  parse?: Expression | SymbolParseHandler;
221
203
  };
222
- export declare type LatexDictionaryEntry = DefaultEntry | MatchfixEntry | InfixEntry | PostfixEntry | PrefixEntry | SymbolEntry | EnvironmentEntry;
204
+ export declare type LatexDictionaryEntry = DefaultEntry | MatchfixEntry | InfixEntry | PostfixEntry | PrefixEntry | SymbolEntry | FunctionEntry | EnvironmentEntry;
223
205
  /** @internal */
224
206
  export declare function isSymbolEntry(entry: LatexDictionaryEntry): entry is SymbolEntry;
225
207
  /** @internal */
208
+ export declare function isFunctionEntry(entry: LatexDictionaryEntry): entry is FunctionEntry;
209
+ /** @internal */
226
210
  export declare function isMatchfixEntry(entry: LatexDictionaryEntry): entry is MatchfixEntry;
227
211
  /** @internal */
228
212
  export declare function isInfixEntry(entry: LatexDictionaryEntry): entry is InfixEntry;
@@ -254,7 +238,7 @@ export declare type ParseLatexOptions = {
254
238
  /**
255
239
  * If true, ignore space characters.
256
240
  *
257
- * Default: `true`
241
+ * **Default**: `true`
258
242
  *
259
243
  */
260
244
  skipSpace: boolean;
@@ -273,7 +257,7 @@ export declare type ParseLatexOptions = {
273
257
  * Otherwise, return each token making up the number (minus sign, digits,
274
258
  * decimal separator, etc...).
275
259
  *
276
- * Default: `true`
260
+ * **Default**: `true`
277
261
  */
278
262
  parseNumbers: boolean;
279
263
  /**
@@ -302,6 +286,8 @@ export declare type ParseLatexOptions = {
302
286
  * may contain a slightly different LaTeX, for example with consecutive spaces
303
287
  * replaced by one, with comments removed and with some low-level LaTeX
304
288
  * commands replaced, for example `\egroup` and `\bgroup`.
289
+ *
290
+ * **Default:** `false`
305
291
  */
306
292
  preserveLatex: boolean;
307
293
  };
@@ -330,8 +316,8 @@ export declare type SerializeLatexOptions = {
330
316
  */
331
317
  multiply: LatexString;
332
318
  /**
333
- * When an expression contains the symbol `Missing`, serialize it
334
- * with this LaTeX string
319
+ * When an expression contains the error expression `["Error", 'missing']`,
320
+ * serialize it with this LaTeX string
335
321
  */
336
322
  missingSymbol: LatexString;
337
323
  applyFunctionStyle: (expr: Expression, level: number) => 'paren' | 'leftright' | 'big' | 'none';
@@ -353,10 +339,10 @@ export declare type NumberFormattingOptions = {
353
339
  * the '.' in '3.1415'.
354
340
  *
355
341
  * Some countries use a comma rather than a dot. In this case it is
356
- * recommended to use `"{,}"` as the marker: the surrounding bracket ensure
342
+ * recommended to use `"{,}"` as the marker: the surrounding brackets ensure
357
343
  * there is no additional gap after the comma.
358
344
  *
359
- * Default: `"."`
345
+ * **Default**: `"."`
360
346
  */
361
347
  decimalMarker: LatexString;
362
348
  /**
@@ -365,10 +351,10 @@ export declare type NumberFormattingOptions = {
365
351
  *
366
352
  * If you change it to another value, be aware that this may lead to
367
353
  * unexpected results. For example, if changing it to `,` the expression
368
- * `x_{1,2}` will parse as `x` with a subscript of `1.2`, rather than `x`
369
- * with two subscripts, `1` and `2`.
354
+ * `\mathrm{Hypot}(1,2)` will parse as `["Hypot", 1.2]` rather than
355
+ * `["Hypot", 1, 2]`.
370
356
  *
371
- * Default: `"\\,"` (thin space, 3/18mu) (Resolution 7 of the 1948 CGPM)
357
+ * **Default**: `"\\,"` (thin space, 3/18mu) (Resolution 7 of the 1948 CGPM)
372
358
  */
373
359
  groupSeparator: LatexString;
374
360
  exponentProduct: LatexString;
@@ -416,7 +402,7 @@ export declare class LatexSyntax {
416
402
  * Use the value returned by this function to the `options` argument of the
417
403
  * constructor.
418
404
  */
419
- static getDictionary(domain?: DictionaryCategory | 'all'): Readonly<LatexDictionary>;
405
+ static getDictionary(domain?: LibraryCategory | 'all'): Readonly<LatexDictionary>;
420
406
  parse(latex: LatexString): Expression;
421
407
  serialize(expr: Expression): LatexString;
422
408
  }
@@ -436,7 +422,11 @@ export interface Serializer {
436
422
  level: number;
437
423
  /** Output a LaTeX string representing the expression */
438
424
  serialize: (expr: Expression | null) => string;
439
- wrapString(s: string, style: 'paren' | 'leftright' | 'big' | 'none'): string;
425
+ wrapString(s: string, style: 'paren' | 'leftright' | 'big' | 'none', fence?: string): string;
426
+ /** A string with the arguments of expr fenced appropriately and separated by
427
+ * commas.
428
+ */
429
+ wrapArguments(expr: Expression): string;
440
430
  /** Add a group fence around the expression if it is
441
431
  * an operator of precedence less than or equal to `prec`.
442
432
  */
@@ -456,7 +446,6 @@ export interface Serializer {
456
446
  }
457
447
  export declare type SerializeHandler = (serializer: Serializer, expr: Expression) => string;
458
448
  export interface Parser {
459
- readonly onError: WarningSignalHandler;
460
449
  readonly options: Required<ParseLatexOptions>;
461
450
  readonly computeEngine?: IComputeEngine;
462
451
  index: number;
@@ -478,6 +467,10 @@ export interface Parser {
478
467
  latexAfter(): string;
479
468
  /** If there are any space, advance the index until a non-space is encountered */
480
469
  skipSpace(): boolean;
470
+ addBoundary(boundary: LatexToken[]): void;
471
+ removeBoundary(): void;
472
+ matchBoundary(): boolean;
473
+ boundaryError(msg: string | [string, ...Expression[]]): Expression;
481
474
  /** If the next token is a character, return it and advance the index
482
475
  * This includes plain characters (e.g. 'a', '+'...), characters
483
476
  * defined in hex (^^ and ^^^^), the `\char` and `\unicode` command.
@@ -497,17 +490,17 @@ export interface Parser {
497
490
  match(tokens: LatexToken): boolean;
498
491
  matchAll(tokens: LatexToken | LatexToken[]): boolean;
499
492
  matchAny(tokens: LatexToken[]): LatexToken;
500
- matchWhile(tokens: LatexToken[]): LatexToken[];
493
+ matchSequence(tokens: LatexToken[]): LatexToken[];
501
494
  /** If the next token matches a `+` or `-` sign, return it and advance the index.
502
495
  * Otherwise return `''` and do not advance */
503
- matchSign(): string;
496
+ matchOptionalSign(): string;
504
497
  matchDecimalDigits(): string;
505
498
  matchSignedInteger(): string;
506
499
  matchExponent(): string;
507
500
  matchNumber(): string;
508
501
  /** Parse a tabular environment, until `\end{endName}`
509
502
  */
510
- matchTabular(endName: string): null | Expression;
503
+ matchTabular(endName: string): null | Expression[][];
511
504
  applyInvisibleOperator(terminator: Terminator, lhs: Expression | null): Expression | null;
512
505
  /** If the next tokens correspond to an optional LaTeX argument,
513
506
  * enclosed with `[` and `]` return the content of the argument
@@ -516,14 +509,21 @@ export interface Parser {
516
509
  * Otherwise, return `null`.
517
510
  */
518
511
  matchOptionalLatexArgument(): Expression | null;
512
+ /**
513
+ * Match a required LaTeX argument:
514
+ * - either enclosed in `{}`
515
+ * - or a single token.
516
+ *
517
+ * Return null if no argument was found
518
+ * Return `['Sequence']` if an empty argument `{}` was found
519
+ */
519
520
  matchRequiredLatexArgument(): Expression | null;
520
521
  /**
521
522
  * - 'enclosure' : will look for an argument inside an enclosure (an open/close fence)
522
- * - 'group': arguments follow directly the symbol, until an end of group token `<}>`
523
523
  * - 'implicit': either an expression inside a pair of `()`, or just a primary
524
524
  * (i.e. we interpret `\cos x + 1` as `\cos(x) + 1`)
525
525
  */
526
- matchArguments(kind: '' | 'group' | 'implicit' | 'enclosure'): Expression[] | null;
526
+ matchArguments(kind: '' | 'implicit' | 'enclosure'): Expression[] | null;
527
527
  /** If matches the normalized open delimiter, returns the
528
528
  * expected closing delimiter.
529
529
  *
@@ -533,6 +533,17 @@ export interface Parser {
533
533
  */
534
534
  matchOpenDelimiter(openDelim: Delimiter, closeDelim: Delimiter): LatexToken[] | null;
535
535
  matchMiddleDelimiter(delimiter: '|' | ':' | LatexToken): boolean;
536
+ /**
537
+ * Match a sequence superfix/subfix operator, e.g. `^{*}`
538
+ *
539
+ * Superfix and subfix need special handling:
540
+ *
541
+ * - they act mostly like an infix operator, but they are commutative, i.e.
542
+ * `x_a^b` should be parsed identically to `x^b_a`.
543
+ *
544
+ * - furthermore, in LaTeX `x^a^b` parses the same as `x^a{}^b`.
545
+ *
546
+ */
536
547
  matchSupsub(lhs: Expression | null): Expression | null;
537
548
  /**
538
549
  *
@@ -554,6 +565,12 @@ export interface Parser {
554
565
  * If not a primary, return `null` and do not advance the index.
555
566
  */
556
567
  matchPrimary(): Expression | null;
568
+ /**
569
+ * A symbol can be:
570
+ * - a single-letter variable: `x`
571
+ * - a single LaTeX command: `\pi`
572
+ */
573
+ matchSymbol(): Expression | null;
557
574
  /**
558
575
  * Parse an expression:
559
576
  *
@@ -572,4 +589,6 @@ export interface Parser {
572
589
  * `until` is `{ minPrec:0 }` by default.
573
590
  */
574
591
  matchExpression(until?: Partial<Terminator>): Expression | null;
592
+ /** Return an error expression with the specified code and arguments */
593
+ error(code: string | [string, ...Expression[]], fromToken: number): Expression;
575
594
  }
@@ -1,4 +1,4 @@
1
- /* 0.6.0 */
1
+ /* 0.7.0 */
2
2
  import { NumberFormattingOptions } from './public';
3
3
  export declare function serializeNumber(expr: Expression | null, options: NumberFormattingOptions): string;
4
4
  export declare function serializeEngineeringNotationNumber(value: number, options: NumberFormattingOptions): string;
@@ -1,4 +1,4 @@
1
- /* 0.6.0 */
1
+ /* 0.7.0 */
2
2
  export declare function getApplyFunctionStyle(_expr: Expression, _level: number): 'paren' | 'leftright' | 'big' | 'none';
3
3
  export declare function getGroupStyle(_expr: Expression, _level: number): 'paren' | 'leftright' | 'big' | 'none';
4
4
  export declare function getRootStyle(_expr: Expression | null, level: number): 'radical' | 'quotient' | 'solidus';
@@ -1,5 +1,5 @@
1
- /* 0.6.0 */
2
- import { NumberFormattingOptions, LatexString, SerializeLatexOptions } from './public';
1
+ /* 0.7.0 */
2
+ import { NumberFormattingOptions, LatexString, SerializeLatexOptions, FunctionEntry } from './public';
3
3
  import { IndexedLatexDictionary, SymbolEntry } from './dictionary/definitions';
4
4
  import { WarningSignalHandler } from '../../common/signals';
5
5
  export declare class Serializer {
@@ -19,8 +19,10 @@ export declare class Serializer {
19
19
  *
20
20
  */
21
21
  wrapShort(expr: Expression | null): string;
22
- wrapString(s: string, style: 'paren' | 'leftright' | 'big' | 'none'): string;
23
- serializeSymbol(expr: Expression, def?: SymbolEntry): string;
22
+ wrapString(s: string, style: 'paren' | 'leftright' | 'big' | 'none', fence?: string): string;
23
+ wrapArguments(expr: Expression): string;
24
+ serializeSymbol(expr: Expression, def?: SymbolEntry | FunctionEntry): string;
25
+ serializeFunction(expr: Expression, def?: FunctionEntry | SymbolEntry): string;
24
26
  serializeDictionary(dict: {
25
27
  [key: string]: Expression;
26
28
  }): string;
@@ -1,4 +1,4 @@
1
- /* 0.6.0 */
1
+ /* 0.7.0 */
2
2
  * ## Reference
3
3
  * TeX source code:
4
4
  * {@link http://tug.org/texlive/devsrc/Build/source/texk/web2c/tex.web | Tex.web}
@@ -0,0 +1,11 @@
1
+ /* 0.7.0 */
2
+ /** The canonical form of `Add`:
3
+ * - removes `0`
4
+ * - capture complex numbers (a + ib or ai +b)
5
+ * */
6
+ export declare function canonicalAdd(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
7
+ export declare function domainAdd(_ce: IComputeEngine, args: BoxedDomain[]): BoxedDomain | null;
8
+ export declare function simplifyAdd(ce: IComputeEngine, args: BoxedExpression[]): BoxedExpression | undefined;
9
+ export declare function evalAdd(ce: IComputeEngine, args: BoxedExpression[], mode?: 'N' | 'eval'): BoxedExpression;
10
+ export declare function canonicalSummation(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression | undefined): BoxedExpression;
11
+ export declare function evalSummation(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression, mode: 'simplify' | 'N' | 'evaluate'): BoxedExpression | undefined;
@@ -0,0 +1,15 @@
1
+ /* 0.7.0 */
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 numerator and denominator are integer literals, return a rational number
8
+ * or Rational experssion
9
+ * - if Divide, transform into Multiply/Power
10
+ */
11
+ export declare function canonicalDivide(ce: IComputeEngine, op1: BoxedExpression, op2: BoxedExpression): BoxedExpression;
12
+ /**
13
+ * Simplify form of 'Divide' (and 'Rational')
14
+ */
15
+ export declare function simplifyDivide(ce: IComputeEngine, op1: BoxedExpression, op2: BoxedExpression): BoxedExpression | undefined;
@@ -0,0 +1,19 @@
1
+ /* 0.7.0 */
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 simplifyMultiply(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression | undefined;
17
+ export declare function evalMultiply(ce: IComputeEngine, ops: BoxedExpression[], mode?: 'N' | 'evaluate'): BoxedExpression | undefined;
18
+ export declare function canonicalMultiplication(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression | undefined): BoxedExpression;
19
+ export declare function evalMultiplication(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression, mode: 'simplify' | 'evaluate' | 'N'): BoxedExpression | undefined;
@@ -1,4 +1,4 @@
1
- /* 0.6.0 */
1
+ /* 0.7.0 */
2
2
  /**
3
3
  *
4
4
  * Return `null` if there is no canonicalization necessary and the result is
@@ -0,0 +1,2 @@
1
+ /* 0.7.0 */
2
+ export declare const ARITHMETIC_LIBRARY: SymbolTable[];
@@ -0,0 +1,2 @@
1
+ /* 0.7.0 */
2
+ export declare const CALCULUS_LIBRARY: SymbolTable[];
@@ -0,0 +1,2 @@
1
+ /* 0.7.0 */
2
+ export declare const COLLECTIONS_LIBRARY: SymbolTable;
@@ -0,0 +1,2 @@
1
+ /* 0.7.0 */
2
+ export declare const CORE_LIBRARY: SymbolTable[];
@@ -0,0 +1,15 @@
1
+ /* 0.7.0 */
2
+ export declare const DOMAIN_CONSTRUCTORS: string[];
3
+ export declare const DOMAIN_ALIAS: {
4
+ NumericFunction: (string | string[])[];
5
+ RealFunction: (string | string[])[];
6
+ TrigonometricFunction: string[];
7
+ LogicOperator: (string | string[])[];
8
+ Predicate: (string | string[])[];
9
+ RelationalOperator: string[];
10
+ };
11
+ export declare function isDomainLiteral(s: string | null): s is DomainLiteral;
12
+ export declare function isSubdomainLiteral(lhs: string, rhs: string): boolean;
13
+ /** Return all the domain literals that are an ancestor of `dom`
14
+ */
15
+ export declare function ancestors(dom: string): string[];