Search code examples
kdbq-lang

matrix multiplication across tables in kdb


I want to multiply all the values in a 4x2 table by a 2x1 table, and get back a 4x1 vector (ideally a column in a table).

How do I convert the data in kdb tables into matrices to permit matrix mulplication?

tab2:([]w:1 3 2 1; x:-6 8 0 -3);
taby:([] b: 3, 2);
r:tab2 mmu taby"  / this doesn't work, but this is what I want to get.

Solution

  • mmu only works with floats so you need to cast to float.

    q)tab2:([]w:"f"$1 3 2 1; x:"f"$-6 8 0 -3);
    q)taby:([] b:"f"$3, 2);
    

    You also need to strip the vectors from the tables by doing "flip value flip"

    q)(flip value flip tab2) mmu flip value flip taby
    -9
    25
    6
    -3