Group Abstract Group Abstract

Message Boards Message Boards

The Dot Operator vs Standard Matrix Multiplication

GROUPS:
The Dot operator can do both dot products and matrix multiplication. As a result,
it can multiply a matrix by a row vector as in M.v, even though that's not kosher
in linear algebra. I wonder if the documentation should warn about that.
POSTED BY: Frank Kampas
Answer
8 months ago
 See 'Possible Issues' in the Dot documentation: Dot effectively treats vectors multiplied from the right as column vectors.
POSTED BY: Ilian Gachevski
Answer
8 months ago
I don't think what's said in Possible Issues is completely correct
In[1]:= {{1, 2}, {3, 4}}.{5, 6}

Out[1]= {17, 39}

In[2]:= {{1, 2}, {3, 4}}.{{5}, {6}}

Out[2]= {{17}, {39}}
since you can use dot to right mulitply a matrix by a column vector.
POSTED BY: Frank Kampas
Answer
8 months ago
I think this is exactly why the 'Possible Issues' give an explicit illustration for the {1, 1} and {{1}, {1}} cases, thereby making the same point.

Both results are certainly correct according to the formal tensor definition given in the Details section.
POSTED BY: Ilian Gachevski
Answer
8 months ago
Ilian, rather than putting this in "Possible Issues", I'd suggest putting it in the main description, explaining how Mathematica's treatment of a matrix as a list of lists enables returning a list of dot products, as a row vector, when mulitplying a matrix by a row vector. 
POSTED BY: Frank Kampas
Answer
8 months ago
Maybe it does need more explaining. Personally I am not entirely convinced, but in any case, there is a 'Give Feedback' button at the bottom of each web documentation page, which is an excellent way to send any feedback or suggestions about the documentation content.
POSTED BY: Ilian Gachevski
Answer
8 months ago
It is somewhat missing the point to talk about how Dot fails as matrix multiplication.  The point of Dot is that it is a tensor operation and that it sometimes corresponds to matrix multiplication. In terms of tensor indices, Dot contracts the right-most index of the first entry with the left-most index of the second. Using Part for indices we have for a tensor of rank p+1 and a tensor of rank q+1 then Dot gives a tensor of rank p+q.
(T.U)[[i1,i2,...,ip,j1,j2,...,jq]]=Sum[T[[i1, i2, ..., ip, k]] U[[k, j1, j2, ..., jq]], {k, 1, n}]
It is typical in Mathematica to define functions that are as general as they can reasonably be; Dot is a tensor generalization of matrix multiplication.
POSTED BY: Terrence Honan
Answer
8 months ago
I'm not saying that Dot fails at matrix multiplication; I'm saying that it overachieves.  That's fine except that it could be confusing to students learning linear algebra. 
POSTED BY: Frank Kampas
Answer
8 months ago
Mathematica was designed to work with tensors of arbitrary dimensions, not just 2-dimensional matrices.  Correspondingly, Dot[] does tensor contraction, not just matrix multiplication.  I find this implementation much more logical and consistent than what I see in rigidly matrix based systems like MATLAB, which don't even support vectors.

On Mathematica.SE I regularly see people trying to use "row vectors" and "column vectors" and a lot of transposision and complex indexing to try to do simple tasks.  This is all unnecessary complication in Mathematica, which does support proper 1D vectors, as well as N-D tensors.  I believe most of these people come from MATLAB or MATLAB-inspired software which typically doesn't support vectors at all, only row and column matrices.

When I studied linear algebra, we started with vectors, not "row vectors" and "column vectors", and I didn't find it confusing.  So it's not quite accurate to say that this is "not kosher".  There are different traditions of teaching this stuff.  One is not necessarily better than the other.
POSTED BY: Szabolcs Horvat
Answer
8 months ago
Yes, so think of all the questions to Stack Exchange that could be eliminated by simply explaining in the documentation how Mathematica takes a more general approach to vectors than other software.  You don't want to discourage people who are trying to switch to Mathematica.  Perhaps I'm thinking of this from a marketing viewpoint.  I suppose that's one of the dangers of having a Ph.D. in Physics and an M.B.A.  However, given the recent push to increase Mathematica's presence in STEM, I think that my suggestion makes sense.
POSTED BY: Frank Kampas
Answer
8 months ago
But isn't it already explained under the Detail section?  There are explicit examples for vector-vector, vector-matrix, matrix-vector and matrix-matrix products, followed by a general definition of tensor contraction, written out explicitly using sums.
POSTED BY: Szabolcs Horvat
Answer
8 months ago
I think the fact that Dot is more general than matrix multiplication should be at the start of its documentation, not inside Detail or Possible Issues. 

For example:  "a.b.c or Dot[a,b,c] gives products of vectors, matrices and tensors.  Note that Dot works in a more general way than matrix mulitplication.  See Details and Possible Issues"

Mathematica is typically regarded as being very powerful but having a steep learning curve.  Anything that makes it easier for new users is a good thing, in my opinion.
POSTED BY: Frank Kampas
Answer
8 months ago
For me, the biggest gotcha for the unsuspecting with Dot is that it's noncommutative. If you're innocently normalizing vectors without a keen and active understanding of Mathematica's generalized approach to tensors and of its Attributes structure (like knowing that noncommutativity is flagged by the ABSENCE of Orderless-ness) you might be in for some troubled times.

Fred
POSTED BY: Fred Klingener
Answer
8 months ago