Skip to content

Instantly share code, notes, and snippets.

@nbrick
Created June 20, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbrick/d1065cb5626f78ba0557 to your computer and use it in GitHub Desktop.
Save nbrick/d1065cb5626f78ba0557 to your computer and use it in GitHub Desktop.
Nullspace encoding example
#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