def test_sympy_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3*I, 'exp(x)': exp(x), 'x!': factorial(x), 'x!!': factorial2(x), '(x + 1)! - 1': factorial(x + 1) - 1, '3.[3]': Rational(10, 3), '.0[3]': Rational(1, 30), '3.2[3]': Rational(97, 30), '1.3[12]': Rational(433, 330), '1 + 3.[3]': Rational(13, 3), '1 + .0[3]': Rational(31, 30), '1 + 3.2[3]': Rational(127, 30), '.[0011]': Rational(1, 909), '0.1[00102] + 1': Rational(366697, 333330), '1.[0191]': Rational(10190, 9999), '10!': 3628800, '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, "S('S(3).n(n=3)')": 3.00, 'factorint(12, visual=True)': Mul( Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir="-")': Limit(sin(x), x, 0, dir='-'), } for text, result in inputs.items(): assert parse_expr(text) == result
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]" assert mcode(conjugate(x)) == "Conjugate[x]" assert mcode(Max(x, y, z) * Min(y, z)) == "Max[x, y, z]*Min[y, z]" assert mcode(fresnelc(x)) == "FresnelC[x]" assert mcode(fresnels(x)) == "FresnelS[x]" assert mcode(gamma(x)) == "Gamma[x]" assert mcode(uppergamma(x, y)) == "Gamma[x, y]" assert mcode(polygamma(x, y)) == "PolyGamma[x, y]" assert mcode(loggamma(x)) == "LogGamma[x]" assert mcode(erf(x)) == "Erf[x]" assert mcode(erfc(x)) == "Erfc[x]" assert mcode(erfi(x)) == "Erfi[x]" assert mcode(erf2(x, y)) == "Erf[x, y]" assert mcode(expint(x, y)) == "ExpIntegralE[x, y]" assert mcode(erfcinv(x)) == "InverseErfc[x]" assert mcode(erfinv(x)) == "InverseErf[x]" assert mcode(erf2inv(x, y)) == "InverseErf[x, y]" assert mcode(Ei(x)) == "ExpIntegralEi[x]" assert mcode(Ci(x)) == "CosIntegral[x]" assert mcode(li(x)) == "LogIntegral[x]" assert mcode(Si(x)) == "SinIntegral[x]" assert mcode(Shi(x)) == "SinhIntegral[x]" assert mcode(Chi(x)) == "CoshIntegral[x]" assert mcode(beta(x, y)) == "Beta[x, y]" assert mcode(factorial(x)) == "Factorial[x]" assert mcode(factorial2(x)) == "Factorial2[x]" assert mcode(subfactorial(x)) == "Subfactorial[x]" assert mcode(FallingFactorial(x, y)) == "FactorialPower[x, y]" assert mcode(RisingFactorial(x, y)) == "Pochhammer[x, y]" assert mcode(catalan(x)) == "CatalanNumber[x]" assert mcode(harmonic(x)) == "HarmonicNumber[x]" assert mcode(harmonic(x, y)) == "HarmonicNumber[x, y]"
def R_nl(n, l, nu, r): """ Returns the radial wavefunction R_{nl} for a 3d isotropic harmonic oscillator. Parameters ========== ``n`` : The "nodal" quantum number. Corresponds to the number of nodes in the wavefunction. ``n >= 0`` ``l`` : The quantum number for orbital angular momentum. ``nu`` : mass-scaled frequency: nu = m*omega/(2*hbar) where `m` is the mass and `omega` the frequency of the oscillator. (in atomic units ``nu == omega/2``) ``r`` : Radial coordinate. Examples ======== >>> from sympy.physics.sho import R_nl >>> from sympy.abc import r, nu, l >>> R_nl(0, 0, 1, r) 2*2**(3/4)*exp(-r**2)/pi**(1/4) >>> R_nl(1, 0, 1, r) 4*2**(1/4)*sqrt(3)*(3/2 - 2*r**2)*exp(-r**2)/(3*pi**(1/4)) l, nu and r may be symbolic: >>> R_nl(0, 0, nu, r) 2*2**(3/4)*sqrt(nu**(3/2))*exp(-nu*r**2)/pi**(1/4) >>> R_nl(0, l, 1, r) r**l*sqrt(2**(l + 3/2)*2**(l + 2)/factorial2(2*l + 1))*exp(-r**2)/pi**(1/4) The normalization of the radial wavefunction is: >>> from sympy import Integral, oo >>> Integral(R_nl(0, 0, 1, r)**2*r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 0, 1, r)**2*r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 1, 1, r)**2*r**2, (r, 0, oo)).n() 1.00000000000000 """ n, l, nu, r = map(S, [n, l, nu, r]) # formula uses n >= 1 (instead of nodal n >= 0) n = n + 1 C = sqrt( ((2*nu)**(l + Rational(3, 2))*2**(n + l + 1)*factorial(n - 1))/ (sqrt(pi)*(factorial2(2*n + 2*l - 1))) ) return C*r**(l)*exp(-nu*r**2)*assoc_laguerre(n - 1, l + S.Half, 2*nu*r**2)
def R_nl(n, l, nu, r): """ Returns the radial wavefunction R_{nl} for a 3d isotropic harmonic oscillator. ``n`` the "nodal" quantum number. Corresponds to the number of nodes in the wavefunction. n >= 0 ``l`` the quantum number for orbital angular momentum ``nu`` mass-scaled frequency: nu = m*omega/(2*hbar) where `m` is the mass and `omega` the frequency of the oscillator. (in atomic units nu == omega/2) ``r`` Radial coordinate Examples ======== >>> from sympy.physics.sho import R_nl >>> from sympy import var >>> var("r nu l") (r, nu, l) >>> R_nl(0, 0, 1, r) 2*2**(3/4)*exp(-r**2)/pi**(1/4) >>> R_nl(1, 0, 1, r) 4*2**(1/4)*sqrt(3)*(-2*r**2 + 3/2)*exp(-r**2)/(3*pi**(1/4)) l, nu and r may be symbolic: >>> R_nl(0, 0, nu, r) 2*2**(3/4)*sqrt(nu**(3/2))*exp(-nu*r**2)/pi**(1/4) >>> R_nl(0, l, 1, r) r**l*sqrt(2**(l + 3/2)*2**(l + 2)/factorial2(2*l + 1))*exp(-r**2)/pi**(1/4) The normalization of the radial wavefunction is: >>> from sympy import Integral, oo >>> Integral(R_nl(0, 0, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 0, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 1, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 """ n, l, nu, r = map(S, [n, l, nu, r]) # formula uses n >= 1 (instead of nodal n >= 0) n = n + 1 C = sqrt( ((2 * nu) ** (l + Rational(3, 2)) * 2 ** (n + l + 1) * factorial(n - 1)) / (sqrt(pi) * (factorial2(2 * n + 2 * l - 1))) ) return C * r ** (l) * exp(-nu * r ** 2) * assoc_laguerre(n - 1, l + S(1) / 2, 2 * nu * r ** 2)
def R_nl(n, l, nu, r): """ Returns the radial wavefunction R_{nl} for a 3d isotropic harmonic oscillator. ``n`` the "nodal" quantum number. Corresponds to the number of nodes in the wavefunction. n >= 0 ``l`` the quantum number for orbital angular momentum ``nu`` mass-scaled frequency: nu = m*omega/(2*hbar) where `m' is the mass and `omega' the frequency of the oscillator. (in atomic units nu == omega/2) ``r`` Radial coordinate :Examples: >>> from sympy.physics.sho import R_nl >>> from sympy import var >>> var("r nu l") (r, nu, l) >>> R_nl(0, 0, 1, r) 2*2**(3/4)*exp(-r**2)/pi**(1/4) >>> R_nl(1, 0, 1, r) 4*2**(1/4)*3**(1/2)*(3/2 - 2*r**2)*exp(-r**2)/(3*pi**(1/4)) l, nu and r may be symbolic: >>> R_nl(0, 0, nu, r) 2*2**(3/4)*(nu**(3/2))**(1/2)*exp(-nu*r**2)/pi**(1/4) >>> R_nl(0, l, 1, r) r**l*(2**(2 + l)*2**(3/2 + l)/(1 + 2*l)!!)**(1/2)*exp(-r**2)/pi**(1/4) The normalization of the radial wavefunction is:: >>> from sympy import Integral, oo >>> Integral(R_nl(0, 0, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 0, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 >>> Integral(R_nl(1, 1, 1, r)**2 * r**2, (r, 0, oo)).n() 1.00000000000000 """ n, l, nu, r = map(S, [n, l, nu, r]) # formula uses n >= 1 (instead of nodal n >= 0) n = n + 1 C = sqrt( ((2 * nu)**(l + Rational(3, 2)) * 2**(n + l + 1) * factorial(n - 1)) / (sqrt(pi) * (factorial2(2 * n + 2 * l - 1)))) return C * r**(l) * exp(-nu * r**2) * laguerre_l(n - 1, l + S(1) / 2, 2 * nu * r**2)
def test_sympy_parser(): x = Symbol("x") inputs = { "2*x": 2 * x, "3.00": Float(3), "22/7": Rational(22, 7), "2+3j": 2 + 3 * I, "exp(x)": exp(x), "x!": factorial(x), "x!!": factorial2(x), "(x + 1)! - 1": factorial(x + 1) - 1, "3.[3]": Rational(10, 3), ".0[3]": Rational(1, 30), "3.2[3]": Rational(97, 30), "1.3[12]": Rational(433, 330), "1 + 3.[3]": Rational(13, 3), "1 + .0[3]": Rational(31, 30), "1 + 3.2[3]": Rational(127, 30), ".[0011]": Rational(1, 909), "0.1[00102] + 1": Rational(366697, 333330), "1.[0191]": Rational(10190, 9999), "10!": 3628800, "-(2)": -Integer(2), "[-1, -2, 3]": [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, "S('S(3).n(n=3)')": 3.00, "factorint(12, visual=True)": Mul(Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir="-")': Limit(sin(x), x, 0, dir="-"), } for text, result in inputs.items(): assert parse_expr(text) == result raises(TypeError, lambda: parse_expr("x", standard_transformations)) raises(TypeError, lambda: parse_expr("x", transformations=lambda x, y: 1)) raises(TypeError, lambda: parse_expr("x", transformations=(lambda x, y: 1, ))) raises(TypeError, lambda: parse_expr("x", transformations=((), ))) raises(TypeError, lambda: parse_expr("x", {}, [], [])) raises(TypeError, lambda: parse_expr("x", [], [], {})) raises(TypeError, lambda: parse_expr("x", [], [], {}))