K i +ddlmZddlmZmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZddlmZmZdd lmZdd lmZdd lmZmZmZe d ZGd deZdZGddeZy))Expr)DefinedFunctionArgumentIndexError)Ipi)S)Dummy)assoc_legendre) factorial)Abs conjugate)exp)sqrt)sincoscotxcTeZdZdZedZdZd dZdZdZ dZ dZ d d Z d Z y )Ynma4 Spherical harmonics defined as .. math:: Y_n^m(\theta, \varphi) := \sqrt{\frac{(2n+1)(n-m)!}{4\pi(n+m)!}} \exp(i m \varphi) \mathrm{P}_n^m\left(\cos(\theta)\right) Explanation =========== ``Ynm()`` gives the spherical harmonic function of order $n$ and $m$ in $\theta$ and $\varphi$, $Y_n^m(\theta, \varphi)$. The four parameters are as follows: $n \geq 0$ an integer and $m$ an integer such that $-n \leq m \leq n$ holds. The two angles are real-valued with $\theta \in [0, \pi]$ and $\varphi \in [0, 2\pi]$. Examples ======== >>> from sympy import Ynm, Symbol, simplify >>> from sympy.abc import n,m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> Ynm(n, m, theta, phi) Ynm(n, m, theta, phi) Several symmetries are known, for the order: >>> Ynm(n, -m, theta, phi) (-1)**m*exp(-2*I*m*phi)*Ynm(n, m, theta, phi) As well as for the angles: >>> Ynm(n, m, -theta, phi) Ynm(n, m, theta, phi) >>> Ynm(n, m, theta, -phi) exp(-2*I*m*phi)*Ynm(n, m, theta, phi) For specific integers $n$ and $m$ we can evaluate the harmonics to more useful expressions: >>> simplify(Ynm(0, 0, theta, phi).expand(func=True)) 1/(2*sqrt(pi)) >>> simplify(Ynm(1, -1, theta, phi).expand(func=True)) sqrt(6)*exp(-I*phi)*sin(theta)/(4*sqrt(pi)) >>> simplify(Ynm(1, 0, theta, phi).expand(func=True)) sqrt(3)*cos(theta)/(2*sqrt(pi)) >>> simplify(Ynm(1, 1, theta, phi).expand(func=True)) -sqrt(6)*exp(I*phi)*sin(theta)/(4*sqrt(pi)) >>> simplify(Ynm(2, -2, theta, phi).expand(func=True)) sqrt(30)*exp(-2*I*phi)*sin(theta)**2/(8*sqrt(pi)) >>> simplify(Ynm(2, -1, theta, phi).expand(func=True)) sqrt(30)*exp(-I*phi)*sin(2*theta)/(8*sqrt(pi)) >>> simplify(Ynm(2, 0, theta, phi).expand(func=True)) sqrt(5)*(3*cos(theta)**2 - 1)/(4*sqrt(pi)) >>> simplify(Ynm(2, 1, theta, phi).expand(func=True)) -sqrt(30)*exp(I*phi)*sin(2*theta)/(8*sqrt(pi)) >>> simplify(Ynm(2, 2, theta, phi).expand(func=True)) sqrt(30)*exp(2*I*phi)*sin(theta)**2/(8*sqrt(pi)) We can differentiate the functions with respect to both angles: >>> from sympy import Ynm, Symbol, diff >>> from sympy.abc import n,m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> diff(Ynm(n, m, theta, phi), theta) m*cot(theta)*Ynm(n, m, theta, phi) + sqrt((-m + n)*(m + n + 1))*exp(-I*phi)*Ynm(n, m + 1, theta, phi) >>> diff(Ynm(n, m, theta, phi), phi) I*m*Ynm(n, m, theta, phi) Further we can compute the complex conjugation: >>> from sympy import Ynm, Symbol, conjugate >>> from sympy.abc import n,m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> conjugate(Ynm(n, m, theta, phi)) (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi) To get back the well known expressions in spherical coordinates, we use full expansion: >>> from sympy import Ynm, Symbol, expand_func >>> from sympy.abc import n,m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> expand_func(Ynm(n, m, theta, phi)) sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*exp(I*m*phi)*assoc_legendre(n, m, cos(theta))/(2*sqrt(pi)) See Also ======== Ynm_c, Znm References ========== .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/ .. [4] https://dlmf.nist.gov/14.30 cV|jr>| }tj|ztdtz|z|zzt ||||zS|jr| }t ||||S|jr*| }tdtz|z|zt ||||zSy)N)could_extract_minus_signr NegativeOnerrr)clsnmthetaphis q/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/functions/special/spherical_harmonics.pyevalzYnm.evals % % 'A==!#c"Q$q&*o5Aq%8MM M  ) ) +FEq!UC( (  ' ' )$Cr!tAvcz?SAuc%:: : *c X|j\}}}}td|zdzdtzz t||z zt||zz t t |z|zzt ||t|z}|jtt|dz dzt|SN) argsrrr rrr rsubsr)selfhintsrrrrrvs r_eval_expand_funczYnm._eval_expand_funcs991eSAaC!Gad#iA&66yQ7GGHAaCG -aCJ?@wwtSZ]NQ./U<WWX Y ]#yy Aq%q53q!UC00 0$T84 4r!c &|jdS)NTfunc)expandr)rrrrkwargss r_eval_rewrite_as_polynomialzYnm._eval_rewrite_as_polynomials{{{%%r!c ,|jtSN)rewriterr5s r_eval_rewrite_as_sinzYnm._eval_rewrite_as_sins||C  r!c ddlm}m}||jd}|j t t |t |i}|||S)Nr)simplifytrigsimpTr2)sympy.simplifyr=r>r4xreplacer r) r)rrrrr6r=r>terms r_eval_rewrite_as_coszYnm._eval_rewrite_as_cossJ5  ./}}c#e*oc%j9:''r!cv|j\}}}}tj|z|j|| ||zSr9)r'rrr3)r)rrrrs r_eval_conjugatezYnm._eval_conjugates9991eS}}a$))Ar5#">>>r!c |j\}}}}td|zdzdtzz t||z zt||zz t ||zzt ||t |z}td|zdzdtzz t||z zt||zz t ||zzt ||t |z}||fSr#)r'rrr rr r) r)deepr*rrrrreims r as_real_imagzYnm.as_real_imags991eSAaC!Gad#iA&66yQ7GGH!C%j)!QE ;<AaC!Gad#iA&66yQ7GGH!C%j)!QE ;<Bxr!cddlm}m}|jdj |}|jdj |}|jdj |}|jdj |}||5|j ||||}dddt j|S#1swYxYw)Nr)mpworkprecr%r$r.)mpmathrKrLr' _to_mpmath spherharmr _from_mpmath) r)precrKrLrrrrress r _eval_evalfzYnm._eval_evalfs ( IIaL # #D ) IIaL # #D ) ! ''-iil%%d+ d^ 1,,q!UC0C 1  d++ 1 1s B<<CN)r&)T)__name__ __module__ __qualname____doc__ classmethodr r,r0r7r;rBrDrIrSr!rrrsEwr ; ;=5&& !(?  ,r!rc0tt||||S)a0 Conjugate spherical harmonics defined as .. math:: \overline{Y_n^m(\theta, \varphi)} := (-1)^m Y_n^{-m}(\theta, \varphi). Examples ======== >>> from sympy import Ynm_c, Symbol, simplify >>> from sympy.abc import n,m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> Ynm_c(n, m, theta, phi) (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi) >>> Ynm_c(n, m, -theta, phi) (-1)**(2*m)*exp(-2*I*m*phi)*Ynm(n, m, theta, phi) For specific integers $n$ and $m$ we can evaluate the harmonics to more useful expressions: >>> simplify(Ynm_c(0, 0, theta, phi).expand(func=True)) 1/(2*sqrt(pi)) >>> simplify(Ynm_c(1, -1, theta, phi).expand(func=True)) sqrt(6)*exp(I*(-phi + 2*conjugate(phi)))*sin(theta)/(4*sqrt(pi)) See Also ======== Ynm, Znm References ========== .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/ )r r)rrrrs rYnm_cr[sP SAuc* ++r!c eZdZdZedZy)Znma{ Real spherical harmonics defined as .. math:: Z_n^m(\theta, \varphi) := \begin{cases} \frac{Y_n^m(\theta, \varphi) + \overline{Y_n^m(\theta, \varphi)}}{\sqrt{2}} &\quad m > 0 \\ Y_n^m(\theta, \varphi) &\quad m = 0 \\ \frac{Y_n^m(\theta, \varphi) - \overline{Y_n^m(\theta, \varphi)}}{i \sqrt{2}} &\quad m < 0 \\ \end{cases} which gives in simplified form .. math:: Z_n^m(\theta, \varphi) = \begin{cases} \frac{Y_n^m(\theta, \varphi) + (-1)^m Y_n^{-m}(\theta, \varphi)}{\sqrt{2}} &\quad m > 0 \\ Y_n^m(\theta, \varphi) &\quad m = 0 \\ \frac{Y_n^m(\theta, \varphi) - (-1)^m Y_n^{-m}(\theta, \varphi)}{i \sqrt{2}} &\quad m < 0 \\ \end{cases} Examples ======== >>> from sympy import Znm, Symbol, simplify >>> from sympy.abc import n, m >>> theta = Symbol("theta") >>> phi = Symbol("phi") >>> Znm(n, m, theta, phi) Znm(n, m, theta, phi) For specific integers n and m we can evaluate the harmonics to more useful expressions: >>> simplify(Znm(0, 0, theta, phi).expand(func=True)) 1/(2*sqrt(pi)) >>> simplify(Znm(1, 1, theta, phi).expand(func=True)) -sqrt(3)*sin(theta)*cos(phi)/(2*sqrt(pi)) >>> simplify(Znm(2, 1, theta, phi).expand(func=True)) -sqrt(15)*sin(2*theta)*cos(phi)/(4*sqrt(pi)) See Also ======== Ynm, Ynm_c References ========== .. [1] https://en.wikipedia.org/wiki/Spherical_harmonics .. [2] https://mathworld.wolfram.com/SphericalHarmonic.html .. [3] https://functions.wolfram.com/Polynomials/SphericalHarmonicY/ c"|jr+t||||t||||ztdz }|S|jrt||||S|j r2t||||t||||z tdt zz }|Sy)Nr$) is_positiverr[ris_zero is_negativer)rrrrrzzs rr zZnm.evalEs ==aE3'%1eS*AAT!WLBI YYq!UC( ( ]]aE3'%1eS*AAd1gaiPBIr!N)rTrUrVrWrXr rYr!rr]r] s7rr!r]N)sympy.core.exprrsympy.core.functionrrsympy.core.numbersrrsympy.core.singletonrsympy.core.symbolr sympy.functionsr (sympy.functions.combinatorial.factorialsr $sympy.functions.elementary.complexesr r &sympy.functions.elementary.exponentialr(sympy.functions.elementary.miscellaneousr(sympy.functions.elementary.trigonometricrrr_xrr[r]rYr!rrosU C$"#*>?69BB 3ZN,/N,b(,VC/Cr!