Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dot product confusion

342 views
Skip to first unread message

S. B. Gray

unread,
May 25, 2010, 6:32:53 AM5/25/10
to
Given

ptsa = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};

I thought the following expressions would be identical:

{aa, bb, cc}.ptsa (* expression 1 *)
ptsa.{aa, bb, cc} (* expression 2 *)

but they are not. They evaluate respectively as:

{aa x1 + bb x2 + cc x3, aa y1 + bb y2 + cc y3,
aa z1 + bb z2 + cc z3}

{aa x1 + bb y1 + cc z1, aa x2 + bb y2 + cc z2,
aa x3 + bb y3 + cc z3}

Since ptsa is itself three xyz coordinates, the expressions might be
ambiguous, but I assumed the dot product would always commute. Should
there be a warning?

The first result is the one I want.

Steve Gray

Alois Steindl

unread,
May 26, 2010, 7:06:31 AM5/26/10
to
Hello,
your assumptions seem to be wrong.
The dot product in Mathematica isn't just "the inner product, but the
matrix product, in your case
ptsa*v or v*ptsa.
In the first case v is considered as column vector, in the second one as
row vector.
Matrix products are rarely abelian.

Alois

--
Alois Steindl, Tel.: +43 (1) 58801 / 32558
Inst. for Mechanics and Mechatronics Fax.: +43 (1) 58801 / 32598
Vienna University of Technology, A-1040 Wiedner Hauptstr. 8-10

Vince Virgilio

unread,
May 26, 2010, 7:06:42 AM5/26/10
to
On May 25, 6:32 am, "S. B. Gray" <ste...@ROADRUNNER.COM> wrote:

SNIP

> ambiguous, but I assumed the dot product would always commute. Should
> there be a warning?

SNIP

Nope, this is just the usual behavior of an inner product of a matrix
and vector.

The doc. says that Dot[] contracts the last dimension of the first
tensor, with the first dimension of the second tensor. So if either
operand has dims > 1, Dot[] doesn't commute.

Vince Virgilio

S. B. Gray

unread,
May 26, 2010, 7:08:00 AM5/26/10
to
Thanks to all who answered my question. I temporarily forgot about
matrix - vector multiplication.

Simon

unread,
May 26, 2010, 7:08:11 AM5/26/10
to
Hi Steve,

The result is correct, since {aa,bb,cc} is treated as a vector that
can be either row or column.
Then, thinking of matrix multiplication - with indices and summation
convention
(A.v)_i = A_{i j} v_j => A.v = ( A_{1 j}v_j , A_{2 j}v_j , A_{3 j}
v_j )
(v.A)_i = v_j A_{j i} => v.A = ( A_{j 1}v_j , A_{j 2}v_j , A_{j 3 }
v_j )

If you want your dot products to be unambiguous, then write your
vector as a 3x1 or a 1x3 matrix - equivalent to a column and row
vector resp.
So using your objects:

In[1]:= ptsa={{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}};

Column vector:
In[2]:= ptsa.{{aa},{bb},{cc}}
Out[2]= {{aa x1+bb y1+cc z1},{aa x2+bb y2+cc z2},{aa x3+bb y3+cc z3}}

Fails if used the wrong way round:
In[3]:= {{aa},{bb},{cc}}.ptsa
During evaluation of In[3]:= Dot::dotsh: Tensors {{aa},{bb},{cc}} and
{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}} have incompatible shapes. >>
Out[3]= {{aa},{bb},{cc}}.{{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}

Row vector:
In[4]:= {{aa,bb,cc}}.ptsa
Out[4]= {{aa x1+bb x2+cc x3,aa y1+bb y2+cc y3,aa z1+bb z2+cc z3}}

Fails if used the wrong way around:
In[5]:= ptsa.{{aa,bb,cc}}
During evaluation of In[5]:= Dot::dotsh: Tensors {{x1,y1,z1},
{x2,y2,z2},{x3,y3,z3}} and {{aa,bb,cc}} have incompatible shapes. >>
Out[5]= {{x1,y1,z1},{x2,y2,z2},{x3,y3,z3}}.{{aa,bb,cc}}

~~~
Simon

Mark Adler

unread,
May 26, 2010, 7:08:22 AM5/26/10
to
On 2010-05-25 03:32:53 -0700, S. B. Gray said:
> ptsa = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};

The dot product of two vectors commutes, but your ptsa is a matrix. In
general matrix multiplication does not commute.

> {aa, bb, cc}.ptsa (* expression 1 *)
> ptsa.{aa, bb, cc} (* expression 2 *)

Mathematica treats ptsa as a column vector (1x3) in expression 1, and
as a row vector (3x1) in expression 2.

Mark

Christopher Arthur

unread,
May 26, 2010, 7:08:32 AM5/26/10
to
try putting the vector in braces like this {{a},{b},{c}}
for a column vector into a row vector

S. B. Gray a =E9crit :
> Given
>
> ptsa == {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};


>
> I thought the following expressions would be identical:
>

> {aa, bb, cc}.ptsa (* expression 1 *)
> ptsa.{aa, bb, cc} (* expression 2 *)
>

inOr

unread,
May 26, 2010, 7:08:53 AM5/26/10
to

Steve,
The dot product is most definitely NON-commutative. To see this for a
case like yours, think of vector-matrix multiplication as the
transformation of a vector into a new vector. In real number
representation, pre-multiplication of a vector by a matrix creates a
new vector that is the sum of the column vectors of the matrix
weighted by the elements of the vector. In this case, the matrix
multiplication creates a column vector out of another column vector.
When the vector is post-multiplied by the matrix, the result is a
vector that is the sum of the ROW vectors of the matrix, still
weighted by the components of the vector. In this case, a row vector
is transformed into another row vector. Disregarding the row- /
column- difference, the two results are equal only if the rows and
columns of the matrix are identical (symmetric matrix).
Mark Harder

Barrie Stokes

unread,
May 26, 2010, 7:09:04 AM5/26/10
to
Hi Steve

>From the Mathematica documentation for Dot:
"The dimensions of the result are those of the input with the common dimension collapsed:"
There is then an example where an object with Dimensions "{2, 3, 4}" Dotted with an object with Dimensions "{4,5,2}" gives an object with dimensions "{2,3,5,2}".

ptsa = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}}

Dimensions @ ptsa

The matrix ptsa has dimensions 3*3.

Dimensions @ {aa, bb, cc}

{aa, bb, cc} is a list with dimension 3:

The product {aa, bb, cc}.ptsa is thus the dot product of a "3" and a "3*3" giving a result with dimension 3:

{aa, bb, cc}.ptsa (* expression 1 *)

Dimensions @ %

On the other hand, ptsa.{aa, bb, cc} is the dot product of a " 3*3" and a "3" giving (another) result with dimension 3:

ptsa.{aa, bb, cc} (* expression 2 *)

Dimensions @ %

Note that:
Dimensions @ Transpose[{{aa, bb, cc}} ]

Transpose[{{aa, bb, cc}} ] is a column vector with dimensions 3*1.

So, ptsa.Transpose[{{aa, bb, cc}} ] is the dot product of a " 3*3" and a "3*1" giving a result with dimension 3*1:

ptsa.Transpose[{{aa, bb, cc}} ] (* expression 3 *)
Dimensions @ %

Barrie

>>> On 25/05/2010 at 8:32 pm, in message <2010052510...@smc.vnet.net>, "S.

dh

unread,
May 26, 2010, 7:09:14 AM5/26/10
to
Hi Steve,
the "." product is defined as:
Sum[x1[[..,i]] x2[[i,..]],{i,1,n}]
that is the last index of x1 is contracted with the first index of x2.
From this it is clear that commutivity only exists if x1 and x2 are
cheers, Daniel


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:d...@metrohm.com>
Internet:<http://www.metrohm.com>


Bob Hanlon

unread,
May 26, 2010, 7:09:57 AM5/26/10
to

The dot product is not commutative in general and the examples in the Documentation Center for Dot explicitly show this (Examples/Basic Examples/Products of matrices and vectors).


Bob Hanlon

---- "S. B. Gray" <ste...@ROADRUNNER.COM> wrote:

=============

Roland Franzius

unread,
May 26, 2010, 7:10:18 AM5/26/10
to
S. B. Gray schrieb:

The first result is mathematically correct as a matrix product with a
left factor a (1x3) matrix and the right factor 3x3 matrix. Nevertheless
for working in the index spaces it is better to use {{aa,bb,cc}} for a
row vector

The second product is mathematically incorrect in the context of general
matrix multiplication because a matrix product of 3x3 . 1x3 does not
exist. but it is conveniently introduced for abuse of notation by lazy
physicists.

In the second product the right factor has to be a 3x1 matrix - or a
column vector - {{aa},{bb},{cc}} and the result has to be of the same type.

Try to
Transpose[{aa,bb,bb}]

No such problems with Transpose[{{aa,bb,bb}}]

--

Roland Franzius

Bill Rowe

unread,
May 26, 2010, 7:10:29 AM5/26/10
to
On 5/25/10 at 6:32 AM, ste...@ROADRUNNER.COM (S. B. Gray) wrote:

>Given

>ptsa = {{x1, y1, z1}, {x2, y2, z2}, {x3, y3, z3}};

>I thought the following expressions would be identical:

>{aa, bb, cc}.ptsa (* expression 1 *)
>ptsa.{aa, bb, cc} (* expression 2 *)

>but they are not.

>Since ptsa is itself three xyz coordinates, the expressions might be


>ambiguous, but I assumed the dot product would always commute.

The dot product of two *vectors* commutes. But you are
multiplying a matrix by a vector which does not commute in general

>Should there be a warning?

No. Until Mathematica is run on a machine with artificial
intelligence and has the capability of reading minds, there
isn't any way for Mathematica to determine the user expected a
result different from the correct mathematical result.


Mark Adler

unread,
May 27, 2010, 6:44:27 AM5/27/10
to
On 2010-05-26 04:10:18 -0700, Roland Franzius said:
> The second product is mathematically incorrect in the context of general
> matrix multiplication because a matrix product of 3x3 . 1x3 does not
> exist.

That vector does not have dimensions 1x3. Its dimensions are simply 3.
It is not a 2-dimensional matrix:

Dimensions[{1, 2, 3}]
{3}
Dimensions[{{1, 2, 3}}]
{1, 3}
Dimensions[{{1}, {2}, {3}}]
{3, 1}

Mathematica is being completely consistent on how those are treated for
an inner product, where simply the inner two dimensions must be equal
and are removed:

m = {{1, 2}, {3, 4}};
Dimensions[m]
{2, 2}
Dimensions[{1, 2}.m]
{2}
Dimensions[{{1, 2}}.m]
{1, 2}
Dimensions[m.{1, 2}]
{2}
Dimensions[m.{{1}, {2}}]
{2, 1}

Mathematica does complain about improper matrix products, when the
arguments are in fact matrices, e.g. 2x2 . 1x2:

m.{{1, 2}}
Dot::dotsh: Tensors {{1,2},{3,4}} and {{1,2}} have incompatible shapes.
{{1, 2}, {3, 4}}.{{1, 2}}

Mark

0 new messages