def phi3(x0, *, dps): # (epx(x)-1-x-0.5*x*x)/(x*x*x) , |x| > 1e-32 -> dps > 100 with mp.workdps(dps): y = mp.matrix([ mp.fdiv( mp.fsub(mp.fsub(mp.expm1(x), x), mp.fmul('0.5', mp.fmul( x, x))), mp.power(x, '3')) if x != 0.0 else mpf(1) / mpf(6) for x in x0 ]) return np.array(y.tolist(), dtype=x0.dtype)[:, 0]
def FreeFermions(eigvec, subsystem, FermiVector): r=range(FermiVector) Cij=mp.matrix([[mp.fsum([eigvec[i,k]*eigvec[j,k] for k in r]) for i in subsystem] for j in subsystem]) C_eigval=mp.eigsy(Cij, eigvals_only=True) EH_eigval=mp.matrix([mp.log(mp.fdiv(mp.fsub(mp.mpf(1.0),x),x)) for x in C_eigval]) S=mp.re(mp.fsum([mp.log(mp.mpf(1.0)+mp.exp(-x))+mp.fdiv(x,mp.exp(x)+mp.mpf(1.0)) for x in EH_eigval])) return(S)
def phi2(x0, *, dps): # (exp(x) - 1 - x) / (x * x), |x| > 1e-32 -> dps > 40 with mp.workdps(dps): y = mp.matrix([ mp.fdiv(mp.fsub(mp.expm1(x), x), mp.fmul(x, x)) if x != 0.0 else mpf(1) / mpf(2) for x in x0 ]) return np.array(y.tolist(), dtype=x0.dtype)[:, 0]
def diff(EP: float, AP: float): ''' Purpose: to calculate the difference between the exact and approx value Return: returns the error ''' return mp.fsub(EP, AP)
def FreeFermions(subsystem, C): C = mp.matrix([[C[x, y] for x in subsystem] for y in subsystem]) C_eigval = mp.eigh(C, eigvals_only=True) EH_eigval = mp.matrix( [mp.log(mp.fdiv(mp.fsub(mp.mpf(1.0), x), x)) for x in C_eigval]) S = mp.re( mp.fsum([ mp.log(mp.mpf(1.0) + mp.exp(-x)) + mp.fdiv(x, mp.exp(x) + mp.mpf(1.0)) for x in EH_eigval ])) return (S)
def FreeFermions(subsystem, C_t): #implements free fermion technique by peschel C = mp.matrix([[C_t[x, y] for x in subsystem] for y in subsystem]) C_eigval = mp.eigh(C, eigvals_only=True) EH_eigval = mp.matrix( [mp.log(mp.fdiv(mp.fsub(mp.mpf(1.0), x), x)) for x in C_eigval]) S = mp.re( mp.fsum([ mp.log(mp.mpf(1.0) + mp.exp(-x)) + mp.fdiv(x, mp.exp(x) + mp.mpf(1.0)) for x in EH_eigval ])) return (S)
def rate_process_RMSE(rate_log_dic, correct_rate, size): l = [] dic = {} correct_rate_mpf = mp.mpf(correct_rate) for i in rate_log_dic: if rate_log_dic[i] == None: continue else: element = mp.fsub(rate_log_dic[i], correct_rate_mpf) l.append(element) square = mp.fsum(l, squared=True) mean = mp.fdiv(square, size) root = mp.sqrt(mean) dic['rate_RMSE'] = root return dic
def Sub(a, b): return mp.fsub(a, b, exact=True)
def dipole(r, theta, charge, a): ''' Purpose : To calculate Electric Potential due to Dipole considering very small values Formula Used : (q*const_k)*(1/r_1 - 1/r_2) where, r_1 = distance between negative charge and point of observation = (r^2 + a^2 - 2*a*Cos(theta))**0.5 r_2 = distance between positive charge and point of observation = (r^2 + a^2 + 2*a*Cos(theta))**0.5 const_k = 4*pi*epsilon_not = 8.9875518 x 10^9 Parameters : a) r - distance between center of the dipole and point of observation b) theta - Angle between positive charge and point of observation c) charge - either charge irrespective of sign d) a - distance between either charge and center of dipole Return: returns exact value of electric field calculated ''' # Editing value of r as per the unit if r[1].lower() in ('meters', 'm'): r = r[0] elif r[1].lower() in ('centimeters', 'cm'): r = mp.fmul(r[0], 10**(-2)) elif r[1].lower() in ('millimeters', 'mm'): r = mp.fmul(r[0], 10**(-3)) elif r[1].lower() in ('angstroms', 'a', 'A'): r = mp.fmul(r[0], 10**(-10)) # Editing value of a as per the unit if a[1].lower() in ('meters', 'm'): a = a[0] elif a[1].lower() in ('centimeters', 'cm'): a = mp.fmul(a[0], 10**(-2)) elif a[1].lower() in ('millimeters', 'mm'): a = mp.fmul(a[0], 10**(-3)) elif a[1].lower() in ('angstroms', 'a', 'A'): a = mp.fmul(a[0], 10**(-10)) # Calculating Value of Cos(theta) if theta[1].lower() == 'radians': cos_theta = round(mp.cos(theta[0]), 5) elif theta[1].lower() == 'degrees': cos_theta = round(mp.cos(mp.radians(theta[0])), 5) # Editing Value of charge as per the unit if charge[1] in ('Coulomb', 'C'): charge = charge[0] elif charge[1] in ('microCoulomb', 'uC'): charge = mp.fmul(charge[0], 10**(-6)) elif charge[1] in ('milliCoulomb', 'mC'): charge = mp.fmul(charge[0], 10**(-3)) elif charge[1] in ('electronCharge', 'eC'): charge = mp.fmul(charge[0], mp.fmul(1.60217646, mp.fmul(10, -19))) # 1.60217646⋅10-19 elif charge[1] in ('nanoCoulomb', 'nC'): charge = mp.fmul(charge[0], mp.power(10, -10)) elif charge[1] in ('picoCharge', 'pC'): charge = mp.fmul(charge[0], mp.power(10, -12)) # Calculating value of r_1 and r_2 r_1 = mp.sqrt( mp.fsub(mp.fadd(mp.power(r, 2), mp.power(a, 2)), mp.fmul(2, mp.fmul(a, mp.fmul(r, cos_theta))))) r_2 = mp.sqrt( mp.fadd(mp.fadd(mp.power(r, 2), mp.power(a, 2)), mp.fmul(2, mp.fmul(a, mp.fmul(r, cos_theta))))) # Calculating final result result = mp.fmul(mp.fmul(charge, const_k), mp.fsub(mp.fdiv(1, r_1), mp.fdiv(1, r_2))) # returning final result return result
# -*- coding: utf-8 -*- """ Created on Thu Jun 6 13:44:19 2019 arbitrary prescision @author: Chris """ from mpmath import mp mp.dps = 100 #set prescision (number of digits) pi = mp.mpf(mp.pi) #mpmath has a good built in pi pi_string = '3.1415926535897932384626433832795028841971693993751058209749445923078164062' print(len(pi_string), ' digits of pi') #mpf input should be a string, entering a float is imprecise pi2 = mp.mpf(pi_string) difference = mp.fsub(pi2, pi) digits_to_print = mp.dps - len(pi_string) #number of sig. figs. to print print('difference: ', end='') mp.nprint(difference, n=digits_to_print) #need nprint for mpf truncation import decimal as de de.getcontext().prec = 50 #set prescision for decimal module pi_de = de.Decimal(pi_string) ### uncomment below #print('decimal pi', pi_de) #print('1/pi', 1/pi_de)