Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created July 12, 2020 16:04
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 tail-call/40fdd258f904fab57894f8bc5bc47c35 to your computer and use it in GitHub Desktop.
Save tail-call/40fdd258f904fab57894f8bc5bc47c35 to your computer and use it in GitHub Desktop.
Failed attempt at sequence compressor
function add(arr1, arr2) {
return arr1.map((x, i) => x + (arr2[i] || 0));
}
function mult(arr1, arr2) {
return arr1.map((x, i) => x * (arr2[i] || 0));
}
function sum(arr) {
return arr.reduce((acc, x) => acc + x);
}
const sequence = [0, 10, 20, 30, 20, 10, 0, -10];
const components = [
"00001111",
"00110011",
"10101010",
].map(s => Array.from(s).map(Number));
const norm = sum(sequence);
console.log('sequence:', sequence);
const ks = components
.map(mask => sum(mult(sequence, mask)) / norm);
console.log('coefficients:', ks);
const restoredMasks = ks
.map((k, i) => components[i].map(x => x * k))
.reduce((acc, mask) => add(acc, mask));
console.log('restored:', restoredMasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment