yat  0.17.3pre
Matrix.h
1 #ifndef _theplu_yat_utility_matrix_
2 #define _theplu_yat_utility_matrix_
3 
4 // $Id: Matrix.h 3855 2020-01-02 01:11:34Z peter $
5 
6 /*
7  Copyright (C) 2003 Daniel Dalevi, Peter Johansson
8  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
10  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
11  Copyright (C) 2012, 2017, 2018, 2019 Peter Johansson
12 
13  This file is part of the yat library, http://dev.thep.lu.se/yat
14 
15  The yat library is free software; you can redistribute it and/or
16  modify it under the terms of the GNU General Public License as
17  published by the Free Software Foundation; either version 3 of the
18  License, or (at your option) any later version.
19 
20  The yat library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with yat. If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include <cassert>
30 
31 #include "config_public.h"
32 
33 // include these operations as they are expected when using Matrix
34 #include "BLAS_level2.h"
35 #include "BLAS_level3.h"
36 
37 #include "Exception.h"
38 #include "MatrixExpression.h"
39 #include "StrideIterator.h"
40 #include "Vector.h"
41 #include "VectorConstView.h"
42 #include "VectorExpression.h"
43 #include "VectorView.h"
44 #include "yat_assert.h"
45 
46 #include <gsl/gsl_matrix.h>
47 
48 #include <cstddef> // size_t
49 #include <iosfwd>
50 
51 namespace theplu {
52 namespace yat {
53 namespace utility {
54 
55  class VectorBase;
56 
74  class Matrix : public BasicMatrix<Matrix>
75  {
76  public:
82  typedef double value_type;
83 
89  typedef double& reference;
90 
96  typedef const double& const_reference;
97 
102 
107 
112 
117 
122 
127 
134  Matrix(void);
135 
144  Matrix(const size_t& r, const size_t& c, double init_value=0);
145 
152  Matrix(const Matrix&);
153 
172  template<class T>
173  Matrix(const MatrixExpression<T>& other);
174 
175 #ifdef YAT_HAVE_RVALUE
176 
181  Matrix(Matrix&&) noexcept;
182 
198  template<class T>
200  : blas_result_(NULL), m_(NULL)
201  {
202  other.move(m_);
203  }
204 
205 #endif
206 
237  explicit Matrix(std::istream &, char sep='\0');
238 
242  ~Matrix(void);
243 
247  void all(const double value);
248 
255  iterator begin(void);
256 
263  const_iterator begin(void) const;
264 
270  iterator begin_column(size_t i);
271 
277  const_iterator begin_column(size_t i) const;
278 
284  iterator begin_row(size_t i);
285 
291  const_iterator begin_row(size_t i) const;
292 
296  VectorView column_view(size_t i);
297 
301  const VectorConstView column_const_view(size_t) const;
302 
306  size_t columns(void) const;
307 
315  void div(const Matrix& b);
316 
320  iterator end(void);
321 
325  const_iterator end(void) const;
326 
330  iterator end_column(size_t i);
331 
335  const_iterator end_column(size_t i) const;
336 
340  iterator end_row(size_t i);
341 
345  const_iterator end_row(size_t i) const;
346 
356  bool equal(const Matrix&, const double precision=0) const;
357 
361  const gsl_matrix* gsl_matrix_p(void) const;
362 
366  gsl_matrix* gsl_matrix_p(void);
367 
375  void mul(const Matrix& b);
376 
387  void resize(size_t r, size_t c, double init_value=0);
388 
392  size_t rows(void) const;
393 
397  VectorView row_view(size_t);
398 
402  const VectorConstView row_const_view(size_t) const;
403 
409  void swap_columns(const size_t i,const size_t j);
410 
419  void swap_rowcol(const size_t i,const size_t j);
420 
426  void swap_rows(const size_t i, const size_t j);
427 
437  void transpose(void);
438 
448  double& operator()(size_t row,size_t column);
449 
460  const double& operator()(size_t row,size_t column) const;
461 
473  bool operator==(const Matrix& other) const;
474 
486  bool operator!=(const Matrix& other) const;
487 
496  const Matrix& operator=(const Matrix& other);
497 
516  template<class T>
517  Matrix& operator=(const MatrixExpression<T>& rhs);
518 
519 #ifdef YAT_HAVE_RVALUE
520 
525  Matrix& operator=(Matrix&& other);
526 
545  template<class T>
547  {
548  // This function steals data from rhs into this, and give the old
549  // m_ in return to rhs so destructor of rhs takes care of
550  // deallocation.
551 
552  rhs.move(m_);
553  return *this;
554  }
555 
556 #endif
557 
569  const Matrix& operator+=(const Matrix& b);
570 
576  template<class T>
578  {
579  *this = *this + rhs;
580  return *this;
581  }
582 
589  const Matrix& operator+=(const double d);
590 
602  const Matrix& operator-=(const Matrix&);
603 
609  template<class T>
611  {
612  *this = *this - rhs;
613  return *this;
614  }
615 
622  const Matrix& operator-=(const double d);
623 
633  const Matrix& operator*=(const Matrix&);
634 
643  const Matrix& operator*=(double d);
644 
645  private:
646 
658  gsl_matrix* create_gsl_matrix_copy(void) const;
659 
665  void delete_allocated_memory(void);
666 
667  // blas_result_ is used to temporarily store result in BLAS calls
668  // and when not NULL it should always have the same size as
669  // m_. Memory is not allocated for blas_result_ until it is
670  // needed.
671  gsl_matrix* blas_result_;
672  gsl_matrix* m_;
673  };
674 
682  void inverse_svd(const Matrix& input, Matrix& result);
683 
692  bool isnull(const Matrix&);
693 
701  double max(const Matrix&);
702 
710  double min(const Matrix&);
711 
722  void minmax_index(const Matrix&,
723  std::pair<size_t,size_t>& min,
724  std::pair<size_t,size_t>& max);
725 
741  bool nan(const Matrix& templat, Matrix& flag);
742 
756  void swap(Matrix&, Matrix&);
757 
763  std::ostream& operator<< (std::ostream& s, const Matrix&);
764 
774  double trace(const Matrix&);
775 
777  template<class T>
779  : blas_result_(NULL), m_(NULL)
780  {
781  // In principle, we could implement this as a move constructor
782  // and steal the data (rather than copy), but it's a bit hackish
783  // to workaround the constness, so users who are eager for speed
784  // should enable rvalues and get access to the faster move
785  // constructor below.
786 
787  try {
788  detail::copy(m_, other.gsl_matrix_p());
789  }
790  catch (GSL_error& e) {
791  detail::deallocate(m_);
792  throw e;
793  }
794  }
795 
796 
797  template<class T>
799  {
800  // Be careful because *this might be embedded in rhs.
801  rhs.get(blas_result_);
802  std::swap(m_, blas_result_);
803  return *this;
804  }
805 
806 
807 }}} // of namespace utility, yat, and theplu
808 
809 #endif
const VectorConstView column_const_view(size_t) const
double trace(const Matrix &)
Trace of matrix.
void mul(const Matrix &b)
const double & const_reference
Definition: Matrix.h:96
void inverse_svd(const Matrix &input, Matrix &result)
iterator end_row(size_t i)
Class for errors reported from underlying GSL calls.
Definition: Exception.h:87
The Department of Theoretical Physics namespace as we define it.
StrideIterator< const double * > const_iterator
Definition: Matrix.h:106
This is the yat interface to gsl_vector_view.
Definition: VectorView.h:79
void resize(size_t r, size_t c, double init_value=0)
Resize Matrix.
size_t rows(void) const
iterator begin_row(size_t i)
const gsl_matrix * gsl_matrix_p(void) const
Definition: MatrixExpression.h:140
StrideIterator< const double * > const_row_iterator
Definition: Matrix.h:126
bool nan(const Matrix &templat, Matrix &flag)
Create a Matrix flag indicating NaN&#39;s in another Matrix templat.
double & operator()(size_t row, size_t column)
Element access operator.
StrideIterator< double * > row_iterator
Definition: Matrix.h:121
double & reference
Definition: Matrix.h:89
void minmax_index(const Matrix &, std::pair< size_t, size_t > &min, std::pair< size_t, size_t > &max)
Locate the maximum and minimum element in the Matrix.
Matrix(void)
The default constructor.
void all(const double value)
Matrix & operator-=(const MatrixExpression< T > &rhs)
Subtraction and assign operator.
Definition: Matrix.h:610
Matrix & operator+=(const MatrixExpression< T > &rhs)
Addition and assign operator.
Definition: Matrix.h:577
Read-only view.
Definition: VectorConstView.h:56
iterator begin_column(size_t i)
double min(const Matrix &)
Get the minimum value of the Matrix.
double value_type
Definition: Matrix.h:82
bool equal(const Matrix &, const double precision=0) const
Check whether matrices are equal within a user defined precision, set by precision.
void swap(Matrix &, Matrix &)
Exchange all elements between the matrices by copying.
bool operator==(const Matrix &other) const
Comparison operator. Takes squared time.
Matrix & operator=(MatrixExpression< T > &&rhs)
Definition: Matrix.h:546
StrideIterator< const double * > const_column_iterator
Definition: Matrix.h:116
~Matrix(void)
The destructor.
const Matrix & operator-=(const Matrix &)
Subtract and assign operator.
std::ostream & operator<<(std::ostream &s, const Matrix &)
The output operator for the Matrix class.
void move(gsl_matrix *&m)
Definition: MatrixExpression.h:123
VectorView column_view(size_t i)
An expression that can be converted to a Matrix.
Definition: MatrixExpression.h:46
void get(gsl_matrix *&m) const
Definition: MatrixExpression.h:112
void div(const Matrix &b)
VectorView row_view(size_t)
const VectorConstView row_const_view(size_t) const
Interface to GSL matrix.
Definition: Matrix.h:74
bool isnull(const Matrix &)
Check if all elements of the Matrix are zero.
Definition: BasicMatrix.h:38
const Matrix & operator=(const Matrix &other)
The assignment operator.
const gsl_matrix * gsl_matrix_p(void) const
StrideIterator< double * > iterator
Definition: Matrix.h:101
StrideIterator< double * > column_iterator
Definition: Matrix.h:111
const Matrix & operator+=(const Matrix &b)
Add and assign operator.
void swap_rows(const size_t i, const size_t j)
Swap rows i and j.
bool operator!=(const Matrix &other) const
Comparison operator. Takes squared time.
const Matrix & operator*=(const Matrix &)
Multiply and assignment operator.
iterator end_column(size_t i)
void swap_columns(const size_t i, const size_t j)
Swap columns i and j.
void transpose(void)
Transpose the matrix.
Adaptor using a stride on underlying iterator.
Definition: StrideIterator.h:50
double max(const Matrix &)
Get the maximum value of the Matrix.
void swap_rowcol(const size_t i, const size_t j)
Swap row i and column j.
size_t columns(void) const

Generated on Thu Aug 27 2020 03:33:18 for yat by  doxygen 1.8.11