ARTS  2.3.1285(git:92a29ea9-dirty)
covariance_matrix.h
Go to the documentation of this file.
1 /* Copyright (C) 2017
2  Simon Pfreundschuh <simonpf@chalmers.se>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License as published by the
6  Free Software Foundation; either version 2, or (at your option) any
7  later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  USA. */
18 
33 #ifndef covariance_matrix_h
34 #define covariance_matrix_h
35 
36 #include <memory>
37 
38 #include "jacobian.h"
39 #include "matpackI.h"
40 #include "matpackII.h"
41 
42 class CovarianceMatrix;
43 
44 //------------------------------------------------------------------------------
45 // Type Aliases
46 //------------------------------------------------------------------------------
47 
48 using IndexPair = std::pair<Index, Index>;
49 
50 //------------------------------------------------------------------------------
51 // Block
52 //------------------------------------------------------------------------------
62 class Block {
63  public:
64  /*
65  * Enum class representing the type of the matrix representing the block, i.e.
66  * whether its of type Matrix (dense) or of type Sparse(sparse).
67  */
68  enum class MatrixType { dense, sparse };
69 
70  /*
71  * Create a correlation block from given row_range, column_range and shared_ptr
72  * to a dense matrix.
73  *
74  * @param row_range The range of element-rows covered by the block
75  * @param column_range The range of element-column covered by the block
76  * @param indices The pair of block-indices that identifies the block in the
77  * covariance matrix.
78  * @param dense A shared pointer to a den matrix of type Matrix
79  */
80  Block(Range row_range,
81  Range column_range,
82  IndexPair indices,
83  std::shared_ptr<Matrix> dense)
84  : row_range_(row_range),
85  column_range_(column_range),
86  indices_(indices),
87  matrix_type_(MatrixType::dense),
88  dense_(dense),
89  sparse_(nullptr) {
90  // Nothing to do here.
91  }
92 
93  /*
94  * Create a correlation block from given row_range, column_range and shared_ptr
95  * to a sparse matrix.
96  *
97  * @param row_range The range of element-rows covered by the block
98  * @param column_range The range of element-column covered by the block
99  * @param indices The pair of block-indices that identifies the block in the
100  * covariance matrix.
101  * @param dense A shared pointer to a den matrix of type Sparse
102  */
103  Block(Range row_range,
104  Range column_range,
105  IndexPair indices,
106  std::shared_ptr<Sparse> sparse)
107  : row_range_(row_range),
108  column_range_(column_range),
109  indices_(indices),
110  matrix_type_(MatrixType::sparse),
111  dense_(nullptr),
112  sparse_(sparse) {
113  // Nothing to do here.
114  }
115 
116  Block(const Block &) = default;
117  Block(Block &&) = default;
118  Block &operator=(const Block &) = default;
119  Block &operator=(Block &&) = default;
120 
121  ~Block() = default;
122 
124  Index nrows() const { return row_range_.get_extent(); }
126  Index ncols() const { return column_range_.get_extent(); }
127 
129  Range get_row_range() const { return row_range_; }
132 
137 
138  void set_matrix(std::shared_ptr<Sparse> sparse) { sparse_ = sparse; }
139  void set_matrix(std::shared_ptr<Matrix> dense) { dense_ = dense; }
140 
142  Vector diagonal() const {
143  if (dense_) {
144  return dense_->diagonal();
145  } else {
146  return sparse_->diagonal();
147  }
148  }
149 
151  IndexPair get_indices() const { return indices_; }
152 
154  void set_indices(Index f, Index s) { indices_ = {f, s}; }
155 
158 
159  const Matrix &get_dense() const {
160  assert(dense_);
161  return *dense_;
162  }
163 
165  assert(dense_);
166  return *dense_;
167  }
168 
169  const Sparse &get_sparse() const {
170  assert(sparse_);
171  return *sparse_;
172  }
173 
175  assert(sparse_);
176  return *sparse_;
177  }
178 
179  // Friend declarations.
180  friend void mult(MatrixView, ConstMatrixView, const Block &);
181  friend void mult(MatrixView, const Block &, ConstMatrixView);
182  friend void mult(VectorView, const Block &, ConstVectorView);
183 
184  friend MatrixView &operator+=(MatrixView &, const Block &);
185 
186  private:
189 
191 
192  std::shared_ptr<Matrix> dense_;
193  std::shared_ptr<Sparse> sparse_;
194 };
195 
196 void mult(MatrixView, ConstMatrixView, const Block &);
197 void mult(MatrixView, const Block &, ConstMatrixView);
198 void mult(VectorView, const Block &, ConstVectorView);
199 
201 void add_inv(MatrixView A, const Block &);
202 
203 //------------------------------------------------------------------------------
204 // Covariance Matrices
205 //------------------------------------------------------------------------------
227  public:
228  CovarianceMatrix() = default;
229  CovarianceMatrix(const CovarianceMatrix &) = default;
230  CovarianceMatrix(CovarianceMatrix &&) = default;
231  CovarianceMatrix &operator=(const CovarianceMatrix &) = default;
233 
234  ~CovarianceMatrix() = default;
235 
236  explicit operator Matrix() const;
237  Matrix get_inverse() const;
238 
239  Index nrows() const;
240  Index ncols() const;
241 
243  Index ndiagblocks() const;
244 
246  Index nblocks() const;
247 
255  bool has_block(Index i, Index j);
256 
269  const Block *get_block(Index i = -1, Index j = -1);
270 
276  std::vector<Block> &get_blocks() { return correlations_; };
277 
283  std::vector<Block> &get_inverse_blocks() { return inverses_; };
284 
296  bool has_diagonal_blocks(const ArrayOfArrayOfIndex &jis) const;
297 
310  bool is_consistent(const ArrayOfArrayOfIndex &jis) const;
311 
322  bool is_consistent(const Block &block) const;
323 
329  void compute_inverse() const;
330 
339  void add_correlation(Block c);
340 
349  void add_correlation_inverse(Block c);
350 
358  Vector diagonal() const;
359 
368  Vector inverse_diagonal() const;
369 
370  // Friend declarations.
371  friend void mult(MatrixView, ConstMatrixView, const CovarianceMatrix &);
372  friend void mult(MatrixView, const CovarianceMatrix &, ConstMatrixView);
373  friend void mult(VectorView, const CovarianceMatrix &, ConstVectorView);
374 
375  friend void mult_inv(MatrixView, ConstMatrixView, const CovarianceMatrix &);
376  friend void mult_inv(MatrixView, const CovarianceMatrix &, ConstMatrixView);
377  friend void solve(VectorView, const CovarianceMatrix &, ConstVectorView);
378 
379  friend MatrixView &operator+=(MatrixView &, const CovarianceMatrix &);
380  friend void add_inv(MatrixView, const CovarianceMatrix &);
381 
382  friend void xml_read_from_stream(istream &,
384  bifstream *,
385  const Verbosity &);
386  friend void xml_write_to_stream(ostream &,
387  const CovarianceMatrix &,
388  bofstream *,
389  const String &,
390  const Verbosity &);
391  friend std::ostream &operator<<(std::ostream &os, const CovarianceMatrix &v);
392 
393  private:
394  void generate_blocks(std::vector<std::vector<const Block *>> &) const;
395  void invert_correlation_block(std::vector<Block> &inverses,
396  std::vector<const Block *> &blocks) const;
397  bool has_inverse(IndexPair indices) const;
398 
399  std::vector<Block> correlations_;
400  mutable std::vector<Block> inverses_;
401 };
402 
406 
410 
412 void add_inv(MatrixView, const CovarianceMatrix &);
413 
414 std::ostream &operator<<(std::ostream &os, const ConstVectorView &v);
415 
416 #endif // covariance_matrix_h
std::pair< Index, Index > IndexPair
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
std::shared_ptr< Sparse > sparse_
Range row_range_
void solve(VectorView, const CovarianceMatrix &, ConstVectorView)
The VectorView class.
Definition: matpackI.h:610
std::ostream & operator<<(std::ostream &os, const ConstVectorView &v)
Definition: matpackI.cc:107
Range get_column_range() const
friend void mult(MatrixView, ConstMatrixView, const Block &)
void xml_write_to_stream(ostream &os_xml, const ArrayOfAgenda &aa, bofstream *pbofs, const String &name, const Verbosity &)
Writes ArrayOfAgenda to XML output stream.
void mult_inv(MatrixView, ConstMatrixView, const CovarianceMatrix &)
invlib::Matrix< ArtsCovarianceMatrixWrapper > CovarianceMatrix
invlib wrapper type for ARTS the ARTS covariance class.
Definition: oem.h:38
std::shared_ptr< Matrix > dense_
const Matrix & get_dense() const
Routines for setting up the jacobian.
The Vector class.
Definition: matpackI.h:860
The MatrixView class.
Definition: matpackI.h:1093
const Sparse & get_sparse() const
Block & operator=(const Block &)=default
The Sparse class.
Definition: matpackII.h:60
The range class.
Definition: matpackI.h:160
~Block()=default
Range column_range_
IndexPair indices_
MatrixType get_matrix_type() const
Range & get_column_range()
std::vector< Block > & get_inverse_blocks()
Blocks of the inverse covariance matrix.
std::vector< Block > & get_blocks()
Block in the covariance matrix.
Header file for sparse matrices.
Sparse & get_sparse()
constexpr Index get_extent() const
Returns the extent of the range.
Definition: matpackI.h:329
Binary output file stream class.
Definition: bifstream.h:42
Matrix & get_dense()
Block(Range row_range, Range column_range, IndexPair indices, std::shared_ptr< Sparse > sparse)
friend MatrixView & operator+=(MatrixView &, const Block &)
std::vector< Block > correlations_
Index ncols() const
The Matrix class.
Definition: matpackI.h:1193
Range get_row_range() const
void set_matrix(std::shared_ptr< Sparse > sparse)
Implementation of Matrix, Vector, and such stuff.
Block(Range row_range, Range column_range, IndexPair indices, std::shared_ptr< Matrix > dense)
void xml_read_from_stream(istream &is_xml, ArrayOfAgenda &aa, bifstream *pbifs, const Verbosity &)
Reads ArrayOfAgenda from XML input stream.
MatrixType matrix_type_
void add_inv(MatrixView A, const Block &)
A constant view of a Vector.
Definition: matpackI.h:476
void set_matrix(std::shared_ptr< Matrix > dense)
Vector diagonal() const
Binary output file stream class.
Definition: bofstream.h:42
A constant view of a Matrix.
Definition: matpackI.h:982
void set_indices(Index f, Index s)
Range & get_row_range()
Index nrows() const
std::vector< Block > inverses_
IndexPair get_indices() const
invlib::Matrix< ArtsMatrix > Matrix
invlib wrapper type for ARTS matrices.
Definition: oem.h:34