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,62 +0,0 @@
|
|
1
|
-
const { create, all, factory } = require('../..')
|
2
|
-
|
3
|
-
// First let's see what the default behavior is:
|
4
|
-
// strings are compared by their numerical value
|
5
|
-
console.log('default (compare string by their numerical value)')
|
6
|
-
const { evaluate } = create(all)
|
7
|
-
evaluateAndLog(evaluate, '2 < 10') // true
|
8
|
-
evaluateAndLog(evaluate, '"2" < "10"') // true
|
9
|
-
evaluateAndLog(evaluate, '"a" == "b"') // Error: Cannot convert "a" to a number
|
10
|
-
evaluateAndLog(evaluate, '"a" == "a"') // Error: Cannot convert "a" to a number
|
11
|
-
console.log('')
|
12
|
-
|
13
|
-
// Suppose we want different behavior for string comparisons. To achieve
|
14
|
-
// this we can replace the factory functions for all relational functions
|
15
|
-
// with our own. In this simple example we use the JavaScript implementation.
|
16
|
-
console.log('custom (compare strings lexically)')
|
17
|
-
|
18
|
-
const allWithCustomFunctions = {
|
19
|
-
...all,
|
20
|
-
|
21
|
-
createEqual: factory('equal', [], () => function equal (a, b) {
|
22
|
-
return a === b
|
23
|
-
}),
|
24
|
-
|
25
|
-
createUnequal: factory('unequal', [], () => function unequal (a, b) {
|
26
|
-
return a !== b
|
27
|
-
}),
|
28
|
-
|
29
|
-
createSmaller: factory('smaller', [], () => function smaller (a, b) {
|
30
|
-
return a < b
|
31
|
-
}),
|
32
|
-
|
33
|
-
createSmallerEq: factory('smallerEq', [], () => function smallerEq (a, b) {
|
34
|
-
return a <= b
|
35
|
-
}),
|
36
|
-
|
37
|
-
createLarger: factory('larger', [], () => function larger (a, b) {
|
38
|
-
return a > b
|
39
|
-
}),
|
40
|
-
|
41
|
-
createLargerEq: factory('largerEq', [], () => function largerEq (a, b) {
|
42
|
-
return a >= b
|
43
|
-
}),
|
44
|
-
|
45
|
-
createCompare: factory('compare', [], () => function compare (a, b) {
|
46
|
-
return a > b ? 1 : a < b ? -1 : 0
|
47
|
-
})
|
48
|
-
}
|
49
|
-
const evaluateCustom = create(allWithCustomFunctions).evaluate
|
50
|
-
evaluateAndLog(evaluateCustom, '2 < 10') // true
|
51
|
-
evaluateAndLog(evaluateCustom, '"2" < "10"') // false
|
52
|
-
evaluateAndLog(evaluateCustom, '"a" == "b"') // false
|
53
|
-
evaluateAndLog(evaluateCustom, '"a" == "a"') // true
|
54
|
-
|
55
|
-
// helper function to evaluate an expression and print the results
|
56
|
-
function evaluateAndLog (evaluate, expression) {
|
57
|
-
try {
|
58
|
-
console.log(expression, evaluate(expression))
|
59
|
-
} catch (err) {
|
60
|
-
console.error(expression, err.toString())
|
61
|
-
}
|
62
|
-
}
|
@@ -1,115 +0,0 @@
|
|
1
|
-
const { create, all } = require('../..')
|
2
|
-
|
3
|
-
const math = create(all)
|
4
|
-
|
5
|
-
// The expression evaluator accepts an optional scope object.
|
6
|
-
// This is the symbol table for variable defintions and function declations.
|
7
|
-
|
8
|
-
// Scope can be a bare object.
|
9
|
-
function withObjectScope () {
|
10
|
-
const scope = { x: 3 }
|
11
|
-
|
12
|
-
math.evaluate('x', scope) // 1
|
13
|
-
math.evaluate('y = 2 x', scope)
|
14
|
-
math.evaluate('scalar = 1', scope)
|
15
|
-
math.evaluate('area(length, width) = length * width * scalar', scope)
|
16
|
-
math.evaluate('A = area(x, y)', scope)
|
17
|
-
|
18
|
-
console.log('Object scope:', scope)
|
19
|
-
}
|
20
|
-
|
21
|
-
// Where flexibility is important, scope can duck type appear to be a Map.
|
22
|
-
function withMapScope (scope, name) {
|
23
|
-
scope.set('x', 3)
|
24
|
-
|
25
|
-
math.evaluate('x', scope) // 1
|
26
|
-
math.evaluate('y = 2 x', scope)
|
27
|
-
math.evaluate('scalar = 1', scope)
|
28
|
-
math.evaluate('area(length, width) = length * width * scalar', scope)
|
29
|
-
math.evaluate('A = area(x, y)', scope)
|
30
|
-
|
31
|
-
console.log(`Map-like scope (${name}):`, scope.localScope)
|
32
|
-
}
|
33
|
-
|
34
|
-
// This is a minimal set of functions to look like a Map.
|
35
|
-
class MapScope {
|
36
|
-
constructor () {
|
37
|
-
this.localScope = new Map()
|
38
|
-
}
|
39
|
-
|
40
|
-
get (key) {
|
41
|
-
// Remember to sanitize your inputs, or use
|
42
|
-
// a datastructure that isn't a footgun.
|
43
|
-
return this.localScope.get(key)
|
44
|
-
}
|
45
|
-
|
46
|
-
set (key, value) {
|
47
|
-
return this.localScope.set(key, value)
|
48
|
-
}
|
49
|
-
|
50
|
-
has (key) {
|
51
|
-
return this.localScope.has(key)
|
52
|
-
}
|
53
|
-
|
54
|
-
keys () {
|
55
|
-
return this.localScope.keys()
|
56
|
-
}
|
57
|
-
}
|
58
|
-
|
59
|
-
/*
|
60
|
-
* This is a more fully featured example, with all methods
|
61
|
-
* used in mathjs.
|
62
|
-
*
|
63
|
-
*/
|
64
|
-
class AdvancedMapScope extends MapScope {
|
65
|
-
constructor (parent) {
|
66
|
-
super()
|
67
|
-
this.parentScope = parent
|
68
|
-
}
|
69
|
-
|
70
|
-
get (key) {
|
71
|
-
return this.localScope.get(key) ?? this.parentScope?.get(key)
|
72
|
-
}
|
73
|
-
|
74
|
-
has (key) {
|
75
|
-
return this.localScope.has(key) ?? this.parentScope?.get(key)
|
76
|
-
}
|
77
|
-
|
78
|
-
keys () {
|
79
|
-
if (this.parentScope) {
|
80
|
-
return new Set([...this.localScope.keys(), ...this.parentScope.keys()])
|
81
|
-
} else {
|
82
|
-
return this.localScope.keys()
|
83
|
-
}
|
84
|
-
}
|
85
|
-
|
86
|
-
delete () {
|
87
|
-
return this.localScope.delete()
|
88
|
-
}
|
89
|
-
|
90
|
-
clear () {
|
91
|
-
return this.localScope.clear()
|
92
|
-
}
|
93
|
-
|
94
|
-
/**
|
95
|
-
* Creates a child scope from this one. This is used in function calls.
|
96
|
-
*
|
97
|
-
* @returns a new Map scope that has access to the symbols in the parent, but
|
98
|
-
* cannot overwrite them.
|
99
|
-
*/
|
100
|
-
createSubScope () {
|
101
|
-
return new AdvancedMapScope(this)
|
102
|
-
}
|
103
|
-
|
104
|
-
toString () {
|
105
|
-
return this.localScope.toString()
|
106
|
-
}
|
107
|
-
}
|
108
|
-
|
109
|
-
withObjectScope()
|
110
|
-
// Where safety is important, scope can also be a Map
|
111
|
-
withMapScope(new Map(), 'simple Map')
|
112
|
-
// Where flexibility is important, scope can duck type appear to be a Map.
|
113
|
-
withMapScope(new MapScope(), 'MapScope example')
|
114
|
-
// Extra methods allow even finer grain control.
|
115
|
-
withMapScope(new AdvancedMapScope(), 'AdvancedScope example')
|
@@ -1,55 +0,0 @@
|
|
1
|
-
const { parse, ConstantNode } = require('../..')
|
2
|
-
|
3
|
-
// Filter an expression tree
|
4
|
-
console.log('Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y"')
|
5
|
-
const node = parse('x^2 + x/4 + 3*y')
|
6
|
-
const filtered = node.filter(function (node) {
|
7
|
-
return node.isSymbolNode && node.name === 'x'
|
8
|
-
})
|
9
|
-
// returns an array with two entries: two SymbolNodes 'x'
|
10
|
-
|
11
|
-
filtered.forEach(function (node) {
|
12
|
-
console.log(node.type, node.toString())
|
13
|
-
})
|
14
|
-
// outputs:
|
15
|
-
// SymbolNode x
|
16
|
-
// SymbolNode x
|
17
|
-
|
18
|
-
// Traverse an expression tree
|
19
|
-
console.log()
|
20
|
-
console.log('Traverse the expression tree of expression "3 * x + 2"')
|
21
|
-
const node1 = parse('3 * x + 2')
|
22
|
-
node1.traverse(function (node, path, parent) {
|
23
|
-
switch (node.type) {
|
24
|
-
case 'OperatorNode':
|
25
|
-
console.log(node.type, node.op)
|
26
|
-
break
|
27
|
-
case 'ConstantNode':
|
28
|
-
console.log(node.type, node.value)
|
29
|
-
break
|
30
|
-
case 'SymbolNode':
|
31
|
-
console.log(node.type, node.name)
|
32
|
-
break
|
33
|
-
default: console.log(node.type)
|
34
|
-
}
|
35
|
-
})
|
36
|
-
// outputs:
|
37
|
-
// OperatorNode +
|
38
|
-
// OperatorNode *
|
39
|
-
// ConstantNode 3
|
40
|
-
// SymbolNode x
|
41
|
-
// ConstantNode 2
|
42
|
-
|
43
|
-
// transform an expression tree
|
44
|
-
console.log()
|
45
|
-
console.log('Replace all symbol nodes "x" in expression "x^2 + 5*x" with a constant 3')
|
46
|
-
const node2 = parse('x^2 + 5*x')
|
47
|
-
const transformed = node2.transform(function (node, path, parent) {
|
48
|
-
if (node.isSymbolNode && node.name === 'x') {
|
49
|
-
return new ConstantNode(3)
|
50
|
-
} else {
|
51
|
-
return node
|
52
|
-
}
|
53
|
-
})
|
54
|
-
console.log(transformed.toString())
|
55
|
-
// outputs: '3 ^ 2 + 5 * 3'
|
@@ -1,50 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Function transforms
|
3
|
-
*
|
4
|
-
* When using functions via the expression parser, it is possible to preprocess
|
5
|
-
* function arguments and post process a functions return value by writing a
|
6
|
-
* *transform* for the function. A transform is a function wrapping around a
|
7
|
-
* function to be transformed or completely replaces a function.
|
8
|
-
*/
|
9
|
-
const { create, all } = require('../..')
|
10
|
-
const math = create(all)
|
11
|
-
|
12
|
-
// create a function
|
13
|
-
function addIt (a, b) {
|
14
|
-
return a + b
|
15
|
-
}
|
16
|
-
|
17
|
-
// attach a transform function to the function addIt
|
18
|
-
addIt.transform = function (a, b) {
|
19
|
-
console.log('input: a=' + a + ', b=' + b)
|
20
|
-
// we can manipulate the input arguments here before executing addIt
|
21
|
-
|
22
|
-
const res = addIt(a, b)
|
23
|
-
|
24
|
-
console.log('result: ' + res)
|
25
|
-
// we can manipulate the result here before returning
|
26
|
-
|
27
|
-
return res
|
28
|
-
}
|
29
|
-
|
30
|
-
// import the function into math.js
|
31
|
-
math.import({
|
32
|
-
addIt: addIt
|
33
|
-
})
|
34
|
-
|
35
|
-
// use the function via the expression parser
|
36
|
-
console.log('Using expression parser:')
|
37
|
-
console.log('2+4=' + math.evaluate('addIt(2, 4)'))
|
38
|
-
// This will output:
|
39
|
-
//
|
40
|
-
// input: a=2, b=4
|
41
|
-
// result: 6
|
42
|
-
// 2+4=6
|
43
|
-
|
44
|
-
// when used via plain JavaScript, the transform is not invoked
|
45
|
-
console.log('')
|
46
|
-
console.log('Using plain JavaScript:')
|
47
|
-
console.log('2+4=' + math.addIt(2, 4))
|
48
|
-
// This will output:
|
49
|
-
//
|
50
|
-
// 6
|
@@ -1,36 +0,0 @@
|
|
1
|
-
// Expression parser 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 the
|
7
|
-
// code server side.
|
8
|
-
//
|
9
|
-
// There is a small number of functions which yield the biggest security risk
|
10
|
-
// in the expression parser of math.js:
|
11
|
-
//
|
12
|
-
// - `import` and `createUnit` which alter the built-in functionality and allow
|
13
|
-
// overriding existing functions and units.
|
14
|
-
// - `evaluate`, `parse`, `simplify`, and `derivative` which parse arbitrary input
|
15
|
-
// into a manipulable expression tree.
|
16
|
-
//
|
17
|
-
// To make the expression parser less vulnerable whilst still supporting most
|
18
|
-
// functionality, these functions can be disabled, as demonstrated in this
|
19
|
-
// example.
|
20
|
-
|
21
|
-
const { create, all } = require('../..')
|
22
|
-
const math = create(all)
|
23
|
-
|
24
|
-
const limitedEvaluate = math.evaluate
|
25
|
-
|
26
|
-
math.import({
|
27
|
-
import: function () { throw new Error('Function import is disabled') },
|
28
|
-
createUnit: function () { throw new Error('Function createUnit is disabled') },
|
29
|
-
evaluate: function () { throw new Error('Function evaluate is disabled') },
|
30
|
-
parse: function () { throw new Error('Function parse is disabled') },
|
31
|
-
simplify: function () { throw new Error('Function simplify is disabled') },
|
32
|
-
derivative: function () { throw new Error('Function derivative is disabled') }
|
33
|
-
}, { override: true })
|
34
|
-
|
35
|
-
console.log(limitedEvaluate('sqrt(16)')) // Ok, 4
|
36
|
-
console.log(limitedEvaluate('parse("2+3")')) // Error: Function parse is disabled
|
@@ -1,43 +0,0 @@
|
|
1
|
-
// This example demonstrates how you could integrate support for BigInt
|
2
|
-
// in mathjs. It's just a proof of concept, for full support you will
|
3
|
-
// have to defined more functions and define conversions from and to
|
4
|
-
// other data types.
|
5
|
-
|
6
|
-
const { create, all, factory } = require('../..')
|
7
|
-
const math = create(all)
|
8
|
-
|
9
|
-
// we can also add conversions here from number or string to BigInt
|
10
|
-
// and vice versa using math.typed.addConversion(...)
|
11
|
-
|
12
|
-
math.import([
|
13
|
-
factory('BigInt', ['typed'], function createBigInt ({ typed }) {
|
14
|
-
typed.addType({
|
15
|
-
name: 'BigInt',
|
16
|
-
test: (x) => typeof x === 'bigint' // eslint-disable-line
|
17
|
-
})
|
18
|
-
|
19
|
-
return BigInt // eslint-disable-line
|
20
|
-
}, { lazy: false }),
|
21
|
-
|
22
|
-
factory('bigint', ['typed', 'BigInt'], function createBigint ({ typed, BigInt }) {
|
23
|
-
return typed('bigint', {
|
24
|
-
'number | string ': (x) => BigInt(x) // eslint-disable-line
|
25
|
-
})
|
26
|
-
}),
|
27
|
-
|
28
|
-
factory('add', ['typed'], function createBigIntAdd ({ typed }) {
|
29
|
-
return typed('add', {
|
30
|
-
'BigInt, BigInt': (a, b) => a + b
|
31
|
-
})
|
32
|
-
}),
|
33
|
-
|
34
|
-
factory('pow', ['typed'], function createBigIntPow ({ typed }) {
|
35
|
-
return typed('pow', {
|
36
|
-
'BigInt, BigInt': (a, b) => a ** b
|
37
|
-
})
|
38
|
-
})
|
39
|
-
])
|
40
|
-
|
41
|
-
console.log(math.evaluate('4349 + 5249'))
|
42
|
-
console.log(math.evaluate('bigint(4349) + bigint(5249)'))
|
43
|
-
console.log(math.evaluate('bigint(4349) ^ bigint(5249)'))
|
@@ -1,24 +0,0 @@
|
|
1
|
-
const { create, all } = require('../../..')
|
2
|
-
const workerpool = require('workerpool')
|
3
|
-
const math = create(all)
|
4
|
-
|
5
|
-
// disable the import function so the math.js instance cannot be changed
|
6
|
-
function noImport () {
|
7
|
-
throw new Error('function import is disabled.')
|
8
|
-
}
|
9
|
-
math.import({ import: noImport }, { override: true })
|
10
|
-
|
11
|
-
/**
|
12
|
-
* Evaluate an expression
|
13
|
-
* @param {string} expr
|
14
|
-
* @return {string} result
|
15
|
-
*/
|
16
|
-
function evaluate (expr) {
|
17
|
-
const ans = math.evaluate(expr)
|
18
|
-
return math.format(ans)
|
19
|
-
}
|
20
|
-
|
21
|
-
// create a worker and register public functions
|
22
|
-
workerpool.worker({
|
23
|
-
evaluate: evaluate
|
24
|
-
})
|
@@ -1,80 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* This example demonstrates how to run math.js in a child process with limited
|
3
|
-
* execution time.
|
4
|
-
*
|
5
|
-
* Prerequisites:
|
6
|
-
*
|
7
|
-
* npm install express workerpool
|
8
|
-
*
|
9
|
-
* Start the server:
|
10
|
-
*
|
11
|
-
* node ./server.js
|
12
|
-
*
|
13
|
-
* Make a request to the server:
|
14
|
-
*
|
15
|
-
* GET http://localhost:8080/mathjs?expr=sqrt(16)
|
16
|
-
*
|
17
|
-
* Note that the query parameter `expr` should be properly url encoded.
|
18
|
-
*/
|
19
|
-
const path = require('path')
|
20
|
-
|
21
|
-
let express
|
22
|
-
let workerpool
|
23
|
-
try {
|
24
|
-
express = require('express')
|
25
|
-
workerpool = require('workerpool')
|
26
|
-
} catch (err) {
|
27
|
-
console.log('Error: To run this example, install express and workerpool first via:\n\n' +
|
28
|
-
' npm install express workerpool\n')
|
29
|
-
process.exit()
|
30
|
-
}
|
31
|
-
|
32
|
-
const app = express()
|
33
|
-
const pool = workerpool.pool(path.join(__dirname, '/math_worker.js'))
|
34
|
-
|
35
|
-
const TIMEOUT = 10000 // milliseconds
|
36
|
-
|
37
|
-
/**
|
38
|
-
* GET /mathjs?expr=...
|
39
|
-
*/
|
40
|
-
app.get('/mathjs', function (req, res) {
|
41
|
-
const expr = req.query.expr
|
42
|
-
if (expr === undefined) {
|
43
|
-
return res.status(400).send('Error: Required query parameter "expr" missing in url.')
|
44
|
-
}
|
45
|
-
|
46
|
-
pool.exec('evaluate', [expr])
|
47
|
-
.timeout(TIMEOUT)
|
48
|
-
.then(function (result) {
|
49
|
-
res.send(result)
|
50
|
-
})
|
51
|
-
.catch(function (err) {
|
52
|
-
res.status(400).send(formatError(err))
|
53
|
-
})
|
54
|
-
})
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Format error messages as string
|
58
|
-
* @param {Error} err
|
59
|
-
* @return {String} message
|
60
|
-
*/
|
61
|
-
function formatError (err) {
|
62
|
-
if (err instanceof workerpool.Promise.TimeoutError) {
|
63
|
-
return 'TimeoutError: Evaluation exceeded maximum duration of ' + TIMEOUT / 1000 + ' seconds'
|
64
|
-
} else {
|
65
|
-
return err.toString()
|
66
|
-
}
|
67
|
-
}
|
68
|
-
|
69
|
-
// handle uncaught exceptions so the application cannot crash
|
70
|
-
process.on('uncaughtException', function (err) {
|
71
|
-
console.log('Caught exception: ' + err)
|
72
|
-
console.trace()
|
73
|
-
})
|
74
|
-
|
75
|
-
// start the server
|
76
|
-
const PORT = process.env.PORT || 8080
|
77
|
-
app.listen(PORT, function () {
|
78
|
-
console.log('Listening at http://localhost:' + PORT)
|
79
|
-
console.log('Example request:\n GET http://localhost:' + PORT + '/mathjs?expr=sqrt(16)')
|
80
|
-
})
|
package/examples/algebra.js
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
// algebra
|
2
|
-
//
|
3
|
-
// math.js has support for symbolic computation (CAS). It can parse
|
4
|
-
// expressions in an expression tree and do algebraic operations like
|
5
|
-
// simplification and derivation on this tree.
|
6
|
-
|
7
|
-
// load math.js (using node.js)
|
8
|
-
const { simplify, parse, derivative } = require('..')
|
9
|
-
|
10
|
-
// simplify an expression
|
11
|
-
console.log('simplify expressions')
|
12
|
-
console.log(simplify('3 + 2 / 4').toString()) // '7 / 2'
|
13
|
-
console.log(simplify('2x + 3x').toString()) // '5 * x'
|
14
|
-
console.log(simplify('2 * 3 * x', { x: 4 }).toString()) // '24'
|
15
|
-
console.log(simplify('x^2 + x + 3 + x^2').toString()) // '2 * x ^ 2 + x + 3'
|
16
|
-
console.log(simplify('x * y * -x / (x ^ 2)').toString()) // '-y'
|
17
|
-
|
18
|
-
// work with an expression tree, evaluate results
|
19
|
-
const f = parse('2x + x')
|
20
|
-
const simplified = simplify(f)
|
21
|
-
console.log(simplified.toString()) // '3 * x'
|
22
|
-
console.log(simplified.evaluate({ x: 4 })) // 12
|
23
|
-
console.log()
|
24
|
-
|
25
|
-
// calculate a derivative
|
26
|
-
console.log('calculate derivatives')
|
27
|
-
console.log(derivative('2x^2 + 3x + 4', 'x').toString()) // '4 * x + 3'
|
28
|
-
console.log(derivative('sin(2x)', 'x').toString()) // '2 * cos(2 * x)'
|
29
|
-
|
30
|
-
// work with an expression tree, evaluate results
|
31
|
-
const h = parse('x^2 + x')
|
32
|
-
const dh = derivative(h, 'x')
|
33
|
-
console.log(dh.toString()) // '2 * x + 1'
|
34
|
-
console.log(dh.evaluate({ x: 3 })) // '7'
|
package/examples/basic_usage.js
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
// basic usage
|
2
|
-
|
3
|
-
// load math.js (using node.js)
|
4
|
-
const math = require('..')
|
5
|
-
|
6
|
-
// functions and constants
|
7
|
-
console.log('functions and constants')
|
8
|
-
print(math.round(math.e, 3)) // 2.718
|
9
|
-
print(math.atan2(3, -3) / math.pi) // 0.75
|
10
|
-
print(math.log(10000, 10)) // 4
|
11
|
-
print(math.sqrt(-4)) // 2i
|
12
|
-
print(math.pow([[-1, 2], [3, 1]], 2)) // [[7, 0], [0, 7]]
|
13
|
-
print(math.derivative('x^2 + x', 'x')) // 2 * x + 1
|
14
|
-
console.log()
|
15
|
-
|
16
|
-
// expressions
|
17
|
-
console.log('expressions')
|
18
|
-
print(math.evaluate('1.2 * (2 + 4.5)')) // 7.8
|
19
|
-
print(math.evaluate('12.7 cm to inch')) // 5 inch
|
20
|
-
print(math.evaluate('sin(45 deg) ^ 2')) // 0.5
|
21
|
-
print(math.evaluate('9 / 3 + 2i')) // 3 + 2i
|
22
|
-
print(math.evaluate('det([-1, 2; 3, 1])')) // -7
|
23
|
-
console.log()
|
24
|
-
|
25
|
-
// chained operations
|
26
|
-
console.log('chained operations')
|
27
|
-
const a = math.chain(3)
|
28
|
-
.add(4)
|
29
|
-
.multiply(2)
|
30
|
-
.done()
|
31
|
-
print(a) // 14
|
32
|
-
console.log()
|
33
|
-
|
34
|
-
// mixed use of different data types in functions
|
35
|
-
console.log('mixed use of data types')
|
36
|
-
print(math.add(4, [5, 6])) // number + Array, [9, 10]
|
37
|
-
print(math.multiply(math.unit('5 mm'), 3)) // Unit * number, 15 mm
|
38
|
-
print(math.subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]
|
39
|
-
print(math.add(math.matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]
|
40
|
-
console.log()
|
41
|
-
|
42
|
-
/**
|
43
|
-
* Helper function to output a value in the console. Value will be formatted.
|
44
|
-
* @param {*} value
|
45
|
-
*/
|
46
|
-
function print (value) {
|
47
|
-
const precision = 14
|
48
|
-
console.log(math.format(value, precision))
|
49
|
-
}
|
package/examples/bignumbers.js
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
/* eslint-disable no-loss-of-precision */
|
2
|
-
|
3
|
-
// BigNumbers
|
4
|
-
|
5
|
-
const { create, all } = require('..')
|
6
|
-
|
7
|
-
// configure the default type of numbers as BigNumbers
|
8
|
-
const config = {
|
9
|
-
// Default type of number
|
10
|
-
// Available options: 'number' (default), 'BigNumber', or 'Fraction'
|
11
|
-
number: 'BigNumber',
|
12
|
-
|
13
|
-
// Number of significant digits for BigNumbers
|
14
|
-
precision: 20
|
15
|
-
}
|
16
|
-
const math = create(all, config)
|
17
|
-
|
18
|
-
console.log('round-off errors with numbers')
|
19
|
-
print(math.add(0.1, 0.2)) // number, 0.30000000000000004
|
20
|
-
print(math.divide(0.3, 0.2)) // number, 1.4999999999999998
|
21
|
-
console.log()
|
22
|
-
|
23
|
-
console.log('no round-off errors with BigNumbers')
|
24
|
-
print(math.add(math.bignumber(0.1), math.bignumber(0.2))) // BigNumber, 0.3
|
25
|
-
print(math.divide(math.bignumber(0.3), math.bignumber(0.2))) // BigNumber, 1.5
|
26
|
-
console.log()
|
27
|
-
|
28
|
-
console.log('create BigNumbers from strings when exceeding the range of a number')
|
29
|
-
print(math.bignumber(1.2e+500)) // BigNumber, Infinity WRONG
|
30
|
-
print(math.bignumber('1.2e+500')) // BigNumber, 1.2e+500
|
31
|
-
console.log()
|
32
|
-
|
33
|
-
console.log('BigNumbers still have a limited precision and are no silve bullet')
|
34
|
-
const third = math.divide(math.bignumber(1), math.bignumber(3))
|
35
|
-
const total = math.add(third, third, third)
|
36
|
-
print(total) // BigNumber, 0.99999999999999999999
|
37
|
-
console.log()
|
38
|
-
|
39
|
-
// one can work conveniently with BigNumbers using the expression parser.
|
40
|
-
// note though that BigNumbers are only supported in arithmetic functions
|
41
|
-
console.log('use BigNumbers in the expression parser')
|
42
|
-
print(math.evaluate('0.1 + 0.2')) // BigNumber, 0.3
|
43
|
-
print(math.evaluate('0.3 / 0.2')) // BigNumber, 1.5
|
44
|
-
console.log()
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Helper function to output a value in the console. Value will be formatted.
|
48
|
-
* @param {*} value
|
49
|
-
*/
|
50
|
-
function print (value) {
|
51
|
-
console.log(math.format(value))
|
52
|
-
}
|