Re: [Numpy-discussion] matrix wart
On Thu, Feb 21, 2008 at 07:10:24PM -0500, Alan G Isaac wrote: On Thu, Feb 21, 2008 at 12:08:32PM -0500, Alan G Isaac wrote: a matrix behavior that I find bothersome and unnatural:: M = N.mat('1 2;3 4') M[0] matrix([[1, 2]]) M[0][0] matrix([[1, 2]]) On Fri, 22 Feb 2008, Stefan van der Walt apparently wrote: This is exactly what I would expect for matrices: M[0] is the first row of the matrix. Define what first row means! There is no standard definition that says this is means the **submatrix** that can be created from the first row. Someone once pointed out on this list that one might consider a matrix to be a container of 1d vectors. For NumPy, however, it is natural that it be a container of 1d arrays. (See the discussion for the distinction.) Could you explain to me how you'd like this to be fixed? If the matrix becomes a container of 1-d arrays, then you can no longer expect x[:,0] to return a column vector -- which was one of the reasons the matrix class was created. While not entirely consistent, one workaround would be to detect when a matrix is a vector, and then do 1-d-like indexing on it. You expect this matrix behavior only from experience with it, which is why I expect it too, while hating it. No, really, I don't ever use the matrix class :) But it is not like the behaviour is set in stone, so I would spend less time hating and more time patching. The example really speaks for itself. Since Konrad is an extremely experienced user/developer, his reaction should speak volumes. Of course, I meant no disrespect to Konrad. I'm just trying to understand the best way to address your concern. Regards Stefan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On Thu, Feb 21, 2008 at 12:08:32PM -0500, Alan G Isaac wrote: a matrix behavior that I find bothersome and unnatural:: M = N.mat('1 2;3 4') M[0] matrix([[1, 2]]) M[0][0] matrix([[1, 2]]) On Fri, 22 Feb 2008, Stefan van der Walt apparently wrote: Could you explain to me how you'd like this to be fixed? If the matrix becomes a container of 1-d arrays, then you can no longer expect x[:,0] to return a column vector -- which was one of the reasons the matrix class was created. While not entirely consistent, one workaround would be to detect when a matrix is a vector, and then do 1-d-like indexing on it. Letting M be a matrix and A=M.A, and i and j are integers. I would want two principles to be honored. 1. ordinary Python indexing produces unsurprising results, so that e.g. M[0][0] returns the first element of the matrix 2. indexing that produces a 2-d array when applied to A will produce the equivalent matrix when applied to M There is some tension between these two requirements, and they do not address your specific example. Various reconciliations can be imagined. I believe a nice one can be achieved with a truly minimal change, as follows. Let M[i] return a 1d array. (Unsurprising!) This is a change: a matrix becomes a container of arrays (e.g., when iterating). Let M[:,i] and M[i,:] behave as now. In addition, as a consistency measure, one might ask that M[i,j] return a 1 x 1 matrix. (This is of secondary importance, but it follows the principle that the use of multiple indexes produces matrices.) Right now I'm operating on caffeine instead of sleep, but that looks right ... Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Konrad Hinsen wrote: On 22.02.2008, at 01:10, Alan G Isaac wrote: Someone once pointed out on this list that one might consider a matrix to be a container of 1d vectors. For NumPy, however, it is natural that it be a container of 1d arrays. (See the discussion for the distinction.) If I were to design a Pythonic implementation of the mathematical concept of a matrix, I'd implement three classes: Matrix, ColumnVector, and RowVector. It would work like this: m = Matrix([[1, 2], [3, 4]]) m[0, :] -- ColumnVector([1, 3]) m[:, 0] -- RowVector([1, 2]) These seem backward to me.I would think that m[0,:] would be the RowVector([1,2]) and m[:,0] be the ColumnVector([1,3]). m[0,0] -- 1 # scalar m.shape -- (2, 2) m[0].shape -- (2,) What is m[0] in this case? The same as m[0, :]? However, the matrix implementation in Numeric was inspired by Matlab, where everything is a matrix. But as I said before, Python is not Matlab. It should be kept in mind, however, that Matlab's matrix object is used successfully by a lot of people and should not be dismissed as irrelevant. I would like to see an improved Matrix object as a built-in type (for 1.1). I am aware of two implementations that could be referred to in creating it: CVXOPT's matrix object and NumPy's matrix object. There may be others as well. If somebody has strong feelings about this sufficient to write a matrix built-in, then the door is wide open. Best, -Travis ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Could you explain to me how you'd like this to be fixed? If the matrix becomes a container of 1-d arrays, then you can no longer expect x[:,0] to return a column vector -- which was one of the reasons the matrix class was created. While not entirely consistent, one workaround would be to detect when a matrix is a vector, and then do 1-d-like indexing on it. Letting M be a matrix and A=M.A, and i and j are integers. I would want two principles to be honored. 1. ordinary Python indexing produces unsurprising results, so that e.g. M[0][0] returns the first element of the matrix 2. indexing that produces a 2-d array when applied to A will produce the equivalent matrix when applied to M There is some tension between these two requirements, and they do not address your specific example. Various reconciliations can be imagined. I believe a nice one can be achieved with a truly minimal change, as follows. Let M[i] return a 1d array. (Unsurprising!) This is a change: a matrix becomes a container This is a concrete proposal and I don't immediately have a problem with it (other than it will break code and so must go in to 1.1). Let M[:,i] and M[i,:] behave as now. Some would expect M[i,:] and M[i] to be the same thing, but I would be willing to squelsh those expectations if many can agree that M[i] should return an array. In addition, as a consistency measure, one might ask that M[i,j] return a 1 x 1 matrix. (This is of secondary importance, but it follows the principle that the use of multiple indexes produces matrices.) I'm pretty sure that wasn't the original principle, but again this is not unreasonable. Right now I'm operating on caffeine instead of sleep, but that looks right ... Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On Fri, 22 Feb 2008, Stefan van der Walt apparently wrote: This is exactly what I would expect for matrices: M[0] is the first row of the matrix. Define what first row means! Konrad has shown that do get it right you really have to introduce three separate things (matrices, row vectors, and column vectors). This is a fine direction to proceed in, but it does complicate things as well. The current implementation has the advantage that row vectors are just 1xN matrices and column vectors are Nx1 matrices, so there is only 1 kind of thing: matrices. The expectation that M[0][0] and M[0,0] return the same thing stems from believing that all objects using [] syntax are just containers. (Think of a dictionary with keys, '0', and '(0,0)' for an example). The matrix object is not a container object. A NumPy array, however, is. They have different behaviors, on purpose. If you don't like the matrix object, then just use the NumPy array. There are situations, however, when the matrix object is very useful. I use it in limited fashion to make expressions easier to read. Imagine if a 2d array behaved this way. Ugh! Note that it too is 2d; you could have the same expectation based on its 2d-ness. Why don't you? The 2d-ness is not the point. The point is that a matrix object is a matrix object and *not* a generic container. Nobody has objected to returning matrices when getitem is fed multiple arguments: these are naturally interpreted as requests for submatrices. M[0][0] and M[:1,:1] are very different kinds of requests: the first should return the 0,0 element but does not, while M[0,0] does! Bizarre! How to guess?? If you teach, do your students expect this behavior? Mine don't! Again, stop believing that M[0][0] and M[0,0] should return the same thing. There is nothing in Python that requires this. As far as I know, the matrix object is consistent. It may not behave as you, or people that you teach, would expect, but it does have reasonable behavior.Expectations are generally learned based on previous experience. Our different experiences will always lead to different expectations. What somebody expects for a matrix behavior will depend on how they were taught what it means to be a matrix. This is a wart. I disagree. It's not a wart, it is intentional. The example really speaks for itself. Since Konrad is an extremely experienced user/developer, his reaction should speak volumes. I'm not as convinced by this kind of argument. I respect Konrad a great deal and am always interested to hear his opinion, and make use of all of the code that he shares with us. His example has been an important part of my Python education. However, we do approach problems differently (probably again based on previous experiences) which leads us to promote different solutions.I also see this in the wider Python community where there is a diversity of user/developers who promote different approaches as well (e.g. the PIL vs NumPy concept of Images comes to mind as well). I've heard many differing points of view on the Matrix object. Stefan's comment is most relevant: the Matrix object can be changed (in 1.1), especially because we are keen on merging CVXOPT's matrix object with NumPy's and making it a builtin type. -Travis O. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On Fri, 22 Feb 2008, Travis E. Oliphant apparently wrote: The point is that a matrix object is a matrix object and not a generic container. I see the point a bit differently: there are costs and benefits to the abandonment of a specific and natural behavior of containers. (The kind of behavior that arrays have.) The costs outweigh the benefits. stop believing that M[0][0] and M[0,0] should return the same thing. There is nothing in Python that requires this. I never suggested there is. My question how to guess? does not imply that. My point is: the matrix object could have more intuitive behavior with no loss of functionality. Or so it seems to me. See my other post. Cheers, Alan ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Alan G Isaac wrote: stop believing that M[0][0] and M[0,0] should return the same thing. There is nothing in Python that requires this. I never suggested there is. My question how to guess? does not imply that. My point is: the matrix object could have more intuitive behavior with no loss of functionality. Do I understand correctly, that by intuitive you mean based on experience with lists, and NumPy arrays?I agree, it is very valuable to be able to use previous understanding to navigate a new thing. That's a big part of why I could see changing the matrix object in 1.1 to behave as you described in your previous post: where M[i] returned a 1-d array and matrices were returned with 2-d (slice-involved) indexing (I would not mind M[0,0] to still return a scalar, however). -Travis ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On Fri, 22 Feb 2008, Travis E. Oliphant apparently wrote: Do I understand correctly, that by intuitive you mean based on experience with lists, and NumPy arrays? Yes. In particular, array behavior is quite lovely and almost never surprising, so matrices should deviate from it only when there is an adequate payoff and, ideally, an easily stated principle. Thanks! Alan PS If you choose to implement such changes, I would find M[0,0] returning a 1×1 matrix to be more consistent, but to be clear, for me this is *very* much a secondary issue. Not even in the same ballpark. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Travis E. Oliphant wrote: to behave as you described in your previous post: where M[i] returned a 1-d array My thoughts on this: As Konrad suggested, row vectors and column vectors are different beasts ,and both need to be easily and intuitively available. M[i] returning a 1-d array breaks this -- that's what raw numpy arrays do, and I like it, but it's not so natural for linear algebra. If we really want to support matrixes, then no, M[i] should not return a 1-d array -- what is a 1-d array mean in the matrix/linear algebra context? It makes me think that M[i] should not even be possible, as you would always want one of: row vector: M[i,:] column vector: M[:,i] element: M[i,j] I do like the idea of a row/column vectors being different objects than matrices, then you could naturally index the elements from them. If you really want a 1-d array, you can always do: M.A[i] What if you want to naturally iterate through all the rows, or all the columns? what about: for row in M.rows for column in M.columns M.rows and M.columns would be iterators. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Alan G Isaac wrote: I propose that the user-friendly question is: why deviate needlessly from array behavior? (Needlessly means: no increase in functionality.) On Fri, 22 Feb 2008, Christopher Barker apparently wrote: because that's the whole point of a Matrix object in the first place. Do you really believe that? As phrased?? (Out of curiosity: do you use matrices?) On Fri, 22 Feb 2008, Christopher Barker apparently wrote: Functionally, you can do everything you need to do with numpy arrays. That is a pretty narrow concept of functionality, which excludes all user convenience aspects. I do not understand why you are introducing it; it seems irrelevant. If you push this line of reasoning, you should just tell me I can do it all in C. On Fri, 22 Feb 2008, Christopher Barker apparently wrote: The only reason there is a matrix class is to create a more natural, and readable way to do linear algebra. That's why the current version always returns matrices -- people don't want to have to keep converting back to matrices from arrays. You are begging the question. Of course we want to be able to conveniently extract submatrices and build new matrices. Nobody has challenged that or proposed otherwise. Or are you complaining that you would have to type M[i,:] instead of M[i]? (No, that cannot be; you were proposing that M[i] be an error...) Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
Alan G Isaac wrote: On Fri, 22 Feb 2008, Christopher Barker apparently wrote: because that's the whole point of a Matrix object in the first place. Do you really believe that? As phrased?? yes -- the matrix object is about style, not functionality -- not that style isn't important (Out of curiosity: do you use matrices?) No. In fact, that's one of the reasons I was overjoyed to find Numeric after using Matlab for along time -- I hardly ever need linear algebra, what I need is n-d arrays. So, yes, I should just shut up and leave the discussion to those that really want to use them. I will note, however, that in reading this list for years, I haven't found that many people really do want matrices -- they are asked for a lot by Matlab converts, then often the users find that they can more easily do what they want with arrays after all. Maybe that's because the Matrix API needs improvement, so I guess what we really need is someone that really wants them to champion the cause. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/ORR/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On Thu, Feb 21, 2008 at 12:08:32PM -0500, Alan G Isaac wrote: a matrix behavior that I find bothersome and unnatural:: M = N.mat('1 2;3 4') M[0] matrix([[1, 2]]) M[0][0] matrix([[1, 2]]) On Fri, 22 Feb 2008, Stefan van der Walt apparently wrote: This is exactly what I would expect for matrices: M[0] is the first row of the matrix. Define what first row means! There is no standard definition that says this is means the **submatrix** that can be created from the first row. Someone once pointed out on this list that one might consider a matrix to be a container of 1d vectors. For NumPy, however, it is natural that it be a container of 1d arrays. (See the discussion for the distinction.) Imagine if a 2d array behaved this way. Ugh! Note that it too is 2d; you could have the same expectation based on its 2d-ness. Why don't you? You expect this matrix behavior only from experience with it, which is why I expect it too, while hating it. It is not what new users will expect and also not desirable. As Konrad noted, it is very odd behavior to treat a matrix as a container of matrices. You can only expect this behavior by learning to expect it (by use), which is undesirable. Nobody has objected to returning matrices when getitem is fed multiple arguments: these are naturally interpreted as requests for submatrices. M[0][0] and M[:1,:1] are very different kinds of requests: the first should return the 0,0 element but does not, while M[0,0] does! Bizarre! How to guess?? If you teach, do your students expect this behavior? Mine don't! This is a wart. The example really speaks for itself. Since Konrad is an extremely experienced user/developer, his reaction should speak volumes. Cheers, Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] matrix wart
On 22.02.2008, at 01:10, Alan G Isaac wrote: Someone once pointed out on this list that one might consider a matrix to be a container of 1d vectors. For NumPy, however, it is natural that it be a container of 1d arrays. (See the discussion for the distinction.) If I were to design a Pythonic implementation of the mathematical concept of a matrix, I'd implement three classes: Matrix, ColumnVector, and RowVector. It would work like this: m = Matrix([[1, 2], [3, 4]]) m[0, :] -- ColumnVector([1, 3]) m[:, 0] -- RowVector([1, 2]) m[0,0] -- 1 # scalar m.shape -- (2, 2) m[0].shape -- (2,) However, the matrix implementation in Numeric was inspired by Matlab, where everything is a matrix. But as I said before, Python is not Matlab. Konrad. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion