I am trying to print b and c from this code, but I am not having any luck. If I am correct, this code should output several points with a step size of 0.05, but I am not seeing it. Does anyone know how to print two values from this code?
import math
def rK3(a, b, c, fa, fb, fc, hs):
a1 = fa(a, b, c)*hs
b1 = fb(a, b, c)*hs
c1 = fc(a, b, c)*hs
ak = a + a1*0.5
bk = b + b1*0.5
ck = c + c1*0.5
a2 = fa(ak, bk, ck)*hs
b2 = fb(ak, bk, ck)*hs
c2 = fc(ak, bk, ck)*hs
ak = a + a2*0.5
bk = b + b2*0.5
ck = c + c2*0.5
a3 = fa(ak, bk, ck)*hs
b3 = fb(ak, bk, ck)*hs
c3 = fc(ak, bk, ck)*hs
ak = a + a3
bk = b + b3
ck = c + c3
a4 = fa(ak, bk, ck)*hs
b4 = fb(ak, bk, ck)*hs
c4 = fc(ak, bk, ck)*hs
a = a + (a1 + 2*(a2 + a3) + a4)/6
b = b + (b1 + 2*(b2 + b3) + b4)/6
c = c + (c1 + 2*(c2 + c3) + c4)/6
return a, b, c
def fa2(a, b, c):
return 0.9*(1 - b*b)*a - b + math.sin(c)
def fb2(a, b, c):
return a
def fc2(a, b, c):
return 0.5
def VDP2():
a, b, c, hs = 1, 1, 0, 0.05
while (c<6):
a, b, c = rK3(a, b, c, fa2, fb2, fc2, hs)
Your code does not have any print statement, so it will not print.
Try inserting something like:
print 'b = {0}, c = {1}'.format(b,c)
Where you want the print to happen. For Python 3 just add parentheses (print is a function now)
print('b = {0}, c = {1}'.format(b,c))
Related
how to convert this type of code from pine script to python
pine script code
get2PoleSSF(src, length) =>
PI = 2 * asin(1)
arg = sqrt(2) * PI / length
a1 = exp(-arg)
b1 = 2 * a1 * cos(arg)
c2 = b1
c3 = -pow(a1, 2)
c1 = 1 - c2 - c3
ssf = 0.0
ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
this is the part im trying to convert it to python
the value of of the code
I tried this
def get2PoleSSD(src,length):
PI = 2* np.arcsin(1)
arg = np.sqrt(2)* PI / length
a1 = np.exp(-arg)
b1 = 2 * a1 *np. cos(arg)
c2 = b1
c3 = -pow(a1, 2)
c1 = 1 - c2 - c3
df['ssf'] = 0.0
df['ssf'] = c1* src
df['ssf_1p'] = df['ssf'].shift(1)
df['ssf_2p'] = df['ssf'].shift(2)
df['ssf_n'] = c1 * src + c2 * df['ssf_1p'] + c3 * df['ssf_2p']
the value from my code
but the value doesn't match at all
This question already has answers here:
Combine two columns of text in pandas dataframe
(21 answers)
Closed 1 year ago.
Below is the input data
Type Cat Var Dist Count
#joy A1 + B1 x + y + z 0:25:75 4
.cet C1 + D1 p + q 50:50 2
sam E1 + F1 g 100:3:2 10
Below is the intended output
Type Cat Var Dist Count Output
#joy A1 + B1 x + y + z 0:25:75 4 #joyA1 + B1x + y +z
.cet C1 + D1 p + q 50:50 2 .cetC1 + D1p + q
sam E1 + F1 g 100:3:2 10 samE1 + F1g
Below is the try from my end:
df.iloc[:,0:3].dot(['Type','Cat','Var'])
You can do that using
df['output'] = df['Type'].map(str) + df['Cat'].map(str) + df['Var].map(str)
you can simply use:
df['Output']=df['Type']+' '+df['Cat']+' '+df['Var']
output:
Type Cat Var Dist Count output
0 #joy A1 + B1 x + y + z 0.018229167 4 #joy A1 + B1 x + y + z
1 .cet C1 + D1 p + q 50:50:00 2 .cet C1 + D1 p + q
2 sam E1 + F1 g 100:03:02 10 sam E1 + F1 g
Base R: Using paste0
df$Output <- paste0(df$Type, df$Cat, df$Var)
Type Cat Var Dist Count Output
1 #joy A1 + B1 x + y + z 0:25:75 4 #joy A1 + B1 x + y + z
2 .cet C1 + D1 p + q 50:50 2 .cet C1 + D1 p + q
3 sam E1 + F1 g 100:3:2 10 sam E1 + F1 g
OR
library(dplyr)
df %>%
mutate(Output = paste(Type, Cat, Var, sep = ""))
Type Cat Var Dist Count Output
1 #joy A1 + B1 x + y + z 0:25:75 4 #joy A1 + B1 x + y + z
2 .cet C1 + D1 p + q 50:50 2 .cet C1 + D1 p + q
3 sam E1 + F1 g 100:3:2 10 sam E1 + F1 g
OR:
library(tidyr)
df %>%
unite(Output, c(Type, Cat, Var), remove=FALSE)
Output Type Cat Var Dist Count
1 #joy_A1 + B1_x + y + z #joy A1 + B1 x + y + z 0:25:75 4
2 .cet_C1 + D1_p + q .cet C1 + D1 p + q 50:50 2
3 sam_E1 + F1_g sam E1 + F1 g 100:3:2 10
Thank you for reading my question.
I am a totally beginner for python and English is not my primary language. Please ask if you don't understand my English. Thank you for your help.
Here's the question. I want to create a Venn diagram using these criteria:
10*P(B) = P(A)
P(C and notA) = 13 * P(B and notC and notA)
P(B and C and notA) = 5 * P(B and C and A)
P(B and C) = 0.18
P(B or C and notA) = 0.07
P(notC) = 0.922 -> P(C) = 0.078
P(B and A) = 0.01
I tried using 'random' but I think it's quite dumb to do so.
This is my code, but if you have a better solution, please do tell me.
oa means Only A, i.e. (A and notB and notC)
abnc: means A and B No C
import random
# a = lowgrade fever
# b = headace
# c = Muscle ache
def haha():
oa = random.random()
ob = random.random()
oc = random.random()
abnc = random.random()
acnb = random.random()
bcna = random.random()
abc = random.random()
a = oa + abnc + acnb + abc
b = ob + abnc + bcna + abc
c = oc + acnb + bcna + abc
ab = abnc + abc
ac = acnb + abc
bc = bcna + abc
# oa + ob + oc + abnc + acnb+ bcna + abc = 1
if (10 * b == a):
c1 = True
if ((oc + bcna) == 13 * ob):
c2 = True
if ((ob + bcna + oc) == 5*abc):
c3 = True
if ((bcna + abc) == 0.018):
c4 = True
if(ob + bcna + oc == 0.07):
c5 = True
if(1 - oc + bcna + abc + acnb == 0.922):
c6 = True
if(abnc + abc == 0.01):
c7 = True
if (c1 and c2 and c3 and c4 and c5 and c6 and c7):
allc = True
if allc:
print(oa)
print(ob)
print(oc)
print(bcna)
print(acnb)
print(abnc)
print(abc)
return allc
haha()
while (allc == False):
haha()
Thank you guys.
You are looking to find 8 different probabilities. Where ~A means not A. These probabilities are the unknowns of our problem.
P( A & B & C) [1]
P( A & B & ~C) [2]
P( A & ~B & C) [3]
P( A & ~B & ~C) [4]
P(~A & B & C) [5]
P(~A & ~B & C) [6]
P(~A & ~B & ~C) [7]
P(~A & B & ~C) [8]
The first step is to write your equations in the variables written above. We will use [1] as a shorthand for P(A & B & C)
<=> 10P(B) - P(A)=0
<=> 10*([1] + [2] + [5] + [8]) - 1*(([1] + [2] + [3] + [4]) = 0
<=> 9*[1] + 9*[2] + -1*[3]-1*[4] + 10*[5] + 0*[6] + 0*[7] + 10*[8] = 0
We can do this for all 7 equations. In linear algebra this is written as a system Ax = b. You might remember that you need 8 equations if you have 8 unknown variables. To get a full solution you must find an extra equation. (Hint: what is the sum of all probabilities?)
To solve the exercise in python can use the following code.
import numpy as np
A = np.array([
[9, 9, -1, -1, 10, 0, 0, 10], # 10P(B) = P(A)
... # Insert 7 more equations here
])
b = np.array([0, ....]) # Insert 7 more numbers here
x = np.linalg.solve(A,b)
print(x)
For convenience, I will use the following shorthand notation:
[1--] denotes P(A),
[0--] denotes P(not A),
[0-1] denotes P(not A and C),
[110] denotes P(A and B and not C),
etc. Using this notation your problem statement can be rewritten as follows:
1) 10*[-1-] = [1--]
2) [0-1] = 13*[010]
3) [011] = 5*[111]
4) [-11] = 0.18
5) [-1-] + [001] = 0.07
6) [--1] = 0.078
7) [11-] = 0.01
Let us now try to solve it manually:
Rewrite 4)
[011] + [111] = 0.18
Substitute 3 into 4)
5*[111] + [111] = 0.18
[111] = 0.03
Rewrite 7) and substitute [111]=0.03
[110] + [111] = 0.01
[110] = -0.02
A probability cannot be negative, hence we may stop here and conclude that there is no probabilistic solution to your problem.
The obtained result can also be explained as follows:
P(ABC) ≤ P(AB) = 0.01
P(~ABC) = 5*P(ABC) ≤ 0.05
Therefore, P(BC) = P(ABC) + P(~ABC) ≤ 0.01 + 0.05 = 0.06
This contradicts condition 4.
I am trying to understand the following code where I need to determine the output that will be printed. However, I am stuck halfway. The code (sorry I can't seem to get the spacing right):
# Let the classes A and B be
class A:
def __init__ (self):
self.i = 3
def doubled (self):
self.i *= 2
class B:
def __init__ (self, an_a_object):
self.a = an_a_object
def put (self, an_a_object):
self.a = an_a_object
# Further we have a program with the following statements/declarationsa
a1 = A()
b1 = B(a1)
def show (an_a_object, a_b_object):
print '%d %d' % (an_a_object.i, a_b_object.a.i)
show(a1, b1)
b1.a.doubled()
show(a1, b1)
a2 = A()
show(a2, b1)
b1.put(a2)
show(a2, b1)
b2 = B(a1)
show(a1, b1)
b2.a.doubled()
show(a2, b2)
b1.a.doubled()
b2.put(b1.a)
show(a2, b2)
What will be printed by this program?
The desired output:
3 3
6 6
3 6
3 3
6 3
3 12
6 6
I understand until 6 3, but after that I don't get it. I thought b2.a.doubled() would double b2 (which I thought to be 3 before doubling, since b2 = B(a1)), but apparently b2 is 12 when doubled? And I thought a2 would be doubled as well, but a2 remains 3? Then why after the first b1.a.doubled(), both a1 and b1 are doubled (since it goes from 3 3 to 6 6)?
I thought b2.a.doubled() would double b2
correct.
(which I thought to be 3 before doubling,
not correct. It is 6
since b2 = B(a1)),
and a1 was 6, since it was doubled in line b1.a.doubled()
As chepner commented,
Changes made via one reference are visible via the other.
Code with correct identation:
class A:
def __init__(self):
self.i = 3
def doubled(self):
self.i *= 2
class B:
def __init__(self, an_a_object):
self.a = an_a_object
def put(self, an_a_object):
self.a = an_a_object
def show(an_a_object, a_b_object):
print '%d %d' % (an_a_object.i, a_b_object.a.i)
a1 = A()
b1 = B(a1)
show(a1, b1)
b1.a.doubled()
show(a1, b1)
a2 = A()
show(a2, b1)
b1.put(a2)
show(a2, b1)
b2 = B(a1)
show(a1, b1)
b2.a.doubled()
show(a2, b2)
b1.a.doubled()
b2.put(b1.a)
show(a2, b2)
Inside class B, self.a refers to the same object as created by a call to A(). modifiying a is the same as modifying b.a and both instances will have the same value.
This code is tricky to read due to many variables with similar names. I recomend you to use more explicative names that allow for easier reading.
It can be interesting to print all results and analyze. I changed the __repr__ function for class A and B, so we can print them easily between each operation. I added the output between each "print" line.
class A:
def __init__ (self):
self.i = 3
def doubled (self):
self.i *= 2
def __repr__ (self):
return str(self.i)
class B:
def __init__ (self, an_a_object):
self.a = an_a_object
def put (self, an_a_object):
self.a = an_a_object
def __repr__ (self):
return str(self.a.i)
a1 = A()
b1 = B(a1)
a2 = None
b2 = None
def show():
print("a1 =", a1, ", a2 =", a2, ", b1 =", b1, ", b2 =", b2)
show()
# a1 = 3 , a2 = None , b1 = 3 , b2 = None
b1.a.doubled()
show()
# a1 = 6 , a2 = None , b1 = 6 , b2 = None
a2 = A()
show()
# a1 = 6 , a2 = 3 , b1 = 6 , b2 = None
b1.put(a2)
show()
# a1 = 6 , a2 = 3 , b1 = 3 , b2 = None
b2 = B(a1)
show()
# a1 = 6 , a2 = 3 , b1 = 3 , b2 = 6
b2.a.doubled()
show()
# a1 = 12 , a2 = 3 , b1 = 3 , b2 = 12
b1.a.doubled()
show()
# a1 = 12 , a2 = 6 , b1 = 6 , b2 = 12
b2.put(b1.a)
show()
# a1 = 12 , a2 = 6 , b1 = 6 , b2 = 6
I am trying to print a combination of np.array values, a string and and some values I get from an iterator.
The code looks like this:
import numpy as np
site = np.genfromtxt('.....\Plot_1.txt', dtype=None, delimiter='\t')
c1 = np.array([148, 108])
c2 = np.array([181, 147])
c3 = np.array([173, 153])
c4 = np.array([98, 221])
c5 = np.array([43, 153])
trees_list = [c1, c2, c3, c4, c5]
def trees_pixel(rc_list, matrix):
t_row = rc_list[0]
t_col = rc_list[1]
tree = matrix[t_row, t_col]
for i in range(1, 6, 1):
print "C",i,"=",tree
return tree
for i in trees_list:
trees_pixel(i, site)
Site is a np.array of 400x370 row/columns, that I need to read the values from. C1...C5 are the locations (row/column) from the 'site' array.
My code prints the following:
C 1 = 8.266602
C 2 = 8.266602
C 3 = 8.266602
C 4 = 8.266602
C 5 = 8.266602
C 1 = 17.89282
C 2 = 17.89282
C 3 = 17.89282
C 4 = 17.89282
C 5 = 17.89282
C 1 = 18.31433
C 2 = 18.31433
C 3 = 18.31433
C 4 = 18.31433
C 5 = 18.31433
etc...
But what I expected was:
C 1 = 8.266602
C 2 = 17.89282
C 3 = 18.31433
C 4 = 20.47229
C 5 = 13.5907
How can I do this, so I will avoid the repeating pattern? Thanks!
You're iterating twice, once inside trees_pixel and once outside of it. If I understand what you mean, you want something that looks like the following:
import numpy as np
site = np.random.random((400, 370)) # Used in place of your data
c1 = np.array([148, 108])
c2 = np.array([181, 147])
c3 = np.array([173, 153])
c4 = np.array([98, 221])
c5 = np.array([43, 153])
trees_list = [c1, c2, c3, c4, c5]
def trees_pixel(rc_list, listIdx, matrix):
t_row = rc_list[0]
t_col = rc_list[1]
tree = matrix[t_row, t_col]
print "C",listIdx,"=",tree
return tree
for i in xrange(len(trees_list)):
trees_pixel(trees_list[i], i+1, site)
C 1 = 0.820317259854
C 2 = 0.960883528796
C 3 = 0.363985436225
C 4 = 0.189575015844
C 5 = 0.667578060856