def print_fun_results(x): res1 = float(rf(x)) res2 = float(fp(x)) print res1 print res2 print res1 - res2 print fdistan_two(res1, res2) print special.airy(x) print special.ai_zeros(1)
def test_trangle_well(self): F = 2e-4 xmax = 1E3 mass = 0.067 # Typical parameters for GaAs x = np.linspace(0, xmax, 200) step = x[1] - x[0] V = F * (xmax - x) Es = np.linspace(0, 0.2, 300) EigenEs = cSimpleSolve1D(step, Es, V, mass) psis = cSimpleFillPsi(step, EigenEs, V, mass) # Theoretical result nmax = 8 an = ai_zeros(nmax)[0] EigenEs_th = -(hbar**2 * F**2 / (2 * m_e * mass * e * ANG**2))**(1 / 3) * an psis_th = np.array([ airy((2 * mass * m_e * e * ANG**2 * F / hbar**2)**(1 / 3) * (x - E / F))[0] for E in EigenEs_th ]) psis_th /= (np.linalg.norm(psis_th, axis=1) * np.sqrt(step))[:, None] np.testing.assert_array_almost_equal(EigenEs[:nmax], EigenEs_th, decimal=E_dec, verbose=True) np.testing.assert_array_almost_equal(psis[:nmax, ::-1], psis_th, decimal=wf_dec, verbose=True)
def triangle_well(F, xmax=1E3): x = np.linspace(0, xmax, 5000) step = x[1] - x[0] V = F * (xmax - x) mass = 0.067 Es = np.linspace(0, 0.15, 100) EigenEs = cSimpleSolve1D(step, Es, V, mass) psis = cSimpleFillPsi(step, EigenEs, V, mass) # an = np.arange(0, 8) + 0.75 # EigenEs_th = (hbar**2/(2*m_e*mass*e*ANG**2)*(3*pi*F*an/2)**2)**(1/3) # Above is approximate Airy zeros result from scipy.special import airy, ai_zeros an = ai_zeros(8)[0] EigenEs_th = -(hbar**2 * F**2 / (2 * m_e * mass * e * ANG**2))**(1 / 3) * an psis_th = np.array([ airy((2 * mass * m_e * e * ANG**2 * F / hbar**2)**(1 / 3) * (x - E / F))[0] for E in EigenEs_th ]) psis_th /= (np.linalg.norm(psis_th, axis=1) * sqrt(step))[:, None] V = np.ascontiguousarray(V[::-1]) psis = np.ascontiguousarray(psis[:, ::-1]) print("Eigen Energy: ", EigenEs) print("Thoery: ", EigenEs_th) print("Diff: ", EigenEs - EigenEs_th) plot(x, V) scale = 0.15 for n in range(0, EigenEs.size): plot(x, EigenEs[n] + psis[n, :] * scale) for n in range(0, EigenEs_th.size): plot(x, EigenEs_th[n] + psis_th[n, :] * scale, '--') return x, V, EigenEs, psis, EigenEs_th, psis_th
def testAiry(self): def fAiry(x): f = special.airy(x) return f[0] f = fAiry solver = newton.Newton(f, tol=1.e-15, maxiter=100) x = solver.solve(-1.5) x0 = special.ai_zeros(1) x0 = x0[0] self.assertAlmostEqual(x, x0)
def time_ai_zeros(self): ai_zeros(100000)
def time_ai_zeros(self): ai_zeros(100000)
from scipy import special as sp print sp.ai_zeros(3)
def calc_zeta(n): """Calculate the nth zero of the Airy function.""" return special.ai_zeros(n)[0][-1]
rectgauss = sym.exp(-u_x**4-u_y**4) stgauss = gauss*(1. - A*(sym.sin(B*(u_x))+sym.sin(B*(u_y - 2*pi*0.3)))) asymgauss = sym.exp(-u_x**2-u_y**4) supergauss2 = sym.exp(-(u_x**2+u_y**2)**2) supergauss3 = sym.exp(-(u_x**2+u_y**2)**3) superlorentz = 1./((u_x**2 + u_y**2)**2+1.) lensf = gauss lensg = np.array([sym.diff(lensf, u_x), sym.diff(lensf, u_y)]) lensh = np.array([sym.diff(lensf, u_x, u_x), sym.diff(lensf, u_y, u_y), sym.diff(lensf, u_x, u_y)]) lensgh = np.array([sym.diff(lensf, u_x, u_x, u_x), sym.diff(lensf, u_x, u_x, u_y), sym.diff(lensf, u_x, u_y, u_y), sym.diff(lensf, u_y, u_y, u_y)]) lensfun = sym.lambdify([u_x, u_y], lensf, "numpy") lensg = sym.lambdify([u_x, u_y], lensg, "numpy") lensh = sym.lambdify([u_x, u_y], lensh, "numpy") lensgh = sym.lambdify([u_x, u_y], lensgh, "numpy") airsqrenv = interp1d(ai_zeros(100)[1], airy(ai_zeros(100)[1])[0]**2/2., kind = 'cubic', fill_value = 'extrapolate') @jit(nopython=True) def alpha(dso, dsl, f, dm): """ Returns alpha coefficient. """ dlo = dso - dsl return -c**2*re*dsl*dlo*dm/(2*pi*f**2*dso) @jit(nopython=True) def rFsqr(dso, dsl, f): """ Returns the square of the Fresnel scale. """ dlo = dso - dsl return c*dsl*dlo/(2*pi*f*dso) @jit(nopython=True) def lensc(dm, f):
def exact(n, l, k): zn = -sp.ai_zeros(4)[0] return 2. - 4. * epsilon * np.cos(2. * np.pi * k / l) + 4. * epsilon * ( (0.5 * np.cos(2. * np.pi * k / l))**(1. / 3.)) * h**(2. / 3.) * zn[n]
from scipy.special import eval_hermite, gamma, ai_zeros def V(x): # potential return beta * abs(x) def uVu(x): # integrand $u_m V u_n$ y, c = x / a0, a0 * np.sqrt( np.pi * gamma(m + 1) * gamma(n + 1) * 2**(m + n)) return V(x) * eval_hermite(m, y) * eval_hermite(n, y) * np.exp(-y * y) / c beta, omega, N = 1., 1., 20 # pot slope, nat freq, num. basis states a0, x0 = 1 / np.sqrt(omega), 1 / (2 * beta)**(1. / 3) # length scale, x0 m, Vm = np.arange(N - 2), np.zeros((N, N)) mm = np.sqrt((m + 1) * (m + 2)) # calc T matrix Tm = np.diag(np.arange(1, N + N, 2, float)) # diagonal Tm -= np.diag(mm, 2) + np.diag(mm, -2) # off diagonal by +/-2 for m in range(N): # calc V matrix for n in range(m, N, 2): # m+n is even every 2 steps Vm[m, n] = 2 * itg.gauss(uVu, 0., 10 * a0) # use symm. if (m != n): Vm[n, m] = Vm[m, n] Tm = Tm * omega / 4. E, u = eigsh(Tm + Vm, 6, which='SA') # get lowest 6 states zn, zpn, zbn, zbpn = ai_zeros(3) # find exact values Ex = -beta * x0 * np.insert(zpn, range(1, 4), zn) # combine even/odd print(E, E / Ex)
field = amp * np.exp(1j * (phase + pshift)) dynspec = np.real(np.multiply(field, np.conj(field))) return rays, amp, phase, field, dynspec, pshift # We are going to solve the lens equation to find the mapping between the # u' and u plane numerically using a root finding algorithm. # For now, however, this won't be necessary - but we'll set up the tools for later (3.2.21). # Compute 100 zeros and values of the Airy function Ai and its derivative # where index 1 for ai_zeros() returns the first 100 zeros of Ai’(x) and # index 0 for airy() returns Ai'(x). # Note for later: A caustic in which two images merge corresponds to a # fold catastrophe, and close to a fold the field follows an Airy function pattern. airyzeros = ai_zeros(100)[1] airyfunc = airy(airyzeros)[0]**2 / 2. airsqrenv = interp1d(airyzeros, airyfunc, kind='cubic', fill_value='extrapolate') # Define screen paramters (Gaussian & Kolmogorov) nscreen = 100 N = 10 sigma = 2 iscreen = 1 #which wavefront to plot dso = 1. * kpc * pctocm #distance from source to observer dsl = 1. * kpc * pctocm / nscreen #distance from source to screen dm = -1e-5 * pctocm #dispersion measure #ax, ay = 0.04*autocm, 0.04*autocm #screen width (x,y)
import numpy as np, integral as itg from scipy.sparse.linalg import eigsh from scipy.special import eval_hermite, gamma, ai_zeros def V(x): # potential return beta*abs(x) def uVu(x): # integrand $u_m V u_n$ y, c = x/a0, a0*np.sqrt(np.pi*gamma(m+1)*gamma(n+1)*2**(m+n)) return V(x)*eval_hermite(m,y)*eval_hermite(n,y)*np.exp(-y*y)/c beta, omega, N = 1., 1., 20 # pot slope, nat freq, num. basis states a0, x0 = 1/np.sqrt(omega), 1/(2*beta)**(1./3) # length scale, x0 m, Vm = np.arange(N-2), np.zeros((N,N)) mm = np.sqrt((m+1)*(m+2)) # calc T matrix Tm = np.diag(np.arange(1, N+N, 2,float)) # diagonal Tm -= np.diag(mm, 2) + np.diag(mm, -2) # off diagonal by +/-2 for m in range(N): # calc V matrix for n in range(m, N, 2): # m+n is even every 2 steps Vm[m, n] = 2*itg.gauss(uVu, 0., 10*a0) # use symm. if (m != n): Vm[n,m] = Vm[m,n] Tm = Tm*omega/4. E, u = eigsh(Tm + Vm, 6, which='SA') # get lowest 6 states zn, zpn, zbn, zbpn = ai_zeros(3) # find exact values Ex = - beta*x0*np.insert(zpn, range(1, 4), zn) # combine even/odd print (E, E/Ex)