Matrix Operations
June 22, 2010
Our Standard Prelude provides a matrix datatype with create, lookup and setting operations. In today’s exercise we extend the matrix datatype with some mathematical operations: sum two matrices, multiply a matrix by a scalar, multiply two matrices, and transpose a matrix.
- Addition of two matrices is done by adding elements in corresponding positions, assuming identical dimensions. For example:
- Multiplying a scalar times a matrix is done by multiplying the scalar times each element in turn. For example:
- The product of an m×n matrix A and an n×p matrix B is the m×p matrix C whose entries are defined by
. For example:
- The transpose of a matrix interchanges the rows and columns of a matrix. For example:
Your task is to write functions that perform the four matrix operations described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.