Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB: apply dyadic function across two lists

Tags:

function

kdb

Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?

like image 545
bigO6377 Avatar asked Feb 13 '23 03:02

bigO6377


2 Answers

You have asked for the cross product of your argument lists, so the correct answer is

raze F ./: xList cross yList
like image 132
mollmerx Avatar answered Feb 15 '23 16:02

mollmerx


Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below

q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each 
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}  
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])} 
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
like image 35
JPC Avatar answered Feb 15 '23 17:02

JPC