KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_mat_view.h
Go to the documentation of this file.
1 /* $Id: m_mat_view.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_M_CPP_MAT_VIEW
21 #define KJB_M_CPP_MAT_VIEW
22 
23 #include <l_cpp/l_exception.h>
24 #include <l_cpp/l_index.h>
25 
26 namespace kjb
27 {
28 
54 template <class Matrix_type>
56 {
57 public:
59  typedef typename Matrix_type::Value_type Value_type;
60  typedef typename Matrix_type::Vec_type Vec_type;
61  typedef int Size_type;
62 
64  Matrix_type& mat,
65  const Index_range& row_indices,
66  const Index_range& col_indices) :
67  row_indices_(row_indices),
68  col_indices_(col_indices),
69  mat_(mat)
70  {
71  }
72 
73  Generic_matrix_view(Matrix_type& mat) :
74  row_indices_(true), // "all" type range
75  col_indices_(true), // "all" type range
76  mat_(mat)
77  {
78  }
79 
81  row_indices_(src.row_indices_), // "all" type range
82  col_indices_(src.col_indices_), // "all" type range
83  mat_(src.mat_)
84  {
85  }
86 
87  // ----------------------
88  // MATRIX-LIKE INTERFACE:
89  // ----------------------
90 
91  bool operator==(const Self& other)
92  {
93  return equality(other);
94  }
95 
96  bool operator==(const Matrix_type& other)
97  {
98  return equality(other);
99  }
100 
101  bool operator!=(const Self& other)
102  {
103  return !equality(other);
104  }
105 
106  bool operator!=(const Matrix_type& other)
107  {
108  return !equality(other);
109  }
110 
114  Value_type& operator()(int r, int c)
115  {
116  int row = row_indices_[r];
117  int col = col_indices_[c];
118  return mat_.at(row, col);
119  }
120 
124  Value_type operator()(int r, int c) const
125  {
126  int row = row_indices_[r];
127  int col = col_indices_[c];
128  return mat_.at(row,col);
129  }
130 
132  {
133  int row, col;
134  compute_row_col( i, &row, &col );
135  return operator()(row, col);
136  }
137 
139  {
140  int row, col;
141  compute_row_col( i, &row, &col );
142  return operator()(row, col);
143 
144  }
145 
147  {
148  return operator()(i);
149  }
150 
152  {
153  return operator()(i);
154  }
155 
157  {
158  // "all"-type ranges can't know their size, so we provide it
159  if(row_indices_.all()) return mat_.get_num_rows();
160  return row_indices_.size();
161  }
162 
164  {
165  // "all"-type ranges can't know their size, so we provide it
166  if(col_indices_.all()) return mat_.get_num_cols();
167  return col_indices_.size();
168  }
169 
170  Self& operator=(const Matrix_type& mat)
171  {
172  if(&mat_ == &mat)
173  {
174  // avoid modifying what we're reading from
175  // copy first, then assign
176  return assign(Matrix_type(mat));
177  }
178 
179  return assign(mat);
180  }
181 
182  Self& operator=(const Self& mat)
183  {
184  // do they refer to the same matrix?
185  if(&mat_ == &mat.mat_)
186  {
187  // avoid modifying what we're reading from
188  // copy first, then assign
189  return assign(Matrix_type(mat));
190  }
191  return assign(mat);
192  }
193 
198  {
199  const int NUM_ROWS = get_num_rows();
200  const int NUM_COLS = get_num_cols();
201 
202  for(int row = 0; row < NUM_ROWS; row++)
203  for(int col = 0; col < NUM_COLS; col++)
204  {
205  operator()(row, col) = s;
206  }
207 
208  return *this;
209  }
215  {
216  return assign_from_vector(v);
217  }
218 
223  Self& operator=(const std::vector<double>& v)
224  {
225  return assign_from_vector(v);
226  }
227 
232  Self& operator=(const std::vector<float>& v)
233  {
234  return assign_from_vector(v);
235  }
236 
237 // Disabled these, since you have to cast to a Matrix anyway, just
238 // let implicit conversion handle it.
239 //
240 // Matrix_type operator*(Value_type s)
241 // {
242 // return Matrix(*this) *= s;
243 // }
244 
245 // Matrix_type operator*(const Matrix_view& s)
246 // {
247 // return Matrix(*this) * Matrix(s);
248 // }
249 
251  {
252  const int NUM_ROWS = get_num_rows();
253  const int NUM_COLS = get_num_cols();
254  for(int row = 0; row < NUM_ROWS; row++)
255  for(int col = 0; col < NUM_COLS; col++)
256  operator()(row, col) *= s;
257 
258  return *this;
259  }
260 
261  /*
262  * This doesn't make sense, since the output dimension could be different from input dimension
263  Self& operator*=(const Matrix_view& s)
264  {
265  }
266  */
267 
269  {
270  const int NUM_ROWS = get_num_rows();
271  const int NUM_COLS = get_num_cols();
272  for(int row = 0; row < NUM_ROWS; row++)
273  for(int col = 0; col < NUM_COLS; col++)
274  operator()(row, col) /= s;
275 
276  return *this;
277  }
278 
279  Self& operator+=(const Self& s)
280  {
281  // do they refer to the same matrix?
282  if(&mat_ == &s.mat_)
283  {
284  // avoid modifying what we're reading from
285  // copy first, then assign
286  return plus_equals(Matrix_type(s));
287  }
288  return plus_equals(s);
289  }
290 
291  Self& operator+=(const Matrix_type& s)
292  {
293  return plus_equals(s);
294  }
295 
296  Self& operator-=(const Self& s)
297  {
298  // do they refer to the same matrix?
299  if(&mat_ == &s.mat_)
300  {
301  // avoid modifying what we're reading from
302  // copy first, then assign
303  return minus_equals(Matrix_type(s));
304  }
305  return minus_equals(s);
306  }
307 
308  Self& operator-=(const Matrix_type& s)
309  {
310  return minus_equals(s);
311  }
312 //
313 // Vector_type operator*(const Vector_type& v)
314 // {
315 // return Matrix(*this) * v;
316 // }
317 protected:
328  int index,
329  int* row,
330  int* col
331  ) const
332  {
333  // Test program was HERE.
334  *row = index / get_num_cols();
335  *col = index - *row * get_num_cols();
336  }
337 
342  template <class Generic_matrix>
343  bool equality(const Generic_matrix& mat)
344  {
345  const int NUM_ROWS = get_num_rows();
346  const int NUM_COLS = get_num_cols();
347  if(mat.get_num_rows() != NUM_ROWS ||
348  mat.get_num_cols() != NUM_COLS )
349  {
351  }
352 
353  for(int row = 0; row < get_num_rows(); row++)
354  {
355  for(int col = 0; col < get_num_cols(); col++)
356  {
357  if(operator()(row, col) != mat(row, col))
358  {
359  return false;
360  }
361  }
362  }
363 
364  return true;
365  }
366 
371  template <class Generic_matrix>
372  Self& assign(const Generic_matrix& mat)
373  {
374  const int NUM_ROWS = get_num_rows();
375  const int NUM_COLS = get_num_cols();
376  if(mat.get_num_rows() != NUM_ROWS ||
377  mat.get_num_cols() != NUM_COLS )
378  {
380  }
381 
382  for(int row = 0; row < get_num_rows(); row++)
383  {
384  for(int col = 0; col < get_num_cols(); col++)
385  {
386  operator()(row, col) = mat(row, col);
387  }
388  }
389 
390  return *this;
391  }
392 
397  template <class T>
399  {
400  bool transpose = false;
401 
402  const int NUM_ROWS = get_num_rows();
403  const int NUM_COLS = get_num_cols();
404 
405  if(NUM_COLS != 1)
406  {
407  transpose = true;
408 
409  if(NUM_ROWS != 1)
410  {
412  }
413  }
414 
415  if(!transpose)
416  {
417  if(v.size() != NUM_ROWS)
418  {
420  }
421  }
422  else
423  {
424  if(v.size() != NUM_COLS)
425  {
427  }
428  }
429 
430  for(int i = 0; i < v.size(); i++)
431  {
432  if(!transpose)
433  {
434  operator()(i, 0) = v[i];
435  }
436  else
437  {
438  operator()(0, i) = v[i];
439  }
440  }
441 
442  return *this;
443  }
444 
445 
450  template <class Generic_matrix>
451  Self& plus_equals(const Generic_matrix& mat)
452  {
453  const int NUM_ROWS = get_num_rows();
454  const int NUM_COLS = get_num_cols();
455  if(NUM_ROWS != mat.get_num_rows() ||
456  NUM_COLS != mat.get_num_cols())
457  {
459  }
460 
461  for(int row = 0; row < NUM_ROWS; row++)
462  {
463  for(int col = 0; col < NUM_COLS; col++)
464  {
465  operator()(row, col) += mat(row, col);
466 
467  }
468  }
469 
470  return *this;
471 
472  }
473 
478  template <class Generic_matrix>
479  Self& minus_equals(const Generic_matrix& mat)
480  {
481  const int NUM_ROWS = get_num_rows();
482  const int NUM_COLS = get_num_cols();
483  if(NUM_ROWS != mat.get_num_rows() ||
484  NUM_COLS != mat.get_num_cols())
485  {
487  }
488 
489  for(int row = 0; row < NUM_ROWS; row++)
490  {
491  for(int col = 0; col < NUM_COLS; col++)
492  {
493  operator()(row, col) -= mat(row, col);
494 
495  }
496  }
497 
498  return *this;
499 
500  }
501 protected:
504  Matrix_type& mat_;
505 
506 };
507 
508 // "template typedef"
509 // see: http://www.gotw.ca/gotw/079.htm
510 template <class Matrix_type>
512 {
514 };
515 
516 
519 } // namespace kjb
520 
521 #endif
Size_type get_num_rows() const
Definition: m_mat_view.h:156
Self & operator-=(const Self &s)
Definition: m_mat_view.h:296
Value_type & operator()(int r, int c)
Definition: m_mat_view.h:114
Self & operator=(const Self &mat)
Definition: m_mat_view.h:182
int Size_type
Definition: m_mat_view.h:61
Generic_matrix_view(Matrix_type &mat)
Definition: m_mat_view.h:73
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Self & minus_equals(const Generic_matrix &mat)
Definition: m_mat_view.h:479
Matrix_type & mat_
Definition: m_mat_view.h:504
Value_type & operator[](int i)
Definition: m_mat_view.h:146
size_t size() const
Definition: l_index.h:273
Size_type get_num_cols() const
Definition: m_mat_view.h:163
Definition: m_mat_view.h:511
r
Definition: APPgetLargeConnectedEdges.m:127
#define KJB_THROW(ex)
Definition: l_exception.h:46
bool all() const
Definition: l_index.h:290
Index_range col_indices_
Definition: m_mat_view.h:503
Matrix_type::Value_type Value_type
Definition: m_mat_view.h:59
Self & operator=(const Vec_type &v)
Definition: m_mat_view.h:214
bool operator!=(const Self &other)
Definition: m_mat_view.h:101
Value_type operator()(int r, int c) const
Definition: m_mat_view.h:124
Definition: m_mat_view.h:55
void compute_row_col(int index, int *row, int *col) const
This quickly computes row and column indices, assuming the Matrix has at least one column...
Definition: m_mat_view.h:327
Self & operator=(const Matrix_type &mat)
Definition: m_mat_view.h:170
Value_type operator[](int i) const
Definition: m_mat_view.h:151
Index_range row_indices_
Definition: m_mat_view.h:502
const Generic_matrix_view< const Matrix_type > Type
Definition: m_mat_view.h:513
Self & assign_from_vector(const T &v)
Definition: m_mat_view.h:398
bool operator!=(const Matrix_type &other)
Definition: m_mat_view.h:106
Self & operator=(const std::vector< double > &v)
Definition: m_mat_view.h:223
Definition: l_index.h:65
Matrix_type::Vec_type Vec_type
Definition: m_mat_view.h:60
Value_type & operator()(int i)
Definition: m_mat_view.h:131
Self & operator+=(const Matrix_type &s)
Definition: m_mat_view.h:291
bool operator==(const Self &other)
Definition: m_mat_view.h:91
Generic_matrix_view(Matrix_type &mat, const Index_range &row_indices, const Index_range &col_indices)
Definition: m_mat_view.h:63
Value_type operator()(int i) const
Definition: m_mat_view.h:138
Self & operator-=(const Matrix_type &s)
Definition: m_mat_view.h:308
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
bool equality(const Generic_matrix &mat)
Definition: m_mat_view.h:343
Generic_matrix_view Self
Definition: m_mat_view.h:58
Generic_matrix_view(const Generic_matrix_view &src)
Definition: m_mat_view.h:80
Support for error handling exception classes in libKJB.
bool operator==(const Matrix_type &other)
Definition: m_mat_view.h:96
Self & operator=(const Value_type &s)
Definition: m_mat_view.h:197
Self & operator/=(Value_type s)
Definition: m_mat_view.h:268
Self & operator=(const std::vector< float > &v)
Definition: m_mat_view.h:232
Self & operator+=(const Self &s)
Definition: m_mat_view.h:279
Self & plus_equals(const Generic_matrix &mat)
Definition: m_mat_view.h:451
Self & operator*=(Value_type s)
Definition: m_mat_view.h:250
Self & assign(const Generic_matrix &mat)
Definition: m_mat_view.h:372