# Introduction

This notebook gives examples of operations that can be performed using the **Numpy** and **Sympy** module. The documentation for **Sympy** can be found [here](https://docs.sympy.org/latest/index.html).
A drafting space is available at the end of the notebook


```python
import numpy as np
from sympy import *
```


```python
init_printing()  # Init pretty print as default

X = symbols('X') # Use for polynomials variables
x = symbols('x') # Declare new variable
y = symbols('y')
a = symbols('a', commutative=False)
b = symbols('b', commutative=False) 
a = symbols('a')
b = symbols('b') 
c = symbols('c')
d = symbols('d') 
```

# Equations

Solve system $$\begin{cases} x + 5y -2 = 0 \\ -3x + 6y - 15 = 0 \end{cases}$$


```python
solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
```

# Integration


```python
integrate(x ** 2 + x + 1, x)  # Give the primitive
```


```python
integrate(x ** 2 + x + 1, (x, 0, 1))  # Give the value of the integral
```

# Matrix
The reference of **Sympy** is [here](http://docs.sympy.org/latest/modules/matrices/matrices.html). See also this [tutorial](https://docs.sympy.org/latest/tutorial/matrices.html) for a quick introduction to **Sympy** matrices.

### Basics


```python
n, m = 3, 4 # Used later for some matrices
```


```python
M = Matrix([
    [a, b],
    [c, d]
])

U = Matrix([
    [x],
    [y]
])

M * U
```


```python
U = Matrix([[x, y]])

U * M
```


```python
CNOT = Matrix([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]
])

H = 1/np.sqrt(2) * np.matrix([
    [1, 1],
    [1, -1]
])

psi = 1/np.sqrt(2) * np.matrix([[1], [1]])
```


```python
CNOT * Matrix([0, 1, a, b])
```


```python
ket = H.dot(psi)
ket
```




    matrix([[1.],
            [0.]])




```python
bra = ket.transpose()
bra
```




    matrix([[1., 0.]])



### Special matrices


```python
I_3 = eye(n) # Identity matrix
I_3
```


```python
M, M2 = zeros(2, 3), zeros(n) # Zero matrix
M, M2
```


```python
M, M2 = ones(1, 2), ones(n) # Matrix of ones
M, M2
```

# Reduction 


```python
M = Matrix([[1, 1], [1, 1]])
M
```


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


```python
P * D * P.inv()
```


```python
M.eigenvals(), M.eigenvects(), M.charpoly(x) # Eigenvalues, associated eigenvectors and characteristic polynomial
```


```python
np.linalg.eig(H) # Eigenvalues and associated eigenvectors of the Hadamard gate
```




    (array([ 1., -1.]), matrix([[ 0.92387953, -0.38268343],
             [ 0.38268343,  0.92387953]]))



# Polynomials


```python
P = M.charpoly(x)
P, P.factor()
```

# Draft space


```python
rho = Matrix([
    [Rational('1/2'), Rational('-1/6')],
    [Rational('-1/6'), Rational('1/2')]
])
rho.eigenvals(), rho.eigenvects(), rho.charpoly(X)
```


```python
colors = ["r", "g", "b"]
n = ["red", "green", "blue"]

m = {key: color for key,color in zip(colors, n)}
m
```




    {'r': 'red', 'g': 'green', 'b': 'blue'}



dd88ddd143224022e131bfa955714357b525566a
ipynb
Jupyter Notebook
maths/maths-demo.ipynb
greglan/python_scripts
f2e98ed3fd975d79b0a6b569b65c850a7f4f3ab3
MIT
maths/maths-demo.ipynb
greglan/python_scripts
f2e98ed3fd975d79b0a6b569b65c850a7f4f3ab3
MIT
maths/maths-demo.ipynb
greglan/python_scripts
f2e98ed3fd975d79b0a6b569b65c850a7f4f3ab3
MIT
Qwen/Qwen-72B
1. YES
2. YES