[Numpy-discussion] Should dot return 1x1 matrix as scalar?

Alan Isaac aisaac at american.edu
Fri Jan 13 11:08:03 CST 2006


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. 

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:

>>> 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
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]])
>>>






More information about the Numpy-discussion mailing list