def int_temp(kA): qp = acos( 0.5*(-cos(kA) + sqrt( (E**2/t**2) - (sin(kA))**2 ) ) ) qm = acos( 0.5*(-cos(kA) - sqrt( (E**2/t**2) - (sin(kA))**2 ) ) ) if qp.imag < 0.0: qp = -qp if qm.imag < 0.0: qm = -qm sig = copysign(1,m-n) const = 1j/(4.0*pi*t**2) if s == 0: return const*E*( exp( 1j*( kA*(m+n) + sig*qp*(m-n) ) ) / ( sin(2*qp) + sin(qp)*cos(kA) ) \ + exp( 1j*( kA*(m+n) + sig*qm*(m-n) ) ) / ( sin(2*qm) + sin(qm)*cos(kA) ) ) elif s == 1: fp = t*( 1.0 + 2.0*cos(qp)*exp(1j*kA) ) fm = t*( 1.0 + 2.0*cos(qm)*exp(1j*kA) ) return const*( exp( 1j*( kA*(m+n) + sig*qp*(m-n) ) )*fp / ( sin(2*qp) + sin(qp)*cos(kA) ) \ + exp( 1j*( kA*(m+n) + sig*qm*(m-n) ) )*fm / ( sin(2*qm) + sin(qm)*cos(kA) ) ) elif s == -1: ftp = t*( 1.0 + 2.0*cos(qp)*exp(-1j*kA) ) ftm = t*( 1.0 + 2.0*cos(qm)*exp(-1j*kA) ) return const*( exp( 1j*( kA*(m+n) + sig*qp*(m-n) ) )*ftp / ( sin(2*qp) + sin(qp)*cos(kA) ) \ + exp( 1j*( kA*(m+n) + sig*qm*(m-n) ) )*ftm / ( sin(2*qm) + sin(qm)*cos(kA) ) ) else: print "Sublattice error in gBulk_kA"
def int_temp(kA): qp = acos( 0.5*(-cos(kA) + sqrt( (E**2/t**2) - (sin(kA))**2 ) ) ) qm = acos( 0.5*(-cos(kA) - sqrt( (E**2/t**2) - (sin(kA))**2 ) ) ) if qp.imag < 0.0: qp = -qp if qm.imag < 0.0: qm = -qm sig = copysign(1,DZ) # Check this versus zigzag SI (should be same though, on exponential) const = 1j/(2.0*pi*t**2) if s_lat == 'bb': return const*E*exp(1j*sig*qp*(DZ))*sin(kA*DA1)*sin(kA*DA2) / ( sin(2*qp) + sin(qp)*cos(kA) ) \ + const*E*exp(1j*sig*qm*(DZ))*sin(kA*DA1)*sin(kA*DA2) / ( sin(2*qm) + sin(qm)*cos(kA) ) elif s_lat == 'ww': fp = 1.0 + 2.0*cos(qp)*exp(1j*kA) fm = 1.0 + 2.0*cos(qm)*exp(1j*kA) ftp = 1.0 + 2.0*cos(qp)*exp(-1j*kA) ftm = 1.0 + 2.0*cos(qm)*exp(-1j*kA) fpasq = 1.0 + 4.0*cos(qp)*cos(kA) + 4.0*cos(qp)**2 fmasq = 1.0 + 4.0*cos(qm)*cos(kA) + 4.0*cos(qm)**2 Np = ( ftp*exp(-1j*kA*DA1) - fp*exp(1j*kA*DA1) )*( fp*exp(1j*kA*DA2) - ftp*exp(-1j*kA*DA2) )/fpasq Nm = ( ftm*exp(-1j*kA*DA1) - fm*exp(1j*kA*DA1) )*( fm*exp(1j*kA*DA2) - ftm*exp(-1j*kA*DA2) )/fmasq return 0.25*const*E*exp(1j*sig*qp*DZ)*Np / ( sin(2*qp) + sin(qp)*cos(kA) ) \ + 0.25*const*E*exp(1j*sig*qm*DZ)*Nm / ( sin(2*qm) + sin(qm)*cos(kA) ) else: print 's_lat not a valid character'
def isparal(a1, a2, a3, a4): a1a2 = m.sqrt(pow((a2[0] - a1[0]), 2) - pow((a2[1] - a1[1]), 2)) a2a3 = m.sqrt(pow((a3[0] - a2[0]), 2) - pow((a3[1] - a2[1]), 2)) a3a4 = m.sqrt(pow((a4[0] - a3[0]), 2) - pow((a4[1] - a3[1]), 2)) a4a1 = m.sqrt(pow((a1[0] - a4[0]), 2) - pow((a1[1] - a4[1]), 2)) d1 = m.sqrt(pow((a4[0] - a2[0]), 2) - pow((a4[1] - a2[1]), 2)) d2 = m.sqrt(pow((a3[0] - a1[0]), 2) - pow((a3[1] - a1[1]), 2)) # Теорема треугольников для нахождения угла по трем сторонам cos_alpha1 = (pow(d1, 2) - pow(a1a2, 2) - pow(a4a1, 2)) / 2 * a1a2 * a4a1 alpha1 = m.acos(cos_alpha1) cos_beta1 = (pow(d1, 2) - pow(a2a3, 2) - pow(a3a4, 2)) / 2 * a2a3 * a3a4 beta1 = m.acos(cos_beta1) cos_alpha2 = (pow(d2, 2) - pow(a1a2, 2) - pow(a2a3, 2)) / 2 * a1a2 * a2a3 alpha2 = m.acos(cos_alpha2) cos_beta2 = (pow(d2, 2) - pow(a4a1, 2) - pow(a3a4, 2)) / 2 * a4a1 * a3a4 beta2 = m.acos(cos_beta2) if a1a2 == a3a4 and a2a3 == a4a1: print("Стороны попарно равны!") if alpha1 == beta1 and alpha2 == beta2: print("Противоположные углы попарно равны!") print("Это - параллелограмм!") else: print( "Противоположные углы попарно не равны - это не параллелограм..." ) else: print("Стороны попарно не равны - это не параллелограм...")
def gTube_Python(nC, m, n, s, E): """The Green's Function of a Carbon Nanotube, written in Python because you never know. Problem: k is a really weird choice for index given that it alludes to the Fermi wavevector""" g = 0.0 for k in range(0, nC): # The zero is unnecessary by Python convention qp = acos(0.5 * (-cos(pi * k / nC) + sqrt((E**2 / t**2) - (sin(pi * k / nC))**2))) qm = acos(0.5 * (-cos(pi * k / nC) - sqrt((E**2 / t**2) - (sin(pi * k / nC))**2))) if qp.imag < 0.0: qp = -qp if qm.imag < 0.0: qm = -qm sig = copysign(1, m - n) const = 1j / (4.0 * t**2) if s == 0: g += const*( E*exp( 1j*( pi*k/nC *(m+n) + sig*qp*(m-n) ) ) / ( sin(2*qp) + sin(qp)*cos(pi*k/nC) ) \ + E*exp( 1j*( pi*k/nC *(m+n) + sig*qm*(m-n) ) ) / ( sin(2*qm) + sin(qm)*cos(pi*k/nC) ) ) elif s == 1: fp = t * (1.0 + 2.0 * cos(qp) * exp(1j * pi * k / nC)) fm = t * (1.0 + 2.0 * cos(qm) * exp(1j * pi * k / nC)) g += const*( fp*exp( 1j*( pi*k/nC *(m+n) + sig*qp*(m-n) ) ) / ( sin(2*qp) + sin(qp)*cos(pi*k/nC) ) \ + fm*exp( 1j*( pi*k/nC *(m+n) + sig*qm*(m-n) ) ) / ( sin(2*qm) + sin(qm)*cos(pi*k/nC) ) ) elif s == -1: ftp = t * (1.0 + 2.0 * cos(qp) * exp(-1j * pi * k / nC)) ftm = t * (1.0 + 2.0 * cos(qm) * exp(-1j * pi * k / nC)) g += const*( exp( 1j*( pi*k/nC *(m+n) + sig*qp*(m-n) ) )*ftp / ( sin(2*qp) + sin(qp)*cos(pi*k/nC) ) \ + exp( 1j*( pi*k/nC *(m+n) + sig*qm*(m-n) ) )*ftm / ( sin(2*qm) + sin(qm)*cos(pi*k/nC) ) ) else: print "Sublattice error in gTube_Python" return g / nC
def calculate_solutions(self): self.C = 3 * self.b**2 - 8 * self.a * self.c self.D = 2 * self.b**3 - 8 * self.a * self.b * self.c + 16 * self.a * self.a * self.d self.E = -3 * self.b ** 4 + 16 * self.a * self.b ** 2 * self.c - 64 * self.a ** 2 * self.b * self.d +\ 256 * self.a ** 3 * self.e self.X = self.C**2 + 3 * self.E self.Y = -self.C**3 + 9 * self.C * self.E + 27 * self.D**2 if self.X > 0: self.W += [ sqrt((self.C + sqrt(self.X) * cos(acos(self.Y / sqrt(self.X**3)) / 3)) / 3), sqrt((self.C + sqrt(self.X) * cos(acos(self.Y / sqrt(self.X**3)) / 3 - 2 * pi / 3)) / 3), sqrt( (self.C + sqrt(self.X) * cos(acos(self.Y / sqrt(self.X**3)) / 3 - 4 * pi / 3)) / 3) ] if self.Y * self.Y >= self.X * self.X * self.X: self.W += [ sqrt((2 * self.C + cubic_root(self.Y + msqrt(self.Y**2 - self.X**3)) + cubic_root(self.Y - msqrt(self.Y**2 - self.X**3))) / 6) ] self.W = sorted(self.W, key=abs) self.Z = self.W[-1]
def acosTheta(a, b): if b > 0: return cmath.acos(-cmath.sqrt((b**2 / 4.0)/(-a**3/27.0))) else: return cmath.acos(cmath.sqrt((b**2 / 4.0)/(-a**3/27.0)))
def acos(self, x) -> complex: """ Returns the acos of the input, handles output in radians and degrees. """ if self.use_radians: return cmath.acos(x) else: return degrees(cmath.acos(x))
def Snell(c1, c2, theta1): arg = c2 / c1 * m.cos(theta1) # branch cut if (arg.real > 1) and (arg.imag == 0): print("on the branch cut") print(theta1) # raise ValueError("arg is on branch cut") return m.acos(arg) return m.acos(arg)
def read_int_positions(): # read intermediate positions n_structures = 0 for line in f_in: item = re.search(r" number of atoms/cell =", line) if item: n_atoms = int(re.search(r"\d+", line).group()) item = re.search(r"CELL_PARAMETERS \(alat=", line) if item: n_structures = n_structures + 1 print("Structure: " + str(n_structures)) alat = float(re.search(r"\d+\.\d{8}", line).group()) line = f_in.readline() a_vect = re.findall(r"-*\d+\.\d{9}", line) a_vect = [float(x) * alat * BOHR for x in a_vect] a_vect = np.asarray(a_vect) line = f_in.readline() b_vect = re.findall(r"-*\d+\.\d{9}", line) b_vect = [float(x) * alat * BOHR for x in b_vect] b_vect = np.asarray(b_vect) line = f_in.readline() c_vect = re.findall(r"-*\d+\.\d{9}", line) c_vect = [float(x) * alat * BOHR for x in c_vect] c_vect = np.asarray(c_vect) a = np.linalg.norm(a_vect) b = np.linalg.norm(b_vect) c = np.linalg.norm(c_vect) alpha = math.degrees( (cmath.acos(np.dot(b_vect, c_vect) / (b * c))).real) beta = math.degrees( (cmath.acos(np.dot(a_vect, c_vect) / (a * c))).real) gamma = math.degrees( (cmath.acos(np.dot(a_vect, b_vect) / (a * b))).real) data_title = "structure_" + str(n_structures) print_cif_header(a, b, c, alpha, beta, gamma, data_title) item = re.search(r"ATOMIC_POSITIONS\s\(angstrom\)", line) if item: for n in range(n_atoms): line = f_in.readline() el_name = re.search(r"[A-Z][a-z]{0,1}", line).group() atom = re.findall(r"-*\d+\.\d{9}", line) atom = [float(a) for a in atom] atom = np.asarray(atom) omega = np.dot(a_vect, np.cross(b_vect, c_vect)) u = np.dot(atom, np.cross(b_vect, c_vect)) / omega v = np.dot(atom, np.cross(c_vect, a_vect)) / omega w = np.dot(atom, np.cross(a_vect, b_vect)) / omega f_out.write("{} {:16.10f} {:16.10f} {:16.10f}\n".format( el_name, u, v, w))
def angle_between(v1, v2): """ Returns the angle in radians between vectors 'v1' and 'v2':: """ v1_u = unit_vector(v1) v2_u = unit_vector(v2) v = np.dot(v1_u, v2_u) if determinant(v1, v2) > 0: return np.real(cmath.acos(v)) else: return np.real(-cmath.acos(v))
def angle_between_anticlockwise(self, v1, v2): """ Returns the angle in radians between vectors 'v1' and 'v2':: """ def determinant(v, w): return v[0] * w[1] - v[1] * w[0] v1_u = v1 / np.linalg.norm(v1) v2_u = v2 / np.linalg.norm(v2) v = np.dot(v1_u, v2_u) if determinant(v1,v2) < 0: return cmath.acos(v) else: return 2*np.pi-cmath.acos(v)
def getRefractedAngle(self, new_medium): snell = self.snell_constant if type(new_medium) is SolidMedium: c = new_medium.c_shear c2 = new_medium.c angle_shear = m.acos(c * snell) angle_p = m.acos(c2*snell) return angle_shear, angle_p else: c = new_medium.c angle = m.acos(c * snell) return angle
def finite_temp_current(data_list): row = len(data_list) col = len(data_list[0]) print("The row and col length of finite temp table are: ", row, col) list_of_r_thetaphi_and_z = [] for i in range(row): #if i <= 5: print("mx, my, mz are: ", data_list[i][0], data_list[i][1], data_list[i][2]) θ_i = cmath.acos(data_list[i][5] / Ms) φ_i = cmath.acos(data_list[i][3] / (Ms * math.sqrt(1 - data_list[i][5]**2))) zup = cmath.cos(θ_i / 2) zdown = cmath.exp(1j * φ_i) * cmath.sin(θ_i / 2) z = [zup, zdown] zd = np.conjugate(z).tolist() r = [data_list[i][0], data_list[i][1], data_list[i][2]] theta_phi = [θ_i, φ_i] list_of_r_thetaphi_and_z.append([r, theta_phi, z, zd]) num = len(list_of_r_thetaphi_and_z) print("Just for the cross check finite temp data input- should be TRUE: ", row == num) my_list = list_of_r_thetaphi_and_z position_current_list = [] for i in range(num - 1): zi = my_list[i][2] zid = my_list[i][3] delx = (my_list[i + 1][0][0] - my_list[i][0][0]) dely = (my_list[i + 1][0][1] - my_list[i][0][1]) delz = list(np.array(my_list[i + 1][2]) - np.array(my_list[i][2])) delzd = list(np.array(my_list[i + 1][2]) - np.array(my_list[i][2])) if delx != 0: delzdelx = list(map(lambda x: x / delx, delz)) delzddelx = list(map(lambda x: x / delx, delzd)) else: delzdelx = 0 delzddelx = 0 if dely != 0: delzdely = list(map(lambda x: x / dely, delz)) delzddely = list(map(lambda x: x / dely, delzd)) else: delzdely = 0 delzddely = 0 # from the finite temperature data: Calculating only finite temp part of current jx1 = -1j * dot(zid, delzdelx) - DbyJ * dot( zid, matrix_by_list(sigma_x, zi)) jy1 = -1j * dot(zid, delzdely) - DbyJ * dot( zid, matrix_by_list(sigma_y, zi)) position_current_list.append([my_list[i][0], [jx1, jy1]]) return position_current_list
def calcDistance(post_lat, post_long, lat, long): distance = (6371 * cmath.acos( cmath.cos(math.radians(lat)) * cmath.cos(math.radians(post_lat)) * cmath.cos(math.radians(post_long) - math.radians(long)) + cmath.sin(math.radians(long)) * cmath.sin(math.radians(post_lat)))) return distance
def test09_trig(): for i in range(-5, 5): for j in range(-5, 5): a = ek.sin(C(i, j)) b = C(cmath.sin(complex(i, j))) assert ek.allclose(a, b) a = ek.cos(C(i, j)) b = C(cmath.cos(complex(i, j))) assert ek.allclose(a, b) sa, ca = ek.sincos(C(i, j)) sb = C(cmath.sin(complex(i, j))) cb = C(cmath.cos(complex(i, j))) assert ek.allclose(sa, sb) assert ek.allclose(ca, cb) # Python appears to handle the branch cuts # differently from Enoki, C, and Mathematica.. a = ek.asin(C(i, j + 0.1)) b = C(cmath.asin(complex(i, j + 0.1))) assert ek.allclose(a, b) a = ek.acos(C(i, j + 0.1)) b = C(cmath.acos(complex(i, j + 0.1))) assert ek.allclose(a, b) if abs(j) != 1 or i != 0: a = ek.atan(C(i, j)) b = C(cmath.atan(complex(i, j))) assert ek.allclose(a, b, atol=1e-7)
def gLin_Chain(n, E): """ A GF for the Linear Chain. You have not taken the sign of n into account. You have also just borrowed the whole expression from Mauro's notes. You terrible terrible cat hiss""" return 1j * exp( 1j * abs(n) * acos(E / (2.0 * t))) / (t * sqrt(4.0 - E**2 / t**2))
def green_function(self, z, zsh, k_x): """ :param z: array or scalar :param zsh: array of scalar :param k_x: scalar :return: """ zv, zshv = np.meshgrid(z, zsh, indexing='ij') gamma = cm.sqrt(self.k0**2 * (1 + 1j * self.params.alpha) - k_x**2) h = self.params.z_computational_grid_m[-1] theta = cm.acos(k_x / self.k0) * 180 / cm.pi r_0 = self.params.lower_refl_coef(theta) r_h = self.params.upper_refl_coef(theta) mult = 1 / (2j * gamma * (1 - r_0 * r_h * np.exp(2j * gamma * h))) res = np.exp(1j * gamma * abs(zv - zshv)) if abs(r_0) > 1e-12: res += r_0 * np.exp(1j * gamma * (zv + zshv)) if abs(r_h) > 1e-12: res += r_h * np.exp(2j * gamma * h) * np.exp(-1j * gamma * (zv + zshv)) if abs(r_0) > 1e-12 and abs(r_h) > 1e-12: res += r_0 * r_h * np.exp(2j * gamma * h) * np.exp( -1j * gamma * abs(zv - zshv)) res *= mult return np.squeeze(res)
def gBulkArmSPA(DA,E): sig = copysign(1.0,-E.real) temp_a1 = 1j*sig*E*exp( sig*2*1j*abs(DA)*acos( (E**2 - 5*t**2)/(4*t**2) ) ) temp_a2 = ( (E**2-9*t**2)*(-E**2+t**2) )**(1.0/4.0) temp_a3 = sqrt( -sig*( 1j/(abs(DA)*pi*(3*t**2 + E**2) ) ) ) ga = (temp_a1/temp_a2)*temp_a3 temp_b1 = 1j*sig*( E*exp(sig*2*1j*abs(DA)*acos( -sqrt( 1 - E**2/t**2 ) ) ) ) temp_b2 = sqrt(3*t**2 + E**2)*( E**2*(t**2 - E**2) )**(1.0/4.0) temp_b3 = sqrt( sig*(1j/(pi*abs(DA))) ) gb = (temp_b1/temp_b2)*temp_b3 g = ga + gb return g
def __init__(self, position, damage, is_player): # конструироваться должен в позиции игрока super(Arrow, self).__init__() arrow_damage = damage (px, py) = position # позиция стреляющего #if is_player: (cx, cy) = pygame.mouse.get_pos() # позиция курсора # else: # for i in player_singlegroup: # (cx, cy) = i.rect.midright # вектор полёта в сторону курсора. +1 от деления на 0 n = cmath.sqrt((cx - px)**2 + (cy - py)**2 + 1) self.ax = (cx - px) / n self.ay = (cy - py) / n rot = 1 if cy > py: # условие переворота стрелы rot = -1 self.image = pygame.image.load('assets/arrow.png') self.rect = self.image.get_rect() # поворот стрелы self.image = pygame.transform.rotate( self.image, rot * 180 / cmath.pi * cmath.acos(self.ax.real).real) self.rect.midleft = position
def get_intersecting_intervals(self, x_m): if self.x0 - self.a < x_m < self.x0 + self.a: t = cm.acos((x_m - self.x0) / self.a) z = abs(self.b * cm.sin(t)) return [(self.z0 - z, self.z0 + z)] else: return []
def getTwoLineAngle(l): """ 得到两根直线之间的夹角 l(4,2) [table 4*2] 矩阵 l1,l3为靠近夹角的点 return: [a, b] a角度, d方向 (1/-1) 1为夹角在右边, -1为夹角在左边 """ assert (len(l) == 4) [x2, y2] = comput_intersec(l[0:2], l[2:4]) #判断夹角的方向 b = 1 if x2 <= l[2, 0]: x1 = l[1, 0] y1 = l[1, 1] x3 = l[3, 0] y3 = l[3, 1] b = enum.ANGLE_LEFT else: x1 = l[0, 0] y1 = l[0, 1] x3 = l[2, 0] y3 = l[2, 1] b = enum.ANGLE_RIGHT a = cmath.acos(np.dot([x1-x2,y1-y2],[x3-x2,y3-y2])/\ (np.linalg.norm([x1-x2,y1-y2]) * np.linalg.norm([x1-x2,y3-y2]))) a = abs(a * 180 / cmath.pi) return [a, b]
def test_complex_inverse_functions(): mp.dps = 15 iv.dps = 15 for (z1, z2) in random_complexes(30): # apparently cmath uses a different branch, so we # can't use it for comparison assert sinh(asinh(z1)).ae(z1) # assert acosh(z1).ae(cmath.acosh(z1)) assert atanh(z1).ae(cmath.atanh(z1)) assert atan(z1).ae(cmath.atan(z1)) # the reason we set a big eps here is that the cmath # functions are inaccurate assert asin(z1).ae(cmath.asin(z1), rel_eps=1e-12) assert acos(z1).ae(cmath.acos(z1), rel_eps=1e-12) one = mpf(1) for i in range(-9, 10, 3): for k in range(-9, 10, 3): a = 0.9 * j * 10**k + 0.8 * one * 10**i b = cos(acos(a)) assert b.ae(a) b = sin(asin(a)) assert b.ae(a) one = mpf(1) err = 2 * 10**-15 for i in range(-9, 9, 3): for k in range(-9, 9, 3): a = -0.9 * 10**k + j * 0.8 * one * 10**i b = cosh(acosh(a)) assert b.ae(a, err) b = sinh(asinh(a)) assert b.ae(a, err)
def new_meteor(self, x, y, width): rd = random.randint(0, width) vie = 20.0 cote_plat = abs(rd - x) cote_droit = y oblique = cmath.sqrt(cote_plat * cote_plat + cote_droit * cote_droit) vitesse = oblique / vie alpha = (cmath.acos(cote_plat / oblique) * 180 / cmath.pi) if rd < x : angle = alpha else : angle = 180 - alpha nb = random.randint(2, 15) list = ["img/ball_flammes.png" ,"img/fireball_r.png" ,"img/fireball_b.png"] color = random.randint(0, 2) for i in range(0,nb) : pos = random.randint(-10, 10) self.ajouter_particule(vie, vitesse.real, angle.real, rd + pos -25, -25, 20+color, list[color] , 50, 50, 50, 0)
def test_complex_inverse_functions(): mp.dps = 15 iv.dps = 15 for (z1, z2) in random_complexes(30): # apparently cmath uses a different branch, so we # can't use it for comparison assert sinh(asinh(z1)).ae(z1) # assert acosh(z1).ae(cmath.acosh(z1)) assert atanh(z1).ae(cmath.atanh(z1)) assert atan(z1).ae(cmath.atan(z1)) # the reason we set a big eps here is that the cmath # functions are inaccurate assert asin(z1).ae(cmath.asin(z1), rel_eps=1e-12) assert acos(z1).ae(cmath.acos(z1), rel_eps=1e-12) one = mpf(1) for i in range(-9, 10, 3): for k in range(-9, 10, 3): a = 0.9*j*10**k + 0.8*one*10**i b = cos(acos(a)) assert b.ae(a) b = sin(asin(a)) assert b.ae(a) one = mpf(1) err = 2*10**-15 for i in range(-9, 9, 3): for k in range(-9, 9, 3): a = -0.9*10**k + j*0.8*one*10**i b = cosh(acosh(a)) assert b.ae(a, err) b = sinh(asinh(a)) assert b.ae(a, err)
def angle(v): x = v[0] * 100 y = v[1] * 100 z = v[2] * 100 phi = math.atan(z / math.sqrt(x ** 2 + y ** 2)) theta = math.acos(x / math.sqrt(y ** 2 + x ** 2)) return (phi.real, theta.real)
def gBulkZigSPA(DZ,E): sigplus = copysign(1.0,-E.real) sigminus = copysign(1.0,E.real) temp_a1 = 1j*sigplus*E*exp( sigplus*2*1j*abs(DZ)*acos( 0.5*(-1.0+sqrt(E**2/t**2)) ) )/(2.0*pi*t**2) temp_a2 = ( 4.0-(1.0-sqrt(E**2/t**2))**2 )**(1.0/4.0) temp_a3 = sqrt( -sigplus*1j*pi/(abs(DZ)*(E**2/t**2-sqrt(E**2/t**2)) ) ) ga = (temp_a1/temp_a2)*temp_a3 temp_b1 = - 1j*sigminus*E*exp( sigminus*2*1j*abs(DZ)*acos( 0.5*(-1.0-sqrt(E**2/t**2)) ) )/(2.0*pi*t**2) temp_b2 = ( 4.0-(1.0+sqrt(E**2/t**2))**2 )**(1.0/4.0) temp_b3 = sqrt( -sigminus*1j*pi/(abs(DZ)*(E**2/t**2+sqrt(E**2/t**2)) ) ) gb = (temp_b1/temp_b2)*temp_b3 g = ga + gb return g
def atenuacao(S, N): """ determina atenuação espacial para dada uma densidade de grade e um S entradas: S - Fator de Courrant N - Densidade da grade """ return -(cmath.acos(1 + 1 / (S**2) * (math.cos(2 * PI * S / N) - 1))).imag
def calcDistance2(post_lat, post_long, lat, long): data = (6371 * cmath.acos( cmath.cos(math.radians(lat)) * cmath.cos(math.radians(post_lat)) * cmath.cos(math.radians(post_long) - math.radians(long)) + cmath.sin(math.radians(lat)) * cmath.sin(math.radians(post_lat)))) result = float(data.real) return result
def get_polar_coordinates(qubit: np.array) -> Tuple[float, float]: a, b = get_coefficients(qubit) theta = 2 * cmath.acos(a) if abs(cmath.sin(theta / 2)) > 1e-33: phi = 1 / complex(0, 1) * cmath.log(b / cmath.sin(theta / 2)) return theta.real, phi.real return theta.real, complex(np.pi, 0).real
def op_acos(x): """Returns the inverse cosine of this mathematical object.""" if isinstance(x, list): return [op_acos(a) for a in x] elif isinstance(x, complex): return cmath.acos(x) else: return math.acos(x)
def zero_temp_current(data_list): row = len(data_list) col = len(data_list[0]) print("The row and col length of zero temp table are: ", row, col) list_of_r_thetaphi_and_z = [] for i in range(row): θ_i = cmath.acos(data_list[i][5] / Ms) φ_i = cmath.acos(data_list[i][3] / (Ms * math.sqrt(1 - data_list[i][5]**2))) zup = cmath.cos(θ_i / 2) zdown = cmath.exp(1j * φ_i) * cmath.sin(θ_i / 2) z = [zup, zdown] zd = np.conjugate(z).tolist() r = [data_list[i][0], data_list[i][1]] # taking only x and y coordinates for position theta_phi = [θ_i, φ_i] list_of_r_thetaphi_and_z.append([r, theta_phi, z, zd]) num = len(list_of_r_thetaphi_and_z) print("Just for the cross check (zero temp) - should be TRUE: ", row == num) my_list = list_of_r_thetaphi_and_z position_current_list = [] for i in range(num - 1): zi = my_list[i][2] zid = my_list[i][3] delx = my_list[i + 1][0][0] - my_list[i][0][0] dely = my_list[i + 1][0][1] - my_list[i][0][1] delz = list(np.array(my_list[i + 1][2]) - np.array(my_list[i][2])) delzd = list(np.array(my_list[i + 1][2]) - np.array(my_list[i][2])) if delx != 0: delzdelx = list(map(lambda x: x / delx, delz)) delzddelx = list(map(lambda x: x / delx, delzd)) else: delzdelx = 0 delzddelx = 0 if dely != 0: delzdely = list(map(lambda x: x / dely, delz)) delzddely = list(map(lambda x: x / dely, delzd)) else: delzdely = 0 delzddely = 0 ax = -1j * (dot(zid, delzdelx) - dot(delzddelx, zi)) ay = -1j * (dot(zid, delzdely) - dot(delzddely, zi)) position_current_list.append([my_list[i][0], [ax, ay]]) return position_current_list
def gSISPA(DZ,DA,E): sig = copysign(1.0,-E.real) temp1 = 2.0*1j*sig*( E*exp(sig*2*1j*DA*acos(- sqrt( 1.0 - E**2/t**2 ) ) ) )*sin( DZ*asin(sqrt(E**2+3*t**2)/(2*t)) )**2 temp2 = sqrt(3*t**2 + E**2)*( E**2*(t**2 - E**2) )**(1.0/4.0) temp3 = sqrt( sig*(1j/(pi*DA)) ) g = (temp1/temp2)*temp3 return g
def asec(x): import math,cmath try: if deg_rad_var.get()==1: return math.acos(1/x) else: return math.degrees(math.acos(1/x)) except: return cmath.acos(1/x)
def p1(x): a = 1.0/2*pi*sqrt(x[1])/x[0] b = x[2]*cos(x[4]) + x[3]*sin(x[4]) c = x[3]*sin(x[4]) - x[2]*cos(x[4]) d = x[0]*abs(sin(x[4])) e = pi*acos(exp(-d)) f = a/b/e g = f/(x[1] + f) h = c/g - b/f return h
def acos(*args, **kw): arg0 = args[0] if isinstance(arg0, (int, float, long)): return _math.acos(*args,**kw) elif isinstance(arg0, complex): return _cmath.acos(*args,**kw) elif isinstance(arg0, _sympy.Basic): return _sympy.acos(*args,**kw) else: return _numpy.arccos(*args,**kw)
def acos(y): """Compute ArcCos >>> from pol import * >>> x,y=mkpol('x,y') >>> acos(cos(.4+x+y)) 0.4 + x + y """ x0=pol(math.acos(y.zero())) for i in range(y.order): x0=x0 + -(y-cos(x0))/sin(x0) return x0
def thetaphi(complexAlpha): phi = cmath.acos(complexAlpha[2]) theta = complex(pi_2) sinphi = np.sin(phi) if sinphi != 0: sintheta = complexAlpha[1] / sinphi theta = cmath.asin(sintheta) if complexAlpha[0] / sinphi < 0: # cos(t)<0 theta = np.pi - theta return theta, phi
def oneArgFuncEval(function, value): # Evaluates functions that take a complex number input if function == "sin": return cmath.sin(value) elif function == "cos": return cmath.cos(value) elif function == "tan": return cmath.tan(value) elif function == "asin": return cmath.asin(value) elif function == "acos": return cmath.acos(value) elif function == "atan": return cmath.atan(value) elif function == "csc": return 1.0 / cmath.sin(value) elif function == "sec": return 1.0 / cmath.cos(value) elif function == "cot": return 1.0 / cmath.tan(value) elif function == "ln": return cmath.log(value) elif function == "sqr": return cmath.sqrt(value) elif function == "abs": return cmath.sqrt((value.real ** 2) + (value.imag ** 2)) elif function == "exp": return cmath.exp(value) if function == "sinh": return cmath.sinh(value) elif function == "cosh": return cmath.cosh(value) elif function == "tanh": return cmath.tanh(value) elif function == "asinh": return cmath.asinh(value) elif function == "acosh": return cmath.acosh(value) elif function == "atanh": return cmath.atanh(value) elif function == "ceil": return math.ceil(value.real) + complex(0, 1) * math.ceil(value.imag) elif function == "floor": return math.floor(value.real) + complex(0, 1) * math.floor(value.imag) elif function == "trunc": return math.trunc(value.real) + complex(0, 1) * math.trunc(value.imag) elif function == "fac": if value.imag == 0 and value < 0 and value.real == int(value.real): return "Error: The factorial function is not defined on the negative integers." return gamma(value + 1) elif function == "log": return cmath.log10(value)
def getDist(lat1,long1,lat2,long2): """ Calc the distance between 2 points using Vincenty """ lat1 = math.radians(lat1) long1 = math.radians(long1) lat2 = math.radians(lat2) long2 = math.radians(long2) R = 6371 # km d = cmath.acos(cmath.sin(lat1) * cmath.sin(lat2) + \ cmath.cos(lat1) * cmath.cos(lat2) * cmath.cos(long2 - long1)) * R return abs(d) # cast to float
def acos(self,command_position,args): try: if ((type(self.stack[command_position+1])==complex) or (self.stack[command_position+1]>1)): value=cmath.acos(self.stack[command_position+1]) else: value=math.acos(self.stack[command_position+1]) self.stack.pop(command_position+1) self.stack[command_position]=value return command_position except: self.statstrings.append("Bad arc cosine attempted, ignoring command") self.error=True self.stack.pop(command_position) return command_position
def acos(x): """ Return the arc cosine of x, in radians. """ if isinstance(x,ADF): ad_funcs = list(map(to_auto_diff,[x])) x = ad_funcs[0].x ######################################## # Nominal value of the constructed ADF: f = acos(x) ######################################## variables = ad_funcs[0]._get_variables(ad_funcs) if not variables or isinstance(f, bool): return f ######################################## # Calculation of the derivatives with respect to the arguments # of f (ad_funcs): lc_wrt_args = [-1/sqrt(1-x**2)] qc_wrt_args = [x/(sqrt(1 - x**2)*(x**2 - 1))] cp_wrt_args = 0.0 ######################################## # Calculation of the derivative of f with respect to all the # variables (Variable) involved. lc_wrt_vars,qc_wrt_vars,cp_wrt_vars = _apply_chain_rule( ad_funcs,variables,lc_wrt_args,qc_wrt_args, cp_wrt_args) # The function now returns an ADF object: return ADF(f, lc_wrt_vars, qc_wrt_vars, cp_wrt_vars) else: # try: # pythonic: fails gracefully when x is not an array-like object # return [acos(xi) for xi in x] # except TypeError: if x.imag: return cmath.acos(x) else: return math.acos(x.real)
def calc_Vr(self, A, B): Pr = [] Qr = [] result = [] result2 = [] ang_pot = [] absA = abs(A) alfa = cm.phase(A) absB = abs(B) beta = cm.phase(B) i = 0 while i > -1: Praux = i * 10e5 / 3 * self.fp Qraux = cm.tan(cm.acos(self.fp)).real * Praux a = pow((absA /absB), 2) b = (2 * absA / absB * (Praux * cm.cos(beta - alfa) + Qraux * cm.sin(beta - alfa) - pow(self.U1, 2) / (2 * absA * absB))).real c = pow(Praux, 2) + pow(Qraux, 2) raiz = pow(b, 2) - 4 * a * c if raiz < 0: result2[len(result2) - 1] = result[len(result) - 1] break Pr.append(Praux) Qr.append(Qraux) coeff = [a, 0, b, 0, c] aux = np.roots(coeff) result.append(max(abs(aux * cm.sqrt(3).real))) result2.append(min(abs(aux * cm.sqrt(3).real))) ang_pot.append(self.calc_ang_pot(A,B,Praux,result[len(result) - 1]/np.sqrt(3).real)) # print(result[len(result)-1]) i = i + self.prec return [3 * np.array(Pr)/self.Sb, np.array(result)/self.Vb, np.array(result2)/self.Vb, 3*np.array(Qr)/self.Sb, ang_pot]
def gterm(ky): q = acos( (E**2 - t2**2 - 4.0*t1**2 *cos(ky)**2)/(4.0*t1*t2 *cos(ky) ) ) if q.imag < 0.0: q = -q Const = 1j/(2.0*nE*t1*t2) Den = cos(ky)*sin(q) if s == 0: sig = copysign(1,m2+n2-m1-n1) return Const*E*exp( 1j*sig*q*(m2+n2-m1-n1) )*sin(ky*(m2-n2))*sin(ky*(m1-n1))/Den elif s == 1: sig = copysign(1,m2+n2-m1-n1) f = t2 + 2.0*t1*cos(ky)*exp(sig*1j*q) return Const*f*exp( 1j*sig*q*(m2+n2-m1-n1) )*sin(ky*(m2-n2))*sin(ky*(m1-n1))/Den elif s == -1: sig = copysign(1,m2+n2-m1-n1-1) ft = t2 + 2.0*t1*cos(ky)*exp(-sig*1j*q) return Const*ft*exp( 1j*sig*q*(m2+n2-m1-n1) )*sin(ky*(m2-n2))*sin(ky*(m1-n1))/Den else: print 'Sublattice error in gRib_Arm'
def gLine_kZ(DA,kZ,s,E): # This really needs to be tested against something """The Graphene Green's function, in the k_y, x basis""" q = acos((E**2 - t**2 - 4.0*t**2 *cos(kZ)**2)/(4.0*t**2 *cos(kZ))) if q.imag < 0.0: q = -q Const = 1j/(4*t**2) Den = cos(kZ)*sin(q) if s == 0: sig = copysign(1,DA) # You haven't made sure DA goes here return Const*E*exp(1j*sig*q*DA)/Den elif s == 1: sig = copysign(1,DA) f = t*(1.0 + 2.0*cos(kZ)*exp(1j*sig*q)) return Const*f*exp( 1j*sig*q*DA )/Den elif s == -1: sig = copysign(1,DA-1) ft = t*(1.0 + 2.0*cos(kZ)*exp(-sig*1j*q)) return Const*ft*exp(1j*sig*q*DA)/Den else: print "Sublattice error in gLine_kZ"
def gI(kZ): q = acos( (E**2 - t2**2 - 4.0*t1**2 *cos(kZ)**2)/(4.0*t1*t2 *cos(kZ) ) ) if q.imag < 0.0: q = -q Const = 1j/(4*pi*t1*t2) Den = cos(kZ)*sin(q) if s == 0: sig = copysign(1,m+n) return Const*E*exp( 1j*(sig*q*(m+n) + kZ*(m-n) ) )/ Den elif s == 1: sig = copysign(1,m+n) f = t2 + 2*t1*exp(sig*1j*q)*cos(kZ) return Const*f*exp( 1j*(sig*q*(m+n) + kZ*(m-n) ) )/Den elif s == -1: sig = copysign(1,m+n-1) ft = t2 + 2*t1*exp(-sig*1j*q)*cos(kZ) return Const*ft*exp( 1j*(sig*q*(m+n) + kZ*(m-n) ) )/Den else: print "Sublattice error in gSBulk"
def gLine_ky(DA,ky,s,E,a=1.0): """The Graphene Green's function, projected onto the k_y, x basis, used in the calculation of infinite lines of impurities""" q = acos( (E**2 - t**2 - 4.0*t**2 *cos(ky*a/2)**2)/(4.0*t**2 *cos(ky*a/2) ) ) if q.imag < 0.0: q = -q Const = 1j/(4*t**2) Den = cos(ky*a/2)*sin(q) if s == 0: sig = copysign(1,DA) # You haven't made sure DA goes here return Const*E*exp( 1j*sig*q*DA )/ Den elif s == 1: sig = copysign(1,DA) f = t*( 1.0 + 2.0*cos(ky*a/2)*exp( 1j*sig*q ) ) return Const*f*exp( 1j*sig*q*DA )/Den elif s == -1: sig = copysign(1,DA-1) ft = t*( 1.0 + 2.0*cos(ky*a/2)*exp(-sig*1j*q) ) return Const*ft*exp( 1j*sig*q*DA )/Den else: print "Sublattice error in gLine_ky"
def geodesic_distance(lat1, long1, lat2, long2): # This converts the points to radians and calculates the distance assuming the earth is a perfect unit sphere # Should probably use something like www.acscdg.com in the future #deg_to_rad = decimal.Decimal(math.pi/180.0) lat1 = round(lat1, 4) lat2 = round(lat1, 4) long1 = round(long1, 4) long2 = round(long2, 4) deg_to_rad = math.pi/180.0 #phi1 = (decimal.Decimal(90.0) - lat1)*deg_to_rad #phi2 = (decimal.Decimal(90.0) - lat2)*deg_to_rad phi1 = (90.0 - lat1)*deg_to_rad phi2 = (90.0 - lat2)*deg_to_rad theta1 = long1*deg_to_rad theta2 = long2*deg_to_rad cos = math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) + math.cos(phi1)*math.cos(phi2) arc = np.real(math.acos(cos)) # earth's radius is approximately 3960 miles return round(arc*3960,4)
# 返回平方根 print(math.sqrt(y)) print(u"常用随机函数") a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] # 从列表a中随机选中一个 print(random.choice(a)) # 从指定的范围(2-100按5递增的数据集)中随机选中一个 print(random.randrange(2, 100, 5)) # 生成一个随机数,它在(0,1)之间 print(random.random()) print(u"常用三角函数") x = 100 # 返回x的反余弦弧度值 print(cmath.acos(x)) # 返回x的正弦弧度值 print(cmath.sin(x)) # 返回x的余弦弧度值 print(cmath.cos(x)) print(u"数学常量") print(cmath.pi) # 返回π
print('Positive infinity =', cmath.inf) print('Positive Complex infinity =', cmath.infj) print('NaN =', cmath.nan) print('NaN Complex =', cmath.nanj) # power and log functions c = 2 + 2j print('e^c =', cmath.exp(c)) print('log2(c) =', cmath.log(c, 2)) print('log10(c) =', cmath.log10(c)) print('sqrt(c) =', cmath.sqrt(c)) # trigonometric functions c = 2 + 2j print('arc sine =', cmath.asin(c)) print('arc cosine =', cmath.acos(c)) print('arc tangent =', cmath.atan(c)) print('sine =', cmath.sin(c)) print('cosine =', cmath.cos(c)) print('tangent =', cmath.tan(c)) # hyperbolic functions c = 2 + 2j print('inverse hyperbolic sine =', cmath.asinh(c)) print('inverse hyperbolic cosine =', cmath.acosh(c)) print('inverse hyperbolic tangent =', cmath.atanh(c)) print('hyperbolic sine =', cmath.sinh(c)) print('hyperbolic cosine =', cmath.cosh(c)) print('hyperbolic tangent =', cmath.tanh(c))
1.整形的位运算:或、异或、与、左移、右移、非 2. """ import math x = 1.1111 y = 2 a =-10 print(math.trunc(x)) # 去掉小数部分,保留整数 print(round(x,2)) # 保留小数点的位数 print(math.floor(x)) # 向下取整 print(math.ceil(x)) # 向上取整 print(math.exp(y)) # 返回e的y次幂 print(math.fabs(a)) # 返回a的绝对值,同内置函数abs() print(math.log(y)) print(max(1,4,5,8,10,3)) # 返回给定参数的最大值 print(math.modf(x)) # 返回x的小数部分和整数部分,整数部分以浮点型表示 # 随机函数 import random print(random.choice((1,4,5,62,2,3))) # 返回序列中随机的一个值 # 三角函数 import cmath b = 100 print(cmath.acos(b)) # 返回b的反余弦弧度值 print(math.hypot(2,3)) # 返回欧几里德范数sqrt(x*x + y*y)。
def __new__(cls, argument): if isinstance(argument, (RealValue, Zero)): return FloatValue(math.acos(float(argument))) if isinstance(argument, (ComplexValue)): return ComplexValue(cmath.acos(complex(argument))) return MathFunction.__new__(cls)
'acoth': lambda x: atanh(1/x), 'floor': lambda x: floor(x), 'ceil': lambda x: ceil(x), 'point': lambda x: x/(10 ** floor(1+log(x)/log(10))) } complex_functions = {'sin': lambda x: cmath.sin(x), 'cos': lambda x: cmath.cos(x), 'tan': lambda x: cmath.tan(x), 'csc': lambda x: 1/cmath.sin(x), 'sec': lambda x: 1/cmath.cos(x), 'cot': lambda x: 1/cmath.tan(x), 'ln': lambda x: cmath.log(x), 'log': lambda x: cmath.log(x)/cmath.log(10), 'fact': lambda x: factorial(int(x)), 'asin': lambda x: cmath.asin(x), 'acos': lambda x: cmath.acos(x), 'atan': lambda x: cmath.atan(x), 'acsc': lambda x: cmath.asin(1/x), 'asec': lambda x: cmath.acos(1/x), 'acot': lambda x: cmath.atan(1/x), 'sinh': lambda x: cmath.sinh(x), 'cosh': lambda x: cmath.cosh(x), 'tanh': lambda x: cmath.tanh(x), 'csch': lambda x: 1/cmath.sinh(x), 'sech': lambda x: 1/cmath.cosh(x), 'coth': lambda x: 1/cmath.tanh(x), 'asinh': lambda x: cmath.asinh(x), 'acosh': lambda x: cmath.acosh(x), 'atanh': lambda x: cmath.atanh(x), 'acsch': lambda x: cmath.asinh(1/x), 'asech': lambda x: cmath.acosh(1/x),
def test_acos(self): self.assertAlmostEqual(complex(0.936812, -2.30551), cmath.acos(complex(3, 4)))
def acos_usecase(x): return cmath.acos(x)
def draw_path(self, canvas, arc_center): # Driving forward if self.go_forward: path_end_pos = self.y+self.size/self.SIDE_BOX_RATIO+self.half_size+self.half_size*self.throttle/-100 canvas.create_line(self.robot_center[0], self.y+self.size/self.SIDE_BOX_RATIO, self.x+self.path_area_size/2, self.y+self.size) canvas.create_line(self.robot_center[0], self.y+self.size/self.SIDE_BOX_RATIO+self.half_size, self.x+self.path_area_size/2, path_end_pos, fill="purple", width=2) canvas.create_oval(self.robot_center[0]-self.path_dot_radius, path_end_pos-self.path_dot_radius, self.robot_center[0]+self.path_dot_radius, path_end_pos+self.path_dot_radius, fill="purple") # canvas.create_oval(arcCenter[0]-self.arcCenterRadius, arcCenter[1]-self.arcCenterRadius, # arcCenter[0]+self.arcCenterRadius, arcCenter[1]+self.arcCenterRadius, # fill="grey") # Rotation elif self.robot_center[0] == arc_center[0] and self.robot_center[1] == arc_center[1]: radius = self.half_size/4 arc_degree_length = float(359)*self.throttle/100 canvas.create_oval(arc_center[0]-radius, arc_center[1]-radius, arc_center[0]+radius, arc_center[1]+radius, fill=None) canvas.create_arc(arc_center[0]-radius, arc_center[1]-radius, arc_center[0]+radius, arc_center[1]+radius, fill=None, style="arc", outline="purple", width=2, start=90, extent=arc_degree_length) path_end_pos_x = arc_center[0]+cmath.cos((arc_degree_length+90)/180*cmath.pi.real).real*radius path_end_pos_y = arc_center[1]+cmath.sin((arc_degree_length+90)/180*cmath.pi.real).real*radius*-1 canvas.create_oval(path_end_pos_x-self.path_dot_radius, path_end_pos_y-self.path_dot_radius, path_end_pos_x+self.path_dot_radius, path_end_pos_y+self.path_dot_radius, fill="purple") # Arcing else: # Compute the radius of the arc radius = dist(arc_center[0], arc_center[1], self.robot_center[0], self.robot_center[1]) # Draw the circle that the arc falls on canvas.create_oval(arc_center[0]-radius, arc_center[1]-radius, arc_center[0]+radius, arc_center[1]+radius, fill=None) theta = 0.0 # Adjacent is the length of line adjacent to theta # Hypotenuse is our radius # Theta is the interior angle around the point of rotation # Top Right Quadrant if arc_center[0] > self.robot_center[0] and arc_center[1] <= self.robot_center[1]: adjacent = arc_center[0] - self.robot_center[0] theta = 180 + (cmath.acos(float(adjacent)/float(radius)).real/cmath.pi.real*180).real # Top Left Quadrant if arc_center[0] < self.robot_center[0] and arc_center[1] < self.robot_center[1]: adjacent = self.robot_center[0] - arc_center[0] theta = 360 - (cmath.acos(float(adjacent)/float(radius)).real/cmath.pi.real*180).real # Bottom Left Quadrant if arc_center[0] < self.robot_center[0] and arc_center[1] > self.robot_center[1]: adjacent = self.robot_center[1] - arc_center[1] theta = (cmath.acos(float(adjacent)/float(radius)).real/cmath.pi.real*180).real-90 # Bottom Right Quadrant if arc_center[0] > self.robot_center[0] and arc_center[1] > self.robot_center[1]: adjacent = arc_center[1] - self.robot_center[1] theta = (cmath.acos(float(adjacent)/float(radius)).real/cmath.pi.real*180).real+90 # We want forward throttle to always move the robot forward. This enforces that behavior if arc_center[0] > self.robot_center[0]: throttlePathMod = -1 arc_degree_length = 360 - (float(359)*self.throttle/100 - theta) else: throttlePathMod = 1 arc_degree_length = 360 - (float(359)*self.throttle/100*-1 - theta) # The purple arc to represent actual drive distance around the circle canvas.create_arc(arc_center[0]-radius, arc_center[1]-radius, arc_center[0]+radius, arc_center[1]+radius, start=theta, extent=359*self.throttle/100*throttlePathMod, fill=None, style="arc", outline="purple", width=2) # The position that the robot will stop at, the end of the arc path_end_pos_x = arc_center[0]+cmath.cos(deg2rad(arc_degree_length)).real*radius path_end_pos_y = arc_center[1]+cmath.sin(deg2rad(arc_degree_length)).real*radius*-1 # Draw a marker to show the position that the robot will stop at canvas.create_oval(path_end_pos_x-self.path_dot_radius, path_end_pos_y-self.path_dot_radius, path_end_pos_x+self.path_dot_radius, path_end_pos_y+self.path_dot_radius, fill="purple") return
def rpn_calc(input, angle_mode = 0): global stack single_arg_funcs = ['exp','sqrt','sin','asin','cos','acos','tan','atan', 'sinh','asinh','cosh','acosh','tanh', 'atanh','!', 'polar'] num, arg, option = parse(input) #print(num, arg, option) #print('\n') if arg == None and (num != None or num != 'Error'): config.stack.append(num) history.append('Entered number: ' + str(num) ) return config.stack # Simple arithmatic----------------------------------------------------------------------- if option == None and num != None: if arg not in single_arg_funcs: last = config.stack.pop() if arg == '+': try: result = Decimal(last) + Decimal(num) hist = str(last) + '+' + str(num) + '=' + str(result) except TypeError: result = last + num hist = str(last) + '+' + str(num) + '=' + str(result) if arg == '-': try: result = Decimal(last) - Decimal(num) hist = str(last) + '-' + str(num) + '=' + str(result) except TypeError: result = last - num hist = str(last) + '-' + str(num) + '=' + str(result) if arg == '*': try: result = Decimal(last) * Decimal(num) hist = str(last) + '*' + str(num) + '=' + str(result) except TypeError: result = last * num hist = str(last) + '*' + str(num) + '=' + str(result) if arg == '/': try: result = Decimal(last) / Decimal(num) hist = str(last) + '/' + str(num) + '=' + str(result) except TypeError: result = last / num hist = str(last) + '/' + str(num) + '=' + str(result) if arg == '**': try: result = pow(Decimal(last), Decimal(num) ) hist = str(last) + '**' + str(num) + '=' + str(result) except TypeError: result = last ** num hist = str(last) + '**' + str(num) + '=' + str(result) if arg == 'rt': try: result = root(Decimal(last), Decimal(num) ) hist = str(last) + 'raised to 1 over ' + str(num) + '=' + str(result) except TypeError: result = root(last + num) hist = str(last) + 'raised to 1 over ' + str(num) + '=' + str(result) if arg == '%': try: result = Decimal(last) % Decimal(num) hist = str(last) + '%' + str(num) + '=' + str(result) except TypeError: result = last % num hist = str(last) + '%' + str(num) + '=' + str(result) if arg == '!': try: result = Decimal(math.factorial(num)) hist = str(num) + '!' + '=' + str(result) except TypeError: result = math.factorial(num) hist = str(num) + '!' + '=' + str(result) if arg == 'exp': try: result = math.exp(Decimal(num)) hist = str(num) + 'exp' + '=' + str(result) except ValueError: result = cmath.exp(Decimal(num)) hist = str(num) + 'exp' + '=' + str(result) except TypeError: result = cmath.exp(num) hist = str(num) + 'exp' + '=' + str(result) if arg == 'sqrt': try: result = math.sqrt(Decimal(num)) hist = str(num) + 'sqrt' + '=' + str(result) except ValueError: result = cmath.sqrt(Decimal(num)) hist = str(num) + 'sqrt' + '=' + str(result) except TypeError: result = cmath.sqrt(num) hist = str(num) + 'sqrt' + '=' + str(result) if arg == 'log': try: result = math.log10(Decimal(num)) hist = str(num) + 'log10' + '=' + str(result) except ValueError: result = cmath.log10(Decimal(num)) hist = str(num) + 'log10' + '=' + str(result) except TypeError: result = cmath.log10(num) hist = str(num) + 'log10' + '=' + str(result) #================================= if arg == 'ln': try: result = math.log(Decimal(num)) hist = str(num) + 'ln' + '=' + str(result) except ValueError: result = cmath.log(Decimal(num)) hist = str(num) + 'ln' + '=' + str(result) except TypeError: result = cmath.log(num) hist = str(num) + 'ln' + '=' + str(result) #--------TRIG-------------------------------- if arg == 'sin': if angle_mode == 1: try: result = math.sin(Decimal(num)) hist = 'sin' + str(num) + '=' + str(result) except TypeError: result = cmath.sin(num) hist = 'sin' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.sin(math.radians(Decimal(num))) hist = 'sin' + str(num) + '=' + str(result) except TypeError: result = cmath.sin(num) hist = 'sin' + str(num) + '=' + str(result) if arg == 'cos': if angle_mode == 1: try: result = math.cos(Decimal(num)) hist = 'cos' + str(num) + '=' + str(result) except TypeError: result = cmath.cos(num) hist = 'cos' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.cos(math.radians(Decimal(num))) hist = 'cos' + str(num) + '=' + str(result) except TypeError: result = cmath.cos(num) hist = 'cos' + str(num) + '=' + str(result) if arg == 'tan': if angle_mode == 1: try: result = math.tan(Decimal(num)) hist = 'tan' + str(num) + '=' + str(result) except TypeError: result = cmath.tan(num) hist = 'tan' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.tan(math.radians(Decimal(num))) hist = 'tan' + str(num) + '=' + str(result) except TypeError: result = cmath.tan(num) hist = 'tan' + str(num) + '=' + str(result) if arg == 'asin': if angle_mode == 1: try: result = math.asin(Decimal(num)) hist = 'asin' + str(num) + '=' + str(result) except TypeError: result = cmath.asin(num) hist = 'asin' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.asin(math.radians(Decimal(num))) hist = 'asin' + str(num) + '=' + str(result) except TypeError: result = cmath.asin(num) hist = 'asin' + str(num) + '=' + str(result) if arg == 'acos': if angle_mode == 1: try: result = math.acos(Decimal(num)) hist = 'acos' + str(num) + '=' + str(result) except TypeError: result = cmath.acos(num) hist = 'acos' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.acos(math.radians(Decimal(num))) hist = 'acos' + str(num) + '=' + str(result) except TypeError: result = cmath.acos(num) hist = 'acos' + str(num) + '=' + str(result) if arg == 'atan': if angle_mode == 1: try: result = math.atan(Decimal(num)) hist = 'atan' + str(num) + '=' + str(result) except TypeError: result = cmath.atan(num) hist = 'atan' + str(num) + '=' + str(result) elif angle_mode == 0: try: result = math.atan(math.radians(Decimal(num))) hist = 'atan' + str(num) + '=' + str(result) except TypeError: result = cmath.atan(num) hist = 'atan' + str(num) + '=' + str(result) if arg == 'sinh': try: result = math.sinh(Decimal(num)) hist = 'sinh' + str(num) + '=' + str(result) except TypeError: result = cmath.sinh(num) hist = 'sinh' + str(num) + '=' + str(result) if arg == 'cosh': try: result = math.cosh(Decimal(num)) hist = 'cosh' + str(num) + '=' + str(result) except TypeError: result = cmath.cosh(num) hist = 'cosh' + str(num) + '=' + str(result) if arg == 'tanh': try: result = math.tanh(Decimal(num)) hist = 'tanh' + str(num) + '=' + str(result) except TypeError: result = cmath.tanh(num) hist = 'tanh' + str(num) + '=' + str(result) if arg == 'asinh': try: result = math.asinh(Decimal(num)) hist = 'asinh' + str(num) + '=' + str(result) except TypeError: result = cmath.asinh(num) hist = 'asinh' + str(num) + '=' + str(result) if arg == 'acosh': try: result = math.acosh(Decimal(num)) hist = 'acosh' + str(num) + '=' + str(result) except TypeError: result = cmath.acosh(num) hist = 'acosh' + str(num) + '=' + str(result) if arg == 'atanh': try: result = math.atanh(Decimal(num)) hist = 'atanh' + str(num) + '=' + str(result) except TypeError: result = cmath.atanh(num) hist = 'atanh' + str(num) + '=' + str(result) if arg == 'rect': #continue here.... try: result = rect(last, num) hist = 'Convert ' + str(last) + ',' + str(num) + ' to rectangular coords.' except TypeError: result = 'Error' hist = 'Error in attempted conversion to rectangular coords. No result.' if arg == 'polar': try: result = polar(num) hist = 'Convert ' + str(num) + ' to polar coords.' except TypeError: result = 'Error' hist = 'Error in attempted conversion to polar coords. No result.' config.stack.append(result) history.append(hist) return config.stack #======================================================================================================================= #----Only argument passed----------------------------------------------------------------------------------------------- #======================================================================================================================= elif option == None and num == None: last = config.stack.pop() if arg not in single_arg_funcs: try: n_minus1 = config.stack.pop() except IndexError: try: config.stack.append(Decimal(last)) except TypeError: config.stack.append(last) except TypeError: config.stack.append(last) except Exception as e: return 'Error' if arg == '+': try: result = Decimal(n_minus1) + Decimal(last) hist = str(n_minus1) + '+' + str(last) + '=' + str(result) except TypeError: result = n_minus1 + last hist = str(n_minus1) + '+' + str(last) + '=' + str(result) if arg == '-': try: result = Decimal(n_minus1) - Decimal(last) hist = str(n_minus1) + '-' + str(last) + '=' + str(result) except TypeError: result = n_minus1 - last hist = str(n_minus1) + '-' + str(last) + '=' + str(result) if arg == '*': try: result = Decimal(n_minus1) * Decimal(last) hist = str(n_minus1) + '*' + str(last) + '=' + str(result) except TypeError: result = n_minus1 * last hist = str(n_minus1) + '*' + str(last) + '=' + str(result) if arg == '/': try: result = Decimal(n_minus1) / Decimal(last) hist = str(n_minus1) + '/' + str(last) + '=' + str(result) except TypeError: result = n_minus1 / last hist = str(n_minus1) + '/' + str(last) + '=' + str(result) if arg == '!': try: result = Decimal(math.factorial(last)) hist = str(last) + '!' '=' + str(result) except TypeError: result = math.factorial(last) hist = str(last) + '!' '=' + str(result) except OverflowError: config.stack.append(last) hist = str('Factorial overflow error, no result.') return 'Error' if arg == '**': try: result = pow(Decimal(last), Decimal(last)) hist = str(n_minus1) + '**' + str(last) + '=' + str(result) except TypeError: result = last ** last hist = str(n_minus1) + '**' + str(last) + '=' + str(result) if arg == 'log': try: result = math.log10(Decimal(last)) hist = str(last) +'log10' + '=' + str(result) except ValueError: result = cmath.log10(Decimal(last)) hist = str(last) +'log10' + '=' + str(result) except TypeError: result = cmath.log10(last) hist = str(last) +'log10' + '=' + str(result) if arg == 'ln': try: result = math.log(Decimal(last)) hist = str(last) +'ln' + '=' + str(result) except ValueError: result = cmath.log(Decimal(last)) hist = str(last) +'ln' + '=' + str(result) except TypeError: result = cmath.log(last) hist = str(last) +'ln' + '=' + str(result) if arg == 'rt': try: result = root(Decimal(last), Decimal(n_minus1)) hist = str(n_minus1) + 'root' + str(last) + '=' + str(result) except TypeError: result = root(last), (n_minus1) hist = str(n_minus1) + 'root' + str(last) + '=' + str(result) if arg == 'exp': try: result = math.exp(Decimal(last)) hist = str(last) +'exp' + '=' + str(result) except TypeError: result = cmath.exp(last) hist = str(last) +'exp' + '=' + str(result) if arg == 'sqrt': try: result = math.sqrt(Decimal(last)) hist = 'Square root of ' + str(last) + '=' + str(result) except ValueError: result = cmath.sqrt(Decimal(last)) hist = 'Square root of ' + str(last) + '=' + str(result) except TypeError: result = cmath.sqrt(last) hist = 'Square root of ' + str(last) + '=' + str(result) #----------Trig---------------------------------------- #--------TRIG-------------------------------- if arg == 'sin': if angle_mode == 1: try: result = math.sin(Decimal(last)) hist = 'sin' + str(last) + '=' + str(result) except TypeError: result = cmath.sin(last) hist = 'sin' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.sin(math.radians(Decimal(last))) hist = 'sin' + str(last) + '=' + str(result) except TypeError: result = cmath.sin(last) hist = 'sin' + str(last) + '=' + str(result) if arg == 'cos': if angle_mode == 1: try: result = math.cos(Decimal(last)) hist = 'cos' + str(last) + '=' + str(result) except TypeError: result = cmath.cos(last) hist = 'cos' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.cos(math.radians(Decimal(last))) hist = 'cos' + str(last) + '=' + str(result) except TypeError: result = cmath.cos(last) hist = 'cos' + str(last) + '=' + str(result) if arg == 'tan': if angle_mode == 1: try: result = math.tan(Decimal(last)) hist = 'tan' + str(last) + '=' + str(result) except TypeError: result = cmath.tan(last) hist = 'tan' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.tan(math.radians(Decimal(last))) hist = 'tan' + str(last) + '=' + str(result) except TypeError: result = cmath.tan(last) hist = 'tan' + str(last) + '=' + str(result) if arg == 'asin': if angle_mode == 1: try: result = math.asin(Decimal(last)) hist = 'asin' + str(last) + '=' + str(result) except TypeError: result = cmath.asin(last) hist = 'asin' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.asin(math.radians(Decimal(last))) hist = 'asin' + str(last) + '=' + str(result) except TypeError: result = cmath.asin(last) hist = 'asin' + str(last) + '=' + str(result) if arg == 'acos': if angle_mode == 1: try: result = math.acos(Decimal(last)) hist = 'acos' + str(last) + '=' + str(result) except TypeError: result = cmath.acos(last) hist = 'acos' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.acos(math.radians(Decimal(last))) hist = 'acos' + str(last) + '=' + str(result) except TypeError: result = cmath.acos(last) hist = 'acos' + str(last) + '=' + str(result) if arg == 'atan': if angle_mode == 1: try: result = math.atan(Decimal(last)) hist = 'atan' + str(last) + '=' + str(result) except TypeError: result = cmath.atan(last) hist = 'atan' + str(last) + '=' + str(result) elif angle_mode == 0: try: result = math.atan(math.radians(Decimal(last))) hist = 'atan' + str(last) + '=' + str(result) except TypeError: result = cmath.atan(last) hist = 'atan' + str(last) + '=' + str(result) if arg == 'sinh': try: result = math.sinh(Decimal(last)) hist = 'sinh' + str(last) + '=' + str(result) except TypeError: result = math.sinh(last) hist = 'sinh' + str(last) + '=' + str(result) if arg == 'cosh': try: result = math.cosh(Decimal(last)) hist = 'cosh' + str(last) + '=' + str(result) except TypeError: result = math.cosh(last) hist = 'cosh' + str(last) + '=' + str(result) if arg == 'tanh': try: result = math.tanh(Decimal(last)) hist = 'tanh' + str(last) + '=' + str(result) except TypeError: result = math.tanh(last) hist = 'tanh' + str(last) + '=' + str(result) if arg == 'asinh': try: result = math.asinh(Decimal(last)) hist = 'asinh' + str(last) + '=' + str(result) except TypeError: result = math.asinh(last) hist = 'asinh' + str(last) + '=' + str(result) if arg == 'acosh': try: result = math.acosh(Decimal(last)) hist = 'acosh' + str(last) + '=' + str(result) except TypeError: result = math.acosh(last) hist = 'acosh' + str(last) + '=' + str(result) if arg == 'atanh': try: result = math.atanh(Decimal(last)) hist = 'atanh' + str(last) + '=' + str(result) except TypeError: result = math.atanh(last) hist = 'atanh' + str(last) + '=' + str(result) if arg == 'rect': #continue here.... try: result = rect(n_minus1, last) hist = 'Convert ' + str(n_minus1) + ',' + str(last) + ' to rectangular coords.' except TypeError: result = 'Error' hist = 'Error in attempted conversion to rectangular coords. No result.' if arg == 'polar': try: result = polar(last) hist = 'Convert complex value ' + str(last) + ' to rectangular coords.' except TypeError: result = 'Error' hist = 'Error in attempted conversion to polar coords. No result.' config.stack.append(result) history.append(hist) return config.stack