# manual translation process from Latex to Sympy for Physics Derivation Graph

Published 2020-07-29T19:39:00Z by Physics Derivation Graph

Suppose I have a Latex string like

1 = \\int_0^W a \\sin\\left(\\frac{n \\pi}{W} x\\right) \\psi(x)^* dx

The first step is to eliminate notation that is presentation oriented

1 = \\int_0^W a \\sin(\\frac{n \\pi}{W} x) \\psi(x)^* dx

Second, rewrite multi-character variables to single variable

>>> import sympy
>>> sympy.__version__
'1.5.1'
>>> from sympy.parsing.latex import parse_latex
>>> sympy.srepr(parse_latex('1 = \int_0^W a \sin(\\frac{n \pi}{W} x) f(x)^* dx'))
"Equality(Integer(1), Integral(Mul(Symbol('a'), Mul(Function('f')(Symbol('x')), sin(Mul(Symbol('x'), Mul(Pow(Symbol('W'), Integer(-1)), Mul(Symbol('n'), Symbol('pi'))))))), Tuple(Symbol('x'), Integer(0), Symbol('W'))))"

Note that the complex conjugate operation was dropped without warning. We'll need to manually insert that later.

The SymPy representation is challenging to review, so it can be transformed back to Latex:
>>> sympy.latex((parse_latex('1 = \int_0^W a \sin(\\frac{n \pi}{W} x) f(x)^* dx')))
'1 = \\int\\limits_{0}^{W} a f{\\left(x \\right)} \\sin{\\left(x \\frac{n \\pi}{W} \\right)}\\, dx'
which shows that the input Latex does not get transformed exactly, but close enough for human verification.

From the single-variable representation, replace variables with PDG symbol IDs.
W --> 2523
a --> ?
n --> 1592
pi --> 3141
x --> ?
f --> psi --> 9489
In the context of an isolated function, I don't know which "a" and which "x" are relevant.
To determine those IDs I would need to review the derivation.

which, replacing all instances, yields
Equality(Integer(1), Integral(Mul(Symbol('a'), Mul(Function('pdg9489')(Symbol('x')), sin(Mul(Symbol('x'), Mul(Pow(Symbol('pdg2523'), Integer(-1)), Mul(Symbol('pdg1592'), Symbol('pdg3141'))))))), Tuple(Symbol('x'), Integer(0), Symbol('pdg2523'))))

Finally, insert the "conjugate" for \psi(x)
Equality(Integer(1), Integral(Mul(Symbol('a'), Mul(conjugate(Function('pdg9489')(Symbol('x'))), sin(Mul(Symbol('x'), Mul(Pow(Symbol('pdg2523'), Integer(-1)), Mul(Symbol('pdg1592'), Symbol('pdg3141'))))))), Tuple(Symbol('x'), Integer(0), Symbol('pdg2523'))))