[Numpy-discussion] Optimize evaluation of function on matrix
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
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:
> 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)
> mgrid = np.meshgrid(meshB, meshA)
> A = self.basisfunction( np.abs(mgrid - mgrid) )
> 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
> How can I optimize eval_BF? Esp. in the case of basisfunction being a list.
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion