1 |
|
2 |
|
3 | # Function apply
|
4 |
|
5 | Apply a function that maps an array to a scalar
|
6 | along a given axis of a matrix or array.
|
7 | Returns a new matrix or array with one less dimension than the input.
|
8 |
|
9 |
|
10 | ## Syntax
|
11 |
|
12 | ```js
|
13 | math.apply(A, dim, callback)
|
14 | ```
|
15 |
|
16 | ### Where
|
17 |
|
18 | - `dim: number` is a zero-based dimension over which to concatenate the matrices.
|
19 |
|
20 | ### Parameters
|
21 |
|
22 | Parameter | Type | Description
|
23 | --------- | ---- | -----------
|
24 | `array` | Array | Matrix | The input Matrix
|
25 | `dim` | number | The dimension along which the callback is applied
|
26 | `callback` | Function | The callback function that is applied. This Function should take an array or 1-d matrix as an input and return a number.
|
27 |
|
28 | ### Returns
|
29 |
|
30 | Type | Description
|
31 | ---- | -----------
|
32 | Array | Matrix | res The residual matrix with the function applied over some dimension.
|
33 |
|
34 |
|
35 | ## Examples
|
36 |
|
37 | ```js
|
38 | const A = [[1, 2], [3, 4]]
|
39 | const sum = math.sum
|
40 |
|
41 | math.apply(A, 0, sum) // returns [4, 6]
|
42 | math.apply(A, 1, sum) // returns [3, 7]
|
43 | ```
|
44 |
|
45 |
|
46 | ## See also
|
47 |
|
48 | [map](map.md),
|
49 | [filter](filter.md),
|
50 | [forEach](forEach.md)
|