23 #ifndef CPPR_UTILS_H //make sure this only gets included once
31 #include <boost/lexical_cast.hpp>
54 template <
typename T >
57 ublas::vector<T> v(n);
58 boost::uniform_real<T> uni_dist(0,1);
59 boost::variate_generator<base_generator_type&, boost::uniform_real<T> > uni(generator, uni_dist);
60 std::generate(v.data().begin(),v.data().end(),uni);
72 template <
typename T >
76 boost::normal_distribution<double> norm_dist(mean,sd);
77 boost::variate_generator<base_generator_type&, boost::normal_distribution<double> > norm(generator, norm_dist);
78 ublas::vector<double> v(n);
79 std::generate(v.data().begin(),v.data().end(),norm);
92 template <
typename T >
97 for(
unsigned int i=0;i<vec.size();i++)
98 ret.push_back(vec[i]);
108 template <
typename T >
112 ublas::vector<T> ret;
113 ret.resize(vec.size());
114 for(
unsigned int i=0;i<vec.size();i++)
125 template <
typename T >
129 ublas::vector<T> ret = vec;
130 std::random_shuffle(ret.begin(), ret.end());
144 template <
typename T >
146 apply(
const ublas::matrix<T> &m, T (*func)(T))
148 ublas::matrix<T> ret(m.size1(),m.size2());
149 for(
unsigned i=0;i<m.size1();i++)
150 for(
unsigned j=0;j<m.size2();j++)
151 ret(i,j) = func(m(i,j));
162 template <
typename T,
typename U >
164 apply(
const ublas::matrix<T> &m, T (*func)(T, U), U arg2)
166 ublas::matrix<T> ret(m.size1(),m.size2());
167 for(
unsigned int i=0;i<m.size1();i++)
168 for(
unsigned int j=0;j<m.size2();j++)
169 ret(i,j) = func(m(i,j), arg2);
179 template <
typename T >
181 apply(
const ublas::vector<T> &m, T (*func)(T))
183 ublas::vector<T> ret(m.size());
184 for(
unsigned i=0;i<m.size();i++)
201 template <
typename T >
203 rowApply(
const ublas::matrix<T> &m, T (*func)(
const ublas::vector<T>&))
205 ublas::vector<T> ret;
206 ret.resize(m.size1());
208 for(
unsigned i=0;i<m.size1();i++)
211 T res = func(ublas::vector<T>(row(m,i)));
227 template <
typename T >
229 columnApply(
const ublas::matrix<T> &m, T (*func)(
const ublas::vector<T>&))
231 ublas::vector<T> ret;
232 ret.resize(m.size1());
234 for(
unsigned i=0;i<m.size2();i++)
237 T res = func(ublas::vector<T>(column(m,i)));
249 template <
typename T >
253 cout.setf(ios::fixed,ios::floatfield);
255 for(
unsigned int i=0;i<m.size1();i++)
257 for(
unsigned int j=0;j<m.size2(); j++)
261 cout << m(i,j) <<
"\t";
270 template <
typename T >
274 cout.setf(ios::fixed,ios::floatfield);
276 for(
unsigned int i=0;i<v.size();i++)
278 cout << v[i] <<
"\t";
287 template <
typename T >
291 cout.setf(ios::fixed,ios::floatfield);
293 for(
unsigned int i=0;i<v.size();i++)
295 cout << v[i] <<
"\t";
306 template <
typename T >
310 cout <<
"[" << m.size1() <<
" " << m.size2() <<
"]\n";
320 template <
typename T >
325 ofile.open(filename.c_str());
330 for(
unsigned int i=0;i<m.size1();i++)
332 for(
unsigned int j=0;j<m.size2(); j++)
334 ofile << m(i,j) <<
" ";
346 template <
typename T >
350 ublas::matrix<T> ret;
352 ifile.open(filename.c_str());
365 ifile.getline(buffer,
sizeof(buffer),
'\n');
373 std::vector<T> row_vector;
374 stringstream line_ss;
378 while(!line_ss.eof())
384 val_temp = boost::lexical_cast<T>(str_temp);
386 catch(...){
continue; }
387 row_vector.push_back(val_temp);
390 if(row_vector.size() > 0)
392 ret.resize(ret.size1()+1, row_vector.size());