gsl_matrix.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef GSLPP_MATRIX_HH
22 #define GSLPP_MATRIX_HH
23 
24 #include <cassert>
25 #include <iterator>
26 #include <gsl/gsl_matrix.h>
27 #include <mia/core/gsl_vector.hh>
28 
29 namespace gsl {
30 
32  friend class const_matrix_iterator;
33 public:
34  matrix_iterator(gsl_matrix *m, bool begin):
35  m_matrix(m),
36  m_current(begin ? m->data : m->data + m->size1 * m->tda),
37  m_current_column(0),
38  m_row_jump(m->tda - m->size2)
39  {
40  }
41 
43  m_matrix(nullptr),
44  m_current(nullptr),
45  m_current_column(0)
46  {
47  }
48 
49 
50  matrix_iterator(const matrix_iterator& other) = default;
51 
52  double& operator *(){
53  assert(m_current);
54  return *m_current;
55  }
56 
57  matrix_iterator& operator ++() {
58  assert(m_current);
59  ++m_current;
60  ++m_current_column;
61  if(m_current_column == m_matrix->size2) {
62  m_current += m_row_jump;
63  m_current_column = 0;
64  }
65  return *this;
66  }
67 
68 
69  matrix_iterator operator ++(int) {
70  matrix_iterator result(*this);
71  ++(*this);
72  return result;
73  }
74 
75  friend bool operator == (const matrix_iterator& lhs, const matrix_iterator& rhs);
76  friend bool operator != (const matrix_iterator& lhs, const matrix_iterator& rhs);
77 private:
78  gsl_matrix *m_matrix;
79  double *m_current;
80  size_t m_current_column;
81  size_t m_row_jump;
82 };
83 
84 bool EXPORT_GSL operator == (const matrix_iterator& lhs, const matrix_iterator& rhs);
85 bool EXPORT_GSL operator != (const matrix_iterator& lhs, const matrix_iterator& rhs);
86 
87 
89 public:
90  const_matrix_iterator(const gsl_matrix *m, bool begin):
91  m_matrix(m),
92  m_current(begin ? m->data : m->data + m->size1 * m->tda),
93  m_current_column(0),
94  m_row_jump(m->tda - m->size2)
95  {
96  }
97 
99  m_matrix(other.m_matrix),
100  m_current(other.m_current),
101  m_current_column(other.m_current_column),
102  m_row_jump(other.m_row_jump)
103  {
104  }
105 
107  m_matrix(nullptr),
108  m_current(nullptr),
109  m_current_column(0)
110  {
111  }
112 
113 
114  const_matrix_iterator(const const_matrix_iterator& other) = default;
115 
116  double operator *() const{
117  assert(m_current);
118  return *m_current;
119  }
120 
121  const_matrix_iterator& operator ++() {
122  assert(m_current);
123  ++m_current;
124  ++m_current_column;
125  if(m_current_column == m_matrix->size2) {
126  m_current += m_row_jump;
127  m_current_column = 0;
128  }
129  return *this;
130  }
131 
132  const_matrix_iterator operator ++(int) {
133  const_matrix_iterator result(*this);
134  ++(*this);
135  return result;
136  }
137 
138  friend bool operator == (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
139  friend bool operator != (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
140 private:
141  const gsl_matrix *m_matrix;
142  const double *m_current;
143  size_t m_current_column;
144  size_t m_row_jump;
145 };
146 
149 
150 
151 
152 
153 
159 public:
162 
163  Matrix();
164 
171  Matrix(size_t rows, size_t columns, bool clean);
172 
179  Matrix(size_t rows, size_t columns, double init);
180 
188  Matrix(size_t rows, size_t columns, const double *init);
189 
193  Matrix(const Matrix& other);
194 
195 
199  Matrix(Matrix&& other);
200 
201 
206  Matrix(gsl_matrix* m);
207 
212  Matrix(const gsl_matrix* m);
213 
218  Matrix transposed() const;
219 
220 
224  Matrix& operator =(const Matrix& other);
225 
229  Matrix& operator =(Matrix&& other);
230 
231 
238  void reset(size_t rows, size_t columns, bool clean);
239 
246  void reset(size_t rows, size_t columns, double init);
247 
248  ~Matrix();
249 
250 
254  size_t rows()const;
255 
259  size_t cols()const;
260 
267  void set(size_t i, size_t j, double x);
268 
276  double operator ()(size_t i, size_t j) const;
277 
281  operator gsl_matrix *();
282 
286  operator const gsl_matrix *() const;
287 
292  Matrix column_covariance() const;
293 
298  Matrix row_covariance() const;
299 
305  matrix_iterator begin();
306 
312  matrix_iterator end();
313 
319  const_matrix_iterator begin() const;
320 
326  const_matrix_iterator end() const;
327 
334  void set_row(int r, const Vector& row);
335 
341  VectorView get_row(int r);
342 
348  ConstVectorView get_row(int r) const;
349 
356  void set_column(int c, const Vector& col);
357 
363  VectorView get_column(int c);
364 
370  ConstVectorView get_column(int c) const;
371 
379  double dot_row(int r, const Vector& v) const;
380 
389  double dot_column(int c, const Vector& col) const;
390 
395  void print(std::ostream& os) const;
396 
402  Matrix& operator -=(const Matrix& rhs) {
403  gsl_matrix_sub(*this, rhs);
404  return *this;
405  }
406 
412  Matrix& operator +=(const Matrix& rhs) {
413  gsl_matrix_add(*this, rhs);
414  return *this;
415  }
416 
422  Matrix& operator *=(double rhs) {
423  gsl_matrix_scale(*this, rhs);
424  return *this;
425  }
426 
427  bool is_valid() const;
428 
429  bool is_writable() const;
430 
431 private:
432  gsl_matrix *m_matrix;
433  const gsl_matrix *m_const_matrix;
434  bool m_owner;
435 };
436 
437 
438 Matrix EXPORT_GSL operator * (const Matrix& lhs, const Matrix& rhs);
439 Matrix EXPORT_GSL operator + (const Matrix& lhs, const Matrix& rhs);
440 Matrix EXPORT_GSL operator - (const Matrix& lhs, const Matrix& rhs);
441 
442 inline std::ostream& operator << (std::ostream& os, const Matrix& m)
443 {
444  m.print(os);
445  return os;
446 }
447 
453 
456 };
457 
463 
464 
465 } // end namespace
466 
467 namespace std {
468 
469 template <>
470 class iterator_traits< gsl::const_matrix_iterator > {
471 public:
472  typedef size_t difference_type;
473  typedef double value_type;
474  typedef const double* pointer;
475  typedef const double& reference;
476  typedef forward_iterator_tag iterator_category;
477 };
478 
479 template <>
480 class iterator_traits< gsl::matrix_iterator > {
481 public:
482  typedef size_t difference_type;
483  typedef double value_type;
484  typedef double* pointer;
485  typedef double& reference;
486  typedef forward_iterator_tag iterator_category;
487 };
488 
489 }
490 
491 
492 #endif
void EXPORT_GSL matrix_inv_sqrt(Matrix &m)
const_matrix_iterator const_iterator
Definition: gsl_matrix.hh:161
matrix_iterator iterator
Definition: gsl_matrix.hh:160
const_matrix_iterator(const gsl_matrix *m, bool begin)
Definition: gsl_matrix.hh:90
EXPORT_2D C2DFVectorfield & operator+=(C2DFVectorfield &a, const C2DFVectorfield &b)
bool EXPORT_GSL operator!=(const matrix_iterator &lhs, const matrix_iterator &rhs)
vector_iterator operator+(const vector_iterator &it, int dist)
vector_iterator operator-(const vector_iterator &it, int dist)
void print(std::ostream &os) const
#define EXPORT_GSL
Definition: gsl_defines.hh:38
matrix_iterator(gsl_matrix *m, bool begin)
Definition: gsl_matrix.hh:34
std::ostream & operator<<(std::ostream &os, const Matrix &m)
Definition: gsl_matrix.hh:442
const_matrix_iterator(const matrix_iterator &other)
Definition: gsl_matrix.hh:98
Matrix EXPORT_GSL operator*(const Matrix &lhs, const Matrix &rhs)
bool EXPORT_GSL operator==(const matrix_iterator &lhs, const matrix_iterator &rhs)