Hi All,
I've been mucking around generating look up tables in C++. Getting
frustrated, I wrote up this J single liner. Can you think of ways to
simplify the LUT computation (the expression we assign to L2 and L3 below)?
*Original Operator (no LUT)*
] x =. ? 5 $ 2
1 0 1 1 0
] y =. 30 > ? 5 6 $ 100
1 0 0 0 0 0
0 1 0 1 1 0
0 1 0 0 0 1
0 0 0 0 1 1
0 1 1 1 0 1
x (+./@:*.) y
1 1 0 0 1 1
*LUT Explanation*
The idea is to break up x into smaller chunks (2-bits, 3-bits, etc) and
precompute the operation for the corresponding chunks of y. Then we just
convert the chunks into indices and look them up in the LUT.
_2<\x
┌───┬───┬─┐
│1 0│1 1│0│
└───┴───┴─┘
_2#.\x
2 3 0
*2-bit LUT*
]L2=. _2((#:@:i.@:(2&^)@:#) (+./"2 @: (*."0 1"1 _)) ])\y
0 0 0 0 0 0
0 1 0 1 1 0
1 0 0 0 0 0
1 1 0 1 1 0
0 0 0 0 0 0
0 0 0 0 1 1
0 1 0 0 0 1
0 1 0 0 1 1
0 0 0 0 0 0
0 1 1 1 0 1
0 0 0 0 0 0
0 0 0 0 0 0
+./ (_2#.\x) {"0 2 L2 NB. Compute by 2-bit lookups
1 1 0 0 1 1
*3-Bit LUT*
]L3=. _3((#:@:i.@:(2&^)@:#) (+./"2 @: (*."0 1"1 _)) ])\y
0 0 0 0 0 0
0 1 0 0 0 1
0 1 0 1 1 0
0 1 0 1 1 1
1 0 0 0 0 0
1 1 0 0 0 1
1 1 0 1 1 0
1 1 0 1 1 1
0 0 0 0 0 0
0 1 1 1 0 1
0 0 0 0 1 1
0 1 1 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
+./ (_3#.\x) {"0 2 L3 NB. Compute by 3-bit lookups
1 1 0 0 1 1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm