From: Sven S. <sve...@gm...> - 2006-07-21 15:08:26
|
Hi, Summary: Slicing seems to be broken with matrices now. I eagerly installed the new beta, but soon stumbled over this bug. I hope I'm missing something, but afaics that behavior used to be different (and correct) before in 0.9.8. Don't know exactly when this changed, though. I did a fresh install (uninstalled old numpy and also matplotlib first) of the official binary for windows/python 2.4. Example: >>> import numpy as n >>> n.__version__ '1.0b1' >>> import numpy.matlib as m >>> a = n.zeros((2,3)) >>> b = m.zeros((2,3)) >>> a[:1,:].shape (1, 3) >>> b[:1,:].shape (3, 1) >>> Note the array slicing works correct, but the equivalent thing with the matrix does not. I also noticed the following (in a new python session) import strangeness: >>> import numpy >>> numpy.matlib.zeros((2,3)) Traceback (most recent call last): File "<interactive input>", line 1, in ? AttributeError: 'module' object has no attribute 'matlib' >>> Why is the direct access to matlib impossible? Either I'm missing something (I well may be, because I'm melting away at 36 centigrades or so...), or imho a new beta should be put out quickly to enable further testing (and use). Thanks, Sven |
From: Bill B. <wb...@gm...> - 2006-07-24 01:58:46
|
Howdy, On 7/22/06, Sven Schreiber <sve...@gm...> wrote: > Hi, > > Summary: Slicing seems to be broken with matrices now. > > ... > Example: > > >>> import numpy as n > >>> n.__version__ > '1.0b1' > >>> import numpy.matlib as m > >>> a = n.zeros((2,3)) > >>> b = m.zeros((2,3)) > >>> a[:1,:].shape > (1, 3) > >>> b[:1,:].shape > (3, 1) > >>> > > Note the array slicing works correct, but the equivalent thing with the > matrix does not. Looks like it happened in rev 2698 of defmatrix.py, matrix.__getitem__ method: if isscalar(index[1]): if isscalar(index[0]): retscal = True elif out.shape[0] == 1: sh = out.shape out.shape = (sh[1], sh[0]) ==> elif isinstance(index[1], (slice, types.EllipsisType)): ==> if out.shape[0] == 1 and not isscalar(index[0]): It behaves like array if you remove the 'not' in the last line. But maybe that breaks some other cases? Maybe you can try making that change in your numpy/core/defmatrix.py (around line 140) and see if anything else breaks for you. > I also noticed the following (in a new python session) import strangeness: > > >>> import numpy > >>> numpy.matlib.zeros((2,3)) > Traceback (most recent call last): > File "<interactive input>", line 1, in ? > AttributeError: 'module' object has no attribute 'matlib' > >>> > > Why is the direct access to matlib impossible? Maybe the thinking is that since it's a compatibily module, if you want it you should explicity import it. Like you have to do with oldnumeric. --bb |
From: Sven S. <sve...@gm...> - 2006-07-24 11:50:21
|
Thanks for helping out on matrix stuff, Bill! Bill Baxter schrieb: > On 7/22/06, Sven Schreiber <sve...@gm...> wrote: >> >> Note the array slicing works correct, but the equivalent thing with the >> matrix does not. > > Looks like it happened in rev 2698 of defmatrix.py, matrix.__getitem__ > method: > > if isscalar(index[1]): > if isscalar(index[0]): > retscal = True > elif out.shape[0] == 1: > sh = out.shape > out.shape = (sh[1], sh[0]) > ==> elif isinstance(index[1], (slice, types.EllipsisType)): > ==> if out.shape[0] == 1 and not isscalar(index[0]): > > It behaves like array if you remove the 'not' in the last line. > But maybe that breaks some other cases? > Maybe you can try making that change in your numpy/core/defmatrix.py > (around line 140) and see if anything else breaks for you. > Hm, I don't know -- if you don't mind I'd like to get a second opinion before I mess around there. It's funny though that the changeset has the title "fixing up matrix slicing" or something like that... >> >> Why is the direct access to matlib impossible? > > Maybe the thinking is that since it's a compatibily module, if you > want it you should explicity import it. Like you have to do with > oldnumeric. > If that is the reason I can't really follow the logic, but I don't really mind the status quo, either. -sven |
From: Travis O. <oli...@ie...> - 2006-07-24 19:55:33
|
Sven Schreiber wrote: > Thanks for helping out on matrix stuff, Bill! > > > Hm, I don't know -- if you don't mind I'd like to get a second opinion > before I mess around there. It's funny though that the changeset has the > title "fixing up matrix slicing" or something like that... > The change was trying to fix up some cases but did break this one. The problem is that figuring out whether or not to transpose the result is a bit tricky. I've obviously still got it wrong. -Travis |
From: Sven S. <sve...@gm...> - 2006-07-24 20:21:24
|
Travis Oliphant schrieb: > Sven Schreiber wrote: >> > The change was trying to fix up some cases but did break this one. The > problem is that figuring out whether or not to transpose the result is a > bit tricky. I've obviously still got it wrong. > Ok, this is obviously one of the places were an automated test would be helpful to avoid regressions. In the beta months ahead, I would like to help with contributing such tests. However, since I'm not a developer, I have little to no experience with that. Is there any numpy-specific documentation or guide on how to add tests? (I know there's plenty of guides on the general python testing framework.) Or any brilliant example in the distribution that should be followed? Regards, Sven p.s.: Is there going to be a new beta soon? Or is there a quick fix to this slicing problem in the meantime? Thanks. |
From: Bill B. <wb...@gm...> - 2006-07-24 23:49:14
|
On 7/25/06, Sven Schreiber <sve...@gm...> wrote: > Travis Oliphant schrieb: > > Sven Schreiber wrote: > >> > > The change was trying to fix up some cases but did break this one. The > > problem is that figuring out whether or not to transpose the result is a > > bit tricky. I've obviously still got it wrong. Is there some test case that demonstrates the problem it was intended to fix? Or can you remember off the top of your head? I tried as many things as I could think of but nothing I tried gave the wrong answer with the fix of removing the 'not' on line 140. > p.s.: Is there going to be a new beta soon? Or is there a quick fix to > this slicing problem in the meantime? Thanks. Sven, if you were happy with the behavior under numpy 0.9.8 then all you have to do is just cut and paste its matrix.__getitem__ method into your current site-packages/numpy/core/defmatrix.py. --bill |