Python Regex简化LaTex分数

时间:2015-06-16 17:12:01

标签: python regex math latex

这是我的python程序:

def fractionSimplifier(content):
    content.replace('\\dfrac','\\frac')
    pat = re.compile('\\frac\{(.*?)\}\{\\frac\{(.*?)\}\{(.*?)\}\}')
    match = pat.match(content)
    expr = ""
    if match:
        expr = '\\frac{{{0}*{2}}}{%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}
}'.format(*match.group())
        #print expr
    return expr

这是应该编辑的LaTex代码。

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

a/(b/c)是我正在寻找的第二类切比雪夫的具体案例。由于它是另一个分数的分母中的一小部分,我想使用Python正则表达式将其从(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}

示例输出:

export function initialize( container, application ) {
    var session = Ember.Object.create({
        user:null,
        authorization:null
    });

    application.register('session:main', session, { instantiate: false });
    application.inject('route', 'session', 'session:main');
    application.inject('controller', 'session', 'session:main');
    application.inject('adapter', 'session', 'session:main');
}

我的目标是为所有不同的积分做到这一点。我的python程序目前根本没有更改代码,任何帮助表示赞赏!谢谢。

1 个答案:

答案 0 :(得分:1)

这里有一些问题。

  1. pat.match checks only at the beginning of the string。你想要pat.search

  2. re.compile命令中,您可以转义{}这是不必要的,并且不要逃避反斜杠。如上所述,字符串引号内的\\f被理解为\f,然后被re库理解为控制字符。要么放四个反斜杠\\\\f,要么更好地使用原始字符串输入(explained here):

  3. re.compile(r'\\frac{(.*?)}{\\frac{(.*?)}{(.*?)}}')
    
    1. .format(*match.group())应为.format(*match.groups())
    2. 通过上述更改,它有点作用:“第二类Chebyshev”示例的输出是

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

      按预期。

      补充说明:

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