Created
July 12, 2020 16:04
-
-
Save tail-call/40fdd258f904fab57894f8bc5bc47c35 to your computer and use it in GitHub Desktop.
Failed attempt at sequence compressor
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
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