def gauss_laguerre(n): d = mp.matrix(mp.arange(1, 2 * n, 2)) e = mp.matrix(mp.arange(-1, -n - 1, -1)) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = z.apply(lambda x: x**2) return d, z.T
def gauss_genlaguerre(n, alpha): d = mp.matrix([i + alpha for i in mp.arange(1, 2 * n, 2)]) e = [-mp.sqrt(k * (k + alpha)) for k in mp.arange(1, n)] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = mp.gamma(alpha + 1) * z.apply(lambda x: x**2) return d, z.T
def n_both_xis(n_max, y, cancel_keff=False): big_xi_list = [ big_xi(n, y, cancel_keff=cancel_keff) for n in mp.arange(n_max + 1) ] small_xi_list = [ small_xi(n, y, cancel_keff=cancel_keff) for n in mp.arange(n_max + 1) ] return np.array(big_xi_list, dtype=return_dtype), np.array(small_xi_list, dtype=return_dtype)
def gauss_jacobi(n, alpha, beta): d = mp.matrix([(beta**2 - alpha**2) / (2 * k + alpha + beta) / (2 * k + alpha + beta - 2) for k in mp.arange(1, n + 1)]) e = [ k * (k + alpha) * (k + beta) * (k + alpha + beta) / ((2 * k + alpha + beta)**2 - 1) for k in mp.arange(1, n) ] e = [ 2 / (2 * k + alpha + beta) * mp.sqrt(x) for k, x in zip(mp.arange(1, n), e) ] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = 2**(alpha + beta + 1) * mp.beta(alpha + 1, beta + 1) * z.apply(lambda x: x**2) return d, z.T
def gauss_hermite(n): d = mp.matrix([mp.mpf('0.0') for _ in range(n)]) e = [mp.sqrt(k / 2) for k in mp.arange(1, n)] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = mp.sqrt(mp.pi) * z.apply(lambda x: x**2) return d, z.T
def gauss_legendre(n): d = mp.matrix([mp.mpf('0.0') for _ in range(n)]) e = [k / mp.sqrt(4 * k**2 - 1) for k in mp.arange(1, n)] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = 2 * z.apply(lambda x: x**2) return d, z.T
def gauss_gegenbauer(n, alpha): d = mp.matrix([mp.mpf('0.0') for _ in range(n)]) e = [ mp.sqrt(k * (k + 2 * alpha - 1) / ((2 * k + 2 * alpha - 1)**2 - 1)) for k in mp.arange(1, n) ] e.append(mp.mpf('0.0')) e = mp.matrix(e) z = mp.eye(n)[0, :] tridiag_eigen(mp, d, e, z) z = 2**(2 * alpha) * mp.beta(alpha + 1 / 2, alpha + 1 / 2) * z.apply(lambda x: x**2) return d, z.T
def segregate(num, precision): mp.dps = precision num = int(mp.floor(mp.sqrt(num)) + 1) cores = processor_cnt if num <= processor_cnt: cores = num - 1 num_mod = mp.fmod(num, cores) num_mods = [1 for _ in mp.arange(num_mod)] while len(num_mods) < cores: num_mods.append(0) num_div = mp.floor(mp.fdiv(num, cores)) num_divs, ip = [], 0 while len(num_divs) < cores: num_divs.append(num_div + num_mods[ip]) ip += 1 num_seg, place, seg = [num_divs[0]], num_divs[0], 1 while len(num_seg) < cores: place += num_divs[seg] num_seg.append(place) seg += 1 num_seg = [int(num_seg[i]) for i in range(len(num_seg))] return num_seg, cores
def n_small_xis(n_count, y, cancel_keff=False): xi_list = [ small_xi(n, y, cancel_keff=cancel_keff) for n in mp.arange(n_count + 1) ] return np.array(xi_list, dtype=return_dtype)
for L in Lrange: mp.dps = 3.0 * L l = int(L / 4) la = list(range(l)) lb = list(range(la[-1] + 1, la[-1] + l + 1)) ld = list(range(lb[-1] + 1, L - l)) lc = list(range(ld[-1] + 1, ld[-1] + l + 1)) iter = 0 #Parameters at t=0- w = mp.mpf(0.1) delta0 = mp.mpf(0.0) #Parameters at t=0+ v = 1.5 deltat = mp.mpf(0.0) tsteps = mp.arange(0.0, 50.01, 0.5) Sqtopo = [] Sab = [] Sbc = [] Sb = [] Sabc = [] pvp = np.zeros(L) pvm = np.zeros(L) for i in list(range(L)): pvp[i] = (i + 1) % L pvm[(i + 1) % L] = i # outputfile="./SqtopoOBC_mpdps_"+str(mp.dps)+"_L_"+str(L)+"_l_"+str(l)+"_mu_"+mp.nstr(chemical0)+"_muf_"+mp.nstr(chemicalt)+"_d_"+mp.nstr(delta0)+"_dt_"+mp.nstr(deltat)+".dat"
def S_Kummer(beta0, kc, L, order, qmax): """Compute even and odd coefficents at an order number""" K = 2 * pi / L beta_n = lambda n: beta0 + n * K gamma_n = lambda n: -1j * np.sqrt(kc ** 2 - beta_n(n) ** 2 + 0j) theta_n = lambda n: np.arcsin(beta_n(n) / kc - 1j * np.sign(n) * np.spacing(1)) if order == 0: qs = np.hstack([np.arange(-qmax, 0), np.arange(1, qmax + 1)]) S0sum = 1 / gamma_n(qs) - 1 / (K * np.abs(qs)) \ - (kc ** 2 + 2 * beta0 ** 2) / (2 * K ** 3 * np.abs(qs) ** 3) S0sum = np.sum(S0sum) S0 = -1 - (2j / pi) * (np.euler_gamma + np.log(kc / (2 * K))) \ - 2j / (gamma_n(0) * L) \ - 2j * (kc ** 2 + 2 * beta0 ** 2) * zeta(3) / (K ** 3 * L) \ - (2j / L) * S0sum return S0 qs = np.arange(1, qmax + 1) oeven = 2 * order oodd = 2 * order - 1 # Even sum with wavenumber terms, large terms excluded SEsum0 = np.exp(-1j * oeven * theta_n(qs)) / gamma_n(qs) \ + np.exp(1j * oeven * theta_n(-qs)) / gamma_n(-qs) SEsum0 = SEsum0.sum() SEsum0 += np.exp(-1j * oeven * theta_n(0)) / gamma_n(0) SEsum0 *= -2j / L # Odd sum with wavenumber terms, large terms excluded SOsum0 = np.exp(-1j * oodd * theta_n(qs)) / gamma_n(qs) \ - np.exp(1j * oodd * theta_n(-qs)) / gamma_n(-qs) SOsum0 = SOsum0.sum() SOsum0 += np.exp(-1j * oodd * theta_n(0)) / gamma_n(0) SOsum0 *= 2j / L # Even sum with factorial terms ms = np.arange(1., order + 1) b = np.array([float(mp.bernpoly(2 * m, beta0 / K)) for m in ms]) SEsumF = (-1) ** ms * 2 ** (2 * ms) * factorial(order + ms - 1) \ * (K / kc) ** (2 * ms) * b \ / (factorial(2 * ms) * factorial(order - ms)) SEsumF = np.sum(SEsumF) SEsumF *= 1j / pi # Odd sum with factorial terms ms = np.arange(0, order) b = np.array([float(mp.bernpoly(2 * m + 1, beta0 / K)) for m in ms]) SOsumF = (-1) ** ms * 2 ** (2 * ms) * factorial(order + ms - 1) \ * (K / kc) ** (2 * ms + 1) * b \ / (factorial(2 * ms + 1) * factorial(order - ms - 1)) SOsumF = np.sum(SOsumF) SOsumF *= -2 / pi # extended precision calculations for large sum terms t1 = (1 / pi) * (kc / (2 * K)) ** oeven t2 = (beta0 * L * order / pi ** 2) * (kc / (2 * K)) ** oodd # assume we need ~15 digits of precision at a magnitude of 1 dps = np.max([int(np.ceil(np.log10(np.abs(t1)))) + 15, int(np.ceil(np.log10(np.abs(t2)))) + 15, 15]) mp.dps = dps arg_ = mp.mpf(kc / (2 * K)) SEinf = -(-1) ** order * arg_ ** (2 * order) \ * mp.zeta(2 * order + 1) / mp.pi SOinf = (-1) ** order * beta0 * L * order * arg_ ** (2 * order - 1) \ * mp.zeta(2 * order + 1) / mp.pi ** 2 for i, m in enumerate(mp.arange(1, qmax + 1)): even_term = ((-1) ** order / (m * mp.pi)) * (arg_ / m) ** (2 * order) odd_term = ((-1) ** order * beta0 * L * order / (m ** 2 * mp.pi ** 2)) \ * (arg_ / m) ** (2 * order - 1) SEinf += even_term SOinf -= odd_term # break condition, where we should be OK moving back to double precision if mp.fabs(even_term) < 1 and mp.fabs(odd_term) < 1: break mp.dps = 15 SEinf = complex(SEinf) SOinf = complex(SOinf) if i + 1 < qmax: ms = np.arange(i + 2, qmax) even_terms = ((-1) ** order / (ms * pi)) \ * (kc / (2 * K * ms)) ** (2 * order) odd_terms = ((-1) ** order * beta0 * L * order / (ms ** 2 * pi ** 2)) \ * (kc / (2 * K * ms)) ** (2 * order - 1) SEinf += even_terms.sum() SEinf *= 2j SOinf -= odd_terms.sum() SOinf *= 2 SEven = SEsum0 + SEinf + 1j / (pi * order) + SEsumF SOdd = SOsum0 + SOinf + SOsumF return SOdd, SEven
from mpmath import mp import numpy as np tin = 0.1 dt = 0.1 tfin = 2 Win = 0 #first amplitude of disorder Wfin = 6 #last amplitude of disorder dW = .2 #resolution of disorder amplitude L = 64 t_vec = [mp.mpf(0.001)] + list(mp.arange(tin, tfin, dt)) W_vec = mp.arange(Win, Wfin, dW) outputfile = "./BUMP_SD_length=" + str(L) + ".dat" f1 = open(outputfile, "w") for t in t_vec: for s in W_vec: print(t, s) inputfile = "./BUMP_SD_length=" + str(L) + "_t=" + mp.nstr( t, 3) + "_s=" + mp.nstr(s, 3) + ".dat" ND = np.genfromtxt(inputfile) #,dtype=float, max_rows=250) ND = ND[~np.isnan(ND)] value = np.mean(ND) st = np.std(ND) f1.write("%.10f,%.10f,%.10f,%.10f,%i\n" % (t, s, value, st, len(ND)))
from mpmath import mp from jinja2 import Environment, FileSystemLoader class str_item: def __init__(self, n, l): self.number = n self.log10 = l[0:2] + "\,".join([l[i:i+3] for i in range(2, len(l), 3)]) if __name__ == "__main__": input_dps = 5 output_dps = 60 rows_per_page = 50 mp.dps = output_dps + 10 table_raw = [] for number in mp.arange(1.0, 10.0, mp.power(10, -(input_dps-1))): table_raw.append([number, mp.log10(number)]) table_str = [] table_str = [str_item(mp.nstr(row[0], input_dps), mp.nstr(row[1], output_dps)) for row in table_raw] pages = [table_str[i:i+rows_per_page] for i in range(0, len(table_str), rows_per_page)] latex_renderer = Environment( block_start_string = "%{", block_end_string = "%}", line_statement_prefix = "%#", variable_start_string = "%{{", variable_end_string = "%}}", loader = FileSystemLoader(".")) template = latex_renderer.get_template("template.tex")