In [382]:
from numpy import roots, random, array, linspace
from numpy import fabs as fb
from math import atan, fabs, pi, tan, cos, sin, sqrt, tan
import sympy
from sympy import Symbol
def cubic (Qx, Qy, Ux, Uy):
a = Uy
b = (Ux + 2*Qy)
c = (2*Qx - Uy)
d = -Ux
det = -4*b*b*b*d + b*b*c*c - 4*a*c*c*c + 18*a*b*c*d - 27*a*a*d*d
if (det < 0):
return 'c'
if (det > 0):
a = roots([a, b, c, d])
a = a.real
a = [atan(a[0]), atan(a[1]), atan(a[2])]
U = [Ux*cos(a[0]) + Uy*sin(a[0]), Ux*cos(a[1]) + Uy*sin(a[1]), Ux*cos(a[2]) + Uy*sin(a[2])]
rightU = [2*sin(a[0])*cos(a[0]), 2*sin(a[1])*cos(a[1]), 2*sin(a[2])*cos(a[2])]
for i in range(0, 3):
if (U[i] * rightU[i] < 0):
a[i] = a[i] + pi
print a[0]*(180/pi), a[1]*(180/pi), a[2]*(180/pi)
a = sorted(a)
a = [a[0] - a[0], a[1] - a[0], a[2] - a[0]]
if (a[2] > pi):
return 'a'
else:
return 'b'
In [346]:
mu, sigma = 0, 1
In [524]:
Ux = 1.0
Uy = 1.0
Qx = -1.0
Qy = 1.1
In [525]:
print cubic(Qx, Qy, Ux, Uy)
In [526]:
U = array([-2*Ux + 2*Uy, -Ux + 2*Uy, 2*Uy, Ux + 2*Uy, 2*Ux + 2*Uy,-2*Ux + Uy, -Ux + Uy, Uy, Ux + Uy, 2*Ux + Uy,-2*Ux, -Ux, 0.0, Ux, 2*Ux,-2*Ux - Uy, -Ux - Uy, -Uy, Ux - Uy, 2*Ux - Uy,-2*Ux - 2*Uy, -Ux - 2*Uy, -2*Uy, Ux - 2*Uy, 2*Ux - 2*Uy])
Q = array([-2*Qx + 2*Qy, -Qx + 2*Qy, 2*Qy, Qx + 2*Qy, 2*Qx + 2*Qy,-2*Qx + Qy, -Qx + Qy, Qy, Qx + Qy, 2*Qx + Qy,-2*Qx, -Qx, 0.0, Qx, 2*Qx,-2*Qx - Qy, -Qx - Qy, -Qy, Qx - Qy, 2*Qx - Qy,-2*Qx - 2*Qy, -Qx - 2*Qy, -2*Qy, Qx - 2*Qy, 2*Qx - 2*Qy])
P_x = Ux * U + Q * Qx
P_y = Uy * U + Q * Qy
In [527]:
P1_x = array([-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2])
P1_y = array([2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2])
P2_x = P1_x + P_x*0.1*np.reciprocal(fb(P_x))
P2_y = P1_y + P_y*0.1*np.reciprocal(fb(P_y))
P3_x = P1_x - P_x*0.1*np.reciprocal(fb(P_x))
P3_y = P1_y - P_y*0.1*np.reciprocal(fb(P_y))
In [528]:
P_x*0.1*np.reciprocal(fb(P_x))
Out[528]:
In [529]:
print P2_x
print P2_y
In [530]:
print P_x*np.reciprocal(P_y)
In [531]:
P2_x
Out[531]:
In [532]:
import matplotlib.pyplot as plt
%matplotlib inline
In [533]:
plt.plot(P1_x, P1_y, 'ro')
plt.plot(P2_x, P2_y, 'go')
plt.plot(P3_x, P3_y, 'bo')
plt.show()
In [339]:
Ux = random.normal(mu, sigma, 1000000)
Uy = random.normal(mu, sigma, 1000000)
Qx = random.normal(mu, sigma, 1000000)
Qy = random.normal(mu, sigma, 1000000)
In [340]:
a, b, c = 0, 0, 0
for i in range(0, 100000):
ch = ''
ch = cubic(Qx[i], Qy[i], Ux[i], Uy[i])
if (ch == 'a'):
a = a + 1
if (ch == 'b'):
b = b + 1
if (ch == 'c'):
c = c + 1
print a/100000.0, b/100000.0, c/100000.0
In [205]:
P_x
Out[205]:
In [210]:
P_y
Out[210]:
In [ ]: