cancel
Showing results for 
Search instead for 
Did you mean: 

Infix functions, f:+/* and bitwise XOR

Swee_Heng
New Contributor
Hi, I am a Q newbie and would like to find answers to 3 questions:Q1: How do I make a dyadic function infix?The only way I've found that seems to work is to append the both-eachadverb. Is this the only way? q)op:{y+10*x} q)1 2 3 op' 7 8 9 17 28 39Q2: Why does f:+/* work in K but not in Q?In K it is is possible to define "f:+/*" and then perform matrixmultiplication as "x f\: y". Doing this in Q, I get an error which Ido not know how to interpret: q)f:+/* '/The following code for matrix multiplication does seem to work though: q)m:{(+/') x *\: y}Q3: How do I bitwise XOR a pair of bytes in Q?Thanks in advance!Swee Heng
6 REPLIES 6

Attila
New Contributor
> Q1: How do I make a dyadic function infix?> The only way I've found that seems to work is to append the both-each> adverb. Is this the only way?> q)op:{y+10*x}> q)1 2 3 op' 7 8 9> 17 28 39q).q.op:{y+10*x}> Q2: Why does f:+/* work in K but not in Q?> In K it is is possible to define "f:+/*" and then perform matrix> multiplication as "x f\: y". Doing this in Q, I get an error which I> do not know how to interpret:> q)f:+/*> '/In q there is no ambivalence, and adverb derived verbs default toinfix (not monadic what you would need here)In q you have to sayq)f:sum@*> Q3: How do I bitwise XOR a pair of bytes in Q?I would do sg like this:q)0b sv(0b vs 0x12)<>0b vs 0x340x26Regards, Attila

> > Q1: How do I make a dyadic function infix?> q).q.op:{y+10*x}Thanks Attila! Defining the function in the .q namespace allowed me touse the function in infix order. Is there any documentation on whydyadic functions in .q are handled differently? I can't seems to findany explanation on code.kx.com. Perhaps I missed something.> > Q3: How do I bitwise XOR a pair of bytes in Q?> I would do sg like this:> q)0b sv(0b vs 0x12)<>0b vs 0x34I defined an infix XOR function as suggested. It works OK for singleitem list. But for longer lists, it does not work as I anticipated: q).q.op:{0b sv(0b vs x)<>0b vs y} q)0x12 op 0xed 0xff q)0x1212 op 0xeded k){x\:y} 'type @ 0b\: 0xededNot unless I use the both-each adverb: q)0x1212 op' 0xeded 0xffffOn the other hand, if I define .q.op:{x|y}, I can use op on two listswithout using the both-each adverb: q).q.op:{x|y} q)0x1234 op 0xa302 0xa334Is there some constraints on what .q.* functions can do?Swee Heng

> I defined an infix XOR function as suggested. It works OK for single> item list. But for longer lists, it does not work as I anticipated:> q).q.op:{0b sv(0b vs x)<>0b vs y}> q)0x1212 op 0xeded> k){x\:y}> 'type> @> 0b\:> 0xededOk, I think I have a better understanding now. It failed because "0bvs x" does not work on lists. But using the each-both adverb solvesit: q).q.op:{0b sv(0b vs x)<>0b vs y}' q)0x1111 op 0x0e0e 0x1f1fAlternatively (and faster) extending using C: #include "k.h" K2(xor){R kg(x->g^y->g);} q)xor:`xor 2:(`xor;2) q).q.op:xor' q)0x1111 op 0x0e0e 0x1f1fIf there are more idiomatic / efficient ways of doing this, do let meknow.Swee Heng

> Alternatively (and faster) extending using C:> #include "k.h"> K2(xor){R kg(x->g^y->g);}>> q)xor:`xor 2:(`xor;2)> q).q.op:xor'> q)0x1111 op 0x0e0e> 0x1f1fyou can make it even faster by writing C code for vectors as welljust out of curiosity why do you need xor (and especially why do youneed it being fast?) Attila

>> > Q1: How do I make a dyadic function infix?>> q).q.op:{y+10*x}> Thanks Attila! Defining the function in the .q namespace allowed me to> use the function in infix order. Is there any documentation on why> dyadic functions in .q are handled differently? I can't seems to find> any explanation on code.kx.com. Perhaps I missed something.One letter namespaces are reserved for special purposes. At the momentq (defining q functions in k :all the dyadic functions here can becalled infix), Q (auxliary functions), h (http server) and o (odbc)are used (and s if the SQL subset is used).Regards, Attila

> One letter namespaces are reserved for special purposes. At the moment> q (defining q functions in k :all the dyadic functions here can be> called infix), Q (auxliary functions), h (http server) and o (odbc)> are used (and s if the SQL subset is used).Thanks Attila for the explanation of the various namespaces.> just out of curiosity why do you need xor (and especially why do you> need it being fast?)I need xor for GF256 arithmetic as I was trying to implement AES in Q.Swee Heng