Matrix Multiply
For this challenge, we'll implement matrix multiplication using SQL.
Matrices are very important data structures in linear algebra. They are basically a grid of numbers, i.e. a 2D array. They can have varying "height" and "width," though we usually call those dimensions m and n respectively, i.e. a matrix has m rows and n columns.
For example, here are 2 matrices named A and B.
A is a 2×4 matrix, i.e. m = 2 and n = 4, and B is a 4×3 matrix.
Before we can multiply matrices, we need a way of representing a matrix in a SQL table.
A good way to do that is to establish coordinates for the individual values in the matrix.
We'll call these coordinates (i, j), where i is the row position and j is the columns position.
So, we can use 1 row in a SQL table to represent 1 value in a matrix, where the row contains i
, j
, and value
.
Since we want to store multiple matrices in our table, we also want a name column to identify the different matrices.
column_name | type |
---|---|
matrix_name | VARCHAR |
i | INTEGER |
j | INTEGER |
value | FLOAT |
To see how to multiply 2 matrices, I recommend this illustration on Wikipedia.
For the example matrices A and B above:
For this challenge, compute A × B for the matrices in the matrices
table.
The output should have the same representation as the input matrices, except that it doesn't need a name.
Difference between FLOAT and NUMERIC
You can read about the differences between these 2 types here.