-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvq.cpp
147 lines (135 loc) · 3.18 KB
/
pvq.cpp
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
146
147
#include "pvq.hpp"
using namespace std;
unsigned long long pvq_N2(unsigned int l,unsigned int K)
{
if(K==0)
return 1;
else if(l==0)
return 0;
else if(l==1)
return 2;
else if(K==1)
return 2*l;
else
return pvq_N2(l-1,K)+pvq_N2(l-1,K-1)+pvq_N2(l,K-1);
}
unsigned long long pvq_N3(unsigned int l, unsigned int K, vector<vector<unsigned long long>> out)
{
if(K==0)
return 1;
else if(l==0)
return 0;
else if(l==1)
return 2;
else if(K==1)
return 2*l;
else {
unsigned long long v1, v2, v3;
v1 = K<out[l-1].size() ? out[l-1][K] : pvq_N3(l-1,K,out);
v2 = (K-1)<out[l-1].size() ? out[l-1][K-1] : pvq_N3(l-1,K-1,out);
v3 = (K-1)<out[l].size() ? out[l][K-1] : pvq_N3(l,K-1,out);
return v1 + v2 + v3;
}
}
vector<vector<unsigned long long>> pvq_Npre(unsigned int l, vector<unsigned int> k)
{
vector<vector<unsigned long long>> out(l+1);
for(unsigned int li=0;li<=l;li++){
for(unsigned int ki=0;ki<=k[li];ki++){
out[li].push_back(pvq_N3(li,ki,out));
}
}
return out;
}
vector<int> pvq_quantize(vector<float> x,unsigned int K)
{
auto n1 = accumulate(x.begin(),x.end(),1e-30f,[](float tot,float i) {return tot+fabs(i);});
for(auto &i:x)
i *= K / n1;
vector<int> xr(x.size());
for(int i = 0;i<x.size();i++)
xr[i] = (int)round(x[i]);
auto tk = (unsigned int)accumulate(xr.begin(),xr.end(),0,[](int tot,int i) {return tot+abs(i);});
while(tk<K) {
auto mi = 0;
auto me = fabs(x[0]) - fabs(xr[0]);
for(int i = 1;i<x.size();i++) {
auto met = fabs(x[i]) - fabs(xr[i]);
if(me<met) {
mi = i;
me = met;
}
}
xr[mi] += ((0<x[mi])-(x[mi]<0));
tk += 1;
}
while(tk>K) {
auto mi = 0;
auto me = fabs(x[0]) - fabs(xr[0]);
for(int i = 1;i<x.size();i++) {
auto met = fabs(x[i]) - fabs(xr[i]);
if(me>met) {
mi = i;
me = met;
}
}
xr[mi] -= ((0<x[mi])-(x[mi]<0));
tk -= 1;
}
return xr;
}
unsigned long long pvq_encode(const vector<int> &x,unsigned int l,unsigned int k,const vector<vector<unsigned long long>> &pvqN)
{
unsigned long long b = 0;
auto i = 0;
while(k>0) {
auto xa = abs(x[i]);
auto xs = (0 < x[i])-(x[i] < 0); // sign function
if(xa==1)
b += pvqN[l-1][k] + (1-xs) / 2 * pvqN[l-1][k-1];
else if(xa>1) {
unsigned long long tmp = 0;
b += 2 * accumulate(next(pvqN[l-1].begin(),k-xa+1),next(pvqN[l-1].begin(),k),tmp);
b += pvqN[l-1][k] + (1-xs) / 2 * pvqN[l-1][k-xa];
}
k -= xa;
l -= 1;
i += 1;
}
return b;
}
vector<int> pvq_decode(unsigned long long b,unsigned int l,unsigned int k,const vector<vector<unsigned long long>> &pvqN)
{
vector<int> x(l);
unsigned long long xb = 0;
auto i = 0;
while(k>0) {
if(b==xb) {
x[i] = 0;
break;
}
else if(b<xb+pvqN[l-1][k]) {
x[i] = 0;
}
else {
xb += pvqN[l-1][k];
unsigned int j = 1;
while(b>=xb+2*pvqN[l-1][k-j]) {
xb += 2*pvqN[l-1][k-j];
j += 1;
}
if(xb<=b && b<xb+pvqN[l-1][k-j])
x[i] = (int)j;
else if(b>=xb+pvqN[l-1][k-j]) {
x[i] = -(int)j;
xb += pvqN[l-1][k-j];
}
k -= j;
}
l -= 1;
i += 1;
}
if(k>0)
x.back() = k-abs(x[i]);
return x;
}