Basic Matrix Arithmetic
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 and follow on X/Twitter.
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.
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 and follow on X/Twitter.