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
I know matrix will call the dot function of ndarray. However, that will not give the answer we expect. A 5*5 matrix multiplies another matrix, we expect answer to be error or a 5*? matrix, not a 1*5 matrix. As ticket 2649 said, I * I * x or I.dot(I.dot(x)) should be as same as I * x. But it will return an error because I and I.dot(x) are not aligned. On Wed, Apr 3, 2013 at 2:50 PM, Alan G Isaac <alan.isaac@gmail.com> wrote:
On 4/3/2013 2:44 PM, huangkandiy@gmail.com wrote:
I suggest add function dot to matrix
import numpy as np; x = np.arange(5); I = np.asmatrix(np.identity(5)); I.dot(x) matrix([[ 0., 1., 2., 3., 4.]])
Alan Isaac
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Kan Huang Department of Applied math & Statistics Stony Brook University 917-767-8018
On 4/3/2013 3:18 PM, huangkandiy@gmail.com wrote:
A 5*5 matrix multiplies another matrix, we expect answer to be error or a 5*? matrix, not a 1*5 matrix.
That is what happens. But you are post"multiplying" a matrix by a one-dimensional list. What should happen then? That is the question. One could argue that this should just raise an error, or that the result should be 1d. In my view, the result should be a 1d array, the same as I.A.dot(x). But the maintainers wanted operations with matrices to return matrices whenever possible. So instead of returning x it returns np.matrix(x). My related grievance is that I[0] is a matrix, not an array. There was a long discussion of this a couple years ago. Anyway, the bottom line is: don't mix matrices and other objects. The matrix object is really built only to interact with other matrix objects. Alan
On Wed, 2013-04-03 at 16:03 -0400, Alan G Isaac wrote:
On 4/3/2013 3:18 PM, huangkandiy@gmail.com wrote:
A 5*5 matrix multiplies another matrix, we expect answer to be error or a 5*? matrix, not a 1*5 matrix.
That is what happens. But you are post"multiplying" a matrix by a one-dimensional list. What should happen then? That is the question.
One could argue that this should just raise an error, or that the result should be 1d. In my view, the result should be a 1d array, the same as I.A.dot(x).
Would it be reasonable if this was a Nx1 matrix? I am not sure how you would implement it exactly into dot. Maybe by transposing the result if the second argument was a vector and the result is not a base class? And then __mul__ can use np.asarray instead of np.asmatrix. Or just fix __mul__ itself to transpose the result and don't care about np.dot? - Sebastian
But the maintainers wanted operations with matrices to return matrices whenever possible. So instead of returning x it returns np.matrix(x).
My related grievance is that I[0] is a matrix, not an array. There was a long discussion of this a couple years ago.
Anyway, the bottom line is: don't mix matrices and other objects. The matrix object is really built only to interact with other matrix objects.
Alan _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Wed, Apr 3, 2013 at 1:03 PM, Alan G Isaac <alan.isaac@gmail.com> wrote:
On 4/3/2013 3:18 PM, huangkandiy@gmail.com wrote:
In my view, the result should be a 1d array, the same as I.A.dot(x).
But the maintainers wanted operations with matrices to return matrices whenever possible. So instead of returning x it returns np.matrix(x).
the matrix object is a fine idea, but the key problem is that it provides a 2-d matrix, but no concept of a 1-d vector. I think it would all be a cleaner if there were a row-vector and column-vector object to accompany matrix -- they things that naturally return a vector could do so, You can't use a regular 1-d array because there is no way to distinguish between a row or column version. But as Alan sid, this was all hashed out a few years back -- a bunch of great ideas, but no one to implement them. The truth is that matrix has little value outside of teaching, so no one with the skills to push it forward uses it themselves. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Agree with the row-vector and column-vector thing. I notice that in ndarraymultiplication, the 1-d array is treated as a column-vector. But in matrix multiplication, 1-d array is converted to a row-vector. So just match the 1-d array to a column-vector, the behavior of ndarray and matrix will be consistent. On Wed, Apr 3, 2013 at 6:59 PM, Chris Barker - NOAA Federal < chris.barker@noaa.gov> wrote:
On Wed, Apr 3, 2013 at 1:03 PM, Alan G Isaac <alan.isaac@gmail.com> wrote:
On 4/3/2013 3:18 PM, huangkandiy@gmail.com wrote:
In my view, the result should be a 1d array, the same as I.A.dot(x).
But the maintainers wanted operations with matrices to return matrices whenever possible. So instead of returning x it returns np.matrix(x).
the matrix object is a fine idea, but the key problem is that it provides a 2-d matrix, but no concept of a 1-d vector. I think it would all be a cleaner if there were a row-vector and column-vector object to accompany matrix -- they things that naturally return a vector could do so, You can't use a regular 1-d array because there is no way to distinguish between a row or column version.
But as Alan sid, this was all hashed out a few years back -- a bunch of great ideas, but no one to implement them.
The truth is that matrix has little value outside of teaching, so no one with the skills to push it forward uses it themselves.
-Chris
--
Christopher Barker, Ph.D. Oceanographer
Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Kan Huang Department of Applied math & Statistics Stony Brook University 917-767-8018
On Wed, Apr 3, 2013 at 5:11 PM, huangkandiy@gmail.com <huangkandiy@gmail.com
wrote:
Agree with the row-vector and column-vector thing. I notice that in ndarray multiplication, the 1-d array is treated as a column-vector. But in matrix multiplication, 1-d array is converted to a row-vector. So just match the 1-d array to a column-vector, the behavior of ndarray and matrix will be consistent.
If someone is motivated to write some code it might not be too difficult to get it in. I don't know what the backwards compatibility problem would be, however. Maybe someone could put together a separate matrix package based on numpy for teaching purposes. <snip> Chuck
participants (6)
-
Alan G Isaac -
Charles R Harris -
Chris Barker - NOAA Federal -
Colin J. Williams -
huangkandiy@gmail.com -
Sebastian Berg