1

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

4
  • Why/How is your solution unsatisfactory. Commented Jun 30, 2015 at 18:52
  • I don't know, I just wonder if there exists a way to avoid that for loop. The list can be avoided using another tricks, but I thinks it would be better if I can have a function from R_n x R_m -> R_n in the same function. Commented Jun 30, 2015 at 18:54
  • You want to avoid creating a big matrix; is 50K x 1K too big? Commented Jun 30, 2015 at 18:58
  • Not really, with 4 bytes floats this is around 200MB on memory. The problem is that I don't know how much big will be v, as I said it's more than 5e+4 of size. Not knowing the future value I want to be sure I stay under 1GB. Commented Jun 30, 2015 at 19:05

1 Answer 1

1

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])
>>>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you. I didn't knew I could do c = v / w[:, np.newaxis]. Maybe I use your solution but slicing the input data in more fragments.
If you can get the arrays into the correct shape for Broadcasting then you can perform operations and avoid Python for loop(s).

Your Answer

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