[Numpy-discussion] 2-D function and meshgrid

Paulo J. S. Silva pjssilva@ime.usp...
Fri Jan 9 20:10:37 CST 2009


Chuck,

Thanks, your version is much faster. I would prefer a solution that
doesn't force me to re-implement weirdDistance (as my two solutions
were). But the function is so simple that it is easier just to re-write
it for speed as you did.

By the way, I came out with one more solution that looks more Pythonic
and does not need to re-write weirdDistance (and hence can be used in
more complicated cases). It is also a tad faster than the fastest
solution from my first post:

Solution 3

    points = np.vstack( [x.ravel(), y.ravel()] ).T
    results = np.array([weirDistance(p) for p in points])
    return results.reshape(x.shape)

(This is basically solution 2 using list comprehensions to make to code
clearer)

best,

Paulo
> 
> Try
> 
> IDLE 1.2.4      
> >>> import numpy as np
> >>> pts = np.random.rand(5,2)
> >>> mat = np.random.rand(2,2)
> >>> res = (np.dot(pts,mat)*pts).sum(axis=1)
> >>> res
> 
> array([ 0.63018561,  0.30829864,  0.23173343,  1.79972127,
> 0.69498856])
> >>> for row in pts : np.dot(row,np.dot(mat,row))
> 
> 0.63018560596590589
> 0.30829864146737423
> 0.23173343333294744
> 1.7997212735553192
> 0.69498855520540959
> 
> Chuck
> 





More information about the Numpy-discussion mailing list