-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.js
145 lines (127 loc) · 3.67 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function hist(n,sort){
// Make an empty nxn array and empty array
var h = makeArray(n,2);
var arr = (new Array(n)).fill(0);
var randomComparator = ()=>Math.floor(Math.random()*3-1);
// Run the sort 100000 times
for(var i = 0; i < 100000; i++){
if(i === 50000)
console.log('halfway');
arr = arr.map((_,i)=>i); // convert each element into its index
sort(arr,randomComparator);
for(var k = 0; k < n;k++){
// Use the initial index (the value of the cell) and the final index (k) to update the histogram
h[arr[k]][k]++;
}
}
return h;
}
function hist2(n,sort){
// Make an empty nxn array and empty array
var h = makeArray(n,4);
var arr = (new Array(n)).fill(0);
var randomComparator = ()=>Math.floor(Math.random()*3-1);
// Run the sort 100000 times
for(var i = 0; i < 100000; i++){
if(i === 50000)
console.log('halfway');
arr = arr.map((_,i)=>i); // convert each element into its index
sort(arr,randomComparator);
for(var k = 0; k < n;k++){
for(var j = 0; j < n;j++){
// Use the initial index (the value of the cell) and the final index (k) to update the histogram
h[j][k][arr[j]][arr[k]]++;
}
}
}
return h;
}
function makeArray(n,d){
if(d == 1){
return new Array(n).fill(0);
}
return new Array(n).fill(0).map(()=>makeArray(n,d-1));
}
function bounds(hist2){
var total = 0, s = sum(hist2);
var a, b;
for(var i = 0; i < hist2.length;i++){
total += hist2[i];
if(total > s/100){
a = i-1;
break;
}
}
total = 0;
for(var i = hist2.length-1; i >= 0;i--){
total += hist2[i];
if(total > s/100){
b = i+1;
break;
}
}
return [a,b];
}
function computeHist2(hist){
var m = max(0,hist);
var hist2 = new Array(1200).fill(0);
for(var i = 0; i < hist.length; i++){
for(var k = 0; k < hist[0].length;k++){
let hue = Math.floor(1200*Math.sqrt(hist[i][k])/Math.sqrt(m));
if(hue >= 1199){
hue = 1199;
}
hist2[hue]++;
}
}
return hist2;
}
var sum = (a)=>a.reduce((a,b)=>a+b,0);
function graph(hist, c = document.createElement('canvas')){
c.width = 300;
c.height = 300;
var ctx = c.getContext('2d');
var m = max(0,hist);
var a = 300/hist.length, b = 300/hist[0].length;
var hist2 = computeHist2(hist); //computes histogram of pixels hues vs count
var [lower,upper] = bounds(hist2); //computes bounds such that 98% of pixels don't get clipped
var hist3 = new Array(300).fill(0);
for(var i = 0; i < hist.length; i++){
for(var k = 0; k < hist[0].length;k++){
let hue = 1200*Math.sqrt(hist[i][k])/Math.sqrt(m);
hue = Math.floor(300/(upper-lower)*(hue - lower));//rescale so the lower bound = 0 and upper bound = 300 and sample with dx=1
hue = Math.min(Math.max(hue,0),299); //clamp to 0-299
hist3[hue]++;
ctx.fillStyle = 'hsl(' + hue + ',100%,50%)';
ctx.fillRect(i*a,k*b,a,b);
}
}
/*m = max(0,hist3);
for(var i = 0; i < 360; i++){
ctx.fillStyle = 'hsl(' + i + ',100%,50%)'
ctx.fillRect(i,400-Math.floor(100*hist3[i]/m),1,Math.floor(100*hist3[i]/m));
}*/
return c.toDataURL();
}
function max(init,arr){
if(Array.isArray(arr)){
return arr.reduce(max,init);
}else{
return Math.max(init,arr);
}
}
//essentially just a matrix multiplication assuming nxn * nxn
function compose(f,g){
var n = f.length;
var out = (new Array(n)).fill(0).map(()=>new Array(n).fill(0));
for(var x = 0; x < n; x++){
let h = f[x];
for(var y = 0; y < n; y++){
let i = g[y];
for(var k = 0; k < n;k++){ //rescale i by h[y] and add to out[x]
out[x][k] += h[y]*i[k];
}
}
}
return out;
}