Ctr

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.


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.

matrix multiplication diagram

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.

© 2022 Andrew Carlson. All rights reserved.

i
INTEGER
j
INTEGER
value
FLOAT
1
1
1
-108.75900000000001
2
1
2
-104.98379999999999
3
1
3
-170.75900000000001
4
1
4
123.75619999999996
5
2
1
150.3417
6
2
2
-43.11519999999999
7
2
3
45.89789999999999
8
2
4
49.68689999999998
9
3
1
267.57300000000004
10
3
2
-0.999400000000005
11
3
3
212.98540000000003
12
3
4
-133.60700000000003
13
4
1
46.95559999999999
14
4
2
-124.99139999999998
15
4
3
-112.76679999999999
16
4
4
-47.509899999999945
17
5
1
-194.26639999999995
18
5
2
-358.903
19
5
3
57.40020000000001
20
5
4
143.54619999999997
20 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?