# SymPy Tutorial

*Arthur Ryman* <br/>
*Last Updated: 2020-04-16*


This notebook contains the examples from the 
[SymPy Tutorial](https://docs.sympy.org/latest/tutorial/index.html).

## Matrices


```python
from sympy import *
init_printing(use_unicode=True)
```

### simplify


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


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


```python
M = Matrix([[1, 2, 3], [3, 2, 1]])
N = Matrix([0, 1, 1])
M*N
```

### Basic Operations

#### Shape


```python
M = Matrix([[1, 2, 3], [-2, 0, 4]])
M
```


```python
M.shape
```

#### Accessing Rows and Columns


```python
M.row(0)
```


```python
M.col(-1)
```

#### Deleting and Inserting Rows and Columns


```python
M.col_del(0)
M
```


```python
M.row_del(1)
M
```


```python
M
```


```python
M = M.row_insert(1, Matrix([[0, 4]]))
M
```


```python
M = M.col_insert(0, Matrix([1, -2]))
M
```

### Basic Methods


```python
M = Matrix([[1, 3], [-2, 3]])
N = Matrix([[0, 3], [0, 7]])
M + N
```


```python
M*N
```


```python
3*M
```


```python
M**2
```


```python
M**-1
```


```python
try:
    N**-1
except Exception as ex:
    print(type(ex))
    print(ex)
```

    <class 'sympy.matrices.common.NonInvertibleMatrixError'>
    Matrix det == 0; not invertible.



```python
M = Matrix([[1, 2, 3], [4, 5, 6]])
M
```


```python
M.T
```

### Matrix Constructors


```python
eye(3)
```


```python
eye(4)
```


```python
zeros(2, 3)
```


```python
ones(3, 2)
```


```python
diag(1, 2, 3)
```


```python
diag(-1, ones(2, 2), Matrix([5, 7, 5]))
```

### Advanced Methods

#### Determinant


```python
M = Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]])
M
```


```python
M.det()
```

#### RREF


```python
M = Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]])
M
```


```python
M.rref()
```

#### Nullspace


```python
M = Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]])
M
```


```python
M.nullspace()
```

#### Columnspace


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


```python
M.columnspace()
```

#### Eigenvalues, Eigenvectors, and Diagonalization


```python
M = Matrix([\
           [3, -2, 4, -2],\
           [5, 3, -3, -2],\
           [5, -2, 2, -2],\
           [5, -2, -3, 3]])
M
```


```python
M.eigenvals()
```


```python
M.eigenvects()
```


```python
P, D = M.diagonalize()
P
```


```python
D
```


```python
P*D*P**-1 == M
```




    True




```python
lamda = symbols('lamda')
p = M.charpoly(lamda)
factor(p)
```

### Possible Issues

#### Zero Testing


```python
from sympy import *
q = Symbol('q', positive=True)
m = Matrix([
    [-2*cosh(q/3),      exp(-q),            1],
    [      exp(q), -2*cosh(q/3),            1],
    [           1,            1, -2*cosh(q/3)]])
m.nullspace()
```


```python
import warnings

def my_iszero(x):
    try:
        result = x.is_zero
    except AttributeError:
        result = None
        
    # Warnings if evaluated into None
    if result is None:
        warnings.warn('Zero testing of {} evaluated into None'.format(x))
    return result

m.nullspace(iszerofunc=my_iszero)
```


```python
def my_iszero(x):
    try:
        result = x.rewrite(exp).simplify().is_zero
    except AttributeError:
        result = None
        
    # Warnings of evaluated into None
    if result is None:
        warnings.warn('Zero testing of {} evaluated into None'.format(x))
    return result

m.nullspace(iszerofunc=my_iszero)
```

1144e766bf6d14a75bacea4e0f5fceb3012e545d
ipynb
Jupyter Notebook
notes/docs.sympy.org/tutorial/matrices.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
notes/docs.sympy.org/tutorial/matrices.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
2022-01-21T22:17:28.000Z
2022-01-21T22:17:28.000Z
notes/docs.sympy.org/tutorial/matrices.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
Qwen/Qwen-72B
1. YES
2. YES