[Numpy-discussion] Optimizing a pure Python Workaround

Ian Mallett geometrian at gmail.com
Sat Jul 18 14:56:03 EDT 2009


Hi,

Sorry, I've been away in Oregon...

The result isn't quite the same.  The arrays must be in the range [0,1], so
I just have it divide x3 and y.  I also have it add 1 to size[1], as I
realized that was also necessary for that behavior:

x = np.arange(size[0])
x2 = np.column_stack([x,x+1]).reshape([-1,1])
x3 = np.array(x2.repeat(size[1]+1,axis=1).flatten(),"f")
y = np.array(np.arange(size[1]+1).repeat(size[0]*2),"f")
array = np.zeros([len(y),3])
array[:,0] = x3/size[0]
array[:,1] = y/size[1]
array = np.array(array,"f")

When size is [3,2], the result from this code is:

[[ 0.          0.          0.        ]
 [ 0.          0.          0.        ]
 [ 0.          0.          0.        ]
 [ 0.33333334  0.          0.        ]
 [ 0.33333334  0.          0.        ]
 [ 0.33333334  0.          0.        ]
 [ 0.33333334  0.5         0.        ]
 [ 0.33333334  0.5         0.        ]
 [ 0.33333334  0.5         0.        ]
 [ 0.66666669  0.5         0.        ]
 [ 0.66666669  0.5         0.        ]
 [ 0.66666669  0.5         0.        ]
 [ 0.66666669  1.          0.        ]
 [ 0.66666669  1.          0.        ]
 [ 0.66666669  1.          0.        ]
 [ 1.          1.          0.        ]
 [ 1.          1.          0.        ]
 [ 1.          1.          0.        ]]

The correct output is:

[[ 0.          0.          0.        ]
 [ 0.33333334  0.          0.        ]
 [ 0.          0.5         0.        ]
 [ 0.33333334  0.5         0.        ]
 [ 0.          1.          0.        ]
 [ 0.33333334  1.          0.        ]
 [ 0.33333334  0.          0.        ]
 [ 0.66666669  0.          0.        ]
 [ 0.33333334  0.5         0.        ]
 [ 0.66666669  0.5         0.        ]
 [ 0.33333334  1.          0.        ]
 [ 0.66666669  1.          0.        ]
 [ 0.66666669  0.          0.        ]
 [ 1.          0.          0.        ]
 [ 0.66666669  0.5         0.        ]
 [ 1.          0.5         0.        ]
 [ 0.66666669  1.          0.        ]
 [ 1.          1.          0.        ]]

Thanks,
Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090718/fbd8728a/attachment.html>


More information about the NumPy-Discussion mailing list