Basic Matrix Arithmetic

by Justin Skycak (x.com/justinskycak) on

We can use arrays to implement matrices and their associated mathematical operations.

This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Basic Matrix Arithmetic. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/basic-matrix-arithmetic/


Want to get notified about new posts? Join the mailing list.

Let’s use arrays to implement matrices and their associated mathematical operations. We will jump straight to exercises – it is assumed that you are already familiar with matrix arithmetic and reduced row echelon form.

Exercise: Addition, Subtraction, Transpose, and Scalar Multiplication

To start, create a class Matrix that implements basic matrix arithmetic.


>>> A = Matrix([
    [1, 2],
    [3, 4]
  ])

>>> A.show()
[ 1 , 2 ]
[ 3 , 4 ]

>>> At = A.transpose()
>>> At.show()
[ 1 , 3 ]
[ 2 , 4 ]

>>> A.add(At).show()
[ 2 , 5 ]
[ 5 , 8 ]

In addition to the methods shown above, you should also implement subtract and scalar_multiply. (The input to scalar_multiply is a number, and it should multiply all entries of the matrix.)

To keep your code clean, you’ll need to implement some helper attributes like num_cols and num_rows.


>>> A = Matrix([
    [1, 2, 3],
    [4, 5, 6]
  ])

>>> A.num_cols
3

>>> A.num_rows
2

Your class (and tests) should be general to matrices of any dimensions. Do not assume the matrix is square. However, if you struggle and get stuck, then you can build some scaffolding for yourself by first hard-coding a class for, say, a $2 \times 3$ matrix. Handling the general case often becomes easier once you’ve worked through a specific case.

Remember that some operations impose constraints on the dimensions of the matrices involved. For example, you cannot add two matrices unless their dimensions are equal. In your code, be sure to check that all such constraints are satisfied before attempting to carry out the operation. If any constraint is not satsified, then throw an error with a descriptive message so that the user can understand why your matrix class is unable to perform the desired computation.

Exercise: Matrix Multiplication and Recursive Determinant

Next, implement the method matrix_multiply. This will be trickier than the methods above, but remember that the element at row $i$ and column $j$ in the product $AB$ is just the dot product of row $i$ in $A$ and column $j$ in $B.$ (This means that you should write a helper function for computing dot products.)

Then, implement the method recursive_determinant which computes the determinant using recursive cofactor expansion. As a refresher, below is an example of using recursive cofactor expansion to compute the determinant of a $3 \times 3$ matrix.

$\begin{align*} &\textrm{det } \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \\[5pt] &= 1 \, \textrm{det} \, \begin{bmatrix} 5 & 6 \\ 8 & 9 \end{bmatrix} - 2 \, \textrm{det} \, \begin{bmatrix} 4 & 6 \\ 7 & 9 \end{bmatrix} + 3 \, \textrm{det} \, \begin{bmatrix} 4 & 5 \\ 7 & 8 \end{bmatrix} \\[5pt] &= 1 \left( 5 \, \textrm{det} \, [9] - 6 \, \textrm{det} \, [8] \right) - 2 \left( 4 \, \textrm{det} \, [9] - 6 \, \textrm{det} \, [7] \right) + 3 \left( 4 \, \textrm{det} \, [8] - 5 \, \textrm{det} \, [7] \right) \\[5pt] &= 1 \left( 5 \cdot 9 - 6 \cdot 8 \right) - 2 \left( 4 \cdot 9 - 6 \cdot 7 \right) + 3 \left( 4 \cdot 8 - 5 \cdot 7 \right) \\[5pt] &= 1 \left( 45 - 48\right) - 2 \left( 36 - 42 \right) + 3 \left( 32 - 35 \right) \\[5pt] &= 1 \left( -3 \right) - 2 \left( -6 \right) + 3 \left( -3 \right) \\[5pt] &= -3 + 12 - 9 \\[5pt] &= 0 \end{align*}$



This post is part of the book Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. Suggested citation: Skycak, J. (2021). Basic Matrix Arithmetic. In Introduction to Algorithms and Machine Learning: from Sorting to Strategic Agents. https://justinmath.com/basic-matrix-arithmetic/


Want to get notified about new posts? Join the mailing list.