GeneralInnerProduct - Maple Help

ArrayTools

 GeneralInnerProduct
 compute a general inner product of two Arrays

 Calling Sequence GeneralInnerProduct(X,f,g,Y,opts)

Parameters

 X - Array, list, or expression f - function to reduce the results of calls to g g - function applied to pairs of values from X and Y Y - Array, list, or expression opts - (optional) one or more options

Description

 • GeneralInnerProduct(X,f,g,Y) accepts two Arrays, X and Y, with N and M dimensions respectively and where the last dimension of X matches the first dimension of Y. The result is an Array with N+M-2 dimensions.
 For each combination of the first N-1 indices of X and last M-1 indices of Y, the function g is applied to the corresponding pairs of the last dimension of X and the first of Y, and the values returned by those calls to g are accumulated by f.
 • When f is the addition function (+) and g the multiplication function (*), and X and Y are matrices or vectors, GeneralInnerProduct is equivalent to matrix/vector multiplication.
 • Unlike matrix multiplication, GeneralInnerProduct is not restricted to one- and two-dimensional Arrays, and is not restricted to multiplying values and adding the results.
 • If X or Y is a list, it is treated as one-dimensional Array.
 • If X is not an Array or list, a one-dimensional Array whose dimension matches the first dimension of Y is created and filled with the value of X.
 Similarly, if Y is not an Array or list, an Array matching the last dimension of X is created and filled with Y.
 • By default, the datatype of the resulting Array is determined by SuggestedDatatype based on the two inputs. Depending on f or g, this may not be suitable. For example, if X and Y have datatype integer[1] and f and g are + and * respectively, 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 and g are first evaluated to hardware floating-point using evalhf, as is the result returned by them. 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 GeneralInnerProduct(..) only after executing the command with(ArrayTools). However, it can always be accessed through the long form of the command by using ArrayTools[GeneralInnerProduct](..).

Examples

 > $\mathrm{with}\left(\mathrm{ArrayTools}\right):$
 > $\mathrm{M1}≔\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(3,2\right)$
 $\left[\begin{array}{rr}99& 92\\ 29& -31\\ 44& 67\end{array}\right]$ (4.1)
 > $\mathrm{M2}≔\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(2,3\right)$
 $\left[\begin{array}{rrr}-32& -4& 8\\ -74& 27& 69\end{array}\right]$ (4.2)
 > $\mathrm{GeneralInnerProduct}\left(\mathrm{M1},\mathrm{+},\mathrm{*},\mathrm{M2}\right)$
 $\left[\begin{array}{rrr}-9976& 2088& 7140\\ 1366& -953& -1907\\ -6366& 1633& 4975\end{array}\right]$ (4.3)
 > $\mathrm{LinearAlgebra}:-\mathrm{Multiply}\left(\mathrm{M1},\mathrm{M2}\right)$
 $\left[\begin{array}{rrr}-9976& 2088& 7140\\ 1366& -953& -1907\\ -6366& 1633& 4975\end{array}\right]$ (4.4)

Using relation and Boolean functions to determine characteristics of a Vector:

 > $V≔⟨1|2|4|8|16|32|64|128⟩$
 $\left[\begin{array}{rrrrrrrr}1& 2& 4& 8& 16& 32& 64& 128\end{array}\right]$ (4.5)

Are all values less than 80?

 > $\mathrm{GeneralInnerProduct}\left(V,\mathrm{and},\mathrm{<},80\right)$
 ${\mathrm{false}}$ (4.6)

Are any values less than 20?

 > $\mathrm{GeneralInnerProduct}\left(V,\mathrm{or},\mathrm{<},20\right)$
 ${\mathrm{true}}$ (4.7)

Using relation and Boolean functions to determine characteristics of rows of a Matrix:

 > $\mathrm{MN}≔⟨⟨1|2|3|4⟩,⟨5|6|0|7⟩,⟨8|9|10|11⟩,⟨0|0|0|0⟩,⟨12|13|14|15⟩⟩$
 $\left[\begin{array}{rrrr}1& 2& 3& 4\\ 5& 6& 0& 7\\ 8& 9& 10& 11\\ 0& 0& 0& 0\\ 12& 13& 14& 15\end{array}\right]$ (4.8)

Which are all zero?

 > $\mathrm{GeneralInnerProduct}\left(\mathrm{MN},\mathrm{and},\mathrm{=},0\right)$
 $\left[\begin{array}{c}\mathrm{false}\\ \mathrm{false}\\ \mathrm{false}\\ \mathrm{true}\\ \mathrm{false}\end{array}\right]$ (4.9)

Which contain at least one zero?

 > $\mathrm{GeneralInnerProduct}\left(\mathrm{MN},\mathrm{or},\mathrm{=},0\right)$
 $\left[\begin{array}{c}\mathrm{false}\\ \mathrm{true}\\ \mathrm{false}\\ \mathrm{true}\\ \mathrm{false}\end{array}\right]$ (4.10)

Which contain at least one nonzero element?

 > $\mathrm{GeneralInnerProduct}\left(\mathrm{MN},\mathrm{or},\mathrm{<>},0\right)$
 $\left[\begin{array}{c}\mathrm{true}\\ \mathrm{true}\\ \mathrm{true}\\ \mathrm{false}\\ \mathrm{true}\end{array}\right]$ (4.11)

The order of X and Y can be reversed to test columns:

Which are all zero?

 > $\mathrm{GeneralInnerProduct}\left(0,\mathrm{and},\mathrm{=},\mathrm{MN}\right)$
 $\left[\begin{array}{cccc}\mathrm{false}& \mathrm{false}& \mathrm{false}& \mathrm{false}\end{array}\right]$ (4.12)

Which contain at least one zero?

 > $\mathrm{GeneralInnerProduct}\left(0,\mathrm{or},\mathrm{=},\mathrm{MN}\right)$
 $\left[\begin{array}{cccc}\mathrm{true}& \mathrm{true}& \mathrm{true}& \mathrm{true}\end{array}\right]$ (4.13)

Which contain only values <= 13?

 > $\mathrm{GeneralInnerProduct}\left(13,\mathrm{and},\mathrm{>=},\mathrm{MN}\right)$
 $\left[\begin{array}{cccc}\mathrm{true}& \mathrm{true}& \mathrm{false}& \mathrm{false}\end{array}\right]$ (4.14)

GeneralInnerProduct works with more than two dimensions.

 > $\mathrm{A1}≔\mathrm{Array}\left(\left[\left[\left[a,b\right],\left[c,d\right]\right],\left[\left[e,f\right],\left[g,h\right]\right]\right]\right)$
 ${{\mathrm{_rtable}}}_{{36893627901289033964}}$ (4.15)
 > $\mathrm{A2}≔\mathrm{Array}\left(\left[\left[1,2\right],\left[3,4\right]\right]\right)$
 $\left[\begin{array}{rr}1& 2\\ 3& 4\end{array}\right]$ (4.16)
 > $R≔\mathrm{GeneralInnerProduct}\left(\mathrm{A1},f,\mathrm{/},\mathrm{A2}\right)$
 ${{\mathrm{_rtable}}}_{{36893627901289036724}}$ (4.17)
 > $R\left[1\right],R\left[2\right]$
 $\left[\begin{array}{cc}{f}{}\left({a}{,}\frac{{b}}{{3}}\right)& {f}{}\left(\frac{{a}}{{2}}{,}\frac{{b}}{{4}}\right)\\ {f}{}\left({c}{,}\frac{{d}}{{3}}\right)& {f}{}\left(\frac{{c}}{{2}}{,}\frac{{d}}{{4}}\right)\end{array}\right]{,}\left[\begin{array}{cc}{f}{}\left({e}{,}\frac{{f}}{{3}}\right)& {f}{}\left(\frac{{e}}{{2}}{,}\frac{{f}}{{4}}\right)\\ {f}{}\left({g}{,}\frac{{h}}{{3}}\right)& {f}{}\left(\frac{{g}}{{2}}{,}\frac{{h}}{{4}}\right)\end{array}\right]$ (4.18)

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

 > $A≔\mathrm{Matrix}\left(\left[\left[1,5\right],\left[10,20\right]\right],\mathrm{datatype}=\mathrm{integer}\left[1\right]\right)$
 $\left[\begin{array}{rr}1& 5\\ 10& 20\end{array}\right]$ (4.19)
 > $\mathrm{GeneralInnerProduct}\left(A,\mathrm{+},\mathrm{*},A\right)$
 > $\mathrm{GeneralInnerProduct}\left(A,\mathrm{+},\mathrm{*},A,\mathrm{datatype}=\mathrm{integer}\left[2\right]\right)$
 $\left[\begin{array}{rr}51& 105\\ 210& 450\end{array}\right]$ (4.20)

GeneralInnerProduct can perform operations on ImageTools images. This example converts an RGB image to gray scale, primarily based on the green channel:

 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$
 > $\mathrm{img}≔\mathrm{SampleImage}\left(3\right)$
 ${{\mathrm{_rtable}}}_{{36893627901289021436}}$ (4.21)
 > $\mathrm{bw}≔\mathrm{GeneralInnerProduct}\left(\mathrm{img},\mathrm{+},\mathrm{*},\left[0.1,0.8,0.1\right],\mathrm{datatype}=\mathrm{float}\left[8\right]\right)$
 ${{\mathrm{_rtable}}}_{{36893627901289009748}}$ (4.22)
 > $\mathrm{Embed}\left(\mathrm{bw}\right)$

Compatibility

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