Writing · q/kdb+, programming
Matrix multiplication in q lang
Following my previous post, I’ve started reading the book “Q for Mortals”. It’s a fascinating book and I highly recommend it to anyone who is interested in learning this language properly. After getting through chapter 1 and realizing how well q-lang handles lists and vectors (considering it’s a vector-language), I started thinking what I need to do to write the matrix multiplication operator in q-lang. But I don’t want to just write it. I mean writing it in a for loop is easy and all, but I want to write it as compact as I can. I tried my best to condense the entire code into a one-liner, but failed. It turned into 2 lines 🥲. Anyways, here it goes.
Consider the matrix and the matrix . Let’s create some simple examples for and .
a:(1 2 3; 2 3 4)
b:(3 5; 4 6; 5 7)Here a is while b is . Let’s see what happens if
I multiply one row of a by b
a[0]*b
// shows:
// 3 5
// 8 12
// 15 21which is multiplying the first row of a by each column in b. Now if we sum this, we get
sum a[0]:b
// shows:
// 26 38which is the first row of the result of the matrix multiplication . See where this is going?
Now we just need to repeat this for each row in a. We can use “each-left” to achieve this, but we
need to define this row-times-matrix operation first.
row_mult:{[row;matrix] sum row*matrix}Now we can use each-left as
a row_mult\: b
// shows:
// 26 38
// 38 56which is the full result of the matrix multiplication. Yay!