mathjs 10.2.0 → 10.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. package/HISTORY.md +43 -0
  2. package/docs/expressions/syntax.md +31 -2
  3. package/docs/reference/functions/cumsum.md +57 -0
  4. package/docs/reference/functions/format.md +1 -1
  5. package/docs/reference/functions/map.md +22 -5
  6. package/docs/reference/functions/subset.md +10 -2
  7. package/docs/reference/functions/sum.md +2 -1
  8. package/docs/reference/functions/symbolicEqual.md +62 -0
  9. package/docs/reference/functions.md +3 -1
  10. package/lib/browser/math.js +6 -6
  11. package/lib/browser/math.js.map +1 -1
  12. package/lib/cjs/entry/dependenciesAny/dependenciesCumSum.generated.js +26 -0
  13. package/lib/cjs/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +26 -0
  14. package/lib/cjs/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +29 -0
  15. package/lib/cjs/entry/dependenciesAny.generated.js +24 -0
  16. package/lib/cjs/entry/dependenciesNumber/dependenciesCumSum.generated.js +26 -0
  17. package/lib/cjs/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +26 -0
  18. package/lib/cjs/entry/dependenciesNumber.generated.js +16 -0
  19. package/lib/cjs/entry/impureFunctionsAny.generated.js +22 -8
  20. package/lib/cjs/entry/impureFunctionsNumber.generated.js +6 -0
  21. package/lib/cjs/entry/pureFunctionsAny.generated.js +18 -12
  22. package/lib/cjs/entry/pureFunctionsNumber.generated.js +8 -2
  23. package/lib/cjs/expression/embeddedDocs/construction/fraction.js +3 -3
  24. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +240 -234
  25. package/lib/cjs/expression/embeddedDocs/function/algebra/symbolicEqual.js +15 -0
  26. package/lib/cjs/expression/embeddedDocs/function/matrix/subset.js +2 -2
  27. package/lib/cjs/expression/embeddedDocs/function/statistics/cumsum.js +15 -0
  28. package/lib/cjs/expression/node/FunctionNode.js +74 -55
  29. package/lib/cjs/expression/parse.js +12 -8
  30. package/lib/cjs/expression/transform/cumsum.transform.js +57 -0
  31. package/lib/cjs/expression/transform/sum.transform.js +1 -1
  32. package/lib/cjs/factoriesAny.js +24 -0
  33. package/lib/cjs/factoriesNumber.js +18 -2
  34. package/lib/cjs/function/algebra/simplify.js +8 -0
  35. package/lib/cjs/function/algebra/simplifyCore.js +2 -2
  36. package/lib/cjs/function/algebra/symbolicEqual.js +88 -0
  37. package/lib/cjs/function/matrix/eigs/complexEigs.js +39 -28
  38. package/lib/cjs/function/matrix/map.js +53 -15
  39. package/lib/cjs/function/matrix/subset.js +15 -5
  40. package/lib/cjs/function/statistics/cumsum.js +151 -0
  41. package/lib/cjs/function/statistics/sum.js +1 -1
  42. package/lib/cjs/function/string/format.js +1 -1
  43. package/lib/cjs/header.js +2 -2
  44. package/lib/cjs/type/fraction/function/fraction.js +20 -8
  45. package/lib/cjs/utils/collection.js +3 -27
  46. package/lib/cjs/utils/switch.js +31 -0
  47. package/lib/cjs/version.js +1 -1
  48. package/lib/esm/entry/dependenciesAny/dependenciesCumSum.generated.js +14 -0
  49. package/lib/esm/entry/dependenciesAny/dependenciesCumSumTransform.generated.js +14 -0
  50. package/lib/esm/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +16 -0
  51. package/lib/esm/entry/dependenciesAny.generated.js +3 -0
  52. package/lib/esm/entry/dependenciesNumber/dependenciesCumSum.generated.js +14 -0
  53. package/lib/esm/entry/dependenciesNumber/dependenciesCumSumTransform.generated.js +14 -0
  54. package/lib/esm/entry/dependenciesNumber.generated.js +2 -0
  55. package/lib/esm/entry/impureFunctionsAny.generated.js +22 -9
  56. package/lib/esm/entry/impureFunctionsNumber.generated.js +8 -2
  57. package/lib/esm/entry/pureFunctionsAny.generated.js +13 -8
  58. package/lib/esm/entry/pureFunctionsNumber.generated.js +6 -1
  59. package/lib/esm/expression/embeddedDocs/construction/fraction.js +3 -3
  60. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +220 -216
  61. package/lib/esm/expression/embeddedDocs/function/algebra/symbolicEqual.js +8 -0
  62. package/lib/esm/expression/embeddedDocs/function/matrix/subset.js +2 -2
  63. package/lib/esm/expression/embeddedDocs/function/statistics/cumsum.js +8 -0
  64. package/lib/esm/expression/node/FunctionNode.js +70 -53
  65. package/lib/esm/expression/parse.js +12 -8
  66. package/lib/esm/expression/transform/cumsum.transform.js +48 -0
  67. package/lib/esm/expression/transform/sum.transform.js +1 -1
  68. package/lib/esm/factoriesAny.js +3 -0
  69. package/lib/esm/factoriesNumber.js +2 -0
  70. package/lib/esm/function/algebra/simplify.js +8 -0
  71. package/lib/esm/function/algebra/simplifyCore.js +2 -2
  72. package/lib/esm/function/algebra/symbolicEqual.js +80 -0
  73. package/lib/esm/function/matrix/eigs/complexEigs.js +36 -25
  74. package/lib/esm/function/matrix/map.js +53 -15
  75. package/lib/esm/function/matrix/subset.js +15 -5
  76. package/lib/esm/function/statistics/cumsum.js +139 -0
  77. package/lib/esm/function/statistics/sum.js +1 -1
  78. package/lib/esm/function/string/format.js +1 -1
  79. package/lib/esm/type/fraction/function/fraction.js +20 -8
  80. package/lib/esm/utils/collection.js +1 -26
  81. package/lib/esm/utils/switch.js +24 -0
  82. package/lib/esm/version.js +1 -1
  83. package/package.json +15 -11
  84. package/types/index.d.ts +209 -23
  85. package/types/index.ts +274 -57
  86. package/types/tsconfig.json +2 -1
package/HISTORY.md CHANGED
@@ -1,5 +1,48 @@
1
1
  # History
2
2
 
3
+ # 2022-03-23, version 10.4.1
4
+
5
+ - Improve TypeScript definitions for function `unit` (#2479).
6
+ Thanks @SinanAkkoyun.
7
+ - Add tests for type declarations (#2448). Thanks @samestep.
8
+ - Further improvement to TypeScript definitions of `std` and `variance`
9
+ (make dimension parameter optional, #2474). Thanks @NattapongSiri.
10
+ - Next step (as per #2431) for full publication of "is" functions like
11
+ `isMatrix` etc: Provide TypeScript definitions of "is" functions and
12
+ make them type guards. (#2432). Thanks @ChristopherChudzicki.
13
+ - Fix #2491: Multi line object expressions don't work with comments (#2492).
14
+ Thanks @gwhitney.
15
+ - Fix #2478: a bug in calculating the eigenvectors when dealing with complex
16
+ numbers (#2496). Thanks @gwhitney.
17
+ - Update project dependencies and devDependencies.
18
+
19
+
20
+ # 2022-03-07, version 10.4.0
21
+
22
+ - Fix #2461: make sure `simplifyCore` recurses over all binary nodes (#2462).
23
+ Thanks @gwhitney.
24
+ - Fix #2429: fix the TypeScript definitions of functions `std` and `variance`
25
+ (#2455). Thanks @NattapongSiri.
26
+ - Fix #1633: implement a `cumsum` function generating cumulative sums of a list
27
+ of values or a matrix. (#1870). Thanks @hjonasson.
28
+ - Upgrade to the latest version of `Fraction.js`, having more strict input,
29
+ only accepting an integer numerator and denominator. See #2427.
30
+ - Fix typo in documentation example for `format`. (#2468) Thanks @abranhe.
31
+ - Write unit tests for all jsdoc examples. See #2452. Thanks @gwhitney.
32
+
33
+
34
+ # 2021-03-02, version 10.3.0
35
+
36
+ - Fix #1260: implement function `symbolicEqual` (#2424). Thanks @gwhitney.
37
+ - Fix #2441, #2442: support passing a function as argument to functions created
38
+ in the expression parser (#2443). Thanks @gwhitney.
39
+ - Fix #2325: improve documentation of subset indices (#2446). Thanks @gwhitney.
40
+ - Fix #2439: fix a bug in `complexEigs` in which real-valued norms were
41
+ inadvertently being typed as complex numbers (#2445). Thanks @gwhitney.
42
+ - Fix #2436: improve documentation and error message of function `map` (#2457).
43
+ Thanks @gwhitney.
44
+
45
+
3
46
  # 2022-03-01, version 10.2.0
4
47
 
5
48
  - Implemented context options to control simplifications allowed in `simplify`,
@@ -138,8 +138,8 @@ math.evaluate('log(10000, 3 + 7)') // 4
138
138
  math.evaluate('sin(pi / 4)') // 0.7071067811865475
139
139
  ```
140
140
 
141
- New functions can be defined using the `function` keyword. Functions can be
142
- defined with multiple variables. Function assignments are limited: they can
141
+ New functions can be defined by "assigning" an expression to a function call
142
+ with one or more variables. Such function assignments are limited: they can
143
143
  only be defined on a single line.
144
144
 
145
145
  ```js
@@ -153,6 +153,35 @@ parser.evaluate('g(x, y) = x ^ y')
153
153
  parser.evaluate('g(2, 3)') // 8
154
154
  ```
155
155
 
156
+ Note that these function assignments do _not_ create closures; put another way,
157
+ all free variables in mathjs are dynamic:
158
+
159
+ ```js
160
+ const parser = math.parser()
161
+
162
+ parser.evaluate('x = 7')
163
+ parser.evaluate('h(y) = x + y')
164
+ parser.evaluate('h(3)') // 10
165
+ parser.evaluate('x = 3')
166
+ parser.evaluate('h(3)') // 6, *not* 10
167
+ ```
168
+
169
+ It is however possible to pass functions as parameters:
170
+
171
+ ```js
172
+ const parser = math.parser()
173
+
174
+ parser.evaluate('twice(func, x) = func(func(x))')
175
+ parser.evaluate('twice(square, 2)') // 16
176
+ parser.evaluate('f(x) = 3*x')
177
+ parser.evaluate('twice(f, 2)') // 18
178
+
179
+ // a simplistic "numerical derivative":
180
+ parser.evaluate('eps = 1e-10')
181
+ parser.evaluate('nd(f, x) = (f(x+eps) - func(x-eps))/(2*eps)')
182
+ parser.evaluate('nd(square,2)') // 4.000000330961484
183
+ ```
184
+
156
185
  Math.js itself heavily uses typed functions, which ensure correct inputs and
157
186
  throws meaningful errors when the input arguments are invalid. One can create
158
187
  a [typed-function](https://github.com/josdejong/typed-function) in the
@@ -0,0 +1,57 @@
1
+ <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
+
3
+ # Function cumsum
4
+
5
+ Compute the cumulative sum of a matrix or a list with values.
6
+ In case of a (multi dimensional) array or matrix, the cumulative sums
7
+ along a specified dimension (defaulting to the first) will be calculated.
8
+
9
+
10
+ ## Syntax
11
+
12
+ ```js
13
+ math.cumsum(a, b, c, ...)
14
+ math.cumsum(A)
15
+ ```
16
+
17
+ ### Parameters
18
+
19
+ Parameter | Type | Description
20
+ --------- | ---- | -----------
21
+ `args` | ... * | A single matrix or or multiple scalar values
22
+
23
+ ### Returns
24
+
25
+ Type | Description
26
+ ---- | -----------
27
+ * | The cumulative sum of all values
28
+
29
+
30
+ ### Throws
31
+
32
+ Type | Description
33
+ ---- | -----------
34
+
35
+
36
+ ## Examples
37
+
38
+ ```js
39
+ math.cumsum(2, 1, 4, 3) // returns [2, 3, 7, 10]
40
+ math.cumsum([2, 1, 4, 3]) // returns [2, 3, 7, 10]
41
+ math.cumsum([[1, 2], [3, 4]]) // returns [[1, 2], [4, 6]]
42
+ math.cumsum([[1, 2], [3, 4]], 0) // returns [[1, 2], [4, 6]]
43
+ math.cumsum([[1, 2], [3, 4]], 1) // returns [[1, 3], [3, 7]]
44
+ math.cumsum([[2, 5], [4, 3], [1, 7]]) // returns [[2, 5], [6, 8], [7, 15]]
45
+ ```
46
+
47
+
48
+ ## See also
49
+
50
+ [mean](mean.md),
51
+ [median](median.md),
52
+ [min](min.md),
53
+ [max](max.md),
54
+ [prod](prod.md),
55
+ [std](std.md),
56
+ [variance](variance.md),
57
+ [sum](sum.md)
@@ -115,7 +115,7 @@ function formatCurrency(value) {
115
115
  // you could also use math.format inside the callback:
116
116
  // return '$' + math.format(value, {notation: 'fixed', precision: 2})
117
117
  }
118
- math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]'
118
+ math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]'
119
119
  ```
120
120
 
121
121
 
@@ -2,8 +2,15 @@
2
2
 
3
3
  # Function map
4
4
 
5
- Create a new matrix or array with the results of the callback function executed on
6
- each entry of the matrix/array.
5
+ Create a new matrix or array with the results of a callback function executed on
6
+ each entry of a given matrix/array.
7
+
8
+ For each entry of the input, the callback is invoked with three arguments:
9
+ the value of the entry, the index at which that entry occurs, and the full
10
+ matrix/array being traversed. Note that because the matrix/array might be
11
+ multidimensional, the "index" argument is always an array of numbers giving
12
+ the index in each dimension. This is true even for vectors: the "index"
13
+ argument is an array of length 1, rather than simply a number.
7
14
 
8
15
 
9
16
  ## Syntax
@@ -16,14 +23,14 @@ math.map(x, callback)
16
23
 
17
24
  Parameter | Type | Description
18
25
  --------- | ---- | -----------
19
- `x` | Matrix &#124; Array | The matrix to iterate on.
20
- `callback` | Function | The callback method is invoked with three parameters: the value of the element, the index of the element, and the matrix being traversed.
26
+ `x` | Matrix &#124; Array | The input to iterate on.
27
+ `callback` | Function | The function to call (as described above) on each entry of the input
21
28
 
22
29
  ### Returns
23
30
 
24
31
  Type | Description
25
32
  ---- | -----------
26
- Matrix &#124; array | Transformed map of x
33
+ Matrix &#124; array | Transformed map of x; always has the same type and shape as x
27
34
 
28
35
 
29
36
  ### Throws
@@ -38,6 +45,16 @@ Type | Description
38
45
  math.map([1, 2, 3], function(value) {
39
46
  return value * value
40
47
  }) // returns [1, 4, 9]
48
+
49
+ // The calling convention for the callback can cause subtleties:
50
+ math.map([1, 2, 3], math.format)
51
+ // throws TypeError: map attempted to call 'format(1,[0])' but argument 2 of type Array does not match expected type number or function or Object or string or boolean
52
+ // [This happens because `format` _can_ take a second argument,
53
+ // but its semantics don't match that of the 2nd argument `map` provides]
54
+
55
+ // To avoid this error, use a function that takes exactly the
56
+ // desired arguments:
57
+ math.map([1, 2, 3], x => math.format(x)) // returns ['1', '2', '3']
41
58
  ```
42
59
 
43
60
 
@@ -17,7 +17,7 @@ math.subset(value, index, replacement [, defaultValue]) // replace a subset
17
17
  Parameter | Type | Description
18
18
  --------- | ---- | -----------
19
19
  `matrix` | Array &#124; Matrix &#124; string | An array, matrix, or string
20
- `index` | Index | An index containing ranges for each dimension
20
+ `index` | Index | For each dimension of the target, specifies an index or a list of indices to fetch or set. `subset` uses the cartesian product of the indices specified in each dimension.
21
21
  `replacement` | * | An array, matrix, or scalar. If provided, the subset is replaced with replacement. If not provided, the subset is returned
22
22
  `defaultValue` | * | Default value, filled in on new entries when the matrix is resized. If not provided, math.matrix elements will be left undefined. Default value: undefined.
23
23
 
@@ -44,8 +44,16 @@ math.subset(d, math.index([0, 1], 1)) // returns [[2], [4]]
44
44
 
45
45
  // replace a subset
46
46
  const e = []
47
- const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]]
47
+ const f = math.subset(e, math.index(0, [0, 2]), [5, 6]) // f = [[5, 6]] and e = [[5, 0, 6]]
48
48
  const g = math.subset(f, math.index(1, 1), 7, 0) // g = [[5, 6], [0, 7]]
49
+
50
+ // get submatrix using ranges
51
+ const M = [
52
+ [1,2,3],
53
+ [4,5,6],
54
+ [7,8,9]
55
+ ]
56
+ math.subset(M, math.index(math.range(0,2), math.range(0,3))) // [[1,2,3],[4,5,6]]
49
57
  ```
50
58
 
51
59
 
@@ -50,4 +50,5 @@ math.sum([[2, 5], [4, 3], [1, 7]]) // returns 22
50
50
  [max](max.md),
51
51
  [prod](prod.md),
52
52
  [std](std.md),
53
- [variance](variance.md)
53
+ [variance](variance.md),
54
+ [cumsum](cumsum.md)
@@ -0,0 +1,62 @@
1
+ <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
+
3
+ # Function symbolicEqual
4
+
5
+ Attempts to determine if two expressions are symbolically equal, i.e.
6
+ one is the result of valid algebraic manipulations on the other.
7
+ Currently, this simply checks if the difference of the two expressions
8
+ simplifies down to 0. So there are two important caveats:
9
+ 1. whether two expressions are symbolically equal depends on the
10
+ manipulations allowed. Therefore, this function takes an optional
11
+ third argument, which are the options that control the behavior
12
+ as documented for the `simplify()` function.
13
+ 2. it is in general intractable to find the minimal simplification of
14
+ an arbitrarily complicated expression. So while a `true` value
15
+ of `symbolicEqual` ensures that the two expressions can be manipulated
16
+ to match each other, a `false` value does not absolutely rule this out.
17
+
18
+
19
+ ## Syntax
20
+
21
+ ```js
22
+ symbolicEqual(expr1, expr2)
23
+ symbolicEqual(expr1, expr2, options)
24
+ ```
25
+
26
+ ### Parameters
27
+
28
+ Parameter | Type | Description
29
+ --------- | ---- | -----------
30
+ `expr1` | Node &#124; string | The first expression to compare
31
+ `expr2` | Node &#124; string | The second expression to compare
32
+ `options` | Object | Optional option object, passed to simplify
33
+
34
+ ### Returns
35
+
36
+ Type | Description
37
+ ---- | -----------
38
+ boolean | Returns true if a valid manipulation making the expressions equal is found.
39
+
40
+
41
+ ### Throws
42
+
43
+ Type | Description
44
+ ---- | -----------
45
+
46
+
47
+ ## Examples
48
+
49
+ ```js
50
+ symbolicEqual('x*y', 'y*x') // true
51
+ symbolicEqual('x*y', 'y*x', {context: {multiply: {commutative: false}}})
52
+ //false
53
+ symbolicEqual('x/y', '(y*x^(-1))^(-1)') // true
54
+ symbolicEqual('abs(x)','x') // false
55
+ symbolicEqual('abs(x)','x', simplify.positiveContext) // true
56
+ ```
57
+
58
+
59
+ ## See also
60
+
61
+ [simplify](simplify.md),
62
+ [evaluate](evaluate.md)
@@ -25,6 +25,7 @@ Function | Description
25
25
  [simplify(expr)](functions/simplify.md) | Simplify an expression tree.
26
26
  [simplifyCore(expr)](functions/simplifyCore.md) | simplifyCore() performs single pass simplification suitable for applications requiring ultimate performance.
27
27
  [math.slu(A,&nbsp;order,&nbsp;threshold)](functions/slu.md) | Calculate the Sparse Matrix LU decomposition with full pivoting.
28
+ [symbolicEqual(expr1,&nbsp;expr2)](functions/symbolicEqual.md) | Attempts to determine if two expressions are symbolically equal, i.
28
29
  [math.usolve(U,&nbsp;b)](functions/usolve.md) | Finds one solution of a linear equation system by backward substitution.
29
30
  [math.usolveAll(U,&nbsp;b)](functions/usolveAll.md) | Finds all solutions of a linear equation system by backward substitution.
30
31
 
@@ -137,7 +138,7 @@ Function | Description
137
138
  [math.identity(n)](functions/identity.md) | Create a 2-dimensional identity matrix with size m x n or n x n.
138
139
  [math.inv(x)](functions/inv.md) | Calculate the inverse of a square matrix.
139
140
  [math.kron(x,&nbsp;y)](functions/kron.md) | Calculates the kronecker product of 2 matrices or vectors.
140
- [math.map(x,&nbsp;callback)](functions/map.md) | Create a new matrix or array with the results of the callback function executed on each entry of the matrix/array.
141
+ [math.map(x,&nbsp;callback)](functions/map.md) | Create a new matrix or array with the results of a callback function executed on each entry of a given matrix/array.
141
142
  [math.matrixFromColumns(...arr)](functions/matrixFromColumns.md) | Create a dense matrix from vectors as individual columns.
142
143
  [math.matrixFromFunction(size,&nbsp;fn)](functions/matrixFromFunction.md) | Create a matrix by evaluating a generating function at each index.
143
144
  [math.matrixFromRows(...arr)](functions/matrixFromRows.md) | Create a dense matrix from vectors as individual rows.
@@ -214,6 +215,7 @@ Function | Description
214
215
 
215
216
  Function | Description
216
217
  ---- | -----------
218
+ [math.cumsum(a,&nbsp;b,&nbsp;c,&nbsp;...)](functions/cumsum.md) | Compute the cumulative sum of a matrix or a list with values.
217
219
  [math.mad(a,&nbsp;b,&nbsp;c,&nbsp;...)](functions/mad.md) | Compute the median absolute deviation of a matrix or a list with values.
218
220
  [math.max(a,&nbsp;b,&nbsp;c,&nbsp;...)](functions/max.md) | Compute the maximum value of a matrix or a list with values.
219
221
  [math.mean(a,&nbsp;b,&nbsp;c,&nbsp;...)](functions/mean.md) | Compute the mean value of matrix or a list with values.