CEBL  2.1
cppR_utils.hpp
Go to the documentation of this file.
1 /*
2 * CEBL : CSU EEG Brain-Computer Interface Lab
3 *
4 * Author: Jeshua Bratman - jeshuabratman@gmail.com
5 *
6 * This file is part of CEBL.
7 *
8 * CEBL is free software; you can redistribute it and/or modify it.
9 * We only ask that if you use our code that you cite the source in
10 * your project or publication.
11 *
12 * EEG Group (www.cs.colostate.edu/eeg)
13 * Department of Computer Science
14 * Colorado State University
15 *
16 */
17 
18 //--------------------------------------------------
19 // cppR_utils.hpp
20 // Matrix and Vector utilities
21 
22 
23 #ifndef CPPR_UTILS_H //make sure this only gets included once
24 #define CPPR_UTILS_H
25 
26 #include <fstream>
27 #include <algorithm>
28 #include <numeric>
29 #include <vector>
30 #include <iterator>
31 #include <boost/lexical_cast.hpp>
32 using namespace std;
33 
34 #include <cppR/cppR_includes.hpp>
35 
36 //--------------------------------------------------
37 
38 
39 //create a global random number generator
40 typedef boost::mt19937 base_generator_type;
41 static base_generator_type generator(time(NULL));
42 
43 //--------------------------------------------------
44 
45 
46 namespace cppR
47 {
48  //==============================
54  template < typename T >
55  ublas::vector<T> runif(int n)
56  {
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);
61  return v;
62  };
63 
64  //==============================
72  template < typename T >
73  ublas::vector<T>
74  rnorm(int n, double mean, double sd)
75  {
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);
80  return v;
81  }
82 
83 
84 
85 
86  //==============================
92  template < typename T >
93  std::vector<T>
94  asStdVector(ublas::vector<T> vec)
95  {
96  std::vector<T> ret;
97  for(unsigned int i=0;i<vec.size();i++)
98  ret.push_back(vec[i]);
99  return ret;
100  }
101 
102  //==============================
108  template < typename T >
109  ublas::vector<T>
110  asUblasVector(std::vector<T> vec)
111  {
112  ublas::vector<T> ret;
113  ret.resize(vec.size());
114  for(unsigned int i=0;i<vec.size();i++)
115  ret(i) = vec[i];
116  return ret;
117  }
118 
119  //==============================
125  template < typename T >
126  ublas::vector<T>
127  sample(ublas::vector<T> vec)
128  {
129  ublas::vector<T> ret = vec;
130  std::random_shuffle(ret.begin(), ret.end());
131  return ret;
132  }
133 
134  //==============================
135  //apply
136 
137 
144  template < typename T >
145  ublas::matrix<T>
146  apply(const ublas::matrix<T> &m, T (*func)(T))
147  {
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));
152  return ret;
153  }
154 
162  template < typename T, typename U >
163  ublas::matrix<T>
164  apply(const ublas::matrix<T> &m, T (*func)(T, U), U arg2)
165  {
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);
170  return ret;
171  }
172 
179  template < typename T >
180  ublas::vector<T>
181  apply(const ublas::vector<T> &m, T (*func)(T))
182  {
183  ublas::vector<T> ret(m.size());
184  for(unsigned i=0;i<m.size();i++)
185  ret(i) = func(m(i));
186  return ret;
187  }
188 
189  //==============================
190 
201  template < typename T >
202  ublas::vector<T>
203  rowApply(const ublas::matrix<T> &m, T (*func)(const ublas::vector<T>&))
204  {
205  ublas::vector<T> ret;
206  ret.resize(m.size1());
207  //loop through rows of m
208  for(unsigned i=0;i<m.size1();i++)
209  {
210  //get the result of the function on this row
211  T res = func(ublas::vector<T>(row(m,i)));
212  ret[i] = res;
213  }
214  return ret;
215  }
216 
227  template < typename T >
228  ublas::vector<T>
229  columnApply(const ublas::matrix<T> &m, T (*func)(const ublas::vector<T>&))
230  {
231  ublas::vector<T> ret;
232  ret.resize(m.size1());
233  //loop through cols of m
234  for(unsigned i=0;i<m.size2();i++)
235  {
236  //get the result of the function on this row
237  T res = func(ublas::vector<T>(column(m,i)));
238  ret[i] = res;
239  }
240  return ret;
241  }
242 
243 
244 
245  //=======================================
249  template < typename T >
250  void
251  printMatrix(const ublas::matrix<T> &m)
252  {
253  cout.setf(ios::fixed,ios::floatfield);
254  cout.precision(5);
255  for(unsigned int i=0;i<m.size1();i++)
256  {
257  for(unsigned int j=0;j<m.size2(); j++)
258  {
259  if(m(i,j) >= 0)
260  cout << " ";
261  cout << m(i,j) << "\t";
262  }
263  cout << "\n";
264  }
265  }
266  //=======================================
270  template < typename T >
271  void
272  printVector(const ublas::vector<T> &v)
273  {
274  cout.setf(ios::fixed,ios::floatfield);
275  cout.precision(5);
276  for(unsigned int i=0;i<v.size();i++)
277  {
278  cout << v[i] << "\t";
279  }
280  cout << "\n";
281 
282  }
283  //=======================================
287  template < typename T >
288  void
289  printVector(const std::vector<T> &v)
290  {
291  cout.setf(ios::fixed,ios::floatfield);
292  cout.precision(5);
293  for(unsigned int i=0;i<v.size();i++)
294  {
295  cout << v[i] << "\t";
296  }
297  cout << "\n";
298  }
299 
300 
301  //=======================================
302 
306  template < typename T >
307  void
308  printMatrixDim(const ublas::matrix<T> &m)
309  {
310  cout << "[" << m.size1() << " " << m.size2() << "]\n";
311  }
312 
313  //=======================================
314 
320  template < typename T >
321  void
322  writeTable(const ublas::matrix<T> &m, string filename)
323  {
324  ofstream ofile;
325  ofile.open(filename.c_str());
326 
327  if(ofile.fail())
328  return;
329 
330  for(unsigned int i=0;i<m.size1();i++)
331  {
332  for(unsigned int j=0;j<m.size2(); j++)
333  {
334  ofile << m(i,j) << " ";
335  }
336  ofile << "\n";
337  }
338 
339  ofile.close();
340  }
341 
346  template < typename T >
347  ublas::matrix<T>
348  readTable(string filename)
349  {
350  ublas::matrix<T> ret;
351  ifstream ifile;
352  ifile.open(filename.c_str());
353 
354  if(ifile.fail())
355 
356  return ret;
357 
358  //loop through lines of file
359  char buffer[1024];
360  string str_temp;
361  T val_temp;
362  int i = 0;
363  while(!ifile.eof())
364  {
365  ifile.getline(buffer,sizeof(buffer),'\n');
366  //skip first line
367  if(i <= 0)
368  {
369  i++;
370  continue;
371  }
372 
373  std::vector<T> row_vector;
374  stringstream line_ss;
375  line_ss << buffer ;
376  line_ss >> str_temp;
377  //loop through whitespace delimited strings
378  while(!line_ss.eof())
379  {
380  line_ss >> str_temp;
381  //attempt to parse string as double
382  try
383  {
384  val_temp = boost::lexical_cast<T>(str_temp);
385  }
386  catch(...){ continue; }
387  row_vector.push_back(val_temp);
388  }
389  //set row of matrix
390  if(row_vector.size() > 0)
391  {
392  ret.resize(ret.size1()+1, row_vector.size());
393  row(ret,ret.size1()-1) = cppR::asUblasVector(row_vector);
394  }
395  i++;
396  }
397 
398  ifile.close();
399  return ret;
400  }
401 
402 
403 
404 }//end of namespace
405 
406 #endif