# dot product not working [closed]

Im supposed to take the dot product of a function and itself. The matrix is a system of 4 equations defined to be :

L[x_, y_, z_, w_] :=
{{4.0 x - 2 y + 3 z - 5 w},
{3 x + 3 y + 3 z - 8 w  },
{-6 x - y + 4 z + 3 w   },
{-4 x + 2 y + 3 z + 5 w }}


I can't take L[x,y,z,w].L[x,y,z,w] because Dot::dotsh error. I was able to successfully take the dot product earlier today but for some reason, I can't anymore.

## closed as off-topic by Szabolcs, J. M. is away♦Sep 26 '17 at 8:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, J. M. is away
If this question can be reworded to fit the rules in the help center, please edit the question.

• Is that really supposed to be a $4\times1$ matrix? If not, remove the inner braces: L[x_, y_, z_, w_] := {4.0 x - 2 y + 3 z - 5 w, (* stuff *)} – J. M. is away Sep 26 '17 at 5:02
• hi. If you do this, then the error goes way r1=L0[x,y,z,w]; r1.Transpose[r1] – Nasser Sep 26 '17 at 5:02
• Have you read the description of this error in the documentation? – Szabolcs Sep 26 '17 at 8:15

## 2 Answers

I think you are looking for this:

L[x_, y_, z_, w_] := {4.0 x - 2 y + 3 z - 5 w,
3 x + 3 y + 3 z - 8 w, -6 x - y + 4 z + 3 w, -4 x + 2 y + 3 z + 5 w}


i.e. remove the multiple curly brackets.

So that you have output

In:= Dot[L[x, y, z, w], L[x, y, z, w]]

Out= (-5 w + 4. x - 2 y + 3 z)^2 + (5 w - 4 x + 2 y +
3 z)^2 + (-8 w + 3 x + 3 y + 3 z)^2 + (3 w - 6 x - y + 4 z)^2


Also check out this.

The reason it worked before is that you were using plain 1-dimensional lists. Dot works well with 1D lists. Your definition of L returns a 2-dimensional list (4 rows by 1 column), which does not work (natively) with Dot .

If you do not want to change the definition of L, you can calculate the dot product using

L[a, b, c, d][[All, 1]].L[w, x, y, z][[All, 1]]


or by using

Plus @@ Apply[Times, Transpose@{L[a, b, c, d], L[w, x, y, z]}, 2]


or what amounts to the same thing,

Total@Flatten[Transpose@L[a, b, c, d]*Transpose@L[w, x, y, z]]


If you want it both ways, you can define a new L that returns a 1-dimensional list. Use the new one for calculations, since it works with Dot. When you want need the old 4x1 form, simply wrap the new form with braces and take the transpose, like this: Transpose[{new}].

• I get your point, but it's a bit confusing to say that "Dot works with 1D lists". Dot works with arbitrary dimensional tensors, contracting the last index of the first tensor with the first index of the second tensor. It's what we use for matrix products. One can't multiply a 4x1 matrix with a 4x1 matrix. – Szabolcs Sep 26 '17 at 8:13
• @Szabolcs Many thanks for pointing that out. It seems so obvious. I would fix my answer, but what more could I say than is already in the documentation, or in your comment. I agree the question is off topic. – LouisB Sep 26 '17 at 8:46