def W2phot(theta1, phi1, theta2, phi2, i_i, i, i_f, e0m1, l1, e0m1p, lp1, delta1, l2, lp2, delta2, i_intn, lint, lintp, deltainter): q0, q1, q2 = symbols("q₀ q₁ q₂", integer=True) s = [ [ [ summation( ((-1)**(lam1 + lam2)/(4 * pi * sqrt(2 * lam2 + 1)) * isst(lam0, q0, l1, i_i, i, e0m1) * a(lint, lintp, i_intn, i, deltainter, lam1, lam2, lam0) * a(l2, lp2, i_f, i_intn, delta2, lam2) * Wigner3j(lam2, q2, lam1, q1, lam0, q0)) * Ynm(lam1, q1, theta1, phi1) * Ynm(lam2, q2, theta2, phi2), (q2, -lam2, lam2), (q1, -lam1, lam1), (q0, -lam0, lam0)) for lam2 in list(range(0, 5, 2)) ] for lam1 in list(range(0, 5, 2)) ] for lam0 in list(range(0, 5, 2)) ] return sum(flatten(s))
def dot_rot_grad_Ynm(j, p, l, m, theta, phi): r""" Returns dot product of rotational gradients of spherical harmonics. This function returns the right hand side of the following expression: .. math :: \vec{R}Y{_j^{p}} \cdot \vec{R}Y{_l^{m}} = (-1)^{m+p} \sum\limits_{k=|l-j|}^{l+j}Y{_k^{m+p}} * \alpha_{l,m,j,p,k} * \frac{1}{2} (k^2-j^2-l^2+k-j-l) Arguments ========= j, p, l, m .... indices in spherical harmonics (expressions or integers) theta, phi .... angle arguments in spherical harmonics Example ======= >>> from sympy import symbols >>> from sympy.physics.wigner import dot_rot_grad_Ynm >>> theta, phi = symbols("theta phi") >>> dot_rot_grad_Ynm(3, 2, 2, 0, theta, phi).doit() 3*sqrt(55)*Ynm(5, 2, theta, phi)/(11*sqrt(pi)) """ j = sympify(j) p = sympify(p) l = sympify(l) m = sympify(m) theta = sympify(theta) phi = sympify(phi) k = Dummy("k") def alpha(l, m, j, p, k): return (sqrt((2 * l + 1) * (2 * j + 1) * (2 * k + 1) / (4 * pi)) * Wigner3j(j, l, k, S.Zero, S.Zero, S.Zero) * Wigner3j(j, l, k, p, m, -m - p)) return (S.NegativeOne)**(m + p) * Sum( Ynm(k, m + p, theta, phi) * alpha(l, m, j, p, k) / 2 * (k**2 - j**2 - l**2 + k - j - l), (k, abs(l - j), l + j), )
def test_dot_rota_grad_SH(): theta, phi = symbols("theta phi") assert dot_rot_grad_Ynm(1, 1, 1, 1, 1, 0) != \ sqrt(30)*Ynm(2, 2, 1, 0)/(10*sqrt(pi)) assert dot_rot_grad_Ynm(1, 1, 1, 1, 1, 0).doit() == \ sqrt(30)*Ynm(2, 2, 1, 0)/(10*sqrt(pi)) assert dot_rot_grad_Ynm(1, 5, 1, 1, 1, 2) != \ 0 assert dot_rot_grad_Ynm(1, 5, 1, 1, 1, 2).doit() == \ 0 assert dot_rot_grad_Ynm(3, 3, 3, 3, theta, phi).doit() == \ 15*sqrt(3003)*Ynm(6, 6, theta, phi)/(143*sqrt(pi)) assert dot_rot_grad_Ynm(3, 3, 1, 1, theta, phi).doit() == \ sqrt(3)*Ynm(4, 4, theta, phi)/sqrt(pi) assert dot_rot_grad_Ynm(3, 2, 2, 0, theta, phi).doit() == \ 3*sqrt(55)*Ynm(5, 2, theta, phi)/(11*sqrt(pi)) assert dot_rot_grad_Ynm(3, 2, 3, 2, theta, phi).doit() == \ -sqrt(70)*Ynm(4, 4, theta, phi)/(11*sqrt(pi)) + \ 45*sqrt(182)*Ynm(6, 4, theta, phi)/(143*sqrt(pi))
real=True, ) eta_sp, eta_psp, eta_pdp, eta_dp, eta_fdp = symbols( 'eta_sp eta_psp eta_pdp eta_dp eta_fdp', real=True, ) phi, theta, varphi = symbols('phi theta varphi', real=True) c0_sp = -sqrt(3) / 3 * coeff_sp c0_dp, c1_dp = sqrt(30) / 15 * coeff_dp, sqrt(10) / 10 * coeff_dp c0_psp = -Rational(1, 3) * coeff_psp c0_pdp, c1_pdp = -Rational(2, 15) * coeff_pdp, -Rational(1, 10) * coeff_pdp c0_fdp, c1_fdp = sqrt(14) / 35 * coeff_fdp, 2 * sqrt(21) / 105 * coeff_fdp waves = { 'm=0': (c0_psp * exp(eta_psp * I) * Ynm(1, 0, theta, varphi) + c0_pdp * exp(eta_pdp * I) * Ynm(1, 0, theta, varphi) + c0_fdp * exp(eta_fdp * I) * Ynm(3, 0, theta, varphi) + c0_sp * exp(eta_sp * I + phi * I) * Ynm(0, 0, theta, varphi) + c0_dp * exp(eta_dp * I + phi * I) * Ynm(2, 0, theta, varphi)), 'm=1': (c1_pdp * exp(eta_pdp * I) * Ynm(1, 1, theta, varphi) + c1_fdp * exp(eta_fdp * I) * Ynm(3, 1, theta, varphi) + c1_dp * exp(eta_dp * I + phi * I) * Ynm(2, 1, theta, varphi)), } pads = { k: sum(Abs(term)**2 for term in expr) for k, expr in { 'm=0': [waves['m=0']], 'm=1': [waves['m=1']], 'summed': [waves['m=1'].subs(varphi, -varphi), waves['m=0'], waves['m=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"\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 )}' 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)'
eta_p, eta_f, eta_s, eta_d = symbols( 'eta_p eta_f eta_s eta_d', real=True, ) c_psp, c_pdp, c_fdp, c_sp, c_dp = symbols( 'c_psp c_pdp c_fdp c_sp c_dp', positive=True, ) c0sp = -sqrt(3)/3*c_sp c0dp, c1dp = sqrt(6)/3*c_dp, sqrt(2)/2*c_dp c0psp = -sqrt(3)/3*c_psp c0pdp, c1pdp = -2*sqrt(15)/15*c_pdp, -sqrt(15)/10*c_pdp c0fdp, c1fdp = sqrt(10)/5*c_fdp, 2*sqrt(15)/15*c_fdp waves_m = { -1: (c1pdp * exp(eta_p * I) * Ynm(1, -1, the, vphi) + c1fdp * exp(eta_f * I) * Ynm(3, -1, the, vphi) + c1dp * exp(eta_d * I + phi * I) * Ynm(2, -1, the, vphi)), 0: (c0psp * exp(eta_p * I) * Ynm(1, 0, the, vphi) + c0pdp * exp(eta_p * I) * Ynm(1, 0, the, vphi) + c0fdp * exp(eta_f * I) * Ynm(3, 0, the, vphi) + c0sp * exp(eta_s * I + phi * I) * Ynm(0, 0, the, vphi) + c0dp * exp(eta_d * I + phi * I) * Ynm(2, 0, the, vphi)), 1: (c1pdp * exp(eta_p * I) * Ynm(1, 1, the, vphi) + c1fdp * exp(eta_f * I) * Ynm(3, 1, the, vphi) + c1dp * exp(eta_d * I + phi * I) * Ynm(2, 1, the, vphi)), } yield_lft = ( expand_func(Abs(waves_m[-1]) ** 2 + Abs(waves_m[0]) ** 2 + Abs(waves_m[1]) ** 2) .subs(sin(the) ** 2, 1 - cos(the) ** 2) )
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Aug 6 13:39:05 2018 @author: dietz """ from sympy import Ynm, Symbol, N theta = Symbol("theta") phi = Symbol("phi") generated_code = '' first_iter = True for n in range(0, 7): for m in range(-n, n + 1): if n == 6 and m == n: generated_code += 'else:\n' else: if first_iter: generated_code += 'if ' else: generated_code += 'elif ' generated_code += 'l == {:d} and m == {:d}:\n'.format(n, m) sph_harm = N(Ynm(n, m, theta, phi).expand(func=True)) sph_harm = str(sph_harm).replace('I', '1j') generated_code += ' return ' + sph_harm + '\n' first_iter = False
from typing import Tuple from functools import wraps from sympy import (Expr, Symbol, symbols, I, pi, exp, Ynm, Abs, cos, sin, arg, sqrt, re, legendre, cancel, expand_func, simplify, expand, solve, lambdify) __all__ = ['lambdified'] # %% pads coeff_s, coeff_p, coeff_d = symbols('coeff_s coeff_p coeff_d', positive=True) eta_s, eta_p, eta_d = symbols('eta_s eta_p eta_d', real=True) phi, theta, varphi = symbols('phi theta varphi', real=True) waves = { 'm=0': (coeff_s * exp(eta_s * I) * Ynm(0, 0, theta, varphi) + coeff_p * exp(eta_p * I + phi * I) * Ynm(1, 0, theta, varphi) + coeff_d * exp(eta_d * I) * Ynm(2, 0, theta, varphi)), } pads = { 'm=0': Abs(waves['m=0'])**2, 'summed': Abs(waves['m=0'])**2, } # %% solve pad eq def expend_cos(expr: Expr, x: Symbol) -> Tuple[Expr, Expr]: while True: term = expr.subs(x, pi / 2) yield term expr = cancel((expr - term) / cos(x))
# %% pads coeff_s, coeff_p, coeff_d = symbols( 'coeff_s coeff_p coeff_d', positive=True, ) eta_s, eta_p, eta_d = symbols( 'eta_s eta_p eta_d', real=True, ) phi, theta, varphi = symbols('phi theta varphi', real=True) c0_ps, c0_sps, c0_dps = coeff_p, coeff_s, coeff_d waves = { 'm=0': (c0_sps * exp(eta_s * I) * Ynm(0, 0, theta, varphi) + c0_ps * exp(eta_p * I + phi * I) * Ynm(1, 0, theta, varphi) + c0_dps * exp(eta_d * I) * Ynm(2, 0, theta, varphi)), } pads = { 'm=0': Abs(waves['m=0'])**2, 'summed': Abs(waves['m=0'])**2, } # %% solve pad eq def solve_eq(pad: Expr) -> dict: # expand left term left = (expand_func(pad).subs(sin(theta)**2, 1 - cos(theta)**2)) terms_lft = list(islice(chain(expend_cos(left, theta), repeat(0)), 5))