Can i make this iteration easier by using a loop?











up vote
1
down vote

favorite












So i'm relatively new to python but i have used loops in previous programs i have made but they were relatively simple.
I was wondering if there was a method that involved a loop or some other way to make this iteration more concise.



Basically is there a way for me to have a variable called 't' that holds values for t1,t2 and t3 in like a list which is then used in 'eq' and 'der' instead of copying and pasting the same piece of code?



I have already tried to look for an answer on here and other places but the solutions i have found so far don't seem to work with what i have got/i'm not python literate enough to understand them.



Thanks in advance.



import numpy as np
U235_Decay_Constant = 9.72e-10
U238_Decay_Constant = 1.54e-10


t0 = 4.1e9


eq = ((np.exp(U238_Decay_Constant*t0)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t0)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t0)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t0))))

t1 = t0 - (eq/der)


eq = ((np.exp(U238_Decay_Constant*t1)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t1)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t1)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t1))))

t2 = t1 - (eq/der)

eq = ((np.exp(U238_Decay_Constant*t2)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t2)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t2)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t2))))

t3 = t2 - (eq/der)
print(t3)









share|improve this question






















  • Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
    – Sylhare
    Nov 18 at 15:04










  • Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
    – Martijn Pieters
    Nov 18 at 15:29















up vote
1
down vote

favorite












So i'm relatively new to python but i have used loops in previous programs i have made but they were relatively simple.
I was wondering if there was a method that involved a loop or some other way to make this iteration more concise.



Basically is there a way for me to have a variable called 't' that holds values for t1,t2 and t3 in like a list which is then used in 'eq' and 'der' instead of copying and pasting the same piece of code?



I have already tried to look for an answer on here and other places but the solutions i have found so far don't seem to work with what i have got/i'm not python literate enough to understand them.



Thanks in advance.



import numpy as np
U235_Decay_Constant = 9.72e-10
U238_Decay_Constant = 1.54e-10


t0 = 4.1e9


eq = ((np.exp(U238_Decay_Constant*t0)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t0)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t0)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t0))))

t1 = t0 - (eq/der)


eq = ((np.exp(U238_Decay_Constant*t1)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t1)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t1)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t1))))

t2 = t1 - (eq/der)

eq = ((np.exp(U238_Decay_Constant*t2)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t2)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t2)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t2))))

t3 = t2 - (eq/der)
print(t3)









share|improve this question






















  • Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
    – Sylhare
    Nov 18 at 15:04










  • Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
    – Martijn Pieters
    Nov 18 at 15:29













up vote
1
down vote

favorite









up vote
1
down vote

favorite











So i'm relatively new to python but i have used loops in previous programs i have made but they were relatively simple.
I was wondering if there was a method that involved a loop or some other way to make this iteration more concise.



Basically is there a way for me to have a variable called 't' that holds values for t1,t2 and t3 in like a list which is then used in 'eq' and 'der' instead of copying and pasting the same piece of code?



I have already tried to look for an answer on here and other places but the solutions i have found so far don't seem to work with what i have got/i'm not python literate enough to understand them.



Thanks in advance.



import numpy as np
U235_Decay_Constant = 9.72e-10
U238_Decay_Constant = 1.54e-10


t0 = 4.1e9


eq = ((np.exp(U238_Decay_Constant*t0)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t0)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t0)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t0))))

t1 = t0 - (eq/der)


eq = ((np.exp(U238_Decay_Constant*t1)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t1)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t1)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t1))))

t2 = t1 - (eq/der)

eq = ((np.exp(U238_Decay_Constant*t2)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t2)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t2)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t2))))

t3 = t2 - (eq/der)
print(t3)









share|improve this question













So i'm relatively new to python but i have used loops in previous programs i have made but they were relatively simple.
I was wondering if there was a method that involved a loop or some other way to make this iteration more concise.



Basically is there a way for me to have a variable called 't' that holds values for t1,t2 and t3 in like a list which is then used in 'eq' and 'der' instead of copying and pasting the same piece of code?



I have already tried to look for an answer on here and other places but the solutions i have found so far don't seem to work with what i have got/i'm not python literate enough to understand them.



Thanks in advance.



import numpy as np
U235_Decay_Constant = 9.72e-10
U238_Decay_Constant = 1.54e-10


t0 = 4.1e9


eq = ((np.exp(U238_Decay_Constant*t0)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t0)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t0)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t0))))

t1 = t0 - (eq/der)


eq = ((np.exp(U238_Decay_Constant*t1)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t1)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t1)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t1))))

t2 = t1 - (eq/der)

eq = ((np.exp(U238_Decay_Constant*t2)-1)-(0.0167*
((np.exp(U235_Decay_Constant*t2)-1)))-0.0094)
der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t2)))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t2))))

t3 = t2 - (eq/der)
print(t3)






python list loops numpy iteration






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 14:54









Robbie Stewart

82




82












  • Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
    – Sylhare
    Nov 18 at 15:04










  • Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
    – Martijn Pieters
    Nov 18 at 15:29


















  • Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
    – Sylhare
    Nov 18 at 15:04










  • Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
    – Martijn Pieters
    Nov 18 at 15:29
















Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
– Sylhare
Nov 18 at 15:04




Hey welcome to stackover flow! So basically you want to have a loop that creates the t3 value? You can edit you question to make it easier for us to help ;)
– Sylhare
Nov 18 at 15:04












Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
– Martijn Pieters
Nov 18 at 15:29




Glad you found both answers to be of help! However, you can only pick one of them to be the 'accepted' answer. The choice is entirely yours, pick one you feel helped you the most perhaps, and picking neither is also an option.
– Martijn Pieters
Nov 18 at 15:29












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Might be a little easier to read as:



import numpy as np
U235_Decay_Constant = 9.72e-10
U238_Decay_Constant = 1.54e-10


t = [4.1e9, None, None, None]

t[0] = 4.1e9
for i in range(3):
eq = ((np.exp(U238_Decay_Constant*t[i])-1)-(0.0167*
((np.exp(U235_Decay_Constant*t[i])-1)))-0.0094)

der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t[0])))-(0.0167*
(U235_Decay_Constant*(np.exp(U235_Decay_Constant*t[0]))))

t[i+1] = t[i] - (eq/der)

print(t[3])





share|improve this answer




























    up vote
    1
    down vote













    Yes, iteration can help here. Add your values to a list, then t? is the last value in the list so far; replacing your t? references with t[-1] gives:



    t = [4.1e9]
    for _ in range(3):
    eq = (
    (np.exp(U238_Decay_Constant * t[-1]) - 1)
    - (0.0167 * ((np.exp(U235_Decay_Constant * t[-1]) - 1)))
    - 0.0094
    )
    der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t[-1]))) - (
    0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t[-1])))
    )
    t.append(t[-1] - (eq / der))


    The general principle is one of accumulation, where you produce the running output of a repeated application of a function. So the itertools.accumulate() function could help here too:



    from itertools import accumulate, chain, repeat

    def u238_decay(t, _):
    eq = (
    (np.exp(U238_Decay_Constant * t) - 1)
    - (0.0167 * ((np.exp(U235_Decay_Constant * t) - 1)))
    - 0.0094
    )
    der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t))) - (
    0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t)))
    )
    return t - (eq / der)

    series = accumulate(chain([4.1e9], repeat(None)), u238_decay)


    The above produces an unending series of decay values:



    >>> series = accumulate(chain([4.1e9], repeat(None)), u238_decay)
    >>> next(series)
    4100000000.0
    >>> next(series)
    4081406102.7439713
    >>> next(series)
    4081163259.5641546
    >>> next(series)
    4081163218.6509323
    >>> next(series)
    4081163218.650931


    You could look into creating a numpy universal function so you can do the same with the numpy.ufunc.accumulate() method.



    However, I suspect that your formula can be re-cast to not depend on the previous input, but only as a formula of starting amount and t as time, at which point you can use full numpy vectorised calculations.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362189%2fcan-i-make-this-iteration-easier-by-using-a-loop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Might be a little easier to read as:



      import numpy as np
      U235_Decay_Constant = 9.72e-10
      U238_Decay_Constant = 1.54e-10


      t = [4.1e9, None, None, None]

      t[0] = 4.1e9
      for i in range(3):
      eq = ((np.exp(U238_Decay_Constant*t[i])-1)-(0.0167*
      ((np.exp(U235_Decay_Constant*t[i])-1)))-0.0094)

      der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t[0])))-(0.0167*
      (U235_Decay_Constant*(np.exp(U235_Decay_Constant*t[0]))))

      t[i+1] = t[i] - (eq/der)

      print(t[3])





      share|improve this answer

























        up vote
        1
        down vote



        accepted










        Might be a little easier to read as:



        import numpy as np
        U235_Decay_Constant = 9.72e-10
        U238_Decay_Constant = 1.54e-10


        t = [4.1e9, None, None, None]

        t[0] = 4.1e9
        for i in range(3):
        eq = ((np.exp(U238_Decay_Constant*t[i])-1)-(0.0167*
        ((np.exp(U235_Decay_Constant*t[i])-1)))-0.0094)

        der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t[0])))-(0.0167*
        (U235_Decay_Constant*(np.exp(U235_Decay_Constant*t[0]))))

        t[i+1] = t[i] - (eq/der)

        print(t[3])





        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Might be a little easier to read as:



          import numpy as np
          U235_Decay_Constant = 9.72e-10
          U238_Decay_Constant = 1.54e-10


          t = [4.1e9, None, None, None]

          t[0] = 4.1e9
          for i in range(3):
          eq = ((np.exp(U238_Decay_Constant*t[i])-1)-(0.0167*
          ((np.exp(U235_Decay_Constant*t[i])-1)))-0.0094)

          der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t[0])))-(0.0167*
          (U235_Decay_Constant*(np.exp(U235_Decay_Constant*t[0]))))

          t[i+1] = t[i] - (eq/der)

          print(t[3])





          share|improve this answer












          Might be a little easier to read as:



          import numpy as np
          U235_Decay_Constant = 9.72e-10
          U238_Decay_Constant = 1.54e-10


          t = [4.1e9, None, None, None]

          t[0] = 4.1e9
          for i in range(3):
          eq = ((np.exp(U238_Decay_Constant*t[i])-1)-(0.0167*
          ((np.exp(U235_Decay_Constant*t[i])-1)))-0.0094)

          der = (U238_Decay_Constant*(np.exp(U238_Decay_Constant*t[0])))-(0.0167*
          (U235_Decay_Constant*(np.exp(U235_Decay_Constant*t[0]))))

          t[i+1] = t[i] - (eq/der)

          print(t[3])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 15:03









          seayak

          6617




          6617
























              up vote
              1
              down vote













              Yes, iteration can help here. Add your values to a list, then t? is the last value in the list so far; replacing your t? references with t[-1] gives:



              t = [4.1e9]
              for _ in range(3):
              eq = (
              (np.exp(U238_Decay_Constant * t[-1]) - 1)
              - (0.0167 * ((np.exp(U235_Decay_Constant * t[-1]) - 1)))
              - 0.0094
              )
              der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t[-1]))) - (
              0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t[-1])))
              )
              t.append(t[-1] - (eq / der))


              The general principle is one of accumulation, where you produce the running output of a repeated application of a function. So the itertools.accumulate() function could help here too:



              from itertools import accumulate, chain, repeat

              def u238_decay(t, _):
              eq = (
              (np.exp(U238_Decay_Constant * t) - 1)
              - (0.0167 * ((np.exp(U235_Decay_Constant * t) - 1)))
              - 0.0094
              )
              der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t))) - (
              0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t)))
              )
              return t - (eq / der)

              series = accumulate(chain([4.1e9], repeat(None)), u238_decay)


              The above produces an unending series of decay values:



              >>> series = accumulate(chain([4.1e9], repeat(None)), u238_decay)
              >>> next(series)
              4100000000.0
              >>> next(series)
              4081406102.7439713
              >>> next(series)
              4081163259.5641546
              >>> next(series)
              4081163218.6509323
              >>> next(series)
              4081163218.650931


              You could look into creating a numpy universal function so you can do the same with the numpy.ufunc.accumulate() method.



              However, I suspect that your formula can be re-cast to not depend on the previous input, but only as a formula of starting amount and t as time, at which point you can use full numpy vectorised calculations.






              share|improve this answer



























                up vote
                1
                down vote













                Yes, iteration can help here. Add your values to a list, then t? is the last value in the list so far; replacing your t? references with t[-1] gives:



                t = [4.1e9]
                for _ in range(3):
                eq = (
                (np.exp(U238_Decay_Constant * t[-1]) - 1)
                - (0.0167 * ((np.exp(U235_Decay_Constant * t[-1]) - 1)))
                - 0.0094
                )
                der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t[-1]))) - (
                0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t[-1])))
                )
                t.append(t[-1] - (eq / der))


                The general principle is one of accumulation, where you produce the running output of a repeated application of a function. So the itertools.accumulate() function could help here too:



                from itertools import accumulate, chain, repeat

                def u238_decay(t, _):
                eq = (
                (np.exp(U238_Decay_Constant * t) - 1)
                - (0.0167 * ((np.exp(U235_Decay_Constant * t) - 1)))
                - 0.0094
                )
                der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t))) - (
                0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t)))
                )
                return t - (eq / der)

                series = accumulate(chain([4.1e9], repeat(None)), u238_decay)


                The above produces an unending series of decay values:



                >>> series = accumulate(chain([4.1e9], repeat(None)), u238_decay)
                >>> next(series)
                4100000000.0
                >>> next(series)
                4081406102.7439713
                >>> next(series)
                4081163259.5641546
                >>> next(series)
                4081163218.6509323
                >>> next(series)
                4081163218.650931


                You could look into creating a numpy universal function so you can do the same with the numpy.ufunc.accumulate() method.



                However, I suspect that your formula can be re-cast to not depend on the previous input, but only as a formula of starting amount and t as time, at which point you can use full numpy vectorised calculations.






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Yes, iteration can help here. Add your values to a list, then t? is the last value in the list so far; replacing your t? references with t[-1] gives:



                  t = [4.1e9]
                  for _ in range(3):
                  eq = (
                  (np.exp(U238_Decay_Constant * t[-1]) - 1)
                  - (0.0167 * ((np.exp(U235_Decay_Constant * t[-1]) - 1)))
                  - 0.0094
                  )
                  der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t[-1]))) - (
                  0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t[-1])))
                  )
                  t.append(t[-1] - (eq / der))


                  The general principle is one of accumulation, where you produce the running output of a repeated application of a function. So the itertools.accumulate() function could help here too:



                  from itertools import accumulate, chain, repeat

                  def u238_decay(t, _):
                  eq = (
                  (np.exp(U238_Decay_Constant * t) - 1)
                  - (0.0167 * ((np.exp(U235_Decay_Constant * t) - 1)))
                  - 0.0094
                  )
                  der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t))) - (
                  0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t)))
                  )
                  return t - (eq / der)

                  series = accumulate(chain([4.1e9], repeat(None)), u238_decay)


                  The above produces an unending series of decay values:



                  >>> series = accumulate(chain([4.1e9], repeat(None)), u238_decay)
                  >>> next(series)
                  4100000000.0
                  >>> next(series)
                  4081406102.7439713
                  >>> next(series)
                  4081163259.5641546
                  >>> next(series)
                  4081163218.6509323
                  >>> next(series)
                  4081163218.650931


                  You could look into creating a numpy universal function so you can do the same with the numpy.ufunc.accumulate() method.



                  However, I suspect that your formula can be re-cast to not depend on the previous input, but only as a formula of starting amount and t as time, at which point you can use full numpy vectorised calculations.






                  share|improve this answer














                  Yes, iteration can help here. Add your values to a list, then t? is the last value in the list so far; replacing your t? references with t[-1] gives:



                  t = [4.1e9]
                  for _ in range(3):
                  eq = (
                  (np.exp(U238_Decay_Constant * t[-1]) - 1)
                  - (0.0167 * ((np.exp(U235_Decay_Constant * t[-1]) - 1)))
                  - 0.0094
                  )
                  der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t[-1]))) - (
                  0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t[-1])))
                  )
                  t.append(t[-1] - (eq / der))


                  The general principle is one of accumulation, where you produce the running output of a repeated application of a function. So the itertools.accumulate() function could help here too:



                  from itertools import accumulate, chain, repeat

                  def u238_decay(t, _):
                  eq = (
                  (np.exp(U238_Decay_Constant * t) - 1)
                  - (0.0167 * ((np.exp(U235_Decay_Constant * t) - 1)))
                  - 0.0094
                  )
                  der = (U238_Decay_Constant * (np.exp(U238_Decay_Constant * t))) - (
                  0.0167 * (U235_Decay_Constant * (np.exp(U235_Decay_Constant * t)))
                  )
                  return t - (eq / der)

                  series = accumulate(chain([4.1e9], repeat(None)), u238_decay)


                  The above produces an unending series of decay values:



                  >>> series = accumulate(chain([4.1e9], repeat(None)), u238_decay)
                  >>> next(series)
                  4100000000.0
                  >>> next(series)
                  4081406102.7439713
                  >>> next(series)
                  4081163259.5641546
                  >>> next(series)
                  4081163218.6509323
                  >>> next(series)
                  4081163218.650931


                  You could look into creating a numpy universal function so you can do the same with the numpy.ufunc.accumulate() method.



                  However, I suspect that your formula can be re-cast to not depend on the previous input, but only as a formula of starting amount and t as time, at which point you can use full numpy vectorised calculations.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 18 at 15:24

























                  answered Nov 18 at 14:59









                  Martijn Pieters

                  692k12923932234




                  692k12923932234






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362189%2fcan-i-make-this-iteration-easier-by-using-a-loop%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy