Created
August 10, 2019 23:26
-
-
Save mwgamera/de1d04a2803dff546bb57f6398575998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Convert a vector or matrix of floating point numbers to a CDR format | |
# which can be relatively quickly loaded and decoded by GNU APL with 12⎕CR. | |
# klg, Aug 2019 | |
use strict; | |
use warnings; | |
use constant CDR_FLT64 => 2; | |
my @shape = (0, 0); | |
my @ravel; | |
while (<>) { | |
my @row = split /\s+/; | |
next unless @row; | |
$shape[1] = @row unless $shape[1]; | |
warn "Not rectangular" unless @row == $shape[1]; | |
push @ravel, map 0+($_//'nan'), @row[0 .. $shape[1]-1]; | |
$shape[0]++; | |
} | |
@shape = $shape[0] if $shape[1] == 1; | |
@shape = $shape[1] if $shape[0] == 1; | |
print pack("NNNCC x![16] NN$#shape d* x![16]", | |
0x2020, | |
0x10 + ((@shape + @ravel) * 8) + 15 & ~15, | |
scalar @ravel, | |
CDR_FLT64, | |
scalar @shape, | |
@shape, @ravel, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment