summaryrefslogtreecommitdiff
path: root/tutorials/module_3/1_1_Linear_Algebra.md
blob: a107c1ff8c2dd6bd81d22ee8d70184896cd946c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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 -