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,379 +0,0 @@
|
|
1
|
-
# Customization
|
2
|
-
|
3
|
-
Besides parsing and evaluating expressions, the expression parser supports
|
4
|
-
a number of features to customize processing and evaluation of expressions
|
5
|
-
and outputting expressions.
|
6
|
-
|
7
|
-
On this page:
|
8
|
-
|
9
|
-
- [Function transforms](#function-transforms)
|
10
|
-
- [Custom argument parsing](#custom-argument-parsing)
|
11
|
-
- [Custom LaTeX handlers](#custom-latex-handlers)
|
12
|
-
- [Custom HTML, LaTeX and string output](#custom-html-latex-and-string-output)
|
13
|
-
- [Customize supported characters](#customize-supported-characters)
|
14
|
-
|
15
|
-
## Function transforms
|
16
|
-
|
17
|
-
It is possible to preprocess function arguments and post process a functions
|
18
|
-
return value by writing a *transform* for the function. A transform is a
|
19
|
-
function wrapping around a function to be transformed or completely replaces
|
20
|
-
a function.
|
21
|
-
|
22
|
-
For example, the functions for math.js use zero-based matrix indices (as is
|
23
|
-
common in programing languages), but the expression parser uses one-based
|
24
|
-
indices. To enable this, all functions dealing with indices have a transform,
|
25
|
-
which changes input from one-based to zero-based, and transforms output (and
|
26
|
-
error message) from zero-based to one-based.
|
27
|
-
|
28
|
-
```js
|
29
|
-
// using plain JavaScript, indices are zero-based:
|
30
|
-
const a = [[1, 2], [3, 4]] // a 2x2 matrix
|
31
|
-
math.subset(a, math.index(0, 1)) // returns 2
|
32
|
-
|
33
|
-
// using the expression parser, indices are transformed to one-based:
|
34
|
-
const a = [[1, 2], [3, 4]] // a 2x2 matrix
|
35
|
-
let scope = {
|
36
|
-
a: a
|
37
|
-
}
|
38
|
-
math.evaluate('subset(a, index(1, 2))', scope) // returns 2
|
39
|
-
```
|
40
|
-
|
41
|
-
To create a transform for a function, the transform function must be attached
|
42
|
-
to the function as property `transform`:
|
43
|
-
|
44
|
-
```js
|
45
|
-
import { create, all } from 'mathjs'
|
46
|
-
const math = create(all)
|
47
|
-
|
48
|
-
// create a function
|
49
|
-
function addIt(a, b) {
|
50
|
-
return a + b
|
51
|
-
}
|
52
|
-
|
53
|
-
// attach a transform function to the function addIt
|
54
|
-
addIt.transform = function (a, b) {
|
55
|
-
console.log('input: a=' + a + ', b=' + b)
|
56
|
-
// we can manipulate input here before executing addIt
|
57
|
-
|
58
|
-
const res = addIt(a, b)
|
59
|
-
|
60
|
-
console.log('result: ' + res)
|
61
|
-
// we can manipulate result here before returning
|
62
|
-
|
63
|
-
return res
|
64
|
-
}
|
65
|
-
|
66
|
-
// import the function into math.js
|
67
|
-
math.import({
|
68
|
-
addIt: addIt
|
69
|
-
})
|
70
|
-
|
71
|
-
// use the function via the expression parser
|
72
|
-
console.log('Using expression parser:')
|
73
|
-
console.log('2+4=' + math.evaluate('addIt(2, 4)'))
|
74
|
-
// This will output:
|
75
|
-
//
|
76
|
-
// input: a=2, b=4
|
77
|
-
// result: 6
|
78
|
-
// 2+4=6
|
79
|
-
|
80
|
-
// when used via plain JavaScript, the transform is not invoked
|
81
|
-
console.log('')
|
82
|
-
console.log('Using plain JavaScript:')
|
83
|
-
console.log('2+4=' + math.addIt(2, 4))
|
84
|
-
// This will output:
|
85
|
-
//
|
86
|
-
// 6
|
87
|
-
```
|
88
|
-
|
89
|
-
Functions with a transform must be imported in the `math` namespace, as they
|
90
|
-
need to be processed at compile time. They are not supported when passed via a
|
91
|
-
scope at evaluation time.
|
92
|
-
|
93
|
-
|
94
|
-
## Custom argument parsing
|
95
|
-
|
96
|
-
The expression parser of math.js has support for letting functions
|
97
|
-
parse and evaluate arguments themselves, instead of calling them with
|
98
|
-
evaluated arguments. This is useful for example when creating a function
|
99
|
-
like `plot(f(x), x)` or `integrate(f(x), x, start, end)`, where some of the
|
100
|
-
arguments need to be processed in a special way. In these cases, the expression
|
101
|
-
`f(x)` will be evaluated repeatedly by the function, and `x` is not evaluated
|
102
|
-
but used to specify the variable looping over the function `f(x)`.
|
103
|
-
|
104
|
-
Functions having a property `rawArgs` with value `true` are treated in a special
|
105
|
-
way by the expression parser: they will be invoked with unevaluated arguments,
|
106
|
-
allowing the function to process the arguments in a customized way. Raw
|
107
|
-
functions are called as:
|
108
|
-
|
109
|
-
```
|
110
|
-
rawFunction(args: Node[], math: Object, scope: Object)
|
111
|
-
```
|
112
|
-
|
113
|
-
Where :
|
114
|
-
|
115
|
-
- `args` is an Array with nodes of the parsed arguments.
|
116
|
-
- `math` is the math namespace against which the expression was compiled.
|
117
|
-
- `scope` is a shallow _copy_ of the `scope` object provided when evaluating
|
118
|
-
the expression, optionally extended with nested variables like a function
|
119
|
-
parameter `x` of in a custom defined function like `f(x) = x^2`.
|
120
|
-
|
121
|
-
Raw functions must be imported in the `math` namespace, as they need to be
|
122
|
-
processed at compile time. They are not supported when passed via a scope
|
123
|
-
at evaluation time.
|
124
|
-
|
125
|
-
A simple example:
|
126
|
-
|
127
|
-
```js
|
128
|
-
function myFunction(args, math, scope) {
|
129
|
-
// get string representation of the arguments
|
130
|
-
const str = args.map(function (arg) {
|
131
|
-
return arg.toString()
|
132
|
-
})
|
133
|
-
|
134
|
-
// evaluate the arguments
|
135
|
-
const res = args.map(function (arg) {
|
136
|
-
return arg.compile().evaluate(scope)
|
137
|
-
})
|
138
|
-
|
139
|
-
return 'arguments: ' + str.join(',') + ', evaluated: ' + res.join(',')
|
140
|
-
}
|
141
|
-
|
142
|
-
// mark the function as "rawArgs", so it will be called with unevaluated arguments
|
143
|
-
myFunction.rawArgs = true
|
144
|
-
|
145
|
-
// import the new function in the math namespace
|
146
|
-
math.import({
|
147
|
-
myFunction: myFunction
|
148
|
-
})
|
149
|
-
|
150
|
-
// use the function
|
151
|
-
math.evaluate('myFunction(2 + 3, sqrt(4))')
|
152
|
-
// returns 'arguments: 2 + 3, sqrt(4), evaluated: 5, 2'
|
153
|
-
```
|
154
|
-
|
155
|
-
## Custom LaTeX handlers
|
156
|
-
|
157
|
-
You can attach a `toTex` property to your custom functions before importing them to define their LaTeX output. This
|
158
|
-
`toTex` property can be a handler in the format described in the next section 'Custom LaTeX and String conversion'
|
159
|
-
or a template string similar to ES6 templates.
|
160
|
-
|
161
|
-
### Template syntax
|
162
|
-
|
163
|
-
- `${name}`: Gets replaced by the name of the function
|
164
|
-
- `${args}`: Gets replaced by a comma separated list of the arguments of the function.
|
165
|
-
- `${args[0]}`: Gets replaced by the first argument of a function
|
166
|
-
- `$$`: Gets replaced by `$`
|
167
|
-
|
168
|
-
#### Example
|
169
|
-
|
170
|
-
```js
|
171
|
-
const customFunctions = {
|
172
|
-
plus: function (a, b) {
|
173
|
-
return a + b
|
174
|
-
},
|
175
|
-
minus: function (a, b) {
|
176
|
-
return a - b
|
177
|
-
},
|
178
|
-
binom: function (n, k) {
|
179
|
-
return 1
|
180
|
-
}
|
181
|
-
}
|
182
|
-
|
183
|
-
customFunctions.plus.toTex = '${args[0]}+${args[1]}' //template string
|
184
|
-
customFunctions.binom.toTex = '\\mathrm{${name}}\\left(${args}\\right)' //template string
|
185
|
-
customFunctions.minus.toTex = function (node, options) { //handler function
|
186
|
-
return node.args[0].toTex(options) + node.name + node.args[1].toTex(options)
|
187
|
-
}
|
188
|
-
|
189
|
-
math.import(customFunctions)
|
190
|
-
|
191
|
-
math.parse('plus(1,2)').toTex() // '1+2'
|
192
|
-
math.parse('binom(1,2)').toTex() // '\\mathrm{binom}\\left(1,2\\right)'
|
193
|
-
math.parse('minus(1,2)').toTex() // '1minus2'
|
194
|
-
```
|
195
|
-
|
196
|
-
## Custom HTML, LaTeX and string output
|
197
|
-
|
198
|
-
All expression nodes have a method `toTex` and `toString` to output an expression respectively in HTML or LaTex format or as regular text .
|
199
|
-
The functions `toHTML`, `toTex` and `toString` accept an `options` argument to customise output. This object is of the following form:
|
200
|
-
|
201
|
-
```js
|
202
|
-
{
|
203
|
-
parenthesis: 'keep', // parenthesis option
|
204
|
-
handler: someHandler, // handler to change the output
|
205
|
-
implicit: 'hide' // how to treat implicit multiplication
|
206
|
-
}
|
207
|
-
```
|
208
|
-
|
209
|
-
### Parenthesis
|
210
|
-
|
211
|
-
The `parenthesis` option changes the way parentheses are used in the output. There are three options available:
|
212
|
-
|
213
|
-
- `keep` Keep the parentheses from the input and display them as is. This is the default.
|
214
|
-
- `auto` Only display parentheses that are necessary. Mathjs tries to get rid of as much parentheses as possible.
|
215
|
-
- `all` Display all parentheses that are given by the structure of the node tree. This makes the output precedence unambiguous.
|
216
|
-
|
217
|
-
There's two ways of passing callbacks:
|
218
|
-
|
219
|
-
1. Pass an object that maps function names to callbacks. Those callbacks will be used for FunctionNodes with
|
220
|
-
functions of that name.
|
221
|
-
2. Pass a function to `toTex`. This function will then be used for every node.
|
222
|
-
|
223
|
-
```js
|
224
|
-
const expression = math.parse('(1+1+1)')
|
225
|
-
|
226
|
-
expression.toString() // (1 + 1 + 1)
|
227
|
-
expression.toString({parenthesis: 'keep'}) // (1 + 1 + 1)
|
228
|
-
expression.toString({parenthesis: 'auto'}) // 1 + 1 + 1
|
229
|
-
expression.toString({parenthesis: 'all'}) // (1 + 1) + 1
|
230
|
-
```
|
231
|
-
|
232
|
-
### Handler
|
233
|
-
|
234
|
-
You can provide the `toTex` and `toString` functions of an expression with your own custom handlers that override the internal behaviour. This is especially useful to provide LaTeX/string output for your own custom functions. This can be done in two ways:
|
235
|
-
|
236
|
-
1. Pass an object that maps function names to callbacks. Those callbacks will be used for FunctionNodes that contain functions with that name.
|
237
|
-
2. Pass a callback directly. This callback will run for every node, so you can replace the output of anything you like.
|
238
|
-
|
239
|
-
A callback function has the following form:
|
240
|
-
|
241
|
-
```js
|
242
|
-
function callback (node, options) {
|
243
|
-
...
|
244
|
-
}
|
245
|
-
```
|
246
|
-
Where `options` is the object passed to `toHTML`/`toTex`/`toString`. Don't forget to pass this on to the child nodes, and `node` is a reference to the current node.
|
247
|
-
|
248
|
-
If a callback returns nothing, the standard output will be used. If your callback returns a string, this string will be used.
|
249
|
-
|
250
|
-
**Although the following examples use `toTex`, it works for `toString` and `toHTML` in the same way**
|
251
|
-
|
252
|
-
#### Examples for option 1
|
253
|
-
|
254
|
-
```js
|
255
|
-
const customFunctions = {
|
256
|
-
binomial: function (n, k) {
|
257
|
-
//calculate n choose k
|
258
|
-
// (do some stuff)
|
259
|
-
return result
|
260
|
-
}
|
261
|
-
}
|
262
|
-
|
263
|
-
const customLaTeX = {
|
264
|
-
'binomial': function (node, options) { //provide toTex for your own custom function
|
265
|
-
return '\\binom{' + node.args[0].toTex(options) + '}{' + node.args[1].toTex(options) + '}'
|
266
|
-
},
|
267
|
-
'factorial': function (node, options) { //override toTex for builtin functions
|
268
|
-
return 'factorial\\left(' + node.args[0] + '\\right)'
|
269
|
-
}
|
270
|
-
}
|
271
|
-
```
|
272
|
-
|
273
|
-
You can simply use your custom toTex functions by passing them to `toTex`:
|
274
|
-
|
275
|
-
```js
|
276
|
-
math.import(customFunctions)
|
277
|
-
const expression = math.parse('binomial(factorial(2),1)')
|
278
|
-
const latex = expression.toTex({handler: customLaTeX})
|
279
|
-
// latex now contains "\binom{factorial\\left(2\\right)}{1}"
|
280
|
-
```
|
281
|
-
|
282
|
-
#### Examples for option 2:
|
283
|
-
|
284
|
-
```js
|
285
|
-
function customLaTeX(node, options) {
|
286
|
-
if ((node.type === 'OperatorNode') && (node.fn === 'add')) {
|
287
|
-
//don't forget to pass the options to the toTex functions
|
288
|
-
return node.args[0].toTex(options) + ' plus ' + node.args[1].toTex(options)
|
289
|
-
}
|
290
|
-
else if (node.type === 'ConstantNode') {
|
291
|
-
if (node.value === 0) {
|
292
|
-
return '\\mbox{zero}'
|
293
|
-
}
|
294
|
-
else if (node.value === 1) {
|
295
|
-
return '\\mbox{one}'
|
296
|
-
}
|
297
|
-
else if (node.value === 2) {
|
298
|
-
return '\\mbox{two}'
|
299
|
-
}
|
300
|
-
else {
|
301
|
-
return node.value
|
302
|
-
}
|
303
|
-
}
|
304
|
-
}
|
305
|
-
|
306
|
-
const expression = math.parse('1+2')
|
307
|
-
const latex = expression.toTex({handler: customLaTeX})
|
308
|
-
// latex now contains '\mbox{one} plus \mbox{two}'
|
309
|
-
```
|
310
|
-
|
311
|
-
Another example in conjunction with custom functions:
|
312
|
-
|
313
|
-
```js
|
314
|
-
const customFunctions = {
|
315
|
-
binomial: function (n, k) {
|
316
|
-
//calculate n choose k
|
317
|
-
// (do some stuff)
|
318
|
-
return result
|
319
|
-
}
|
320
|
-
}
|
321
|
-
|
322
|
-
function customLaTeX(node, options) {
|
323
|
-
if ((node.type === 'FunctionNode') && (node.name === 'binomial')) {
|
324
|
-
return '\\binom{' + node.args[0].toTex(options) + '}{' + node.args[1].toTex(options) + '}'
|
325
|
-
}
|
326
|
-
}
|
327
|
-
|
328
|
-
math.import(customFunctions)
|
329
|
-
const expression = math.parse('binomial(2,1)')
|
330
|
-
const latex = expression.toTex({handler: customLaTeX})
|
331
|
-
// latex now contains "\binom{2}{1}"
|
332
|
-
```
|
333
|
-
|
334
|
-
### Implicit multiplication
|
335
|
-
|
336
|
-
You can change the way that implicit multiplication is converted to a string or LaTeX. The two options are `hide`, to not show a multiplication operator for implicit multiplication and `show` to show it.
|
337
|
-
|
338
|
-
Example:
|
339
|
-
|
340
|
-
```js
|
341
|
-
const node = math.parse('2a')
|
342
|
-
|
343
|
-
node.toString() // '2 a'
|
344
|
-
node.toString({implicit: 'hide'}) // '2 a'
|
345
|
-
node.toString({implicit: 'show'}) // '2 * a'
|
346
|
-
|
347
|
-
node.toTex() // '2~ a'
|
348
|
-
node.toTex({implicit: 'hide'}) // '2~ a'
|
349
|
-
node.toTex({implicit: 'show'}) // '2\\cdot a'
|
350
|
-
```
|
351
|
-
|
352
|
-
|
353
|
-
## Customize supported characters
|
354
|
-
|
355
|
-
It is possible to customize the characters allowed in symbols and digits.
|
356
|
-
The `parse` function exposes the following test functions:
|
357
|
-
|
358
|
-
- `math.parse.isAlpha(c, cPrev, cNext)`
|
359
|
-
- `math.parse.isWhitespace(c, nestingLevel)`
|
360
|
-
- `math.parse.isDecimalMark(c, cNext)`
|
361
|
-
- `math.parse.isDigitDot(c)`
|
362
|
-
- `math.parse.isDigit(c)`
|
363
|
-
|
364
|
-
The exact signature and implementation of these functions can be looked up in
|
365
|
-
the [source code of the parser](https://github.com/josdejong/mathjs/blob/master/lib/expression/parse.js). The allowed alpha characters are described here: [Constants and variables](syntax.md#constants-and-variables).
|
366
|
-
|
367
|
-
For example, the phone character <code>☎</code> is not supported by default. It can be enabled
|
368
|
-
by replacing the `isAlpha` function:
|
369
|
-
|
370
|
-
```js
|
371
|
-
const isAlphaOriginal = math.parse.isAlpha
|
372
|
-
math.parse.isAlpha = function (c, cPrev, cNext) {
|
373
|
-
return isAlphaOriginal(c, cPrev, cNext) || (c === '\u260E')
|
374
|
-
}
|
375
|
-
|
376
|
-
// now we can use the \u260E (phone) character in expressions
|
377
|
-
const result = math.evaluate('\u260Efoo', {'\u260Efoo': 42}) // returns 42
|
378
|
-
console.log(result)
|
379
|
-
```
|