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

# What is Symbolic Computation?
Symbolic computation deals with the computation of mathematical objects symbolically. This means that the mathematical objects are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form.

# SymPy Solver Demo
let's create a symbolic variable $z$, and define an arbitrary equation of $55.9728340445699z^2+32z−28.1086638217203=0$, and solve it symbolically. 


```python
# Written by Jacob Chodoriwsky and Ghassem Tofighi
# SymPy is the python library for symbolic mathematics.
# https://docs.sympy.org/latest/index.html
import sympy as sp
import math
sp.init_printing(use_unicode=True)

# Define z as a symbolic variable.
z = sp.symbols('z')

# Get the k-value, substitute it into the function. 
# Note that cos() takes angles in radians, so convert from degrees. 
k = math.cos(math.radians(33))
k
```


```python
# Define our function. 
f = (81*k**2 - 1)*z**2 + 32*z + (324*k**2 - 256)
f
```


```python
# Define the related equation. 
eqn = sp.Eq(f,0)
eqn
```


```python
# Solve the equation
sp.solveset(eqn, z)
```

# How calculate RREF and solve system of linear equations in SymPy?
Assume the following system of linear equations. SymPy can reduce the following matrices to RREF:

$\left(\begin{array}{ccc|c}1 & 2 & -4 & 12 \\ 2 & 6 & 8 & 14 \\ 0 & 1 & 8 & -5\end{array}\right)$

\begin{alignedat}{3}
% R & L   &  R & L   &  R & L 
  x & +{} &  2y & +{} & -4z & = 12 \\
  2x & +{} &  6y & +{} &  8z & = 14 \\
 & {} &  y & +{} &  8z & = -5 \\
\end{alignedat}

To calculate RREF we can use augmented matrix as follows:


```python
#Defining symbolic augmented matrix
A = sp.Matrix([[1,2,-4,12],[2,6,8,14],[0,1,8,-5]])
A

```




$\displaystyle \left[\begin{matrix}1 & 2 & -4 & 12\\2 & 6 & 8 & 14\\0 & 1 & 8 & -5\end{matrix}\right]$




```python
# print RREF
A.rref()
```




$\displaystyle \left( \left[\begin{matrix}1 & 0 & -20 & 22\\0 & 1 & 8 & -5\\0 & 0 & 0 & 0\end{matrix}\right], \  \left( 0, \  1\right)\right)$



# Solving system of linear equations in SymPy

RREF method does not provide final solutions for the following system of linear equations:

\begin{alignedat}{3}
% R & L   &  R & L   &  R & L 
  x & +{} &  2y & +{} & -4z & = 12 \\
  2x & +{} &  6y & +{} &  8z & = 14 \\
 & {} &  y & +{} &  8z & = -5 \\
\end{alignedat}

To find solutions in Sympy you should use the *Sandard Matrix Representation*, $A\mathbf{x}=\mathbf{b}$:

\begin{alignedat}{3}
\left[\begin{array}{ccc}1 & 2 & -4 \\ 2 & 6 & 8 \\ 0 & 1 & 8\end{array}\right]\left[\begin{array}{l}x \\ y \\ z\end{array}\right]=\left[\begin{array}{c}12 \\ 14 \\ -5\end{array}\right]
\end{alignedat}

- $A$ is called the coefficient matrix
- $\mathbf{x}$ is the unknowns or solution vector
- $\mathbf{b}$ is the constant vector

Where

- $A=\left[\begin{array}{ccc}1 & 2 & -4 \\ 2 & 6 & 8 \\ 0 & 1 & 8\end{array}\right]$

- $\mathbf{x}=\left[\begin{array}{c}x \\ y \\ z\end{array}\right]$

- $\mathbf{b}=\left[\begin{array}{c}12 \\ 14 \\ -5\end{array}\right]$

To solve the system you can use `sp.linsolve` as follows:


```python
x, y, z = sp.symbols('x, y, z')
A = sp.Matrix([[1, 2, -4], [2, 6, 8], [0, 1, 8]])
b = sp.Matrix([12, 14, -5])
sp.linsolve((A, b), x, y, z)
```

The above answer is equivalent to what we find manually after solving using RREF method:
$$
\begin{aligned}
&z=t \in \mathbb{R}\\
&y=-5-8 t \\
&x=22+20 t
\end{aligned}
$$

# Calculating the Inverse Matrix
You can calculate the inverse of matrix using `inv()` method as follows: 
## Note:
A square matrix is invertible if and only if the determinant is not equal to zero.


```python
A = sp.Matrix([[1, 2, -4], [2, 6, 8], [0, 1, 8]])
if(A.det()!=0):
  A_inv=A.inv()
else:
  A_inv=[]
  print ("Non Invertible Matrix: Matrix det == 0; not invertible.")
A_inv
```


```python
A = sp.Matrix([[2, 1, 0], [0, 4, -3], [1, -3, 2]])
if(A.det()!=0):
  A_inv=A.inv()
else:
  A_inv=[]
  print ("Non Invertible Matrix: Matrix det == 0; not invertible.")

A_inv
```




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



12be23f22cf2294bfeca272ec473363e38ed6e14
ipynb
Jupyter Notebook
Solving_The_System_of_Linear_Equations_Using_SymPy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Solving_The_System_of_Linear_Equations_Using_SymPy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Solving_The_System_of_Linear_Equations_Using_SymPy.ipynb
tofighi/Linear-Algebra
bea7d2a4a81e0c49b324f23c47cf03db72e376cf
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES