apply.md (1240B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 36 37 Type | Description 38 ---- | ----------- 39 40 41 ## Examples 42 43 ```js 44 const A = [[1, 2], [3, 4]] 45 const sum = math.sum 46 47 math.apply(A, 0, sum) // returns [4, 6] 48 math.apply(A, 1, sum) // returns [3, 7] 49 ``` 50 51 52 ## See also 53 54 [map](map.md), 55 [filter](filter.md), 56 [forEach](forEach.md)