-
Notifications
You must be signed in to change notification settings - Fork 3
/
pvec.h
271 lines (227 loc) · 4.9 KB
/
pvec.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Positive vector
*/
#ifndef _PVEC_H
#define _PVEC_H
#include <vector>
#include <string>
#include <cassert>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#define EXIT_ERR( s1, s2 ) {printf("[Error] %s%s\n", s1, s2); \
exit(EXIT_FAILURE);}
using namespace std;
template<class T>
class Pvec
{
private:
vector<T> p;
public:
Pvec(){}
Pvec(size_t n): p(n) {}
Pvec(size_t n, T v): p(n, v) {}
Pvec(const vector<T>& v): p(v) {}
Pvec(const Pvec<T>& v): p(v.p) {}
Pvec(const string& line);
void resize(size_t n) {p.resize(n);}
void resize(size_t n, const T& v) {p.resize(n, v);}
void assign(size_t n, const T& v) {p.assign(n, v);}
void rand_init();
void uniform_init();
void bias_init(double v); // fix p[0] = v
void push_back(T v) {p.push_back(v);}
void load(const string& inf, char delim=' ');
T sum() const;
T norm() const;
void normalize(double smoother=0);
void exp_normalize(); // normalize to all exponents of entries sum to 1
Pvec<T> &operator= (const vector<T>& v) {p=v; return *this;}
T &operator[] ( size_t i ) {
assert(i<p.size());
return p[i];
}
const T &operator[] ( size_t i ) const{
assert(i<p.size());
return p[i];
}
Pvec<T> operator+(const T & v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
tp[i] = p[i] + v;
return tp;
}
Pvec<T>& operator+=(const T & v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
p[i] += v;
return *this;
}
Pvec<T> operator+(const Pvec& v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
tp[i] = p[i] + v[i];
return tp;
}
Pvec<T> &operator+=(const Pvec& v){
for (int i=0; i<p.size(); ++i)
p[i] += v[i];
return *this;
}
Pvec<T> operator-(const T & v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
tp[i] = p[i] - v;
return tp;
}
Pvec<T>& operator-=(const T & v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
p[i] -= v;
return *this;
}
Pvec<T> operator-(const Pvec& v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
tp[i] = p[i] - v[i];
return tp;
}
Pvec<T> &operator-=(const Pvec& v){
for (int i=0; i<p.size(); ++i)
p[i] -= v[i];
return *this;
}
Pvec<T> operator*(const T & v){
vector<T> tp(p.size());
for (int i=0; i<p.size(); ++i)
tp[i] = p[i] * v;
return tp;
}
T max() const {
T max_v = -10000000;
for (int i=0; i<p.size(); ++i) {
if (p[i] > max_v) {
max_v = p[i];
}
}
return max_v;
}
int max_idx() const {
T max_v = -10000000;
int idx = 0;
for (int i=0; i<p.size(); ++i) {
if (p[i] > max_v) {
max_v = p[i];
idx = i;
}
}
return idx;
}
size_t size() const {return p.size();}
vector<T> to_vector() {return p;}
string str(char delim=' ') const;
void write(const string& pt);
};
template<class T>
Pvec<T>::Pvec(const string& line) {
p.clear();
istringstream iss(line);
T v;
while (iss >> v) {
p.push_back(v);
}
}
template<class T>
void Pvec<T>::rand_init() {
srand(time(NULL));
for (size_t i = 0; i < p.size(); ++i)
p[i] = rand() % 100 + 1;
normalize();
}
template<class T>
void Pvec<T>::uniform_init() {
for( size_t i = 0 ; i < p.size() ; ++i )
p[i] = double(1) / p.size();
}
template<class T>
void Pvec<T>::bias_init(double v) {
assert( v < 1);
p[0] = v;
for( size_t i = 1 ; i < p.size() ; ++i )
p[i] = (double)((1-v) / (p.size()-1));
}
// load a varible an line, make sure no empty lines
// the number of rows determinates the dimension of vector
template<class T>
void Pvec<T>::load(const string& inf, char delim) {
ifstream rf(inf.c_str());
if (!rf)
EXIT_ERR("file not find:", inf.c_str());
p.clear();
string line;
while (getline(rf, line, delim)) {
istringstream iss(line);
T v;
iss >> v;
p.push_back(v);
}
}
template<class T>
T Pvec<T>::sum() const{
T s = 0;
for( size_t i = 0 ; i < p.size() ; ++i )
s += p[i];
return s;
}
template<class T>
T Pvec<T>::norm() const{
T s = 0;
for( size_t i = 0 ; i < p.size() ; ++i )
s += p[i]*p[i];
return sqrt(s);
}
template<class T>
void Pvec<T>::normalize(double smoother) {
T s = sum();
assert(s>0);
int K = p.size();
// avoid numerical problem
for( size_t i = 0 ; i < K ; ++i ) {
p[i] = (p[i] + smoother)/(s + K*smoother);
}
}
template<class T>
void Pvec<T>::exp_normalize() {
vector<T> tmp(p);
for (size_t i = 0; i < p.size(); ++i ) {
double s = 0.0;
for (size_t j = 0; j < p.size(); ++j )
s += exp( tmp[j] - tmp[i] );
assert(s>=1);
p[i] = 1/s;
}
}
template<class T>
string Pvec<T>::str(char delim) const{
ostringstream os;
size_t i;
for( i = 0 ; i < p.size()-1; ++i )
os << p[i] << delim;
if (i < p.size())
os << p[i];
return os.str();
}
template<class T>
void Pvec<T>::write(const string& pt) {
ofstream wf(pt.c_str());
if (!wf) {
cout << "Path not exists:" << pt << endl;
wf.open("pvec.txt");
}
wf << str() << endl;
}
#endif