0

I have a function that I would like to apply on each element of a cartesian product of a linear space. I do know how to do it with functions of one variable that is defined using lambda, namely using map. Here is an example:

import numpy as np
xpts = np.linspace(0, 1, 5)
fun = lambda p: p**2
arr = np.array(list(map(fun , xpts)))

But with a multivariate function I did not manage to use the map function. Here is an example of what I am doing instead, which is slow:

def fun(x,y):
    return 2*x+y

xpts = np.linspace(0, 1, 5)

# make dict of indexes
count=0
dic=dict()
for j in xpts:
    dic[j]=count
    count+=1

# preallocate array
arr = np.empty([len(xpts)]*2)

for tup in itertools.product(xpts, xpts):

    ind1 = dic[tup[0]]
    ind2 = dic[tup[1]]

    val1 = tup[0]
    val2 = tup[1]

    arr[ind1, ind2]   = fun(val1, val2)

As my function is complicated and the space is large, I am looking for an efficient/scalable way.

5
  • map isn't going to make things faster. Optimizing execution of a function in NumPy almost always requires some degree of examining the function's implementation, particularly whether it broadcasts and whether it can be rewritten to broadcast. Commented May 2, 2018 at 1:21
  • xy = np.linspace(0, 1, 5)... then, m = np.meshgrid(xy, xy) to get a meshgrid. You can then use your function with the results of m, like... fun(m[1], m[0]) or swap the results around Commented May 2, 2018 at 2:26
  • 1
    fun(np.linspace(0, 1, 5)[:,None], np.linspace(0, 1, 10)] is the most efficient approach - when fun is writen so it takes arrays, not scalars. Commented May 2, 2018 at 6:53
  • Thanks @hpaulj, just for completeness, I suppose you meant fun(np.linspace(0, 1, 5)[:,None], np.linspace(0, 1, 5)) Commented May 3, 2018 at 16:10
  • When broadcastng arrays like that the 2 lengths don't have to match. In my example the result should be a (5,10) array. Commented May 3, 2018 at 16:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.