Python正则表达式可简化LaTex分数 [英] Python Regex to Simplify LaTex Fractions

查看:44
本文介绍了Python正则表达式可简化LaTex分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的python程序:

This is my python program:

def fractionSimplifier(content):
    content.replace('\\dfrac','\\frac')
    pat = re.compile('\\frac\{(.*?)\}\{\\frac\{(.*?)\}\{(.*?)\}\}')
    match = pat.match(content)
    expr = ""
    if match:
        expr = '\\frac{{{0}*{2}}}{{{1}}}'.format(*match.group())
        #print expr
    return expr

这是应该编辑的LaTex代码.

This is the LaTex code that should be edited.

%Jacobi
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\JacobiP{\alpha}{\beta}{\ell}@{x})^2}{\mathcal{A}_\ell}=\frac{\frac{\pochhammer{n+\alpha+\beta+1}{n}}{2^nn!}}{\mathcal{A}_n\frac{\pochhammer{n+1+\alpha+\beta+1}{n+1}}{2^n+1n+1!}}\frac{\JacobiP{\alpha}{\beta}{n+1}@{x}\JacobiP{\alpha}{\beta}{n}@{y}-\JacobiP{\alpha}{\beta}{n}@{x}\JacobiP{\alpha}{\beta}{n+1}@{y}}{x-y}
\end{equation}

%Ultraspherical(Gegenbauer)
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\Ultraspherical{\lambda}{\ell}@{x})^2}{\frac{2^{1-2\lambda}\pi\EulerGamma@{\ell+2\lambda}}{(\ell+\lambda)\left(\EulerGamma@{\lambda}\right)^2\ell!}}=\frac{\frac{2^n\pochhammer{\lambda}{n}}{n!}}{\frac{2^{1-2\lambda}\pi\EulerGamma@{n+2\lambda}}{(n+\lambda)\left(\EulerGamma@{\lambda}\right)^2n!}\frac{2^n+1\pochhammer{\lambda}{n+1}}{n+1!}}\frac{\Ultraspherical{\lambda}{n+1}@{x}\Ultraspherical{\lambda}{n}@{y}-\Ultraspherical{\lambda}{n}@{x}\Ultraspherical{\lambda}{n+1}@{y}}{x-y}
\end{equation}

%Chebyshevoffirstkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyT{\ell}@{x})^2}{\frac{\cpi}{\epsilon_\ell}}=\frac{\frac{2^n}{\epsilon_n}}{\frac{\cpi}{\epsilon_n}\frac{2^n+1}{\epsilon+1_n+1}}\frac{\ChebyT{n+1}@{x}\ChebyT{n}@{y}-\ChebyT{n}@{x}\ChebyT{n+1}@{y}}{x-y}
\end{equation}

%Chebyshevofsecondkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyU{\ell}@{x})^2}{\frac{\pi}{2}}=\frac{2^n}{\frac{\pi}{2}2^n+1}\frac{\ChebyU{n+1}@{x}\ChebyU{n}@{y}-\ChebyU{n}@{x}\ChebyU{n+1}@{y}}{x-y}
\end{equation}

%Chebyshevofthirdkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyV{\ell}@{x})^2}{\pi}=\frac{2^n}{\pi2^n+1}\frac{\ChebyV{n+1}@{x}\ChebyV{n}@{y}-\ChebyV{n}@{x}\ChebyV{n+1}@{y}}{x-y}
\end{equation}

%Chebyshevoffourthkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyW{\ell}@{x})^2}{\pi}=\frac{2^n}{\pi2^n+1}\frac{\ChebyW{n+1}@{x}\ChebyW{n}@{y}-\ChebyW{n}@{x}\ChebyW{n+1}@{y}}{x-y}
\end{equation}

%ShiftedChebyshevoffirstkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyTs{\ell}@{x})^2}{\frac{\cpi}{\epsilon_\ell}}=\frac{\frac{2^n}{\epsilon_n}}{\frac{\cpi}{\epsilon_n}\frac{2^n+1}{\epsilon+1_n+1}}\frac{\ChebyTs{n+1}@{x}\ChebyTs{n}@{y}-\ChebyTs{n}@{x}\ChebyTs{n+1}@{y}}{x-y}
\end{equation}

%ShiftedChebyshevofsecondkind
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\ChebyUs{\ell}@{x})^2}{\frac{\pi}{8}}=\frac{2^{2n}}{\frac{\pi}{8}2^{2n+1}}\frac{\ChebyUs{n+1}@{x}\ChebyUs{n}@{y}-\ChebyUs{n}@{x}\ChebyUs{n+1}@{y}}{x-y}
\end{equation}

%Legendre
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\LegendrePoly{\ell}@{x})^2}{\frac{2}{(2\ell+1)}}=\frac{\frac{2^n\pochhammer{\frac{1}{2}}{n}}{n!}}{\frac{2}{(2n+1)}\frac{2^n+1\pochhammer{\frac{1}{2}}{n+1}}{n+1!}}\frac{\LegendrePoly{n+1}@{x}\LegendrePoly{n}@{y}-\LegendrePoly{n}@{x}\LegendrePoly{n+1}@{y}}{x-y}
\end{equation}

%ShiftedLegendre
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\LegendrePolys{\ell}@{x})^2}{\frac{1}{(2\ell+1)}}=\frac{\frac{2^{2n}\pochhammer{\frac{1}{2}}{n}}{n!}}{\frac{1}{(2n+1)}\frac{2^{2n+1}\pochhammer{\frac{1}{2}}{n+1}}{n+1!}}\frac{\LegendrePolys{n+1}@{x}\LegendrePolys{n}@{y}-\LegendrePolys{n}@{x}\LegendrePolys{n+1}@{y}}{x-y}
\end{equation}

%Laguerre
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\LaguerreL[\alpha]{\ell}@{x})^2}{\frac{\EulerGamma@{\ell+\alpha+1}}{\ell!}}=\frac{\frac{\opminus^n}{n!}}{\frac{\EulerGamma@{n+\alpha+1}}{n!}\frac{\opmin+1us^n+1}{n+1!}}\frac{\LaguerreL[\alpha]{n+1}@{x}\LaguerreL[\alpha]{n}@{y}-\LaguerreL[\alpha]{n}@{x}\LaguerreL[\alpha]{n+1}@{y}}{x-y}
\end{equation}

%Hermite
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\HermiteH{\ell}@{x})^2}{\pi^{\frac{1}{2}}2^\ell\ell!}=\frac{2^n}{\pi^{\frac{1}{2}}2^nn!2^n+1}\frac{\HermiteH{n+1}@{x}\HermiteH{n}@{y}-\HermiteH{n}@{x}\HermiteH{n+1}@{y}}{x-y}
\end{equation}

%Hermite
\begin{equation}
\sum_{\ell\hiderel{=}0}^n\frac{(\HermiteHe{\ell}@{x})^2}{(2\pi)^{\frac{1}{2}}\ell!}=\frac{1}{(2\pi)^{\frac{1}{2}}n!1}\frac{\HermiteHe{n+1}@{x}\HermiteHe{n}@{y}-\HermiteHe{n}@{x}\HermiteHe{n+1}@{y}}{x-y}
\end{equation}

\ frac {(\ ChebyU {\ ell} @ {x})^ 2} * {\ frac {\ pi} {2}} 是我要寻找的特定部分第二类切比雪夫案.由于它是另一个分数的分母,因此我想使用Python正则表达式将其从 a/(b/c)更改为(ac)/b .

\frac{(\ChebyU{\ell}@{x})^2}*{\frac{\pi}{2}} is the fraction I am looking at for the specific case of Chebyshev of Second Kind. Since it is a fraction in the denominator of another fraction I would like to use Python regex to change this from a/(b/c) to (ac)/b.

示例输出:

%Chebyshev of second kind
\begin{equation}
\sum_{\ell \hiderel{=} 0}^n\frac{(2 \ChebyU{\ell}@{x}  )^2}{\pi}*=\frac{ 2^n  }{ \frac{\pi}{2}   2^n+1  }\frac{ \ChebyU{n+1}@{x}   \ChebyU{n}@{y}  - \ChebyU{n}@{x}   \ChebyU{n+1}@{y}  }{x-y}
\end{equation}

我的目标是对所有不同的积分执行此操作.我的python程序目前根本不更改代码,不胜感激!谢谢.

My goal is to do this for all the different integrals. My python program currently does not change the code at all, any help is appreciated! Thank you.

推荐答案

此处有一些问题.

  1. pat.match re.compile 命令中,您可以转义 {} ,这是不必要的,并且转义的反斜杠也不够.如所写,字符串引号内的 \\ f 被理解为 \ f ,然后被re库理解为控制字符.要么放四个反斜杠 \\\\ f ,或者最好使用原始字符串输入(在此处解释):

    In re.compile command you escape {} which is unnecessary, and do not escape backslashes enough. As written, \\f inside string quotes is understood as \f, which then is understood as a control character by re library. Either put four backslashes \\\\f or, better, use raw string input (explained here):

    re.compile(r'\\frac{(.*?)}{\\frac{(.*?)}{(.*?)}}')
    

    1. .format(* match.group())应该是 .format(* match.groups())

    在进行了上述更改之后,它有些起作用:您的第二种切比雪夫"示例的输出为

    With the above changes, it works somewhat: the output for your "Chebyshev of second kind" example is

     \frac{(\ChebyU{\ell}@{x})^2*2}{\pi} 
    

    按预期.

    其他说明:

    • 您的代码目前无法处理多个匹配项
    • 您没有代码将expr替换为原始字符串来代替比赛
    • 在LaTeX中,不是使用星号,而是使用 \ cdot
    • 使用正则表达式解析复杂的数学公式通常不是一个好主意.

    这篇关于Python正则表达式可简化LaTex分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆