Symbolic Analysis with SymPy Exercises


Instructions: Create a new notebook called SymPyExercises in your SymPy directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.

Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title Symbolic Analysis with SymPy Exercises. Then include the inline functions libraries by adding a code cell that invokes the %pylab inline magic and imports the needed packages.



In [ ]:
from IPython.html.widgets import interact
from IPython.display import display

In [ ]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

In [ ]:
from sympy import *
init_printing(use_latex='mathjax')

Question 1

This question looks at a class of integrals that appears often in physics, the so called Gaussian integrals of the form:

$$ I_n(a) = \int_{-\infty}^{\infty} z^n e^{-a z^2} dz $$

(a) Define SymPy symbols for the following variables:

  • z that is real
  • a that is real and positive
  • n that is a positive integer

In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

(b). Compute the integral $I_0(a)$ symbolically by passing the full integrand to the integrate function. Save the result as variable I0 and display it using IPython.display.display.


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

(c) Verify that $I_m(a)=0$ if $m$ is odd by computing the integral symbolically for m=[1,3,5,7,9]. Use a loop or other control structure to interate through values of $m$. Why should you not use the variable n for this part?


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

(d) Define a symbolic expression, saved under the variable In, for the unevaluated integral $I_n(a)$ using SymPy's Integral class. Use symbolic manipulations to verify the following identity:

$$ I_2(a) = - \frac{\partial I_0}{\partial a} $$

It may be helpful to use Eq(lhs,rhs).doit() to verify mathematical equality.


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

Question 2

(a) Define and display the following matrix symbolically, where $a$ is a positive real number:

$$ H = \left[\begin{matrix}- a + 1 & 0 & 0\\0 & 1 & a\\0 & a & 2\end{matrix}\right] $$

In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

(b) Find the eigenvalues of $H$ and store them in a list.


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

(c) Write a Python function with the signature shown below. This function should return two NumPy arrays, which, when passed to the matplotlib.pyplot.plot function will plot the expression as a function of the symbol over some range.

def expr_to_data(expr, lim, n=100):
    """Evaluate an expression numerically over a range.

    Parameters
    ----------
    expr : Expr
        A SymPy expression containing one free symbol, call it f(x).
    lim : (x, xmin, xmax)
        A 3-tuple of the symbol to use as the x variable and its xmin and
        xmax values for numerical evaluation. In your function you can call
        these "x", but the user will be able to pass any symbol.
    n : int
        The number of points to use in evaluating the expression between
        xmin and xmax.

    Returns
    -------
    A 2-tuple of NumPy arrays (x, f(x)), which, when passed to
    matplotlib's plot function will plot the expression as a function of the
    symbol.
    """

Use SymPy's lambdify function with modules='numpy' to build a function that can be used to evaluate the expression numerically. You will also have to perform the following logic on xmin and xmax:

  • First convert to a SymPy object using sympify.
  • Then produce a numerical value using N/evalf.
  • Finally convert to a Python float using float.

When working with SymPy and regular Python numbers at the same time, it is important to keep track of what types you have. You can always show the type using Python's type function.


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

Here are a few assert statements you can run to test your function:


In [ ]:
p = Symbol('p')
assert len(expr_to_data(p, (p,0,1), n=10)) == 2
assert len(expr_to_data(p, (p,0,1), n=10)[0]) == 10 and len(expr_to_data(p, (p,0,1), n=10)[1]) == 10
assert np.all(expr_to_data(p, (p,0,1), n=10)[1] == np.linspace(0,1,10))
assert np.all(expr_to_data(p**2, (p,0,1), n=10)[1] == np.linspace(0,1,10)**2)

(d) Use your expr_to_data function to generate data that can be used to plot the above eigenvalues as functions of the parameter a over the range [0,1]. The plots of all three eigenvalues should appear on the same plot.


In [ ]:
# Copy the exercise statement to a markdown cell in your notebook
# and then implement your solution in a code cell here.

All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.