Code golf - hex to (raw) binary conversion - binary

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
In response to this question asking about hex to (raw) binary conversion, a comment suggested that it could be solved in "5-10 lines of C, or any other language."
I'm sure that for (some) scripting languages that could be achieved, and would like to see how. Can we prove that comment true, for C, too?
NB: this doesn't mean hex to ASCII binary - specifically the output should be a raw octet stream corresponding to the input ASCII hex. Also, the input parser should skip/ignore white space.
edit (by Brian Campbell) May I propose the following rules, for consistency? Feel free to edit or delete these if you don't think these are helpful, but I think that since there has been some discussion of how certain cases should work, some clarification would be helpful.
The program must read from stdin and write to stdout (we could also allow reading from and writing to files passed in on the command line, but I can't imagine that would be shorter in any language than stdin and stdout)
The program must use only packages included with your base, standard language distribution. In the case of C/C++, this means their respective standard libraries, and not POSIX.
The program must compile or run without any special options passed to the compiler or interpreter (so, 'gcc myprog.c' or 'python myprog.py' or 'ruby myprog.rb' are OK, while 'ruby -rscanf myprog.rb' is not allowed; requiring/importing modules counts against your character count).
The program should read integer bytes represented by pairs of adjacent hexadecimal digits (upper, lower, or mixed case), optionally separated by whitespace, and write the corresponding bytes to output. Each pair of hexadecimal digits is written with most significant nibble first.
The behavior of the program on invalid input (characters besides [a-fA-F \t\r\n], spaces separating the two characters in an individual byte, an odd number of hex digits in the input) is undefined; any behavior (other than actively damaging the user's computer or something) on bad input is acceptable (throwing an error, stopping output, ignoring bad characters, treating a single character as the value of one byte, are all OK)
The program may write no additional bytes to output.
Code is scored by fewest total bytes in the source file. (Or, if we wanted to be more true to the original challenge, the score would be based on lowest number of lines of code; I would impose an 80 character limit per line in that case, since otherwise you'd get a bunch of ties for 1 line).

edit Checkers has reduced my C solution to 46 bytes, which was then reduced to 44 bytes thanks to a tip from BillyONeal plus a bugfix on my part (no more infinite loop on bad input, now it just terminates the loop). Please give credit to Checkers for reducing this from 77 to 46 bytes:
main(i){while(scanf("%2x",&i)>0)putchar(i);}
And I have a much better Ruby solution than my last, in 42 38 bytes (thanks to Joshua Swank for the regexp suggestion):
STDIN.read.scan(/\S\S/){|x|putc x.hex}
original solutions
C, in 77 bytes, or two lines of code (would be 1 if you could put the #include on the same line). Note that this has an infinite loop on bad input; the 44 byte solution with the help of Checkers and BillyONeal fixes the bug, and simply stops on bad input.
#include <stdio.h>
int main(){char c;while(scanf("%2x",&c)!=EOF)putchar(c);}
It's even just 6 lines if you format it normally:
#include <stdio.h>
int main() {
char c;
while (scanf("%2x",&c) != EOF)
putchar(c);
}
Ruby, 79 bytes (I'm sure this can be improved):
STDOUT.write STDIN.read.scan(/[^\s]\s*[^\s]\s*/).map{|x|x.to_i(16)}.pack("c*")
These both take input from STDIN and write to STDOUT

39 char perl oneliner
y/A-Fa-f0-9//dc,print pack"H*",$_ for<>
Edit: wasn't really accepting uppercase, fixed.

45 byte executable (base64 encoded):
6BQAitjoDwDA4AQI2LQCitDNIevrWMOy/7QGzSF09jLkBMAa5YDkByrEJA/D
(paste into a file with a .com extension)
EDIT: Ok, here's the code. Open a Window's console, create a file with 45 bytes called 'hex.com', type "debug hex.com" then 'a' and enter. Copy and paste these lines:
db e8,14,00,8a,d8,e8,0f,00,c0,e0,04,08,d8,b4,02,8a,d0,cd,21,eb,eb,cd,20
db b2,ff,b4,06,cd,21,74,f6,32,e4,04,c0,1a,e5,80,e4,07,2a,c4,24,0f,c3
Press enter, 'w' and then enter again, 'q' and enter. You can now run 'hex.com'
EDIT2: Made it two bytes smaller!
db e8, 11, 00, 8a, d8, e8, 0c, 00, b4, 02, 02, c0, 67, 8d, 14, c3
db cd, 21, eb, ec, ba, ff, 00, b4, 06, cd, 21, 74, 0c, 04, c0, 18
db ee, 80, e6, 07, 28, f0, 24, 0f, c3, cd, 20
That was tricky. I can't believe I spent time doing that.

Brian's 77-byte C solution can be improved to 44 bytes, thanks to leniency of C with regard to function prototypes.
main(i){while(scanf("%2x",&i)>0)putchar(i);}

In Python:
binary = binascii.unhexlify(hex_str)
ONE LINE! (Yes, this is cheating.)

EDIT: This code was written a long time before the question edit which fleshed out the requirements.
Given that a single line of C can contain a huge number of statements, it's almost certainly true without being useful.
In C# I'd almost certainly write it in more than 10 lines, even though it would be feasible in 10. I'd separate out the "parse nybble" part from the "convert a string to a byte array" part.
Of course, if you don't care about spotting incorrect lengths etc, it becomes a bit easier. Your original text also contained spaces - should those be skipped, validated, etc? Are they part of the required input format?
I rather suspect that the comment was made without consideration as to what a pleasant, readable solution would look like.
Having said that, here's a hideous version in C#. For bonus points, it uses LINQ completely inappropriately in an effort to save a line or two of code. The lines could be longer, of course...
using System;
using System.Linq;
public class Test
{
static void Main(string[] args)
{
byte[] data = ParseHex(args[0]);
Console.WriteLine(BitConverter.ToString(data));
}
static byte[] ParseHex(string text)
{
Func<char, int> parseNybble = c => (c >= '0' && c <= '9') ? c-'0' : char.ToLower(c)-'a'+10;
return Enumerable.Range(0, text.Length/2)
.Select(x => (byte) ((parseNybble(text[x*2]) << 4) | parseNybble(text[x*2+1])))
.ToArray();
}
}
(This is avoiding "cheating" by using any built-in hex parsing code, such as Convert.ToByte(string, 16). Aside from anything else, that would mean losing the use of the word nybble, which is always a bonus.)

Perl
In, of course, one (fairly short) line:
my $bin = map { chr hex } ($hex =~ /\G([0-9a-fA-F]{2})/g);

Haskell:
import Data.Char
import Numeric
import System.IO
import Foreign
main = hGetContents stdin >>=
return.fromHexStr.filter (not.isSpace) >>=
mapM_ (writeOneByte stdout)
fromHexStr (a:b:tl) = fromHexDgt [a,b]:fromHexStr tl
fromHexStr [] = []
fromHexDgt str = case readHex str of
[(i,"")] -> fromIntegral (i)
s -> error$show s
writeOneByte h i = allocaBytes 1 (wob' h i)
wob' :: Handle -> Int8 -> (Ptr Int8) -> IO ()
wob' h i ptr = poke ptr i >> hPutBuf h ptr 1

Gah.
You aren't allowed to call me on my off-the-cuff estimates! ;-P
Here's a 9 line C version with no odd formatting (Well, I'll grant you that the tohex array would be better split into 16 lines so you can see which character codes map to which values...), and only 2 shortcuts that I wouldn't deploy in anything other than a one-off script:
#include <stdio.h>
char hextonum[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,10,11,12,13,14,15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char input[81]="8b1f0008023149f60300f1f375f40c72f77508507676720c560d75f002e5ce000861130200000000";
void main(void){
int i = 0;
FILE *fd = fopen("outfile.bin", "wb");
while((input[i] != 0) && (input[i+1] != 0))
fputc(hextonum[input[i++]] * 16 + hextonum[input[i++]], fd);
}
No combined lines (each statement is given its own line), it's perfectly readable, etc. An obfuscated version could undoubtedly be shorter, one could cheat and put the close braces on the same line as the preceding statement, etc, etc, etc.
The two things I don't like about it is that I don't have a close(fd) in there, and main shouldn't be void and should return an int. Arguably they're not needed - the OS will release every resource the program used, the file will close without any problems, and the compiler will take care of the program exit value. Given that it's a one-time use script, it's acceptable, but don't deploy this.
It becomes eleven lines with both, so it's not a huge increase anyway, and a ten line version would include one or the other depending on which one might feel is the lessor of two evils.
It doesn't do any error checking, and it doesn't allow whitespace - assuming, again, that it's a one time program then it's faster to do search/replace and get rid of spaces and other whitespace before running the script, however it shouldn't need more than another few lines to eat whitespace as well.
There are, of course, ways to make it shorter but they would likely decrease readability significantly...
Hmph. Just read the comment about line length, so here's a newer version with an uglier hextonum macro, rather than the array:
#include <stdio.h>
#define hextonum(x) (((x)<'A')?((x)-'0'):(((x)<'a')?((x)+10-'A'):((x)+10-'a')))
char input[81]="8b1f0008023149f60300f1f375f40c72f77508507676720c560d75f002e5ce000861130200000000";
void main(void){
int i = 0;
FILE *fd = fopen("outfile.bin", "wb");
for(i=0;(input[i] != 0) && (input[i+1] != 0);i+=2)
fputc(hextonum(input[i]) * 16 + hextonum(input[i+1]), fd);
}
It isn't horribly unreadable, but I know many people have issues with the ternary operator, but the appropriate naming of the macro and some analysis should readily yield how it works to the average C programmer. Due to side effects in the macro I had to move to a for loop so I didn't have to have another line for i+=2 (hextonum(i++) will increment i by 5 each time it's called, macro side effects are not for the faint of heart!).
Also, the input parser should skip/ignore white space.
grumble, grumble, grumble.
I had to add a few lines to take care of this requirement, now up to 14 lines for a reasonably formatted version. It will ignore everything that's not a hexadecimal character:
#include <stdio.h>
int hextonum[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
char input[]="8b1f 0008 0231 49f6 0300 f1f3 75f4 0c72 f775 0850 7676 720c 560d 75f0 02e5 ce00 0861 1302 0000 0000";
void main(void){
unsigned char i = 0, nibble = 1, byte = 0;
FILE *fd = fopen("outfile.bin", "wb");
for(i=0;input[i] != 0;i++){
if(hextonum[input[i]] == -1)
continue;
byte = (byte << 4) + hextonum[input[i]];
if((nibble ^= 0x01) == 0x01)
fputc(byte, fd);
}
}
I didn't bother with the 80 character line length because the input isn't even less than 80 characters, but a 3 level ternary macro could replace the first 256 entry array. If one didn't mind a bit of "alternative formatting" then the following 10 line version isn't completely unreadable:
#include <stdio.h>
int hextonum[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
char input[]="8b1f 0008 0231 49f6 0300 f1f3 75f4 0c72 f775 0850 7676 720c 560d 75f0 02e5 ce00 0861 1302 0000 0000";
void main(void){
unsigned char i = 0, nibble = 1, byte = 0;
FILE *fd = fopen("outfile.bin", "wb");
for(i=0;input[i] != 0;i++){
if(hextonum[input[i]] == -1) continue;
byte = (byte << 4) + hextonum[input[i]];
if((nibble ^= 0x01) == 0x01) fputc(byte, fd);}}
And, again, further obfuscation and bit twiddling could result in an even shorter example.

.
Its an language called "Hex!". Its only usage is to read hex data from stdin and output it to stdout.
Hex! is parsed by an simple python script.
import sys
try:
data = open(sys.argv[1], 'r').read()
except IndexError:
data = raw_input("hex!> ")
except Exception as e:
print "Error occurred:",e
if data == ".":
hex = raw_input()
print int(hex, 16)
else:
print "parsing error"

Fairly readably C solution (9 "real" lines):
#include <stdio.h>
int getNextHexDigit() {
int v;
while((v = fgetc(stdin)) < '0' && v != -1) { /* Until non-whitespace or EOF */
}
return v > '9' ? 9 + (v & 0x0F) : v - '0'; /* Extract number from hex digit (ASCII) */
}
int main() {
int v;
fputc(v = (getNextHexDigit() << 4) | getNextHexDigit(), stdout);
return v > 0 ? main(0) : 0;
}
To support 16-bit little endian goodness, replace main with:
int main() {
int v, q;
v = (getNextHexDigit() << 4) | getNextHexDigit();
fputc(q = (getNextHexDigit() << 4) | getNextHexDigit(), stdout);
fputc(v, stdout);
return (v | q) > 0 ? main(0) : 0;
}

A 31-character Perl solution:
s/\W//g,print(pack'H*',$_)for<>

I can't code this off the top of my head, but for every two characters, output (byte)((AsciiValueChar1-(AsciiValueChar1>64?48:55)*16)+(AsciiValueChar1-(AsciiValueChar1>64?48:55))) to get a hex string changed into raw binary. This would break horribly if your input string has anything other than 0 to 9 or A to B, so I can't say how useful it would be to you.

I know Jon posted a (cleaner) LINQ solution already. But for once I am able to use a LINQ statement which modifies a string during its execution and abuses LINQ's deferred evaluation without getting yelled at by my co-workers. :p
string hex = "FFA042";
byte[] bytes =
hex.ToCharArray()
.Select(c => ('0' <= c && c <= '9') ?
c - '0' :
10 + (('a' <= c) ? c - 'a' : c - 'A'))
.Select(c => (hex = hex.Remove(0, 1)).Length > 0 ? (new int[] {
c,
hex.ToCharArray()
.Select(c2 => ('0' <= c2 && c2 <= '9') ?
c2 - '0' :
10 + (('a' <= c2) ? c2 - 'a' : c2 - 'A'))
.FirstOrDefault() }) : ( new int[] { c } ) )
.Where(c => (hex.Length % 2) == 1)
.Select(ca => ((byte)((ca[0] << 4) + ca[1]))).ToArray();
1 statement formatted for readability.
Update
Support for spaces and uneven amount of decimals (89A is equal to 08 9A)
byte[] bytes =
hex.ToCharArray()
.Where(c => c != ' ')
.Reverse()
.Select(c => (char)(c2 | 32) % 39 - 9)
.Select(c =>
(hex =
new string('0',
(2 + (hex.Replace(" ", "").Length % 2)) *
hex.Replace(" ", "")[0].CompareTo('0')
.CompareTo(0)) +
hex.Replace(" ", "").Remove(hex.Replace(" ", "").Length - 1))
.Length > 0 ? (new int[] {
hex.ToCharArray()
.Reverse()
.Select(c2 => (char)(c2 | 32) % 39 - 9)
.FirstOrDefault(), c }) : new int[] { 0, c } )
.Where(c => (hex.Length % 2) == 1)
.Select(ca => ((byte)((ca[0] << 4) + ca[1])))
.Reverse().ToArray();
Still one statement. Could be made much shorter by running the replace(" ", "") on hex string in the start, but this would be a second statement.
Two interesting points with this one. How to track the character count without the help of outside variables other than the source string itself. While solving this I encountered the fact that char y.CompareTo(x) just returns "y - x" while int y.CompareTo(x) returns -1, 0 or 1. So char y.CompareTo(x).CompareTo(0) equals a char comparison which returns -1, 0 or 1.

PHP, 28 symbols:
<?=pack(I,hexdec($argv[1]));

Late to the game, but here's some Python{2,3} one-liner (100 chars, needs import sys, re):
sys.stdout.write(''.join([chr(int(x,16)) for x in re.findall(r'[A-Fa-f0-9]{2}', sys.stdin.read())]))

Related

Dmux4Way is producing error in line 7: error from Hardware Simulator?

Did I miss anything or is Hardware Simulator wrong?
The simulator is producing error!
Please can you run this and see the error.
Please can you run this and see the error.
Please can you run this and see the error.
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/01/DMux4Way.hdl
/**
* 4-way demultiplexor:
* {a, b, c, d} = {in, 0, 0, 0} if sel == 00
* {0, in, 0, 0} if sel == 01
* {0, 0, in, 0} if sel == 10
* {0, 0, 0, in} if sel == 11
*/
CHIP DMux4Way {
IN in, sel[2];
OUT a, b, c, d;
PARTS:
// Put your code here:
Not(in=sel[0], out=nsel0);
Not(in=sel[1], out=nsel1);
And(a=nsel0, b=nsel1, out=outa);
And(a=in, b=outa, out=a);
And(a=nsel0, b=sel[1], out=outb);
And(a=in, b=outb, out=b);
And(a=sel[0], b=nsel1, out=outc);
And(a=in, b=outc, out=c);
And(a=sel[0], b=sel[1], out=outd);
And(a=in, b=outd, out=d);
//DMux(in=in,sel=sel[1],a=ao,b=bo);
//DMux(in=ao,sel=sel[0],a=a,b=b);
//DMux(in=bo,sel=sel[0],a=c,b=d);
}
Your chip is producing the wrong result when in=1 and sel[2]=01 and also when in=1 and sel[2]=11 (line 8). If you single-step the simulator, you will see that when you do the tests, the output pin that gets set doesn't increment the way you want it to.
So why is that? It has to do with misunderstanding what bits in sel are referred to by sel[0] and sel[1]. Sel[0] is the rightmost, least-significant bit in sel. Sel[1] is the leftmost, most-significant bit. Your chip assumes the opposite.
Don't feel bad, this kind of mistake has bitten every programmer at least once.
See also: https://en.wikipedia.org/wiki/Endianness

How to reorder subarrays of fixed size inside a big 1D array in Thrust

I have 100000 arrays that are stored in a big 1D array.
The size of each array is L =1000 and are ordered in descending order order.
Each array is divided in m=5 segments.(S1,S2,S3,S4,S5)
For example :
A1=[S1,S2,S3,S4,S5]
A2=[S1,S2,S3,S4,S5]
A3=[S1,S2,S3,S4,S5]
…
A100000=[S1,S2,S3,S4,S5]
Here an example of the big container array is as follows:
My question is :
For each window of w=10 arrays (for example) I want to reorganize the 10 arrays as follows:
Put the segments S1 of each of these 10 arrays first, after that put S2 segments , S3 segments ….
Hereunder an example with w =6 :
As information L , m and w are parameters that may take on different values.
Is there a fast way to do it with Thrust ?
the methodology will be very similar to what is described here.
construct a map vector (i.e. sequence produced by iterator) which will map input to output. We will use a thrust::counting_iterator to provide an ordinal sequence 0, 1, 2,..., and then construct the map iterator using a thrust::transform_iterator. The challenge is creating the correct arithmetic for the operation passed to the transform iterator. The code below is commented extensively to explain that.
pass that map vector, via a thrust::permutation_iterator, to thrust::copy_n. Note that sometimes good advice is to not copy data. If you are only using this "transformed view" of the data once, then just pass that permutation iterator to whatever thrust algorithm you are using once, rather than actually copying the data. If you need to use the "transformed view" of the data many times, it may be more efficient to copy it once.
Here is a worked example:
$ cat t5.cu
#include <thrust/sequence.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <iostream>
typedef int dtype; // data type
typedef int itype; // indexing type - change to size_t if L*m*s > int
const itype s = 3; // segment_width
const itype m = 2; // number of segments per sub-array
const itype L = 4; // number of sub-arrays per array
const itype w = 2; // width of group (# of sub-arrays) for segment reordering
// S1 S1 S2 S2 S1 S1 S2 S2
// 0 1 2 6 7 8 3 4 5 9 10 11 12 13 14 18 19 20 15 16 17 21 22 23
using namespace thrust::placeholders;
int main(){
// we require data that consists of L*m*s elements
// we also require L to be whole-number divisible by w
thrust::device_vector<dtype> d_data(L*m*s);
thrust::device_vector<dtype> d_result(L*m*s);
thrust::sequence(d_data.begin(), d_data.end());
// we will build up the necessary map iterator progressively
// we will start with an target_index sequence i = 0, 1, 2, 3, 4, 5, ... which defines the location in the target vector
// seg_idx (position within a segment) = i%s 0, 1, 2, 0, 1, 2, ...
// which_seg (which segment within sub-array) = (i/(w*s))%m 0, 0, 0, 0, 0, 0, 1, ...
// which_sub (which sub-array in group) = (i/s)%w 0, 0, 0, 1, 1, 1, 0, ...
// which_grp (which group in array) = i/(w*s*m) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ...
// map index = which_grp*group_width + which_sub*subarray_width + which_seg*segment_width + seg_idx
// map index = (i/(w*s*m))*(w*s*m) + ((i/s)%w)*(s*m) + ((i/(w*s))%m)*s + i%s
thrust::copy_n(thrust::make_permutation_iterator(d_data.begin(), thrust::make_transform_iterator(thrust::counting_iterator<itype>(0), (_1/(w*s*m))*(w*s*m) + ((_1/s)%w)*(s*m) + ((_1/(w*s))%m)*s + _1%s)), L*m*s, d_result.begin());
thrust::copy(d_result.begin(), d_result.end(), std::ostream_iterator<dtype>(std::cout, ","));
std::cout << std::endl;
}
$ nvcc -o t5 t5.cu
$ ./t5
0,1,2,6,7,8,3,4,5,9,10,11,12,13,14,18,19,20,15,16,17,21,22,23,
$
Note that if the overall data length (L*m*s) is greater than what can be held in a int quantity, then the above code would have to be refactored to use size_t instead of int for the itype typedef.

Converting from fractional to decimal representation in Octave

I'm getting the following warning:
warning: Using rat() heuristics for double-precision input (is this what you wanted?)
and my resultant calculation is using the rational approach when I would like the decimal form. How can I force the computation to convert the rational to a decimal representation?
Here is the code:
pkg load symbolic
syms a b c d real
C = [1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 0, 1; 0, 0, 1, 0]
H = (1/sqrt(2))*[1, 1; 1, -1]
I = [1, 0; 0, 1]
X = [a, b, c, d]
s = kron(H, I)
s*C*X'
The rational representation can be converted to a float one using vpa. vpa(x,n) evaluates x to at least n significant digits. If you want to use current value of digits, you can omit n.
vpa(s*C*X.',4)
% Above line evaluates the result to at least 4 significant digits
Also note that ' is not transpose. It is complex conjugate transpose. Use transpose (i.e. .') when you are meant to take transpose. That's why I made the replacement in the above code.
Regarding the warning message, it can be turned off by:
warning ('off', 'OctSymPy:sym:rationalapprox');
You can turn it on again by replacing off with on in the above code.

cudaMalloc3DArray fails with bad value -- limit to size?

Calling:
cudaExtent extent = make_cudaExtent( 1920 * sizeof(float), 1080, 10);
chanDesc = cudaCreateChannelDesc ( 32, 0, 0, 0, cudaChannelFormatKindFloat);
err = cudaMalloc3DArray ( &(devYAll[0]), &chanDesc, extent, 0);
is failing with err=cudaErrorInvalidValue. When I use the first arguement to extent as 1024 or smaller then the call to 3D array succeeds. Is there somehow a limit on the size of the memory that can be allocated with cudaMalloc3DArray ?
Yes there is a limit on extent size - either 2048x2048x2048 or 4096x4096x4096 depending on which hardware you have (from the details in your question I presume you have a Fermi card). But that true source of your problem is your make_cudaExtent call. For cudaMalloc3DArray, the first argument of extent should be given in elements, not bytes. This is why you are getting an error for first dimensions > 1024, as 1024 * sizeof(float) = 4096 which is the limit of Fermi GPUs.
So to allocate a 1920x1080x10 3D array, do this:
cudaExtent extent = make_cudaExtent( 1920, 1080, 10);
chanDesc = cudaCreateChannelDesc ( 32, 0, 0, 0, cudaChannelFormatKindFloat);
err = cudaMalloc3DArray ( &(devYAll[0]), &chanDesc, extent, 0);
In this call, the size of the type is deduced from the channel description, and the extent dimensions are modified as required to meet the pitch/alignment requirements of the hardware.

Efficient way to get bit in binary expansion of [0,1] real in Mathematica?

As is well known, any real in [0,1] can be written as a binary expansion in base 1/2:
x = b1 * 1/2^1 + b2 * 1/2^2 + ...
I would like an efficient way to get bi for a given x and index i, and I don't think there's any built-in way to do that in Mathematica. IntegerDigits and RealDigits don't seem to be able to help, and none of the related functions are pertinent.
The obvious solution is to do the manual conversion, but I was hoping to avoid that. Am I missing something?
EDIT: for future reference, what I was looking for can be done this way,
BinaryExpansionBit[p, j] := RealDigits[p, 2, 1, -j][[1]][[1]]
where
BinaryExpansionBit[x, i]
gives the bi I was talking about.
I don't see what's wrong with RealDigits.
rd=RealDigits[0.1,2]
gives a nice binary expansion:
(* out:
{{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
1, 0, 0, 1, 1, 0, 1, 0}, -3}
*)
testing:
rd[[1]].Table[1/2^(n - rd[[2]]), {n, Length[rd[[1]]]}]
(* out: 3602879701896397 / 36028797018963968, which is 0.1*)
The second element of RealDigit's output tells you location of the first element with respect to the decimal point. So, for a real r, 0<r<1 your bi = rd[[1,i-rd[[2]]].
It depends on what you mean by "efficient". Mathematica can easily convert to binary, as this Wolfram Alpha example shows.
Otherwise what you are looking for is the parity of the integer part of x * 2^i.