mathjs 10.2.0 → 10.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/HISTORY.md +12 -0
  2. package/docs/expressions/syntax.md +31 -2
  3. package/docs/reference/functions/map.md +22 -5
  4. package/docs/reference/functions/subset.md +10 -2
  5. package/docs/reference/functions/symbolicEqual.md +62 -0
  6. package/docs/reference/functions.md +2 -1
  7. package/lib/browser/math.js +5 -5
  8. package/lib/browser/math.js.map +1 -1
  9. package/lib/cjs/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +29 -0
  10. package/lib/cjs/entry/dependenciesAny.generated.js +8 -0
  11. package/lib/cjs/entry/impureFunctionsAny.generated.js +16 -8
  12. package/lib/cjs/entry/pureFunctionsAny.generated.js +9 -9
  13. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +237 -234
  14. package/lib/cjs/expression/embeddedDocs/function/algebra/symbolicEqual.js +15 -0
  15. package/lib/cjs/expression/embeddedDocs/function/matrix/subset.js +2 -2
  16. package/lib/cjs/expression/node/FunctionNode.js +74 -55
  17. package/lib/cjs/factoriesAny.js +8 -0
  18. package/lib/cjs/function/algebra/simplify.js +8 -0
  19. package/lib/cjs/function/algebra/symbolicEqual.js +88 -0
  20. package/lib/cjs/function/matrix/eigs/complexEigs.js +8 -6
  21. package/lib/cjs/function/matrix/map.js +53 -15
  22. package/lib/cjs/function/matrix/subset.js +15 -5
  23. package/lib/cjs/header.js +2 -2
  24. package/lib/cjs/version.js +1 -1
  25. package/lib/esm/entry/dependenciesAny/dependenciesSymbolicEqual.generated.js +16 -0
  26. package/lib/esm/entry/dependenciesAny.generated.js +1 -0
  27. package/lib/esm/entry/impureFunctionsAny.generated.js +16 -9
  28. package/lib/esm/entry/pureFunctionsAny.generated.js +8 -8
  29. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +218 -216
  30. package/lib/esm/expression/embeddedDocs/function/algebra/symbolicEqual.js +8 -0
  31. package/lib/esm/expression/embeddedDocs/function/matrix/subset.js +2 -2
  32. package/lib/esm/expression/node/FunctionNode.js +70 -53
  33. package/lib/esm/factoriesAny.js +1 -0
  34. package/lib/esm/function/algebra/simplify.js +8 -0
  35. package/lib/esm/function/algebra/symbolicEqual.js +80 -0
  36. package/lib/esm/function/matrix/eigs/complexEigs.js +8 -6
  37. package/lib/esm/function/matrix/map.js +53 -15
  38. package/lib/esm/function/matrix/subset.js +15 -5
  39. package/lib/esm/version.js +1 -1
  40. package/package.json +1 -1
  41. package/types/index.d.ts +4 -4
package/HISTORY.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # History
2
2
 
3
+ # 2021-03-02, version 10.3.0
4
+
5
+ - Fix #1260: implement function `symbolicEqual` (#2424). Thanks @gwhitney.
6
+ - Fix #2441, #2442: support passing a function as argument to functions created
7
+ in the expression parser (#2443). Thanks @gwhitney.
8
+ - Fix #2325: improve documentation of subset indices (#2446). Thanks @gwhitney.
9
+ - Fix #2439: fix a bug in `complexEigs` in which real-valued norms were
10
+ inadvertently being typed as complex numbers (#2445). Thanks @gwhitney.
11
+ - Fix #2436: improve documentation and error message of function `map` (#2457).
12
+ Thanks @gwhitney.
13
+
14
+
3
15
  # 2022-03-01, version 10.2.0
4
16
 
5
17
  - 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
@@ -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 | 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 | 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 | array | Transformed map of x
33
+ Matrix | 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 | Matrix | 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
 
@@ -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.