Created
June 20, 2015 16:19
-
-
Save nbrick/d1065cb5626f78ba0557 to your computer and use it in GitHub Desktop.
Nullspace encoding example
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> // rand() | |
#include <string.h> // strlen() | |
const char* txt = "THESEMUSTBEUPPERCASELETTERS"; | |
const int colors = 256; | |
int num (char letter) { | |
return (int)letter - 64; | |
} | |
int main () { | |
int n = strlen(txt); | |
// Convert the string to an array of ints, with a large negative | |
// int at the end such that the sum over the array is zero. | |
int w = n+1; // width of matrix | |
int soln[w]; // vector to encode | |
{ | |
int accum = 0; | |
for (int j = 0; j < n; j++) { | |
accum += (soln[j] = num(txt[j])); | |
} | |
soln[w-1] = -accum; | |
} | |
int i = 0; // row label | |
// We need at least w-1 rows for the null vector to be | |
// well-defined. | |
while (i < w-1) { | |
int res = 0; // scalar result of row*soln | |
int row[w]; | |
// Populate the row with random elements. | |
for (int j = 0; j < w; j++) { | |
row[j] = rand()%colors; | |
res += row[j]*soln[j]; | |
} | |
// If nullity holds then print. | |
if (res == 0) { | |
for (int j = 0; j < w; j++) { | |
printf("%d", row[j]); | |
if (j != w-1) | |
printf("\t"); | |
} | |
printf("\n"); | |
i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment