def eigenvalue(ev): a = sp.Symbol('a') b = sp.Symbol('b') c = sp.Symbol('c') Hna = sp.hermite(ev, a) Hnb = sp.hermite(ev, b) Psi = (1 / ((2**e_l * sp.factorial(ev))**(1 / 2)) * (np.pi**(-1 / 4)) * sp.exp(-(a**2 + b**2) / 2) * Hna * Hnb) return int( sp.simplify( (-1 / 2 * (sp.diff(Psi, a, a) + sp.diff(Psi, b, b)) + 1 / 2 * (a**2 + b**2) * Psi) / Psi))
def __init__(self, M): super(HOOrbitals, self).__init__(M, "HarmonicOscillator") self.Hx = [] self.Hy = [] self.nShells = int(0.5*(sqrt(1 + 4*self.maxImplemented) - 1)) for i in range(self.nShells): self.Hx.append(hermite(i, k*x)) self.Hy.append(hermite(i, k*y)) self.expFactor = exp(-Rational(1,2)*k**2*(x**2 + y**2))
def test_manualintegrate_orthogonal_poly(): n = symbols('n') a, b = 7, S(5)/3 polys = [jacobi(n, a, b, x), gegenbauer(n, a, x), chebyshevt(n, x), chebyshevu(n, x), legendre(n, x), hermite(n, x), laguerre(n, x), assoc_laguerre(n, a, x)] for p in polys: integral = manualintegrate(p, x) for deg in [-2, -1, 0, 1, 3, 5, 8]: # some accept negative "degree", some do not try: p_subbed = p.subs(n, deg) except ValueError: continue assert (integral.subs(n, deg).diff(x) - p_subbed).expand() == 0 # can also integrate simple expressions with these polynomials q = x*p.subs(x, 2*x + 1) integral = manualintegrate(q, x) for deg in [2, 4, 7]: assert (integral.subs(n, deg).diff(x) - q.subs(n, deg)).expand() == 0 # cannot integrate with respect to any other parameter t = symbols('t') for i in range(len(p.args) - 1): new_args = list(p.args) new_args[i] = t assert isinstance(manualintegrate(p.func(*new_args), t), Integral)
def callback(q, v=0, u = 0, w=0, kind = 0): ans = '' if kind == 1: ans = str(sp.gamma(v)) elif kind == 2: ans = str(sp.gamma(u) * sp.gamma(v) / sp.gamma(u + v)) elif kind == 3: ans = str(functions.Legendre_Polynomials(v)) elif kind == 4: ans = str(sp.assoc_legendre(v, u, x)) elif kind == 5: ans = str(functions.bessel_function_1st(v)) elif kind == 6: ans = str(sp.jacobi(u, v, w, x)) elif kind == 7: ans = str(sp.jacobi_normalized(u, v, w, x)) elif kind == 8: ans = str(sp.gegenbauer(u, v, x)) elif kind == 9: # 1st kind ans = str(sp.chebyshevt(u, x)) elif kind == 10: ans = str(sp.chebyshevt_root(u, v)) elif kind == 11: # 2nd kind ans = str(sp.chebyshevu(u, x)) elif kind == 12: ans = str(sp.chebyshevu_root(u, v)) elif kind == 13: ans = str(sp.hermite(u, x)) elif kind == 14: ans = str(sp.laguerre(u, x)) elif kind == 15: ans = str(sp.assoc_laguerre(u, v, x)) q.put(ans)
def test_manualintegrate_orthogonal_poly(): n = symbols("n") a, b = 7, Rational(5, 3) polys = [ jacobi(n, a, b, x), gegenbauer(n, a, x), chebyshevt(n, x), chebyshevu(n, x), legendre(n, x), hermite(n, x), laguerre(n, x), assoc_laguerre(n, a, x), ] for p in polys: integral = manualintegrate(p, x) for deg in [-2, -1, 0, 1, 3, 5, 8]: # some accept negative "degree", some do not try: p_subbed = p.subs(n, deg) except ValueError: continue assert (integral.subs(n, deg).diff(x) - p_subbed).expand() == 0 # can also integrate simple expressions with these polynomials q = x * p.subs(x, 2 * x + 1) integral = manualintegrate(q, x) for deg in [2, 4, 7]: assert (integral.subs(n, deg).diff(x) - q.subs(n, deg)).expand() == 0 # cannot integrate with respect to any other parameter t = symbols("t") for i in range(len(p.args) - 1): new_args = list(p.args) new_args[i] = t assert isinstance(manualintegrate(p.func(*new_args), t), Integral)
def __init__(self, M, doInit=True, toCPP=False): self.name = "HarmonicOscillator" self.setMax(M) self.Hx = [] self.Hy = [] self.nShells = int(0.5*(sqrt(1 + 4*self.maxImplemented) - 1)) for i in range(self.nShells): self.Hx.append(hermite(i, k*x)) self.Hy.append(hermite(i, k*y)) self.expFactor = exp(-Rational(1,2)*k**2*(x**2 + y**2)) super(HOOrbitals, self).__init__(doInit, toCPP)
def eigfun(omega, n): ''' returns normalized eigenfunctions of the 1D harmonic oscillator ''' prefactor = 1.0 / math.sqrt(2 ** n * math.factorial(n)) prefactor *= (omega / math.pi) ** (1.0 / 4.0) gaussian = sympy.exp(-omega * x ** 2 / 2.0) poly = sympy.hermite(n, math.sqrt(omega) * x) return prefactor * gaussian * poly
def __init__(self, M): super(HOOrbitals3D, self).__init__(M, "HarmonicOscillator3D") self.Hx = [] self.Hy = [] self.Hz = [] nShells = 1 + (M > 2) + (M > 8) + (M > 20) + (M > 40) self.nShells = nShells print nShells for i in range(self.nShells): self.Hx.append(hermite(i, k * x)) self.Hy.append(hermite(i, k * y)) self.Hz.append(hermite(i, k * z)) self.expFactor = exp(-Rational(1, 2) * k**2 * (x**2 + y**2 + z**2))
def __init__(self, M): super(HOOrbitals3D, self).__init__(M, "HarmonicOscillator3D") self.Hx = [] self.Hy = [] self.Hz = [] nShells = 1 + (M > 2) + (M > 8) + (M > 20) + (M > 40) self.nShells = nShells print nShells for i in range(self.nShells): self.Hx.append(hermite(i, k*x)) self.Hy.append(hermite(i, k*y)) self.Hz.append(hermite(i, k*z)) self.expFactor = exp(-Rational(1,2)*k**2*(x**2 + y**2 + z**2))
def Herm_pol(n): p = sp.Symbol('p') Hn = sp.lambdify(p, sp.hermite(n, p)) return Hn
import sys from mpi4py import MPI from sympy import symbols, exp, hermite, cos import numpy as np from shenfun import inner, grad, TestFunction, TrialFunction, \ Array, Function, Basis, TensorProductSpace assert len(sys.argv) == 2, 'Call with one command-line argument' assert isinstance(int(sys.argv[-1]), int) comm = MPI.COMM_WORLD # Use sympy to compute a rhs, given an analytical solution x, y = symbols("x,y") #ue = sin(4*x)*exp(-x**2) ue = cos(4 * y) * hermite(4, x) * exp(-x**2 / 2) fe = ue.diff(x, 2) + ue.diff(y, 2) # Size of discretization N = int(sys.argv[-1]) SD = Basis(N, 'Hermite') K0 = Basis(N, 'Fourier', dtype='d') T = TensorProductSpace(comm, (SD, K0), axes=(0, 1)) X = T.local_mesh(True) u = TrialFunction(T) v = TestFunction(T) # Get f on quad points fj = Array(T, buffer=fe)
def test_hermite(): assert hermite(0, x) == 1 assert hermite(1, x) == 2*x assert hermite(2, x) == 4*x**2 - 2 assert hermite(3, x) == 8*x**3 - 12*x assert hermite(4, x) == 16*x**4 - 48*x**2 + 12 assert hermite(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120 n = Symbol("n") assert hermite(n, x) == hermite(n, x) assert hermite(n, -x) == (-1)**n*hermite(n, x) assert hermite(-n, x) == hermite(-n, x) assert conjugate(hermite(n, x)) == hermite(n, conjugate(x)) assert diff(hermite(n, x), x) == 2*n*hermite(n - 1, x) assert diff(hermite(n, x), n) == Derivative(hermite(n, x), n)
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == r'f{\left (x \right )}' assert latex(f) == r'f' g = Function('g') assert latex(g(x, y)) == r'g{\left (x,y \right )}' assert latex(g) == r'g' h = Function('h') assert latex(h(x, y, z)) == r'h{\left (x,y,z \right )}' assert latex(h) == r'h' Li = Function('Li') assert latex(Li) == r'\operatorname{Li}' assert latex(Li(x)) == r'\operatorname{Li}{\left (x \right )}' beta = Function('beta') # not to be confused with the beta function assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(beta) == r"\beta" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\left\lvert{x}\right\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma{\left(x \right)}" w = Wild('w') assert latex(gamma(w)) == r"\Gamma{\left(w \right)}" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x, 0)) == r"\mathcal{O}\left(x\right)" assert latex(Order(x, x, oo)) == r"\mathcal{O}\left(x; x\rightarrow\infty\right)" assert latex( Order(x, x, y) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow0\right)" assert latex( Order(x, x, y, 0) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow0\right)" assert latex( Order(x, x, y, oo) ) == r"\mathcal{O}\left(x; \begin{pmatrix}x, & y\end{pmatrix}\rightarrow\infty\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex(polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(elliptic_k(z)) == r"K\left(z\right)" assert latex(elliptic_k(z)**2) == r"K^{2}\left(z\right)" assert latex(elliptic_f(x, y)) == r"F\left(x\middle| y\right)" assert latex(elliptic_f(x, y)**2) == r"F^{2}\left(x\middle| y\right)" assert latex(elliptic_e(x, y)) == r"E\left(x\middle| y\right)" assert latex(elliptic_e(x, y)**2) == r"E^{2}\left(x\middle| y\right)" assert latex(elliptic_e(z)) == r"E\left(z\right)" assert latex(elliptic_e(z)**2) == r"E^{2}\left(z\right)" assert latex(elliptic_pi(x, y, z)) == r"\Pi\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y, z)**2) == \ r"\Pi^{2}\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y)) == r"\Pi\left(x\middle| y\right)" assert latex(elliptic_pi(x, y)**2) == r"\Pi^{2}\left(x\middle| y\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}' assert latex(Chi(x)) == r'\operatorname{Chi}{\left (x \right )}' assert latex(jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi( n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex(gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer( n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex(chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex(chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex(assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre( n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex(assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre( n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' theta = Symbol("theta", real=True) phi = Symbol("phi", real=True) assert latex(Ynm(n, m, theta, phi)) == r'Y_{n}^{m}\left(\theta,\phi\right)' assert latex( Ynm(n, m, theta, phi)**3) == r'\left(Y_{n}^{m}\left(\theta,\phi\right)\right)^{3}' assert latex(Znm(n, m, theta, phi)) == r'Z_{n}^{m}\left(\theta,\phi\right)' assert latex( Znm(n, m, theta, phi)**3) == r'\left(Z_{n}^{m}\left(\theta,\phi\right)\right)^{3}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift(0)** 3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}" assert latex(totient(n)) == r'\phi\left( n \right)' # some unknown function name should get rendered with \operatorname fjlkd = Function('fjlkd') assert latex(fjlkd(x)) == r'\operatorname{fjlkd}{\left (x \right )}' # even when it is referred to without an argument assert latex(fjlkd) == r'\operatorname{fjlkd}'
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == r'f{\left (x \right )}' assert latex(f) == r'f' g = Function('g') assert latex(g(x, y)) == r'g{\left (x,y \right )}' assert latex(g) == r'g' h = Function('h') assert latex(h(x, y, z)) == r'h{\left (x,y,z \right )}' assert latex(h) == r'h' Li = Function('Li') assert latex(Li) == r'\operatorname{Li}' assert latex(Li(x)) == r'\operatorname{Li}{\left (x \right )}' beta = Function('beta') # not to be confused with the beta function assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(beta) == r"\beta" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex( FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\left\lvert{x}\right\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex( polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(elliptic_k(z)) == r"K\left(z\right)" assert latex(elliptic_k(z)**2) == r"K^{2}\left(z\right)" assert latex(elliptic_f(x, y)) == r"F\left(x\middle| y\right)" assert latex(elliptic_f(x, y)**2) == r"F^{2}\left(x\middle| y\right)" assert latex(elliptic_e(x, y)) == r"E\left(x\middle| y\right)" assert latex(elliptic_e(x, y)**2) == r"E^{2}\left(x\middle| y\right)" assert latex(elliptic_e(z)) == r"E\left(z\right)" assert latex(elliptic_e(z)**2) == r"E^{2}\left(z\right)" assert latex(elliptic_pi(x, y, z)) == r"\Pi\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y, z)**2) == \ r"\Pi^{2}\left(x; y\middle| z\right)" assert latex(elliptic_pi(x, y)) == r"\Pi\left(x\middle| y\right)" assert latex(elliptic_pi(x, y)**2) == r"\Pi^{2}\left(x\middle| y\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}', latex(Chi(x)**2) assert latex( jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi(n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex( gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer(n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex( chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex( chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex( assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre(n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex( assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre(n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' theta = Symbol("theta", real=True) phi = Symbol("phi", real=True) assert latex(Ynm(n,m,theta,phi)) == r'Y_{n}^{m}\left(\theta,\phi\right)' assert latex(Ynm(n, m, theta, phi)**3) == r'\left(Y_{n}^{m}\left(\theta,\phi\right)\right)^{3}' assert latex(Znm(n,m,theta,phi)) == r'Z_{n}^{m}\left(\theta,\phi\right)' assert latex(Znm(n, m, theta, phi)**3) == r'\left(Z_{n}^{m}\left(\theta,\phi\right)\right)^{3}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift( 0)**3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}" assert latex(totient(n)) == r'\phi\left( n \right)' # some unknown function name should get rendered with \operatorname fjlkd = Function('fjlkd') assert latex(fjlkd(x)) == r'\operatorname{fjlkd}{\left (x \right )}' # even when it is referred to without an argument assert latex(fjlkd) == r'\operatorname{fjlkd}'
""" import os import sys from sympy import symbols, exp, hermite, lambdify import numpy as np from shenfun import inner, grad, TestFunction, TrialFunction, \ Array, Function, FunctionSpace assert len(sys.argv) == 2, 'Call with one command-line argument' assert isinstance(int(sys.argv[-1]), int) # Use sympy to compute a rhs, given an analytical solution x = symbols("x", real=True) #ue = sin(4*x)*exp(-x**2) ue = hermite(4, x) * exp(-x**2 / 2) fe = ue.diff(x, 2) # Size of discretization N = int(sys.argv[-1]) SD = FunctionSpace(N, 'Hermite') u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fe) # Compute right hand side of Poisson equation f_hat = Function(SD) f_hat = inner(v, -fj, output_array=f_hat)
def test_hermite(): assert hermite(0, x) == 1 assert hermite(1, x) == 2*x assert hermite(2, x) == 4*x**2 - 2 assert hermite(3, x) == 8*x**3 - 12*x assert hermite(4, x) == 16*x**4 - 48*x**2 + 12 assert hermite(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120 n = Symbol("n") assert unchanged(hermite, n, x) assert hermite(n, -x) == (-1)**n*hermite(n, x) assert unchanged(hermite, -n, x) assert hermite(n, 0) == 2**n*sqrt(pi)/gamma(S.Half - n/2) assert hermite(n, oo) is oo assert conjugate(hermite(n, x)) == hermite(n, conjugate(x)) _k = Dummy('k') assert hermite(n, x).rewrite("polynomial").dummy_eq(factorial(n)*Sum((-1) **_k*(2*x)**(-2*_k + n)/(factorial(_k)*factorial(-2*_k + n)), (_k, 0, floor(n/2)))) assert diff(hermite(n, x), x) == 2*n*hermite(n - 1, x) assert diff(hermite(n, x), n) == Derivative(hermite(n, x), n) raises(ArgumentIndexError, lambda: hermite(n, x).fdiff(3))
from sympy import hermite, Poly from sympy.abc import x f = open("coefs.txt", "a") maxN = 100 for n in range(maxN + 1): if (n == 0): f.write("[") listAsString = ', '.join(map(str, Poly(hermite(n, x), x).coeffs()[::-1])) f.write("[" + listAsString + "]") if (n != maxN): f.write(",\n") else: f.write("]") f.close()
def sympy_basis(self, i=0, x=sp.symbols('x')): return sp.hermite(i, x) * sp.exp(-x**2 / 2) * self.factor(i)
def test_hermite(): assert hermite(0, x) == 1 assert hermite(1, x) == 2 * x assert hermite(2, x) == 4 * x**2 - 2 assert hermite(3, x) == 8 * x**3 - 12 * x assert hermite(4, x) == 16 * x**4 - 48 * x**2 + 12 assert hermite(6, x) == 64 * x**6 - 480 * x**4 + 720 * x**2 - 120 n = Symbol("n") assert hermite(n, x) == hermite(n, x) assert hermite(n, -x) == (-1)**n * hermite(n, x) assert hermite(-n, x) == hermite(-n, x) assert conjugate(hermite(n, x)) == hermite(n, conjugate(x)) assert diff(hermite(n, x), x) == 2 * n * hermite(n - 1, x) assert diff(hermite(n, x), n) == Derivative(hermite(n, x), n)
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function("f") assert latex(f(x)) == "\\operatorname{f}{\\left (x \\right )}" beta = Function("beta") assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2 * x ** 2), fold_func_brackets=True) == r"\sin {2 x^{2}}" assert latex(sin(x ** 2), fold_func_brackets=True) == r"\sin {x^{2}}" assert latex(asin(x) ** 2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x) ** 2, inv_trig_style="full") == r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x) ** 2, inv_trig_style="power") == r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x ** 2), inv_trig_style="power", fold_func_brackets=True) == r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x ** 3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y) ** 2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x ** 3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y) ** 2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\lvert{x}\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r"\gamma\left(x, y\right)" assert latex(uppergamma(x, y)) == r"\Gamma\left(x, y\right)" assert latex(cot(x)) == r"\cot{\left (x \right )}" assert latex(coth(x)) == r"\coth{\left (x \right )}" assert latex(re(x)) == r"\Re{x}" assert latex(im(x)) == r"\Im{x}" assert latex(root(x, y)) == r"x^{\frac{1}{y}}" assert latex(arg(x)) == r"\arg{\left (x \right )}" assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x) ** 2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y) ** 2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x) ** 2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex(polylog(x, y) ** 2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n) ** 2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(Ei(x)) == r"\operatorname{Ei}{\left (x \right )}" assert latex(Ei(x) ** 2) == r"\operatorname{Ei}^{2}{\left (x \right )}" assert latex(expint(x, y) ** 2) == r"\operatorname{E}_{x}^{2}\left(y\right)" assert latex(Shi(x) ** 2) == r"\operatorname{Shi}^{2}{\left (x \right )}" assert latex(Si(x) ** 2) == r"\operatorname{Si}^{2}{\left (x \right )}" assert latex(Ci(x) ** 2) == r"\operatorname{Ci}^{2}{\left (x \right )}" assert latex(Chi(x) ** 2) == r"\operatorname{Chi}^{2}{\left (x \right )}" assert latex(jacobi(n, a, b, x)) == r"P_{n}^{\left(a,b\right)}\left(x\right)" assert latex(jacobi(n, a, b, x) ** 2) == r"\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}" assert latex(gegenbauer(n, a, x)) == r"C_{n}^{\left(a\right)}\left(x\right)" assert latex(gegenbauer(n, a, x) ** 2) == r"\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}" assert latex(chebyshevt(n, x)) == r"T_{n}\left(x\right)" assert latex(chebyshevt(n, x) ** 2) == r"\left(T_{n}\left(x\right)\right)^{2}" assert latex(chebyshevu(n, x)) == r"U_{n}\left(x\right)" assert latex(chebyshevu(n, x) ** 2) == r"\left(U_{n}\left(x\right)\right)^{2}" assert latex(legendre(n, x)) == r"P_{n}\left(x\right)" assert latex(legendre(n, x) ** 2) == r"\left(P_{n}\left(x\right)\right)^{2}" assert latex(assoc_legendre(n, a, x)) == r"P_{n}^{\left(a\right)}\left(x\right)" assert latex(assoc_legendre(n, a, x) ** 2) == r"\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}" assert latex(laguerre(n, x)) == r"L_{n}\left(x\right)" assert latex(laguerre(n, x) ** 2) == r"\left(L_{n}\left(x\right)\right)^{2}" assert latex(assoc_laguerre(n, a, x)) == r"L_{n}^{\left(a\right)}\left(x\right)" assert latex(assoc_laguerre(n, a, x) ** 2) == r"\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}" assert latex(hermite(n, x)) == r"H_{n}\left(x\right)" assert latex(hermite(n, x) ** 2) == r"\left(H_{n}\left(x\right)\right)^{2}" # Test latex printing of function names with "_" assert latex(polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift(0) ** 3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}"
def test_hermite(): assert hermite(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == '\\operatorname{f}{\\left (x \\right )}' beta = Function('beta') assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\lvert{x}\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex(polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}' assert latex(jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi( n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex(gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer( n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex(chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex(chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex(assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre( n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex(assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre( n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift(0)** 3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}"
def test_hermite(): assert hermite(6, x) == 64 * x**6 - 480 * x**4 + 720 * x**2 - 120