def __call__(self,t): with mp.extradps(self.prec): t = mpf(t) if t == 0: print "ERROR: Inverse transform can not be calculated for t=0" return ("Error"); N = 2*self.N # Initiate the stepsize (mit aktueller Präsision) h = 2*pi/N # The for loop is evaluating the Laplace inversion at each point theta i # which is based on the trapezoidal rule ans = 0.0 for k in range(self.N): theta = -pi + (k+0.5)*h z = self.shift + N/t*(Talbot.c1*theta/tan(Talbot.c2*theta) - Talbot.c3 + Talbot.c4*theta) dz = N/t * (-Talbot.c1*Talbot.c2*theta/sin(Talbot.c2*theta)**2 + Talbot.c1/tan(Talbot.c2*theta)+Talbot.c4) v1 = exp(z*t)*dz prec = floor(max(log10(abs(v1)),0)) with mp.extradps(prec): value = self.F(z) ans += v1*value return ((h/pi)*ans).imag
def __call__(self,t): if t == 0: print("ERROR: Inverse transform can not be calculated for t=0") return ("Error"); # Initiate the stepsize h = 2*pi/self.N ans = 0.0 # parameters from # T. Schmelzer, L.N. Trefethen, SIAM J. Numer. Anal. 45 (2007) 558-571 c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0','0.2645') # The for loop is evaluating the Laplace inversion at each point theta i # which is based on the trapezoidal rule for k in range(self.N): theta = -pi + (k+0.5)*h z = self.shift + self.N/t*(c1*theta/tan(c2*theta) - c3 + c4*theta) dz = self.N/t * (-c1*c2*theta/sin(c2*theta)**2 + c1/tan(c2*theta)+c4) ans += exp(z*t)*self.F(z)*dz return ((h/(2j*pi))*ans).real
def BSLaplace(S, K, T, t, r, sig, N, phi): """Solving the Black Scholes PDE in the Laplace domain""" x = ln(S / K) r = mpf(r) sig = mpf(sig) T = mpf(T) t = mpf(t) S = mpf(S) K = mpf(K) x = mpf(x) mu = r - 0.5 * (sig**2) tau = T - t c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0', '0.2645') ans = 0.0 h = 2 * pi / N h = mpf(h) for k in range(N / 2): # Use symmetry theta = -pi + (k + 0.5) * h z = N / tau * (c1 * theta / tan(c2 * theta) - c3 + c4 * theta) dz = N / tau * (-c1 * c2 * theta / (sin(c2 * theta)**2) + c1 / tan(c2 * theta) + c4) eps1 = (-mu + sqrt(mu**2 + 2 * (sig**2) * (z + r))) / (sig**2) eps2 = (-mu - sqrt(mu**2 + 2 * (sig**2) * (z + r))) / (sig**2) b1 = 1 / (eps1 - eps2) * (eps2 / (z + r) + (1 - eps2) / z) b2 = 1 / (eps1 - eps2) * (eps1 / (z + r) + (1 - eps1) / z) ans += exp(z * tau) * bs(x, b1, b2, eps1, eps2, z, r, phi) * dz val = (K * (h / (2j * pi) * ans)).real return 2 * val
def BSLaplace(S,K,T,t,r,sig,N,phi): """Solving the Black Scholes PDE in the Laplace domain""" x = ln(S/K) r = mpf(r);sig = mpf(sig);T = mpf(T);t=mpf(t) S = mpf(S);K = mpf(K);x=mpf(x) mu = r - 0.5*(sig**2) tau = T - t c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0','0.2645') ans = 0.0 h = 2*pi/N h = mpf(h) for k in range(N/2): # Use symmetry theta = -pi + (k+0.5)*h z = N/tau*(c1*theta/tan(c2*theta) - c3 + c4*theta) dz = N/tau*(-c1*c2*theta/(sin(c2*theta)**2) + c1/tan(c2*theta)+c4) eps1 = (-mu + sqrt(mu**2 + 2*(sig**2)*(z+r)))/(sig**2) eps2 = (-mu - sqrt(mu**2 + 2*(sig**2)*(z+r)))/(sig**2) b1 = 1/(eps1-eps2)*(eps2/(z+r) + (1 - eps2)/z) b2 = 1/(eps1-eps2)*(eps1/(z+r) + (1 - eps1)/z) ans += exp(z*tau)*bs(x,b1,b2,eps1,eps2,z,r,phi)*dz val = (K*(h/(2j*pi)*ans)).real return 2*val
def plot_basic(self): x = numpy.arange(0.0, float(1/mpmath.sin(mpmath.pi/3)), 0.01) t = [] for l in numpy.nditer(x): if l < (1 / (2 * mpmath.sin(mpmath.pi/3))): t.append(float(mpmath.tan(mpmath.pi/3) * l)) else: t.append(float(1 - mpmath.tan(mpmath.pi/3) * (l - 1/(2*mpmath.sin(mpmath.pi/3))))) pylab.plot(x, t) pylab.hold(True)
def family_graphical_combination(t, c, angle_alpha_prime): """ Aberdeen family of graphical operators """ if not (isinstance(t, Opinion) and isinstance(c, Opinion) \ and t.check() and c.check()): raise Exception("Two valid Opinions are required!") if mpmath.almosteq(angle_alpha_prime, -mpmath.pi / 3, epsilon): new_magnitude = c.get_magnitude_ratio() * ( mpmath.mpf("2") * t.getUncertainty() / mpmath.sqrt(mpmath.mpf("3"))) #new_magnitude = 0 elif mpmath.almosteq(angle_alpha_prime, mpmath.mpf("2/3") * mpmath.pi, epsilon): new_magnitude = c.get_magnitude_ratio() * ( mpmath.mpf("2") * (mpmath.mpf("1") - t.getUncertainty()) / mpmath.sqrt(mpmath.mpf("3"))) #new_magnitude = 0 elif mpmath.almosteq(angle_alpha_prime, mpmath.mpf("1/2") * mpmath.pi, epsilon): #new_magnitude = 1 - t.getUncertainty() new_magnitude = 2 * t.getBelief() else: new_magnitude = c.get_magnitude_ratio() * (mpmath.mpf("2") * \ (mpmath.sqrt(mpmath.power(mpmath.tan(angle_alpha_prime), mpmath.mpf("2")) +1 ) / ( mpmath.absmax(mpmath.tan(angle_alpha_prime) + mpmath.sqrt(mpmath.mpf("3"))) ) ) * \ t.getBelief()) new_uncertainty = t.getUncertainty( ) + mpmath.sin(angle_alpha_prime) * new_magnitude new_disbelief = t.getDisbelief() + ( t.getUncertainty() - new_uncertainty) * mpmath.cos( mpmath.pi / 3) + mpmath.cos(angle_alpha_prime) * mpmath.sin( mpmath.pi / 3) * new_magnitude if mpmath.almosteq(new_uncertainty, 1, epsilon): new_uncertainty = mpmath.mpf("1") if mpmath.almosteq(new_uncertainty, 0, epsilon): new_uncertainty = mpmath.mpf("0") if mpmath.almosteq(new_disbelief, 1, epsilon): new_disbelief = mpmath.mpf("1") if mpmath.almosteq(new_disbelief, 0, epsilon): new_disbelief = mpmath.mpf("0") return Opinion(1 - new_disbelief - new_uncertainty, new_disbelief, new_uncertainty, "1/2")
def Asian(S, K, T, t, sig, r, N): # Assigning multi precision S = mpf(S) K = mpf(K) sig = mpf(sig) T = mpf(T) t = mpf(t) r = mpf(r) # Geman and Yor's variable tau = mpf(((sig**2) / 4) * (T - t)) v = mpf(2 * r / (sig**2) - 1) alp = mpf(sig**2 / (4 * S) * K * T) beta = mpf(-1 / (2 * alp)) tau = mpf(tau) N = mpf(N) # Initiate the stepsize h = 2 * pi / N mp.dps = 100 c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0', '0.2645') # The for loop is evaluating the Laplace inversion at each point theta which is based on the trapezoidal # rule ans = 0.0 for k in range(N / 2): # N/2 : symmetry theta = -pi + (k + 0.5) * h z = 2 * v + 2 + N / tau * (c1 * theta / tan(c2 * theta) - c3 + c4 * theta) dz = N / tau * (-c1 * c2 * theta / sin(c2 * theta)**2 + c1 / tan(c2 * theta) + c4) zz = N / tau * (c1 * theta / tan(c2 * theta) - c3 + c4 * theta) mu = sqrt(v**2 + 2 * z) a = mu / 2 - v / 2 - 1 b = mu / 2 + v / 2 + 2 G = (2 * alp)**(-a) * gamma(b) / gamma(mu + 1) * hyp1f1( a, mu + 1, beta) / (z * (z - 2 * (1 + v))) ans += exp(zz * tau) * G * dz return 2 * exp(tau * (2 * v + 2)) * exp( -r * (T - t)) * 4 * S / (T * sig**2) * h / (2j * pi) * ans
def Hypergeometric1F1(a,b,t,N): # Initiate the stepsize h = 2*pi/N; c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0','0.2645') # The for loop is evaluating the Laplace inversion at each point theta # which is based on the trapezoidal rule ans = 0.0 for k in range(N): theta = -pi + (k+0.5)*h z = N/t*(c1*theta/tan(c2*theta) - c3 + c4*theta) dz = N/t*(-c1*c2*theta/sin(c2*theta)**2 + c1/tan(c2*theta)+c4) ans += exp(z*t)*F(a,b,t,z)*dz return gamma(b)*t**(1-b)*exp(t)*((h/(2j*pi))*ans)
def Hypergeometric1F1(a, b, t, N): # Initiate the stepsize h = 2 * pi / N c1 = mpf('0.5017') c2 = mpf('0.6407') c3 = mpf('0.6122') c4 = mpc('0', '0.2645') # The for loop is evaluating the Laplace inversion at each point theta # which is based on the trapezoidal rule ans = 0.0 for k in range(N): theta = -pi + (k + 0.5) * h z = N / t * (c1 * theta / tan(c2 * theta) - c3 + c4 * theta) dz = N / t * (-c1 * c2 * theta / sin(c2 * theta)**2 + c1 / tan(c2 * theta) + c4) ans += exp(z * t) * F(a, b, t, z) * dz return gamma(b) * t**(1 - b) * exp(t) * ((h / (2j * pi)) * ans)
def fpaa_oracle(qc, q, n, clauses, oracle, index): theta = math.pi / clauses delta = pow(2, -0.5 * pow(n, 2)) / 2 _lambda = pow(math.sin(theta), 2) / 4 + pow(1 / 2 - math.cos(theta) / 2, 2) L = int(math.ceil(2 * math.log(2 / delta) / math.sqrt(_lambda))) gamma = mpmath.cos(mpmath.acos(1 / delta) / L) qc.barrier() A_matrix(qc, q, n, clauses, oracle, theta) qc.barrier() for k in range(1, L): alpha = abs(2 * mpmath.acot( mpmath.tan(2 * math.pi * (k) / L) * mpmath.sqrt(1 - 1 / pow(gamma, -2)))) beta = -abs(2 * mpmath.acot( mpmath.tan(2 * math.pi * (L - (k - 1)) / L) * mpmath.sqrt(1 - 1 / pow(gamma, -2)))) qc.h(q[n]) U_matrix(qc, q, n, beta) qc.h(q[n]) Adgr_matrix(qc, q, n, clauses, oracle, theta) U_matrix(qc, q, n, alpha) A_matrix(qc, q, n, clauses, oracle, theta) qc.barrier() qc.h(q[n]) qc.barrier() qc.x(q[n]) qc.x(q[n + 1]) qc.h(q[n + 1]) qc.cx(q[n], q[n + 1]) qc.h(q[n + 1]) qc.x(q[n + 1]) qc.x(q[n])
def getRegularPolygonArea( n, k ): if real( n ) < 3: raise ValueError( 'the number of sides of the polygon cannot be less than 3,' ) if not isinstance( k, RPNMeasurement ): return getRegularPolygonArea( n, RPNMeasurement( real( k ), 'meter' ) ) dimensions = k.getDimensions( ) if dimensions != { 'length' : 1 }: raise ValueError( '\'polygon_area\' argument 2 must be a length' ) return multiply( fdiv( n, fmul( 4, tan( fdiv( pi, n ) ) ) ), getPower( k, 2 ) ).convert( 'meter^2' )
def calc_wr(psi, ups_r, En, Lz, Q, aa, slr, ecc, x): """ Computes wr by analytic evaluation of the integral in Drasco and Hughes (2005) """ a1 = (1 - ecc**2) * (1 - En**2) b1 = 2 * (1 - En**2 - (1 - ecc**2) / slr) c1 = (((3 + ecc**2) * (1 - En**2)) / (1 - ecc**2) - 4 / slr + ((1 - ecc**2) * (aa**2 * (1 - En**2) + Lz**2 + Q)) / slr**2) if psi == mp.pi: # the closed form function has a singularity at psi = pi # but it can be evaluated in integral form to be pi return mp.pi else: return ((-2j * (1 - ecc**2) * ups_r * cos(psi / 2.)**2 * ellipf( 1j * asinh( sqrt((a1 - (-1 + ecc) * (b1 + c1 - c1 * ecc)) / (a1 + b1 + c1 - c1 * ecc**2 + sqrt( (b1**2 - 4 * a1 * c1) * ecc**2))) * tan(psi / 2.)), (a1 + b1 + c1 - c1 * ecc**2 + sqrt( (b1**2 - 4 * a1 * c1) * ecc**2)) / (a1 + b1 + c1 - c1 * ecc**2 - sqrt( (b1**2 - 4 * a1 * c1) * ecc**2))) * sqrt(2 + (2 * (a1 - (-1 + ecc) * (b1 + c1 - c1 * ecc)) * tan(psi / 2.)**2) / (a1 + b1 + c1 - c1 * ecc**2 - sqrt( (b1**2 - 4 * a1 * c1) * ecc**2))) * sqrt(1 + ((a1 - (-1 + ecc) * (b1 + c1 - c1 * ecc)) * tan(psi / 2.)**2) / (a1 + b1 + c1 - c1 * ecc**2 + sqrt( (b1**2 - 4 * a1 * c1) * ecc**2)))) / (sqrt((a1 - (-1 + ecc) * (b1 + c1 - c1 * ecc)) / (a1 + b1 + c1 - c1 * ecc**2 + sqrt( (b1**2 - 4 * a1 * c1) * ecc**2))) * slr * sqrt(2 * a1 + 2 * b1 + 2 * c1 + c1 * ecc**2 + 2 * (b1 + 2 * c1) * ecc * cos(psi) + c1 * ecc**2 * cos(2 * psi))))
def butter_lp(n, Wn): """ Lowpass Butterworth digital filter design. This computes the same result as scipy.signal.butter(n, Wn, output='zpk'), but it uses mpmath, and the results are returned in lists instead of numpy arrays. """ zeros = [] poles = _butter_analog_poles(n) k = 1 fs = 2 warped = 2 * fs * mpmath.tan(mpmath.pi * Wn / fs) z, p, k = _zpklp2lp(zeros, poles, k, wo=warped) z, p, k = _zpkbilinear(z, p, k, fs=fs) return z, p, k
def elliptic_core_g(x,y): x = x/2 y = y/2 factor = (cos(x)/sin(y) + sin(y)/cos(x) - (cos(y)/tan(y)/cos(x) + sin(x)*tan(x)/sin(y)))/pi k = tan(x)/tan(y) m = k*k n = (sin(x)/sin(y))*(sin(x)/sin(y)) u = asin(tan(y)/tan(x)) complete = ellipk(m) - ellippi(n, m) incomplete = ellipf(u,m) - ellippi(n/k/k,1/m)/k #incomplete = ellipf(u,m) - ellippi(n,u,m) return re(1.0 - factor*(incomplete + complete))
import numpy as np import decimal as dec import math from sys import maxsize import mpmath as mp import matplotlib.pyplot as plt f1 = lambda x: mp.cos(x) * mp.cosh(x) - 1 f2 = lambda x: 1 / x - mp.tan(x) if x != 0 else maxsize f3 = lambda x: 2**(-x) + mp.exp(x) + 2 * mp.cos(x) - 6 f1d = lambda x: mp.cos(x) * mp.sinh(x) - mp.sin(x) * mp.cosh(x) f2d = lambda x: -1 / (x**2) - 1 / (mp.cos(x)**2) f3d = lambda x: mp.exp(x) - (2**(-x) * mp.log(2) - 2 * mp.sin(x)) funcs = [f1, f2, f3] funcsD = [f1d, f2d, f3d] intervals = [(3 / 2 * math.pi, 2 * math.pi), (0, math.pi / 2), (1, 3)] precisions = [math.pow(10, -7), math.pow(10, -15), math.pow(10, -33)] DEC = mp.mpf def bisection(f, a, b, precision, E): mp.mp.dps = precision if mp.sign(f(a)) == mp.sign(f(b)): print("no root") return (maxsize, maxsize) middle = DEC(a) + (DEC(b) - DEC(a)) / 2 numOfSteps = 0 while abs(DEC(a) - DEC(b)) > E: middle = DEC(a) + (DEC(b) - DEC(a)) / 2
def get_max_x_cartesian(self): return (mpmath.mpf("2") - self.get_y_cartesian() + mpmath.tan(self.get_angle_alpha()) * self.get_x_cartesian()) / (mpmath.tan(self.get_angle_alpha()) + mpmath.sqrt("3"))
def constrain_square(): global DEBUG, last_action, points, dirs, images, img_size, index, input_dir, output_dir, args, width, height, image_width, image_height, lines, p_colors, l_colors, a_color, b_color, c_color, rectangles if len(points) == 3: dist = [] pairs = [] for pointA in points: for pointB in points: dist.append(abs(distance.euclidean(pointA, pointB))) pairs.append((pointA, pointB)) for point in points: # arbitrarily define temporary points in order to find pointC if not ((point == pairs[dist.index(max(dist))][0]) or (point == pairs[dist.index(max(dist))][1])): pointC = point hypot = max(dist) temp_distance_0 = abs( distance.euclidean(pointC, pairs[dist.index(max(dist))][0])) temp_distance_1 = abs( distance.euclidean(pointC, pairs[dist.index(max(dist))][1])) if (temp_distance_0 > temp_distance_1): pointA = pairs[dist.index(max(dist))][0] pointB = pairs[dist.index(max(dist))][1] angle_flip = False else: pointA = pairs[dist.index(max(dist))][1] pointB = pairs[dist.index(max(dist))][0] angle_flip = True if DEBUG: p_colors[points.index(pointA)] = a_color p_colors[points.index(pointB)] = b_color p_colors[points.index(pointC)] = c_color leg1 = abs(distance.euclidean(pointC, pointA)) hypot = abs(distance.euclidean(pointB, pointA)) leg1_vector = (pointC[0] - pointA[0], pointC[1] - pointA[1]) hypot_vector = (pointB[0] - pointA[0], pointB[1] - pointA[1]) if DEBUG: add_line(pointA, pointB, std_color) print( f'leg vector is {leg1_vector} and hyp_vector is {hypot_vector}' ) print( f'pointA is {pointA} and pointB is {pointB} and pointC is {pointC}' ) theta = sym.acos((leg1_vector[0] * hypot_vector[0] + leg1_vector[1] * hypot_vector[1]) / (leg1 * hypot)) std_unit_vector = (1, 0) theta_prime = sym.acos((leg1_vector[0] * std_unit_vector[0] + leg1_vector[1] * std_unit_vector[1]) / (leg1)) leg2 = leg1 * mp.tan(theta) increment = (leg2 * mp.sin(theta_prime), leg2 * mp.cos(theta_prime)) temp_b_check = pointB[0] > pointA[0] if pointC[1] > pointA[1]: increment = (-1 * increment[0], increment[1]) if not (temp_b_check == (float(pointC[0] + increment[0]) > pointA[0])): increment = (-1 * increment[0], -1 * increment[1]) third_point = (float(pointC[0] + increment[0]), float(pointC[1] + increment[1])) points[points.index(pointB)] = third_point pointB = third_point pointD = (float(pointA[0] + increment[0]), float(pointA[1] + increment[1])) add_point(pointD[0], pointD[1], std_color) validate_constraint() angle_factor = -1 rectangle_tilt = get_angle([pointC[0], pointC[1]], [pointA[0], pointA[1]], [pointA[0] + 20, pointA[1]]) if DEBUG: print(f'rectangle tilt is: {180 * rectangle_tilt / mp.pi}') rectangle_tilt *= angle_factor if DEBUG: print(f'shifted rectangle tilt is: {180 * rectangle_tilt / mp.pi}') rectangle_width = abs(distance.euclidean(pointC, pointA)) rectangle_height = abs(distance.euclidean(pointD, pointA)) averageX = 0 averageY = 0 for point in points: averageX += point[0] averageY += point[1] averageX /= len(points) averageY /= len(points) add_rectangle(averageX, averageY, rectangle_width, rectangle_height, rectangle_tilt) points = [] else: last_action = 'constrain_square failed: not enough points' lines = []
def tan(op): op = convertToRadians(op) return mpmath.tan(op)
'euler': mp.euler, #euler's constance = 0.577216... 'mpf': ['primitive', [lambda x, y: mp.mpf(str(x)), None]], 'mpc': ['primitive', [lambda x, y: mp.mpc(x, y[0]), None]], # 'sqrt': ['primitive', [lambda x, y: mp.sqrt(x), None]], 'cbrt': ['primitive', [lambda x, y: mp.cbrt(x), None]], 'root': ['primitive', [lambda x, y: mp.root(x, y[0]), None]], # y's root 'unitroots': ['primitive', [lambda x, y: Vector(mp.unitroots(x)), None]], # 'hypot': ['primitive', [lambda x, y: mp.hypot(x, y[0]), None]], # sqrt(x**2+y**2) # 'sin': ['primitive', [lambda x, y: mp.sin(x), None]], 'cos': ['primitive', [lambda x, y: mp.cos(x), None]], 'tan': ['primitive', [lambda x, y: mp.tan(x), None]], 'sinpi': ['primitive', [lambda x, y: mp.sinpi(x), None]], #sin(x * pi) 'cospi': ['primitive', [lambda x, y: mp.cospi(x), None]], 'sec': ['primitive', [lambda x, y: mp.sec(x), None]], 'csc': ['primitive', [lambda x, y: mp.csc(x), None]], 'cot': ['primitive', [lambda x, y: mp.cot(x), None]], 'asin': ['primitive', [lambda x, y: mp.asin(x), None]], 'acos': ['primitive', [lambda x, y: mp.acos(x), None]], 'atan': ['primitive', [lambda x, y: mp.atan(x), None]], 'atan2': ['primitive', [lambda x, y: mp.atan2(y[0], x), None]], 'asec': ['primitive', [lambda x, y: mp.asec(x), None]], 'acsc': ['primitive', [lambda x, y: mp.acsc(x), None]], 'acot': ['primitive', [lambda x, y: mp.acot(x), None]], 'sinc': ['primitive', [lambda x, y: mp.sinc(x), None]], 'sincpi': ['primitive', [lambda x, y: mp.sincpi(x), None]], 'degrees': ['primitive', [lambda x, y: mp.degrees(x),
def getNthHexagonalSquareNumberOperator(n): return nint( floor(fdiv(power(tan(fdiv(fmul(3, pi), 8)), fsub(fmul(8, n), 4)), 32)))
def QStan(x): if QSMODE == MODE_NORM: return cmath.tan(x) else: return mpmath.tan(x)
def calculate_all(self): ''' BDA Method's Approach : F`(X) ≈ [F(X) - F(X - ΔX)] / ΔX Where ΔX Is Equal To Value Of Step Size. ''' if (int(scale_for_func.get()) == 1): # Functions : func = lambda x: np.exp(x) derived_func = func # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 2): # Functions : func = lambda x: np.log(x) derived_func = lambda x: 1 / x # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 3): # Functions : func = lambda x: 1 / x derived_func = lambda x: -1 / (x**(2.0)) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 4): # Functions : func = lambda x: 10**x derived_func = lambda x: np.log(10) * (10**(x)) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 5): # Functions : func = lambda x: np.log10(x) derived_func = lambda x: 1 / (np.log(10) * x) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 6): # Functions : func = lambda x: np.log2(x) derived_func = lambda x: 1 / (np.log(2) * x) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 7): # Functions : func = lambda x: math.sin(math.radians(x)) derived_func = lambda x: math.cos(math.radians(x)) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 8): # Functions : func = lambda x: math.cos(math.radians(x)) derived_func = lambda x: -1 * (math.sin(math.radians(x))) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 9): # Functions : func = lambda x: mpmath.tan(mpmath.radians(x)) derived_func = lambda x: ((mpmath.sec(mpmath.radians(x)))**(2)) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 10): # Functions : func = lambda x: x**(2.0) derived_func = lambda x: 2 * x # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 11): # Functions : func = lambda x: math.sqrt(x) derived_func = lambda x: (1 / (2 * func(x))) # Calculations. exact_value = derived_func(float(x_value_entry.get())) appr_value = (func(float(x_value_entry.get())) - func( (float(x_value_entry.get()) - float(delta_x_input_entry.get())))) / float( delta_x_input_entry.get()) abs_error_value = (np.abs( ((exact_value - appr_value) / exact_value))) * float(100.0) self.clear_all() # Insertions. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, str(round(abs_error_value, 5)) + " %") elif (int(scale_for_func.get()) == 12): msb.showinfo("'None' Selected !", "Please Select Another Function !") else: msb.showerror("Error", "Please Try Again !")
def getRegularPolygonArea(n, k): return multiply(fdiv(n, fmul(4, tan(fdiv(pi, n)))), getPower(k, 2)).convert('meter^2')
from __future__ import division import mpmath as math from units import * from copy import copy def sqrt(x): if hasattr(x,'unit'): return math.sqrt(x.number) | x.unit**0.5 return math.sqrt(x) #trigonometric convenience functions which are "unit aware" sin=lambda x: math.sin(1.*x) cos=lambda x: math.cos(1.*x) tan=lambda x: math.tan(1.*x) cot=lambda x: math.cot(1.*x) asin=lambda x: math.asin(x) | rad acos=lambda x: math.acos(x) | rad atan=lambda x: math.atan(x) | rad atan2=lambda x,y: math.atan2(x,y) | rad #~ cos=math.cos #~ sin=math.sin #~ tan=math.tan #~ cosh=math.cosh #~ sinh=math.sinh #~ tanh=math.tanh #~ acos=math.acos #~ asin=math.asin #~ atan=math.atan acosh=math.acosh
def elliptic_core_h(core_g,x,y): M = elliptic_core_m(x,y) factor = -sin(y)*tan(y) sine2 = sin(y)*sin(y) return (M - core_g*cos(y) + cos(y) - cos(x)*cos(y))/sine2
def getNthHexagonalSquareNumber( n ): return nint( floor( fdiv( power( tan( fdiv( fmul( 3, pi ), 8 ) ), fsub( fmul( 8, real_int( n ) ), 4 ) ), 32 ) ) )
def eval(self, z): return mpmath.tan(z)
def tan_degrees(theta): """Return tangent of theta (given in degrees).""" return tan(radians(theta))
def calculate_all(self): ''' FDA Method's Approach : F`(X) ≈ [F(X + ΔX) - F(X)] / ΔX Where ΔX Is Equal To The Value Of Size Of Step. ''' if (int(func_scale.get()) == 1): ''' Since The Derivative Of exp(X) Is Equal To Itself, Both Results Will Be Extremely Irrelevant Towards Each Other. No Form Of Correlation Is Shown, Thus The Value Of Abs. Relative True Error Will Be Extremely High. ''' # Calculations. exact_value = np.exp(float(x_val_entry.get())) appr_value = (np.exp( (float(x_val_entry.get()) + float(delta_x_entry.get()))) - exact_value) / float(delta_x_entry.get()) abs_error = str( round( np.abs((exact_value - appr_value) / exact_value) * float(100.0), 5)) + " %" self.clear_all() # Insertions Where Results Are Rounded Off To At Most 5 Decimal Points. exact_entry.insert(self.__length_of_exact_entry, str(round(exact_value, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_value, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error) elif (int(func_scale.get()) == 2): ''' Derivative Of ln(X) -> np.log(X) Is X**(-1) Which Will Be Used For Calculating Exact Values. ''' try: exact_val = 1 / (float(x_val_entry.get())**(1.0)) appr_val = (np.log( (float(x_val_entry.get()) + float(delta_x_entry.get()) )) - np.log(float(x_val_entry.get()))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 3): ''' Derivative Of X**(-1) Is -1*(X**(-2)). ''' try: exact_val = -1 * (1 / (float(x_val_entry.get())**(2.0))) appr_val = (((float(x_val_entry.get()) + float(delta_x_entry.get()))**(-1.0)) - (float(x_val_entry.get())**(-1.0))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 4): ''' Derivative Of 10**(X) Is ln(10) * 10**(X). ''' try: exact_val = np.log(float(10.0)) * (10**(float( x_val_entry.get()))) appr_val = (10**( (float(x_val_entry.get()) + float(delta_x_entry.get())))) - (10**(float( x_val_entry.get()))) / float(delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 5): ''' Derivative Of log(X)[Base = 10] Is (ln(10) * X)**(-1). ''' try: exact_val = 1 / (np.log(float(10.0)) * float(x_val_entry.get())) appr_val = ((np.log10( (float(x_val_entry.get()) + float(delta_x_entry.get()) ))) - np.log10(float(x_val_entry.get()))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 6): ''' Derivative Of log(X)[Base = 2] Is (ln(2) * X)**(-1). ''' try: exact_val = 1 / (np.log(float(2.0)) * float(x_val_entry.get())) appr_val = (np.log2( (float(x_val_entry.get()) + float(delta_x_entry.get()) )) - np.log2(float(x_val_entry.get()))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 7): ''' Derivative Of Sin(X) Is Cos(X). ''' try: exact_val = np.cos(math.radians(float(x_val_entry.get()))) appr_val = (np.sin( (math.radians(float(x_val_entry.get())) + math.radians(float(delta_x_entry.get())))) - np.sin(math.radians(float(x_val_entry.get()))) ) / math.radians(float(delta_x_entry.get())) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 8): ''' Derivative Of Cos(X) Is -Sin(X). ''' try: exact_val = float(-1.0) * np.sin( math.radians(float(x_val_entry.get()))) appr_val = (np.cos( (math.radians(float(x_val_entry.get())) + math.radians(float(delta_x_entry.get())))) - np.cos(math.radians(float(x_val_entry.get()))) ) / math.radians(float(delta_x_entry.get())) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 9): ''' Derivative Of Tan(X) Is Sec^2(X). ''' try: exact_val = float( sec(float(math.radians(x_val_entry.get()))))**(2.0) appr_val = (tan( math.radians(float(x_val_entry.get())) + math.radians(float(delta_x_entry.get()))) - tan(math.radians(float(x_val_entry.get()))) ) / math.radians(float(delta_x_entry.get())) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 10): ''' Derivative Of X^2 Is 2 * X. ''' try: exact_val = float(2 * float(x_val_entry.get())) appr_val = (((float(x_val_entry.get()) + float(delta_x_entry.get()))**(2.0)) - (float(x_val_entry.get())**(2.0))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 11): ''' Derivative Of √X Is 1 / (2 * √X). ''' try: exact_val = float(1 / (2 * sqrt(float(x_val_entry.get())))) appr_val = (sqrt( (float(x_val_entry.get()) + float(delta_x_entry.get()) )) - sqrt(float(x_val_entry.get()))) / float( delta_x_entry.get()) abs_error_val = str( round( np.abs((exact_val - appr_val) / exact_val) * float(100.0), 5)) + " %" self.clear_all() exact_entry.insert(self.__length_of_exact_entry, str(round(exact_val, 5))) appr_entry.insert(self.__length_of_appr_entry, str(round(appr_val, 5))) abs_error_entry.insert(self.__length_of_abserror_entry, abs_error_val) except Exception: msb.showerror("INFO", "An Error Occured !") elif (int(func_scale.get()) == 12): msb.showinfo("'None' Specified !", "No Function Is Specified.") else: msb.showerror("Invalid Selection", "Please Try Again.")
def lat_to_tiley(lat, zoom): lat1 = lat * mpmath.pi / 180.0 result = int( (1.0 - math.log(mpmath.tan(lat1) + mpmath.sec(lat1)) / mpmath.pi) / 2.0 * (2.0**zoom)) return result