def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1) assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n)*gegenbauer(n, a + S(1)/2, x)/RisingFactorial(2*a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x)* factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x)* gamma(-b + n + 1)/gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(S(3)/2, n)*chebyshevu(n, x)/factorial(n + 1) assert jacobi(n, -S.Half, -S.Half, x) == RisingFactorial(S(1)/2, n)*chebyshevt(n, x)/factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper((-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m) assert conjugate(jacobi(m, a, b, x)) == jacobi(m, conjugate(a), conjugate(b), conjugate(x)) assert diff(jacobi(n,a,b,x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n,a,b,x), x) == (a/2 + b/2 + n/2 + S(1)/2)*jacobi(n - 1, a + 1, b + 1, x)
def test_assoc_legendre(): Plm = assoc_legendre Q = sqrt(1 - x**2) assert Plm(0, 0, x) == 1 assert Plm(1, 0, x) == x assert Plm(1, 1, x) == -Q assert Plm(2, 0, x) == (3 * x**2 - 1) / 2 assert Plm(2, 1, x) == -3 * x * Q assert Plm(2, 2, x) == 3 * Q**2 assert Plm(3, 0, x) == (5 * x**3 - 3 * x) / 2 assert Plm(3, 1, x).expand() == ((3 * (1 - 5 * x**2) / 2).expand() * Q).expand() assert Plm(3, 2, x) == 15 * x * Q**2 assert Plm(3, 3, x) == -15 * Q**3 # negative m assert Plm(1, -1, x) == -Plm(1, 1, x) / 2 assert Plm(2, -2, x) == Plm(2, 2, x) / 24 assert Plm(2, -1, x) == -Plm(2, 1, x) / 6 assert Plm(3, -3, x) == -Plm(3, 3, x) / 720 assert Plm(3, -2, x) == Plm(3, 2, x) / 120 assert Plm(3, -1, x) == -Plm(3, 1, x) / 12 n = Symbol("n") m = Symbol("m") X = Plm(n, m, x) assert isinstance(X, assoc_legendre) assert Plm(n, 0, x) == legendre(n, x) assert conjugate(assoc_legendre(n, m, x)) == \ assoc_legendre(n, conjugate(m), conjugate(x)) raises(ValueError, lambda: Plm(0, 1, x))
def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1) assert jacobi(n, a, a, x) == RisingFactorial( a + 1, n)*gegenbauer(n, a + S.Half, x)/RisingFactorial(2*a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x)* factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x)* gamma(-b + n + 1)/gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial( Rational(3, 2), n)*chebyshevu(n, x)/factorial(n + 1) assert jacobi(n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial( S.Half, n)*chebyshevt(n, x)/factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper( (-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m) assert unchanged(jacobi, n, a, b, oo) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) _k = Dummy('k') assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), a).dummy_eq(Sum((jacobi(n, a, b, x) + (2*_k + a + b + 1)*RisingFactorial(_k + b + 1, -_k + n)*jacobi(_k, a, b, x)/((-_k + n)*RisingFactorial(_k + a + b + 1, -_k + n)))/(_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), b).dummy_eq(Sum(((-1)**(-_k + n)*(2*_k + a + b + 1)*RisingFactorial(_k + a + 1, -_k + n)*jacobi(_k, a, b, x)/ ((-_k + n)*RisingFactorial(_k + a + b + 1, -_k + n)) + jacobi(n, a, b, x))/(_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), x) == \ (a/2 + b/2 + n/2 + S.Half)*jacobi(n - 1, a + 1, b + 1, x) assert jacobi_normalized(n, a, b, x) == \ (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1) /((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))) raises(ValueError, lambda: jacobi(-2.1, a, b, x)) raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo)) assert jacobi(n, a, b, x).rewrite("polynomial").dummy_eq(Sum((S.Half - x/2) **_k*RisingFactorial(-n, _k)*RisingFactorial(_k + a + 1, -_k + n)* RisingFactorial(a + b + n + 1, _k)/factorial(_k), (_k, 0, n))/factorial(n)) raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def test_assoc_legendre(): Plm = assoc_legendre Q = sqrt(1 - x**2) assert Plm(0, 0, x) == 1 assert Plm(1, 0, x) == x assert Plm(1, 1, x) == -Q assert Plm(2, 0, x) == (3 * x**2 - 1) / 2 assert Plm(2, 1, x) == -3 * x * Q assert Plm(2, 2, x) == 3 * Q**2 assert Plm(3, 0, x) == (5 * x**3 - 3 * x) / 2 assert Plm(3, 1, x).expand() == ((3 * (1 - 5 * x**2) / 2).expand() * Q).expand() assert Plm(3, 2, x) == 15 * x * Q**2 assert Plm(3, 3, x) == -15 * Q**3 # negative m assert Plm(1, -1, x) == -Plm(1, 1, x) / 2 assert Plm(2, -2, x) == Plm(2, 2, x) / 24 assert Plm(2, -1, x) == -Plm(2, 1, x) / 6 assert Plm(3, -3, x) == -Plm(3, 3, x) / 720 assert Plm(3, -2, x) == Plm(3, 2, x) / 120 assert Plm(3, -1, x) == -Plm(3, 1, x) / 12 n = Symbol("n") m = Symbol("m") X = Plm(n, m, x) assert isinstance(X, assoc_legendre) assert Plm(n, 0, x) == legendre(n, x) assert Plm(n, m, 0) == 2**m * sqrt(pi) / (gamma(-m / 2 - n / 2 + S.Half) * gamma(-m / 2 + n / 2 + 1)) assert diff(Plm(m, n, x), x) == (m * x * assoc_legendre(m, n, x) - (m + n) * assoc_legendre(m - 1, n, x)) / (x**2 - 1) _k = Dummy("k") assert (Plm(m, n, x).rewrite("polynomial").dummy_eq( (1 - x**2)**(n / 2) * Sum( (-1)**_k * 2**(-m) * x**(-2 * _k + m - n) * factorial(-2 * _k + 2 * m) / (factorial(_k) * factorial(-_k + m) * factorial(-2 * _k + m - n)), (_k, 0, floor(m / 2 - n / 2)), ))) assert conjugate(assoc_legendre(n, m, x)) == assoc_legendre( n, conjugate(m), conjugate(x)) raises(ValueError, lambda: Plm(0, 1, x)) raises(ValueError, lambda: Plm(-1, 1, x)) raises(ArgumentIndexError, lambda: Plm(n, m, x).fdiff(1)) raises(ArgumentIndexError, lambda: Plm(n, m, x).fdiff(2)) raises(ArgumentIndexError, lambda: Plm(n, m, x).fdiff(4))
def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a / 2 - b / 2 + x * (a / 2 + b / 2 + 1) assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n) * gegenbauer( n, a + S(1) / 2, x) / RisingFactorial(2 * a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a * (-x + 1)**(-a / 2) * (x + 1)**(a / 2) * assoc_legendre(n, a, x) * factorial(-a + n) * gamma(a + n + 1) / (factorial(a + n) * gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b / 2) * (x + 1)**(-b / 2) * assoc_legendre(n, b, x) * gamma(-b + n + 1) / gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(S(3) / 2, n) * chebyshevu( n, x) / factorial(n + 1) assert jacobi( n, -S.Half, -S.Half, x) == RisingFactorial(S(1) / 2, n) * chebyshevt(n, x) / factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n * jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n) * gamma(a + n + 1) * hyper( (-b - n, -n), (a + 1, ), -1) / (factorial(n) * gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n) / factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo * RisingFactorial(a + b + m + 1, m) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), x) == \ (a/2 + b/2 + n/2 + S(1)/2)*jacobi(n - 1, a + 1, b + 1, x) assert jacobi_normalized(n, a, b, x) == \ (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1) /((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))) raises(ValueError, lambda: jacobi(-2.1, a, b, x)) raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo))
def semiNormalizedSH_genericNumpy(e, a, target=None) : """ Returns the value of the sh components at the specified orientation. Version based on the generic formula, using sympy just for legendre. """ x,y,z = ead2xyz(e, a, 1) sn3d = np.zeros((order+1,order+1)) if target is None else target from sympy import factorial, sqrt, S import sympy as sp ra = math.radians(a) for l in xrange(order+1) : for m in xrange(-l,l+1) : absm = abs(m) factor = math.sqrt(2) if m else 1. sn3d[shi(l,m)] = (factor / math.sqrt( # sp.factorial(l+absm) / sp.factorial(l-absm) # clearest for the next one np.prod(xrange(l-absm+1, l+absm+1)) ) * ( sp.assoc_legendre(l,absm,z) * S(-1)**m ).evalf() * ( np.cos(m*ra) if m>=0 else np.sin(absm*ra) ) ) return sn3d
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 __init__(self, ell=2, m=0): self.ell = ell self.m = m x = sym.Symbol('x') y = sym.Symbol('y') z = sym.Symbol('z') r = sym.Symbol('r', real=True, positive=True) theta = sym.Symbol('theta', real=True, positive=True) phi = sym.Symbol('phi', real=True, positive=True) def c(ell, m): if (m >= 0): return (factorial(ell - 1) * (-2)**Abs(m)) / factorial(ell + Abs(m)) * cos(m * phi) else: return (factorial(ell - 1) * (-2)**Abs(m)) / factorial(ell + Abs(m)) * sin( Abs(m) * phi) legendre_ell = ell + 1 Sigma = c(legendre_ell, m) * r**legendre_ell * assoc_legendre( legendre_ell, Abs(m), cos(theta)) Sigma = sym.simplify(Sigma) Sigma = Sigma.subs({Abs(sin(theta)): sin(theta)}) Sigma = Sigma.expand(trig=True) self.Sigma_spherical = Sigma Sigma = Sigma.subs({ r * cos(theta): z, r * sin(theta) * sin(phi): y, r * sin(theta) * cos(phi): x }) Sigma = Sigma.subs({r**2 * sin(theta)**2: x**2 + y**2}) Sigma = Sigma.subs({r**2: x**2 + y**2 + z**2}) Sigma = sym.simplify(Sigma.expand()) self.Sigma = Sigma Pix = Derivative(Sigma, x) Piy = Derivative(Sigma, y) Piz = Derivative(Sigma, z) Pix = Pix.doit() Piy = Piy.doit() Piz = Piz.doit() Pix = sym.simplify(Pix.expand()) Piy = sym.simplify(Piy.expand()) Piz = sym.simplify(Piz.expand()) self.Pix = Pix self.Piy = Piy self.Piz = Piz self.fPix = lambdify([x, y, z], Pix) self.fPiy = lambdify([x, y, z], Piy) self.fPiz = lambdify([x, y, z], Piz)
def assoc_legendre_tf(n,m,x_tf): x_np = x_tf.numpy() x = symbols('x') func = assoc_legendre(n,m,x) value = lambdify(x,func,"numpy") result = value(x_np) # tensor = tf.convert_to_tensor(value(x_np)) return result
def _get_Ylm(self, l, m): """ Compute an expression for spherical harmonic of order (l,m) in terms of Cartesian unit vectors, :math:`\hat{z}` and :math:`\hat{x} + i \hat{y}` Parameters ---------- l : int the degree of the harmonic m : int the order of the harmonic; |m| < l Returns ------- expr : a sympy expression that corresponds to the requested Ylm References ---------- https://en.wikipedia.org/wiki/Spherical_harmonics """ import sympy as sp # the relevant cartesian and spherical symbols x, y, z, r = sp.symbols('x y z r', real=True, positive=True) xhat, yhat, zhat = sp.symbols('xhat yhat zhat', real=True, positive=True) xpyhat = sp.Symbol('xpyhat', complex=True) phi, theta = sp.symbols('phi theta') defs = [(sp.sin(phi), y / sp.sqrt(x**2 + y**2)), (sp.cos(phi), x / sp.sqrt(x**2 + y**2)), (sp.cos(theta), z / sp.sqrt(x**2 + y**2 + z**2))] # the cos(theta) dependence encoded by the associated Legendre poly expr = sp.assoc_legendre(l, m, sp.cos(theta)) # the exp(i*m*phi) dependence expr *= sp.expand_trig(sp.cos( m * phi)) + sp.I * sp.expand_trig(sp.sin(m * phi)) # simplifying optimizations expr = sp.together(expr.subs(defs)).subs(x**2 + y**2 + z**2, r**2) expr = expr.expand().subs([(x / r, xhat), (y / r, yhat), (z / r, zhat)]) expr = expr.factor().factor(extension=[sp.I]).subs( xhat + sp.I * yhat, xpyhat) expr = expr.subs(xhat**2 + yhat**2, 1 - zhat**2).factor() # and finally add the normalization amp = sp.sqrt((2 * l + 1) / (4 * numpy.pi) * sp.factorial(l - m) / sp.factorial(l + m)) expr *= amp return expr
def get_norm_p(n, m, t): pnm = sympy.assoc_legendre(n, m, t) if m == 0: j = 1 if m != 0: j = 2 p1 = j * (2.0 * n + 1.0) p2 = float(mt.factorial(n-m)) p3 = float(mt.factorial(n+m)) p4 = p2/p3 return (np.sqrt(p1 * p4)) * pnm
def _get_Ylm(self, l, m): """ Compute an expression for spherical harmonic of order (l,m) in terms of Cartesian unit vectors, :math:`\hat{z}` and :math:`\hat{x} + i \hat{y}` Parameters ---------- l : int the degree of the harmonic m : int the order of the harmonic; |m| < l Returns ------- expr : a sympy expression that corresponds to the requested Ylm References ---------- https://en.wikipedia.org/wiki/Spherical_harmonics """ import sympy as sp # the relevant cartesian and spherical symbols x, y, z, r = sp.symbols('x y z r', real=True, positive=True) xhat, yhat, zhat = sp.symbols('xhat yhat zhat', real=True, positive=True) xpyhat = sp.Symbol('xpyhat', complex=True) phi, theta = sp.symbols('phi theta') defs = [(sp.sin(phi), y/sp.sqrt(x**2+y**2)), (sp.cos(phi), x/sp.sqrt(x**2+y**2)), (sp.cos(theta), z/sp.sqrt(x**2 + y**2 + z**2)) ] # the cos(theta) dependence encoded by the associated Legendre poly expr = sp.assoc_legendre(l, m, sp.cos(theta)) # the exp(i*m*phi) dependence expr *= sp.expand_trig(sp.cos(m*phi)) + sp.I*sp.expand_trig(sp.sin(m*phi)) # simplifying optimizations expr = sp.together(expr.subs(defs)).subs(x**2 + y**2 + z**2, r**2) expr = expr.expand().subs([(x/r, xhat), (y/r, yhat), (z/r, zhat)]) expr = expr.factor().factor(extension=[sp.I]).subs(xhat+sp.I*yhat, xpyhat) expr = expr.subs(xhat**2 + yhat**2, 1-zhat**2).factor() # and finally add the normalization amp = sp.sqrt((2*l+1) / (4*numpy.pi) * sp.factorial(l-m) / sp.factorial(l+m)) expr *= amp return expr
def test_assoc_legendre(): Plm = assoc_legendre Q = sqrt(1 - x**2) assert Plm(0, 0, x) == 1 assert Plm(1, 0, x) == x assert Plm(1, 1, x) == -Q assert Plm(2, 0, x) == (3*x**2 - 1)/2 assert Plm(2, 1, x) == -3*x*Q assert Plm(2, 2, x) == 3*Q**2 assert Plm(3, 0, x) == (5*x**3 - 3*x)/2 assert Plm(3, 1, x).expand() == (( 3*(1 - 5*x**2)/2 ).expand() * Q).expand() assert Plm(3, 2, x) == 15*x * Q**2 assert Plm(3, 3, x) == -15 * Q**3 # negative m assert Plm(1, -1, x) == -Plm(1, 1, x)/2 assert Plm(2, -2, x) == Plm(2, 2, x)/24 assert Plm(2, -1, x) == -Plm(2, 1, x)/6 assert Plm(3, -3, x) == -Plm(3, 3, x)/720 assert Plm(3, -2, x) == Plm(3, 2, x)/120 assert Plm(3, -1, x) == -Plm(3, 1, x)/12 n = Symbol("n") m = Symbol("m") X = Plm(n, m, x) assert isinstance(X, assoc_legendre) assert Plm(n, 0, x) == legendre(n, x) raises(ValueError, lambda: Plm(-1, 0, x)) raises(ValueError, lambda: Plm(0, 1, x)) assert conjugate(assoc_legendre(n, m, x)) == \ assoc_legendre(n, conjugate(m), conjugate(x))
def getSphericalFunc(self, l, m): if m < 0: m = abs(m) fac = sin(m*theta) else: fac = cos(m*theta) P = assoc_legendre(l, m, cos(phi)) res = fac*P res = self.sphere2Cart(trigsimp(res)) #Takes care of Abs when the argument is real.. res = res.subs(r2d, r_2d).subs(r3d, r).subs(r, r3d).subs(r_2d, r2d) return res;
def getSphericalFunc(self, l, m): if m < 0: m = abs(m) fac = sin(m*theta) else: fac = cos(m*theta) P = assoc_legendre(l, m, cos(phi)) res = fac*P res = self.sphere2Cart(trigsimp(res)) #Line which takes care of the stupid Abs when the argument is obviously real.. res = res.subs(r2d, r_2d).subs(r3d, r).subs(r, r3d).subs(r_2d, r2d) return res;
def sphericalHarmonic(l,n) : """Returns an evaluator for the seminormalized real spherical harmonic of order l, degree n as defined in: http://ambisonics.iem.at/xchange/format/ambisonics-xchange-format-appendix """ import sympy sign = -1 if n<0 else +1 n = abs(n) x,y=sympy.var("x y") f= sympy.powsimp(sympy.trigsimp( sympy.assoc_legendre(l,n,sympy.sin(x)) * (-1)**n * sympy.sqrt((2*l+1) * (1 if n==0 else 2) * sympy.factorial(l-n)/sympy.factorial(l+n)) * (sympy.cos(n*y) if sign>=0 else sympy.sin(n*y)) )) return SympyEvaluator(f,x,y)
def get_p(n, m, t): return sympy.assoc_legendre(n, m, t)
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}'
def get_real_Ylm(l, m): """ Return a function that computes the real spherical harmonic of order (l,m) Parameters ---------- l : int the degree of the harmonic m : int the order of the harmonic; abs(m) <= l Returns ------- Ylm : callable a function that takes 4 arguments: (xhat, yhat, zhat) unit-normalized Cartesian coordinates and returns the specified Ylm References ---------- https://en.wikipedia.org/wiki/Spherical_harmonics#Real_form """ import sympy as sp # make sure l,m are integers l = int(l) m = int(m) # the relevant cartesian and spherical symbols x, y, z, r = sp.symbols('x y z r', real=True, positive=True) xhat, yhat, zhat = sp.symbols('xhat yhat zhat', real=True, positive=True) phi, theta = sp.symbols('phi theta') defs = [(sp.sin(phi), y / sp.sqrt(x**2 + y**2)), (sp.cos(phi), x / sp.sqrt(x**2 + y**2)), (sp.cos(theta), z / sp.sqrt(x**2 + y**2 + z**2))] # the normalization factors if m == 0: amp = sp.sqrt((2 * l + 1) / (4 * numpy.pi)) else: amp = sp.sqrt(2 * (2 * l + 1) / (4 * numpy.pi) * sp.factorial(l - abs(m)) / sp.factorial(l + abs(m))) # the cos(theta) dependence encoded by the associated Legendre poly expr = (-1)**m * sp.assoc_legendre(l, abs(m), sp.cos(theta)) # the phi dependence if m < 0: expr *= sp.expand_trig(sp.sin(abs(m) * phi)) elif m > 0: expr *= sp.expand_trig(sp.cos(m * phi)) # simplify expr = sp.together(expr.subs(defs)).subs(x**2 + y**2 + z**2, r**2) expr = amp * expr.expand().subs([(x / r, xhat), (y / r, yhat), (z / r, zhat)]) Ylm = sp.lambdify((xhat, yhat, zhat), expr, 'numexpr') # attach some meta-data Ylm.expr = expr Ylm.l = l Ylm.m = m return Ylm
def test_J11(): assert simplify(assoc_legendre(3, 1, x)) == simplify( -R(3, 2) * sqrt(1 - x**2) * (5 * x**2 - 1))
(secsq_alpha + sqrt(secsq_alpha + tan(beta)**2))) s_pt2 = (atan(tan(alpha) * tan(beta)) / (secsq_beta + sqrt(secsq_beta + tan(alpha)**2))) S = (4 * R**2) * (s_pt1 + s_pt2) sval = lambdify([alpha, beta, R], S, 'mpmath') #%% # First implement the on-axis response. subsdict = {'n': 1, 'alpha': pi / 2.5, 'beta': pi / 2.5, 'k': 10, 'R': 0.1} ion_pt1_1 = (tan(alpha) / (sqrt(cos(phi)**2 + tan(alpha)**2))) ion_pt1_asocleg_term = cos(phi) / (sqrt(cos(phi)**2 + tan(alpha)**2)) ion_pt1_2 = assoc_legendre(n, -1, ion_pt1_asocleg_term) ion_integral_limit1 = atan(tan(beta) / tan(alpha)) ion_pt1 = Integral(ion_pt1_1 * ion_pt1_2, (phi, 0, ion_integral_limit1)) Ion_pt1 = lambdify([n, alpha, beta], ion_pt1, 'sympy') sqrt_num_pt2 = sqrt(sin(phi)**2 + tan(beta)**2) ion_pt2_1 = tan(beta) / sqrt_num_pt2 ion_pt2_assocleg_term = sin(phi) / sqrt_num_pt2 ion_pt2_2 = assoc_legendre(n, -1, ion_pt2_assocleg_term) ion2_integral_limit = (pi / 2 - atan(tan(alpha) / tan(beta)), pi / 2 + atan(tan(alpha) / tan(beta))) ion_pt2 = Integral(ion_pt2_1 * ion_pt2_2, (phi, ion2_integral_limit[0], ion2_integral_limit[1])) Ion_pt2 = lambdify([n, alpha, beta], ion_pt2, 'sympy') # print(Ion_pt2(subsdict['n'],subsdict['alpha'],subsdict['beta']).evalf())
def acustic_sh(l,m,a,z) : am = numpy.abs(m) if m<0 : return sh_normalization(l,am) * sympy.sin(am*a) * sympy.assoc_legendre(l,am,sympy.cos(z)) else: return sh_normalization(l,am) * sympy.cos(am*a) * sympy.assoc_legendre(l,am,sympy.cos(z))
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_J10(): mu, nu = symbols('mu, nu', integer=True) assert assoc_legendre(nu, mu, 0) == 2**mu*sqrt(pi)/gamma((nu - mu)/2 + 1)/gamma((-nu - mu + 1)/2)
def test_J11(): assert simplify(assoc_legendre(3, 1, x)) == simplify(-R(3, 2)*sqrt(1 - x**2)*(5*x**2 - 1))
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 )}"
a0, z0 = 0, numpy.pi/2 epsilon = a*1e-15 + z*1e-15 order = 5 nSamples = 70 angles = numpy.arange(0,nSamples+1)*2*numpy.pi/nSamples w = 2*math.pi*spectralRange/spectrumBins * numpy.arange(spectrumBins) if False: print "Checking that the azimuthal formula is equivalent to the zenital formula" for l in xrange(0,order+1) : # pattern1 = sum([sh_normalization2(l,m)*sympy.assoc_legendre(l,m,sympy.cos(z))*sympy.assoc_legendre(l,m,1) for m in xrange(0,l+1)]) # pattern1 = sh_normalization2(l,0)*sympy.assoc_legendre(l,0,sympy.cos(z)) pattern1 = sh_normalization2(l,0)*sympy.legendre(l,sympy.cos(z)) pylab.polar(angles, [ pattern1.subs(z,angle)/(2*l+1) for angle in angles ], label="Zenital %s"%l) pattern2 = sum([sh_normalization2(l,m)*sympy.cos(m*z)*sympy.assoc_legendre(l,m,0)**2 for m in xrange(0,l+1)]) pylab.polar(angles, [ pattern2.subs(z,angle)/(2*l+1) for angle in angles ], label="Azimuthal %s"%l) pylab.title("Zenital vs Azimuthal variation",horizontalalignment='center', verticalalignment='baseline', position=(.5,-.1)) pylab.rgrids(numpy.arange(.4,1,.2),angle=220) pylab.legend(loc=2) pylab.savefig(figurePath(__file__,"pdf")) pylab.show() # We take the azimuthal formula which is faster print "Computing component patterns" patternComponents = [ sh_normalization2(l,0)*sympy.legendre(l,sympy.cos(z)) for l in xrange(0,order+1) ] for l, pattern in enumerate(patternComponents) : print "%i:"%l, pattern
def dPlm(l, m, x): if l == 0 and m == 0: return np.zeros_like(x) else: return lambdify(r, diff(assoc_legendre(l, m, r), r), modules=['numpy'])(x)
theta=sym.Symbol('theta',real=True,positive=True) phi=sym.Symbol('phi',real=True,positive=True) r=sym.Symbol('r',real=True,positive=True) def c(ell,m): if(m>=0): return (factorial(ell-1)*(-2)**Abs(m))/factorial(ell+Abs(m))*cos(m*phi) else: return (factorial(ell-1)*(-2)**Abs(m))/factorial(ell+Abs(m))*sin(Abs(m)*phi) ell=3 m=0 Sigma=c(ell,m)*r**ell*assoc_legendre(ell,Abs(m),cos(theta)) Sigma=sym.simplify(Sigma) Sigma=Sigma.subs({Abs(sin(theta)):sin(theta)}) Sigma=Sigma.expand(trig=True) print("Sigma in spherical coordinates is %s"%Sigma) Sigma=Sigma.subs({r*cos(theta):z, r*sin(theta)*sin(phi):y, r*sin(theta)*cos(phi):x}) Sigma=Sigma.subs({r**2*sin(theta)**2:x**2+y**2}) Sigma=Sigma.subs({r**2:x**2+y**2+z**2}) Sigma=sym.simplify(Sigma.expand()) print("Sigma in cartesian coordinates is %s"%Sigma) Pix=Derivative(Sigma,x) Piy=Derivative(Sigma,y) Piz=Derivative(Sigma,z)
def test_J11(): assert assoc_legendre(3, 1, x) == sqrt(1 - x**2) * (R(3, 2) - R(15, 2) * x**2)
def Plm(l, m, x): # assoc_legendre return a number 0 for l=m=0 if l == 0 and m == 0: return np.ones_like(x) else: return lambdify(r, assoc_legendre(l, m, r), modules=['numpy'])(x)
def test_J10(): mu, nu = symbols('mu, nu', integer=True) assert assoc_legendre( nu, mu, 0) == 2**mu * sqrt(pi) / gamma((nu - mu) / 2 + 1) / gamma( (-nu - mu + 1) / 2)
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_J11(): assert assoc_legendre(3,1,x) == sqrt(1 - x**2)*(R(3,2) - R(15,2)*x**2)