/usr/include/mia-2.4/mia/core/gsl_matrix.hh is in libmia-2.4-dev 2.4.3-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2016 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);
/**
This is a wrapper class around the GSL matrix type. It provides
a compatibility to avoid handling de-alloction manually.
*/
class EXPORT_GSL Matrix {
public:
typedef matrix_iterator iterator;
typedef const_matrix_iterator const_iterator;
Matrix();
/**
Create a matrix of size rows \f$\times\f$ columns,
\param rows
\param columns
\param clean allocate zeroing out all elements
*/
Matrix(size_t rows, size_t columns, bool clean);
/**
Create a matrix of size rows \f$\times\f$ columns,
\param rows
\param columns
\param init set the matrix elements to this value
*/
Matrix(size_t rows, size_t columns, double init);
/**
Create a matrix of size rows \f$\times\f$ columns and initialize it
with the given data
\param rows
\param columns
\param init the input data in row major format
*/
Matrix(size_t rows, size_t columns, const double *init);
/**
Copy constructor that executes a deep copy
*/
Matrix(const Matrix& other);
/**
Wrap an existing GSL matrix
\param m the GSL matrix
*/
Matrix(gsl_matrix* m);
/**
Wrap an existing GSL constant matrix
\param m the GSL matrix
*/
Matrix(const gsl_matrix* m);
/**
Acquire the transposed matrix.
\returns transposed matrix as newly created object
*/
Matrix transposed() const;
/**
Copy operator that executes a deep copy
*/
Matrix& operator =(const Matrix& other);
/**
Reset the matrix with the new dimensions.
\param rows
\param columns
\param clean - set all values to zero
*/
void reset(size_t rows, size_t columns, bool clean);
/**
Reset the matrix with the new dimensions.
\param rows
\param columns
\param init - set all values to \a init
*/
void reset(size_t rows, size_t columns, double init);
~Matrix();
/**
\returns the number of rows in the matrix
*/
size_t rows()const;
/**
\returns the number of columns in the matrix
*/
size_t cols()const;
/**
Set a value of the matrix
\param i row
\param j column
\param x value
*/
void set(size_t i, size_t j, double x);
/**
Get a value of the matrix
\param i row
\param j column
\returns the value
*/
double operator ()(size_t i, size_t j) const;
/**
operator to get the underlying gsl_matrix pointer
*/
operator gsl_matrix *();
/**
operator to get the underlying const gsl_matrix pointer
*/
operator const gsl_matrix *() const;
/**
Evaluate the covariance matrix between the columns of this matrix
\returns the covariance matrix
*/
Matrix column_covariance() const;
/**
Evaluate the covariance matrix between the rows of this matrix
\returns the covariance matrix
*/
Matrix row_covariance() const;
/**
Iterator over the matrix elements of the matrix, column indices are the
fastest changing indices.
\returns the begin of the range
*/
matrix_iterator begin();
/**
Iterator over the matrix elements of the matrix, column indices are the
fastest changing indices.
\returns the end of the range
*/
matrix_iterator end();
/**
Read only iterator over the matrix elements of the matrix, column indices are the
fastest changing indices.
\returns the begin of the range
*/
const_matrix_iterator begin() const;
/**
Read only iterator over the matrix elements of the matrix, column indices are the
fastest changing indices.
\returns the end of the range
*/
const_matrix_iterator end() const;
/**
Set a matrix row
\param r index of row to be set, must be in [0, this->rows()-1]
\param row vector containing the new values. It's size must be equal
to the number of columns of the matrix
*/
void set_row(int r, const Vector& row);
/**
Get a read-write view of a matrix row
\param r index of the matrix row
\returns view
*/
VectorView get_row(int r);
/**
Get a read-only view of a matrix row
\param r index of the matrix row
\returns constant view
*/
ConstVectorView get_row(int r) const;
/**
Set a matrix column
\param c index of column to be set, must be in [0, this->cols()-1]
\param col vector containing the new values. It's size must be equal
to the number of rows of the matrix
*/
void set_column(int c, const Vector& col);
/**
Get a read-write view of a matrix column
\param c index of the matrix column
\returns view
*/
VectorView get_column(int c);
/**
Get a read-only view of a matrix column
\param c index of the matrix column
\returns view
*/
ConstVectorView get_column(int c) const;
/**
Evaluate the dot product between a row of the matrix and a given vector
\param r index of row, must be in [0, this->rows()-1]
\param v vector to evaluate the dot product with. The vector's size must be equal to the
number of matrix columns.
\returns the dot product of the row vector and the input vector
*/
double dot_row(int r, const Vector& v) const;
/**
Evaluate the dot product between a column of the matrix and a given vector
\param c index of column, must be in [0, this->cols()-1]
\param col vector to evaluate the dot product with. The vector's size must be equal to the
number of matrix rows.
\returns the dot product of the row vector and the input vector
*/
double dot_column(int c, const Vector& col) const;
/**
Print matrix to a ostream
\param os output stream to write to
*/
void print(std::ostream& os) const;
/**
Element wise matrix subtraction
\param rhs matrix to subtract
\returns reference of this matrix
*/
Matrix& operator -=(const Matrix& rhs) {
gsl_matrix_sub(*this, rhs);
return *this;
}
/**
Element wise matrix addition
\param rhs matrix to add
\returns reference of this matrix
*/
Matrix& operator +=(const Matrix& rhs) {
gsl_matrix_add(*this, rhs);
return *this;
}
/**
Scale elements with a factor
\param rhs scaling factor
\returns reference of this matrix
*/
Matrix& operator *=(double rhs) {
gsl_matrix_scale(*this, rhs);
return *this;
}
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;
}
/**
Evaluate the eigenvalues and eigenvectors of the input matrix
*/
struct EXPORT_GSL CSymmvEvalEvec {
CSymmvEvalEvec(Matrix m);
Matrix evec;
Vector eval;
};
/**
Evaluate in place: pow(m, -0.5);
*/
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
|