7.20 Multiply matrices using Cartesian product

 

Calculate result of multiplying matrices through filtering Cartesian product.

Matrix
row
col
value

imagepng

Below is the computing problem’s mathematical formula:

imagepng

Use xjoin() function to calculate Cartesian product and filter the result according to the specified condition.

SPL script:

A
1 =connect(“db”)
2 =A1.query(“select * from MatrixA”)
3 =A1.query@x(“select * from MatrixB”)
4 =xjoin(A2:A; A3:B, A.col==A3.row)
5 =A4.groups(A.row:row,B.col:col;sum(A.value * B.value):value)

A1 Connect to the database.
A2 Query MatrixA table.
A3 Query MatrixB table.
A4 Use xjoin() function to calculate Cartesian product while performing the conditional filtering.
A5 Group A4’s records and calculate value at each intersection of row and column.

Execution result:

row col value
1 1 14
1 2 32
2 1 32
2 2 77