Reputation:
I have a function like this:
def foo(v, w):
return sum(np.exp(v/w))
Where v in the beginning is a numpy array and w a number. Now I want to plot the value of this function for more values of w, so I need a function that works for different sizes of vectors. My solution for now is the obvious one
r = []
for e in w:
r.append(foo(v, e))
but I wonder if there is a better way to do it. Also, I want to stay low on memory, so I need to avoid create a big matrix, then applying the function to every value and sum over the columns (the length of v is more than 5e+4 and the length of w is 1e+3).
Thanks
Upvotes: 1
Views: 413
Reputation: 23753
If you cannot determine an upper bound for the length of v
and ensure that you don't exceed the memory requirements, I think you will have to stay with your solution.
If you can determine an upper bound for length of v
and meet your memory requirements using a Mx1000 array, you can do this.
import numpy as np
v = np.array([1,2,3,4,5])
w = np.array([10.,5.])
c = v / w[:, np.newaxis]
d = np.exp(c)
e = d.sum(axis = 1)
>>>
>>> v
array([1, 2, 3, 4, 5])
>>> w
array([ 10., 5.])
>>> c
array([[ 0.1, 0.2, 0.3, 0.4, 0.5],
[ 0.2, 0.4, 0.6, 0.8, 1. ]])
>>> d
array([[ 1.10517092, 1.22140276, 1.34985881, 1.4918247 , 1.64872127],
[ 1.22140276, 1.4918247 , 1.8221188 , 2.22554093, 2.71828183]])
>>> e
array([ 6.81697845, 9.47916901])
>>>
Upvotes: 1