0

I need to integrate a matrix function as in the example below:

def func(a1, a2, a3):
     return np.array([a1, (a1 + a2), a3])

The non-efficient way to do this is using three for loops. Although, I would like to make it more efficient. I thought about using "map", like:

def integral(func, a1, a2, a3, w):
    f = np.array(list(map(func, a1, a2, a3)))
    I = np.zeros((3, ))
    for fi, wi in zip(f, w):
        I = I + wi*np.array(fi)
    return I

a1, a2, a3 and w are arrays of the same size (a's are the sample points and w are the weights)

Is this the most optimized way to do so?

1
  • If you are going to zip f there's no point in starting with array. zip on a list is faster. But the zip loop is just np.dot(w, f). Commented Jan 8, 2019 at 18:41

1 Answer 1

1

For this function in particular you can vectorize everything.

I = [email protected]([a1,a1+a2,a3]).T

However, in general it isn't fast to apply a python function over a numpy array.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, but that function in particular was just an example. The real function is far more complicated and has NxM dimension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.