The world’s leading publication for data science, AI, and ML professionals.

Fundamentals of Matrix Algebra with Python | Part 2

Understanding and implementing basic matrix algebra concepts and operations with Python


Introduction

This article follows "Fundamentals of Matrix Algebra with Python | Part 1". It introduces more basic concepts of matrix algebra with some Python code to demonstrate results.

Fundamentals of Matrix Algebra with Python | Part 1

Photo by Dan Schiumarini on Unsplash
Photo by Dan Schiumarini on Unsplash

Contents

  1. Trace
  2. Determinant
  3. Inverse
  4. Orthogonality
  5. Row permutations

This preliminary material on matrix facilitates the exploration of more advanced topics such as simulating dynamic systems using state-space models.

Modelling Dynamic Systems in Python


1) Trace

The trace of a matrix is simply the sum of its diagonal elements, as highlighted below in Figure 1.

Figure 1 - Trace of a Matrix (Image By Author)
Figure 1 – Trace of a Matrix (Image By Author)

Gist 1 provides the Python code to calculate the trace of a matrix, which is trivial using Numpy.


2) Determinant

The determinant of a square matrix is a scalar. Non-square matrices do not have determinants.

Take A in Figure 2 as an example.

Figure 2 - Example 2×2 matrix (Image By Author)
Figure 2 – Example 2×2 matrix (Image By Author)

Figure 3 presents the formula for evaluating a 2×2 determinant.

Figure 3 –Compute 2×2 Determinant (Image By Author)
Figure 3 –Compute 2×2 Determinant (Image By Author)

An essential geometric consequence is that the 2×2 determinant scalar value equals the area formed by a parallelogram made up of the column vectors of A.

Plotting the column vectors of a 2×2 matrix in Matplotlib using the code in Gist 2 shows that v₁ (first column) and v₂ (second column) comprise the sides of a parallelogram.

Figure 4 is a labelled graph generated from the code above. The column vectors form a parallelogram.

Figure 4 - Area of Parallelogram (Image By Author)
Figure 4 – Area of Parallelogram (Image By Author)

Examine and modify the Python code in Gist 2 to produce to see more examples.

Figure 5 depicts two collinear vectors, meaning they are linearly dependent. Therefore, it is not possible to construct a parallelogram.

Figure 5 - Linearly Dependent Vectors (Image By Author)
Figure 5 – Linearly Dependent Vectors (Image By Author)

Therefore, this shows that if rows or columns are linearly dependent, the determinant has a value of 0.


3) Inverse

Figure 6 depicts a 3×3 identity matrix, I.

Figure 6 - Identity Matrix (Image By Author)
Figure 6 – Identity Matrix (Image By Author)

Multiplying ** a matrix by its inverse gives the Identity Matrix, I, i.e. _A A⁻¹ = I,_ where __ A⁻¹ means the inverse of A. Use the expression in Figure 7 to calculate the inverse of a 2**×2.

Figure 7 –2×2 Matrix Inverse (Image By Author)
Figure 7 –2×2 Matrix Inverse (Image By Author)

Not all matrices are invertible. As seen in Figure 7, the determinant appears in the calculation. Therefore, A must be square (n×n) and have a non-zero determinant to have an inverse.


4) Orthogonality

Orthogonality is synonymous with orthonormality. Orthogonality applies when columns or rows are orthonormal vectors.

Orthonormal vectors are simply perpendicular, as demonstrated in Figure 8.

Figure 8 - Orthogonal Vectors (Image By Author)
Figure 8 – Orthogonal Vectors (Image By Author)

Test for orthogonality using the condition in Figure 9.

Figure 9 - Orthogonality Condition (Image By Author)
Figure 9 – Orthogonality Condition (Image By Author)

Fundamentally the expression means that A is orthogonal if its transpose is equal to its inverse – use code similar to Gist 3 to check if a matrix is orthogonal.


5) Row permutations

A permutation matrix is a square binary matrix with one entry of 1 in each row and each column and 0s elsewhere.

Having introduced the Identity matrix, permitting rows of matrix A involves multiplying the matrix by an altered version of I.

Figure 9 - Permutation Matrix (Image By Author)
Figure 9 – Permutation Matrix (Image By Author)

Take matrix A in Figure 10 as an example.

Figure 10 - Example Matrix to Demonstrate Permutations (Image By Author)
Figure 10 – Example Matrix to Demonstrate Permutations (Image By Author)

Permuting rows involves changing the order of rows or columns, for example, moving r₁ to r₂ and r₂ to r₁. Figure 11 depicts examples of both types of permutations using the permutation matrix P defined above.

Figure 11 - Row and Column Permutations (Image By Author)
Figure 11 – Row and Column Permutations (Image By Author)

Gist 4 gives the Python code necessary to perform these operations.


Conclusion

This article has presented five more fundamental concepts of matrix algebra. The topics above are concise in detail, so the reader is encouraged to use this as a starting point for further research.

A recommended next topic to reinforce some of the matrix algebra concepts is solving systems of linear equations ** using the Gaussian elimination algorith**m in Python.

Gaussian Elimination Algorithm in Python

Join Medium with my referral link – Andrew Joseph Davies

Thanks for reading.


References

[1] Matrix Algebra for EngineersJeffrey R. Chasnov[2] Permutation Matrix – Wikepedia


Related Articles