[Numpy-discussion] try to solve issue #2649 and revisit #473

huangkandiy@gmai... huangkandiy@gmai...
Wed Apr 3 13:44:23 CDT 2013


Hello, all

I try to solve issue 2649 which is related to 473 on multiplication of a
matrix and an array. As 2649 shows

   import numpy as np
   x = np.arange(5)
   I = np.asmatrix(np.identity(5))
   print np.dot(I, x).shape
   # -> (1, 5)

First of all I assume we expect that I.dot(x) and I * x behave the same, so
I suggest add function dot to matrix, like

def dot(self, other):
      return self * other

Then the major issue is the constructor of array and matrix interpret a
list differently. array([0,1]).shape = (2,)  and matrix([0,1]).shape = (1,
2). It will throw error when run np.dot(I, x), because in __mul__, x will
be converted to a 1*5 matrix first. It's not consistent with
np.dot(np.identity(5),
x), which returns x. To fix that, I suggest to check the dimension of array
when convert it to matrix. If it's 1D array, then convert it to a vertical
vector explicitly like this

         if isinstance(data, N.ndarray):
+           if len(data.shape) == 1:
+               data = data.reshape(data.shape[0], 1)
             if dtype is None:
                 intype = data.dtype
             else:

Any comments?

-- 
Kan Huang
Department of Applied math & Statistics
Stony Brook University
917-767-8018
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130403/9ff81ba4/attachment.html 


More information about the NumPy-Discussion mailing list