0

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

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}} 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.

Example output:

%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}

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.

zara
  • 375
  • 1
  • 4
  • 18

1 Answers1

1

A few issues here.

  1. pat.match checks only at the beginning of the string. You want pat.search there.

  2. 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()) should be .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} 

as intended.

Additional remarks:

  • Your code doesn't deal with multiple matches at present
  • You have no code for substituting expr back into the original string in place of the match
  • Instead of asterisk, in LaTeX one uses \cdot
  • Parsing complex math formulas with regex is generally a bad idea.
Community
  • 1
  • 1
  • Hello. Sorry for the late response. I am horrible at python, sadly. If you don't mind, can you suggest a way for me to deal with multiple matches and substituting expr back into the original string? It would be greatly appreciated. Also, I am required to use regex for this project. – zara Jul 20 '15 at 15:00