
Symbolic computing using SymPy
Symbolic computation manipulates mathematical objects and expressions. These mathematical objects and expressions are represented as they are, and they are not evaluated/approximated. Expressions/objects with unevaluated variables are left in their symbolic form.
Let's see the difference between computerized normal computation and computerized symbolic computation in the following diagram. We have two examples each for both the cases. Example A1 and Example A2 are examples of normal computation, and Example B1 and Example B2 are examples of symbolic computation. Example A1 and Example A2 have obvious output. Let's take a look at the output of Example B1 and Example B2. The output of Example B1 is the same sqrt(3)
. No evaluation is performed; it's only the original symbols. This is because in symbolic computing, if the argument for the sqrt
function is not a perfect square, then it will be left as it is. On the other hand, in Example B2, the output is slightly simpler. The reason is that for this example, it is possible to simplify the answer; sqrt(27)
can be written as sqrt (9 X 3)
or 3(sqrt(3)
, so it is simplified to 3sqrt(3)
.

Comparison of normal computation and symbolic computation
The features of SymPy
As it is a symbolic computation library, SymPy has the ability to perform all types of computations symbolically. It can simplify expressions (as we have seen for sqrt(8)
); compute differentiation, integration, and limits; and solve equations, matrix operations, and various other mathematical functions. All of these functionalities are performed symbolically.
Let's discuss the various features of SymPy. The SymPy library is composed of core capabilities and a number of optional modules. The following are the functionalities supported by SymPy:
- Core capabilities such as basic arithmetic and simplification, and pattern matching functions such as trigonometric, hyperbolic, exponential, logarithms, and many more
- It supports polynomial operations, for example, basic arithmetic, factorization, and various other operations
- The calculus functionality, for example, limits, differentiation, integration, and more
- Solving various types of equations, for example, polynomials, systems of equations, and differential equations
- Discrete mathematics
- The functionality of matrix representations and operations
- Geometric functions
- Plotting with the help of an external pyglet module
- Support for physics
- Performing statistical operations, such as probability and distributions
- Various printing functionalities
- Code generation for programming languages and LaTeX
Why SymPy?
SymPy is an open source library and is licensed under the liberal BSD license. You are allowed to modify the source code. This is not the case with other alternatives, such as Maple and Mathematica. Another advantage of SymPy is that it is designed, developed, and executed in Python. For a Python developer, this brings an added advantage. This library is highly extensible in comparison to alternative tools.
The plotting library
The chart plotting library of Python is named matplotlib. It provides an object-oriented API for addition of charts in an application developed using various Python GUI toolkits. SciPy/NumPy uses matplotlib to draw 2D charts of arrays. The design philosophy behind matplotlib is to simplify the plotting functionality. The user can easily create various types of plots using few function calls, or only one function call. There are some specialized toolkits/APIs that extend the functionality of matplotlib. Some of these tools are bundled with matplotlib, and others are available as separate downloads. Some of them are listed here:
- Basemap is a map plotting toolkit
- The Cartopy package is used to easily make drawing maps for data analysis and visualization
- Excel tools supports exchange of data with Microsoft Excel
- Interfaces for Qt and GTK+
- mplot3d can be used to draw 3D plots
The various types of the charts that can be plotted using matplotlib are given in the following table. These screenshots of charts have been taken from the matplotlib web page at http://matplotlib.org/users/screenshots.html:
