1
$\begingroup$

Does anyone know why Inner[] behaves differently in the last of these three examples?

Inner[f, {a, b}, {e1, e2}, List]

{f[a, e1], f[b, e2]}

Inner[f, {a, b}, {{1, 0}, e2}, List]

{f[a, {1, 0}], f[b, e2]}

Inner[f, {a, b}, {{1, 0}, {0, 1}}, List]

{{f[a, 1], f[b, 0]}, {f[a, 0], f[b, 1]}}

Why didn't the last case return {f[a, {1, 0}], f[b, {0, 1}]}?

enter image description here

I am using Mathematica 11.

$\endgroup$
1
  • 1
    $\begingroup$ Please post your code as copyable plain text rather than an image. The easier it is for someone to reproduce your example, the more likely you'll get a response. $\endgroup$ Commented Jun 12, 2019 at 15:02

1 Answer 1

3
$\begingroup$

Like Dot, Inner doesn't always operate f on the elements on the first levels of the input lists:

Like Dot, Inner effectively contracts the last index of the first tensor with the first index of the second tensor. Applying Inner to a rank r tensor and a rank s tensor gives a rank r+s-2 tensor.

In other words, since you're contracting a vector (rank 1) and a matrix (rank 2) you end up with a rank 1 list if you use a general function for the last argument of Inner:

Inner[f, {a, b}, {{0, 1}, {2, 3}}, g]

{g[f[a, 0], f[b, 2]], g[f[a, 1], f[b, 3]]}

To get the result you really want, it's probably easier to use MapThread:

MapThread[f, {{a, b}, {{0, 1}, {2, 3}}}]

{f[a, {0, 1}], f[b, {2, 3}]}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.