GeneralOuterProduct - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

ArrayTools

 GeneralOuterProduct
 compute a general outer product of two Arrays

 Calling Sequence GeneralOuterProduct(X,f,Y,opts)

Parameters

 X - Array, list, or expression f - function to apply to each pair of values from X and Y Y - Array, list, or expression opts - (optional) one or more options

Description

 • GeneralOuterProduct(X,f,g,Y) accepts two Arrays, X and Y, with N and M dimensions respectively. The result is an Array of N+M dimensions.
 For each combination of indices of X and indices of Y, the function f is applied to the elements from X and Y.
 • For example, when X and Y are vectors of integers from 1 to 10, and f is the multiplication function (*), GeneralOuterProduct produces a matrix representing a traditional multiplication table.
 • If X or Y is a list, it is treated as one-dimensional Array.
 • If either X or Y is not an Array or list, it is interpreted as an Array with zero dimensions (and the value X or Y).
 • By default, the datatype of the resulting Array is determined by SuggestedDatatype based on the two inputs. Depending on f, this may not be suitable. For example, if X and Y have datatype integer[1] and f is *, the computed values might 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 f are first evaluated to hardware floating-point using evalhf, as is the result returned by f. Furthermore, unless the datatype option was specified, the resulting Array has datatype float[8].
 • The subtype of the result depends on the subtypes of X and Y, as described by SuggestedSubtype. The option ':-subtype'=st', where st is a valid rtable subtype, overrides the default subtype.
 • The storage order of the result is based on the storage orders of X and Y, as described by SuggestedOrder. This can be overridden using the option :-order=ord, where ord is one of C_order or Fortran_order.
 • This function is part of the ArrayTools package, so it can be used in the short form GeneralOuterProduct(..) only after executing the command with(ArrayTools). However, it can always be accessed through the long form of the command by using ArrayTools[GeneralOuterProduct](..).

Examples

Produce a multiplication table of integers from 1 to 10.

 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$
 > $V≔⟨\mathrm{}\left(1..10\right)⟩:$
 > $\mathrm{GeneralOuterProduct}\left(V,\mathrm{*},V\right)$
 $\left[\begin{array}{rrrrrrrrrr}1& 2& 3& 4& 5& 6& 7& 8& 9& 10\\ 2& 4& 6& 8& 10& 12& 14& 16& 18& 20\\ 3& 6& 9& 12& 15& 18& 21& 24& 27& 30\\ 4& 8& 12& 16& 20& 24& 28& 32& 36& 40\\ 5& 10& 15& 20& 25& 30& 35& 40& 45& 50\\ 6& 12& 18& 24& 30& 36& 42& 48& 54& 60\\ 7& 14& 21& 28& 35& 42& 49& 56& 63& 70\\ 8& 16& 24& 32& 40& 48& 56& 64& 72& 80\\ 9& 18& 27& 36& 45& 54& 63& 72& 81& 90\\ 10& 20& 30& 40& 50& 60& 70& 80& 90& 100\end{array}\right]$ (4.1)

GeneralOuterProduct works with Arrays of arbitrary dimension. Generate a three-dimensional Array of the sum of all combinations of the roll of 3 dice:

 > $R≔⟨1,2,3,4,5,6⟩:$

2-D intermediate result

 > $\mathrm{R2}≔\mathrm{GeneralOuterProduct}\left(R,\mathrm{+},R\right)$
 $\left[\begin{array}{rrrrrr}2& 3& 4& 5& 6& 7\\ 3& 4& 5& 6& 7& 8\\ 4& 5& 6& 7& 8& 9\\ 5& 6& 7& 8& 9& 10\\ 6& 7& 8& 9& 10& 11\\ 7& 8& 9& 10& 11& 12\end{array}\right]$ (4.2)
 > $\mathrm{whattype}\left(\mathrm{R2}\right)$
 ${\mathrm{Matrix}}$ (4.3)

3-D final result

 > $\mathrm{R3}≔\mathrm{GeneralOuterProduct}\left(\mathrm{R2},\mathrm{+},R\right)$
 > $\mathrm{whattype}\left(\mathrm{R3}\right)$
 ${\mathrm{Array}}$ (4.4)

The possibilities when the last roll was 1.

 > ${\mathrm{R3}}_{1}$
 $\left[\begin{array}{rrrrrr}3& 4& 5& 6& 7& 8\\ 4& 5& 6& 7& 8& 9\\ 5& 6& 7& 8& 9& 10\\ 6& 7& 8& 9& 10& 11\\ 7& 8& 9& 10& 11& 12\\ 8& 9& 10& 11& 12& 13\end{array}\right]$ (4.5)

The possibilities when the last roll was 6.

 > ${\mathrm{R3}}_{6}$
 $\left[\begin{array}{rrrrrr}8& 9& 10& 11& 12& 13\\ 9& 10& 11& 12& 13& 14\\ 10& 11& 12& 13& 14& 15\\ 11& 12& 13& 14& 15& 16\\ 12& 13& 14& 15& 16& 17\\ 13& 14& 15& 16& 17& 18\end{array}\right]$ (4.6)

Count occurrences of each total from 1 to 18.

 > $\mathrm{map2}\left(\mathrm{numboccur},\mathrm{R3},\left[\mathrm{}\left(1..18\right)\right]\right)$
 $\left[{0}{,}{0}{,}{1}{,}{3}{,}{6}{,}{10}{,}{15}{,}{21}{,}{25}{,}{27}{,}{27}{,}{25}{,}{21}{,}{15}{,}{10}{,}{6}{,}{3}{,}{1}\right]$ (4.7)

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

 > $A≔{\mathrm{Vector}}_{\mathrm{row}}\left(\left[4,8,12\right],\mathrm{datatype}={\mathrm{integer}}_{1}\right)$
 $\left[\begin{array}{rrr}4& 8& 12\end{array}\right]$ (4.8)
 > $\mathrm{GeneralOuterProduct}\left(A,\mathrm{*},A\right)$
 > $\mathrm{GeneralOuterProduct}\left(A,\mathrm{*},A,\mathrm{datatype}={\mathrm{integer}}_{2}\right)$
 $\left[\begin{array}{rrr}16& 32& 48\\ 32& 64& 96\\ 48& 96& 144\end{array}\right]$ (4.9)

If the results of GeneralOuterProduct fall in the range 0..1, they can be visualized using ImageTools.

 > $V≔\mathrm{Array}\left(\left[\mathrm{seq}\left(i,i=0..1,\mathrm{numelems}=101\right)\right],\mathrm{datatype}={\mathrm{float}}_{8}\right):$
 > ${V}_{1..4}$
 $\left[\begin{array}{cccc}0.0& 0.01& 0.02& 0.03\end{array}\right]$ (4.10)
 > ${V}_{-4..-1}$
 > $\mathrm{img}≔\mathrm{GeneralOuterProduct}\left(V,\mathrm{max},V\right)$
 > ${\mathrm{img}}_{1..4,1..4}$
 $\left[\begin{array}{cccc}0.0& 0.01& 0.02& 0.03\\ 0.01& 0.01& 0.02& 0.03\\ 0.02& 0.02& 0.02& 0.03\\ 0.03& 0.03& 0.03& 0.03\end{array}\right]$ (4.11)
 > ${\mathrm{img}}_{-4..-1,-4..-1}$
 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$
 > $\mathrm{Embed}\left(\mathrm{img}\right)$

Compatibility

 • The ArrayTools[GeneralOuterProduct] command was introduced in Maple 2021.
 • For more information on Maple 2021 changes, see Updates in Maple 2021.