mathjs 10.5.2 → 10.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. package/HISTORY.md +8 -0
  2. package/lib/browser/math.js +3 -3
  3. package/lib/browser/math.js.map +1 -1
  4. package/lib/cjs/header.js +2 -2
  5. package/lib/cjs/version.js +1 -1
  6. package/lib/esm/version.js +1 -1
  7. package/package.json +6 -11
  8. package/docs/command_line_interface.md +0 -87
  9. package/docs/core/chaining.md +0 -41
  10. package/docs/core/configuration.md +0 -144
  11. package/docs/core/extension.md +0 -263
  12. package/docs/core/index.md +0 -21
  13. package/docs/core/serialization.md +0 -50
  14. package/docs/custom_bundling.md +0 -116
  15. package/docs/datatypes/bignumbers.md +0 -102
  16. package/docs/datatypes/complex_numbers.md +0 -168
  17. package/docs/datatypes/fractions.md +0 -75
  18. package/docs/datatypes/index.md +0 -67
  19. package/docs/datatypes/matrices.md +0 -375
  20. package/docs/datatypes/numbers.md +0 -106
  21. package/docs/datatypes/units.md +0 -444
  22. package/docs/expressions/algebra.md +0 -110
  23. package/docs/expressions/customization.md +0 -379
  24. package/docs/expressions/expression_trees.md +0 -710
  25. package/docs/expressions/html_classes.md +0 -38
  26. package/docs/expressions/index.md +0 -21
  27. package/docs/expressions/parsing.md +0 -224
  28. package/docs/expressions/security.md +0 -89
  29. package/docs/expressions/syntax.md +0 -706
  30. package/docs/getting_started.md +0 -124
  31. package/docs/index.md +0 -39
  32. package/docs/reference/classes/densematrix.md +0 -247
  33. package/docs/reference/classes/fibonacciheap.md +0 -70
  34. package/docs/reference/classes/matrixindex.md +0 -133
  35. package/docs/reference/classes/matrixrange.md +0 -158
  36. package/docs/reference/classes/resultset.md +0 -47
  37. package/docs/reference/classes/sparsematrix.md +0 -245
  38. package/docs/reference/classes/unit.md +0 -242
  39. package/docs/reference/classes.md +0 -86
  40. package/docs/reference/constants.md +0 -29
  41. package/docs/reference/functions/abs.md +0 -46
  42. package/docs/reference/functions/acos.md +0 -49
  43. package/docs/reference/functions/acosh.md +0 -47
  44. package/docs/reference/functions/acot.md +0 -48
  45. package/docs/reference/functions/acoth.md +0 -46
  46. package/docs/reference/functions/acsc.md +0 -49
  47. package/docs/reference/functions/acsch.md +0 -46
  48. package/docs/reference/functions/add.md +0 -59
  49. package/docs/reference/functions/and.md +0 -53
  50. package/docs/reference/functions/apply.md +0 -56
  51. package/docs/reference/functions/arg.md +0 -53
  52. package/docs/reference/functions/asec.md +0 -49
  53. package/docs/reference/functions/asech.md +0 -46
  54. package/docs/reference/functions/asin.md +0 -49
  55. package/docs/reference/functions/asinh.md +0 -46
  56. package/docs/reference/functions/atan.md +0 -49
  57. package/docs/reference/functions/atan2.md +0 -56
  58. package/docs/reference/functions/atanh.md +0 -46
  59. package/docs/reference/functions/bellNumbers.md +0 -45
  60. package/docs/reference/functions/bignumber.md +0 -47
  61. package/docs/reference/functions/bin.md +0 -45
  62. package/docs/reference/functions/bitAnd.md +0 -51
  63. package/docs/reference/functions/bitNot.md +0 -51
  64. package/docs/reference/functions/bitOr.md +0 -52
  65. package/docs/reference/functions/bitXor.md +0 -51
  66. package/docs/reference/functions/boolean.md +0 -50
  67. package/docs/reference/functions/catalan.md +0 -45
  68. package/docs/reference/functions/cbrt.md +0 -60
  69. package/docs/reference/functions/ceil.md +0 -63
  70. package/docs/reference/functions/chain.md +0 -54
  71. package/docs/reference/functions/clone.md +0 -43
  72. package/docs/reference/functions/column.md +0 -45
  73. package/docs/reference/functions/combinations.md +0 -49
  74. package/docs/reference/functions/combinationsWithRep.md +0 -49
  75. package/docs/reference/functions/compare.md +0 -67
  76. package/docs/reference/functions/compareNatural.md +0 -92
  77. package/docs/reference/functions/compareText.md +0 -54
  78. package/docs/reference/functions/compile.md +0 -56
  79. package/docs/reference/functions/complex.md +0 -17
  80. package/docs/reference/functions/composition.md +0 -46
  81. package/docs/reference/functions/concat.md +0 -56
  82. package/docs/reference/functions/config.md +0 -44
  83. package/docs/reference/functions/conj.md +0 -50
  84. package/docs/reference/functions/cos.md +0 -51
  85. package/docs/reference/functions/cosh.md +0 -46
  86. package/docs/reference/functions/cot.md +0 -47
  87. package/docs/reference/functions/coth.md +0 -49
  88. package/docs/reference/functions/count.md +0 -45
  89. package/docs/reference/functions/createUnit.md +0 -52
  90. package/docs/reference/functions/cross.md +0 -58
  91. package/docs/reference/functions/csc.md +0 -47
  92. package/docs/reference/functions/csch.md +0 -49
  93. package/docs/reference/functions/ctranspose.md +0 -50
  94. package/docs/reference/functions/cube.md +0 -51
  95. package/docs/reference/functions/cumsum.md +0 -57
  96. package/docs/reference/functions/deepEqual.md +0 -53
  97. package/docs/reference/functions/derivative.md +0 -61
  98. package/docs/reference/functions/det.md +0 -49
  99. package/docs/reference/functions/diag.md +0 -61
  100. package/docs/reference/functions/diff.md +0 -70
  101. package/docs/reference/functions/distance.md +0 -80
  102. package/docs/reference/functions/divide.md +0 -55
  103. package/docs/reference/functions/dot.md +0 -48
  104. package/docs/reference/functions/dotDivide.md +0 -52
  105. package/docs/reference/functions/dotMultiply.md +0 -52
  106. package/docs/reference/functions/dotPow.md +0 -49
  107. package/docs/reference/functions/eigs.md +0 -54
  108. package/docs/reference/functions/equal.md +0 -75
  109. package/docs/reference/functions/equalText.md +0 -53
  110. package/docs/reference/functions/erf.md +0 -49
  111. package/docs/reference/functions/evaluate.md +0 -56
  112. package/docs/reference/functions/exp.md +0 -54
  113. package/docs/reference/functions/expm.md +0 -49
  114. package/docs/reference/functions/expm1.md +0 -54
  115. package/docs/reference/functions/factorial.md +0 -49
  116. package/docs/reference/functions/fft.md +0 -35
  117. package/docs/reference/functions/filter.md +0 -50
  118. package/docs/reference/functions/fix.md +0 -62
  119. package/docs/reference/functions/flatten.md +0 -46
  120. package/docs/reference/functions/floor.md +0 -66
  121. package/docs/reference/functions/forEach.md +0 -41
  122. package/docs/reference/functions/format.md +0 -127
  123. package/docs/reference/functions/fraction.md +0 -45
  124. package/docs/reference/functions/gamma.md +0 -49
  125. package/docs/reference/functions/gcd.md +0 -50
  126. package/docs/reference/functions/getMatrixDataType.md +0 -59
  127. package/docs/reference/functions/hasNumericValue.md +0 -55
  128. package/docs/reference/functions/help.md +0 -42
  129. package/docs/reference/functions/hex.md +0 -45
  130. package/docs/reference/functions/hypot.md +0 -51
  131. package/docs/reference/functions/identity.md +0 -57
  132. package/docs/reference/functions/ifft.md +0 -35
  133. package/docs/reference/functions/im.md +0 -53
  134. package/docs/reference/functions/import.md +0 -68
  135. package/docs/reference/functions/index.md +0 -55
  136. package/docs/reference/functions/intersect.md +0 -50
  137. package/docs/reference/functions/inv.md +0 -45
  138. package/docs/reference/functions/invmod.md +0 -47
  139. package/docs/reference/functions/isInteger.md +0 -55
  140. package/docs/reference/functions/isNaN.md +0 -56
  141. package/docs/reference/functions/isNegative.md +0 -55
  142. package/docs/reference/functions/isNumeric.md +0 -55
  143. package/docs/reference/functions/isPositive.md +0 -57
  144. package/docs/reference/functions/isPrime.md +0 -54
  145. package/docs/reference/functions/isZero.md +0 -59
  146. package/docs/reference/functions/kldivergence.md +0 -41
  147. package/docs/reference/functions/kron.md +0 -53
  148. package/docs/reference/functions/larger.md +0 -60
  149. package/docs/reference/functions/largerEq.md +0 -56
  150. package/docs/reference/functions/lcm.md +0 -54
  151. package/docs/reference/functions/leafCount.md +0 -52
  152. package/docs/reference/functions/leftShift.md +0 -52
  153. package/docs/reference/functions/lgamma.md +0 -46
  154. package/docs/reference/functions/log.md +0 -57
  155. package/docs/reference/functions/log10.md +0 -50
  156. package/docs/reference/functions/log1p.md +0 -54
  157. package/docs/reference/functions/log2.md +0 -50
  158. package/docs/reference/functions/lsolve.md +0 -51
  159. package/docs/reference/functions/lsolveAll.md +0 -51
  160. package/docs/reference/functions/lup.md +0 -52
  161. package/docs/reference/functions/lusolve.md +0 -59
  162. package/docs/reference/functions/mad.md +0 -50
  163. package/docs/reference/functions/map.md +0 -65
  164. package/docs/reference/functions/matrix.md +0 -56
  165. package/docs/reference/functions/matrixFromColumns.md +0 -49
  166. package/docs/reference/functions/matrixFromFunction.md +0 -54
  167. package/docs/reference/functions/matrixFromRows.md +0 -49
  168. package/docs/reference/functions/max.md +0 -61
  169. package/docs/reference/functions/mean.md +0 -57
  170. package/docs/reference/functions/median.md +0 -57
  171. package/docs/reference/functions/min.md +0 -61
  172. package/docs/reference/functions/mod.md +0 -59
  173. package/docs/reference/functions/mode.md +0 -50
  174. package/docs/reference/functions/multinomial.md +0 -46
  175. package/docs/reference/functions/multiply.md +0 -60
  176. package/docs/reference/functions/norm.md +0 -59
  177. package/docs/reference/functions/not.md +0 -50
  178. package/docs/reference/functions/nthRoot.md +0 -53
  179. package/docs/reference/functions/nthRoots.md +0 -58
  180. package/docs/reference/functions/number.md +0 -49
  181. package/docs/reference/functions/numeric.md +0 -52
  182. package/docs/reference/functions/oct.md +0 -45
  183. package/docs/reference/functions/ones.md +0 -59
  184. package/docs/reference/functions/or.md +0 -53
  185. package/docs/reference/functions/parse.md +0 -56
  186. package/docs/reference/functions/parser.md +0 -70
  187. package/docs/reference/functions/partitionSelect.md +0 -53
  188. package/docs/reference/functions/permutations.md +0 -51
  189. package/docs/reference/functions/pickRandom.md +0 -59
  190. package/docs/reference/functions/pinv.md +0 -44
  191. package/docs/reference/functions/pow.md +0 -65
  192. package/docs/reference/functions/print.md +0 -64
  193. package/docs/reference/functions/prod.md +0 -55
  194. package/docs/reference/functions/qr.md +0 -65
  195. package/docs/reference/functions/quantileSeq.md +0 -62
  196. package/docs/reference/functions/random.md +0 -54
  197. package/docs/reference/functions/randomInt.md +0 -53
  198. package/docs/reference/functions/range.md +0 -70
  199. package/docs/reference/functions/rationalize.md +0 -68
  200. package/docs/reference/functions/re.md +0 -53
  201. package/docs/reference/functions/reshape.md +0 -60
  202. package/docs/reference/functions/resize.md +0 -51
  203. package/docs/reference/functions/resolve.md +0 -46
  204. package/docs/reference/functions/rightArithShift.md +0 -52
  205. package/docs/reference/functions/rightLogShift.md +0 -52
  206. package/docs/reference/functions/rotate.md +0 -53
  207. package/docs/reference/functions/rotationMatrix.md +0 -57
  208. package/docs/reference/functions/round.md +0 -61
  209. package/docs/reference/functions/row.md +0 -45
  210. package/docs/reference/functions/sec.md +0 -47
  211. package/docs/reference/functions/sech.md +0 -49
  212. package/docs/reference/functions/setCartesian.md +0 -49
  213. package/docs/reference/functions/setDifference.md +0 -47
  214. package/docs/reference/functions/setDistinct.md +0 -43
  215. package/docs/reference/functions/setIntersect.md +0 -46
  216. package/docs/reference/functions/setIsSubset.md +0 -47
  217. package/docs/reference/functions/setMultiplicity.md +0 -46
  218. package/docs/reference/functions/setPowerset.md +0 -43
  219. package/docs/reference/functions/setSize.md +0 -47
  220. package/docs/reference/functions/setSymDifference.md +0 -47
  221. package/docs/reference/functions/setUnion.md +0 -46
  222. package/docs/reference/functions/sign.md +0 -52
  223. package/docs/reference/functions/simplify.md +0 -130
  224. package/docs/reference/functions/simplifyConstant.md +0 -52
  225. package/docs/reference/functions/simplifyCore.md +0 -50
  226. package/docs/reference/functions/sin.md +0 -51
  227. package/docs/reference/functions/sinh.md +0 -46
  228. package/docs/reference/functions/size.md +0 -50
  229. package/docs/reference/functions/slu.md +0 -57
  230. package/docs/reference/functions/smaller.md +0 -60
  231. package/docs/reference/functions/smallerEq.md +0 -56
  232. package/docs/reference/functions/sort.md +0 -56
  233. package/docs/reference/functions/sparse.md +0 -51
  234. package/docs/reference/functions/splitUnit.md +0 -37
  235. package/docs/reference/functions/sqrt.md +0 -50
  236. package/docs/reference/functions/sqrtm.md +0 -46
  237. package/docs/reference/functions/square.md +0 -51
  238. package/docs/reference/functions/squeeze.md +0 -53
  239. package/docs/reference/functions/std.md +0 -76
  240. package/docs/reference/functions/stirlingS2.md +0 -52
  241. package/docs/reference/functions/string.md +0 -49
  242. package/docs/reference/functions/subset.md +0 -65
  243. package/docs/reference/functions/subtract.md +0 -54
  244. package/docs/reference/functions/sum.md +0 -54
  245. package/docs/reference/functions/symbolicEqual.md +0 -62
  246. package/docs/reference/functions/tan.md +0 -49
  247. package/docs/reference/functions/tanh.md +0 -50
  248. package/docs/reference/functions/to.md +0 -47
  249. package/docs/reference/functions/trace.md +0 -50
  250. package/docs/reference/functions/transpose.md +0 -49
  251. package/docs/reference/functions/typeOf.md +0 -81
  252. package/docs/reference/functions/typed.md +0 -56
  253. package/docs/reference/functions/unaryMinus.md +0 -49
  254. package/docs/reference/functions/unaryPlus.md +0 -48
  255. package/docs/reference/functions/unequal.md +0 -74
  256. package/docs/reference/functions/unit.md +0 -48
  257. package/docs/reference/functions/usolve.md +0 -51
  258. package/docs/reference/functions/usolveAll.md +0 -51
  259. package/docs/reference/functions/variance.md +0 -78
  260. package/docs/reference/functions/xgcd.md +0 -47
  261. package/docs/reference/functions/xor.md +0 -53
  262. package/docs/reference/functions/zeros.md +0 -57
  263. package/docs/reference/functions.md +0 -297
  264. package/docs/reference/index.md +0 -5
  265. package/examples/advanced/convert_fraction_to_bignumber.js +0 -59
  266. package/examples/advanced/custom_argument_parsing.js +0 -98
  267. package/examples/advanced/custom_datatype.js +0 -56
  268. package/examples/advanced/custom_evaluate_using_factories.js +0 -19
  269. package/examples/advanced/custom_evaluate_using_import.js +0 -18
  270. package/examples/advanced/custom_loading.js +0 -33
  271. package/examples/advanced/custom_relational_functions.js +0 -62
  272. package/examples/advanced/custom_scope_objects.js +0 -115
  273. package/examples/advanced/expression_trees.js +0 -55
  274. package/examples/advanced/function_transform.js +0 -50
  275. package/examples/advanced/more_secure_eval.js +0 -36
  276. package/examples/advanced/use_bigint.js +0 -43
  277. package/examples/advanced/web_server/math_worker.js +0 -24
  278. package/examples/advanced/web_server/server.js +0 -80
  279. package/examples/algebra.js +0 -34
  280. package/examples/basic_usage.js +0 -49
  281. package/examples/bignumbers.js +0 -52
  282. package/examples/browser/angle_configuration.html +0 -134
  283. package/examples/browser/basic_usage.html +0 -39
  284. package/examples/browser/currency_conversion.html +0 -125
  285. package/examples/browser/custom_separators.html +0 -81
  286. package/examples/browser/plot.html +0 -78
  287. package/examples/browser/pretty_printing_with_mathjax.html +0 -122
  288. package/examples/browser/printing_html.html +0 -170
  289. package/examples/browser/requirejs_loading.html +0 -20
  290. package/examples/browser/rocket_trajectory_optimization.html +0 -301
  291. package/examples/browser/webworkers/webworkers.html +0 -80
  292. package/examples/browser/webworkers/worker.js +0 -28
  293. package/examples/chaining.js +0 -56
  294. package/examples/complex_numbers.js +0 -67
  295. package/examples/expressions.js +0 -188
  296. package/examples/fractions.js +0 -74
  297. package/examples/import.js +0 -95
  298. package/examples/matrices.js +0 -101
  299. package/examples/objects.js +0 -35
  300. package/examples/package.json +0 -3
  301. package/examples/serialization.js +0 -16
  302. package/examples/sparse_matrices.js +0 -19
  303. package/examples/units.js +0 -108
@@ -1,67 +0,0 @@
1
- // complex numbers
2
-
3
- // load js (using node.js)
4
- const { complex, add, multiply, sin, sqrt, pi, equal, sort, format } = require('..')
5
-
6
- // create a complex number with a numeric real and complex part
7
- console.log('create and manipulate complex numbers')
8
- const a = complex(2, 3)
9
- print(a) // 2 + 3i
10
-
11
- // read the real and complex parts of the complex number
12
- print(a.re) // 2
13
- print(a.im) // 3
14
-
15
- // clone a complex value
16
- const clone = a.clone()
17
- print(clone) // 2 + 3i
18
-
19
- // adjust the complex value
20
- a.re = 5
21
- print(a) // 5 + 3i
22
-
23
- // create a complex number by providing a string with real and complex parts
24
- const b = complex('3-7i')
25
- print(b) // 3 - 7i
26
- console.log()
27
-
28
- // perform operations with complex numbers
29
- console.log('perform operations')
30
- print(add(a, b)) // 8 - 4i
31
- print(multiply(a, b)) // 36 - 26i
32
- print(sin(a)) // -9.6541254768548 + 2.8416922956064i
33
-
34
- // some operations will return a complex number depending on the arguments
35
- print(sqrt(4)) // 2
36
- print(sqrt(-4)) // 2i
37
- console.log()
38
-
39
- // create a complex number from polar coordinates
40
- console.log('create complex numbers with polar coordinates')
41
- const c = complex({ r: sqrt(2), phi: pi / 4 })
42
- print(c) // 1 + i
43
-
44
- // get polar coordinates of a complex number
45
- const d = complex(3, 4)
46
- console.log(d.abs(), d.arg()) // radius = 5, phi = 0.9272952180016122
47
- console.log()
48
-
49
- // comparision operations
50
- // note that there is no mathematical ordering defined for complex numbers
51
- // we can only check equality. To sort a list with complex numbers,
52
- // the natural sorting can be used
53
- console.log('\ncomparision and sorting operations')
54
- console.log('equal', equal(a, b)) // returns false
55
- const values = [a, b, c]
56
- console.log('values:', format(values, 14)) // [5 + 3i, 3 - 7i, 1 + i]
57
- sort(values, 'natural')
58
- console.log('sorted:', format(values, 14)) // [1 + i, 3 - 7i, 5 + 3i]
59
-
60
- /**
61
- * Helper function to output a value in the console. Value will be formatted.
62
- * @param {*} value
63
- */
64
- function print (value) {
65
- const precision = 14
66
- console.log(format(value, precision))
67
- }
@@ -1,188 +0,0 @@
1
- /**
2
- * Expressions can be evaluated in various ways:
3
- *
4
- * 1. using the function math.evaluate
5
- * 2. using the function math.parse
6
- * 3. using a parser. A parser contains functions evaluate and parse,
7
- * and keeps a scope with assigned variables in memory
8
- */
9
-
10
- // load math.js (using node.js)
11
- const math = require('..')
12
-
13
- // 1. using the function math.evaluate
14
- //
15
- // Function `evaluate` accepts a single expression or an array with
16
- // expressions as first argument, and has an optional second argument
17
- // containing a scope with variables and functions. The scope is a regular
18
- // JavaScript Object. The scope will be used to resolve symbols, and to write
19
- // assigned variables or function.
20
- console.log('1. USING FUNCTION MATH.EVAL')
21
-
22
- // evaluate expressions
23
- console.log('\nevaluate expressions')
24
- print(math.evaluate('sqrt(3^2 + 4^2)')) // 5
25
- print(math.evaluate('sqrt(-4)')) // 2i
26
- print(math.evaluate('2 inch to cm')) // 5.08 cm
27
- print(math.evaluate('cos(45 deg)')) // 0.70711
28
-
29
- // evaluate multiple expressions at once
30
- console.log('\nevaluate multiple expressions at once')
31
- print(math.evaluate([
32
- 'f = 3',
33
- 'g = 4',
34
- 'f * g'
35
- ])) // [3, 4, 12]
36
-
37
- // provide a scope (just a regular JavaScript Object)
38
- console.log('\nevaluate expressions providing a scope with variables and functions')
39
- const scope = {
40
- a: 3,
41
- b: 4
42
- }
43
-
44
- // variables can be read from the scope
45
- print(math.evaluate('a * b', scope)) // 12
46
-
47
- // variable assignments are written to the scope
48
- print(math.evaluate('c = 2.3 + 4.5', scope)) // 6.8
49
- print(scope.c) // 6.8
50
-
51
- // scope can contain both variables and functions
52
- scope.hello = function (name) {
53
- return 'hello, ' + name + '!'
54
- }
55
- print(math.evaluate('hello("hero")', scope)) // "hello, hero!"
56
-
57
- // define a function as an expression
58
- const f = math.evaluate('f(x) = x ^ a', scope)
59
- print(f(2)) // 8
60
- print(scope.f(2)) // 8
61
-
62
- // 2. using function math.parse
63
- //
64
- // Function `math.parse` parses expressions into a node tree. The syntax is
65
- // similar to function `math.evaluate`.
66
- // Function `parse` accepts a single expression or an array with
67
- // expressions as first argument. The function returns a node tree, which
68
- // then can be compiled against math, and then evaluated against an (optional
69
- // scope. This scope is a regular JavaScript Object. The scope will be used
70
- // to resolve symbols, and to write assigned variables or function.
71
- console.log('\n2. USING FUNCTION MATH.PARSE')
72
-
73
- // parse an expression
74
- console.log('\nparse an expression into a node tree')
75
- const node1 = math.parse('sqrt(3^2 + 4^2)')
76
- print(node1.toString()) // "sqrt((3 ^ 2) + (4 ^ 2))"
77
-
78
- // compile and evaluate the compiled code
79
- // you could also do this in two steps: node1.compile().evaluate()
80
- print(node1.evaluate()) // 5
81
-
82
- // provide a scope
83
- console.log('\nprovide a scope')
84
- const node2 = math.parse('x^a')
85
- const code2 = node2.compile()
86
- print(node2.toString()) // "x ^ a"
87
- const scope2 = {
88
- x: 3,
89
- a: 2
90
- }
91
- print(code2.evaluate(scope2)) // 9
92
-
93
- // change a value in the scope and re-evaluate the node
94
- scope2.a = 3
95
- print(code2.evaluate(scope2)) // 27
96
-
97
- // 3. using function math.compile
98
- //
99
- // Function `math.compile` compiles expressions into a node tree. The syntax is
100
- // similar to function `math.evaluate`.
101
- // Function `compile` accepts a single expression or an array with
102
- // expressions as first argument, and returns an object with a function evaluate
103
- // to evaluate the compiled expression. On evaluation, an optional scope can
104
- // be provided. This scope will be used to resolve symbols, and to write
105
- // assigned variables or function.
106
- console.log('\n3. USING FUNCTION MATH.COMPILE')
107
-
108
- // parse an expression
109
- console.log('\ncompile an expression')
110
- const code3 = math.compile('sqrt(3^2 + 4^2)')
111
-
112
- // evaluate the compiled code
113
- print(code3.evaluate()) // 5
114
-
115
- // provide a scope for the variable assignment
116
- console.log('\nprovide a scope')
117
- const code4 = math.compile('a = a + 3')
118
- const scope3 = {
119
- a: 7
120
- }
121
- code4.evaluate(scope3)
122
- print(scope3.a) // 10
123
-
124
- // 4. using a parser
125
- //
126
- // In addition to the static functions `math.evaluate` and `math.parse`, math.js
127
- // contains a parser with functions `evaluate` and `parse`, which automatically
128
- // keeps a scope with assigned variables in memory. The parser also contains
129
- // some convenience methods to get, set, and remove variables from memory.
130
- console.log('\n4. USING A PARSER')
131
- const parser = math.parser()
132
-
133
- // evaluate with parser
134
- console.log('\nevaluate expressions')
135
- print(parser.evaluate('sqrt(3^2 + 4^2)')) // 5
136
- print(parser.evaluate('sqrt(-4)')) // 2i
137
- print(parser.evaluate('2 inch to cm')) // 5.08 cm
138
- print(parser.evaluate('cos(45 deg)')) // 0.70710678118655
139
-
140
- // define variables and functions
141
- console.log('\ndefine variables and functions')
142
- print(parser.evaluate('x = 7 / 2')) // 3.5
143
- print(parser.evaluate('x + 3')) // 6.5
144
- print(parser.evaluate('f2(x, y) = x^y')) // f2(x, y)
145
- print(parser.evaluate('f2(2, 3)')) // 8
146
-
147
- // manipulate matrices
148
- // Note that matrix indexes in the expression parser are one-based with the
149
- // upper-bound included. On a JavaScript level however, math.js uses zero-based
150
- // indexes with an excluded upper-bound.
151
- console.log('\nmanipulate matrices')
152
- print(parser.evaluate('k = [1, 2; 3, 4]')) // [[1, 2], [3, 4]]
153
- print(parser.evaluate('l = zeros(2, 2)')) // [[0, 0], [0, 0]]
154
- print(parser.evaluate('l[1, 1:2] = [5, 6]')) // [5, 6]
155
- print(parser.evaluate('l')) // [[5, 6], [0, 0]]
156
- print(parser.evaluate('l[2, :] = [7, 8]')) // [7, 8]
157
- print(parser.evaluate('l')) // [[5, 6], [7, 8]]
158
- print(parser.evaluate('m = k * l')) // [[19, 22], [43, 50]]
159
- print(parser.evaluate('n = m[2, 1]')) // 43
160
- print(parser.evaluate('n = m[:, 1]')) // [[19], [43]]
161
-
162
- // get and set variables and functions
163
- console.log('\nget and set variables and function in the scope of the parser')
164
- const x = parser.get('x')
165
- console.log('x =', x) // x = 3.5
166
- const f2 = parser.get('f2')
167
- console.log('f2 =', math.format(f2)) // f2 = f2(x, y)
168
- const h = f2(3, 3)
169
- console.log('h =', h) // h = 27
170
-
171
- parser.set('i', 500)
172
- print(parser.evaluate('i / 2')) // 250
173
- parser.set('hello', function (name) {
174
- return 'hello, ' + name + '!'
175
- })
176
- print(parser.evaluate('hello("hero")')) // "hello, hero!"
177
-
178
- // clear defined functions and variables
179
- parser.clear()
180
-
181
- /**
182
- * Helper function to output a value in the console. Value will be formatted.
183
- * @param {*} value
184
- */
185
- function print (value) {
186
- const precision = 14
187
- console.log(math.format(value, precision))
188
- }
@@ -1,74 +0,0 @@
1
- // Fractions
2
-
3
- // load math.js (using node.js)
4
- const { create, all } = require('..')
5
-
6
- // configure the default type of numbers as Fractions
7
- const config = {
8
- // Default type of number
9
- // Available options: 'number' (default), 'BigNumber', or 'Fraction'
10
- number: 'Fraction'
11
- }
12
-
13
- // create a mathjs instance with everything included
14
- const math = create(all, config)
15
-
16
- console.log('basic usage')
17
- printRatio(math.fraction(0.125)) // Fraction, 1/8
18
- printRatio(math.fraction(0.32)) // Fraction, 8/25
19
- printRatio(math.fraction('1/3')) // Fraction, 1/3
20
- printRatio(math.fraction('0.(3)')) // Fraction, 1/3
21
- printRatio(math.fraction(2, 3)) // Fraction, 2/3
22
- printRatio(math.fraction('0.(285714)')) // Fraction, 2/7
23
- console.log()
24
-
25
- console.log('round-off errors with numbers')
26
- print(math.add(0.1, 0.2)) // number, 0.30000000000000004
27
- print(math.divide(0.3, 0.2)) // number, 1.4999999999999998
28
- console.log()
29
-
30
- console.log('no round-off errors with fractions :)')
31
- print(math.add(math.fraction(0.1), math.fraction(0.2))) // Fraction, 0.3
32
- print(math.divide(math.fraction(0.3), math.fraction(0.2))) // Fraction, 1.5
33
- console.log()
34
-
35
- console.log('represent an infinite number of repeating digits')
36
- print(math.fraction('1/3')) // Fraction, 0.(3)
37
- print(math.fraction('2/7')) // Fraction, 0.(285714)
38
- print(math.fraction('23/11')) // Fraction, 2.(09)
39
- console.log()
40
-
41
- // one can work conveniently with fractions using the expression parser.
42
- // note though that Fractions are only supported by basic arithmetic functions
43
- console.log('use fractions in the expression parser')
44
- printRatio(math.evaluate('0.1 + 0.2')) // Fraction, 3/10
45
- printRatio(math.evaluate('0.3 / 0.2')) // Fraction, 3/2
46
- printRatio(math.evaluate('23 / 11')) // Fraction, 23/11
47
- console.log()
48
-
49
- // output formatting
50
- console.log('output formatting of fractions')
51
- const a = math.fraction('2/3')
52
- console.log(math.format(a)) // Fraction, 2/3
53
- console.log(math.format(a, { fraction: 'ratio' })) // Fraction, 2/3
54
- console.log(math.format(a, { fraction: 'decimal' })) // Fraction, 0.(6)
55
- console.log(a.toString()) // Fraction, 0.(6)
56
- console.log()
57
-
58
- /**
59
- * Helper function to output a value in the console.
60
- * Fractions will be formatted as ratio, like '1/3'.
61
- * @param {*} value
62
- */
63
- function printRatio (value) {
64
- console.log(math.format(value, { fraction: 'ratio' }))
65
- }
66
-
67
- /**
68
- * Helper function to output a value in the console.
69
- * Fractions will be formatted as decimal, like '0.(3)'.
70
- * @param {*} value
71
- */
72
- function print (value) {
73
- console.log(math.format(value, { fraction: 'decimal' }))
74
- }
@@ -1,95 +0,0 @@
1
- /**
2
- * Math.js can easily be extended with functions and variables using the
3
- * `import` function. The function `import` accepts a module name or an object
4
- * containing functions and variables.
5
- */
6
-
7
- // load math.js (using node.js)
8
- const { create, all } = require('..')
9
- const math = create(all)
10
-
11
- /**
12
- * Define new functions and variables
13
- */
14
- math.import({
15
- myConstant: 42,
16
- hello: function (name) {
17
- return 'hello, ' + name + '!'
18
- }
19
- })
20
-
21
- // defined methods can be used in both JavaScript as well as the parser
22
- print(math.myConstant * 2) // 84
23
- print(math.hello('user')) // 'hello, user!'
24
-
25
- print(math.evaluate('myConstant + 10')) // 52
26
- print(math.evaluate('hello("user")')) // 'hello, user!'
27
-
28
- /**
29
- * Import the math library numbers.js, https://github.com/sjkaliski/numbers.js
30
- * The library must be installed first using npm:
31
- * npm install numbers
32
- */
33
- try {
34
- // load the numbers.js library
35
- const numbers = require('numbers')
36
-
37
- // import the numbers.js library into math.js
38
- math.import(numbers, { wrap: true, silent: true })
39
-
40
- if (math.fibonacci) {
41
- // calculate fibonacci
42
- print(math.fibonacci(7)) // 13
43
- print(math.evaluate('fibonacci(7)')) // 13
44
- }
45
- } catch (err) {
46
- console.log('Warning: To use numbers.js, the library must ' +
47
- 'be installed first via `npm install numbers`.')
48
- }
49
-
50
- /**
51
- * Import the math library numeric.js, https://github.com/sloisel/numeric
52
- * The library must be installed first using npm:
53
- * npm install numeric
54
- */
55
- try {
56
- // load the numeric.js library
57
- const numeric = require('numeric')
58
-
59
- // import the numeric.js library into math.js
60
- math.import(numeric, { wrap: true, silent: true })
61
-
62
- if (math.eig) {
63
- // calculate eigenvalues of a matrix
64
- print(math.evaluate('eig([1, 2; 4, 3])').lambda.x) // [5, -1]
65
-
66
- // solve AX = b
67
- const A = math.evaluate('[1, 2, 3; 2, -1, 1; 3, 0, -1]')
68
- const b = [9, 8, 3]
69
- print(math.solve(A, b)) // [2, -1, 3]
70
- }
71
- } catch (err) {
72
- console.log('Warning: To use numeric.js, the library must ' +
73
- 'be installed first via `npm install numeric`.')
74
- }
75
-
76
- /**
77
- * By default, the function import does not allow overriding existing functions.
78
- * Existing functions can be overridden by specifying option `override: true`
79
- */
80
- math.import({
81
- pi: 3.14
82
- }, {
83
- override: true
84
- })
85
-
86
- print(math.pi) // returns 3.14 instead of 3.141592653589793
87
-
88
- /**
89
- * Helper function to output a value in the console. Value will be formatted.
90
- * @param {*} value
91
- */
92
- function print (value) {
93
- const precision = 14
94
- console.log(math.format(value, precision))
95
- }
@@ -1,101 +0,0 @@
1
- // matrices
2
-
3
- // load math.js (using node.js)
4
- const math = require('..')
5
-
6
- // create matrices and arrays. a matrix is just a wrapper around an Array,
7
- // providing some handy utilities.
8
- console.log('create a matrix')
9
- const a = math.matrix([1, 4, 9, 16, 25])
10
- print(a) // [1, 4, 9, 16, 25]
11
- const b = math.matrix(math.ones([2, 3]))
12
- print(b) // [[1, 1, 1], [1, 1, 1]]
13
- print(b.size()) // [2, 3]
14
-
15
- // the Array data of a Matrix can be retrieved using valueOf()
16
- const array = a.valueOf()
17
- print(array) // [1, 4, 9, 16, 25]
18
-
19
- // Matrices can be cloned
20
- const clone = a.clone()
21
- print(clone) // [1, 4, 9, 16, 25]
22
- console.log()
23
-
24
- // perform operations with matrices
25
- console.log('perform operations')
26
- print(math.sqrt(a)) // [1, 2, 3, 4, 5]
27
- const c = [1, 2, 3, 4, 5]
28
- print(math.factorial(c)) // [1, 2, 6, 24, 120]
29
- console.log()
30
-
31
- // create and manipulate matrices. Arrays and Matrices can be used mixed.
32
- console.log('manipulate matrices')
33
- const d = [[1, 2], [3, 4]]
34
- print(d) // [[1, 2], [3, 4]]
35
- const e = math.matrix([[5, 6], [1, 1]])
36
- print(e) // [[5, 6], [1, 1]]
37
-
38
- // set a submatrix.
39
- // Matrix indexes are zero-based.
40
- e.subset(math.index(1, [0, 1]), [[7, 8]])
41
- print(e) // [[5, 6], [7, 8]]
42
- const f = math.multiply(d, e)
43
- print(f) // [[19, 22], [43, 50]]
44
- const g = f.subset(math.index(1, 0))
45
- print(g) // 43
46
- console.log()
47
-
48
- // get a sub matrix
49
- // Matrix indexes are zero-based.
50
- console.log('get a sub matrix')
51
- const h = math.diag(math.range(1, 4))
52
- print(h) // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
53
- print(h.subset(math.index([1, 2], [1, 2]))) // [[2, 0], [0, 3]]
54
- const i = math.range(1, 6)
55
- print(i) // [1, 2, 3, 4, 5]
56
- print(i.subset(math.index(math.range(1, 4)))) // [2, 3, 4]
57
- console.log()
58
-
59
- // resize a multi dimensional matrix
60
- console.log('resizing a matrix')
61
- const j = math.matrix()
62
- let defaultValue = 0
63
- j.resize([2, 2, 2], defaultValue)
64
- print(j) // [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
65
- print(j.size()) // [2, 2, 2]
66
- j.resize([2, 2])
67
- print(j) // [[0, 0], [0, 0]]
68
- print(j.size()) // [2, 2]
69
- console.log()
70
-
71
- // setting a value outside the matrices range will resize the matrix.
72
- // new elements will be initialized with zero.
73
- console.log('set a value outside a matrices range')
74
- const k = math.matrix()
75
- k.subset(math.index(2), 6)
76
- print(k) // [0, 0, 6]
77
- console.log()
78
-
79
- console.log('set a value outside a matrices range, setting other new entries to null')
80
- const m = math.matrix()
81
- defaultValue = null
82
- m.subset(math.index(2), 6, defaultValue)
83
- print(m) // [null, null, 6]
84
- console.log()
85
-
86
- // create ranges
87
- console.log('create ranges')
88
- print(math.range(1, 6)) // [1, 2, 3, 4, 5]
89
- print(math.range(0, 18, 3)) // [0, 3, 6, 9, 12, 15]
90
- print(math.range('2:-1:-3')) // [2, 1, 0, -1, -2]
91
- print(math.factorial(math.range('1:6'))) // [1, 2, 6, 24, 120]
92
- console.log()
93
-
94
- /**
95
- * Helper function to output a value in the console. Value will be formatted.
96
- * @param {*} value
97
- */
98
- function print (value) {
99
- const precision = 14
100
- console.log(math.format(value, precision))
101
- }
@@ -1,35 +0,0 @@
1
- // objects
2
-
3
- // load math.js (using node.js)
4
- const { evaluate, format } = require('..')
5
-
6
- // create an object. Keys can be symbols or strings
7
- print(evaluate('{x: 2 + 1, y: 4}')) // {"x": 3, "y": 4}
8
- print(evaluate('{"name": "John"}')) // {"name": "John"}
9
-
10
- // create an object containing an object
11
- print(evaluate('{a: 2, b: {c: 3, d: 4}}')) // {"a": 2, "b": {"c": 3, "d": 4}}
12
-
13
- const scope = {
14
- obj: {
15
- prop: 42
16
- }
17
- }
18
-
19
- // retrieve properties using dot notation or bracket notation
20
- print(evaluate('obj.prop', scope)) // 42
21
- print(evaluate('obj["prop"]', scope)) // 42
22
-
23
- // set properties (returns the whole object, not the property value!)
24
- print(evaluate('obj.prop = 43', scope)) // 43
25
- print(evaluate('obj["prop"] = 43', scope)) // 43
26
- print(scope.obj) // {"prop": 43}
27
-
28
- /**
29
- * Helper function to output a value in the console. Value will be formatted.
30
- * @param {*} value
31
- */
32
- function print (value) {
33
- const precision = 14
34
- console.log(format(value, precision))
35
- }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,16 +0,0 @@
1
- // load math.js (using node.js)
2
- const { complex, replacer, reviver, typeOf } = require('..')
3
-
4
- // serialize a math.js data type into a JSON string
5
- // the replacer function is needed to correctly stringify a value like Infinity
6
- const x = complex('2+3i')
7
- const str1 = JSON.stringify(x, replacer)
8
- console.log(str1)
9
- // outputs {"mathjs":"Complex","re":2,"im":3}
10
-
11
- // deserialize a JSON string into a math.js data type
12
- // note that the reviver of math.js is needed for this:
13
- const str2 = '{"mathjs":"Unit","value":5,"unit":"cm"}'
14
- const y = JSON.parse(str2, reviver)
15
- console.log(typeOf(y)) // 'Unit'
16
- console.log(y.toString()) // 5 cm
@@ -1,19 +0,0 @@
1
- // load math.js (using node.js)
2
- const { identity, multiply, transpose, complex } = require('..')
3
-
4
- // create a sparse matrix
5
- console.log('creating a 1000x1000 sparse matrix...')
6
- const a = identity(1000, 1000, 'sparse')
7
-
8
- // do operations with a sparse matrix
9
- console.log('doing some operations on the sparse matrix...')
10
- const b = multiply(a, a)
11
- const c = multiply(b, complex(2, 2))
12
- const d = transpose(c)
13
- const e = multiply(d, a)
14
- console.log('size(e)=', e.size())
15
-
16
- // we will not print the output, but doing the same operations
17
- // with a dense matrix are very slow, try it for yourself.
18
- console.log('already done')
19
- console.log('now try this with a dense matrix :)')