Derived formulas are only useful once they reach a paper, a notebook or production code.
SymPy has a family of printers that convert an expression tree to text:
str(e)andprint(e): Python-like syntax thatsympifycan parse back.srepr(e): the exact constructor tree, for debugging.pprint(e)/pretty(e): 2D Unicode or ASCII art.latex(e): LaTeX source for papers and slides, with options likemode="equation"ormul_symbol="dot". Jupyter renders expressions with LaTeX automatically afterinit_printing().
Code printers turn expressions into source code in other languages: ccode, cxxcode, fcode (Fortran), rust_code, jscode, octave_code, and pycode for plain Python. They translate functions and constants (pi becomes M_PI in C), and can assign to a variable with assign_to=. ccode(Piecewise(...)) becomes a ternary or if block.
For whole functions or modules, sympy.utilities.codegen.codegen generates complete C or Fortran source and header files, and autowrap compiles them into Python-callable extensions.
Two practical tips. First, run cse() (common subexpression elimination) before printing large expressions, so repeated parts are computed once. Second, mind numeric behaviour: printers emit literal translations, so a formula that is exact symbolically can still suffer cancellation in floating point.
Example
from sympy import symbols, latex, ccode, pycode, sqrt, sin, pi, Rational, srepr
x, y = symbols("x y")
e = sqrt(x**2 + y**2) * sin(pi * x) / 2
print(latex(e)) # \frac{\sqrt{x^{2} + y^{2}} \sin{\left(\pi x \right)}}{2}
print(ccode(e)) # (1.0/2.0)*sqrt(pow(x, 2) + pow(y, 2))*sin(M_PI*x)
print(ccode(e, assign_to="r"))
# r = (1.0/2.0)*sqrt(pow(x, 2) + pow(y, 2))*sin(M_PI*x);
print(pycode(e)) # (1/2)*math.sqrt(x**2 + y**2)*math.sin(math.pi*x)
print(srepr(Rational(1, 2) * x)) # Mul(Rational(1, 2), Symbol('x'))Remember
- latex() for documents; pprint() for terminals; srepr() for debugging
- ccode, fcode, pycode and friends emit source in other languages
- Apply cse() before generating code from large expressions