Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

For example, this input...

{{1, 2}, {3, 4}}*{{1, 2}, {3, 4}}

produces this output...

{{1, 4}, {9, 16}}

and this input...

{{1, 2}, {3, 4}}^2

produces the same...

{{1, 4}, {9, 16}}

What I want in both cases is...

{{7, 10}, {15, 22}}

I think I know what's going on here. Mathematica doesn't seem to be discriminating between lists of lists, and matrices, and the * and ^ operators are just threading over the lists.

I've found . and MatrixPower which do what I think * and ^ should.

But the question still remains, is this confusing (to at least me) behavior of * and ^ by design, and what benefit does it confer?

share|improve this question
10  
It's a common question from users used to matrix focused systems like Matlab and NumPy - but saying "wrong thing" in the title does prejudice the question a little... – Simon Jan 24 '12 at 1:11
@Simon, good call. I've changed the name of the question to emphasize that it's merely my expectations being violated. – Harold Jan 24 '12 at 14:47

2 Answers

up vote 13 down vote accepted

Matrices in Mathematica are nothing but a specific type of list of lists — specifically, a two dimensional list of lists.

* is the short form for the Times function, which threads over lists elementwise, and this is what you'd use if you wanted to take the Hadamard product of two matrices. So when you say A*B, you're actually saying Times[A, B].

. on the other hand, is short form for Dot, which lets you take the usual matrix products. So A.B is equivalent to Dot[A, B]. Both of these are different and it just boils down to understanding and remembering the short forms and the functions they represent.

If you're coming from a language like MATLAB, you might be confused at first, because * and ^ indeed do behave the way you described in that language. Although one should familiarize themselves with each language's differences, this might help you in remembering it — * and ^ behave exactly like .* and .^ respectively in MATLAB, in that they operate element wise.

Whether it is intuitive or not depends on your personal preferences (and experience with other languages). In the same vein, you could also ask why Infix is ~, when MATLAB treats it as the not operator or throwaway variable, depending on how you use it :)

share|improve this answer
1  
For the MATLAB people: * and ^ in Mathematica act like, respectively, .* and .^ in MATLAB (in MATLAB parlance, they are array operators and not matrix operators); this is because * and ^ have the Listable attribute. – 0x4A4D Jan 24 '12 at 15:16

This is by design, because a list of list does not necessarily describe a matrix. Operations like addition, multiplication and power spread over lists without caring what those lists contain. Note that if this would not be so, it might lead to very strange behaviour:

list = {a,a}
lsq = list*list
(*
hypothetical result: 2*a^2
*)
a = {1,1}
lsq
(*
hypothetical result: 4 (because the scalar product of (1,1) with itself is 2)
*)

but

list = {a,a}
a = {1,1} (*now list evaluates to {{1,1},{1,1}}*)
lsq = list*list
(*
hypothetical result: {{2,2},{2,2}} (the square of the matrix {{1,1},{1,1}})
*)

With the current rules both give the same result (namely {{1,1},{1,1}}).

Moreover, the standard multiplication is commutative, while matrix multiplication is not. Therefore you'd get wrong results with expressions like A*B*A^-1 where A and B are intended to be matrices, but are symbols.Mathematica could not know that you intend them to be matrices, and therefore it would simplify this to B, which in general would be wrong if A and B are matrices.

The only way to solve this while using * for matrix multiplication would be to have a type system where you can define a symbol to stand for a certain type. But that's not the way Mathematica works.

share|improve this answer
Good points. The only way to get them to work that way, would be to construct objects using UpValues. It's just easier to use Dot and be done with it. – rcollyer Jan 24 '12 at 3:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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