CEBL  2.1
cppR_construction.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 //--------------------------------------------------
20 // cppR_construction.hpp
21 // Matrix and Vector construction and manipulation functions
22 
23 #ifndef CPPR_CONSTRUCTION_H //make sure this only gets included once
24 #define CPPR_CONSTRUCTION_H
25 
26 
27 #include <cppR/cppR_includes.hpp>
28 
29 
30 namespace cppR
31 {
32  //==============================
33 
39  template < typename T >
40  ublas::vector<T> createVector(const ublas::matrix<T> &m)
41  {
42  ublas::vector<T> ret(m.size1()* m.size2());
43  int count = 0;
44  for(unsigned int i=0; i<m.size1(); i++)
45  {
46  for(unsigned int j=0; j<m.size2(); j++)
47  ret(count++) = m(i,j);
48  }
49  return ret;
50  }
51 
52 
60  template < typename T >
61  ublas::vector<T> createVector(const T& value, int size)
62  {
63  ublas::vector<T> ret(size);
64  for(int i=0; i<size; i++)
65  {
66  ret(i) = value;
67  }
68  return ret;
69  }
70 
71  //==============================
72  //createMatrix
73 
75 
89  template < typename T >
90  ublas::matrix<T>
91  createMatrix(const ublas::vector<T> &vec,
92  int rows, int cols, bool byrow=false)
93  {
94  int size = vec.size();
95  if(rows <= 0 && cols > 0)
96  rows = size/cols;
97  else if(cols <= 0 && rows > 0)
98  cols = size / rows;
99  else if(rows <= 0 && cols <=0)
100  {
101  rows = 1;
102  cols = size;
103  }
104 
105  ublas::matrix<T> new_matrix(rows,cols);
106  for(int i=0; i<rows; i++)
107  {
108  for(int j=0; j<cols; j++)
109  {
110  if(byrow)
111  {
112  new_matrix(i,j) = vec[(i*rows + j) % size];
113  }
114  else
115  {
116  new_matrix(i,j) = vec[(j*rows + i) % size];
117  }
118  }
119  }
120  return new_matrix;
121  }
122 
131  template < typename T >
132  ublas::matrix<T>
133  createMatrix(const std::vector<T> &vec, int rows, int cols, bool byrow=false)
134  {
135  ublas::vector<T> v;
136  v.resize(vec.size());
137  std::copy(vec.begin(), vec.end(), v.begin());
138  return createMatrix(v,rows,cols,byrow);
139  }
140 
141 
149  template < typename T >
150  ublas::matrix<T>
151  createMatrix(T value, int rows, int cols)
152  {
153  int size = 1;
154  if(rows <= 0 && cols > 0)
155  rows = size/cols;
156  else if(cols <= 0 && rows > 0)
157  cols = size / rows;
158  else if(rows <= 0 && cols <=0)
159  {
160  rows = 1;
161  cols = size;
162  }
163  ublas::matrix<T> new_matrix(rows,cols);
164  for(int i=0; i<rows; i++)
165  {
166  for(int j=0; j<cols; j++)
167  {
168  new_matrix(i,j) = value;
169  }
170  }
171  return new_matrix;
172  }
173 
174  //==============================
181  template < typename T >
182  ublas::vector<T> bind(ublas::vector<T> v1, ublas::vector<T> v2)
183  {
184  ublas::vector<T> ret = v1;
185  ret.resize(v1.size()+v2.size());
186  for(unsigned int i = v1.size(); i < v2.size(); i++)
187  {
188  ret[i] = v2[i-v1.size()];
189  }
190 
191  return ret;
192  }
193 
194  //==============================
201  template < typename T >
202  ublas::vector<T>
203  rep(const ublas::vector<T> &vec, int n)
204  {
205  unsigned int size = vec.size();
206  ublas::vector<T> new_vector(size * n);
207  for( int i=0; i<n; i++)
208  {
209  for(unsigned int j=0; j < size; j++)
210  {
211  new_vector[i*size+j] = vec[j];
212  }
213  }
214  return new_vector;
215  }
216 
217 
224  template < typename T >
225  ublas::vector<T>
226  rep(const T &value, int n)
227  {
228  ublas::vector<T> new_vector(n);
229  for( int i=0; i<n; i++)
230  {
231  new_vector[i] = value;
232  }
233  return new_vector;
234  }
235 
236  //==============================
237  //diag
238 
244  template < typename T >
245  ublas::vector<T> diag(const ublas::matrix<T> &m)
246  {
247  ublas::vector<T> ret(m.size1());
248  for(unsigned int i=0; i<m.size1(); i++)
249  {
250  if(i<m.size2())
251  ret[i] = m(i,i);
252  else
253  break;
254  }
255  return ret;
256  }
257 
263  template < typename T >
264  ublas::matrix<T> diag(const ublas::vector<T> &v)
265  {
266  ublas::matrix<T> ret = createMatrix(
267  rep(static_cast<T>(0),
268  v.size() * v.size()),
269  v.size(),
270  v.size()
271  );
272  for(unsigned int i=0; i<v.size(); i++)
273  {
274  ret(i, i) = v[i];
275  }
276  return ret;
277  }
278 
286  template < typename T >
287  ublas::matrix<T> diag(const T &value, int size)
288  {
289  ublas::matrix<T> ret = createMatrix(
290  rep(static_cast<T>(0), size * size),
291  size,
292  size
293  );
294  for(int i=0; i<size; i++)
295  {
296  ret(i, i) = value;
297  }
298  return ret;
299  }
300 
301 
302 
303  //==============================
310  template < typename T >
311  ublas::vector<T>
313  {
314  cppR_assert(max > min, "Max must be greater than min in vecRange.");
315 
316  ublas::vector<int> ret;
317  int size = max - min + 1;
318  ret.resize(size);
319  int val = min;
320  for(int i=0;i<size;i++)
321  {
322  ret[i] = val;
323  val++;
324  }
325  return ret;
326  }
327 
328 
329 }//end of namepsace
330 
331 
332 //--------------------------------------------------
333 
334 
335 
336 #endif