[Numpy-discussion] Optimize evaluation of function on matrix

Florian Lindner mailinglists at xgm.de
Sat Mar 25 13:46:52 EDT 2017


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


More information about the NumPy-Discussion mailing list