# Intro to SymPy 

Here we give a (quick) introduction to SymPy. This tutorial assumes you are already familiar with SymPy expressions, so this notebook should serve as a refresher.

## Expressions

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.


```python
from sympy import *
init_printing()
```

SymPy symbols are created with the `symbols()` function. SymPy expressions are built up from symbols, numbers, and SymPy functions, 


```python
x, y, z = symbols('x y z')
```

SymPy automatically pretty prints symbols with greek letters and subscripts.


```python
alpha1, omega_2 = symbols('alpha1 omega_2')
alpha1, omega_2
```


```python
sin(x + 1) - cos(y)
```

### Exercise

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`.


```python
# Write your answer here

```

## Some reminders on common gotchas:

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`. 


```python
x + 1/2
```


```python
x + S(1)/2
```

`^` is the XOR operator. Use `**` for powers. 


```python
x ^ y
```


```python
x ** y
```

All SymPy expressions are immutable. Functions that operate on an expression return a new expression.


```python
expr = x + 1
expr
```


```python
expr.subs(x, 2)
```


```python
expr
```

## Floating point representation

Since most languages targeted will not support symbolic representation it is useful to let SymPy evaluate a floating point approximation (up to a user specified number of digits).


```python
sqrt(2)
```


```python
sqrt(2).evalf(7)
```

### Exercise
Compute 100 digits of $\pi$.


```python
# Write your answer here

```

## Undefined functions

Undefined functions are created with `Function()`. Undefined are useful to state that one variable depends on another (for the purposes of differentiation).


```python
f = Function('f')
```


```python
f(x) + 1
```

## Derivatives

Derivatives are computed with the `diff()` function, using the syntax `diff(expr, var1, var2, ...)`.


```python
diff(sin(x + 1)*cos(y), x, y)
```


```python
diff(f(x) + 1, x)
```

### Exercise

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


```python
Eq(x, y)
```


```python
# Write your answer here

```

## Matrices
Matrices are created with `Matrix`. A matrix can contain any symbolic expression. See `Matrix?` for different ways to create a Matrix.


```python
Matrix([[1, 2], [3, 4]])
```


```python
# An unnested list will create a column vector
Matrix([1, 2, 3])
```


```python
Matrix([x, y, z])
```

Matrices support all common operations, and have many methods for performing operations.


```python
Matrix([[1, 2], [3, 4]])*Matrix([x, y])
```

for example, calculating the Jacobian matrix is as easy as:


```python
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.

### Exercise

Create the following matrix $$\left[\begin{matrix}1 & 0 & 1\\-1 & 2 & 3\\1 & 2 & 3\end{matrix}\right]$$


```python
# 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].$$




```python
# Write your answer here

```

Now take the Jacobian of that matrix with respect to your column vector, to get the original matrix back.


```python
# Write your answer here

```

## Matrix symbols

SymPy can also operate on matrices of symbolic dimension ($n \times m$). `MatrixSymbol("M", n, m)` creates a matrix $M$ of shape $n \times m$. 


```python
n, m = symbols('n m', integer=True)
M = MatrixSymbol("M", n, m)
b = MatrixSymbol("b", m, 1)
```


```python
M*b
```


```python
(M*b).shape
```

Some matrix expression functions do not evaluate unless you call `doit`.


```python
Transpose(M*b)
```


```python
Transpose(M*b).doit()
```

### Exercise

Write a matrix expression representing $$Au + Bv,$$ where $A$ and $B$ are $100\times 100$ and $u$ and $v$ are $100 \times 1$.


```python
# Write your answer here

```

## Indexed
Indexed symbols can be created with `IndexedBase` and `Idx`. `IndexedBase("A")` represents an array `A` and `Idx('i')` represents an index `i`. You can also use `symbols('i')` instead of `Idx('i')`.


```python
A = IndexedBase("A")
i = Idx('i')
```


```python
A[i]
```

### Exercise

Write an Indexed expression for $$A[i, j, k]$$


```python
# Write your answer here

```

## Solving systems of equations

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.


```python
solve(Eq(x**2, 4), x)
```


```python
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.


```python
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])
```

### Exercises 

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}$$


```python
# Write your answer here

```

## Solving differential equations
If you are dealing with a differential equation, say:

$$
f''(x) = -f(x)
$$

SymPy's dsolve can (sometimes) produce an exact symbolic solution. Like `solve`, `dsolve` assumes that expressions are equal to 0. 


```python
f = Function('f')
```


```python
dsolve(f(x).diff(x, 2) + f(x))
```

### Exercise

Solve the following ODE: $$f''(x) + 2f'(x) + f(x) = \sin(x)$$


```python
# Write your answer here

```

ed67798184be5cd9271a6477178c902460ef07fc
ipynb
Jupyter Notebook
notebooks/01-intro-sympy.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-05-31T21:01:22.000Z
2022-02-14T04:26:01.000Z
notebooks/01-intro-sympy.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-06-06T19:05:04.000Z
2018-08-29T23:15:19.000Z
notebooks/01-intro-sympy.ipynb
gvvynplaine/scipy-2017-codegen-tutorial
4bd0cdb1bdbdc796bb90c08114a00e390b3d3026
BSD-3-Clause
2017-06-06T14:45:12.000Z
2021-09-25T14:11:06.000Z
Qwen/Qwen-72B
1. YES
2. YES