yat  0.21pre
Matrix.h
1 #ifndef _theplu_yat_utility_matrix_
2 #define _theplu_yat_utility_matrix_
3 
4 // $Id: Matrix.h 4207 2022-08-26 04:36:28Z 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, 2020, 2021, 2022 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 "Exception.h"
34 #include "MatrixExpression.h"
35 #include "MatrixMutable.h"
36 #include "StrideIterator.h"
37 #include "Vector.h"
38 #include "VectorConstView.h"
39 #include "VectorExpression.h"
40 #include "VectorView.h"
41 #include "yat_assert.h"
42 
43 #include <gsl/gsl_matrix.h>
44 
45 #include <cstddef> // size_t
46 #include <iosfwd>
47 
48 namespace theplu {
49 namespace yat {
50 namespace utility {
51 
52  class VectorBase;
53  class Matrix;
54 
56 
57  namespace detail {
58 
59  /*
60  Class involved in move assignment of MatrixMutable to determine
61  when both lhs and rhs are a Matrix. When the behavoiur only
62  depends on the type of one object, dynamic polymorphism with a
63  virtual function suffices; when the behaviour depends on two
64  type of two objects, we need to simulate double dispatch and
65  this class is helping the Matrix and MatrixMutable to implement
66  that.
67 
68  In brief, it's constructed inside lhs (being a Matrix), taking
69  and storing a reference to lhs. It then calls operator() taking
70  rhs. It then uses the Visitor pattern calling rhs::visit(Mover),
71  where visit is a virtual function having a special implemntation
72  for Matrix where it's using fast move assignment, whereas for
73  other rhs types move assignment can't be used (because the rhs
74  doesn't own its data) and we use copy assignment instead.
75  */
76  class Mover
77  {
78  public:
79  Mover(Matrix& lhs);
80  void operator()(MatrixMutable& rhs);
81  Matrix& lhs(void);
82  void copy_assign(const MatrixMutable& rhs);
83  private:
84  Matrix& lhs_;
85  };
86  }
88 
104  class Matrix : public MatrixMutable
105  {
106  public:
113  Matrix(void);
114 
123  Matrix(const size_t& r, const size_t& c, double init_value=0);
124 
131  Matrix(const Matrix&);
132 
138  explicit Matrix(const MatrixBase&);
139 
143  virtual ~Matrix(void);
144 
166  template<class T>
167  Matrix(const MatrixExpression<T>& other);
168 
176  Matrix(Matrix&&) noexcept;
177 
184 
202  template<class T>
204  : m_(nullptr)
205  {
206  other.move(m_);
207  }
208 
209 
240  explicit Matrix(std::istream &, char sep='\0');
241 
245  gsl_matrix* gsl_matrix_p(void);
246 
250  const gsl_matrix* gsl_matrix_p(void) const;
251 
262  void resize(size_t r, size_t c, double init_value=0);
263 
273  void transpose(void);
274 
282  const Matrix& operator=(const Matrix& other);
283  using MatrixMutable::operator=; // access to =(const MatrixBase&)
284 
294  Matrix& operator=(Matrix&& other);
295 
296  // to get access to operator*=(double)
297  using MatrixMutable::operator*=;
298 
308  const Matrix& operator*=(const Matrix&);
309 
310  protected:
316  void copy_assign(const gsl_matrix* rhs);
318 
326  void move_assign(MatrixMutable&& rhs);
327 
333  void move_assign(gsl_matrix*&& rhs);
334 
335  private:
336  gsl_matrix* m_;
337 
349  gsl_matrix* create_gsl_matrix_copy(void) const;
350 
351  // Used in move_assignment, see docs for Mover class.
352  friend class detail::Mover;
353  void visit(detail::Mover& mover);
354  };
355 
366  void inverse_svd(const MatrixBase& input, Matrix& result);
367 
383  bool nan(const MatrixBase& templat, Matrix& flag);
384 
386  template<class T>
388  {
389  // In principle, we could implement this as a move constructor
390  // and steal the data (rather than copy), but it's a bit hackish
391  // to workaround the constness, so users who are eager for speed
392  // should enable rvalues and get access to the faster move
393  // constructor below.
394 
395  try {
396  detail::copy(m_, other.gsl_matrix_p());
397  }
398  catch (GSL_error& e) {
399  detail::deallocate(m_);
400  throw e;
401  }
402  }
403 
404 }}} // of namespace utility, yat, and theplu
405 
406 #endif
void copy_assign(const gsl_matrix *rhs)
Definition: MatrixBase.h:54
gsl_matrix * gsl_matrix_p(void)
Class for errors reported from underlying GSL calls.
Definition: Exception.h:102
The Department of Theoretical Physics namespace as we define it.
virtual ~Matrix(void)
Destructor.
const gsl_matrix * gsl_matrix_p(void) const
Definition: MatrixExpression.h:138
void resize(size_t r, size_t c, double init_value=0)
Resize Matrix.
void copy_assign(const MatrixBase &other)
Behaves like operator=(const MatrixBase&)
Matrix(void)
The default constructor.
Matrix(MatrixExpression< T > &&other)
Definition: Matrix.h:203
void move(gsl_matrix *&m)
Definition: MatrixExpression.h:121
An expression that can be converted to a Matrix.
Definition: MatrixExpression.h:46
Interface to GSL matrix.
Definition: Matrix.h:104
void move_assign(MatrixMutable &&rhs)
const Matrix & operator=(const Matrix &other)
The assignment operator.
Definition: MatrixMutable.h:58
const Matrix & operator*=(const Matrix &)
Multiply and assignment operator.
void transpose(void)
Transpose the matrix.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14