[Numpy-discussion] problems with multiple outputs with numpy.nditer
Mark Wiebe
mwwiebe@gmail....
Wed Aug 10 11:15:48 CDT 2011
On Wed, Aug 10, 2011 at 3:45 AM, George Nurser <gnurser@gmail.com> wrote:
> Hi,
> I'm running numpy 1.6.1rc2 + python 2.7.1 64-bit from python.org on OSX
> 10.6.8.
>
> I have a f2py'd fortran routine that inputs and outputs fortran real*8
> scalars, and I normally call it like
>
> tu,tv,E,El,IF,HF,HFI = LW.rotate2u(u,v,NN,ff,0)
>
> I now want to call it over 2D arrays UT,VT,N,f
>
> Using steam-age indexing works fine:
>
> mflux_east,mflux_north,IWE,IWE_lin,InvFr,HFroude =
> np.empty([6,ny-1,nx],dtype=np.float64)
> for j in range(ny-1):
> for i in range(nx):
> u,v,NN,ff = [x[j,i] for x in UT,VT,N,f]
>
> mflux_east[j,i],mflux_north[j,i],IWE[j,i],IWE_lin[j,i],InvFr[j,i],HFroude[j,i],HFI
> = LW.rotate2u(u,v,NN,ff,0)
>
>
>
> I decided to try the new nditer option, with
>
> it = np.nditer([UT,VT,N,f,None,None,None,None,None,None,None]
> ,op_flags=4*[['readonly']]+7*[['writeonly','allocate']]
> ,op_dtypes=np.float64)
> for (u,v,NN,ff,tu,tv,E,El,IF,HF,HFI) in it:
> tu,tv,E,El,IF,HF,HFI = LW.rotate2u(u,v,NN,ff,0)
>
>
> Unfortunately this doesn't seem to work. Writing
> aa,bb,cc,dd,ee,ff,gg = it.operands[4:]
>
One problem here is that the assignment needs to assign into the view the
iterator gives, something a direct assignment doesn't actually do. Instead
of
a, b = f(c,d)
you need to write it like
a[...], b[...] = f(c,d)
so that the actual values being iterated get modified. Here's what I get:
In [7]: a = np.arange(5.)
In [8]: b, c, d = a + 1, a + 2, a + 3
In [9]: it = np.nditer([a,b,c,d] + [None]*7,
...: op_flags=4*[['readonly']]+7*[['writeonly','allocate']],
...: op_dtypes=np.float64)
In [10]: for (x,y,z,w,A,B,C,D,E,F,G) in it:
....: A[...], B[...], C[...], D[...], E[...], F[...], G[...] = x, y,
z, w, x+y, y+z, z+w
....:
In [11]: it.operands[4]
Out[11]: array([ 0., 1., 2., 3., 4.])
In [12]: it.operands[5]
Out[12]: array([ 1., 2., 3., 4., 5.])
In [13]: it.operands[6]
Out[13]: array([ 2., 3., 4., 5., 6.])
In [14]: it.operands[7]
Out[14]: array([ 3., 4., 5., 6., 7.])
In [15]: it.operands[8]
Out[15]: array([ 1., 3., 5., 7., 9.])
In [16]: it.operands[9]
Out[16]: array([ 3., 5., 7., 9., 11.])
In [17]: it.operands[10]
Out[17]: array([ 5., 7., 9., 11., 13.])
-Mark
>
> aa seems to contain the contents of UT (bizarrely rescaled to lie
> between 0 and 1), while bb,cc etc are all zero.
>
>
> I'm not sure whether I've just called it incorrectly, or whether
> perhaps it's only supposed to work with one output array.
>
>
> --George Nurser.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110810/bc48679f/attachment.html
More information about the NumPy-Discussion
mailing list