gsl_matrix.hh
Go to the documentation of this file.
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GSLPP_MATRIX_HH
#define GSLPP_MATRIX_HH
#include <cassert>
#include <iterator>
#include <gsl/gsl_matrix.h>
#include <mia/core/gsl_vector.hh>
namespace gsl
{
class EXPORT_GSL matrix_iterator
{
friend class const_matrix_iterator;
public:
matrix_iterator(gsl_matrix *m, bool begin):
m_matrix(m),
m_current(begin ? m->data : m->data + m->size1 * m->tda),
m_current_column(0),
m_row_jump(m->tda - m->size2)
{
}
matrix_iterator():
m_matrix(nullptr),
m_current(nullptr),
m_current_column(0)
{
}
matrix_iterator(const matrix_iterator& other) = default;
double& operator *()
{
assert(m_current);
return *m_current;
}
matrix_iterator& operator ++()
{
assert(m_current);
++m_current;
++m_current_column;
if (m_current_column == m_matrix->size2) {
m_current += m_row_jump;
m_current_column = 0;
}
return *this;
}
matrix_iterator operator ++(int)
{
matrix_iterator result(*this);
++(*this);
return result;
}
friend bool operator == (const matrix_iterator& lhs, const matrix_iterator& rhs);
friend bool operator != (const matrix_iterator& lhs, const matrix_iterator& rhs);
private:
gsl_matrix *m_matrix;
double *m_current;
size_t m_current_column;
size_t m_row_jump;
};
bool EXPORT_GSL operator == (const matrix_iterator& lhs, const matrix_iterator& rhs);
bool EXPORT_GSL operator != (const matrix_iterator& lhs, const matrix_iterator& rhs);
class EXPORT_GSL const_matrix_iterator
{
public:
const_matrix_iterator(const gsl_matrix *m, bool begin):
m_matrix(m),
m_current(begin ? m->data : m->data + m->size1 * m->tda),
m_current_column(0),
m_row_jump(m->tda - m->size2)
{
}
const_matrix_iterator(const matrix_iterator& other):
m_matrix(other.m_matrix),
m_current(other.m_current),
m_current_column(other.m_current_column),
m_row_jump(other.m_row_jump)
{
}
const_matrix_iterator():
m_matrix(nullptr),
m_current(nullptr),
m_current_column(0)
{
}
const_matrix_iterator(const const_matrix_iterator& other) = default;
double operator *() const
{
assert(m_current);
return *m_current;
}
const_matrix_iterator& operator ++()
{
assert(m_current);
++m_current;
++m_current_column;
if (m_current_column == m_matrix->size2) {
m_current += m_row_jump;
m_current_column = 0;
}
return *this;
}
const_matrix_iterator operator ++(int)
{
const_matrix_iterator result(*this);
++(*this);
return result;
}
friend bool operator == (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
friend bool operator != (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
private:
const gsl_matrix *m_matrix;
const double *m_current;
size_t m_current_column;
size_t m_row_jump;
};
bool EXPORT_GSL operator == (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
bool EXPORT_GSL operator != (const const_matrix_iterator& lhs, const const_matrix_iterator& rhs);
class EXPORT_GSL Matrix
{
public:
typedef matrix_iterator iterator;
typedef const_matrix_iterator const_iterator;
Matrix();
Matrix(size_t rows, size_t columns, bool clean);
Matrix(size_t rows, size_t columns, double init);
Matrix(size_t rows, size_t columns, const double *init);
Matrix(const Matrix& other);
Matrix(Matrix&& other);
Matrix(gsl_matrix *m);
Matrix(const gsl_matrix *m);
Matrix transposed() const;
Matrix& operator =(const Matrix& other);
Matrix& operator =(Matrix&& other);
void reset(size_t rows, size_t columns, bool clean);
void reset(size_t rows, size_t columns, double init);
~Matrix();
size_t rows()const;
size_t cols()const;
void set(size_t i, size_t j, double x);
double operator ()(size_t i, size_t j) const;
operator gsl_matrix *();
operator const gsl_matrix *() const;
Matrix column_covariance() const;
Matrix row_covariance() const;
matrix_iterator begin();
matrix_iterator end();
const_matrix_iterator begin() const;
const_matrix_iterator end() const;
void set_row(int r, const Vector& row);
VectorView get_row(int r);
ConstVectorView get_row(int r) const;
void set_column(int c, const Vector& col);
VectorView get_column(int c);
ConstVectorView get_column(int c) const;
double dot_row(int r, const Vector& v) const;
double dot_column(int c, const Vector& col) const;
void print(std::ostream& os) const;
Matrix& operator -=(const Matrix& rhs)
{
gsl_matrix_sub(*this, rhs);
return *this;
}
Matrix& operator +=(const Matrix& rhs)
{
gsl_matrix_add(*this, rhs);
return *this;
}
Matrix& operator *=(double rhs)
{
gsl_matrix_scale(*this, rhs);
return *this;
}
bool is_valid() const;
bool is_writable() const;
private:
gsl_matrix *m_matrix;
const gsl_matrix *m_const_matrix;
bool m_owner;
};
Matrix EXPORT_GSL operator * (const Matrix& lhs, const Matrix& rhs);
Matrix EXPORT_GSL operator + (const Matrix& lhs, const Matrix& rhs);
Matrix EXPORT_GSL operator - (const Matrix& lhs, const Matrix& rhs);
inline std::ostream& operator << (std::ostream& os, const Matrix& m)
{
m.print(os);
return os;
}
struct EXPORT_GSL CSymmvEvalEvec {
CSymmvEvalEvec(Matrix m);
Matrix evec;
Vector eval;
};
void EXPORT_GSL matrix_inv_sqrt(Matrix& m);
} // end namespace
namespace std
{
template <>
class iterator_traits< gsl::const_matrix_iterator >
{
public:
typedef size_t difference_type;
typedef double value_type;
typedef const double *pointer;
typedef const double& reference;
typedef forward_iterator_tag iterator_category;
};
template <>
class iterator_traits< gsl::matrix_iterator >
{
public:
typedef size_t difference_type;
typedef double value_type;
typedef double *pointer;
typedef double& reference;
typedef forward_iterator_tag iterator_category;
};
}
#endif
operator!=
bool operator!=(const C2DImage &a, const C2DImage &b)
Definition 2d/image.hh:413
operator+=
EXPORT_2D C2DFVectorfield & operator+=(C2DFVectorfield &a, const C2DFVectorfield &b)
operator==
bool operator==(const CAttribute &a, const CAttribute &b)
Definition attributes.hh:95
gsl::ConstVectorView
Definition gsl_vector.hh:244
gsl::Matrix
Definition gsl_matrix.hh:172
gsl::Matrix::rows
size_t rows() const
gsl::Matrix::begin
const_matrix_iterator begin() const
gsl::Matrix::Matrix
Matrix(gsl_matrix *m)
gsl::Matrix::dot_row
double dot_row(int r, const Vector &v) const
gsl::Matrix::row_covariance
Matrix row_covariance() const
gsl::Matrix::transposed
Matrix transposed() const
gsl::Matrix::Matrix
Matrix()
gsl::Matrix::const_iterator
const_matrix_iterator const_iterator
Definition gsl_matrix.hh:175
gsl::Matrix::get_column
VectorView get_column(int c)
gsl::Matrix::set_row
void set_row(int r, const Vector &row)
gsl::Matrix::reset
void reset(size_t rows, size_t columns, double init)
gsl::Matrix::get_column
ConstVectorView get_column(int c) const
gsl::Matrix::get_row
VectorView get_row(int r)
gsl::Matrix::column_covariance
Matrix column_covariance() const
gsl::Matrix::Matrix
Matrix(size_t rows, size_t columns, const double *init)
gsl::Matrix::end
const_matrix_iterator end() const
gsl::Matrix::iterator
matrix_iterator iterator
Definition gsl_matrix.hh:174
gsl::Matrix::set_column
void set_column(int c, const Vector &col)
gsl::Matrix::Matrix
Matrix(Matrix &&other)
gsl::Matrix::Matrix
Matrix(const gsl_matrix *m)
gsl::Matrix::print
void print(std::ostream &os) const
gsl::Matrix::dot_column
double dot_column(int c, const Vector &col) const
gsl::Matrix::end
matrix_iterator end()
gsl::Matrix::Matrix
Matrix(const Matrix &other)
gsl::Matrix::get_row
ConstVectorView get_row(int r) const
gsl::Matrix::reset
void reset(size_t rows, size_t columns, bool clean)
gsl::Matrix::~Matrix
~Matrix()
gsl::Matrix::begin
matrix_iterator begin()
gsl::Matrix::Matrix
Matrix(size_t rows, size_t columns, bool clean)
gsl::Matrix::is_valid
bool is_valid() const
gsl::Matrix::set
void set(size_t i, size_t j, double x)
gsl::Matrix::cols
size_t cols() const
gsl::Matrix::is_writable
bool is_writable() const
gsl::Matrix::Matrix
Matrix(size_t rows, size_t columns, double init)
gsl::VectorView
Definition gsl_vector.hh:228
gsl::Vector
Definition gsl_vector.hh:39
gsl::const_matrix_iterator
Definition gsl_matrix.hh:96
gsl::const_matrix_iterator::const_matrix_iterator
const_matrix_iterator()
Definition gsl_matrix.hh:114
gsl::const_matrix_iterator::const_matrix_iterator
const_matrix_iterator(const gsl_matrix *m, bool begin)
Definition gsl_matrix.hh:98
gsl::const_matrix_iterator::const_matrix_iterator
const_matrix_iterator(const const_matrix_iterator &other)=default
gsl::const_matrix_iterator::const_matrix_iterator
const_matrix_iterator(const matrix_iterator &other)
Definition gsl_matrix.hh:106
gsl::matrix_iterator
Definition gsl_matrix.hh:33
gsl::matrix_iterator::matrix_iterator
matrix_iterator(const matrix_iterator &other)=default
gsl::matrix_iterator::const_matrix_iterator
friend class const_matrix_iterator
Definition gsl_matrix.hh:34
gsl::matrix_iterator::matrix_iterator
matrix_iterator(gsl_matrix *m, bool begin)
Definition gsl_matrix.hh:36
gsl::matrix_iterator::matrix_iterator
matrix_iterator()
Definition gsl_matrix.hh:44
std::iterator_traits< gsl::const_matrix_iterator >::pointer
const double * pointer
Definition gsl_matrix.hh:493
std::iterator_traits< gsl::const_matrix_iterator >::iterator_category
forward_iterator_tag iterator_category
Definition gsl_matrix.hh:495
std::iterator_traits< gsl::const_matrix_iterator >::value_type
double value_type
Definition gsl_matrix.hh:492
std::iterator_traits< gsl::const_matrix_iterator >::reference
const double & reference
Definition gsl_matrix.hh:494
std::iterator_traits< gsl::const_matrix_iterator >::difference_type
size_t difference_type
Definition gsl_matrix.hh:491
std::iterator_traits< gsl::matrix_iterator >::difference_type
size_t difference_type
Definition gsl_matrix.hh:502
std::iterator_traits< gsl::matrix_iterator >::reference
double & reference
Definition gsl_matrix.hh:505
std::iterator_traits< gsl::matrix_iterator >::value_type
double value_type
Definition gsl_matrix.hh:503
std::iterator_traits< gsl::matrix_iterator >::pointer
double * pointer
Definition gsl_matrix.hh:504
std::iterator_traits< gsl::matrix_iterator >::iterator_category
forward_iterator_tag iterator_category
Definition gsl_matrix.hh:506
EXPORT_GSL
#define EXPORT_GSL
Definition gsl_defines.hh:38
gsl_vector.hh
gsl
Definition gsl_iterator.hh:29
gsl::operator==
bool EXPORT_GSL operator==(const matrix_iterator &lhs, const matrix_iterator &rhs)
gsl::operator!=
bool EXPORT_GSL operator!=(const matrix_iterator &lhs, const matrix_iterator &rhs)
gsl::operator-
vector_iterator operator-(const vector_iterator &it, int dist)
Definition gsl_iterator.hh:155
gsl::operator+
vector_iterator operator+(const vector_iterator &it, int dist)
Definition gsl_iterator.hh:148
gsl::operator*
Matrix EXPORT_GSL operator*(const Matrix &lhs, const Matrix &rhs)
gsl::operator<<
std::ostream & operator<<(std::ostream &os, const Matrix &m)
Definition gsl_matrix.hh:459
gsl::matrix_inv_sqrt
void EXPORT_GSL matrix_inv_sqrt(Matrix &m)
std
Definition gsl_iterator.hh:324
operator*
F operator*(const typename TSparseSolver< F >::A_mult_x &A, const F &x)
Definition sparse_solver.hh:182
gsl::CSymmvEvalEvec::evec
Matrix evec
Definition gsl_matrix.hh:471
gsl::CSymmvEvalEvec::CSymmvEvalEvec
CSymmvEvalEvec(Matrix m)
gsl::CSymmvEvalEvec::eval
Vector eval
Definition gsl_matrix.hh:472