Symbolic calculations ===================== Even if we want to deal with numerical computations mainly, I'd like to give a short introduction into symbolic calculation. There are several reasons why we want to include some symbolic manipulations in a numerical code. For example we want to do an error analysis and would like to be able to evaluate the known exact solution on various different grids. Or we have a closed form solution for some right hand side. Beyond the toy examples there exist also industrial strength codes that use symbolic computation in a preparation step before going into the numerics [#]_. In any case, we could put the symbolic expression literally into the source code. But sometimes it would be useful if we could do a few more things with the expression beside numerical evaluation, for example we may need the first derivative. The benefit of symbolic calculation is now that we can compute this derivation without explicitly put it into the source code or falling back to numerical techniques. In the following section I'll show a really simple example of how to use the power of the ``sympy`` python module. This module acts as a library for symbolic calculation and is quite easy to use yet surprisingly powerful for it's complexity. .. literalinclude:: TutCodes/symbolics_demo.py :language: python And this is how the output looks like .. image:: TutCodes/symbolics_demo.png For many other simple examples, please look at the introductory level ``sympy`` tutorial [#]_. Note that you should use the **computationaly expensive symbolical calculations** only in the preparation phase of a numerical simulation program and never in a main computation loop. .. rubric:: Footnotes .. [#] See fore example `SyFi `_ which is part of the FEniCS project http://www.fenicsproject.org/ .. [#] http://docs.sympy.org/latest/tutorial/index.html