ArrayTools
ReduceAlongDimension
reduce the elements of an Array by a function
Calling Sequence
Parameters
Description
Examples
Compatibility
ReduceAlongDimension(fcn,A,dim,opts)
fcn
-
function to reduce along a dimension to a single value
A
Array
dim
(optional) dimension to add along
opts
(optional) one or more options
ReduceAlongDimension(fcn,A,dim) reduces with function fcn the elements in dimension dim of an N-dimensional Array to produce an Array with (N-1) dimensions.
When dim is not specified, the first non-singleton dimension is used for reduction. If the dimension specified is a higher dimension than the number of dimensions in the input, the input itself is returned.
If ReduceAlongDimension is applied to a Vector, a scalar is returned. If applied to a Matrix, a Vector is returned.
By default, the datatype of the result Array is the same as that of input Array A. Depending on fcn, this may not be suitable. For example, if A has datatype integer[1], and fcn adds up values, the result may be too large to store in that datatype.
The option ':-datatype'=dt, where dt is a valid Array datatype, can be used to override the default datatype for the result.
If option evalhf or evalhf=true is passed, the arguments passed to each call to fcn are first evaluated to hardware floating-point using evalhf, as is the result returned by fcn. Furthermore, unless the datatype option was specified, the result Array will have datatype float[8].
This function is part of the ArrayTools package, so it can be used in the short form ReduceAlongDimension(..) only after executing the command with(ArrayTools). However, it can always be accessed through the long form of the command by using ArrayTools[ReduceAlongDimension](..).
with⁡ArrayTools:
Collapse a Matrix into a Vector of row or column totals.
M≔Matrix⁡a,b,c,d,e,f
M≔abcdef
ReduceAlongDimension⁡`+`,M,1
a+db+ec+f
ReduceAlongDimension⁡`+`,M,2
a+b+cd+e+f
Find the maximum per column or row of a Matrix.
N≔Matrix⁡3,1,4,1,5,9,2,6,5
N≔314159265
ReduceAlongDimension⁡max,N,1
369
ReduceAlongDimension⁡max,N,2
496
Compute sums along each dimension of a three-dimensional Array.
A≔Array⁡1..2,1..2,1..2,x,y,z↦a‖x‖y‖z
A1,A2
a111a112a121a122,a211a212a221a222
ReduceAlongDimension⁡`+`,A,1
a111+a211a112+a212a121+a221a122+a222
ReduceAlongDimension⁡`+`,A,2
a111+a121a112+a122a211+a221a212+a222
ReduceAlongDimension⁡`+`,A,3
a111+a112a121+a122a211+a212a221+a222
The datatype option can be used if the input datatype would not be suitable for the result.
A≔Array⁡1,40,80,30,40,50,datatype=integer1
A≔14080304050
Error, (in ArrayTools:-ReduceAlongDimension) unable to store '130' when datatype=integer[1]
ReduceAlongDimension⁡`+`,A,1,datatype=integer2
3180130
The ArrayTools[ReduceAlongDimension] command was introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
See Also
ArrayTools[AddAlongDimension]
ArrayTools[MultiplyAlongDimension]
ArrayTools[ScanAlongDimension]
Download Help Document