mathjs 10.5.2 → 10.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (329) hide show
  1. package/HISTORY.md +28 -0
  2. package/lib/browser/math.js +6 -5
  3. package/lib/browser/math.js.map +1 -1
  4. package/lib/cjs/entry/dependenciesAny/dependenciesFft.generated.js +41 -0
  5. package/lib/cjs/entry/dependenciesAny/dependenciesIfft.generated.js +29 -0
  6. package/lib/cjs/entry/dependenciesAny.generated.js +16 -0
  7. package/lib/cjs/entry/impureFunctionsAny.generated.js +66 -64
  8. package/lib/cjs/entry/pureFunctionsAny.generated.js +189 -171
  9. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +6 -0
  10. package/lib/cjs/expression/embeddedDocs/function/matrix/fft.js +15 -0
  11. package/lib/cjs/expression/embeddedDocs/function/matrix/ifft.js +15 -0
  12. package/lib/cjs/factoriesAny.js +16 -0
  13. package/lib/cjs/function/algebra/derivative.js +1 -1
  14. package/lib/cjs/function/matrix/fft.js +128 -0
  15. package/lib/cjs/function/matrix/ifft.js +49 -0
  16. package/lib/cjs/header.js +2 -2
  17. package/lib/cjs/version.js +1 -1
  18. package/lib/esm/entry/dependenciesAny/dependenciesFft.generated.js +24 -0
  19. package/lib/esm/entry/dependenciesAny/dependenciesIfft.generated.js +16 -0
  20. package/lib/esm/entry/dependenciesAny.generated.js +2 -0
  21. package/lib/esm/entry/impureFunctionsAny.generated.js +63 -61
  22. package/lib/esm/entry/pureFunctionsAny.generated.js +161 -145
  23. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +4 -0
  24. package/lib/esm/expression/embeddedDocs/function/matrix/fft.js +8 -0
  25. package/lib/esm/expression/embeddedDocs/function/matrix/ifft.js +8 -0
  26. package/lib/esm/factoriesAny.js +2 -0
  27. package/lib/esm/function/algebra/derivative.js +1 -1
  28. package/lib/esm/function/matrix/fft.js +104 -0
  29. package/lib/esm/function/matrix/ifft.js +38 -0
  30. package/lib/esm/version.js +1 -1
  31. package/package.json +22 -24
  32. package/types/index.d.ts +1052 -283
  33. package/types/index.ts +711 -7
  34. package/docs/command_line_interface.md +0 -87
  35. package/docs/core/chaining.md +0 -41
  36. package/docs/core/configuration.md +0 -144
  37. package/docs/core/extension.md +0 -263
  38. package/docs/core/index.md +0 -21
  39. package/docs/core/serialization.md +0 -50
  40. package/docs/custom_bundling.md +0 -116
  41. package/docs/datatypes/bignumbers.md +0 -102
  42. package/docs/datatypes/complex_numbers.md +0 -168
  43. package/docs/datatypes/fractions.md +0 -75
  44. package/docs/datatypes/index.md +0 -67
  45. package/docs/datatypes/matrices.md +0 -375
  46. package/docs/datatypes/numbers.md +0 -106
  47. package/docs/datatypes/units.md +0 -444
  48. package/docs/expressions/algebra.md +0 -110
  49. package/docs/expressions/customization.md +0 -379
  50. package/docs/expressions/expression_trees.md +0 -710
  51. package/docs/expressions/html_classes.md +0 -38
  52. package/docs/expressions/index.md +0 -21
  53. package/docs/expressions/parsing.md +0 -224
  54. package/docs/expressions/security.md +0 -89
  55. package/docs/expressions/syntax.md +0 -706
  56. package/docs/getting_started.md +0 -124
  57. package/docs/index.md +0 -39
  58. package/docs/reference/classes/densematrix.md +0 -247
  59. package/docs/reference/classes/fibonacciheap.md +0 -70
  60. package/docs/reference/classes/matrixindex.md +0 -133
  61. package/docs/reference/classes/matrixrange.md +0 -158
  62. package/docs/reference/classes/resultset.md +0 -47
  63. package/docs/reference/classes/sparsematrix.md +0 -245
  64. package/docs/reference/classes/unit.md +0 -242
  65. package/docs/reference/classes.md +0 -86
  66. package/docs/reference/constants.md +0 -29
  67. package/docs/reference/functions/abs.md +0 -46
  68. package/docs/reference/functions/acos.md +0 -49
  69. package/docs/reference/functions/acosh.md +0 -47
  70. package/docs/reference/functions/acot.md +0 -48
  71. package/docs/reference/functions/acoth.md +0 -46
  72. package/docs/reference/functions/acsc.md +0 -49
  73. package/docs/reference/functions/acsch.md +0 -46
  74. package/docs/reference/functions/add.md +0 -59
  75. package/docs/reference/functions/and.md +0 -53
  76. package/docs/reference/functions/apply.md +0 -56
  77. package/docs/reference/functions/arg.md +0 -53
  78. package/docs/reference/functions/asec.md +0 -49
  79. package/docs/reference/functions/asech.md +0 -46
  80. package/docs/reference/functions/asin.md +0 -49
  81. package/docs/reference/functions/asinh.md +0 -46
  82. package/docs/reference/functions/atan.md +0 -49
  83. package/docs/reference/functions/atan2.md +0 -56
  84. package/docs/reference/functions/atanh.md +0 -46
  85. package/docs/reference/functions/bellNumbers.md +0 -45
  86. package/docs/reference/functions/bignumber.md +0 -47
  87. package/docs/reference/functions/bin.md +0 -45
  88. package/docs/reference/functions/bitAnd.md +0 -51
  89. package/docs/reference/functions/bitNot.md +0 -51
  90. package/docs/reference/functions/bitOr.md +0 -52
  91. package/docs/reference/functions/bitXor.md +0 -51
  92. package/docs/reference/functions/boolean.md +0 -50
  93. package/docs/reference/functions/catalan.md +0 -45
  94. package/docs/reference/functions/cbrt.md +0 -60
  95. package/docs/reference/functions/ceil.md +0 -63
  96. package/docs/reference/functions/chain.md +0 -54
  97. package/docs/reference/functions/clone.md +0 -43
  98. package/docs/reference/functions/column.md +0 -45
  99. package/docs/reference/functions/combinations.md +0 -49
  100. package/docs/reference/functions/combinationsWithRep.md +0 -49
  101. package/docs/reference/functions/compare.md +0 -67
  102. package/docs/reference/functions/compareNatural.md +0 -92
  103. package/docs/reference/functions/compareText.md +0 -54
  104. package/docs/reference/functions/compile.md +0 -56
  105. package/docs/reference/functions/complex.md +0 -17
  106. package/docs/reference/functions/composition.md +0 -46
  107. package/docs/reference/functions/concat.md +0 -56
  108. package/docs/reference/functions/config.md +0 -44
  109. package/docs/reference/functions/conj.md +0 -50
  110. package/docs/reference/functions/cos.md +0 -51
  111. package/docs/reference/functions/cosh.md +0 -46
  112. package/docs/reference/functions/cot.md +0 -47
  113. package/docs/reference/functions/coth.md +0 -49
  114. package/docs/reference/functions/count.md +0 -45
  115. package/docs/reference/functions/createUnit.md +0 -52
  116. package/docs/reference/functions/cross.md +0 -58
  117. package/docs/reference/functions/csc.md +0 -47
  118. package/docs/reference/functions/csch.md +0 -49
  119. package/docs/reference/functions/ctranspose.md +0 -50
  120. package/docs/reference/functions/cube.md +0 -51
  121. package/docs/reference/functions/cumsum.md +0 -57
  122. package/docs/reference/functions/deepEqual.md +0 -53
  123. package/docs/reference/functions/derivative.md +0 -61
  124. package/docs/reference/functions/det.md +0 -49
  125. package/docs/reference/functions/diag.md +0 -61
  126. package/docs/reference/functions/diff.md +0 -70
  127. package/docs/reference/functions/distance.md +0 -80
  128. package/docs/reference/functions/divide.md +0 -55
  129. package/docs/reference/functions/dot.md +0 -48
  130. package/docs/reference/functions/dotDivide.md +0 -52
  131. package/docs/reference/functions/dotMultiply.md +0 -52
  132. package/docs/reference/functions/dotPow.md +0 -49
  133. package/docs/reference/functions/eigs.md +0 -54
  134. package/docs/reference/functions/equal.md +0 -75
  135. package/docs/reference/functions/equalText.md +0 -53
  136. package/docs/reference/functions/erf.md +0 -49
  137. package/docs/reference/functions/evaluate.md +0 -56
  138. package/docs/reference/functions/exp.md +0 -54
  139. package/docs/reference/functions/expm.md +0 -49
  140. package/docs/reference/functions/expm1.md +0 -54
  141. package/docs/reference/functions/factorial.md +0 -49
  142. package/docs/reference/functions/fft.md +0 -35
  143. package/docs/reference/functions/filter.md +0 -50
  144. package/docs/reference/functions/fix.md +0 -62
  145. package/docs/reference/functions/flatten.md +0 -46
  146. package/docs/reference/functions/floor.md +0 -66
  147. package/docs/reference/functions/forEach.md +0 -41
  148. package/docs/reference/functions/format.md +0 -127
  149. package/docs/reference/functions/fraction.md +0 -45
  150. package/docs/reference/functions/gamma.md +0 -49
  151. package/docs/reference/functions/gcd.md +0 -50
  152. package/docs/reference/functions/getMatrixDataType.md +0 -59
  153. package/docs/reference/functions/hasNumericValue.md +0 -55
  154. package/docs/reference/functions/help.md +0 -42
  155. package/docs/reference/functions/hex.md +0 -45
  156. package/docs/reference/functions/hypot.md +0 -51
  157. package/docs/reference/functions/identity.md +0 -57
  158. package/docs/reference/functions/ifft.md +0 -35
  159. package/docs/reference/functions/im.md +0 -53
  160. package/docs/reference/functions/import.md +0 -68
  161. package/docs/reference/functions/index.md +0 -55
  162. package/docs/reference/functions/intersect.md +0 -50
  163. package/docs/reference/functions/inv.md +0 -45
  164. package/docs/reference/functions/invmod.md +0 -47
  165. package/docs/reference/functions/isInteger.md +0 -55
  166. package/docs/reference/functions/isNaN.md +0 -56
  167. package/docs/reference/functions/isNegative.md +0 -55
  168. package/docs/reference/functions/isNumeric.md +0 -55
  169. package/docs/reference/functions/isPositive.md +0 -57
  170. package/docs/reference/functions/isPrime.md +0 -54
  171. package/docs/reference/functions/isZero.md +0 -59
  172. package/docs/reference/functions/kldivergence.md +0 -41
  173. package/docs/reference/functions/kron.md +0 -53
  174. package/docs/reference/functions/larger.md +0 -60
  175. package/docs/reference/functions/largerEq.md +0 -56
  176. package/docs/reference/functions/lcm.md +0 -54
  177. package/docs/reference/functions/leafCount.md +0 -52
  178. package/docs/reference/functions/leftShift.md +0 -52
  179. package/docs/reference/functions/lgamma.md +0 -46
  180. package/docs/reference/functions/log.md +0 -57
  181. package/docs/reference/functions/log10.md +0 -50
  182. package/docs/reference/functions/log1p.md +0 -54
  183. package/docs/reference/functions/log2.md +0 -50
  184. package/docs/reference/functions/lsolve.md +0 -51
  185. package/docs/reference/functions/lsolveAll.md +0 -51
  186. package/docs/reference/functions/lup.md +0 -52
  187. package/docs/reference/functions/lusolve.md +0 -59
  188. package/docs/reference/functions/mad.md +0 -50
  189. package/docs/reference/functions/map.md +0 -65
  190. package/docs/reference/functions/matrix.md +0 -56
  191. package/docs/reference/functions/matrixFromColumns.md +0 -49
  192. package/docs/reference/functions/matrixFromFunction.md +0 -54
  193. package/docs/reference/functions/matrixFromRows.md +0 -49
  194. package/docs/reference/functions/max.md +0 -61
  195. package/docs/reference/functions/mean.md +0 -57
  196. package/docs/reference/functions/median.md +0 -57
  197. package/docs/reference/functions/min.md +0 -61
  198. package/docs/reference/functions/mod.md +0 -59
  199. package/docs/reference/functions/mode.md +0 -50
  200. package/docs/reference/functions/multinomial.md +0 -46
  201. package/docs/reference/functions/multiply.md +0 -60
  202. package/docs/reference/functions/norm.md +0 -59
  203. package/docs/reference/functions/not.md +0 -50
  204. package/docs/reference/functions/nthRoot.md +0 -53
  205. package/docs/reference/functions/nthRoots.md +0 -58
  206. package/docs/reference/functions/number.md +0 -49
  207. package/docs/reference/functions/numeric.md +0 -52
  208. package/docs/reference/functions/oct.md +0 -45
  209. package/docs/reference/functions/ones.md +0 -59
  210. package/docs/reference/functions/or.md +0 -53
  211. package/docs/reference/functions/parse.md +0 -56
  212. package/docs/reference/functions/parser.md +0 -70
  213. package/docs/reference/functions/partitionSelect.md +0 -53
  214. package/docs/reference/functions/permutations.md +0 -51
  215. package/docs/reference/functions/pickRandom.md +0 -59
  216. package/docs/reference/functions/pinv.md +0 -44
  217. package/docs/reference/functions/pow.md +0 -65
  218. package/docs/reference/functions/print.md +0 -64
  219. package/docs/reference/functions/prod.md +0 -55
  220. package/docs/reference/functions/qr.md +0 -65
  221. package/docs/reference/functions/quantileSeq.md +0 -62
  222. package/docs/reference/functions/random.md +0 -54
  223. package/docs/reference/functions/randomInt.md +0 -53
  224. package/docs/reference/functions/range.md +0 -70
  225. package/docs/reference/functions/rationalize.md +0 -68
  226. package/docs/reference/functions/re.md +0 -53
  227. package/docs/reference/functions/reshape.md +0 -60
  228. package/docs/reference/functions/resize.md +0 -51
  229. package/docs/reference/functions/resolve.md +0 -46
  230. package/docs/reference/functions/rightArithShift.md +0 -52
  231. package/docs/reference/functions/rightLogShift.md +0 -52
  232. package/docs/reference/functions/rotate.md +0 -53
  233. package/docs/reference/functions/rotationMatrix.md +0 -57
  234. package/docs/reference/functions/round.md +0 -61
  235. package/docs/reference/functions/row.md +0 -45
  236. package/docs/reference/functions/sec.md +0 -47
  237. package/docs/reference/functions/sech.md +0 -49
  238. package/docs/reference/functions/setCartesian.md +0 -49
  239. package/docs/reference/functions/setDifference.md +0 -47
  240. package/docs/reference/functions/setDistinct.md +0 -43
  241. package/docs/reference/functions/setIntersect.md +0 -46
  242. package/docs/reference/functions/setIsSubset.md +0 -47
  243. package/docs/reference/functions/setMultiplicity.md +0 -46
  244. package/docs/reference/functions/setPowerset.md +0 -43
  245. package/docs/reference/functions/setSize.md +0 -47
  246. package/docs/reference/functions/setSymDifference.md +0 -47
  247. package/docs/reference/functions/setUnion.md +0 -46
  248. package/docs/reference/functions/sign.md +0 -52
  249. package/docs/reference/functions/simplify.md +0 -130
  250. package/docs/reference/functions/simplifyConstant.md +0 -52
  251. package/docs/reference/functions/simplifyCore.md +0 -50
  252. package/docs/reference/functions/sin.md +0 -51
  253. package/docs/reference/functions/sinh.md +0 -46
  254. package/docs/reference/functions/size.md +0 -50
  255. package/docs/reference/functions/slu.md +0 -57
  256. package/docs/reference/functions/smaller.md +0 -60
  257. package/docs/reference/functions/smallerEq.md +0 -56
  258. package/docs/reference/functions/sort.md +0 -56
  259. package/docs/reference/functions/sparse.md +0 -51
  260. package/docs/reference/functions/splitUnit.md +0 -37
  261. package/docs/reference/functions/sqrt.md +0 -50
  262. package/docs/reference/functions/sqrtm.md +0 -46
  263. package/docs/reference/functions/square.md +0 -51
  264. package/docs/reference/functions/squeeze.md +0 -53
  265. package/docs/reference/functions/std.md +0 -76
  266. package/docs/reference/functions/stirlingS2.md +0 -52
  267. package/docs/reference/functions/string.md +0 -49
  268. package/docs/reference/functions/subset.md +0 -65
  269. package/docs/reference/functions/subtract.md +0 -54
  270. package/docs/reference/functions/sum.md +0 -54
  271. package/docs/reference/functions/symbolicEqual.md +0 -62
  272. package/docs/reference/functions/tan.md +0 -49
  273. package/docs/reference/functions/tanh.md +0 -50
  274. package/docs/reference/functions/to.md +0 -47
  275. package/docs/reference/functions/trace.md +0 -50
  276. package/docs/reference/functions/transpose.md +0 -49
  277. package/docs/reference/functions/typeOf.md +0 -81
  278. package/docs/reference/functions/typed.md +0 -56
  279. package/docs/reference/functions/unaryMinus.md +0 -49
  280. package/docs/reference/functions/unaryPlus.md +0 -48
  281. package/docs/reference/functions/unequal.md +0 -74
  282. package/docs/reference/functions/unit.md +0 -48
  283. package/docs/reference/functions/usolve.md +0 -51
  284. package/docs/reference/functions/usolveAll.md +0 -51
  285. package/docs/reference/functions/variance.md +0 -78
  286. package/docs/reference/functions/xgcd.md +0 -47
  287. package/docs/reference/functions/xor.md +0 -53
  288. package/docs/reference/functions/zeros.md +0 -57
  289. package/docs/reference/functions.md +0 -297
  290. package/docs/reference/index.md +0 -5
  291. package/examples/advanced/convert_fraction_to_bignumber.js +0 -59
  292. package/examples/advanced/custom_argument_parsing.js +0 -98
  293. package/examples/advanced/custom_datatype.js +0 -56
  294. package/examples/advanced/custom_evaluate_using_factories.js +0 -19
  295. package/examples/advanced/custom_evaluate_using_import.js +0 -18
  296. package/examples/advanced/custom_loading.js +0 -33
  297. package/examples/advanced/custom_relational_functions.js +0 -62
  298. package/examples/advanced/custom_scope_objects.js +0 -115
  299. package/examples/advanced/expression_trees.js +0 -55
  300. package/examples/advanced/function_transform.js +0 -50
  301. package/examples/advanced/more_secure_eval.js +0 -36
  302. package/examples/advanced/use_bigint.js +0 -43
  303. package/examples/advanced/web_server/math_worker.js +0 -24
  304. package/examples/advanced/web_server/server.js +0 -80
  305. package/examples/algebra.js +0 -34
  306. package/examples/basic_usage.js +0 -49
  307. package/examples/bignumbers.js +0 -52
  308. package/examples/browser/angle_configuration.html +0 -134
  309. package/examples/browser/basic_usage.html +0 -39
  310. package/examples/browser/currency_conversion.html +0 -125
  311. package/examples/browser/custom_separators.html +0 -81
  312. package/examples/browser/plot.html +0 -78
  313. package/examples/browser/pretty_printing_with_mathjax.html +0 -122
  314. package/examples/browser/printing_html.html +0 -170
  315. package/examples/browser/requirejs_loading.html +0 -20
  316. package/examples/browser/rocket_trajectory_optimization.html +0 -301
  317. package/examples/browser/webworkers/webworkers.html +0 -80
  318. package/examples/browser/webworkers/worker.js +0 -28
  319. package/examples/chaining.js +0 -56
  320. package/examples/complex_numbers.js +0 -67
  321. package/examples/expressions.js +0 -188
  322. package/examples/fractions.js +0 -74
  323. package/examples/import.js +0 -95
  324. package/examples/matrices.js +0 -101
  325. package/examples/objects.js +0 -35
  326. package/examples/package.json +0 -3
  327. package/examples/serialization.js +0 -16
  328. package/examples/sparse_matrices.js +0 -19
  329. package/examples/units.js +0 -108
@@ -1,710 +0,0 @@
1
- # Expression trees
2
-
3
- When parsing an expression via `math.parse(expr)`, math.js generates an
4
- expression tree and returns the root node of the tree. An expression tree can
5
- be used to analyze, manipulate, and evaluate expressions.
6
-
7
- Example:
8
-
9
- ```js
10
- const node = math.parse('sqrt(2 + x)')
11
- ```
12
-
13
- In this case, the expression `sqrt(2 + x)` is parsed as:
14
-
15
- ```
16
- FunctionNode sqrt
17
- |
18
- OperatorNode +
19
- / \
20
- ConstantNode 2 x SymbolNode
21
- ```
22
-
23
- Alternatively, this expression tree can be build by manually creating nodes:
24
-
25
- ```js
26
- const node1 = new math.ConstantNode(2)
27
- const node2 = new math.SymbolNode('x')
28
- const node3 = new math.OperatorNode('+', 'add', [node1, node2])
29
- const node4 = new math.FunctionNode('sqrt', [node3])
30
- ```
31
-
32
- The resulting expression tree with root node `node4` is equal to the expression
33
- tree generated by `math.parse('sqrt(2 + x)')`.
34
-
35
-
36
- ## API
37
-
38
- ### Methods
39
-
40
- All nodes have the following methods:
41
-
42
- - `clone() : Node`
43
-
44
- Create a shallow clone of the node.
45
- The node itself is cloned, its childs are not cloned.
46
-
47
- - `cloneDeep() : Node`
48
-
49
- Create a deep clone of the node.
50
- Both the node as well as all its childs are cloned recursively.
51
-
52
- - `compile() : Object`
53
-
54
- Compile an expression into optimized JavaScript code. `compile` returns an
55
- object with a function `evaluate([scope])` to evaluate. Example:
56
-
57
- ```js
58
- const node = math.parse('2 + x') // returns the root Node of an expression tree
59
- const code = node.compile() // returns {evaluate: function (scope) {...}}
60
- const evaluate = code.evaluate({x: 3}) // returns 5
61
- ```
62
-
63
- - `evaluate([scope]) : Object`
64
-
65
- Compile and evaluate an expression, this is the equivalent of doing
66
- `node.compile().evaluate(scope)`. Example:
67
-
68
- ```js
69
- const node = math.parse('2 + x') // returns the root Node of an expression tree
70
- const evaluate = node.evaluate({x: 3}) // returns 5
71
- ```
72
-
73
- - `equals(other: Node) : boolean`
74
-
75
- Test whether this node equals an other node. Does a deep comparison of the
76
- values of both nodes.
77
-
78
- - `filter(callback: function) : Node[]`
79
-
80
- Recursively filter nodes in an expression tree. The `callback` function is
81
- called as `callback(node: Node, path: string, parent: Node) : boolean` for
82
- every node in the tree, and must return a boolean. The function `filter`
83
- returns an array with nodes for which the test returned true.
84
- Parameter `path` is a string containing a relative JSON Path.
85
-
86
- Example:
87
-
88
- ```js
89
- const node = math.parse('x^2 + x/4 + 3*y')
90
- const filtered = node.filter(function (node) {
91
- return node.isSymbolNode && node.name === 'x'
92
- })
93
- // returns an array with two entries: two SymbolNodes 'x'
94
- ```
95
-
96
- - `forEach(callback: function) : Node[]`
97
-
98
- Execute a callback for each of the child nodes of this node. The `callback`
99
- function is called as `callback(child: Node, path: string, parent: Node)`.
100
- Parameter `path` is a string containing a relative JSON Path.
101
-
102
- See also `traverse`, which is a recursive version of `forEach`.
103
-
104
- Example:
105
-
106
- ```js
107
- const node = math.parse('3 * x + 2')
108
- node.forEach(function (node, path, parent) {
109
- switch (node.type) {
110
- case 'OperatorNode':
111
- console.log(node.type, node.op)
112
- break
113
- case 'ConstantNode':
114
- console.log(node.type, node.value)
115
- break
116
- case 'SymbolNode':
117
- console.log(node.type, node.name)
118
- break
119
- default:
120
- console.log(node.type)
121
- }
122
- })
123
- // outputs:
124
- // OperatorNode *
125
- // ConstantNode 2
126
- ```
127
-
128
- - `map(callback: function) : Node[]`
129
-
130
- Transform a node. Creates a new Node having it's childs be the results of
131
- calling the provided callback function for each of the childs of the original
132
- node. The `callback` function is called as `callback(child: Node, path: string,
133
- parent: Node)` and must return a Node. Parameter `path` is a string containing
134
- a relative JSON Path.
135
-
136
- See also `transform`, which is a recursive version of `map`.
137
-
138
- - `toHTML(options: object): string`
139
-
140
- Get a HTML representation of the parsed expression. Example:
141
-
142
- ```js
143
- const node = math.parse('sqrt(2/3)')
144
- node.toHTML()
145
- // returns
146
- // <span class="math-function">sqrt</span>
147
- // <span class="math-paranthesis math-round-parenthesis">(</span>
148
- // <span class="math-number">2</span>
149
- // <span class="math-operator math-binary-operator math-explicit-binary-operator">/</span>
150
- // <span class="math-number">3</span>
151
- // <span class="math-paranthesis math-round-parenthesis">)</span>
152
- ```
153
-
154
- Information about the available HTML classes in [HTML Classes](html_classes.md).
155
- Information about the options in [Customization](customization.md#custom-html-latex-and-string-output).
156
-
157
- - `toString(options: object) : string`
158
-
159
- Get a string representation of the parsed expression. This is not exactly
160
- the same as the original input. Example:
161
-
162
- ```js
163
- const node = math.parse('3+4*2')
164
- node.toString() // returns '3 + (4 * 2)'
165
- ```
166
-
167
- Information about the options in [Customization](customization.md#custom-html-latex-and-string-output).
168
-
169
- - `toTex(options: object): string`
170
-
171
- Get a [LaTeX](https://en.wikipedia.org/wiki/LaTeX) representation of the
172
- expression. Example:
173
-
174
- ```js
175
- const node = math.parse('sqrt(2/3)')
176
- node.toTex() // returns '\sqrt{\frac{2}{3}}'
177
- ```
178
-
179
- Information about the options in [Customization](customization.md#custom-html-latex-and-string-output).
180
-
181
- - `transform(callback: function)`
182
-
183
- Recursively transform an expression tree via a transform function. Similar
184
- to `Array.map`, but recursively executed on all nodes in the expression tree.
185
- The callback function is a mapping function accepting a node, and returning
186
- a replacement for the node or the original node. Function `callback` is
187
- called as `callback(node: Node, path: string, parent: Node)` for every node
188
- in the tree, and must return a `Node`. Parameter `path` is a string containing
189
- a relative JSON Path.
190
-
191
- The transform function will stop iterating when a node is replaced by the
192
- callback function, it will not iterate over replaced nodes.
193
-
194
- For example, to replace all nodes of type `SymbolNode` having name 'x' with a
195
- ConstantNode with value `3`:
196
-
197
- ```js
198
- const node = math.parse('x^2 + 5*x')
199
- const transformed = node.transform(function (node, path, parent) {
200
- if (node.isSymbolNode && node.name === 'x') {
201
- return new math.ConstantNode(3)
202
- }
203
- else {
204
- return node
205
- }
206
- })
207
- transformed.toString() // returns '3 ^ 2 + 5 * 3'
208
- ```
209
-
210
- - `traverse(callback)`
211
-
212
- Recursively traverse all nodes in a node tree. Executes given callback for
213
- this node and each of its child nodes. Similar to `Array.forEach`, except
214
- recursive.
215
- The callback function is a mapping function accepting a node, and returning
216
- a replacement for the node or the original node. Function `callback` is
217
- called as `callback(node: Node, path: string, parent: Node)` for every node
218
- in the tree. Parameter `path` is a string containing a relative JSON Path.
219
- Example:
220
-
221
- ```js
222
- const node = math.parse('3 * x + 2')
223
- node.traverse(function (node, path, parent) {
224
- switch (node.type) {
225
- case 'OperatorNode':
226
- console.log(node.type, node.op)
227
- break
228
- case 'ConstantNode':
229
- console.log(node.type, node.value)
230
- break
231
- case 'SymbolNode':
232
- console.log(node.type, node.name)
233
- break
234
- default:
235
- console.log(node.type)
236
- }
237
- })
238
- // outputs:
239
- // OperatorNode +
240
- // OperatorNode *
241
- // ConstantNode 3
242
- // SymbolNode x
243
- // ConstantNode 2
244
- ```
245
-
246
-
247
- ### Properties
248
-
249
- Each `Node` has the following properties:
250
-
251
- - `comment: string`
252
-
253
- A string holding a comment if there was any in the expression, or else the
254
- string will be empty string. A comment can be attached to the root node of
255
- an expression or to each of the childs nodes of a `BlockNode`.
256
-
257
- - `isNode: true`
258
-
259
- Is defined with value `true` on Nodes. Additionally, each type of node
260
- adds it's own flag, for example a `SymbolNode` as has a property
261
- `isSymbolNode: true`.
262
-
263
- - `type: string`
264
-
265
- The type of the node, for example `'SymbolNode'` in case of a `SymbolNode`.
266
-
267
-
268
- ## Nodes
269
-
270
- math.js has the following types of nodes. All nodes are available at the
271
- namespace `math`.
272
-
273
-
274
- ### AccessorNode
275
-
276
- Construction:
277
-
278
- ```
279
- new AccessorNode(object: Node, index: IndexNode)
280
- ```
281
-
282
- Properties:
283
-
284
- - `object: Node`
285
- - `index: IndexNode`
286
- - `name: string` (read-only) The function or method name. Returns an empty string when undefined.
287
-
288
- Examples:
289
-
290
- ```js
291
- const node1 = math.parse('a[3]')
292
-
293
- const object = new math.SymbolNode('a')
294
- const index = new math.IndexNode([3])
295
- const node2 = new math.AccessorNode(object, index)
296
- ```
297
-
298
-
299
- ### ArrayNode
300
-
301
- Construction:
302
-
303
- ```
304
- new ArrayNode(items: Node[])
305
- ```
306
-
307
- Properties:
308
-
309
- - `items: Node[]`
310
-
311
- Examples:
312
-
313
- ```js
314
- const node1 = math.parse('[1, 2, 3]')
315
-
316
- const one = new math.ConstantNode(1)
317
- const two = new math.ConstantNode(2)
318
- const three = new math.ConstantNode(3)
319
- const node2 = new math.ArrayNode([one, two, three])
320
- ```
321
-
322
-
323
- ### AssignmentNode
324
-
325
- Construction:
326
-
327
- ```
328
- new AssignmentNode(object: SymbolNode, value: Node)
329
- new AssignmentNode(object: SymbolNode | AccessorNode, index: IndexNode, value: Node)
330
- ```
331
-
332
- Properties:
333
-
334
- - `object: SymbolNode | AccessorNode`
335
- - `index: IndexNode | null`
336
- - `value: Node`
337
- - `name: string` (read-only) The function or method name. Returns an empty string when undefined.
338
-
339
- Examples:
340
-
341
- ```js
342
- const node1 = math.parse('a = 3')
343
-
344
- const object = new math.SymbolNode('a')
345
- const value = new math.ConstantNode(3)
346
- const node2 = new math.AssignmentNode(object, value)
347
- ```
348
-
349
-
350
- ### BlockNode
351
-
352
- A `BlockNode` is created when parsing a multi line expression like `a=2;b=3` or
353
- `a=2\nb=3`. Evaluating a `BlockNode` returns a `ResultSet`. The results can be
354
- retrieved via `ResultSet.entries` or `ResultSet.valueOf()`, which contains
355
- an `Array` with the results of the visible lines (i.e. lines not ending with
356
- a semicolon).
357
-
358
- Construction:
359
-
360
- ```
361
- block = new BlockNode(Array.<{node: Node} | {node: Node, visible: boolean}>)
362
- ```
363
-
364
- Properties:
365
-
366
- - `blocks: Array.<{node: Node, visible: boolean}>`
367
-
368
- Examples:
369
-
370
- ```js
371
- const block1 = math.parse('a=1; b=2; c=3')
372
-
373
- const a = new math.SymbolNode('a')
374
- const one = new math.ConstantNode(1)
375
- const ass1 = new math.AssignmentNode(a, one)
376
-
377
- const b = new math.SymbolNode('b')
378
- const two = new math.ConstantNode(2)
379
- const ass2 = new math.AssignmentNode(b, two)
380
-
381
- const c = new math.SymbolNode('c')
382
- const three = new math.ConstantNode(3)
383
- const ass3 = new math.AssignmentNode(c, three)
384
-
385
- const block2 = new BlockNode([
386
- {node: ass1, visible: false},
387
- {node: ass2, visible: false},
388
- {node: ass3, visible: true}
389
- ])
390
- ```
391
-
392
-
393
- ### ConditionalNode
394
-
395
- Construction:
396
-
397
- ```
398
- new ConditionalNode(condition: Node, trueExpr: Node, falseExpr: Node)
399
- ```
400
-
401
- Properties:
402
-
403
- - `condition: Node`
404
- - `trueExpr: Node`
405
- - `falseExpr: Node`
406
-
407
- Examples:
408
-
409
- ```js
410
- const node1 = math.parse('a > 0 ? a : -a')
411
-
412
- const a = new math.SymbolNode('a')
413
- const zero = new math.ConstantNode(0)
414
- const condition = new math.OperatorNode('>', 'larger', [a, zero])
415
- const trueExpr = a
416
- const falseExpr = new math.OperatorNode('-', 'unaryMinus', [a])
417
- const node2 = new math.ConditionalNode(condition, trueExpr, falseExpr)
418
- ```
419
-
420
- ### ConstantNode
421
-
422
- Construction:
423
-
424
- ```
425
- new ConstantNode(value: *)
426
- ```
427
-
428
- Properties:
429
-
430
- - `value: *`
431
-
432
- Examples:
433
-
434
- ```js
435
- const node1 = math.parse('2.4')
436
-
437
- const node2 = new math.ConstantNode(2.4)
438
- const node3 = new math.ConstantNode('foo')
439
- ```
440
-
441
-
442
- ### FunctionAssignmentNode
443
-
444
- Construction:
445
-
446
- ```
447
- new FunctionAssignmentNode(name: string, params: string[], expr: Node)
448
- ```
449
-
450
- Properties:
451
-
452
- - `name: string`
453
- - `params: string[]`
454
- - `expr: Node`
455
-
456
- Examples:
457
-
458
- ```js
459
- const node1 = math.parse('f(x) = x^2')
460
-
461
- const x = new math.SymbolNode('x')
462
- const two = new math.ConstantNode(2)
463
- const expr = new math.OperatorNode('^', 'pow', [x, 2])
464
- const node2 = new math.FunctionAssignmentNode('f', ['x'], expr)
465
- ```
466
-
467
-
468
- ### FunctionNode
469
-
470
- Construction:
471
-
472
- ```
473
- new FunctionNode(fn: Node | string, args: Node[])
474
- ```
475
-
476
- Properties:
477
-
478
- - `fn: Node | string` (read-only) The object or function name which to invoke.
479
- - `args: Node[]`
480
-
481
- Static functions:
482
-
483
- - `FunctionNode.onUndefinedFunction(name: string)`. This function is invoked when an undefined function is evaluated. By default, the function throws an exception "Undefined function x". The function can be overwritten to customize this behavior. See also `SymbolNode.onUndefinedSymbol`.
484
-
485
-
486
- Examples:
487
-
488
- ```js
489
- const node1 = math.parse('sqrt(4)')
490
-
491
- const four = new math.ConstantNode(4)
492
- const node3 = new math.FunctionNode(new SymbolNode('sqrt'), [four])
493
- ```
494
-
495
-
496
- ### IndexNode
497
-
498
- Construction:
499
-
500
- ```
501
- new IndexNode(dimensions: Node[])
502
- new IndexNode(dimensions: Node[], dotNotation: boolean)
503
- ```
504
-
505
- Each dimension can be a single value, a range, or a property. The values of
506
- indices are one-based, including range end.
507
-
508
- An optional property `dotNotation` can be provided describing whether this index
509
- was written using dot notation like `a.b`, or using bracket notation
510
- like `a["b"]`. Default value is `false`. This information is used when
511
- stringifying the IndexNode.
512
-
513
- Properties:
514
-
515
- - `dimensions: Node[]`
516
- - `dotNotation: boolean`
517
-
518
- Examples:
519
-
520
- ```js
521
- const node1 = math.parse('A[1:3, 2]')
522
-
523
- const A = new math.SymbolNode('A')
524
- const one = new math.ConstantNode(1)
525
- const two = new math.ConstantNode(2)
526
- const three = new math.ConstantNode(3)
527
-
528
- const range = new math.RangeNode(one, three)
529
- const index = new math.IndexNode([range, two])
530
- const node2 = new math.AccessNode(A, index)
531
- ```
532
-
533
- ### ObjectNode
534
-
535
- Construction:
536
-
537
- ```
538
- new ObjectNode(properties: Object.<string, Node>)
539
- ```
540
-
541
- Properties:
542
-
543
- - `properties: Object.<string, Node>`
544
-
545
- Examples:
546
-
547
- ```js
548
- const node1 = math.parse('{a: 1, b: 2, c: 3}')
549
-
550
- const a = new math.ConstantNode(1)
551
- const b = new math.ConstantNode(2)
552
- const c = new math.ConstantNode(3)
553
- const node2 = new math.ObjectNode({a: a, b: b, c: c})
554
- ```
555
-
556
-
557
- ### OperatorNode
558
-
559
- Construction:
560
-
561
- ```
562
- new OperatorNode(op: string, fn: string, args: Node[], implicit: boolean = false)
563
- ```
564
-
565
- Additional methods:
566
-
567
- - `isUnary() : boolean`
568
-
569
- Returns true when the `OperatorNode` contains exactly one argument,
570
- like with a unary minus:
571
-
572
- ```js
573
- const a = new math.ConstantNode(2)
574
- const b = new math.OperatorNode('-', 'unaryMinus', [a])
575
- b.isUnary() // true
576
- ```
577
-
578
- - `isBinary() : boolean`
579
-
580
- Returns true when the `OperatorNode` contains exactly two arguments,
581
- like with most regular operators:
582
-
583
- ```js
584
- const a = new math.ConstantNode(2)
585
- const b = new math.ConstantNode(3)
586
- const c = new math.OperatorNode('+', 'add', [a, b])
587
- c.isBinary() // true
588
- ```
589
-
590
- Properties:
591
-
592
- - `op: string`
593
- - `fn: string`
594
- - `args: Node[]`
595
- - `implicit: boolean` True in case of an implicit multiplication, false otherwise
596
-
597
- Examples:
598
-
599
- ```js
600
- const node1 = math.parse('2.3 + 5')
601
-
602
- const a = new math.ConstantNode(2.3)
603
- const b = new math.ConstantNode(5)
604
- const node2 = new math.OperatorNode('+', 'add', [a, b])
605
- ```
606
-
607
- ### ParenthesisNode
608
-
609
- Construction:
610
-
611
- ```
612
- new ParenthesisNode(content: Node)
613
- ```
614
-
615
- Properties:
616
-
617
- - `content: Node`
618
-
619
- Examples:
620
-
621
- ```js
622
- const node1 = math.parse('(1)')
623
-
624
- const a = new math.ConstantNode(1)
625
- const node2 = new math.ParenthesisNode(a)
626
- ```
627
-
628
- ### RangeNode
629
-
630
- Construction:
631
-
632
- ```
633
- new RangeNode(start: Node, end: Node [, step: Node])
634
- ```
635
-
636
- Properties:
637
-
638
- - `start: Node`
639
- - `end: Node`
640
- - `step: Node | null`
641
-
642
- Examples:
643
-
644
- ```js
645
- const node1 = math.parse('1:10')
646
- const node2 = math.parse('0:2:10')
647
-
648
- const zero = new math.ConstantNode(0)
649
- const one = new math.ConstantNode(1)
650
- const two = new math.ConstantNode(2)
651
- const ten = new math.ConstantNode(10)
652
-
653
- const node3 = new math.RangeNode(one, ten)
654
- const node4 = new math.RangeNode(zero, ten, two)
655
- ```
656
-
657
- ### RelationalNode
658
-
659
- Construction:
660
-
661
- ```
662
- new RelationalNode(conditionals: string[], params: Node[])
663
- ```
664
-
665
- `conditionals` is an array of strings, each of which may be 'smaller', 'larger', 'smallerEq', 'largerEq', 'equal', or 'unequal'. The `conditionals` array must contain exactly one fewer item than `params`.
666
-
667
- Properties:
668
-
669
- - `conditionals: string[]`
670
- - `params: Node[]`
671
-
672
- A `RelationalNode` efficiently represents a chained conditional expression with two or more comparison operators, such as `10 < x <= 50`. The expression is equivalent to `10 < x and x <= 50`, except that `x` is evaluated only once, and evaluation stops (is "short-circuited") once any condition tests false. Operators that are subject to chaining are `<`, `>`, `<=`, `>=`, `==`, and `!=`. For backward compatibility, `math.parse` will return an `OperatorNode` if only a single conditional is present (such as `x > 2`).
673
-
674
- Examples:
675
-
676
- ```js
677
-
678
- const ten = new math.ConstantNode(10)
679
- const x = new math.SymbolNode('x')
680
- const fifty = new math.ConstantNode(50)
681
-
682
- const node1 = new math.RelationalNode(['smaller', 'smallerEq'], [ten, x, fifty])
683
- const node2 = math.parse('10 < x <= 50')
684
- ```
685
-
686
-
687
- ### SymbolNode
688
-
689
- Construction:
690
-
691
- ```
692
- new SymbolNode(name: string)
693
- ```
694
-
695
- Properties:
696
-
697
- - `name: string`
698
-
699
- Static functions:
700
-
701
- - `SymbolNode.onUndefinedSymbol(name: string)`. This function is invoked when an undefined symbol is evaluated. By default, the function throws an exception "Undefined symbol x". The function can be overwritten to customize this behavior. See also `FunctionNode.onUndefinedFunction`.
702
-
703
-
704
- Examples:
705
-
706
- ```js
707
- const node = math.parse('x')
708
-
709
- const x = new math.SymbolNode('x')
710
- ```