import numpy as np
def AND(x1, x2):
x = np.array(x1,x2)
w = np.array(0.5,0.5)
b = -0.7
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
def NAND(x1, x2):
x = np.array([x1, x2])
w = np.array([-0.5, -0.5])
b = 0.7
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
print(NAND(1,0))
print(NAND(1,1))
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
print(OR(0,1))
print(OR(0,0))
def XOR(x1, x2):
s1 = NAND(x1,x2)
s2 = OR(x1,x2)
y = AND(s1,s2)
return y
print(XOR(0,1))
I completed AND, OR, NAND gate. and they all works really precisely as i expected. And finally i tried to make a XOR logic by combining NAND, OR, AND in sequence... But here comes the traceback error message i 've never expected like below. What should be modified to fulfill my original purpose.
TypeError: Cannot interpret '1' as a data type
The first (and second) line of AND is not the same as the first line of OR and NAND. That's the problem. See it?
Ironic that you had unit tests for OR and NAND, but not for AND.
Related
Finding the positions of multiple objects in an image, but only when they are next to each other. So I would like to use parameters to decide whether 2 or 3 are next to each other and then get the coordinates.
At the moment I can only find one and it works. Code:
def diff(a, b):
return sum((a - b) ** 2 for a, b in zip(a, b))
def g_c_o(c, _b):
time.sleep(1)
s_i_p = ''
c_b_64 = _b.execute_script("return arguments[0].toDataURL('image/png').substring(21);", c)
c_i = base64.b64decode(c_b_64)
with open(r"canvas.png", 'wb') as f:
f.write(c_i)
with open("files/important.pickle", "rb") as f:
d_r = pickle.load(f)
if s_i_p == '' and d_r[10] is not None and d_r[10] != '':
s_i_p = d_r[10]
c_s_i = Image.open('canvas.png')
i_s = c_s_i.size
s_i = Image.open("findMe.png")
w_s = s_i.size
x0, y0 = w_s[0] // 2, w_s[1] // 2
p = s_i.getpixel((x0, y0))[:-1]
b = (100, 0, 0)
c = []
for x in range(i_s[0]):
for y in range(i_s[1]):
i_p_s = c_s_i.getpixel((x, y))
d = diff(i_p_s, p)
if d < b[0]:
b = (d, x, y)
x, y = b[1:]
return [x, y]
And here the images, if this can help:
Image to find:
Find in:
And it works, i find this:
Can anyone tell me if I can find 2 or 3 that are next to each other and coordinates of them, not just the one?
Can anyone tell me if I can find 2 or 3 that are next to each other and coordinates of them, not just the one?
I got some if/elif structures. I have no errors when it runs, but the 3 outputs are the same (they should not be). I can not really see, what is the cause of this problem.
The concerned part of my code: (RGB_l is a 1 by 3 matrix, I create a variable for each of its component and apply an if/elif structure, to decide which calculation needs to be done).
X = float(input("X: "))/100
Y = float(input("Y: "))/100
Z = float(input("Z: "))/100
b = np.array([[X],
[Y],
[Z]])
a = np.array([[3.2406, -1.5372, -0.4986],
[-0.9689, 1.8758, 0.0415],
[0.0557, -0.2040, 1.057]])
RGB_l = np.dot(a, b)
print("RGB linear :", RGB_l)
R_l = RGB_l[0, 0]
G_l = RGB_l[1, 0]
B_l = RGB_l[2, 0]
a = 0.055
if R_l <= 0.0031308:
R = 12.92*R_l
elif R_l > 0.0031308:
R = ((1+a)**(1/2.4))-a
print(R)
if G_l <= 0.0031308:
G = 12.92*G_l
elif G_l > 0.0031308:
G = ((1+a)**(1/2.4))-a
print(G)
if B_l <= 0.0031308:
B = 12.92*B_l
elif B_l > 0.0031308:
B = ((1+a)**(1/2.4))-a
print(B)
Outputs for R, G and B are :
0.967559351663262
0.967559351663262
0.967559351663262
Could you help me to fix my code? Thanks
All the values you have are greater than 0.0031308
and so the print statement for each is ((1+a)**(1/2.4))-a
the actual value in the array isn't part of the output.
So I am very unexperienced with Python, I know basically nothing, and our teacher gave us the task to write a code that makes a partial fraction decomposition with this function:
I don't really know how to start or even how to define that function. I tried this at first: `
def function(x):
a = (x^4)-(3*x^2)+x+5
b = (x^11)-(3*x^10)-(x^9)+(7*x^8)-(9*x^7)+(23*x^6)-(11*x^5)-(3*x^4)-(4*x^3)-(32*x^2)-16
return a/b
But our maths script says that we need to split up the denominator and then make a system of equations out of it and solve it.
So I was thinking about defining each part of the function itself and then make a function somehow like a = 7*x and use it like f(x) = b/a^7 if this works but I don't really know. We are unfortunately not allowed to use "apart" which I think is a sympy-function?
Thank you so much in advance!
Sincerely, Phie
Addition: So after a few hours of trying I figured this. But I am very sure that this is not the way to do it. Also it tells me that variable l is not defined in z and I am sure that all the others aren't as well. I don't know what to do.
def function(x):
global a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v
a = (x^4)-(3*x^2)+x+5
b = 11
c = 10
d = 9
e = 8
f = 7
g = 6
h = 5
i = 4
j = 3
k = 2
l = x**b
m = 3*x**c
n = x**d
o = 7*x**e
p = 9*x**f
q = 23*x**g
r = 11*x**h
s = 3*x**i
t = 4*x**j
u = 32*x**k
v = 16
return a/(l-m-n+o-p+q-r-s-t-u-v)
print("We are starting the partial fraction decomposition with this
function: (x^4)-(3*x^2)+x+5 / (x^11)-(3*x^10)-(x^9)+(7*x^8)-(9*x^7)+
(23*x^6)-(11*x^5)-(3*x^4)-(4*x^3)-(32*x^2)-16")
z = l-m-n+o-p+q-r-s-t-u-v
while c >= 0:
c = c-1
z = z-l
while d >= 0:
d = d-1
z = z-m
while e >= 0:
e = e-1
z = z-n
while f >= 0:
f = f-1
z = z+o
while g >= 0:
g = g-1
z = z-p
while h >= 0:
h = h-1
z = z+q
while i >= 0:
i = i-1
z = z-r
while j >= 0:
j = j-1
z = z-s
while k >= 0:
k = k-1
z = z-t
print(z)
Since I just solved this myself, here's some input:
Let poly = function() for your function, although be careful to replace ^ with **. Include both from sympy import * and from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, x.
Using factor(exp) you can find all the roots of your function, use these to define the 11 terms term_1 = a/(x-2), term_2 = b/(x2-)**2, ... , term_6 = (f*x + g)/(x**2 +1), ..., term_8 = (j*x + k)/(x**2 + 1) (you get the gist). Define your_sum = term_1 + ... + term_8, eq = Eq(your_sum, poly)
Define the variable your_sum = sum(term_1, ..., term_8), and use solve_undetermined_coeffs(eq, [a,b, ..., k], x))) to get the result.
I am trying to solve the following question. I have made codes for implanting the direct method and Horner's rule, which I believe I have done this correctly. However past that I am having some problems figuring out the rest. Looking for some help with this, all help is greatly appreciated!
Here is the code I have produced for Horner's rule, which I believe I have done correctly.
def poly_horner(A, x):
p = A[-1]
i = len(A) - 2
while i >= 0:
p = p * x + A[i]
i -= 1
return p
And here is the code I have produced for the direct method:
def poly_naive(A, x):
p = 0
for i, a in enumerate(A):
p += (x ** i) * a
return p
How can I put this code together and finish the rest?
Using global as suggested in the paper,
flops = 0
def add(x1, x2):
global flops
flops += 1
return x1 + x2
def multiply(x1, x2):
global flops
flops += 1
return x1 * x2
def poly_horner(A, x):
global flops
flops = 0
p = A[-1]
i = len(A) - 2
while i >= 0:
p = add(multiply(p, x), A[i])
i -= 1
return p
def poly_naive(A, x):
global flops
flops = 0
p = 0
for i, a in enumerate(A):
xp = a
for _ in range(i):
xp = multiply(xp, x)
p = add(p, xp)
return p
To run the above code, for example:
>>> poly_horner([1,2,3,4,5], 2)
129
>>> print(flops)
8
Compare to numpy's polyval:
>>> import numpy as np
>>> np.polyval([5,4,3,2,1], 2)
129
As say in the title i'm actually using a triple for loop to treat a numpy matrix called A in my python program and , as we can expect, it's very slow.
I have a function that we gonna call "return_bool" which return a boolean in function of a parameter h and i and j the index of the matrix element. I want to use this function for several value of h to make a type of "gradient matrix".
Here is my code :
A = np.zero(iindex,jindex)
for h in hvalue:
for in iindex:
for j in jindex:
if (return_bool(h,i,j) : A[i][j] =+ 1
Are there any tips to increase the speed of this matrix scanning ?
Here is the code of my "return_bool" function (All the value are float or points (x,y) with x and y points) :
def inclu_geo(coord1,coord2,y1,y2 , hh , hh2,y1droite,y2droite, hhdroite , hh2droite,intersec1,intersec2,rho):
global y
global yprime
global largeur_pale
#equation des droites
equdroite1 = eqdroite(y1,y2,hh,hh2)
equdroite2 = eqdroite(y1droite,y2droite,hhdroite,hh2droite)
if 0< rho < 90 :
if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
if inclu(intersec1[0],intersec1[1]):
b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((intersec1[0]<=coord1<=yprime)and(hh2<=coord2<=intersec1[1]))or((y1<=coord1<=intersec1[0])and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
if inclu(intersec2[0],intersec2[1]):
b = ((y<=coord1<=intersec2[0])and(hh2droite<=coord2<=intersec2[1]))or((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((intersec2[0]<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
else:
if (hh != 0) and inclu(y1,hh):
b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((y1<=coord1<=y2)and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
elif (hhdroite != 0) and inclu(y1droite,hhdroite):
b = ((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((y2droite<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
elif (hhdroite != 0) or (hh != 0):
b = True
else:
b = False
else:
if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
if inclu(intersec1[0],intersec1[1]):
b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((intersec1[0]<=coord1<=yprime)and(intersec1[1]<=coord2<=hh2))or((y1<=coord1<=intersec1[0])and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
if inclu(intersec2[0],intersec2[1]):
b = ((y<=coord1<=intersec2[0])and(intersec2[1]<=coord2<=hh2droite))or((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((intersec2[0]<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
else:
if (hh != largeur_pale) and inclu(y1,hh):
b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((y1<=coord1<=y2)and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
elif (hhdroite != largeur_pale) and inclu(y1droite,hhdroite):
b = ((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((y2droite<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
elif (hhdroite != largeur_pale) or (hh != largeur_pale):
b = True
else:
b = False
return b
I rewrite my return_bool(h,i,j) to treat line by line the matrix (return_bool(h,i)), with this method it take 2,5 sec instead of 416 sec , so the problem is solve i guess.
I used "&" and "|" to adapt my logicals equations to an entire line.
Thank you all for your help.