Inner

Inner is a generalization of Dot in that other functions can be used in place of Times and Plus. In the next two cells we see Inner[Times, list1, list2, Plus] is equivalent to  Dot[list1, list2] .  Before demonstrating Dot I clear the values from any symbols.

Clear["Global`*"] ; Dot[{a, b, c}, {x, y, z}]

a x + b y + c z

Inner[Times, {a, b, c}, {x, y, z}, Plus]

a x + b y + c z

The next cell shows a generic application of Inner.

Inner[f, {a, b, c}, {x, y, z}, g]

g[f[a, x], f[b, y], f[c, z]]

In the next cell we see that when Inner is given only three arguments Plus is used in place of a fourth argument.

Inner[f, {a, b, c}, {x, y, z}]

f[a, x] + f[b, y] + f[c, z]

In the next cell we see Inner can handle things other than lists. Inner only requires that the objects have the same Head and compatible dimensions.

Inner[f, h[a, b, c], h[x, y, z], g]

g[f[a, x], f[b, y], f[c, z]]

Inner product with Matrices and Tensors

In the section on Dot we saw that Dot can be used on lists, matrices and tensors. Inner does the same thing with Lists, matrices and tensors except other functions can be used in place of Times and Plus.

In the next cell (A) and (B) are matrices and Inner[Times, A, B, Plus] is the same as Dot[A, B].

A = {{a1, a2}, {b1, b2}} ; B = {{x1, x2, x3}, {y1, y2, y3}} ;  Inner[Times, A, B, Plus] === Dot[A, B]

True

In the next cell we evaluate  Inner[f, A, B, g]  and we get somthing like matrix multiplication except (f) is used in place of multiplication, and (g) is used in place of addition.

Inner[f, A, B, g]

{{g[f[a1, x1], f[a2, y1]], g[f[a1, x2], f[a2, y2]], g[f[a1, x3], f[a2, y3]]}, {g[f[b1, x1], f[b2, y1]], g[f[b1, x2], f[b2, y2]], g[f[b1, x3], f[b2, y3]]}}

I don't give examples of using Inner on tensors, but I think you will find it's very much like the Dot product of tensors.

Giving Inner a 5^th argument

In the next example Inner is given (1) as a fifth argument and, (at least in this example) Inner[Times,A,B,Plus,1] is equivalent to Dot[Transpose[A], B].

A = {{a1, a2}, {b1, b2}} ; B = {{x1, x2, x3}, {y1, y2, y3}} ;  Inner[Times, A, B, Plus, 1] === Dot[Transpose[A], B]

True

The next example shows that (at least in this example) the default value of the fifth argument is 2.

Inner[f, A, B, g] === Inner[f, A, B, g, 2]

True


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page