[Numpy-discussion] Optimize evaluation of function on matrix
sebastian at sipsolutions.net
Sat Mar 25 17:31:12 EDT 2017
On Sat, 2017-03-25 at 18:46 +0100, Florian Lindner 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
> 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
Are you sure you need to optimize it? If they have a couple of hundred
elements or so for each row, the math is probably the problem and most
of that might be the `exp`.
You can get rid of the `row` loop though in case row if an individual
row is a pretty small array.
To be honest, I am a bit surprised that its a problem, since "basis
function" sounds a bit like you have to do this once and then use the
result many times.
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 801 bytes
Desc: This is a digitally signed message part
More information about the NumPy-Discussion