# Linear Algebra Although this isn't a course in linear algebra we are going to use some fundamental concepts from linear algebra to solve systems of equations. If you haven't taken linear algebra before, it is the study of linear equations. These equations can be represented in the form of matrices. Matrices are rectangular arrays of numbers arranged in rows and columns. They are widely used in both engineering and computer science. Let's say we have the following system of equation. $$ \begin{cases} a_{11} x_1 + a_{12} x_2 + a_{13} x_3 = b_1 \\ a_{21} x_1 + a_{22} x_2 + a_{23} x_3 = b_2 \\ a_{31} x_1 + a_{32} x_2 + a_{33} x_3 = b_3 \end{cases} \tag{1} $$ Where both the $a_{nn}$ coefficients and the $b_n$ variables are all known. We can re-write this system into a matrix form by creating an $A$ matrix of all $a_{nn}$ values and a vector in the form of: $$ \left[ {\begin{array}{cc} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{array} } \right] \left[ {\begin{array}{cc} x_{1}\\ x_{2}\\ x_{3}\\ \end{array} } \right] = \left[ {\begin{array}{cc} b_{1}\\ b_{2}\\ b_{3}\\ \end{array} } \right] \tag{2} $$ where $$ \left[ {\begin{array}{cc} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{array} } \right] = A, \left[ {\begin{array}{cc} b_{1}\\ b_{2}\\ b_{3}\\ \end{array} } \right] = b $$ we can also write the equation in it's simplified form $$ Ax=b \tag{3} $$ Take a close look at equation 1 and 2 and look at their similarity and differences. Notice how the pattern of the $A$ matrix and $b$ vector and follow the same patterns as however the elements of the $x$ vector ($x_1$,$x_2$,$x_3$) are flipped vertically. This is because of how we multiple matrices. ![](https://www.mathsisfun.com/algebra/images/matrix-multiply-a.svg) ![](https://www.mathsisfun.com/algebra/images/matrix-multiply-b.svg) ## Matrix operations in python - Matrices of the same size can be added or subtracted element-wise: - Multiply a matrix by a scalar yields a new matrix where every element is multiplied by the scalar. - The determinant of a matrix is number of a square matrix that summarizes certain properties of the matrix. - **Identity Matrix $I$**: Square matrix with 1’s on the diagonal and 0’s elsewhere. - **Inverse Matrix $A^{-1}$**: Satisfies $AA^{-1} = I$. (Only square, non-singular matrices have inverses.) ## Matrix Math in Python Let's create two square matrices $A$ and $B$ to perform some matrix operations on: ```python import numpy as np np.set_printoptions(suppress=True) A = np.array([[1, 3], [2, 4]]) # 2x2 B = np.array([[5, 6], [7, 8]]) # 2x2 A.shape, B.shape b = np.array([3, 5]) b.shape ``` Test and see how it works. ```python # Addition and Subtraction of same shapes A + B A - B # Scalar Multiplication 3*A 5*B # Matrix Multiplication mat_mult = A @ B # Transpose B.T # Identity and Inverse matrix I = np.eye(2) A_inv = np.linalg.inv(A) A @ A_inv, A_inv @ A # Determinants detA = np.linalg.det(A) detB = np.linalg.det(B) detA, detB ``` ### Problem 1 Re-write the following systems of equation in matrix form to normal algebraic equations. $$ \left[ {\begin{array}{cc} 3 & 5 & 7\\ 2 & 1 & 3\\ 2 & 3 & 6\\ \end{array} } \right] \left[ {\begin{array}{cc} x_{1}\\ x_{2}\\ x_{3}\\ \end{array} } \right] = \left[ {\begin{array}{cc} 24\\ 52\\ 12\\ \end{array} } \right] $$ ## Problem 2 ```python import numpy as np # Coefficient matrix A A = np.array([[2, 3], [4, 5]]) # Right - hand side vector b b = np.array([4, 6]) # Solve the system of equations x = np.linalg.solve(A, b) print(x) ``` ## Problem 3 Matrix Determinate Cramer's Rule -