```python
from scipy import linalg as sla
import numpy as np
import sympy as sy
sy.init_printing()
import matplotlib.pyplot as plt
```


```python
A=sy.Matrix([[3,-2],[-3,5]])
A
```


```python
A.rank()
```


```python
A.condition_number().simplify()
```


```python
sy.N(_)
```


```python
A.norm()
```

# confirm matrix $A$ property using numpy


```python
A=np.array([[3,2],[-3,5]])
```


```python
np.linalg.matrix_rank(A)
```




    2




```python
np.linalg.cond(A)
```


```python
np.linalg.norm(A)
```

# solve linear equation $A\mathbf{x}=\mathbf{b}$ using sympy


```python
A=sy.Matrix([[3,2],[-3,5]])
b=sy.Matrix([8,-1])
A,b
```


```python
x=A.inv()@b
x
```


```python
x=A.inv()*b
x
```


```python
#check solve
b_=A*x
b_
```


```python
#you can also express as follow using solve
x=A.solve(b)
x
```

# LU-decomposition


```python
A=sy.Matrix([[3,2],[3,-5]])
A
```


```python
L,U,_=A.LUdecomposition()
L,U,_
```


```python
#check A=LU
L*U
```


```python
%timeit x=A.solve(b)
```

    100 loops, best of 3: 844 µs per loop



```python
%timeit x=A.LUsolve(b)
```

    1000 loops, best of 3: 293 µs per loop


# solve linear eq $A\mathbf{x}=\mathbf{b}$ using numpy and scipy


```python
A=np.array([[3,2],[-3,5]])
b=np.array([[8,-1]]).T
x=sla.solve(A,b)
print(x)
```

    [[ 2.]
     [ 1.]]



```python
#check
A @ x-b
```




    array([[ 0.],
           [ 0.]])




```python
P,L,U=sla.lu(A)
print(P)
print(L)
print(U)
```

    [[ 1.  0.]
     [ 0.  1.]]
    [[ 1.  0.]
     [-1.  1.]]
    [[ 3.  2.]
     [ 0.  7.]]



```python
#check PLU=A 
P @ L @ U
```




    array([[ 3.,  2.],
           [-3.,  5.]])




```python
LU=sla.lu_factor(A)
LU
```




    (array([[ 3.,  2.],
            [-1.,  7.]]), array([0, 1], dtype=int32))




```python
x=sla.lu_solve(LU,b)
x
```




    array([[ 2.],
           [ 1.]])




```python

```

f0c0f2093a48d28a38586455a79eb7cc46ec4517
ipynb
Jupyter Notebook
sympyExercise/solve-lineareq.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
sympyExercise/solve-lineareq.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
sympyExercise/solve-lineareq.ipynb
terasakisatoshi/pythonCodes
baee095ecee96f6b5ec6431267cdc6c40512a542
MIT
Qwen/Qwen-72B
1. YES
2. YES