2

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.
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36

1 Answers1

2

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
terrylynch
  • 11,844
  • 13
  • 21
  • Thanks. Are these operations efficient for very large data sets (a large number of rows in `tab2`? Or is there a better way? kdb doesn't seem very amenable to linear algebra routines ? My use cases will include large amounts of float type data stored in tables which i'll want to apply linear algebra techniques. Such as linear regression, pca. The examples i've looked at so far (q for mortals) seem to assume in advance you have a matrix to work with at the start, and not a table. –  May 19 '17 at 20:37
  • 1
    The operations will be pretty efficient. The only inconvenience is extracting the matrices (lists of lists) from the tables. You could also choose to not store the data as tables but it depends on your use case. I would recommend also checking out the q math library: http://althenia.net/qml – terrylynch May 19 '17 at 21:13