mathjs 10.5.2 → 10.6.1
Sign up to get free protection for your applications and to get access to all the features.
- package/HISTORY.md +28 -0
- package/lib/browser/math.js +6 -5
- package/lib/browser/math.js.map +1 -1
- package/lib/cjs/entry/dependenciesAny/dependenciesFft.generated.js +41 -0
- package/lib/cjs/entry/dependenciesAny/dependenciesIfft.generated.js +29 -0
- package/lib/cjs/entry/dependenciesAny.generated.js +16 -0
- package/lib/cjs/entry/impureFunctionsAny.generated.js +66 -64
- package/lib/cjs/entry/pureFunctionsAny.generated.js +189 -171
- package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +6 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/fft.js +15 -0
- package/lib/cjs/expression/embeddedDocs/function/matrix/ifft.js +15 -0
- package/lib/cjs/factoriesAny.js +16 -0
- package/lib/cjs/function/algebra/derivative.js +1 -1
- package/lib/cjs/function/matrix/fft.js +128 -0
- package/lib/cjs/function/matrix/ifft.js +49 -0
- package/lib/cjs/header.js +2 -2
- package/lib/cjs/version.js +1 -1
- package/lib/esm/entry/dependenciesAny/dependenciesFft.generated.js +24 -0
- package/lib/esm/entry/dependenciesAny/dependenciesIfft.generated.js +16 -0
- package/lib/esm/entry/dependenciesAny.generated.js +2 -0
- package/lib/esm/entry/impureFunctionsAny.generated.js +63 -61
- package/lib/esm/entry/pureFunctionsAny.generated.js +161 -145
- package/lib/esm/expression/embeddedDocs/embeddedDocs.js +4 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/fft.js +8 -0
- package/lib/esm/expression/embeddedDocs/function/matrix/ifft.js +8 -0
- package/lib/esm/factoriesAny.js +2 -0
- package/lib/esm/function/algebra/derivative.js +1 -1
- package/lib/esm/function/matrix/fft.js +104 -0
- package/lib/esm/function/matrix/ifft.js +38 -0
- package/lib/esm/version.js +1 -1
- package/package.json +22 -24
- package/types/index.d.ts +1052 -283
- package/types/index.ts +711 -7
- package/docs/command_line_interface.md +0 -87
- package/docs/core/chaining.md +0 -41
- package/docs/core/configuration.md +0 -144
- package/docs/core/extension.md +0 -263
- package/docs/core/index.md +0 -21
- package/docs/core/serialization.md +0 -50
- package/docs/custom_bundling.md +0 -116
- package/docs/datatypes/bignumbers.md +0 -102
- package/docs/datatypes/complex_numbers.md +0 -168
- package/docs/datatypes/fractions.md +0 -75
- package/docs/datatypes/index.md +0 -67
- package/docs/datatypes/matrices.md +0 -375
- package/docs/datatypes/numbers.md +0 -106
- package/docs/datatypes/units.md +0 -444
- package/docs/expressions/algebra.md +0 -110
- package/docs/expressions/customization.md +0 -379
- package/docs/expressions/expression_trees.md +0 -710
- package/docs/expressions/html_classes.md +0 -38
- package/docs/expressions/index.md +0 -21
- package/docs/expressions/parsing.md +0 -224
- package/docs/expressions/security.md +0 -89
- package/docs/expressions/syntax.md +0 -706
- package/docs/getting_started.md +0 -124
- package/docs/index.md +0 -39
- package/docs/reference/classes/densematrix.md +0 -247
- package/docs/reference/classes/fibonacciheap.md +0 -70
- package/docs/reference/classes/matrixindex.md +0 -133
- package/docs/reference/classes/matrixrange.md +0 -158
- package/docs/reference/classes/resultset.md +0 -47
- package/docs/reference/classes/sparsematrix.md +0 -245
- package/docs/reference/classes/unit.md +0 -242
- package/docs/reference/classes.md +0 -86
- package/docs/reference/constants.md +0 -29
- package/docs/reference/functions/abs.md +0 -46
- package/docs/reference/functions/acos.md +0 -49
- package/docs/reference/functions/acosh.md +0 -47
- package/docs/reference/functions/acot.md +0 -48
- package/docs/reference/functions/acoth.md +0 -46
- package/docs/reference/functions/acsc.md +0 -49
- package/docs/reference/functions/acsch.md +0 -46
- package/docs/reference/functions/add.md +0 -59
- package/docs/reference/functions/and.md +0 -53
- package/docs/reference/functions/apply.md +0 -56
- package/docs/reference/functions/arg.md +0 -53
- package/docs/reference/functions/asec.md +0 -49
- package/docs/reference/functions/asech.md +0 -46
- package/docs/reference/functions/asin.md +0 -49
- package/docs/reference/functions/asinh.md +0 -46
- package/docs/reference/functions/atan.md +0 -49
- package/docs/reference/functions/atan2.md +0 -56
- package/docs/reference/functions/atanh.md +0 -46
- package/docs/reference/functions/bellNumbers.md +0 -45
- package/docs/reference/functions/bignumber.md +0 -47
- package/docs/reference/functions/bin.md +0 -45
- package/docs/reference/functions/bitAnd.md +0 -51
- package/docs/reference/functions/bitNot.md +0 -51
- package/docs/reference/functions/bitOr.md +0 -52
- package/docs/reference/functions/bitXor.md +0 -51
- package/docs/reference/functions/boolean.md +0 -50
- package/docs/reference/functions/catalan.md +0 -45
- package/docs/reference/functions/cbrt.md +0 -60
- package/docs/reference/functions/ceil.md +0 -63
- package/docs/reference/functions/chain.md +0 -54
- package/docs/reference/functions/clone.md +0 -43
- package/docs/reference/functions/column.md +0 -45
- package/docs/reference/functions/combinations.md +0 -49
- package/docs/reference/functions/combinationsWithRep.md +0 -49
- package/docs/reference/functions/compare.md +0 -67
- package/docs/reference/functions/compareNatural.md +0 -92
- package/docs/reference/functions/compareText.md +0 -54
- package/docs/reference/functions/compile.md +0 -56
- package/docs/reference/functions/complex.md +0 -17
- package/docs/reference/functions/composition.md +0 -46
- package/docs/reference/functions/concat.md +0 -56
- package/docs/reference/functions/config.md +0 -44
- package/docs/reference/functions/conj.md +0 -50
- package/docs/reference/functions/cos.md +0 -51
- package/docs/reference/functions/cosh.md +0 -46
- package/docs/reference/functions/cot.md +0 -47
- package/docs/reference/functions/coth.md +0 -49
- package/docs/reference/functions/count.md +0 -45
- package/docs/reference/functions/createUnit.md +0 -52
- package/docs/reference/functions/cross.md +0 -58
- package/docs/reference/functions/csc.md +0 -47
- package/docs/reference/functions/csch.md +0 -49
- package/docs/reference/functions/ctranspose.md +0 -50
- package/docs/reference/functions/cube.md +0 -51
- package/docs/reference/functions/cumsum.md +0 -57
- package/docs/reference/functions/deepEqual.md +0 -53
- package/docs/reference/functions/derivative.md +0 -61
- package/docs/reference/functions/det.md +0 -49
- package/docs/reference/functions/diag.md +0 -61
- package/docs/reference/functions/diff.md +0 -70
- package/docs/reference/functions/distance.md +0 -80
- package/docs/reference/functions/divide.md +0 -55
- package/docs/reference/functions/dot.md +0 -48
- package/docs/reference/functions/dotDivide.md +0 -52
- package/docs/reference/functions/dotMultiply.md +0 -52
- package/docs/reference/functions/dotPow.md +0 -49
- package/docs/reference/functions/eigs.md +0 -54
- package/docs/reference/functions/equal.md +0 -75
- package/docs/reference/functions/equalText.md +0 -53
- package/docs/reference/functions/erf.md +0 -49
- package/docs/reference/functions/evaluate.md +0 -56
- package/docs/reference/functions/exp.md +0 -54
- package/docs/reference/functions/expm.md +0 -49
- package/docs/reference/functions/expm1.md +0 -54
- package/docs/reference/functions/factorial.md +0 -49
- package/docs/reference/functions/fft.md +0 -35
- package/docs/reference/functions/filter.md +0 -50
- package/docs/reference/functions/fix.md +0 -62
- package/docs/reference/functions/flatten.md +0 -46
- package/docs/reference/functions/floor.md +0 -66
- package/docs/reference/functions/forEach.md +0 -41
- package/docs/reference/functions/format.md +0 -127
- package/docs/reference/functions/fraction.md +0 -45
- package/docs/reference/functions/gamma.md +0 -49
- package/docs/reference/functions/gcd.md +0 -50
- package/docs/reference/functions/getMatrixDataType.md +0 -59
- package/docs/reference/functions/hasNumericValue.md +0 -55
- package/docs/reference/functions/help.md +0 -42
- package/docs/reference/functions/hex.md +0 -45
- package/docs/reference/functions/hypot.md +0 -51
- package/docs/reference/functions/identity.md +0 -57
- package/docs/reference/functions/ifft.md +0 -35
- package/docs/reference/functions/im.md +0 -53
- package/docs/reference/functions/import.md +0 -68
- package/docs/reference/functions/index.md +0 -55
- package/docs/reference/functions/intersect.md +0 -50
- package/docs/reference/functions/inv.md +0 -45
- package/docs/reference/functions/invmod.md +0 -47
- package/docs/reference/functions/isInteger.md +0 -55
- package/docs/reference/functions/isNaN.md +0 -56
- package/docs/reference/functions/isNegative.md +0 -55
- package/docs/reference/functions/isNumeric.md +0 -55
- package/docs/reference/functions/isPositive.md +0 -57
- package/docs/reference/functions/isPrime.md +0 -54
- package/docs/reference/functions/isZero.md +0 -59
- package/docs/reference/functions/kldivergence.md +0 -41
- package/docs/reference/functions/kron.md +0 -53
- package/docs/reference/functions/larger.md +0 -60
- package/docs/reference/functions/largerEq.md +0 -56
- package/docs/reference/functions/lcm.md +0 -54
- package/docs/reference/functions/leafCount.md +0 -52
- package/docs/reference/functions/leftShift.md +0 -52
- package/docs/reference/functions/lgamma.md +0 -46
- package/docs/reference/functions/log.md +0 -57
- package/docs/reference/functions/log10.md +0 -50
- package/docs/reference/functions/log1p.md +0 -54
- package/docs/reference/functions/log2.md +0 -50
- package/docs/reference/functions/lsolve.md +0 -51
- package/docs/reference/functions/lsolveAll.md +0 -51
- package/docs/reference/functions/lup.md +0 -52
- package/docs/reference/functions/lusolve.md +0 -59
- package/docs/reference/functions/mad.md +0 -50
- package/docs/reference/functions/map.md +0 -65
- package/docs/reference/functions/matrix.md +0 -56
- package/docs/reference/functions/matrixFromColumns.md +0 -49
- package/docs/reference/functions/matrixFromFunction.md +0 -54
- package/docs/reference/functions/matrixFromRows.md +0 -49
- package/docs/reference/functions/max.md +0 -61
- package/docs/reference/functions/mean.md +0 -57
- package/docs/reference/functions/median.md +0 -57
- package/docs/reference/functions/min.md +0 -61
- package/docs/reference/functions/mod.md +0 -59
- package/docs/reference/functions/mode.md +0 -50
- package/docs/reference/functions/multinomial.md +0 -46
- package/docs/reference/functions/multiply.md +0 -60
- package/docs/reference/functions/norm.md +0 -59
- package/docs/reference/functions/not.md +0 -50
- package/docs/reference/functions/nthRoot.md +0 -53
- package/docs/reference/functions/nthRoots.md +0 -58
- package/docs/reference/functions/number.md +0 -49
- package/docs/reference/functions/numeric.md +0 -52
- package/docs/reference/functions/oct.md +0 -45
- package/docs/reference/functions/ones.md +0 -59
- package/docs/reference/functions/or.md +0 -53
- package/docs/reference/functions/parse.md +0 -56
- package/docs/reference/functions/parser.md +0 -70
- package/docs/reference/functions/partitionSelect.md +0 -53
- package/docs/reference/functions/permutations.md +0 -51
- package/docs/reference/functions/pickRandom.md +0 -59
- package/docs/reference/functions/pinv.md +0 -44
- package/docs/reference/functions/pow.md +0 -65
- package/docs/reference/functions/print.md +0 -64
- package/docs/reference/functions/prod.md +0 -55
- package/docs/reference/functions/qr.md +0 -65
- package/docs/reference/functions/quantileSeq.md +0 -62
- package/docs/reference/functions/random.md +0 -54
- package/docs/reference/functions/randomInt.md +0 -53
- package/docs/reference/functions/range.md +0 -70
- package/docs/reference/functions/rationalize.md +0 -68
- package/docs/reference/functions/re.md +0 -53
- package/docs/reference/functions/reshape.md +0 -60
- package/docs/reference/functions/resize.md +0 -51
- package/docs/reference/functions/resolve.md +0 -46
- package/docs/reference/functions/rightArithShift.md +0 -52
- package/docs/reference/functions/rightLogShift.md +0 -52
- package/docs/reference/functions/rotate.md +0 -53
- package/docs/reference/functions/rotationMatrix.md +0 -57
- package/docs/reference/functions/round.md +0 -61
- package/docs/reference/functions/row.md +0 -45
- package/docs/reference/functions/sec.md +0 -47
- package/docs/reference/functions/sech.md +0 -49
- package/docs/reference/functions/setCartesian.md +0 -49
- package/docs/reference/functions/setDifference.md +0 -47
- package/docs/reference/functions/setDistinct.md +0 -43
- package/docs/reference/functions/setIntersect.md +0 -46
- package/docs/reference/functions/setIsSubset.md +0 -47
- package/docs/reference/functions/setMultiplicity.md +0 -46
- package/docs/reference/functions/setPowerset.md +0 -43
- package/docs/reference/functions/setSize.md +0 -47
- package/docs/reference/functions/setSymDifference.md +0 -47
- package/docs/reference/functions/setUnion.md +0 -46
- package/docs/reference/functions/sign.md +0 -52
- package/docs/reference/functions/simplify.md +0 -130
- package/docs/reference/functions/simplifyConstant.md +0 -52
- package/docs/reference/functions/simplifyCore.md +0 -50
- package/docs/reference/functions/sin.md +0 -51
- package/docs/reference/functions/sinh.md +0 -46
- package/docs/reference/functions/size.md +0 -50
- package/docs/reference/functions/slu.md +0 -57
- package/docs/reference/functions/smaller.md +0 -60
- package/docs/reference/functions/smallerEq.md +0 -56
- package/docs/reference/functions/sort.md +0 -56
- package/docs/reference/functions/sparse.md +0 -51
- package/docs/reference/functions/splitUnit.md +0 -37
- package/docs/reference/functions/sqrt.md +0 -50
- package/docs/reference/functions/sqrtm.md +0 -46
- package/docs/reference/functions/square.md +0 -51
- package/docs/reference/functions/squeeze.md +0 -53
- package/docs/reference/functions/std.md +0 -76
- package/docs/reference/functions/stirlingS2.md +0 -52
- package/docs/reference/functions/string.md +0 -49
- package/docs/reference/functions/subset.md +0 -65
- package/docs/reference/functions/subtract.md +0 -54
- package/docs/reference/functions/sum.md +0 -54
- package/docs/reference/functions/symbolicEqual.md +0 -62
- package/docs/reference/functions/tan.md +0 -49
- package/docs/reference/functions/tanh.md +0 -50
- package/docs/reference/functions/to.md +0 -47
- package/docs/reference/functions/trace.md +0 -50
- package/docs/reference/functions/transpose.md +0 -49
- package/docs/reference/functions/typeOf.md +0 -81
- package/docs/reference/functions/typed.md +0 -56
- package/docs/reference/functions/unaryMinus.md +0 -49
- package/docs/reference/functions/unaryPlus.md +0 -48
- package/docs/reference/functions/unequal.md +0 -74
- package/docs/reference/functions/unit.md +0 -48
- package/docs/reference/functions/usolve.md +0 -51
- package/docs/reference/functions/usolveAll.md +0 -51
- package/docs/reference/functions/variance.md +0 -78
- package/docs/reference/functions/xgcd.md +0 -47
- package/docs/reference/functions/xor.md +0 -53
- package/docs/reference/functions/zeros.md +0 -57
- package/docs/reference/functions.md +0 -297
- package/docs/reference/index.md +0 -5
- package/examples/advanced/convert_fraction_to_bignumber.js +0 -59
- package/examples/advanced/custom_argument_parsing.js +0 -98
- package/examples/advanced/custom_datatype.js +0 -56
- package/examples/advanced/custom_evaluate_using_factories.js +0 -19
- package/examples/advanced/custom_evaluate_using_import.js +0 -18
- package/examples/advanced/custom_loading.js +0 -33
- package/examples/advanced/custom_relational_functions.js +0 -62
- package/examples/advanced/custom_scope_objects.js +0 -115
- package/examples/advanced/expression_trees.js +0 -55
- package/examples/advanced/function_transform.js +0 -50
- package/examples/advanced/more_secure_eval.js +0 -36
- package/examples/advanced/use_bigint.js +0 -43
- package/examples/advanced/web_server/math_worker.js +0 -24
- package/examples/advanced/web_server/server.js +0 -80
- package/examples/algebra.js +0 -34
- package/examples/basic_usage.js +0 -49
- package/examples/bignumbers.js +0 -52
- package/examples/browser/angle_configuration.html +0 -134
- package/examples/browser/basic_usage.html +0 -39
- package/examples/browser/currency_conversion.html +0 -125
- package/examples/browser/custom_separators.html +0 -81
- package/examples/browser/plot.html +0 -78
- package/examples/browser/pretty_printing_with_mathjax.html +0 -122
- package/examples/browser/printing_html.html +0 -170
- package/examples/browser/requirejs_loading.html +0 -20
- package/examples/browser/rocket_trajectory_optimization.html +0 -301
- package/examples/browser/webworkers/webworkers.html +0 -80
- package/examples/browser/webworkers/worker.js +0 -28
- package/examples/chaining.js +0 -56
- package/examples/complex_numbers.js +0 -67
- package/examples/expressions.js +0 -188
- package/examples/fractions.js +0 -74
- package/examples/import.js +0 -95
- package/examples/matrices.js +0 -101
- package/examples/objects.js +0 -35
- package/examples/package.json +0 -3
- package/examples/serialization.js +0 -16
- package/examples/sparse_matrices.js +0 -19
- package/examples/units.js +0 -108
@@ -1,38 +0,0 @@
|
|
1
|
-
# HTML output
|
2
|
-
|
3
|
-
The expression parser can output a HTML string, where every `Node` is
|
4
|
-
transformed into a `<span>` element with semantic class names. Each class
|
5
|
-
name begins with the `math-` prefix. These class names can be used in CSS to
|
6
|
-
highlight the syntax or change the default layout (e.g. spaces around operators).
|
7
|
-
|
8
|
-
## Available class names
|
9
|
-
|
10
|
-
- `math-number`
|
11
|
-
- `math-string`
|
12
|
-
- `math-boolean` (`true` and `false`)
|
13
|
-
- `math-undefined`
|
14
|
-
- `math-function` (function names)
|
15
|
-
- `math-parameter` (function parameters)
|
16
|
-
- `math-property` (object members)
|
17
|
-
- `math-symbol` (variables, units and built-in constants)
|
18
|
-
- `math-null-symbol` (`null`)
|
19
|
-
- `math-nan-symbol` (`NaN`)
|
20
|
-
- `math-infinity-symbol` (`Infinity`)
|
21
|
-
- `math-imaginary-symbol` (`i`)
|
22
|
-
- `math-operator`
|
23
|
-
- `math-unary-operator`
|
24
|
-
- `math-lefthand-unary-operator`
|
25
|
-
- `math-righthand-unary-operator`
|
26
|
-
- `math-binary-operator`
|
27
|
-
- `math-explicit-binary-operator`
|
28
|
-
- `math-implicit-binary-operator` (empty element)
|
29
|
-
- `math-assignment-operator`
|
30
|
-
- `math-variable-assignment-operator` (`=`)
|
31
|
-
- `math-property-assignment-operator` (`:` in objects)
|
32
|
-
- `math-accessor-operator` (`.` in objects)
|
33
|
-
- `math-range-operator` (`:` in ranges)
|
34
|
-
- `math-parenthesis`
|
35
|
-
-`math-round-parenthesis` (`(` and `)`)
|
36
|
-
-`math-square-parenthesis` (`[` and `]`)
|
37
|
-
-`math-curly-parenthesis` (`{` and `}`)
|
38
|
-
- `math-separator` (�,`, `;` and <code><br /></code>)
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# Expressions
|
2
|
-
|
3
|
-
Math.js contains a flexible and easy to use expression parser.
|
4
|
-
The parser supports all data types, functions and constants available in math.js.
|
5
|
-
|
6
|
-
Whilst the math.js library is aimed at JavaScript developers, the expression
|
7
|
-
parser is aimed at end users: mathematicians, engineers, students, pupils.
|
8
|
-
The syntax of the expression parser differs from JavaScript and the low-level
|
9
|
-
math.js library.
|
10
|
-
|
11
|
-
This section is divided in the following pages:
|
12
|
-
|
13
|
-
- [Parsing and evaluation](parsing.md) describes how to parse and
|
14
|
-
evaluate expressions with math.js.
|
15
|
-
- [Syntax](syntax.md) describes how to write expressions.
|
16
|
-
- [Expression trees](expression_trees.md) explains how to parse an expression into an
|
17
|
-
expression tree, and use this to analyse and manipulate the expression.
|
18
|
-
- [Algebra](algebra.md) describing symbolic computation in math.js.
|
19
|
-
- [Customization](customization.md) describes how to customize processing and
|
20
|
-
evaluation of expressions.
|
21
|
-
- [Security](security.md) about security risks of executing arbitrary expressions.
|
@@ -1,224 +0,0 @@
|
|
1
|
-
# Expression parsing and evaluation
|
2
|
-
|
3
|
-
Expressions can be parsed and evaluated in various ways:
|
4
|
-
|
5
|
-
- Using the function [`math.evaluate(expr [,scope])`](#evaluate).
|
6
|
-
- Using the function [`math.compile(expr)`](#compile).
|
7
|
-
- Using the function [`math.parse(expr)`](#parse).
|
8
|
-
- By creating a [parser](#parser), `math.parser()`, which contains a method
|
9
|
-
`evaluate` and keeps a scope with assigned variables in memory.
|
10
|
-
|
11
|
-
|
12
|
-
## Evaluate
|
13
|
-
|
14
|
-
Math.js comes with a function `math.evaluate` to evaluate expressions. Syntax:
|
15
|
-
|
16
|
-
```js
|
17
|
-
math.evaluate(expr)
|
18
|
-
math.evaluate(expr, scope)
|
19
|
-
math.evaluate([expr1, expr2, expr3, ...])
|
20
|
-
math.evaluate([expr1, expr2, expr3, ...], scope)
|
21
|
-
```
|
22
|
-
|
23
|
-
Function `evaluate` accepts a single expression or an array with
|
24
|
-
expressions as the first argument and has an optional second argument
|
25
|
-
containing a scope with variables and functions. The scope can be a regular
|
26
|
-
JavaScript Object, or Map. The scope will be used to resolve symbols, and to write
|
27
|
-
assigned variables or function.
|
28
|
-
|
29
|
-
The following code demonstrates how to evaluate expressions.
|
30
|
-
|
31
|
-
```js
|
32
|
-
// evaluate expressions
|
33
|
-
math.evaluate('sqrt(3^2 + 4^2)') // 5
|
34
|
-
math.evaluate('sqrt(-4)') // 2i
|
35
|
-
math.evaluate('2 inch to cm') // 5.08 cm
|
36
|
-
math.evaluate('cos(45 deg)') // 0.7071067811865476
|
37
|
-
|
38
|
-
// provide a scope
|
39
|
-
let scope = {
|
40
|
-
a: 3,
|
41
|
-
b: 4
|
42
|
-
}
|
43
|
-
math.evaluate('a * b', scope) // 12
|
44
|
-
math.evaluate('c = 2.3 + 4.5', scope) // 6.8
|
45
|
-
scope.c // 6.8
|
46
|
-
```
|
47
|
-
|
48
|
-
|
49
|
-
## Compile
|
50
|
-
|
51
|
-
Math.js contains a function `math.compile` which compiles expressions
|
52
|
-
into JavaScript code. This is a shortcut for first [parsing](#parse) and then
|
53
|
-
compiling an expression. The syntax is:
|
54
|
-
|
55
|
-
```js
|
56
|
-
math.compile(expr)
|
57
|
-
math.compile([expr1, expr2, expr3, ...])
|
58
|
-
```
|
59
|
-
|
60
|
-
Function `compile` accepts a single expression or an array with
|
61
|
-
expressions as the argument. Function `compile` returns an object with a function
|
62
|
-
`evaluate([scope])`, which can be executed to evaluate the expression against an
|
63
|
-
(optional) scope:
|
64
|
-
|
65
|
-
```js
|
66
|
-
const code = math.compile(expr) // compile an expression
|
67
|
-
const result = code.evaluate([scope]) // evaluate the code with an optional scope
|
68
|
-
```
|
69
|
-
|
70
|
-
An expression needs to be compiled only once, after which the
|
71
|
-
expression can be evaluated repeatedly and against different scopes.
|
72
|
-
The optional scope is used to resolve symbols and to write assigned
|
73
|
-
variables or functions. Parameter [`scope`](#scope) can be a regular Object, or Map.
|
74
|
-
|
75
|
-
Example usage:
|
76
|
-
|
77
|
-
```js
|
78
|
-
// parse an expression into a node, and evaluate the node
|
79
|
-
const code1 = math.compile('sqrt(3^2 + 4^2)')
|
80
|
-
code1.evaluate() // 5
|
81
|
-
```
|
82
|
-
|
83
|
-
|
84
|
-
## Parse
|
85
|
-
|
86
|
-
Math.js contains a function `math.parse` to parse expressions into an
|
87
|
-
[expression tree](expression_trees.md). The syntax is:
|
88
|
-
|
89
|
-
```js
|
90
|
-
math.parse(expr)
|
91
|
-
math.parse([expr1, expr2, expr3, ...])
|
92
|
-
```
|
93
|
-
|
94
|
-
Function `parse` accepts a single expression or an array with
|
95
|
-
expressions as the argument. Function `parse` returns a the root node of the tree,
|
96
|
-
which can be successively compiled and evaluated:
|
97
|
-
|
98
|
-
```js
|
99
|
-
const node = math.parse(expr) // parse expression into a node tree
|
100
|
-
const code = node.compile() // compile the node tree
|
101
|
-
const result = code.evaluate([scope]) // evaluate the code with an optional scope
|
102
|
-
```
|
103
|
-
|
104
|
-
The API of nodes is described in detail on the page
|
105
|
-
[Expression trees](expression_trees.md).
|
106
|
-
|
107
|
-
An expression needs to be parsed and compiled only once, after which the
|
108
|
-
expression can be evaluated repeatedly. On evaluation, an optional scope
|
109
|
-
can be provided, which is used to resolve symbols and to write assigned
|
110
|
-
variables or functions. Parameter [`scope`](#scope) is a regular Object or Map.
|
111
|
-
|
112
|
-
Example usage:
|
113
|
-
|
114
|
-
```js
|
115
|
-
// parse an expression into a node, and evaluate the node
|
116
|
-
const node1 = math.parse('sqrt(3^2 + 4^2)')
|
117
|
-
const code1 = node1.compile()
|
118
|
-
code1.evaluate() // 5
|
119
|
-
|
120
|
-
// provide a scope
|
121
|
-
const node2 = math.parse('x^a')
|
122
|
-
const code2 = node2.compile()
|
123
|
-
let scope = {
|
124
|
-
x: 3,
|
125
|
-
a: 2
|
126
|
-
}
|
127
|
-
code2.evaluate(scope) // 9
|
128
|
-
|
129
|
-
// change a value in the scope and re-evaluate the node
|
130
|
-
scope.a = 3
|
131
|
-
code2.evaluate(scope) // 27
|
132
|
-
```
|
133
|
-
|
134
|
-
Parsed expressions can be exported to text using `node.toString()`, and can
|
135
|
-
be exported to LaTeX using `node.toTex()`. The LaTeX export can be used to
|
136
|
-
pretty print an expression in the browser with a library like
|
137
|
-
[MathJax](https://www.mathjax.org/). Example usage:
|
138
|
-
|
139
|
-
```js
|
140
|
-
// parse an expression
|
141
|
-
const node = math.parse('sqrt(x/x+1)')
|
142
|
-
node.toString() // returns 'sqrt((x / x) + 1)'
|
143
|
-
node.toTex() // returns '\sqrt{ {\frac{x}{x} }+{1} }'
|
144
|
-
```
|
145
|
-
|
146
|
-
|
147
|
-
## Parser
|
148
|
-
|
149
|
-
In addition to the static functions [`math.evaluate`](#evaluate) and
|
150
|
-
[`math.parse`](#parse), math.js contains a parser with functions `evaluate` and
|
151
|
-
`parse`, which automatically keeps a scope with assigned variables in memory.
|
152
|
-
The parser also contains some convenience functions to get, set, and remove
|
153
|
-
variables from memory.
|
154
|
-
|
155
|
-
A parser can be created by:
|
156
|
-
|
157
|
-
```js
|
158
|
-
const parser = math.parser()
|
159
|
-
```
|
160
|
-
|
161
|
-
The parser contains the following functions:
|
162
|
-
|
163
|
-
- `clear()`
|
164
|
-
Completely clear the parser's scope.
|
165
|
-
- `evaluate(expr)`
|
166
|
-
Evaluate an expression. Returns the result of the expression.
|
167
|
-
- `get(name)`
|
168
|
-
Retrieve a variable or function from the parser's scope.
|
169
|
-
- `getAll()`
|
170
|
-
Retrieve a map with all defined a variables from the parser's scope.
|
171
|
-
- `remove(name)`
|
172
|
-
Remove a variable or function from the parser's scope.
|
173
|
-
- `set(name, value)`
|
174
|
-
Set a variable or function in the parser's scope.
|
175
|
-
|
176
|
-
The following code shows how to create and use a parser.
|
177
|
-
|
178
|
-
```js
|
179
|
-
// create a parser
|
180
|
-
const parser = math.parser()
|
181
|
-
|
182
|
-
// evaluate expressions
|
183
|
-
parser.evaluate('sqrt(3^2 + 4^2)') // 5
|
184
|
-
parser.evaluate('sqrt(-4)') // 2i
|
185
|
-
parser.evaluate('2 inch to cm') // 5.08 cm
|
186
|
-
parser.evaluate('cos(45 deg)') // 0.7071067811865476
|
187
|
-
|
188
|
-
// define variables and functions
|
189
|
-
parser.evaluate('x = 7 / 2') // 3.5
|
190
|
-
parser.evaluate('x + 3') // 6.5
|
191
|
-
parser.evaluate('f(x, y) = x^y') // f(x, y)
|
192
|
-
parser.evaluate('f(2, 3)') // 8
|
193
|
-
|
194
|
-
// get and set variables and functions
|
195
|
-
const x = parser.get('x') // x = 7
|
196
|
-
const f = parser.get('f') // function
|
197
|
-
const g = f(3, 3) // g = 27
|
198
|
-
parser.set('h', 500)
|
199
|
-
parser.evaluate('h / 2') // 250
|
200
|
-
parser.set('hello', function (name) {
|
201
|
-
return 'hello, ' + name + '!'
|
202
|
-
})
|
203
|
-
parser.evaluate('hello("user")') // "hello, user!"
|
204
|
-
|
205
|
-
// clear defined functions and variables
|
206
|
-
parser.clear()
|
207
|
-
```
|
208
|
-
|
209
|
-
## Scope
|
210
|
-
|
211
|
-
The scope is a data-structure used to store and lookup variables and functions defined and used by expressions.
|
212
|
-
|
213
|
-
It is passed to mathjs via calls to [`math.evaluate`](#evaluate) or `simplify`.
|
214
|
-
|
215
|
-
For ease of use, it can be a Plain Javascript Object; for safety it can be a plain `Map` and for flexibility, any object that has
|
216
|
-
the methods `get`/`set`/`has`/`keys`, seen on `Map`.
|
217
|
-
|
218
|
-
Some care is taken to mutate the same object that is passed into mathjs, so they can collect the definitions from mathjs scripts and expressions.
|
219
|
-
|
220
|
-
`evaluate` will fail if the expression uses a blacklisted symbol, preventing mathjs expressions to escape into Javascript. This is enforced by access to the scope.
|
221
|
-
|
222
|
-
For less reliance on this blacklist, scope can also be a `Map`, which allows mathjs expressions to define variables and functions of any name.
|
223
|
-
|
224
|
-
For more, see [examples of custom scopes](../../examples/advanced/custom_scope_objects.js).
|
@@ -1,89 +0,0 @@
|
|
1
|
-
# Security
|
2
|
-
|
3
|
-
Executing arbitrary expressions like enabled by the expression parser of
|
4
|
-
mathjs involves a risk in general. When you're using mathjs to let users
|
5
|
-
execute arbitrary expressions, it's good to take a moment to think about
|
6
|
-
possible security and stability implications, especially when running
|
7
|
-
the code server side.
|
8
|
-
|
9
|
-
## Security risks
|
10
|
-
|
11
|
-
A user could try to inject malicious JavaScript code via the expression
|
12
|
-
parser. The expression parser of mathjs offers a sandboxed environment
|
13
|
-
to execute expressions which should make this impossible. It's possible
|
14
|
-
though that there are unknown security vulnerabilities, so it's important
|
15
|
-
to be careful, especially when allowing server side execution of
|
16
|
-
arbitrary expressions.
|
17
|
-
|
18
|
-
The expression parser of mathjs parses the input in a controlled
|
19
|
-
way into an expression tree or abstract syntax tree (AST).
|
20
|
-
In a "compile" step, it does as much as possible preprocessing on the
|
21
|
-
static parts of the expression, and creates a fast performing function
|
22
|
-
which can be used to evaluate the expression repeatedly using a
|
23
|
-
dynamically passed scope.
|
24
|
-
|
25
|
-
The parser actively prevents access to JavaScripts internal `eval` and
|
26
|
-
`new Function` which are the main cause of security attacks. Mathjs
|
27
|
-
versions 4 and newer does not use JavaScript's `eval` under the hood.
|
28
|
-
Version 3 and older did use `eval` for the compile step. This is not
|
29
|
-
directly a security issue but results in a larger possible attack surface.
|
30
|
-
|
31
|
-
When running a node.js server, it's good to be aware of the different
|
32
|
-
types of security risks. The risk whe running inside a browser may be
|
33
|
-
limited though it's good to be aware of [Cross side scripting (XSS)](https://www.wikiwand.com/en/Cross-site_scripting) vulnerabilities. A nice overview of
|
34
|
-
security risks of a node.js servers is listed in an article [Node.js security checklist](https://blog.risingstack.com/node-js-security-checklist/) by Gergely Nemeth.
|
35
|
-
|
36
|
-
### Less vulnerable expression parser
|
37
|
-
|
38
|
-
There is a small number of functions which yield the biggest security
|
39
|
-
risk in the expression parser:
|
40
|
-
|
41
|
-
- `import` and `createUnit` which alter the built-in functionality and
|
42
|
-
allow overriding existing functions and units.
|
43
|
-
- `evaluate`, `parse`, `simplify`, and `derivative` which parse arbitrary
|
44
|
-
input into a manipulable expression tree.
|
45
|
-
|
46
|
-
To make the expression parser less vulnerable whilst still supporting
|
47
|
-
most functionality, these functions can be disabled:
|
48
|
-
|
49
|
-
```js
|
50
|
-
import { create, all } from 'mathjs'
|
51
|
-
|
52
|
-
const math = create(all)
|
53
|
-
const limitedEvaluate = math.evaluate
|
54
|
-
|
55
|
-
math.import({
|
56
|
-
'import': function () { throw new Error('Function import is disabled') },
|
57
|
-
'createUnit': function () { throw new Error('Function createUnit is disabled') },
|
58
|
-
'evaluate': function () { throw new Error('Function evaluate is disabled') },
|
59
|
-
'parse': function () { throw new Error('Function parse is disabled') },
|
60
|
-
'simplify': function () { throw new Error('Function simplify is disabled') },
|
61
|
-
'derivative': function () { throw new Error('Function derivative is disabled') }
|
62
|
-
}, { override: true })
|
63
|
-
|
64
|
-
console.log(limitedEvaluate('sqrt(16)')) // Ok, 4
|
65
|
-
console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled
|
66
|
-
```
|
67
|
-
|
68
|
-
|
69
|
-
### Found a security vulnerability? Please report in private!
|
70
|
-
|
71
|
-
You found a security vulnerability? Awesome! We hope you don't have bad
|
72
|
-
intentions and want to help fix the issue. Please report the
|
73
|
-
vulnerability in a private way by contacting one of the maintainers
|
74
|
-
via mail or an other private channel. That way we can work together
|
75
|
-
on a fix before sharing the issue with everybody including the bad guys.
|
76
|
-
|
77
|
-
## Stability risks
|
78
|
-
|
79
|
-
A user could accidentally or on purpose execute a
|
80
|
-
heavy expression like creating a huge matrix. That can let the
|
81
|
-
JavaScript engine run out of memory or freeze it when the CPU goes
|
82
|
-
to 100% for a long time.
|
83
|
-
|
84
|
-
To protect against this sort of issue, one can run the expression parser
|
85
|
-
in a separate Web Worker or child_process, so it can't affect the
|
86
|
-
main process. The workers can be killed when it runs for too
|
87
|
-
long or consumes too much memory. A useful library in this regard
|
88
|
-
is [workerpool](https://github.com/josdejong/workerpool), which makes
|
89
|
-
it easy to manage a pool of workers in both browser and node.js.
|