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)
python list loops numpy iteration
add a comment |
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)
python list loops numpy iteration
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
add a comment |
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)
python list loops numpy iteration
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
python list loops numpy iteration
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
add a comment |
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
add a comment |
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])
add a comment |
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.
add a comment |
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])
add a comment |
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])
add a comment |
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])
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])
answered Nov 18 at 15:03
seayak
6617
6617
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 18 at 15:24
answered Nov 18 at 14:59
Martijn Pieters♦
692k12923932234
692k12923932234
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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