<a href="https://colab.research.google.com/github/tofighi/Linear-Algebra/blob/main/Matrix_Operations.ipynb" target="_parent"></a>

# Differences in Matrix Representation - SymPy vs. NumPy
SymPy stores entries in 1D arrays, while NumPy stores entries in 2D arrays.


```python
import sympy as sp 
import numpy as np
sp.init_printing(use_unicode=True)
```


```python
#Sympy version of our demo matrix
As = sp.Matrix([[1,2,3], [4,5,6]])
As
```




$\displaystyle \left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\end{matrix}\right]$




```python
#Numpy version of our demo matrix - Libraries are inter-compatible
An = np. array(As)
An
```




    array([[1, 2, 3],
           [4, 5, 6]], dtype=object)




```python
#We can't obtain the dimensions of the SymPy Matrix easily
len(As)
```


```python
#We can obtain the size of the NumPy Matrix:
size= [len(An), len(An[0])]
size
```


```python
#We can also obtain the size of the NumPy Matrix using shape property
An.shape
```


```python
An
```




    array([[1, 2, 3],
           [4, 5, 6]], dtype=object)




```python
An+1
```




    array([[2, 3, 4],
           [5, 6, 7]], dtype=object)




```python
As.T
```




$\displaystyle \left[\begin{matrix}1 & 4\\2 & 5\\3 & 6\end{matrix}\right]$




```python
An.T
```




    array([[1, 4],
           [2, 5],
           [3, 6]], dtype=object)




```python
3*As
```




$\displaystyle \left[\begin{matrix}3 & 6 & 9\\12 & 15 & 18\end{matrix}\right]$




```python
As/2
```




$\displaystyle \left[\begin{matrix}\frac{1}{2} & 1 & \frac{3}{2}\\2 & \frac{5}{2} & 3\end{matrix}\right]$




```python
Bs = 3*As
Bs
```




$\displaystyle \left[\begin{matrix}3 & 6 & 9\\12 & 15 & 18\end{matrix}\right]$




```python
#As is 2x3 Bs^T is 3x2, so the product is 2x2 ...
As*Bs.T
```




$\displaystyle \left[\begin{matrix}42 & 96\\96 & 231\end{matrix}\right]$




```python
#Try the following code, it will generate ShapeError: Matrix size mismatch: (2, 3) * (2, 3).
#As*Bs
```


```python
# You can use Numpy library as well
Bn = 3*An
Bn
```




    array([[3, 6, 9],
           [12, 15, 18]], dtype=object)




```python
# we use dot method in Numpy for Matrix multiplications
np.dot(An, Bn. T)
```




    array([[42, 96],
           [96, 231]], dtype=object)




```python
# if you use * for multiplication in Numpy, it is element-wise, it is NOT a matrix product
An*Bn
```




    array([[3, 12, 27],
           [48, 75, 108]], dtype=object)



# Matrix Inverse


```python
x = np.array([[1,2],[3,4]]) 
y = np.linalg.inv(x) 
print (x) 
print (y) 
print (np.dot(x,y))
```

    [[1 2]
     [3 4]]
    [[-2.   1. ]
     [ 1.5 -0.5]]
    [[1.0000000e+00 0.0000000e+00]
     [8.8817842e-16 1.0000000e+00]]


58f0489583d1829cf61c6de0de5e8b3eb11d8cd4
ipynb
Jupyter Notebook
Matrix_Operations.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Matrix_Operations.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Matrix_Operations.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES