[Numpy-discussion] Optimize evaluation of function on matrix

Matthew Harrigan harrigan.matthew at gmail.com
Mon Mar 27 20:19:24 EDT 2017


The best way to get good optimized code is to find it.  Does this do what
you want?

https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.Rbf.html

For some advice here, first avoid loops in Python and instead stick to
ufuncs and broadcasting.  It looks like the matrix is symmetric with
constant diagonals, so much of the work can possibly be avoided.  Finally
consider threading over blocks or different basis functions.  To dial it up
to 11 write low level c code to do exactly what you want, writing a custom
ufunc or call it cython.

On Mar 25, 2017 2:03 PM, "Florian Lindner" <mailinglists at xgm.de> wrote:

> Hello,
>
> I have this function:
>
> def eval_BF(self, meshA, meshB):
>         """ Evaluates single BF or list of BFs on the meshes. """
>         if type(self.basisfunction) is list:
>             A = np.empty((len(meshA), len(meshB)))
>             for i, row in enumerate(meshA):
>                 for j, col in enumerate(meshB):
>                     A[i, j] = self.basisfunction[j](row - col)
>         else:
>             mgrid = np.meshgrid(meshB, meshA)
>             A = self.basisfunction( np.abs(mgrid[0] - mgrid[1]) )
>         return A
>
>
> meshA and meshB are 1-dimensional numpy arrays. self.basisfunction is e.g.
>
> def Gaussian(radius, shape):
>     """ Gaussian Basis Function """
>     return np.exp( -np.power(shape*abs(radius), 2))
>
>
> or a list of partial instantations of such functions (from
> functools.partial).
>
> How can I optimize eval_BF? Esp. in the case of basisfunction being a list.
>
> Thanks!
> Florian
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170327/e391cde4/attachment-0001.html>


More information about the NumPy-Discussion mailing list