The function init_printing() will enable LaTeX pretty printing in the notebook for SymPy expressions. We recommend calling it at the top of any notebook that uses SymPy.
In [ ]:
    
from sympy import *
init_printing()
    
SymPy symbols are created with the symbols() function. SymPy expressions are built up from symbols, numbers, and SymPy functions,
In [ ]:
    
x, y, z = symbols('x y z')
    
SymPy automatically pretty prints symbols with greek letters and subscripts.
In [ ]:
    
alpha1, omega_2 = symbols('alpha1 omega_2')
alpha1, omega_2
    
In [ ]:
    
sin(x + 1) - cos(y)
    
Write a symbolic expression for $$\frac{1}{\sqrt{2\pi\sigma^2} } \; e^{ -\frac{(x-\mu)^2}{2\sigma^2} }.$$ Remember that the function for $e^x$ is exp(x). You will need to create symbols for sigma and mu. Square root is sqrt.
In [ ]:
    
# Write your answer here
    
Dividing two integers in Python creates a float, like 1/2 -> 0.5. If you want a rational number, use Rational(1, 2) or S(1)/2.
In [ ]:
    
x + 1/2
    
In [ ]:
    
x + S(1)/2
    
^ is the XOR operator. Use ** for powers.
In [ ]:
    
x ^ y
    
In [ ]:
    
x ** y
    
All SymPy expressions are immutable. Functions that operate on an expression return a new expression.
In [ ]:
    
expr = x + 1
expr
    
In [ ]:
    
expr.subs(x, 2)
    
In [ ]:
    
expr
    
In [ ]:
    
sqrt(2)
    
In [ ]:
    
sqrt(2).evalf(7)
    
In [ ]:
    
# Write your answer here
    
In [ ]:
    
f = Function('f')
    
In [ ]:
    
f(x) + 1
    
In [ ]:
    
diff(sin(x + 1)*cos(y), x, y)
    
In [ ]:
    
diff(f(x) + 1, x)
    
Write an expression representing the wave equation in one dimension: $${\partial^2 u\over \partial t^2 } = c^2 { \partial^2 u\over \partial  x^2}.$$ Remember that $u$ is a function in two variables. You can represent an equation using Eq, like
In [ ]:
    
Eq(x, y)
    
In [ ]:
    
# Write your answer here
    
In [ ]:
    
Matrix([[1, 2], [3, 4]])
    
In [ ]:
    
# An unnested list will create a column vector
Matrix([1, 2, 3])
    
In [ ]:
    
Matrix([x, y, z])
    
Matrices support all common operations, and have many methods for performing operations.
In [ ]:
    
Matrix([[1, 2], [3, 4]])*Matrix([x, y])
    
for example, calculating the Jacobian matrix is as easy as:
In [ ]:
    
Matrix([sin(x) + y, cos(y) + x, z]).jacobian([x, y, z])
    
and for those of you who don't remember, the Jacobian is defined as:
$$ J = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \cdots \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & ~ \\ \vdots & ~ & \ddots \\ \end{bmatrix} $$you will come across this mathematical entity in later notebooks in this tutorial.
Create the following matrix $$\left[\begin{matrix}1 & 0 & 1\\-1 & 2 & 3\\1 & 2 & 3\end{matrix}\right]$$
In [ ]:
    
# Write your answer here
    
Now create a matrix representing $$\left[\begin{matrix}x\\y\\z\end{matrix}\right]$$ and multiply it with the previous matrix to get $$\left[\begin{matrix}x + z\\- x + 2 y + 3 z\\x + 2 y + 3 z\end{matrix}\right].$$
In [ ]:
    
# Write your answer here
    
Now take the Jacobian of that matrix with respect to your column vector, to get the original matrix back.
In [ ]:
    
# Write your answer here
    
In [ ]:
    
n, m = symbols('n m', integer=True)
M = MatrixSymbol("M", n, m)
b = MatrixSymbol("b", m, 1)
    
In [ ]:
    
M*b
    
In [ ]:
    
(M*b).shape
    
Some matrix expression functions do not evaluate unless you call doit.
In [ ]:
    
Transpose(M*b)
    
In [ ]:
    
Transpose(M*b).doit()
    
Write a matrix expression representing $$Au + Bv,$$ where $A$ and $B$ are $100\times 100$ and $u$ and $v$ are $100 \times 1$.
In [ ]:
    
# Write your answer here
    
In [ ]:
    
A = IndexedBase("A")
i = Idx('i')
    
In [ ]:
    
A[i]
    
Write an Indexed expression for $$A[i, j, k]$$
In [ ]:
    
# Write your answer here
    
A useful tool in your toolbelt when manipulating expressions is the solve function. solve solves equations symbolically (not numerically). The return value is a list of solutions.
You can give solve an Eq, or if you give it an expression, it automatically assumes that it is equal to 0.
In [ ]:
    
solve(Eq(x**2, 4), x)
    
In [ ]:
    
solve(x**2 + 3*x - 3, x)
    
It can also handle systems of equations. The return is a list of dictionaries, mapping symbols to solutions.
In [ ]:
    
eq1 = x**2 + y**2 - 4  # circle of radius 2
eq2 = 2*x + y - 1  # straight line: y(x) = -2*x + 1
solve([eq1, eq2], [x, y])
    
Solve the following system of equations: $$\begin{align}z &= x^2 - y^2\\z^2 &= x^2 + y^2 + 4\\z &= x + y\end{align}$$
In [ ]:
    
# Write your answer here
    
In [ ]:
    
f = Function('f')
    
In [ ]:
    
dsolve(f(x).diff(x, 2) + f(x))
    
Solve the following ODE: $$f''(x) + 2f'(x) + f(x) = \sin(x)$$
In [ ]:
    
# Write your answer here