Message Boards Message Boards

1
|
26554 Views
|
19 Replies
|
17 Total Likes
View groups...
Share
Share this post:

The Dot Operator vs Standard Matrix Multiplication

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
19 Replies
 See 'Possible Issues' in the Dot documentation: Dot effectively treats vectors multiplied from the right as column vectors.
POSTED BY: Ilian Gachevski
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
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
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
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
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
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
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 Horvát
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
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
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
Posted 5 years ago

Let me try this question. Is there anyway to get mathematica, e.g. dot product, to do a one by one multiplication or multiplicative mapping. I have been trying to get this tyoeresult: a= {1,2,3}, b={10,20,30}, c=(5,5,1} a*b={10,40,90} a*c={5,10,3} I want an element by element scalar multiplication building into an array. I have tried dot, matrices,Kronecker,Outer products, to no avail, and I cant find under mapping. Appreciate any help but please use the figures in my example so I can follow along. Thanks!

POSTED BY: Frank Lobue
Posted 2 years ago

If I multiply a 2 by 3 matrix by a 3 by 2 in matrix form in either order, I get the expected answer, but in list form I get an error. I am pretty new to Mathematica, so maybe it's my fault, but this seems strange to me.

Edit: I found that the problem is that List does not convert a matrix to a list of lists in the right form for a matrix.

POSTED BY: Robert Cordery

You have entered your matrices a,b in two dimensional form, but the kernel sees them a lists of lists:

In[4]:= a

Out[4]= {{1, 2}, {3, 4}, {5, 6}}

There is no need to "convert" them to lists of lists.

POSTED BY: Gianluca Gorni
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 Horvát

It looks like regular Times[ ] multiplication does element-by-element multiplication, as least in these cases.

What drives me crazy is the hoops I have to jump through just to do standard linear algebra such as multiplying a matrix times a column vector. More on that later. Have to watch myself as a humble Home User.

enter image description hereenter image description here

Can you explain in more details those hoops you feel you need jump through? Surely you do not mean the simple matter of having to use . instead of *?

POSTED BY: Szabolcs Horvát

One main wish: Anywhere we have what is a vector argument to a function, we should be able to type it as a row or column vector. I've figured out that I can just add in a substitution rule to do that, so I guess that's not too much to ask. But it would be much easier just to have Mathematica convert row and column vectors to lists when they're passed to ParametricPlot, etc. enter image description here Also, couldn't matrices be threaded over lists of vectors, instead of requiring the clutter of mapping the dot-product? And why doesn't MatrixForm applied to the result give me the expected list of matrices, in standard layout. Even TraditionalForm, doesn't give the expected, traditional list of vectors in column form. The readability of column vectors vs. lists is worth it to me.

enter image description here

If I have time, I'll try to develop a linear algebra package that lets me use the time-tested traditional notation, from square brackets, to a space meaning matrix multiplication, to always being able to use traditional matrix and vector layout.

It is a good feature that I can use oblong matrices to put all my vertices into, so I won't complain too much about these small difficulties. enter image description here enter image description here I'm assuming that the different forms of bracketing needed in column vectors versus row vectors have some payoff later in terms of being able to use the Dot[ ] function with tensors. But for beginners, I think it would be good not to have any extra quirks or hurdles for them to get over.

Here's my notebook with the above examples in it:

/Users/chrisyoungsmacpro/Library/Mobile Documents/com~apple~CloudDocs/Documents/Math/Subjects/Algebra/Linear Algebra/Vectors, tensors/Mathematica; matrices and vectors with ~/Combining vectors and matrices/B3b. .nb

Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract