Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/notebooks/Chapter 2 - Basic Matrix Algebra.ipynb
Views: 449
Matrix Operations
Matrix addition operations are straightforward:
, where is the zero matrix
For any , there exists an , such that .
They are as obvious as it looks, so no proofs are provided. And the matrix multiplication properties are:
Note that we need to differentiate between two types of multiplication: Hadamard multiplication, denoted as (element-wise multiplication), and matrix multiplication, denoted simply as .
Let's show explicitly the matrix multiplication rule for each element:
SymPy Demonstration: Addition
Let's define all the letters as symbols in case we need to use them repeatedly. With this library, we can perform computations analytically, making it a valuable tool for learning linear algebra.
SymPy Demonstration: Multiplication
The matrix multiplication rules can be clearly understood by using symbols.
Commutability
Matrix multiplication usually does not commute, meaning . For instance, consider matrices and :
How do we find a commutable matrix? Let's try find out analytically
To show , we need to prove
That gives us a system of linear equations $$ \begin{align} b g - c f&=0 \ a f - b e + b h - d f&=0\
a g + c e - c h + d g&=0 \
b g + c f&=0 \end{align} $$
To solve it, we can use the Gauss-Jordan elimination method. If we treat as coefficients of the system, we can extract an augmented matrix.
So the augmented matrix takes the form
Perform Gaussian-Jordon elimination till row reduced formed.
The general solution is
Transpose of Matrices
Matrix and its transpose is
The properties of transpose are
We can show why the last property holds with SymPy, define and , multiply them, then transpose, that means
Transpose and , then multiply, meaning
Check if they are equal
Identity Matrices
This is an identity matrix , only 's on principal diagonal, all rest elements are 's.
Identity matrix properties:
Let's generate and and show if it holds
Check if they are equal
Elementary Matrix
An elementary matrix is a matrix that can be obtained from a single elementary row operation on an identity matrix. Such as:
Where means exchanging row 1 and row 2, we denote the transformed matrix as . Then, left multiply onto a matrix will perform the exact the same row operation.
First, generate matrix .
Create an elementary matrix with
Notice that by left-multiplying onto , matrix also switches rows 1 and 2.
Adding a multiple of a row onto another row in the identity matrix also gives us an elementary matrix.
$$\left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right]\\ \longrightarrow \left[ \right]$$Let's verify with SymPy.
We will see the takes places on
We can also reproduce this by explicit row operation on .
In the next section, we will refresh an important conclusion: an invertible matrix is a product of a series of elementary matrices.
Inverse Matrices
If , is called the inverse of matrix , denoted as .
NumPy has convenient function np.linalg.inv()
for computing inverse matrices. Generate
Verify if they are truly inverse of each other
The -0.
means there are more digits after point, but omitted here.
Gauss-Jordan Elimination Method for Matrix Inversion
A convenient way to calculate the inverse of a matrix is to construct an augmented matrix . Then, multiply a series of elementary matrices (representing elementary row operations) until matrix is in row-reduced form. If is of full rank, this process will transform into an identity matrix . Consequently, the identity matrix on the right-hand side of the augmented matrix will be converted into automatically.
We can demonstrate this using SymPy's .rref()
function on the augmented matrix .
Extract the RHS block, this is the .
I wrote a function to round the float numbers to the -th digits, on the top of this file, just for sake of readability.
We can verify if
We got , which means the RHS block is indeed .
An Example of Existence of Inverse
Determine the values of such that the matrix is not invertible.
Still,we are using SymPy to solve the problem.
Form the augmented matrix.
To make the matrix invertible we notice that is multiple conditions to be satisfied, (in the denominators): However, they are actually one condition, because they are multiples of each other.
Solve for 's.
So this is the that makes the matrix invertible. Let's test this with the determinant. If , then the matrix is not invertible, so plug in back in then calculate determinant. Don't worry about determinants; we will revisit this topic later.
The is .
So we found that one condition, as long as , the matrix is invertible.
Properties of Inverse Matrices
If and are both invertible, then .
If is invertible, then .
If and are both invertible and symmetric such that , then is symmetric.
The first property is straightforward
The trick of second property is to show that We can use the property of transpose
The third property is to show Again use the property of transpose We use the condition to proceed The plug in the previous equation, we have