ScanAlongDimension - Maple Help

ArrayTools

 ScanAlongDimension
 accumulate the elements of an Array by a function

 Calling Sequence ScanAlongDimension(fcn,A,dim,opts)

Parameters

 fcn - function to accumulate along a dimension to a single value A - Array dim - (optional) dimension to add along opts - (optional) one or more options

Description

 • ScanAlongDimension(fcn,A,dim) accumulates with function fcn the elements in dimension dim of an N-dimensional Array to produce another Array with N dimensions.
 • When dim is not specified, the first non-singleton dimension is used for accumulation. If the dimension specified is a higher dimension than the number of dimensions in the input, the input itself 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].
 • ScanAlongDimensions usually returns a new Array with the same dimensions (but possibly different datatype) as A. Passing the option inplace or inplace=true causes A to be updated in-place. This option only applies to dense rectangular Arrays with no indexing functions, and causes the datatype option to be ignored.
 • This function is part of the ArrayTools package, so it can be used in the short form ScanAlongDimension(..) only after executing the command with(ArrayTools). However, it can always be accessed through the long form of the command by using ArrayTools[ScanAlongDimension](..).

Examples

 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$
 > $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{ScanAlongDimension}\left(\mathrm{+},M,1\right)$
 $\left[\begin{array}{ccc}{a}& {b}& {c}\\ {a}{+}{d}& {b}{+}{e}& {c}{+}{f}\end{array}\right]$ (2)
 > $\mathrm{ScanAlongDimension}\left(\mathrm{+},M,2\right)$
 $\left[\begin{array}{ccc}{a}& {a}{+}{b}& {a}{+}{b}{+}{c}\\ {d}& {d}{+}{e}& {d}{+}{e}{+}{f}\end{array}\right]$ (3)
 > $N≔\mathrm{Matrix}\left(\left[\left[1,5,3\right],\left[2,4,3\right]\right]\right)$
 ${N}{≔}\left[\begin{array}{ccc}{1}& {5}& {3}\\ {2}& {4}& {3}\end{array}\right]$ (4)
 > $\mathrm{ScanAlongDimension}\left(\mathrm{max},N,1\right)$
 $\left[\begin{array}{ccc}{1}& {5}& {3}\\ {2}& {5}& {3}\end{array}\right]$ (5)
 > $\mathrm{ScanAlongDimension}\left(\mathrm{max},N,2\right)$
 $\left[\begin{array}{ccc}{1}& {5}& {5}\\ {2}& {4}& {4}\end{array}\right]$ (6)
 > $v≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[1,2,3,2,1\right]\right)$
 ${v}{≔}\left[\begin{array}{ccccc}{1}& {2}& {3}& {2}& {1}\end{array}\right]$ (7)
 > $\mathrm{ScanAlongDimension}\left(\mathrm{max},v\right)$
 $\left[\begin{array}{ccccc}{1}& {2}& {3}& {3}& {3}\end{array}\right]$ (8)
 > $\mathrm{ScanAlongDimension}\left(\left(a,b\right)↦10\cdot a+b,v\right)$
 $\left[\begin{array}{ccccc}{1}& {12}& {123}& {1232}& {12321}\end{array}\right]$ (9)
 > $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}$ (10)
 > $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]$ (11)
 > $\mathrm{A1}≔\mathrm{ScanAlongDimension}\left(\mathrm{+},A,1\right)$
 ${\mathrm{A1}}{≔}\begin{array}{c}\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a121}}\\ {\mathrm{a111}}{+}{\mathrm{a211}}& {\mathrm{a121}}{+}{\mathrm{a221}}\end{array}\right]\\ \hfill {\text{slice of 2 × 2 × 2 Array}}\end{array}$ (12)
 > $\mathrm{A1}\left[1\right],\mathrm{A1}\left[2\right]$
 $\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a112}}\\ {\mathrm{a121}}& {\mathrm{a122}}\end{array}\right]{,}\left[\begin{array}{cc}{\mathrm{a111}}{+}{\mathrm{a211}}& {\mathrm{a112}}{+}{\mathrm{a212}}\\ {\mathrm{a121}}{+}{\mathrm{a221}}& {\mathrm{a122}}{+}{\mathrm{a222}}\end{array}\right]$ (13)
 > $\mathrm{A2}≔\mathrm{ScanAlongDimension}\left(\mathrm{+},A,2\right)$
 ${\mathrm{A2}}{≔}\begin{array}{c}\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a111}}{+}{\mathrm{a121}}\\ {\mathrm{a211}}& {\mathrm{a211}}{+}{\mathrm{a221}}\end{array}\right]\\ \hfill {\text{slice of 2 × 2 × 2 Array}}\end{array}$ (14)
 > $\mathrm{A2}\left[1\right],\mathrm{A2}\left[2\right]$
 $\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a112}}\\ {\mathrm{a111}}{+}{\mathrm{a121}}& {\mathrm{a112}}{+}{\mathrm{a122}}\end{array}\right]{,}\left[\begin{array}{cc}{\mathrm{a211}}& {\mathrm{a212}}\\ {\mathrm{a211}}{+}{\mathrm{a221}}& {\mathrm{a212}}{+}{\mathrm{a222}}\end{array}\right]$ (15)
 > $\mathrm{A3}≔\mathrm{ScanAlongDimension}\left(\mathrm{+},A,3\right)$
 ${\mathrm{A3}}{≔}\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}$ (16)
 > $\mathrm{A3}\left[1\right],\mathrm{A3}\left[2\right]$
 $\left[\begin{array}{cc}{\mathrm{a111}}& {\mathrm{a111}}{+}{\mathrm{a112}}\\ {\mathrm{a121}}& {\mathrm{a121}}{+}{\mathrm{a122}}\end{array}\right]{,}\left[\begin{array}{cc}{\mathrm{a211}}& {\mathrm{a211}}{+}{\mathrm{a212}}\\ {\mathrm{a221}}& {\mathrm{a221}}{+}{\mathrm{a222}}\end{array}\right]$ (17)

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]$ (18)
 > $\mathrm{ScanAlongDimension}\left(\mathrm{+},A,1\right)$
 > $\mathrm{ScanAlongDimension}\left(\mathrm{+},A,1,\mathrm{datatype}=\mathrm{integer}\left[2\right]\right)$
 $\left[\begin{array}{ccc}{1}& {40}& {80}\\ {31}& {80}& {130}\end{array}\right]$ (19)

Compatibility

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