Should dot return 1x1 matrix as scalar?

Hello, I was playing with the matrix type in numpy and I felt the "need" to have the "dot" function returning a 1x1 matrix as a scalar. This would allow for expressions like: x = matrix(arange(10.)) versorX = x / sqrt(x.T*x) Right now, I have to write: versorX = x / sqrt(x.T*x).item() Actually, a.T*b can not always be used as the inner product with the current behavior. Note that dot already returns scalars when we multiply rank-1 arrays, but matrices are always rank-2. Best, Paulo

On Fri, 13 Jan 2006, "Paulo J. S. Silva" apparently wrote:
I was playing with the matrix type in numpy and I felt the "need" to have the "dot" function returning a 1x1 matrix as a scalar.
import numpy as N x = N.mat(range(10)) x matrix([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) x*x.T matrix([[285]]) 285*x matrix([[ 0, 285, 570, 855, 1140, 1425, 1710, 1995, 2280, 2565]]) (x*x.T)*(x.T*x) Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\Lib\site-packages\numpy\core\defmatrix.py", line 128, in __m
Since a 1x1 matrix is not a scalar, the current behavior is desirable, IMO. Specifically, I want an error in the last example below, since otherwise matrix multiplication will not be associative. Cheers, Alan Isaac PS Current behavior: Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. ul__ return N.dot(self, other) ValueError: objects are not aligned

I am sorry, but I don't agree with you. The matrix class seems to be there to help people with numerical linear algebra algorithms "a la matlab". Is this an incorrect assumption? The behavior I am asking can be restricted to the matrix class objects only. However as dot already makes a scalar out the multiplication of two rank-1 arrays (in which case it computes the inner product), I thought that this behavior could be extended to matrix objects. Another possibility (without touching dot) would be to change the functions __mul__, __rmul__, and __imul__ in the matrix class to deal with 1x1 matrices as scalars. That would be OK to me too. The current behavior is odd when writing numerical algorithms. Take a look at these two examples: 1) Computing the norm: The natural way: def norm(x): return sqrt(x.T*x) But in this case you can't write norm(v)*v! Then, you have then to define norm as def norm(x): return sqrt(x.T*x).item() 2) The conjugate gradient iteration: alpha = (r.T*r) / (p.T*A*p) x += alpha*p rold = rnew r += alpha*(A*p) beta = (r.T*r) / (rold.T*rold) p = -r + beta*p This returns an error on second, forth, and the last equation! In all these cases, you'll need a call to the "item" method. Finally, the example you gave (x*x.T)*(x.T*x) does not make sense as matrix multiplication formula unless you interpret the first part as a scalar. You seem to be happy that the compiler got "the mistake". But in any Mathematics book the formula above would be considered OK as the first part would be interpreted as an inner product (and the second an outer product). In my opinion, it is not a mistake in a linear algebra setting. My 2 cents, Paulo -- Paulo José da Silva e Silva Professor Assistente do Dep. de Ciência da Computação (Assistant Professor of the Computer Science Dept.) Universidade de São Paulo - Brazil e-mail: rsilva@ime.usp.br Web: http://www.ime.usp.br/~rsilva Teoria é o que não entendemos o (Theory is something we don't) suficiente para chamar de prática. (understand well enough to call practice)

On Fri, 13 Jan 2006, "Paulo J. S. Silva" wrote:
as dot already makes a scalar out the multiplication of two rank-1 arrays (in which case it computes the inner product), I thought that this behavior could be extended to matrix objects.
t matrix([[2]]) z matrix([[0, 0, 0], [0, 1, 2], [0, 2, 4]]) t+z matrix([[2, 2, 2], [2, 3, 4], [2, 4, 6]]) t*z Traceback (most recent call last): File "<stdin>", line 1, in ? File "C:\Python24\Lib\site-packages\numpy\core\defmatrix.py", line 128, in __m
Well, 'dot' for matrices is just a synonym for 'matrixmultiply', which it cannot be (in the same sense) for arrays. But I grant that I find it odd to enforce conformability for multiplication and not for addition. (See below.) I will also grant that GAUSS and Matlab behave as you wish, which might reflect a natural convenience and might reflect their impoverished types. Finally, I grant that I have not been able to quickly think up a use case where I want a 1x1 matrix for anything except error checking. Just to be clear, you do not want to get rid of 1x1 matrices, you just want to get rid of them as the result of *multiplication*, right. So [[1]]*[[2]]=2 but [[1]]+[[2]]=[[3]]. Right? And you would, I presume, find a 'scalar' function to be too clumsy. Cheers, Alan Isaac PS Conformability details: ul__ return N.dot(self, other) ValueError: objects are not aligned
t.A*z.A array([[0, 0, 0], [0, 2, 4], [0, 4, 8]])
participants (3)
-
Alan G Isaac
-
Alan Isaac
-
Paulo J. S. Silva