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
Related
Iam doing GCD tool in python but for some reason every time I use 2 negative values I get back negative value back. Beceause this is for school i cant use things like obs() ort math etc.. Can some help me with that?
from sys import stdin
a = 0
b = 0
a0 = 0
b0 = 0
a1 = 0
b1 = 0
n = 0
na = 0
nb = 0
q = 0
for line in stdin:
input = line.lstrip().rstrip().split()
if line == '' or len(input) != 2:
break
a, b = [int(x) for x in line.lstrip().rstrip().split()]
if a > b:
a, b = b, a
#
# a | b
# +---------+
# a | 1 | 0 | "( a0, b0 )"
# b | 0 | 1 | "( a1, b1 )"
# n | na | nb | q
# | | |
#
#
a0 = 1
b0 = 0
a1 = 0
b1 = 1
n = a % b
q = a / b
na = a0 - q * a1
nb = b0 - q * b1
a = b
a0 = a1
b0 = b1
b = n
a1 = na
b1 = nb
while n != 0:
n = a % b
q = a // b
na = a0 + q * a1
nb = b0 + q * b1
a = b
a0 = a1
b0 = b1
b = n
a1 = na
b1 = nb
print(a)
I tried messing around with operators. I expect to -888 -2 be 2 not -2 (I need to fix the code not to edit results )
Edit 1 : Here are some examples of what i need
Input Output
7 11 1
888 2 2
905 5 5
-7 11 1
-888 -2 2
905 -5 5
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
I am trying to minimize 2 functions with 2 variables at the same time.
I have a set of data and 2 equations like:
B1 = 4
P1 = 6
G1 = 2
E1 = 3
F1 = B - G - E = -1
F2 = P - G - E = 1
Given a new B2 = 5 and P2 = 6 I would like to calculate the variables G2, E2 so that the difference between the old values F1 = -1 and F2 = 1 and the new values F3 and F4 are minimized:
B2 = 5
P2 = 6
G2 = ?
E2 = ?
F3 = B2 - G2 - E2 ---> as close as possible to F1
F4 = P2 - G2 - E2 ---> as close as possible to F2
I was trying:
def diff(param):
G2, E2 = param
return abs(B1 - G2 - E2 - F1)
x0 = [2,2]
res = minimize(diff, x0)
This resolve properly the minimization that gives F3 = F1 = -1, but does not solve my intention to also minimize the difference (F4 - F2).
Do you know how to include also the second minimization problem?
thank you in advance
I am not sure if my approach is too naive (it probably is, to be honest), but considering that you want to minimize two differences, meaning that each difference must tend to zero, then why don't you try to minimize the sum of the differences? This sum should also tend to zero. It would look like that:
from scipy.optimize import minimize
def diff(x):
G2, E2 = x
return (abs(B2 - G2 - E2 - F1 + P2 - G2 - E2 - F2))
B1 = 4
P1 = 6
G1 = 2
E1 = 3
F1 = B1 - G1 - E1
F2 = P1 - G1 - E1
B2 = 5
P2 = 6
res = minimize(diff, x0=(1, 1))
res.x returns [2.75, 2.75], which gives F3 = -0.5, F4 = 0.5.
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))
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