UNPKG

240 kBTypeScriptView Raw
1import { Decimal } from 'decimal.js'
2
3declare const math: math.MathJsStatic
4export as namespace math
5export = math
6
7type NoLiteralType<T> = T extends number
8 ? number
9 : T extends string
10 ? string
11 : T extends boolean
12 ? boolean
13 : T
14
15declare namespace math {
16 // TODO: introduce generics for MathCollection, MathMatrix, and MathArray
17 type MathNumericType = number | BigNumber | Fraction | Complex
18 type MathScalarType = MathNumericType | Unit
19 type MathArray = MathNumericType[] | MathNumericType[][] // TODO: MathArray can also contain Unit
20 type MathCollection = MathArray | Matrix
21 type MathType = MathScalarType | MathCollection
22 type MathExpression = string | string[] | MathCollection
23
24 // eslint-disable-next-line @typescript-eslint/no-explicit-any
25 type FactoryFunction<T> = (scope: any) => T
26
27 // FactoryFunctionMap can be nested; all nested objects will be flattened
28 interface FactoryFunctionMap {
29 // eslint-disable-next-line @typescript-eslint/no-explicit-any
30 [key: string]: FactoryFunction<any> | FactoryFunctionMap
31 }
32
33 /** Available options for parse */
34 interface ParseOptions {
35 /** a set of custom nodes */
36 nodes?: Record<string, MathNode>
37 }
38 /**
39 * Parse an expression. Returns a node tree, which can be evaluated by
40 * invoking node.evaluate().
41 *
42 * Note the evaluating arbitrary expressions may involve security risks,
43 * see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
44 *
45 * Syntax:
46 *
47 * math.parse(expr)
48 * math.parse(expr, options)
49 * math.parse([expr1, expr2, expr3, ...])
50 * math.parse([expr1, expr2, expr3, ...], options)
51 *
52 * Example:
53 *
54 * const node1 = math.parse('sqrt(3^2 + 4^2)')
55 * node1.compile().evaluate() // 5
56 *
57 * let scope = {a:3, b:4}
58 * const node2 = math.parse('a * b') // 12
59 * const code2 = node2.compile()
60 * code2.evaluate(scope) // 12
61 * scope.a = 5
62 * code2.evaluate(scope) // 20
63 *
64 * const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
65 * nodes[2].compile().evaluate() // 12
66 *
67 * See also:
68 *
69 * evaluate, compile
70 */
71 interface ParseFunction {
72 /**
73 * Parse an expression. Returns a node tree, which can be evaluated by
74 * invoking node.evaluate();
75 *
76 * @param expr Expression to be parsed
77 * @param options Available options
78 * @returns A node
79 */
80 (expr: MathExpression, options?: ParseOptions): MathNode
81
82 /**
83 * Parse an expression. Returns a node tree, which can be evaluated by
84 * invoking node.evaluate();
85 *
86 * @param exprs Expressions to be parsed
87 * @param options Available options
88 * @returns An array of nodes
89 */
90 (exprs: MathExpression[], options?: ParseOptions): MathNode[]
91
92 /**
93 * Checks whether the current character `c` is a valid alpha character:
94 *
95 * - A latin letter (upper or lower case) Ascii: a-z, A-Z
96 * - An underscore Ascii: _
97 * - A dollar sign Ascii: $
98 * - A latin letter with accents Unicode: \u00C0 - \u02AF
99 * - A greek letter Unicode: \u0370 - \u03FF
100 * - A mathematical alphanumeric symbol Unicode: \u{1D400} - \u{1D7FF} excluding invalid code points
101 *
102 * The previous and next characters are needed to determine whether
103 * this character is part of a unicode surrogate pair.
104 *
105 * @param c Current character in the expression
106 * @param cPrev Previous character
107 * @param cNext Next character
108 */
109 isAlpha(c: string, cPrev: string, cNext: string): boolean
110 /**
111 * Test whether a character is a valid latin, greek, or letter-like character
112 *
113 * @param c
114 */
115 isValidLatinOrGreek(c: string): boolean
116 /**
117 * Test whether two given 16 bit characters form a surrogate pair of a
118 * unicode math symbol.
119 *
120 * https://unicode-table.com/en/
121 * https://www.wikiwand.com/en/Mathematical_operators_and_symbols_in_Unicode
122 *
123 * Note: In ES6 will be unicode aware:
124 * https://stackoverflow.com/questions/280712/javascript-unicode-regexes
125 * https://mathiasbynens.be/notes/es6-unicode-regex
126 *
127 * @param high
128 * @param low
129 */
130 isValidMathSymbol(high: string, low: string): boolean
131 /**
132 * Check whether given character c is a white space character: space, tab, or enter
133 *
134 * @param c
135 * @param nestingLevel
136 */
137 isWhitespace(c: string, nestingLevel: number): boolean
138 /**
139 * Test whether the character c is a decimal mark (dot).
140 * This is the case when it's not the start of a delimiter '.*', './', or '.^'
141 *
142 * @param c
143 * @param cNext
144 */
145 isDecimalMark(c: string, cNext: string): boolean
146 /**
147 * checks if the given char c is a digit or dot
148 *
149 * @param c a string with one character
150 */
151 isDigitDot(c: string): boolean
152 /**
153 * checks if the given char c is a digit
154 *
155 * @param c a string with one character
156 */
157 isDigit(c: string): boolean
158 /**
159 * checks if the given char c is a hex digit
160 *
161 * @param c a string with one character
162 */
163 isHexDigit(c: string): boolean
164 }
165
166 interface NodeCtor {
167 new (): MathNode
168 }
169
170 interface AccessorNode<TObject extends MathNode = MathNode> extends MathNode {
171 type: 'AccessorNode'
172 isAccessorNode: true
173 object: TObject
174 index: IndexNode
175 name: string
176 }
177 interface AccessorNodeCtor {
178 new <TObject extends MathNode = MathNode>(
179 object: TObject,
180 index: IndexNode
181 ): AccessorNode<TObject>
182 }
183
184 interface ArrayNode<TItems extends MathNode[] = MathNode[]> extends MathNode {
185 type: 'ArrayNode'
186 isArrayNode: true
187 items: TItems
188 }
189 interface ArrayNodeCtor {
190 new <TItems extends MathNode[] = MathNode[]>(
191 items: MathNode[]
192 ): ArrayNode<TItems>
193 }
194
195 interface AssignmentNode<TValue extends MathNode = MathNode>
196 extends MathNode {
197 type: 'AssignmentNode'
198 isAssignmentNode: true
199 object: SymbolNode | AccessorNode
200 index: IndexNode | null
201 value: TValue
202 name: string
203 }
204 interface AssignmentNodeCtor {
205 new <TValue extends MathNode = MathNode>(
206 object: SymbolNode,
207 value: TValue
208 ): AssignmentNode<TValue>
209 new <TValue extends MathNode = MathNode>(
210 object: SymbolNode | AccessorNode,
211 index: IndexNode,
212 value: TValue
213 ): AssignmentNode<TValue>
214 }
215
216 interface BlockNode<TNode extends MathNode = MathNode> extends MathNode {
217 type: 'BlockNode'
218 isBlockNode: true
219 blocks: Array<{ node: TNode; visible: boolean }>
220 }
221 interface BlockNodeCtor {
222 new <TNode extends MathNode = MathNode>(
223 arr: Array<{ node: TNode } | { node: TNode; visible: boolean }>
224 ): BlockNode
225 }
226
227 interface ConditionalNode<
228 TCond extends MathNode = MathNode,
229 TTrueNode extends MathNode = MathNode,
230 TFalseNode extends MathNode = MathNode
231 > extends MathNode {
232 type: 'ConditionalNode'
233 isConditionalNode: boolean
234 condition: TCond
235 trueExpr: TTrueNode
236 falseExpr: TFalseNode
237 }
238 interface ConditionalNodeCtor {
239 new <
240 TCond extends MathNode = MathNode,
241 TTrueNode extends MathNode = MathNode,
242 TFalseNode extends MathNode = MathNode
243 >(
244 condition: TCond,
245 trueExpr: TTrueNode,
246 falseExpr: TFalseNode
247 ): ConditionalNode
248 }
249
250 interface ConstantNode<TValue extends string | number = number>
251 extends MathNode {
252 type: 'ConstantNode'
253 isConstantNode: true
254 // eslint-disable-next-line @typescript-eslint/no-explicit-any
255 value: TValue
256 }
257
258 interface ConstantNodeCtor {
259 new <TValue extends string | number = string>(
260 value: TValue
261 ): ConstantNode<TValue>
262 }
263
264 interface FunctionAssignmentNode<TExpr extends MathNode = MathNode>
265 extends MathNode {
266 type: 'FunctionAssignmentNode'
267 isFunctionAssignmentNode: true
268 name: string
269 params: string[]
270 expr: TExpr
271 }
272 interface FunctionAssignmentNodeCtor {
273 new <TExpr extends MathNode = MathNode>(
274 name: string,
275 params: string[],
276 expr: TExpr
277 ): FunctionAssignmentNode<TExpr>
278 }
279
280 interface FunctionNode<
281 TFn = SymbolNode,
282 TArgs extends MathNode[] = MathNode[]
283 > extends MathNode {
284 type: 'FunctionNode'
285 isFunctionNode: true
286 fn: TFn
287 args: TArgs
288 }
289 interface FunctionNodeCtor {
290 new <TFn = SymbolNode, TArgs extends MathNode[] = MathNode[]>(
291 fn: TFn,
292 args: TArgs
293 ): FunctionNode<TFn, TArgs>
294 // eslint-disable-next-line @typescript-eslint/no-explicit-any
295 onUndefinedFunction: (name: string) => any
296 }
297
298 interface IndexNode<TDims extends MathNode[] = MathNode[]> extends MathNode {
299 type: 'IndexNode'
300 isIndexNode: true
301 dimensions: TDims
302 dotNotation: boolean
303 }
304 interface IndexNodeCtor {
305 new <TDims extends MathNode[] = MathNode[]>(dimensions: TDims): IndexNode
306 new <TDims extends MathNode[] = MathNode[]>(
307 dimensions: TDims,
308 dotNotation: boolean
309 ): IndexNode<TDims>
310 }
311
312 interface ObjectNode<
313 TProps extends Record<string, MathNode> = Record<string, MathNode>
314 > extends MathNode {
315 type: 'ObjectNode'
316 isObjectNode: true
317 properties: TProps
318 }
319 interface ObjectNodeCtor {
320 new <TProps extends Record<string, MathNode> = Record<string, MathNode>>(
321 properties: TProps
322 ): ObjectNode<TProps>
323 }
324
325 type OperatorNodeMap = {
326 xor: 'xor'
327 and: 'and'
328 or: 'or'
329 bitOr: '|'
330 bitXor: '^|'
331 bitAnd: '&'
332 equal: '=='
333 unequal: '!='
334 smaller: '<'
335 larger: '>'
336 smallerEq: '<='
337 largerEq: '>='
338 leftShift: '<<'
339 rightArithShift: '>>'
340 rightLogShift: '>>>'
341 to: 'to'
342 add: '+'
343 subtract: '-'
344 multiply: '*'
345 divide: '/'
346 dotMultiply: '.*'
347 dotDivide: './'
348 mod: 'mod'
349 unaryPlus: '+'
350 unaryMinus: '-'
351 bitNot: '~'
352 not: 'not'
353 pow: '^'
354 dotPow: '.^'
355 factorial: '!'
356 }
357
358 type OperatorNodeOp = OperatorNodeMap[keyof OperatorNodeMap]
359 type OperatorNodeFn = keyof OperatorNodeMap
360
361 interface OperatorNode<
362 TOp extends OperatorNodeMap[TFn] = never,
363 TFn extends OperatorNodeFn = never,
364 TArgs extends MathNode[] = MathNode[]
365 > extends MathNode {
366 type: 'OperatorNode'
367 isOperatorNode: true
368 op: TOp
369 fn: TFn
370 args: TArgs
371 implicit: boolean
372 isUnary(): boolean
373 isBinary(): boolean
374 }
375
376 interface OperatorNodeCtor extends MathNode {
377 new <
378 TOp extends OperatorNodeMap[TFn],
379 TFn extends OperatorNodeFn,
380 TArgs extends MathNode[]
381 >(
382 op: TOp,
383 fn: TFn,
384 args: TArgs,
385 implicit?: boolean
386 ): OperatorNode<TOp, TFn, TArgs>
387 }
388 interface ParenthesisNode<TContent extends MathNode = MathNode>
389 extends MathNode {
390 type: 'ParenthesisNode'
391 isParenthesisNode: true
392 content: TContent
393 }
394 interface ParenthesisNodeCtor {
395 new <TContent extends MathNode>(
396 content: TContent
397 ): ParenthesisNode<TContent>
398 }
399
400 interface RangeNode<
401 TStart extends MathNode = MathNode,
402 TEnd extends MathNode = MathNode,
403 TStep extends MathNode = MathNode
404 > extends MathNode {
405 type: 'RangeNode'
406 isRangeNode: true
407 start: TStart
408 end: TEnd
409 step: TStep | null
410 }
411 interface RangeNodeCtor {
412 new <
413 TStart extends MathNode = MathNode,
414 TEnd extends MathNode = MathNode,
415 TStep extends MathNode = MathNode
416 >(
417 start: TStart,
418 end: TEnd,
419 step?: TStep
420 ): RangeNode<TStart, TEnd, TStep>
421 }
422
423 interface RelationalNode<TParams extends MathNode[] = MathNode[]>
424 extends MathNode {
425 type: 'RelationalNode'
426 isRelationalNode: true
427 conditionals: string[]
428 params: TParams
429 }
430 interface RelationalNodeCtor {
431 new <TParams extends MathNode[] = MathNode[]>(
432 conditionals: string[],
433 params: TParams
434 ): RelationalNode<TParams>
435 }
436
437 interface SymbolNode extends MathNode {
438 type: 'SymbolNode'
439 isSymbolNode: true
440 name: string
441 }
442 interface SymbolNodeCtor {
443 new (name: string): SymbolNode
444 // eslint-disable-next-line @typescript-eslint/no-explicit-any
445 onUndefinedSymbol: (name: string) => any
446 }
447
448 /**
449 * @deprecated since version 11.3. Prefer `MathNode` instead
450 */
451 type MathNodeCommon = MathNode
452
453 type MathJsFunctionName = keyof MathJsStatic
454
455 interface LUDecomposition {
456 L: MathCollection
457 U: MathCollection
458 p: number[]
459 }
460
461 interface SLUDecomposition extends LUDecomposition {
462 q: number[]
463 }
464
465 interface QRDecomposition {
466 Q: MathCollection
467 R: MathCollection
468 }
469
470 interface SchurDecomposition {
471 U: MathCollection
472 T: MathCollection
473 }
474
475 interface FractionDefinition {
476 a: number
477 b: number
478 }
479
480 interface MathJsStatic extends FactoryDependencies {
481 e: number
482 pi: number
483 i: number
484 Infinity: number
485 LN2: number
486 LN10: number
487 LOG2E: number
488 LOG10E: number
489 NaN: number
490 phi: number
491 SQRT1_2: number
492 SQRT2: number
493 tau: number
494
495 // Class-like constructors
496 Node: NodeCtor
497 AccessorNode: AccessorNodeCtor
498 ArrayNode: ArrayNodeCtor
499 AssignmentNode: AssignmentNodeCtor
500 BlockNode: BlockNodeCtor
501 ConditionalNode: ConditionalNodeCtor
502 ConstantNode: ConstantNodeCtor
503 FunctionAssignmentNode: FunctionAssignmentNodeCtor
504 FunctionNode: FunctionNodeCtor
505 IndexNode: IndexNodeCtor
506 ObjectNode: ObjectNodeCtor
507 OperatorNode: OperatorNodeCtor
508 ParenthesisNode: ParenthesisNodeCtor
509 RangeNode: RangeNodeCtor
510 RelationalNode: RelationalNodeCtor
511 SymbolNode: SymbolNodeCtor
512
513 Matrix: MatrixCtor
514
515 /**
516 * If null were to be included in this interface, it would be
517 * auto-suggested as an import in VSCode. This causes issues because
518 * `null` is not a valid label.
519 *
520 * @see https://github.com/josdejong/mathjs/issues/2019
521 */
522 // null: number;
523
524 // eslint-disable-next-line @typescript-eslint/no-explicit-any
525 uninitialized: any
526 version: string
527
528 expression: MathNode
529
530 /**
531 * Returns reviver function that can be used as reviver in JSON.parse function.
532 */
533 // eslint-disable-next-line @typescript-eslint/no-explicit-any
534 reviver(): (key: any, value: any) => any
535
536 /**
537 * Returns replacer function that can be used as replacer in JSON.stringify function.
538 */
539 // eslint-disable-next-line @typescript-eslint/no-explicit-any
540 replacer(): (key: any, value: any) => any
541
542 /*************************************************************************
543 * Core functions
544 ************************************************************************/
545
546 /**
547 * Set configuration options for math.js, and get current options. Will
548 * emit a ‘config’ event, with arguments (curr, prev, changes).
549 * @param options Available options: {number} epsilon Minimum relative
550 * difference between two compared values, used by all comparison
551 * functions. {string} matrix A string ‘Matrix’ (default) or ‘Array’.
55