From: Pau G. <pau...@gm...> - 2006-05-16 13:55:48
|
On 5/1/06, Pierre Barbier de Reuille <p.b...@ue...> wrote= : > Hello, > > I notices a bug in ndarray.argmax which prevent from getting the argmax > from any axis but the last one. > I join a patch to correct this. > Also, here is a small python code to test the behaviour of argmax I > implemented : > > =3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D= =3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D8<=3D=3D=3D > > from numpy import array, random, all > > a =3D random.normal( 0, 1, ( 4,5,6,7,8 ) ) > for i in xrange( a.ndim ): > amax =3D a.max( i ) > aargmax =3D a.argmax( i ) > axes =3D range( a.ndim ) > axes.remove( i ) > assert all( amax =3D=3D aargmax.choose( *a.transpose( i, *axes ) ) ) > > =3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D= =3D8<=3D=3D=3D=3D8<=3D=3D=3D=3D8<=3D=3D=3D8<=3D=3D=3D > > Pierre > > > diff numpy-0.9.6/numpy/core/src/multiarraymodule.c numpy-0.9.6.mod/numpy/= core/src/multiarraymodule.c > 1952a1953,1955 > > If orign > ap->nd, then we cannot "swap it back" > > as the dimension does not exist anymore. It means > > the axis must be put back at the end of the array. > 1956c1959,1979 > < (op) =3D (PyAO *)PyArray_SwapAxes((ap), axis, ori= gn); \ > --- > > int nb_dims =3D (ap)->nd; = \ > > if (orign > nb_dims-1 ) { \ > > PyArray_Dims dims; \ > > int i; \ > > dims.ptr =3D ( intp* )malloc( sizeof( int= p )*nb_dims );\ > > dims.len =3D nb_dims; = \ > > for(i =3D 0 ; i < axis ; ++i) = \ > > { \ > > dims.ptr[i] =3D i; = \ > > } \ > > for(i =3D axis ; i < nb_dims-1 ; ++i) = \ > > { \ > > dims.ptr[i] =3D i+1; = \ > > } \ > > dims.ptr[nb_dims-1] =3D axis; = \ > > (op) =3D (PyAO *)PyArray_Transpose((ap), = &dims ); \ > > } \ > > else \ > > { \ > > (op) =3D (PyAO *)PyArray_SwapAxes((ap), a= xis, orign); \ > > } \ > > > The bug seems to be still there in the current svn version, so I filled out a ticket for this. Is the first time i do such a thing, so someone competent should _please_ take a look at it. Thanks, and sorry in advance if i did something wrong. pau |