After executing this directive, the variable Pi will represent the symbolic value of π, not precisely its numerical value. As a result, if we now perform the print (Pi) directive, we will obtain pi instead of its numerical value. Furthermore, the print (Pi**2) directive will give pi**2. Notice that we have incorporated symbolic mathematics into our programs.

## 11.1 Analytical Expressions and Symbols

*x*

^{2}+ 5 +

*x*+ 1 is shown in Listing 11-1.

Code for the 2*x*^{²} + 5 + *x* + 1 Equation

creates Q as a symbolic expression. It is worth noticing in this code that you don’t have to declare Q as a symbolic variable.

Up to this point, we have learned that Q is not the usual numerical function that would allow us to calculate the numerical values of our expressions, but rather is a *symbolic representation* of an algebraic equation.

Incorrect Way to Obtain Numerical Q Values

*π*. However, in this case, we will obtain several errors, including this one:

With this outcome, we should now realize that the variable Pi in the first example holds only one value. In contrast, the Q variable is a function of x, which means it can take an infinite set of values.

This way, we can obtain numerical values of our symbolic Q equation. However, this is not exactly what we want, as we are looking for a numerical function.

Converting a Symbolic Function to a Numeric Function

assigns to the variable R the properties of a typical numerical function according to our symbolic Q equation. Therefore, R becomes a numerical function, and we can proceed to calculate values of it. For example, R(1) gives the value of the equation at x=1. The previous code prints the requested result to the screen, which in this case is 9.

## 11.2 Declaring Functions with Analytical Expressions

Splitting a Symbolic Expression Into Several Code Lines

Example of a Symbolic Expression with Several Lines of Code

creates the numerical function R corresponding to the algebraic expression. R now allows numerical evaluation.

After executing this code, print( R(-2) ) will give the value -19956.25. This is the correct value of our symbolic equation, evaluated at x=-2.

## 11.3 Solving Equations

*ax*+

*b*= 0, where

*a*and

*b*are constant terms.

Code for Solving *ax* + *b* = 0 Symbolically

In this code, x, a, and b are symbolic variables. After running the code, the program will give the result [-b/a], which is correct.

*ax*

^{²}+

*bx*+

*c*= 0.

Symbolically Solving *ax*^{²} + *bx* + *c* = 0

Alternative Code for Solving *ax*^{²} + *bx* + *c* = 0

*ax*

^{³}+

*bx*

^{²}+

*cx*+

*d*= 0. The corresponding code is shown in Listing 11-9.

Symbolically Solving *ax*^{³} + *bx*^{²} + *cx* + *d* = 0

Printing Numerical Values to the Symbolic Solution of *ax*^{³} + *bx*^{²} + *cx* + *d* = 0 for Specific Values

Note that the first root is a real number, while the other two solutions are complex, accomplished using the complex conjugate roots theorem. The parameter j represents the imaginary unit.

Verifying Specific Solutions for *ax*^{³} + *bx*^{²} + *cx* + *d* = 0

Each calculated root should satisfy the cubic equation, rigorously speaking, so the three values should be 0. Although they are not precisely equal to 0, the three numerical values are, for practical purposes, negligible (in the order of 10^{-16}). This confirms that, within the numerical precision of the processor, these three roots are indeed solutions of the cubic equation.

Finally, let’s investigate if SymPy can solve the algebraic quartic equation written in the form *ax*^{4} + *bx*^{3} + *cx*^{2} + *dx* + *e* = 0.

Symbolically Solving *ax*^{4} + *bx*^{3} + *cx*^{2} + *dx* + *e* = 0

We can print the symbolic results by double-clicking each one of these messages. However, the results are lengthy. Therefore, as in the previous case, we need to verify their correctness by testing particular values.

## 11.4 Solving Simultaneous Equations

Symbolically Solving Two Simultaneous Linear Equations

As the solutions are lengthy, we can proceed as in the previous example to test some particular solutions.

## 11.5 Symbolic Differentiation

*x*.

Example of Symbolic Differentiation

Second Symbolic Derivative Example

*x*, and the first and second derivative with respect to the variable

*y*. The code is shown in Listing 11-16.

Symbolic Third Order with Partial Derivatives

## 11.6 Integration

Symbolic Integration Example

It is well known that the result of this integral equals .

Symbolic Improper Integral Calculation

This result confirms the capability of SymPy to calculate improper integrals.

Symbolic Two-Dimensional Improper Integral Calculation

After executing this code, we obtain the result, pi, as expected.

In the following chapters, you will use some SymPy functions. In the next subsection, we describe the procedure to incorporate SymPy into Android projects.

## 11.7 Incorporating SymPy Into Android Projects

Now you learn how to incorporate SymPy into your Android projects. At first glance, you might think that it will suffice to add SymPy to the end of the requirements line in the buildozer.spec file, in the same manner as you did with NumPy and Pillow. However, if you do so, Buildozer will fail in the conversion process. Among the list of errors, you will see: "mpmath with version larger or equal to 0.19 is required."

To verify if mpath is installed on your computer, open a Terminal window and type pip3 list. In our case, we had mpmath version 1.1.0.

**,**instead of SymPy

**,**to the row that corresponds to requirements in the buildozer.spec file, as follows:

In this path, the term ComputerName has to be replaced with the name you assigned to your computer when Ubuntu was installed.

The source code we are looking for corresponds to the folder named sympy. Therefore, you must copy this folder into your working one.

In the following chapters, you will need SymPy.

## 11.8 Summary

In this chapter, we introduced SymPy to symbolically access special mathematical functions and polynomials. We described how to incorporate SymPy into your projects, and we described its functionality by using working examples.