title: How to iterate through a tuple or list of functions and operate on their return values?
body: <p>I have been working on module that supposedly accepts a generic tuple of coupled equations spits out the solution. Initially I tested it on the specific system of equations I am interested in, and it worked wonderfully. But then I tested it on a simpler system just to see if I had really achieved a function that is generic. </p>
  
  <p>It seems like in the instance that worked, I'm able to pull each function from the list, insert the arguments, and operate on the return value.</p>
  
  <p>In the case where it doesn't work, it looks like Python is classifying my functions as "generators", something that I can't do arithmetic on...</p>
  
  <p>This code is also my first attempt at using <code>*args</code> and <code>**kwargs</code>, which is exciting, but maybe there is some error there too... </p>
  
  <p><strong>I am a relatively new programmer, who has learned mostly from copying and pasting from documentation and forums until my code works. Please be kind. If my question is ill formed, let me know how I can ask a better question. If you answer, please do so in the context of my knowledge level (or provide links to documentation if there is something that I need to research to understand your answer)</strong></p>
  
  <p>The first snippet is the original function to solve the problem:</p>
  
  <pre><code># Below is the standard representation of RK4, generalized to any system
  # ** init is the solution vector y_(n-1) from the previous step
  #    used to solve for the solution at the next step, y_n.
  # ** t is the previous time step
  # ** dfuncs is the vector field dy/dt = f(t,y)
  
  def RK4(init, t, dfuncs, h):
      k1 = [ h*f(*init,t) for f in dfuncs ]
      args = [ r+0.5*kr for r,kr in zip((*init,t),(*k1, h)) ]
      k2 = [ h*f(*args) for f in dfuncs ]
      args = [ r+0.5*kr for r,kr in zip((*init,t),(*k2, h)) ]
      k3 = [ h*f(*args) for f in dfuncs ]
      args = [ r+kr for r,kr in zip((*init,t),(*k3, h)) ]
      k4 =[ h*f(*args) for f in dfuncs ]
      return (r+(k1r+2*k2r+2*k3r+k4r)/6 for r,k1r,k2r,k3r,k4r in
              zip(init,k1, k2, k3, k4))
  </code></pre>
  
  <p>Here is the code that the function worked well for:</p>
  
  <pre><code> #The following three functions represent the three ODEs in question
      # dB/Dt =
      def fx(B, S, E, t):
          return (r_b*B*(1 -  (B*(pow(T, 2)
                          + pow(E, 2)))/(K*S*pow(E, 2)))
                          - (beta*pow(B, 2))/(pow((alpha*S),2)
                          + pow(B, 2)))
      #dS/dt =
      def fy(B, S, E, t):
          return r_s*S*(1 - (S*K_e) / (E*K_s))
  
      # dE/dt =
      def fz(B, S, E, t):
          return r_e*E*(1 - E/K_e) - (P*B*pow(E, 2))/(S*(pow(T,2) + pow(E, 2)))
  
      # set parameter values from Ludwig paper
      r_b = 1.52
      r_s = 0.095
      r_e = 0.92
      alpha = 1.11
      beta = 43200
      K = 355
      K_s = 25440
      K_e = 1
      P = 0.00195
      T = 0.1
      t_0 = 0.
      t_n = 50.
      Dt = .5
      steps=int(np.floor((t_n - t_0) / Dt))
  
      # initialize solution vectors
      t = steps * [0.0]
      B = steps * [0.0]
      S = steps * [0.0]
      E = steps * [0.0]
  
      #Set initial conditions
      B[0],S[0],E[0],t[0] = 1e-16, .075*K_s, 1., 0.
  
      # Solve the system using RK4
      for i in range(1, steps):
          B[i],S[i],E[i] = RK4((B[i - 1], S[i - 1], E[i - 1]), t[i - 1], (fx, fy, fz), Dt)
  </code></pre>
  
  <p>And here is the simpler system that it failed on:</p>
  
  <pre><code>def dy(y, z, t):
      return y
  def dz(y, z, t):
      return pow(z, 2)
  
  t0 = 0
  tn = 10
  y0 = 1
  z0 = 0
  Dt = 0.01
  steps = int(np.floor((tn - t0) / Dt))
  
  y = steps * [0.0]
  z = steps * [0.0]
  t = steps * [0.0]
  
  y[0] = y0
  z[0] = z0
  t[0] = t0
  
  for i in range(1, steps):
      y[i] = RK4((y[i-1], z[i-1]), t[i-1], (dy, dz), Dt)
  </code></pre>
  
  <p>With traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:/Users/wesle/PycharmProjects/Budworms/basic.py", line 27, in &lt;module&gt;
      y[i] = RK4((y[i-1], z[i-1]), t[i-1], (dy, dz), Dt)
    File "C:\Users\wesle\PycharmProjects\Budworms\RK4.py", line 23, in RK4
      k1 = [ h*f(*init,t) for f in dfuncs ]
    File "C:\Users\wesle\PycharmProjects\Budworms\RK4.py", line 23, in &lt;listcomp&gt;
      k1 = [ h*f(*init,t) for f in dfuncs ]
  TypeError: unsupported operand type(s) for *: 'float' and 'generator'
  </code></pre>