CEBL  2.1
cppR_manipulation.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_manipulation.hpp
20 // Matrix and Vector manipulation and slicing
21 
22 #ifndef CPPR_MANIPULATION_H //make sure this only gets included once
23 #define CPPR_MANIPULATION_H
24 
25 
26 #include <cppR/cppR_includes.hpp>
27 
28 
29 //--------------------------------------------------
30 
31 
32 namespace cppR
33 {
34  //==============================
52  template < typename T >
53  ublas::matrix_slice<ublas::matrix<T> >
54  submatrix(ublas::matrix<T> &m, int r1,int r2,int c1,int c2)
55  {
56  if(r2 == 0)
57  r2 = m.size1()-1;
58  if(c2 == 0)
59  c2 = m.size2()-1;
60 
61  //make upper bound inclusive
62  r2++;
63  c2++;
64  cppR_assert(r1 <= r2,
65  "in submatrix: row start must be less than or equal to row end"
66  );
67  cppR_assert(c1 <= c2,
68  "in submatrix: col start must be less than or equal to col end"
69  );
70  cppR_assert(r1 >= 0,
71  "in submatrix: row start must be greater than or equal to 0");
72  cppR_assert(c1 >= 0,
73  "in submatrix: col start must be greater than or equal to 0"
74  );
75  cppR_assert(unsigned(r2-r1) <= (m.size1()),
76  "in submatrix: rows range larger than rows of matrix"
77  );
78  cppR_assert(unsigned(c2-c1) <= (m.size2()),
79  "in submatrix: col range larger than columns of matrix"
80  );
81  return subslice(m,r1,1,r2-r1,c1,1,c2-c1);
82  }
83 
84  //==============================
97  template < typename T >
98  const ublas::matrix_slice<ublas::matrix<T> >
99  submatrix(const ublas::matrix<T> &m, int r1,int r2,int c1,int c2)
100  {
101  if(r2 == 0)
102  r2 = m.size1()-1;
103  if(c2 == 0)
104  c2 = m.size2()-1;
105 
106  //make upper bound inclusive
107  r2++;
108  c2++;
109 
110  cppR_assert(r1 <= r2,
111  "in submatrix: row start must be less than or equal to row end"
112  );
113  cppR_assert(c1 <= c2,
114  "in submatrix: col start must be less than or equal to col end"
115  );
116  cppR_assert(r1 >= 0,
117  "in submatrix: row start must be greater than or equal to 0"
118  );
119  cppR_assert(c1 >= 0,
120  "in submatrix: col start must be greater than or equal to 0"
121  );
122  cppR_assert(r2-r1 <= m.size1(),
123  "in submatrix: rows range larger than rows of matrix"
124  );
125  cppR_assert(c2-c1 <= m.size2(),
126  "in submatrix: col range larger than columns of matrix"
127  );
128  ublas::matrix<T> m1 = m;
129 
130  return subslice(m1,r1,1,r2-r1,c1,1,c2-c1);
131  }
132 
133 
134 
135  //==============================
136  //submatrixAssign
152  template < typename T >
153  void submatrixAssign(ublas::matrix<T> &m1,
154  int m1start1, int m1end1, int m1start2, int m1end2,
155  const ublas::matrix<T> &m2,
156  int m2start1, int m2end1, int m2start2, int m2end2)
157  {
158  //make sure dimensions are right
159  if(m1end1-m1start1!=m2end1-m2start1)
160  {
161  std::cerr << "ERROR: number of rows to copy differs\n";
162  return;
163  }
164  if(m1end2-m1start2!=m2end2-m2start2)
165  {
166  std::cerr << "ERROR: number of cols to copy differs\n";
167  return;
168  }
169  ublas::matrix_slice<ublas::matrix<T> >
170  slice1(m1,
171  ublas::slice(m1start1,1,m1end1-m1start1),
172  ublas::slice(m1start2,1,m1end2-m1start2)
173  );
174 
175  //TODO: remove this pointless copy...
176  ublas::matrix<T> m3 = m2;
177 
178  ublas::matrix_slice<ublas::matrix<T> >
179  slice2(m3,
180  ublas::slice(m2start1,1,m2end1-m2start1),
181  ublas::slice(m2start2,1,m2end2-m2start2)
182  );
183  slice1 = slice2;
184  }
185 
186 
187  //==============================
195  template < typename T >
196  std::vector<bool> createMask(T value, ublas::vector<T> v)
197  {
198  std::vector<bool> ret(v.size());
199  for(unsigned int i=0; i<v.size(); i++)
200  ret[i] = v[i]==value;
201  return ret;
202  }
203 
204 
205  //==============================
214  template < typename T >
215  ublas::matrix<T> rowMask(const ublas::matrix<T> &m,
216  std::vector<bool> mask)
217  {
218  ublas::matrix<T> ret(count(mask.begin(), mask.end(), true),m.size2());
219  int count = 0;
220  for(unsigned int i=0; i<mask.size(); i++)
221  {
222  if(mask[i])
223  row(ret,count++) = row(m, i);
224  }
225  return ret;
226  }
227 
228  //==============================
236  template < typename T >
237  ublas::matrix<T> cbind(const ublas::matrix<T> &m1,
238  const ublas::matrix<T> &m2)
239  {
240 
241  ublas::matrix<T> ret = m1;
242 
243  //assert that the matrices have the same number of rows
244  cppR_assert(m1.size1() == m2.size1(),
245  "in cbind: rows of matrices must match");
246 
247  int original_size2 = m1.size2();
248  ret.resize(m1.size1(), m1.size2() + m2.size2());
249 
250  submatrix(ret,
251  0, m1.size1()-1,
252  original_size2, original_size2 + m2.size2()-1) = m2;
253 
254  return ret;
255  }
256 
257 
258  //==============================
266  template < typename T >
267  ublas::matrix<T> rbind(const ublas::matrix<T> &m1,
268  const ublas::matrix<T> &m2)
269  {
270 
271  ublas::matrix<T> ret = m1;
272 
273  //assert that the matrices have the same number of columns
274  cppR_assert(m1.size2() == m2.size2(),
275  "in rbind: columns of matrices must match");
276 
277 
278  int original_size1 = m1.size1();
279  ret.resize(m1.size1()+m2.size1(), m1.size2());
280 
281  submatrix(ret,
282  original_size1, original_size1 + m2.size1()-1,
283  0, m1.size2()-1) = m2;
284 
285  return ret;
286  }
287 
288  //==============================
296  template < typename T >
297  ublas::matrix<T> rev(const ublas::matrix<T> & m1)
298  {
299  ublas::matrix<T> ret;
300  int size = m1.size1() * m1.size2();
301  ret.resize(size,1);
302 
303  int count = 0;
304  for(unsigned int i=0;i<m1.size1();i++)
305  {
306  for(unsigned int j=0;j<m1.size2();j++)
307  {
308  ret(count++,0) = m1(i,j);
309  }
310  }
311  return ret;
312  }
313 
314  //==============================
320  template < typename T >
321  ublas::vector<T> rev(const ublas::vector<T> & v)
322  {
323  ublas::vector<T> ret;
324  int count = 0;
325  for(unsigned int i=0;i<v.size();i++)
326  {
327  ret[count++] = v[i];
328 
329  }
330  return ret;
331  }
332 
333 
334 }//end of namespace
335 
336 
337 #endif