Does anyone could help with this code? I'm writing this code to compute some values over some equations. I satarted by reading the CSV values into a dictionary, but after calculating the values to reach the final set of parameters, I cannot find a way to iterate repeatedly over the same list.
To simply, I have two input lists dPdT and listT. I need to iterate every parameter of list dPdT over listT and produce three different lists P.
I thank anyone willing to help. This is a study project for a course.
# Request user's interval parameters for calculations
print("Inform the temperature range and interval (°C). Only integers.")
minT = int(input("Min: "))
maxT = int(input("Max: "))
stepT = int(input("Temperature interval: "))
# create a list of temperature values to compute pressure parameters
listT = []
for x in range(minT, (maxT+stepT), stepT):
listT.append(x)
# Open CSV file in read mode to acces data and read it into a dictionary
with open(CSVfile, "r") as CSV:
reader = csv.DictReader(CSV)
listDict = []
# Creates a list of dictionaries with the fluid inclusion parameters
for lines in reader:
listDict.append(lines)
# Define list parameters to be computated
a, b, c, dPdT, P = [], [], [], [], []
# Loop iterates over the dictionary list and computates parameters a,b,c stored in lists a,b,c
for i, rows in enumerate(listDict):
a.append(i)
b.append(i)
c.append(i)
if "sal" in rows:
a[i] = (18.28 + 1.4413 * float(rows["sal"]) + 0.0047241 * float(rows["sal"]) ** 2
+ 0.0024213 * float(rows["sal"]) ** 3 + 0.000038064 * float(rows["sal"]) ** 4)
b[i] = (0.019041 - 1.4268 * 0.01 * float(rows["sal"]) + 5.66012 * 0.0001 * float(rows["sal"]) ** 2
- 4.2329 * 0.000001 * float(rows["sal"]) ** 3 - 3.0354 * 0.00000001 * float(rows["sal"]) ** 4)
c[i] = (- 1.5988 * 0.0001 + 3.6892 * (10 ** -5) * float(rows["sal"]) - 1.9473 * (10 ** -6) * float(rows["sal"]) ** 2
+ 4.1674 * (10 ** -8) * float(rows["sal"]) ** 3 - 3.3008 * (10 ** -10) * float(rows["sal"]) ** 4)
# Loop iterates over the dictionary list and computates dPdT to store the values in list dPdT
for i, rows in enumerate(listDict):
dPdT.append(i)
if "th" in rows:
dPdT[i] = a[i] + b[i] * float(rows["th"]) + c[i] * float(rows["th"]) ** 2
# Loop populates list P (pressure values)
for i in range(len(listT)):
P.append(i)
# problem starts here: I need to iterate over the listT or P, repeating it for every dPdT values.
# Loop to calculate P based on lits dPdT and listT.
while i in range(len(dPdT)):
for j in range(len(P)):
P[j] = dPdT[j] * listT[j]
I need to iterate every parameter of list dPdT over listT and produce three different lists P.
I'm sorry it's hard to understand your question. Explain this line.
Do you want to multiply each element in dPdT with each element in listT resulting in a P array of length = len(dPdT) * len(listT) ??? Because your current code is doing exactly that.
Edit:
Ok, I understand. I think the best way to go about it is to make P a 2d array. Basically, P will be a huge array containing multiple arrays within it. len(P) = len(dPdT) and len(P[i]) = len(listT)
P = [ [0 for i in range(len(listT)] for j in range(len(dPdT)) ]
for row in range(len(dPdT)):
for col in range(len(listT)):
P[row][col] = dPdT[row] * listT[col]
I hope this is what you wanted. Basically, the nth element of P will also be array containing the numbers in listT all multiplied by the nth element of dPdT.
:)
Related
Creating evenly spaced numbers on a log scale (a geometric progression) can easily be done for a given base and number of elements if the starting and final values of the sequence are known, e.g., with numpy.logspace and numpy.geomspace. Now assume I want to define the geometric progression the other way around, i.e., based on the properties of the resulting geometric series. If I know the sum of the series as well as the first and last element of the progression, can I compute the quotient and number of elements?
For instance, assume the first and last elements of the progression are and and the sum of the series should be equal to . I know from trial and error that it works out for n=9 and r≈1.404, but how could these values be computed?
You have enough information to solve it:
Sum of series = a + a*r + a*(r^2) ... + a*(r^(n-1))
= a*((r^n)-1)/(r-1)
= a*((last element * r) - 1)/(r-1)
Given the sum of series, a, and the last element, you can use the above equation to find the value of r.
Plugging in values for the given example:
50 = 1 * ((15*r)-1) / (r-1)
50r - 50 = 15r - 1
35r = 49
r = 1.4
Then, using sum of series = a*((r^n)-1)/(r-1):
50 = 1*((1.4^n)-1)(1.4-1)
21 = 1.4^n
n = log(21)/log(1.4) = 9.04
You can approximate n and recalculate r if n isn't an integer.
We have to reconstruct geometric progesssion, i.e. obtain a, q, m (here ^ means raise into power):
a, a * q, a * q^2, ..., a * q^(m - 1)
if we know first, last, total:
first = a # first item
last = a * q^(m - 1) # last item
total = a * (q^m - 1) / (q - 1) # sum
Solving these equation we can find
a = first
q = (total - first) / (total - last)
m = log(last / a) / log(q)
if you want to get number of items n, note that n == m + 1
Code:
import math
...
def Solve(first, last, total):
a = first
q = (total - first) / (total - last)
n = math.log(last / a) / math.log(q) + 1
return (a, q, n);
Fiddle
If you put your data (1, 15, 50) you'll get the solution
a = 1
q = 1.4
n = 9.04836151801382 # not integer
since n is not an integer you, probably want to adjust; let last == 15 be exact, when total can vary. In this case q = (last / first) ^ (1 / (n - 1)) and total = first * (q ^ n - 1) / (q - 1)
a = 1
q = 1.402850552006674
n = 9
total = 49.752 # now n is integer, but total <> 50
You have to solve the following two equations for r and n:
a:= An / Ao = r^(n - 1)
and
s:= Sn / Ao = (r^n - 1) / (r - 1)
You can eliminate n by
s = (r a - 1) / (r - 1)
and solve for r. Then n follows by log(a) / log(r) + 1.
In your case, from s = 50 and a = 15, we obtain r = 7/5 = 1.4 and n = 9.048...
It makes sense to round n to 9, but then r^8 = 15 (r ~ 1.40285) and r = 1.4 are not quite compatible.
I'm using sympy to solve an equation in a for loop in which at each interaction a variable (kp) multiples the function. But in each interaction the length of the output increases. I have an array k and kp is selected from k
k = [2,4,5,7,9]
for kp in k:
didt = beta * kp * teta
dteta = integrate(1/((kp-1) * pk * didt / avgk),teta)
dt = integrate(1,(t,0 ,1))
teta2 = solve(dteta - dt ,teta)
#print(solve(dteta - dt ,teta))
didt2 = beta * solve(dteta - dt ,teta) *kp
print(didt2)
Also, the output for didt2 for 1st iteration is
[1.49182469764127, 1.49182469764127]
for the second one is
[11.0231763806416, 11.0231763806416, 11.0231763806416, 11.0231763806416]
for the 3rd one is [54.5981500331442, 54.5981500331442, 54.5981500331442, 54.5981500331442, 54.5981500331442]
I'm just wondering, why the length of didt2 increases at each interaction?
It looks like solve() returns a list. This means that beta * solve(dteta - dt ,teta) *kp doesn't do what you think. Rather than multiplying the result, you are duplicating the elements of the returned list. For a simple example, try to see what the output is:
[0] * 10
In your case, kp takes on the values of 2, 4, and 5 on each iteration of the list, so the output you see is the result of doing
[1.49182469764127] * 2
[11.0231763806416] * 4
[54.5981500331442] * 5
These all result in lists with the exact length of the value of kp. It does not do numeric multiplication.
This is a MATLAB function from the author of RainbowCrack:
function ret = calc_success_probability(N, t, m)
arr = zeros(1, t - 1);
arr(1) = m;
for i = 2 : t - 1
arr(i) = N * (1 - (1 - 1 / N) ^ arr(i - 1));
end
exp = 0;
for i = 1 : t - 1
exp = exp + arr(i);
end
ret = 1 - (1 - 1 / N) ^ exp;
It calculates the probability of success in finding a plaintext password given a rainbow table with keyspace N, a large unsigned integer, chain of length t, and number of chains m.
A sample run:
calc_success_probability(80603140212, 2400, 40000000)
Returns 0.6055.
I am having difficulty converting this into Python. In Python 3, there is no max integer anymore, so N isn't an issue. I think in the calculations I have to force everything to a large floating point number, but I'm not sure.
I also don't know the order of operations in MATLAB. I think the code is saying this:
Create array of size [1 .. 10] so ten elements
Initialize every element of that array with zero
In zero-based indexing, I think this would be array[0 .. t-1], it looks like MATLAB uses 1 as the first (0'th) index.
Then second element of array (0-based indexing) initialized to m.
For each element in array, pos=1 (0-based indexing) to t-1:
array[pos] = N * (1 - (1 - 1/N) ** array[pos-1]
Where ** is the power operator. I think power is ^ in MATLAB, so N * (1 - (1-1/N) to the array[pos-1] power is like that above.
Then set an exponent. For each element in array 0 to t-1:
exponent is exponent + 1
return probability = 1 - (1 - 1/N) power of exp;
My Python code looks like this, and doesn't work. I can't figure out why, but it could be that I don't understand MATLAB enough, or Python, both, or I'm reading the math wrong somehow and what's going on in MATLAB is not what I'm expecting, i.e. I have order of operations and/or types wrong to make it work and I'm missing something in those terms...
def calc_success_probability(N, t, m):
comp_arr = []
# array with indices 1 to t-1 in MATLAB, which is otherwise 0 to t-2???
# range with 0, t is 0 to t excluding t, so t here is t-1, t-1 is up
# to including t-2... sounds wrong...
for i in range(0, t-1):
# initialize array
comp_arr.append(0)
print("t = {0:d}, array size is {1:d}".format(t, len(comp_arr)))
# zero'th element chain count
comp_arr[0] = m
for i in range(1, t-1):
comp_arr[i] = N * (1 - (1 - 1 / N)) ** comp_arr[i-1]
final_exp = 0
for i in range(0, t-1):
final_exp = final_exp + comp_arr[i]
probability = (1 - (1 - 1 / N)) ** final_exp
return probability
Watch your brackets! You have translated this:
arr(i) = N * ( 1 - ( 1 - 1 / N ) ^ arr(i - 1) );
to this:
comp_arr[i] = N * ( 1 - ( 1 - 1 / N ) ) ** comp_arr[i-1]
I've lined up everything so you can better see where it goes wrong. You've moved a bracket to the wrong location.
It should be:
comp_arr[i] = N * ( 1 - ( 1 - 1 / N ) ** comp_arr[i-1] )
Similarly,
ret = 1 - (1 - 1 / N) ^ exp;
is not the same as
probability = (1 - (1 - 1 / N)) ** final_exp
This should be
probability = 1 - (1 - 1 / N) ** final_exp
I have got a function:
def euler9():
for b in range(1, 500):
a = (500000 - 1000 * b) / (1000 - b)
if a % 1 == 0:
print(b * a * (1000 - a - b))
And I want to make it in one line like
x*x for x in range(1,1)
This is what I have done:
def euler9():
print([b * a * (1000 - a - b) for b in range(1, 500) for a in (500000 - 1000 * b) / (1000 - b) if a % 1 == 0])
but I do not know what am I doing wrong. I have got an error:
TypeError: 'float' object is not iterable
for is for iteration (looping). When you say for b in range(1, 500) you are not setting b = range(1, 500), since that would make b a list. You are extracting each individual value and using them one at a time. You cannot extract values from a float.
Python has no syntax for simple assignment in list comprehensions, but you can work around that by putting the value inside a single-element list, thus making it iterable:
[b * a * (1000 - a - b) for b in range(1, 500) for a in [(500000 - 1000 * b) / (1000 - b)] if a % 1 == 0]
(You can put print(...) around the initial expression if you want but I assume you want to actually use the values)
But don't ever do this, it's hard to read and unnecessary.
Here for a in (500000 - 1000 * b) / (1000 - b) you are trying to iterate over a float number which is the result of a devision.
As a quick fix try this:
def euler9():
print([b * ((500000 - 1000 * b) / (1000 - b)) * (1000 - ((500000 - 1000 * b) / (1000 - b)) - b)
for b in range(1, 500) if ((500000 - 1000 * b) / (1000 - b)) % 1 == 0])
But, as you see it gets a bit messy that way, and it is recommended to use loops instead of list comprehension when things get complicated.
Turn it into a generator by changing print to yield:
def euler9():
for b in range(1, 500):
a = (500000 - 1000 * b) / (1000 - b)
if a % 1 == 0:
yield (b * a * (1000 - a - b))
Then you can access it as a list comprehension:
print [x for x in euler9()]
I want to solve an equation which I am supposed to solve it recursively, I uploaded the picture of formula (Sorry! I did not know how to write mathematical formulas here!)
I wrote the code in Python as below:
import math
alambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / alambda
P0 = (1 - (alambda*b))
P1 = (1-(alambda*b))*(math.exp(alambda*b) - 1)
def a(n):
a_n = math.exp(-alambda*b) * ((alambda*b)**n) / math.factorial(n)
return a_n
def P(n):
P(n) = (P0+P1)*a(n) + sigma(n)
def sigma(n):
j = 2
result = 0
while j <= n+1:
result = result + P(j)*a(n+1-j)
j += 1
return result
It is obvious that I could not finish P function. So please help me with this.
when n=1 I should extract P2, when n=2 I should extract P3.
By the way, P0 and P1 are as written in line 6 and 7.
When I call P(5) I want to see P(0), P(1), P(2), P(3), P(4), P(5), P(6) at the output.
You need to reorganize the formula so that you don't have to calculate P(3) to calculate P(2). This is pretty easy to do, by bringing the last term of the summation, P(n+1)a(0), to the left side of the equation and dividing through by a(0). Then you have a formula for P(n+1) in terms of P(m) where m <= n, which is solvable by recursion.
As Bruce mentions, it's best to cache your intermediate results for P(n) by keeping them in a dict so that a) you don't have to recalculate P(2) etc everytime you need it, and b) after you get the value of P(n), you can just print the dict to see all the values of P(m) where m <= n.
import math
a_lambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / a_lambda
p0 = (1 - (a_lambda*b))
p1 = (1-(a_lambda*b))*(math.exp(a_lambda*b) - 1)
p_dict = {0: p0, 1: p1}
def a(n):
return math.exp(-a_lambda*b) * ((a_lambda*b)**n) / math.factorial(n)
def get_nth_p(n, p_dict):
# return pre-calculated value if p(n) is already known
if n in p_dict:
return p_dict[n]
# Calculate p(n) using modified formula
p_n = ((get_nth_p(n-1, p_dict)
- (get_nth_p(0, p_dict) + get_nth_p(1, p_dict)) * a(n - 1)
- sum(get_nth_p(j, p_dict) * a(n + 1 - j) for j in xrange(2, n)))
/ a(0))
# Save computed value into the dict
p_dict[n] = p_n
return p_n
get_nth_p(6, p_dict)
print p_dict
Edit 2
Some cosmetic updates to the code - shortening the name and making p_dict a mutable default argument (something I try to use only sparingly) really makes the code much more readable:
import math
# Customary to distinguish variables that are unchanging by making them ALLCAP
A_LAMBDA = 1.0
RHO = 0.8
C = 1.0
B = RHO * C / A_LAMBDA
P0 = (1 - (A_LAMBDA*B))
P1 = (1-(A_LAMBDA*B))*(math.exp(A_LAMBDA*B) - 1)
p_value_cache = {0: P0, 1: P1}
def a(n):
return math.exp(-A_LAMBDA*B) * ((A_LAMBDA*B)**n) / math.factorial(n)
def p(n, p_dict=p_value_cache):
# return pre-calculated value if p(n) is already known
if n in p_dict:
return p_dict[n]
# Calculate p(n) using modified formula
p_n = ((p(n-1)
- (p(0) + p(1)) * a(n - 1)
- sum(p(j) * a(n + 1 - j) for j in xrange(2, n)))
/ a(0))
# Save computed value into the dict
p_dict[n] = p_n
return p_n
p(6)
print p_value_cache
You could fix if that way:
import math
alambda = 1.0
rho = 0.8
c = 1.0
b = rho * c / alambda
def a(n):
# you might want to cache a as well
a_n = math.exp(-alambda*b) * ((alambda*b)**n) / math.factorial(n)
return a_n
PCache={0:(1 - (alambda*b)),1:(1-(alambda*b))*(math.exp(alambda*b) - 1)}
def P(n):
if n in PCache:
return PCache[n]
ret= (P(0)+P(1))*a(n) + sigma(n)
PCache[n]=ret
return ret
def sigma(n):
# caching this seems smart as well
j = 2
result = 0
while j <= n+1:
result = result + P(j)*a(n+1-j)
j += 1
return result
void displayP(n):
P(n) # fill cache :-)
for x in range(n):
print ("%u -> %d\n" % (x,PCache[x]))
Instead of managing the cache manually, you might want to use a memoize decorator (see http://www.python-course.eu/python3_memoization.php )
Notes:
not tested, but you should get the idea behind it
your recurrence won't work P(n) depends on P(n+1) on your equation... This will never end
It looks like I misunderstood P0 and P1 as being Both constants (big numbers) and results (small numbers, indices)... Notation is not the best choice I guess...