 ReduceAlongDimension - Maple Help

ArrayTools

 ReduceAlongDimension
 reduce the elements of an Array by a function Calling Sequence ReduceAlongDimension(fcn,A,dim,opts) Parameters

 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 Description

 • 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, 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.
 • 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](..). Examples

 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$

Collapse a Matrix into a Vector of row or column totals.

 > $M≔\mathrm{Matrix}\left(\left[\left[a,b,c\right],\left[d,e,f\right]\right]\right)$
 ${M}{≔}\left[\begin{array}{ccc}{a}& {b}& {c}\\ {d}& {e}& {f}\end{array}\right]$ (1)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},M,1\right)$
 $\left[\begin{array}{ccc}{a}{+}{d}& {b}{+}{e}& {c}{+}{f}\end{array}\right]$ (2)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},M,2\right)$
 $\left[\begin{array}{c}{a}{+}{b}{+}{c}\\ {d}{+}{e}{+}{f}\end{array}\right]$ (3)

Find the maximum per column or row of a Matrix.

 > $N≔\mathrm{Matrix}\left(\left[\left[3,1,4\right],\left[1,5,9\right],\left[2,6,5\right]\right]\right)$
 ${N}{≔}\left[\begin{array}{ccc}{3}& {1}& {4}\\ {1}& {5}& {9}\\ {2}& {6}& {5}\end{array}\right]$ (4)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{max},N,1\right)$
 $\left[\begin{array}{ccc}{3}& {6}& {9}\end{array}\right]$ (5)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{max},N,2\right)$
 $\left[\begin{array}{c}{4}\\ {9}\\ {6}\end{array}\right]$ (6)

Compute sums along each dimension of a three-dimensional Array.

 > $A≔\mathrm{Array}\left(1..2,1..2,1..2,\left(x,y,z\right)↦a‖x‖y‖z\right)$
 ${A}{≔}\begin{array}{c}\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a121}}\\ {\mathrm{a211}}& {\mathrm{a221}}\end{array}\right]\\ \hfill {\text{slice of 2 × 2 × 2 Array}}\end{array}$ (7)
 > $A\left[1\right],A\left[2\right]$
 $\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a112}}\\ {\mathrm{a121}}& {\mathrm{a122}}\end{array}\right]{,}\left[\begin{array}{cc}{\mathrm{a211}}& {\mathrm{a212}}\\ {\mathrm{a221}}& {\mathrm{a222}}\end{array}\right]$ (8)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},A,1\right)$
 $\left[\begin{array}{cc}{\mathrm{a111}}{+}{\mathrm{a211}}& {\mathrm{a112}}{+}{\mathrm{a212}}\\ {\mathrm{a121}}{+}{\mathrm{a221}}& {\mathrm{a122}}{+}{\mathrm{a222}}\end{array}\right]$ (9)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},A,2\right)$
 $\left[\begin{array}{cc}{\mathrm{a111}}{+}{\mathrm{a121}}& {\mathrm{a112}}{+}{\mathrm{a122}}\\ {\mathrm{a211}}{+}{\mathrm{a221}}& {\mathrm{a212}}{+}{\mathrm{a222}}\end{array}\right]$ (10)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},A,3\right)$
 $\left[\begin{array}{cc}{\mathrm{a111}}{+}{\mathrm{a112}}& {\mathrm{a121}}{+}{\mathrm{a122}}\\ {\mathrm{a211}}{+}{\mathrm{a212}}& {\mathrm{a221}}{+}{\mathrm{a222}}\end{array}\right]$ (11)

The datatype option can be used if the input datatype would not be suitable for the result.

 > $A≔\mathrm{Array}\left(\left[\left[1,40,80\right],\left[30,40,50\right]\right],\mathrm{datatype}=\mathrm{integer}\left[1\right]\right)$
 ${A}{≔}\left[\begin{array}{ccc}{1}& {40}& {80}\\ {30}& {40}& {50}\end{array}\right]$ (12)
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},A,1\right)$
 > $\mathrm{ReduceAlongDimension}\left(\mathrm{+},A,1,\mathrm{datatype}=\mathrm{integer}\left[2\right]\right)$
 $\left[\begin{array}{ccc}{31}& {80}& {130}\end{array}\right]$ (13) Compatibility

 • The ArrayTools[ReduceAlongDimension] command was introduced in Maple 2021.