@cortex-js/compute-engine 0.12.3 → 0.12.5

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 (89) hide show
  1. package/dist/compute-engine.esm.js +24382 -28195
  2. package/dist/compute-engine.js +24557 -0
  3. package/dist/compute-engine.min.esm.js +24540 -2
  4. package/dist/compute-engine.min.js +24553 -2
  5. package/dist/math-json.esm.js +256 -185
  6. package/dist/math-json.js +283 -0
  7. package/dist/math-json.min.esm.js +258 -2
  8. package/dist/math-json.min.js +283 -2
  9. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  10. package/dist/types/common/signals.d.ts +1 -1
  11. package/dist/types/common/utils.d.ts +1 -1
  12. package/dist/types/compute-engine/assume.d.ts +1 -1
  13. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +3 -1
  14. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  15. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +1 -1
  16. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -2
  17. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +1 -1
  18. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +4 -3
  19. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +2 -2
  20. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  21. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +1 -1
  22. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  25. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  26. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  27. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/validate.d.ts +1 -1
  29. package/dist/types/compute-engine/compute-engine.d.ts +4 -3
  30. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  31. package/dist/types/compute-engine/domain-utils.d.ts +1 -1
  32. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  33. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  34. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  35. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  36. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-inequalities.d.ts +1 -1
  37. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  38. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  39. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  40. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +2 -1
  41. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +25 -16
  43. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +15 -0
  45. package/dist/types/compute-engine/latex-syntax/parse.d.ts +31 -34
  46. package/dist/types/compute-engine/latex-syntax/public.d.ts +63 -66
  47. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +10 -6
  50. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +2 -1
  51. package/dist/types/compute-engine/library/arithmetic-add.d.ts +3 -2
  52. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +1 -1
  53. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +2 -2
  54. package/dist/types/compute-engine/library/arithmetic-power.d.ts +1 -1
  55. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  56. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  57. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  58. package/dist/types/compute-engine/library/core.d.ts +1 -1
  59. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  60. package/dist/types/compute-engine/library/library.d.ts +1 -1
  61. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  62. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  63. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  64. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  65. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  66. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  67. package/dist/types/compute-engine/library/utils.d.ts +1 -1
  68. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +1 -1
  69. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  70. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  71. package/dist/types/compute-engine/numerics/numeric.d.ts +1 -1
  72. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  73. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  74. package/dist/types/compute-engine/public.d.ts +17 -8
  75. package/dist/types/compute-engine/rules.d.ts +1 -1
  76. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  77. package/dist/types/compute-engine/solve.d.ts +1 -1
  78. package/dist/types/compute-engine/symbolic/expand.d.ts +18 -10
  79. package/dist/types/compute-engine/symbolic/flatten.d.ts +1 -1
  80. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  81. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  82. package/dist/types/compute-engine/symbolic/product.d.ts +4 -1
  83. package/dist/types/compute-engine/symbolic/sum.d.ts +2 -1
  84. package/dist/types/compute-engine/symbolic/utils.d.ts +1 -1
  85. package/dist/types/compute-engine.d.ts +2 -2
  86. package/dist/types/math-json/math-json-format.d.ts +1 -1
  87. package/dist/types/math-json/utils.d.ts +4 -1
  88. package/dist/types/math-json.d.ts +2 -2
  89. package/package.json +5 -14
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { Serializer } from './serializer';
3
3
  import { Expression } from '../../math-json/math-json-format';
4
4
  import { WarningSignalHandler } from '../../common/signals';
@@ -0,0 +1,15 @@
1
+ /* 0.12.5 */
2
+ import { Parser } from './public';
3
+ /** For error handling, if we have a identifier prefix, assume
4
+ * the identifier is invalid (it would have been captured by
5
+ * `mathIdentifier()` otherwise) and return an error expression */
6
+ export declare function matchInvalidIdentifier(parser: Parser): Expression | null;
7
+ /**
8
+ * Match an identifier. It can be:
9
+ * - a sequence of emojis: `👍🏻👍🏻👍🏻`
10
+ * - a single-letter identifier: `a`
11
+ * - some LaTeX commands: `\alpha`
12
+ * - a multi-letter identifier with a prefix: `\mathrm{speed}`
13
+ * - an identifier with modifiers: `\mathrm{\alpha_{12}}` or `\mathit{speed\unicode{"2012}of\unicode{"2012}sound}`
14
+ */
15
+ export declare function matchIdentifier(parser: Parser): string | null;
@@ -1,5 +1,5 @@
1
- /* 0.12.3 */
2
- import { IndexedLatexDictionary, InfixEntry, PostfixEntry, PrefixEntry, SymbolEntry } from './dictionary/definitions';
1
+ /* 0.12.5 */
2
+ import { IndexedLatexDictionary, IndexedInfixEntry, IndexedPostfixEntry, IndexedPrefixEntry, IndexedIdentifierEntry } from './dictionary/definitions';
3
3
  import { IComputeEngine } from '../public';
4
4
  import { Expression } from '../../math-json/math-json-format';
5
5
  export declare const DEFAULT_LATEX_NUMBER_OPTIONS: NumberFormattingOptions;
@@ -33,11 +33,10 @@ export declare class _Parser implements Parser {
33
33
  /**
34
34
  * Return true if
35
35
  * - at end of the token stream
36
- * - the upcoming tokens match `t.tokens`
37
36
  * - the `t.condition` function returns true
38
37
  * Note: the `minPrec` condition is not checked. It should be checked separately.
39
38
  */
40
- atTerminator(t?: Terminator): boolean;
39
+ atTerminator(t?: Partial<Terminator>): boolean;
41
40
  /** True if the current token matches any of the boundaries we are waiting for */
42
41
  get atBoundary(): boolean;
43
42
  addBoundary(boundary: LatexToken[]): void;
@@ -62,11 +61,11 @@ export declare class _Parser implements Parser {
62
61
  lookAhead(): string[];
63
62
  /** Return all the definitions that potentially match the tokens ahead */
64
63
  peekDefinitions(kind: 'function'): [FunctionEntry, number][] | null;
65
- peekDefinitions(kind: 'symbol'): [SymbolEntry, number][] | null;
66
- peekDefinitions(kind: 'postfix'): [PostfixEntry, number][] | null;
67
- peekDefinitions(kind: 'infix'): [InfixEntry, number][] | null;
68
- peekDefinitions(kind: 'prefix'): [PrefixEntry, number][] | null;
69
- peekDefinitions(kind: 'operator'): [InfixEntry | PrefixEntry | PostfixEntry, number][] | null;
64
+ peekDefinitions(kind: 'identifier'): [IndexedIdentifierEntry, number][] | null;
65
+ peekDefinitions(kind: 'postfix'): [IndexedPostfixEntry, number][] | null;
66
+ peekDefinitions(kind: 'infix'): [IndexedInfixEntry, number][] | null;
67
+ peekDefinitions(kind: 'prefix'): [IndexedPrefixEntry, number][] | null;
68
+ peekDefinitions(kind: 'operator'): [IndexedInfixEntry | IndexedPrefixEntry | IndexedPostfixEntry, number][] | null;
70
69
  /** Skip strictly `<space>` tokens.
71
70
  * To also skip `{}` see `skipSpace()`.
72
71
  * To skip visual space (e.g. `\,`) see `skipVisualSpace()`.
@@ -113,15 +112,21 @@ export declare class _Parser implements Parser {
113
112
  * - 'enclosure' : will look for an argument inside an enclosure (open/close fence)
114
113
  * - 'implicit': either an expression inside a pair of `()`, or just a product
115
114
  * (i.e. we interpret `\cos 2x + 1` as `\cos(2x) + 1`)
115
+ *
116
+ * This returns an array of arguments, or null if there is no match.
116
117
  */
117
- matchArguments(kind: undefined | '' | 'enclosure' | 'implicit'): Expression[] | null;
118
+ matchArguments(kind: undefined | '' | 'enclosure' | 'implicit', until?: Terminator): Expression[] | null;
118
119
  /**
119
120
  * A function can be followed by the following suffixes:
120
121
  * - a `\prime`, `\doubleprime`, `'`, `(n)` to indicate a derivative
121
122
  * - a subscript to indicate an argument
122
123
  * - an argument, optionally inside an enclosure
123
124
  */
124
- matchFunctionSuffix(): Expression | null;
125
+ matchFunctionSuffix(id: string): Expression;
126
+ /** A prime suffix is a sequence of `'`, `\prime` or `\doubleprime`
127
+ * after a function or in a superscript.
128
+ */
129
+ matchPrimeSuffix(): number;
125
130
  /** If matches the normalized open delimiter, return the
126
131
  * expected closing delimiter.
127
132
  *
@@ -143,33 +148,25 @@ export declare class _Parser implements Parser {
143
148
  * and finally a closing matching operator.
144
149
  */
145
150
  matchEnclosure(): Expression | null;
151
+ matchIdentifier(): string | null;
146
152
  /**
147
- * Match an identifier. It can be:
148
- * - a symbol
149
- * - a simple multi-letter identifier: `\mathrm{speed}`
150
- * - a complex multi-letter identifier: `\mathrm{\alpha_{12}}` or `\mathit{speed\unicode{"2012}of\unicode{"2012}sound}`
151
- * - a command: `\alpha` @todo
152
- */
153
- matchIdentifier(): string | Expression | null;
154
- /**
155
- * A function is a function identifier followed by arguments
156
- * - a function with explicit arguments `f(x)`
157
- * - a function with explicit arguments `\mathrm{floor}(x)`
158
- * - a function name: `\mathrm{floor}`
159
- * - a function with implicit arguments: `\cos x` (via a custom parser)
153
+ * A function is an identifier followed by arguments
154
+ * - a single letter identifier with explicit arguments `f(x)`
155
+ * - a multiletter identifier with explicit arguments `\mathrm{floor}(x)`
156
+ * - an identifier: `\mathrm{floor}`
157
+ * - a command with implicit arguments: `\cos x` (via a custom parser)
160
158
  *
161
159
  */
162
- matchFunction(): Expression | null;
160
+ matchFunction(until?: Partial<Terminator>): Expression | null;
163
161
  /**
164
- * A symbol can be:
165
- * - a single-letter variable: `x`
166
- * - a single LaTeX command: `\pi`
162
+ * A symbol is an identifier or a custom definition
167
163
  */
168
- matchSymbol(): Expression | null;
169
- matchOptionalLatexArgument(): Expression | null;
170
- matchRequiredLatexArgument(excluding?: string[]): Expression | null;
164
+ matchSymbol(until?: Partial<Terminator>): Expression | null;
165
+ matchLatexOptionalGroup(): Expression | null;
166
+ matchSingleAtomArgument(): Expression | null;
167
+ matchLatexGroup(): Expression | null;
171
168
  matchSupsub(lhs: Expression | null): Expression | null;
172
- matchPostfix(lhs: Expression | null): Expression | null;
169
+ matchPostfix(lhs: Expression | null, until?: Partial<Terminator>): Expression | null;
173
170
  /** Match a string used as a LaTeX identifier, for example an environment
174
171
  * name.
175
172
  * Not suitable for general purpose text, e.g. argument of a `\text{}
@@ -208,7 +205,7 @@ export declare class _Parser implements Parser {
208
205
  * - x2 -> no
209
206
  * => lhs is a number, rhs is a number, but not a literal
210
207
  */
211
- applyInvisibleOperator(terminator: Terminator, lhs: Expression | null): Expression | null;
208
+ applyInvisibleOperator(until: Terminator, lhs: Expression | null): Expression | null;
212
209
  matchUnexpectedLatexCommand(): Expression | null;
213
210
  /**
214
211
  * <primary> :=
@@ -220,7 +217,7 @@ export declare class _Parser implements Parser {
220
217
  * <matchfix-op-open> <expression> [<matchfix-op-separator> <expression>] <matchfix-op-close>
221
218
  *
222
219
  */
223
- matchPrimary(): Expression | null;
220
+ matchPrimary(until?: Partial<Terminator>): Expression | null;
224
221
  /**
225
222
  * Parse an expression:
226
223
  *
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { Expression } from '../../math-json/math-json-format';
3
3
  import type { IComputeEngine } from '../public';
4
4
  /**
@@ -23,7 +23,7 @@ export type LibraryCategory = 'algebra' | 'arithmetic' | 'calculus' | 'collectio
23
23
  * This indicates a condition under which parsing should stop:
24
24
  * - an operator of a precedence higher than specified has been encountered
25
25
  * - the last token has been reached
26
- * - or if a function is provided, the function returns true;
26
+ * - or if a condition is provided, the condition returns true;
27
27
  */
28
28
  export type Terminator = {
29
29
  minPrec: number;
@@ -51,13 +51,13 @@ export type Terminator = {
51
51
  *
52
52
  */
53
53
  export type EnvironmentParseHandler = (parser: Parser, reqArgs: Expression[], optArgs: Expression[]) => Expression | null;
54
- export type SymbolParseHandler = (parser: Parser) => Expression | null;
55
- export type FunctionParseHandler = (parser: Parser) => Expression | null;
56
- export type PostfixParseHandler = (parser: Parser, lhs: Expression) => Expression | null;
54
+ export type IdentifierParseHandler = (parser: Parser, until?: Terminator) => Expression | null;
55
+ export type FunctionParseHandler = (parser: Parser, until?: Terminator) => Expression | null;
56
+ export type PostfixParseHandler = (parser: Parser, lhs: Expression, until?: Terminator) => Expression | null;
57
57
  export type PrefixParseHandler = (parser: Parser, until: Terminator) => Expression | null;
58
58
  export type InfixParseHandler = (parser: Parser, until: Terminator, lhs: Expression) => Expression | null;
59
59
  export type MatchfixParseHandler = (parser: Parser, body: Expression) => Expression | null;
60
- export type ParseHandler = SymbolParseHandler | FunctionParseHandler | EnvironmentParseHandler | PostfixParseHandler | PrefixParseHandler | InfixParseHandler | MatchfixParseHandler;
60
+ export type ParseHandler = IdentifierParseHandler | FunctionParseHandler | EnvironmentParseHandler | PostfixParseHandler | PrefixParseHandler | InfixParseHandler | MatchfixParseHandler;
61
61
  export 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.
@@ -74,7 +74,7 @@ export type BaseEntry = {
74
74
  * However, an entry with no `name` can be used to define a synonym (for example
75
75
  * for the symbol `\varnothing` which is a synonym for `\emptyset`).
76
76
  *
77
- * If not `parse` handler is provided, only the trigger is used to select this
77
+ * If no `parse` handler is provided, only the trigger is used to select this
78
78
  * entry. Otherwise, if the trigger of the entry matches the current
79
79
  * token, the `parse` handler is invoked.
80
80
  */
@@ -159,39 +159,26 @@ export type EnvironmentEntry = BaseEntry & {
159
159
  kind: 'environment';
160
160
  parse: EnvironmentParseHandler;
161
161
  };
162
- export type SymbolEntry = BaseEntry & {
163
- kind: 'symbol';
162
+ export type IdentifierEntry = BaseEntry & {
163
+ kind: 'identifier';
164
164
  /** Used for appropriate wrapping (i.e. when to surround it with parens) */
165
165
  precedence?: number;
166
- parse: Expression | SymbolParseHandler;
166
+ parse: Expression | IdentifierParseHandler;
167
167
  };
168
+ /**
169
+ * A function has the following form:
170
+ * - a prefix such as `\mathrm` or `\operatorname`
171
+ * - a trigger string, such as `gcd`
172
+ * - some postfix operators such as `\prime`
173
+ * - an optional list of arguments in an enclosure (parentheses)
174
+ *
175
+ * For more complex situations, for example implicit arguments or
176
+ * inverse functions postfix (i.e. ^{-1}), use a custom parse handler with a
177
+ * base entry.
178
+ */
168
179
  export type FunctionEntry = BaseEntry & {
169
180
  kind: 'function';
170
- /**
171
- * Indicate if this symbol can be followed by arguments.
172
- *
173
- * The presence of arguments will indicate that the arguments should be
174
- * applied to the symbol. Otherwise, the invisible operator is applied
175
- * to the symbol and the arguments.
176
- *
177
- * If `arguments` is `"group"`:
178
- *
179
- * "f(x)" -> `["f", "x"]`
180
- * "f x" -> `["Multiply", "f", "x"]`
181
- *
182
- * If `arguments` is `""`:
183
- *
184
- * "f(x)" -> `["Multiply", "f", "x"]`
185
- * "f x" -> `["Multiply", "f", "x"]`
186
- *
187
- * If `arguments` is `"implicit"` and the symbol is followed either
188
- * by a group or by a primary (prefix + symbol + subsupfix + postfix).
189
- * Used for trig functions. i.e. `\sin x` vs `\sin(x)`:
190
- *
191
- * "f(x)" -> `["f", "x"]`
192
- * "f x" -> `["f", "x"]`
193
- *
194
- */
181
+ trigger: string;
195
182
  parse: Expression | FunctionParseHandler;
196
183
  };
197
184
  /**
@@ -199,11 +186,11 @@ export type FunctionEntry = BaseEntry & {
199
186
  */
200
187
  export type DefaultEntry = BaseEntry & {
201
188
  precedence?: number;
202
- parse?: Expression | SymbolParseHandler;
189
+ parse?: Expression | IdentifierParseHandler;
203
190
  };
204
- export type LatexDictionaryEntry = DefaultEntry | MatchfixEntry | InfixEntry | PostfixEntry | PrefixEntry | SymbolEntry | FunctionEntry | EnvironmentEntry;
191
+ export type LatexDictionaryEntry = DefaultEntry | MatchfixEntry | InfixEntry | PostfixEntry | PrefixEntry | IdentifierEntry | FunctionEntry | EnvironmentEntry;
205
192
  /** @internal */
206
- export declare function isSymbolEntry(entry: LatexDictionaryEntry): entry is SymbolEntry;
193
+ export declare function isIdentifierEntry(entry: LatexDictionaryEntry): entry is IdentifierEntry;
207
194
  /** @internal */
208
195
  export declare function isFunctionEntry(entry: LatexDictionaryEntry): entry is FunctionEntry;
209
196
  /** @internal */
@@ -222,7 +209,8 @@ export type ParseLatexOptions = {
222
209
  * This function is invoked when a number is followed by a symbol,
223
210
  * an open delimiter or a function.
224
211
  *
225
- * If this function is set to `null`, the lhs and rhs are joined as a `Sequence`.
212
+ * If this function is set to `null`, the lhs and rhs are joined as a
213
+ * `Sequence`.
226
214
  *
227
215
  * If this function is set to `undefined` it behaves in the following way:
228
216
  * - a number followed by a numeric expression is considered as separated
@@ -236,7 +224,7 @@ export type ParseLatexOptions = {
236
224
  */
237
225
  applyInvisibleOperator: 'auto' | null | ((parser: Parser, lhs: Expression, rhs: Expression) => Expression | null);
238
226
  /**
239
- * If true, ignore space characters.
227
+ * If true, ignore space characters in math mode.
240
228
  *
241
229
  * **Default**: `true`
242
230
  *
@@ -261,22 +249,24 @@ export type ParseLatexOptions = {
261
249
  */
262
250
  parseNumbers: boolean;
263
251
  /**
264
- * This handler is invoked when the parser encounter a set of tokens
265
- * at a position that could be a symbol or function.
252
+ * This handler is invoked when the parser encounters an identifier
253
+ * that does not have a corresponding entry in the dictionary.
266
254
  *
267
- * The `symbol` argument is one or more tokens.
255
+ * The `identifier` argument is a valid identifier
256
+ * (see https://cortexjs.io/math-json/#identifiers for the definition of a
257
+ * valid identifier).
268
258
  *
269
259
  * The handler can return:
270
260
  *
271
- * - `symbol` to indicate the string represent a constant or variable.
261
+ * - `"symbol"`: the identifier is a constant or variable name.
272
262
  *
273
- * - `function` to indicate the string is a function name. If an apply
274
- * function operator (typically, parentheses) follow, parse them as arguments
275
- * to the function.
263
+ * - `"function"`: the identifier is a function name. If an apply
264
+ * function operator (typically, parentheses) follow, they will be parsed
265
+ * as arguments to the function.
276
266
  *
277
- * - `error`, an error condition is raised.
267
+ * - `"unknown"`: the identifier is not recognized.
278
268
  */
279
- parseUnknownIdentifier: (symbol: string, parser: Parser) => 'symbol' | 'function' | 'unknown';
269
+ parseUnknownIdentifier: (identifier: string, parser: Parser) => 'symbol' | 'function' | 'unknown';
280
270
  /**
281
271
  * If true, the expression will be decorated with the LaTeX
282
272
  * fragments corresponding to each elements of the expression.
@@ -454,7 +444,7 @@ export interface Parser {
454
444
  /** Return the next token, without advancing the index */
455
445
  readonly peek: LatexToken;
456
446
  /** Return true if the terminator condition is met */
457
- atTerminator(t: Terminator): boolean;
447
+ atTerminator(t: Terminator | undefined): boolean;
458
448
  /** Return an array of string corresponding to tokens ahead.
459
449
  * The index is unchanged.
460
450
  */
@@ -502,36 +492,42 @@ export interface Parser {
502
492
  */
503
493
  matchTabular(endName: string): null | Expression[][];
504
494
  applyInvisibleOperator(terminator: Terminator, lhs: Expression | null): Expression | null;
505
- /** If the next tokens correspond to an optional LaTeX argument,
506
- * enclosed with `[` and `]` return the content of the argument
507
- * as an expression and advance the index past the closing `]`.
495
+ /** Some LaTeX commands (but not all) can accept an argument without braces,
496
+ * for example `^` , `\sqrt` or `\frac`.
508
497
  *
509
- * Otherwise, return `null`.
498
+ * The following tokens are excluded from consideration to fail early when
499
+ * encountering a likely syntax error, for example `x^(2)` (instead of
500
+ * `x^{2}`). With `(` in the list of excluded tokens, the match will fail
501
+ * and the error can be recovered.
502
+ *
503
+ * The excluded tokens are `!"#$%&(),/;:?@[]`|~", `\left` and `\bigl`
510
504
  */
511
- matchOptionalLatexArgument(): Expression | null;
505
+ matchSingleAtomArgument(): Expression | null;
512
506
  /**
513
- * Match a required LaTeX argument:
514
- * - either enclosed in `{}`
515
- * - or a single token (except if token is in `excluding`)
516
507
  *
517
- * The `excluding` option is useful to fail early when encountering a likely
518
- * syntax error, for example `x^(2)` (instead of `x^{2}`). With `(` in the list
519
- * of excluded tokens, the match will fail and the error can be recovered.
520
- *
521
- * If none is provided, `excluding` is `!"#$%&(),/;:?@[]`|~", `\left` and `\bigl`
508
+ * Match a LaTeX expression enclosed in `[]`
522
509
  *
510
+ * Otherwise, return `null`.
511
+ */
512
+ matchLatexOptionalGroup(): Expression | null;
513
+ /**
514
+ * Match a LaTeX group enclosed in `{}`
523
515
  *
524
516
  * Return null if no argument was found
525
517
  * Return `['Sequence']` if an empty argument `{}` was found
526
518
  */
527
- matchRequiredLatexArgument(excluding?: string[]): Expression | null;
519
+ matchLatexGroup(): Expression | null;
528
520
  /**
529
521
  * - 'enclosure' : will look for an argument inside an enclosure (an open/close fence)
530
522
  * - 'implicit': either an expression inside a pair of `()`, or just a primary
531
523
  * (i.e. we interpret `\cos x + 1` as `\cos(x) + 1`)
532
524
  */
533
- matchArguments(kind: '' | 'implicit' | 'enclosure'): Expression[] | null;
525
+ matchArguments(kind: '' | 'implicit' | 'enclosure', until?: Terminator): Expression[] | null;
534
526
  matchStringArgument(): string | null;
527
+ matchPrimeSuffix(): number;
528
+ /** Parse function 'id' followed optionally by some prefix (primes, etc
529
+ * and an argument list in an enclosure. */
530
+ matchFunctionSuffix(id: string): Expression;
535
531
  /** If matches the normalized open delimiter, returns the
536
532
  * expected closing delimiter.
537
533
  *
@@ -578,7 +574,7 @@ export interface Parser {
578
574
  * - a single-letter variable: `x`
579
575
  * - a single LaTeX command: `\pi`
580
576
  */
581
- matchSymbol(): Expression | null;
577
+ matchSymbol(until?: Terminator): Expression | null;
582
578
  /**
583
579
  * Parse an expression:
584
580
  *
@@ -597,6 +593,7 @@ export interface Parser {
597
593
  * `until` is `{ minPrec:0 }` by default.
598
594
  */
599
595
  matchExpression(until?: Partial<Terminator>): Expression | null;
596
+ latex(start: number, end?: number): string;
600
597
  /** Return an error expression with the specified code and arguments */
601
598
  error(code: string | [string, ...Expression[]], fromToken: number): Expression;
602
599
  }
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { NumberFormattingOptions } from './public';
3
3
  export declare function serializeNumber(expr: Expression | null, options: NumberFormattingOptions): string;
4
4
  /**
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
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,7 +1,7 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { WarningSignalHandler } from '../../common/signals';
3
- import { NumberFormattingOptions, LatexString, SerializeLatexOptions, FunctionEntry } from './public';
4
- import { IndexedLatexDictionary, SymbolEntry } from './dictionary/definitions';
3
+ import { NumberFormattingOptions, LatexString, SerializeLatexOptions } from './public';
4
+ import { IndexedLatexDictionary, IndexedIdentifierEntry, IndexedFunctionEntry } from './dictionary/definitions';
5
5
  export declare class Serializer {
6
6
  readonly onError: WarningSignalHandler;
7
7
  options: NumberFormattingOptions & SerializeLatexOptions;
@@ -15,14 +15,18 @@ export declare class Serializer {
15
15
  * @todo: don't wrap Abs, Floor, Ceil, Delimiter
16
16
  */
17
17
  wrap(expr: Expression | null, prec?: number): string;
18
- /** If this is a "short" expression (atomic), wrap it.
18
+ /**
19
+ * If this is a "short" expression, wrap it.
20
+ * Do not wrap identifiers, positive numbers or functions.
21
+ *
22
+ * This is called by the serializer for power and division (i.e. "(a+1)/b")
19
23
  *
20
24
  */
21
25
  wrapShort(expr: Expression | null): string;
22
26
  wrapString(s: string, style: 'paren' | 'leftright' | 'big' | 'none', fence?: string): string;
23
27
  wrapArguments(expr: Expression): string;
24
- serializeSymbol(expr: Expression, def?: SymbolEntry | FunctionEntry): string;
25
- serializeFunction(expr: Expression, def?: FunctionEntry | SymbolEntry): string;
28
+ serializeSymbol(expr: Expression, def?: IndexedIdentifierEntry | IndexedFunctionEntry): string;
29
+ serializeFunction(expr: Expression, def?: IndexedFunctionEntry | IndexedIdentifierEntry): string;
26
30
  serializeDictionary(dict: {
27
31
  [key: string]: Expression;
28
32
  }): string;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  * ## Reference
3
3
  * TeX source code:
4
4
  * {@link http://tug.org/texlive/devsrc/Build/source/texk/web2c/tex.web | Tex.web}
@@ -12,5 +12,6 @@ export type Token = string;
12
12
  * marker) and multiple lines.
13
13
  */
14
14
  export declare function tokenize(s: string, args: string[]): Token[];
15
+ export declare function countTokens(s: string): number;
15
16
  export declare function joinLatex(segments: Iterable<string>): string;
16
17
  export declare function tokensToString(tokens: Token | Token[] | [Token[] | Token][]): string;
@@ -1,11 +1,12 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  /** The canonical form of `Add`:
3
3
  * - removes `0`
4
4
  * - capture complex numbers (a + ib or ai +b)
5
5
  * */
6
6
  export declare function canonicalAdd(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
7
7
  export declare function domainAdd(_ce: IComputeEngine, args: BoxedDomain[]): BoxedDomain | null;
8
- export declare function simplifyAdd(ce: IComputeEngine, args: BoxedExpression[]): BoxedExpression | undefined;
8
+ export declare function simplifyAdd(ce: IComputeEngine, args: BoxedExpression[]): BoxedExpression;
9
9
  export declare function evalAdd(ce: IComputeEngine, ops: BoxedExpression[], mode?: 'N' | 'evaluate'): BoxedExpression;
10
10
  export declare function canonicalSummation(ce: IComputeEngine, body: BoxedExpression, range: BoxedExpression | undefined): BoxedExpression;
11
11
  export declare function evalSummation(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression, mode: 'simplify' | 'N' | 'evaluate'): BoxedExpression | undefined;
12
+ export declare function normalizeLimits(range: BoxedExpression): [string, number, number, boolean];
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  /**
3
3
  * Canonical form of 'Divide' (and 'Rational')
4
4
  * - remove denominator of 1
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  /** The canonical form of `Multiply`:
3
3
  * - remove `1`
4
4
  * - combine literal integers and rationals
@@ -14,5 +14,5 @@
14
14
  export declare function canonicalMultiply(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
15
15
  export declare function simplifyMultiply(ce: IComputeEngine, ops: BoxedExpression[]): BoxedExpression;
16
16
  export declare function evalMultiply(ce: IComputeEngine, ops: BoxedExpression[], mode?: 'N' | 'evaluate'): BoxedExpression | undefined;
17
- export declare function canonicalMultiplication(ce: IComputeEngine, body: BoxedExpression | undefined, range: BoxedExpression | undefined): BoxedExpression;
17
+ export declare function canonicalProduct(ce: IComputeEngine, body: BoxedExpression | undefined, range: BoxedExpression | undefined): BoxedExpression;
18
18
  export declare function evalMultiplication(ce: IComputeEngine, expr: BoxedExpression, range: BoxedExpression, mode: 'simplify' | 'evaluate' | 'N'): BoxedExpression | undefined;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  /**
3
3
  *
4
4
  */
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const ARITHMETIC_LIBRARY: IdTable[];
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const CALCULUS_LIBRARY: IdTable[];
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const COLLECTIONS_LIBRARY: IdTable;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const CORE_LIBRARY: IdTable[];
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const DOMAIN_CONSTRUCTORS: string[];
3
3
  export declare const DOMAIN_ALIAS: {
4
4
  NumericFunction: (string | string[])[];
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { IComputeEngine, IdTable } from '../public';
3
3
  export declare function getStandardLibrary(categories: LibraryCategory[] | LibraryCategory | 'all'): Readonly<IdTable>[];
4
4
  export declare const LIBRARIES: {
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const LOGIC_LIBRARY: IdTable;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const POLYNOMIALS_LIBRARY: IdTable[];
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function randomExpression(level?: number): Expression;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const RELOP_LIBRARY: IdTable;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const SETS_LIBRARY: IdTable;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare const TRIGONOMETRY_LIBRARY: IdTable[];
@@ -1,3 +1,3 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function isSymbolDefinition(def: BoxedSymbolDefinition | BoxedFunctionDefinition | SymbolDefinition | FunctionDefinition | undefined | null): def is BoxedSymbolDefinition;
3
3
  export declare function isFunctionDefinition(def: BoxedSymbolDefinition | BoxedFunctionDefinition | SymbolDefinition | FunctionDefinition | undefined | null): def is BoxedFunctionDefinition;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function bigint(a: Decimal | number | bigint | string): bigint;
3
3
  export declare function gcd(a: bigint, b: bigint): bigint;
4
4
  export declare function lcm(a: bigint, b: bigint): bigint;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import { IComputeEngine } from '../public';
3
3
  export declare function gcd(a: Decimal, b: Decimal): Decimal;
4
4
  export declare function lcm(a: Decimal, b: Decimal): Decimal;
@@ -1,3 +1,3 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function gamma(c: Complex): Complex;
3
3
  export declare function lngamma(c: Complex): Complex;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  import Decimal from 'decimal.js';
3
3
  import { BoxedExpression } from '../public';
4
4
  export declare const MACHINE_PRECISION_BITS = 53;
@@ -1,2 +1,2 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function isPrime(n: number): boolean | undefined;
@@ -1,4 +1,4 @@
1
- /* 0.12.3 */
1
+ /* 0.12.5 */
2
2
  export declare function isRational(x: any | null): x is Rational;
3
3
  export declare function isMachineRational(x: any | null): x is [number, number];
4
4
  export declare function isBigRational(x: any | null): x is [bigint, bigint];