简体   繁体   English

制表球谐函数

[英]Tabulate Spherical Harmonic Functions

Hey I am trying to express the spherical harmonics in a compact way.嘿,我正在尝试以紧凑的方式表达球谐函数。 I can not figure out how to express this in a table I got the spherical Harmonics for l = 0,1,2 and m = -l, ...,+l in a list.我不知道如何在表格中表达这一点,我在列表中得到了 l = 0,1,2 和 m = -l, ...,+l 的球面谐波。 But wanted a Table in the form:但想要一个表格形式:

 m -2 -1 0 1 2
l        
0        x
1      x x x
2    x x x x x

Does anyone know how to do this since I only worked out to create simpler tables with tabulate using with normal headers.有谁知道如何做到这一点,因为我只是想用普通标题创建更简单的表格。 I have got the functions in the list sph_harmonics[].我有列表 sph_harmonics[] 中的函数。

from sympy import Ynm
from tabulate import tabulate

sph_harmonics = []
for l in range (0, 3):
            for m in range(-l, l+1):
                print('l =', l)
                print('m =',m)
                print(sp.simplify(Ynm(l, m, theta, phi).expand(func=True)))
                sph_harmonics.append(sp.simplify(Ynm(l, m, theta, phi).expand(func=True)))

Mathjax itself doesn't have the table / tabular environment, but still it's possible to use the array environment for this ( link1 ; link2 ; link3 ). Mathjax 本身没有table / tabular环境,但仍然可以为此使用array环境( link1 ; link2 ; link3 )。

So based on this, I wrote the following formatting function called fmt_table which formats the formulas like you mentioned:因此,基于此,我编写了名为fmt_table的以下格式 function ,它像您提到的那样格式化公式:

from sympy import Ynm, simplify, symbols, latex
theta, phi = symbols('\Theta \phi')
from IPython.display import HTML, display, Math, Latex

def fmt_table(data,center_data=False,add_row_nums=False):
    from math import ceil
    buf='''
\\newcommand\\T{\\Rule{0pt}{1em}{.3em}}
\\begin{array}{%s}    
'''
    max_cols = max(len(r) for r in data)
    column_spec = '|' + '|'.join(['c']*max_cols) + '|'
    buf = buf % column_spec
    row_idx = 0
    for row_data in data:
        row = ''
        if add_row_nums and row_idx > 0:
            row += str(row_idx) + " & "
        if center_data and row_idx > 0:
            to_add = ceil( (max_cols - len(row_data))/2 )
            row += ' & '.join([''] * to_add)
        row += ' & '.join(row_data)
        if row_idx == 0:
            row = '''\\hline''' + row + '''\\\\\hline'''
        else:
            row += '''\\\\\hline'''
        row += "\n"
        buf +=row
        row_idx += 1
    buf += '''\\end{array}'''
    # print(buf)
    return buf
    

tbl = []
tbl.append(['\\texttt{m/l}','-2','-1','0','1','2'])
for l in range (0, 3):
    new_row = []    
    for m in range(-l, l+1):
        h = simplify(Ynm(l, m, theta, phi).expand(func=True))
        new_row.append(latex(h))
    tbl.append(new_row)
    

display(Math(fmt_table(tbl,add_row_nums=True,center_data=True)))

OUTPUT: OUTPUT:

在此处输入图像描述

All the code in this answer is also available here .此答案中的所有代码也可在此处获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM