-1

So I'm trying run a simple test of an idea. Basically I have some function that I define that depends on a variable and a parameter constant. And I have an array of parameter values. For the first parameter values, I have a set of start and end points of integration. For the second I have a different set of start and end points of integration. I've got the code working thanks to this link Integrating functions that return an array in Python

And it's basically exactly in that form.

My question is, if I have a definition of a function with a for loop in it, and that function looks something like:

def F(a):
    F = []
    for i in len(a):
        F[i] = scipy.integrate.quad(g,0,1,args = (a[i],))
    return F

(where g is some function I've defined previously in the code), then when I implement this function (or plug in an array for a) will all the elements in the array run consecutively? or will the integral at each element run at the same time?

Or in other words, in the link I attached at the beginning, when the function that is defined with a for loop is called, do all calculations in the function run consecutively (like a for loop running through indices) or concurrently since all elements are already defined?

Community
  • 1
  • 1
sasha
  • 7
  • 7
  • Thanks for the welcome. I've looked through the tour information. Is there any chance you have an answer to my question – sasha Jan 25 '17 at 19:51
  • Hello again. I recommended the tour because it was apparent you had not even performed that step to familiarize yourself with how this place works. Next step would be to read how to create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. Then edit your post to provide that. As one example, your code says `simpy.integrate.quad` when I am *guessing* you meant `scipy.integrate.quad`. And anytime we have to *guess*, we are much more likely to move on and help someone who has spent more of their time making it easy for them to be helped. – Stephen Rauch Jan 25 '17 at 20:20
  • I have fixed that typo. Hopefully that makes it more clear. Though the question is more about how for loops in definitions run when those functions are later called. – sasha Jan 25 '17 at 20:55

1 Answers1

1

The loop can better be expressed as:

def F(a):
    results = []
    for element in a:
        results.append(scipy.integrate.quad(g, 0, 1, args=(element,)))
    return results

or as a one-liner using a list comprehension:

def F(a):
    return [scipy.integrate.quad(g, 0, 1, args=(element,)) for element in a]

And in both of these cases, the integrations will be done one at at time (consecutively).

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Is there a way to apply integration on a matrix the same way using numpy applies other operations (like addition) concurrently? Such that F(a) is an array where each element is equal to F(element of a)? – sasha Jan 25 '17 at 21:15
  • Probably, but it would be best if you ask a new question. Do your best to create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. Good Luck. – Stephen Rauch Jan 25 '17 at 21:17