SHARE
TWEET

Conv

a guest Dec 29th, 2016 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Quick convolution approximate for puzzle http://puzzling.stackexchange.com/questions/47219/little-bit-of-a-code-in-binary
  2. //Code by Bloc97
  3.  
  4. if (!print) {
  5.     var print = function(str) {
  6.         console.log(str);
  7.     }
  8.     //var print2dArray = print;
  9. } else {
  10. }
  11.  
  12.  
  13. var tab = [
  14. [0,1,1,1,1,1,1,1,0,0,1,0,0,1,0,1],
  15. [0,1,0,1,1,1,1,1,0,1,1,0,0,1,0,1],
  16. [0,0,0,1,1,1,1,1,0,1,0,0,1,1,0,1],
  17. [0,0,1,0,1,1,1,1,0,1,1,0,0,0,1,0],
  18. [1,1,1,1,0,1,0,1,0,0,1,1,1,0,0,0],
  19. [1,1,1,1,0,0,1,1,0,0,1,1,1,1,0,1],
  20. [1,1,1,1,1,0,1,0,0,0,1,1,0,1,0,0],
  21. [1,1,1,1,0,0,0,1,0,0,1,1,0,0,0,1],
  22. [0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,1],
  23. [0,0,1,0,0,1,1,1,0,1,1,1,1,1,1,1],
  24. [0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1],
  25. [0,1,1,0,0,0,0,1,1,0,0,0,1,1,1,1],
  26. [0,0,1,1,0,1,1,0,1,1,1,1,1,1,1,0],
  27. [0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,0],
  28. [0,0,1,1,0,1,0,1,1,1,1,1,0,0,0,1],
  29. [0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1]];
  30.  
  31.  
  32. var test = convolute(tab, 2, 1, 0);
  33. var test = convolute(tab, 2, 2, 0);
  34. var test = convolute(tab, 4, 1, 0);
  35. var test = convolute(tab, 4, 2, 1);
  36. var test = convolute(tab, 4, 4, 0);
  37.  
  38. // var test = convolute(tab, 2, 1, 1);
  39. // var test = convolute(tab, 2, 2, 0);
  40. // var test = convolute(tab, 4, 1, 1);
  41. // var test = convolute(tab, 4, 1, 2);
  42. // var test = convolute(tab, 4, 1, 3);
  43. // var test = convolute(tab, 4, 2, 2);
  44. // var test = convolute(tab, 4, 4, 0);
  45. // var test = convolute(tab, 8, 4, 4);
  46. // var test = convolute(tab, 8, 8, 4);
  47.  
  48.  
  49. function convolute(arr, kernelsize, stride, padding) {
  50.     var paddedarray = pad(arr, padding);
  51.     var newarray = [];
  52.     var height = paddedarray.length;
  53.     var width = paddedarray[0].length;
  54.     for (var i=0; i<height; i+=stride) {
  55.         var newrow = [];
  56.         for (var j=0; j<width; j+=stride) {
  57.             var thiselement = 0;
  58.             for (var k=0; k<kernelsize; k++) {
  59.                 for (var l=0; l<kernelsize; l++) {
  60.                     thiselement += getElement(paddedarray,i+k,j+l);
  61.                 }
  62.             }
  63.             newrow.push(thiselement);
  64.         }
  65.         newarray.push(newrow);
  66.     }
  67.     print("Convolution, Kernel size: "+ kernelsize + ", Stride: " + stride + ", Padding: " + padding);
  68.     //print(newarray);
  69.     print2dArray(newarray);
  70.     return newarray;
  71.    
  72. }
  73.  
  74. function getElement(arr, i, j) {
  75.     if (i < arr.length && j < arr[i].length) {
  76.         return arr[i][j];
  77.     }
  78.     return 0;
  79. }
  80.  
  81. function pad(arr, padding) {
  82.     var height = arr.length;
  83.     var width = arr[0].length;
  84.    
  85.     var newarray = [];
  86.    
  87.     for (var i=0; i<height; i++) {
  88.         newarray[i] = [];
  89.         for (var j=0; j<height; j++) {
  90.             newarray[i][j] = arr[i][j];
  91.         }
  92.     }
  93.    
  94.     for (var i=0; i<height; i++) {
  95.         for (var n=0; n<padding; n++) {
  96.             newarray[i].unshift(0);
  97.             newarray[i].push(0);
  98.         }
  99.     }
  100.     for (var k=0; k<padding; k++) {
  101.         newarray.unshift(emptyArray(width+padding+padding));
  102.         newarray.push(emptyArray(width+padding+padding));
  103.     }
  104.     return newarray;
  105. }
  106.  
  107. function emptyArray(length) {
  108.     var newarray = []
  109.     for (var i=0; i<length; i++) {
  110.         newarray[i] = 0;
  111.     }
  112.     return newarray;
  113. }
  114.  
  115. function getLongestText(array) {
  116.     var length = 0;
  117.     for (var i=0; i<array.length; i++) {
  118.         for (var j=0; j<array[i].length; j++) {
  119.             elementtext = array[i][j] + "";
  120.             if (elementtext.length > length) {
  121.                 length = elementtext.length;
  122.             }
  123.         }
  124.     }
  125.     return length;
  126. }
  127.  
  128. function repeatSpace(n) {
  129.     var str = "";
  130.     for (var i=0; i<n; i++) {
  131.         str += " ";
  132.     }
  133.     return str;
  134. }
  135.  
  136. function print2dArray(array) {
  137.     print("----");
  138.     var maxlength = getLongestText(array);
  139.     for (var i=0; i<array.length; i++) {
  140.         var rowtext = "";
  141.         for (var j=0; j<array[i].length; j++) {
  142.             var elementtext = array[i][j] + "";
  143.             var rowtext = rowtext + elementtext + repeatSpace(maxlength-elementtext.length+1);
  144.         }
  145.         print(rowtext);
  146.     }
  147.     print("----");
  148. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top