ARTS  2.4.0(git:4fb77825)
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;
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 
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
Block::dense_
std::shared_ptr< Matrix > dense_
Definition: covariance_matrix.h:192
Matrix
The Matrix class.
Definition: matpackI.h:1193
CovarianceMatrix::ncols
Index ncols() const
Definition: covariance_matrix.cc:222
Block::get_dense
const Matrix & get_dense() const
Definition: covariance_matrix.h:159
MatrixView
The MatrixView class.
Definition: matpackI.h:1093
Block::get_sparse
const Sparse & get_sparse() const
Definition: covariance_matrix.h:169
CovarianceMatrix::CovarianceMatrix
CovarianceMatrix(CovarianceMatrix &&)=default
Block::column_range_
Range column_range_
Definition: covariance_matrix.h:187
Block::operator=
Block & operator=(const Block &)=default
Block::~Block
~Block()=default
CovarianceMatrix::mult_inv
friend void mult_inv(MatrixView, ConstMatrixView, const CovarianceMatrix &)
Definition: covariance_matrix.cc:574
Block::get_column_range
Range get_column_range() const
Definition: covariance_matrix.h:131
CovarianceMatrix::correlations_
std::vector< Block > correlations_
Definition: covariance_matrix.h:399
oem::Matrix
invlib::Matrix< ArtsMatrix > Matrix
invlib wrapper type for ARTS matrices.
Definition: oem.h:34
Sparse
The Sparse class.
Definition: matpackII.h:60
CovarianceMatrix
Definition: covariance_matrix.h:226
operator+=
MatrixView & operator+=(MatrixView &, const Block &)
Definition: covariance_matrix.cc:120
Block::get_matrix_type
MatrixType get_matrix_type() const
Definition: covariance_matrix.h:157
Block::indices_
IndexPair indices_
Definition: covariance_matrix.h:188
CovarianceMatrix::diagonal
Vector diagonal() const
Diagonal elements as vector.
Definition: covariance_matrix.cc:516
Block::Block
Block(const Block &)=default
CovarianceMatrix::has_diagonal_blocks
bool has_diagonal_blocks(const ArrayOfArrayOfIndex &jis) const
Checks that the covariance matrix contains one diagonal block per retrieval quantity.
Definition: covariance_matrix.cc:267
add_inv
void add_inv(MatrixView A, const Block &)
CovarianceMatrix::CovarianceMatrix
CovarianceMatrix(const CovarianceMatrix &)=default
CovarianceMatrix::compute_inverse
void compute_inverse() const
Compute the inverse of this correlation matrix.
Definition: covariance_matrix.cc:391
Block::MatrixType::dense
@ dense
CovarianceMatrix::~CovarianceMatrix
~CovarianceMatrix()=default
CovarianceMatrix::mult
friend void mult(MatrixView, ConstMatrixView, const CovarianceMatrix &)
Definition: covariance_matrix.cc:544
matpackI.h
Implementation of Matrix, Vector, and such stuff.
Array< ArrayOfIndex >
CovarianceMatrix::inverses_
std::vector< Block > inverses_
Definition: covariance_matrix.h:400
CovarianceMatrix::nblocks
Index nblocks() const
! The number of blocks in the matrix excluding inverses.
Definition: covariance_matrix.cc:237
Block::Block
Block(Range row_range, Range column_range, IndexPair indices, std::shared_ptr< Sparse > sparse)
Definition: covariance_matrix.h:103
CovarianceMatrix::operator=
CovarianceMatrix & operator=(CovarianceMatrix &&)=default
my_basic_string< char >
Block::operator=
Block & operator=(Block &&)=default
CovarianceMatrix::get_block
const Block * get_block(Index i=-1, Index j=-1)
Return a pointer to the block with indices (i,j).
Definition: covariance_matrix.cc:252
Block::get_column_range
Range & get_column_range()
Definition: covariance_matrix.h:136
CovarianceMatrix::get_inverse
Matrix get_inverse() const
Definition: covariance_matrix.cc:172
Block::Block
Block(Block &&)=default
VectorView
The VectorView class.
Definition: matpackI.h:610
CovarianceMatrix::add_correlation_inverse
void add_correlation_inverse(Block c)
Add block inverse to covariance matrix.
Definition: covariance_matrix.cc:512
CovarianceMatrix::operator<<
friend std::ostream & operator<<(std::ostream &os, const CovarianceMatrix &v)
Definition: covariance_matrix.cc:617
CovarianceMatrix::ndiagblocks
Index ndiagblocks() const
! The number of diagonal blocks in the matrix excluding inverses.
Definition: covariance_matrix.cc:224
Block::get_row_range
Range get_row_range() const
Definition: covariance_matrix.h:129
jacobian.h
Routines for setting up the jacobian.
Verbosity
Definition: messages.h:49
CovarianceMatrix::add_correlation
void add_correlation(Block c)
Add block to covariance matrix.
Definition: covariance_matrix.cc:510
bifstream
Binary output file stream class.
Definition: bifstream.h:42
solve
void solve(VectorView, const CovarianceMatrix &, ConstVectorView)
Definition: covariance_matrix.cc:594
CovarianceMatrix::generate_blocks
void generate_blocks(std::vector< std::vector< const Block * >> &) const
Definition: covariance_matrix.cc:347
operator<<
std::ostream & operator<<(std::ostream &os, const ConstVectorView &v)
Definition: matpackI.cc:107
Block::set_matrix
void set_matrix(std::shared_ptr< Sparse > sparse)
Definition: covariance_matrix.h:138
Block::get_sparse
Sparse & get_sparse()
Definition: covariance_matrix.h:174
Block::matrix_type_
MatrixType matrix_type_
Definition: covariance_matrix.h:190
Block::get_dense
Matrix & get_dense()
Definition: covariance_matrix.h:164
mult_inv
void mult_inv(MatrixView, ConstMatrixView, const CovarianceMatrix &)
Definition: covariance_matrix.cc:574
IndexPair
std::pair< Index, Index > IndexPair
Definition: covariance_matrix.h:48
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:982
Range::get_extent
constexpr Index get_extent() const
Returns the extent of the range.
Definition: matpackI.h:329
CovarianceMatrix::operator+=
friend MatrixView & operator+=(MatrixView &, const CovarianceMatrix &)
Definition: covariance_matrix.cc:604
Block::set_matrix
void set_matrix(std::shared_ptr< Matrix > dense)
Definition: covariance_matrix.h:139
Range
The range class.
Definition: matpackI.h:160
Block::set_indices
void set_indices(Index f, Index s)
Definition: covariance_matrix.h:154
CovarianceMatrix::xml_write_to_stream
friend void xml_write_to_stream(ostream &, const CovarianceMatrix &, bofstream *, const String &, const Verbosity &)
Write CovarianceMatrix to XML output stream.
Definition: xml_io_compound_types.cc:208
Block::MatrixType
MatrixType
Definition: covariance_matrix.h:68
CovarianceMatrix::has_block
bool has_block(Index i, Index j)
Check if the block with indices (i, j) is contained in the covariance matrix.
Definition: covariance_matrix.cc:239
Block::operator+=
friend MatrixView & operator+=(MatrixView &, const Block &)
Definition: covariance_matrix.cc:120
Block::ncols
Index ncols() const
Definition: covariance_matrix.h:126
Block::Block
Block(Range row_range, Range column_range, IndexPair indices, std::shared_ptr< Matrix > dense)
Definition: covariance_matrix.h:80
CovarianceMatrix::xml_read_from_stream
friend void xml_read_from_stream(istream &, CovarianceMatrix &, bifstream *, const Verbosity &)
Reads CovarianceMatrix from XML input stream.
Definition: xml_io_compound_types.cc:129
Block::get_row_range
Range & get_row_range()
Definition: covariance_matrix.h:134
Block::nrows
Index nrows() const
Definition: covariance_matrix.h:124
matpackII.h
Header file for sparse matrices.
CovarianceMatrix::solve
friend void solve(VectorView, const CovarianceMatrix &, ConstVectorView)
Definition: covariance_matrix.cc:594
Block
Definition: covariance_matrix.h:62
Block::MatrixType::sparse
@ sparse
mult
void mult(MatrixView, ConstMatrixView, const Block &)
Definition: covariance_matrix.cc:38
CovarianceMatrix::has_inverse
bool has_inverse(IndexPair indices) const
Definition: covariance_matrix.cc:338
CovarianceMatrix::nrows
Index nrows() const
Definition: covariance_matrix.cc:199
Block::sparse_
std::shared_ptr< Sparse > sparse_
Definition: covariance_matrix.h:193
CovarianceMatrix::is_consistent
bool is_consistent(const ArrayOfArrayOfIndex &jis) const
Checks that the dimensions of the blocks in the covariance matrix are consistent with ranges occupied...
Definition: covariance_matrix.cc:283
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Block::get_indices
IndexPair get_indices() const
Definition: covariance_matrix.h:151
CovarianceMatrix::add_inv
friend void add_inv(MatrixView, const CovarianceMatrix &)
Definition: covariance_matrix.cc:611
CovarianceMatrix::operator=
CovarianceMatrix & operator=(const CovarianceMatrix &)=default
CovarianceMatrix::get_inverse_blocks
std::vector< Block > & get_inverse_blocks()
Blocks of the inverse covariance matrix.
Definition: covariance_matrix.h:283
CovarianceMatrix::get_blocks
std::vector< Block > & get_blocks()
Block in the covariance matrix.
Definition: covariance_matrix.h:276
Block::diagonal
Vector diagonal() const
Definition: covariance_matrix.h:142
Block::row_range_
Range row_range_
Definition: covariance_matrix.h:187
Vector
The Vector class.
Definition: matpackI.h:860
CovarianceMatrix::inverse_diagonal
Vector inverse_diagonal() const
Diagonal of the inverse of the covariance matrix as vector.
Definition: covariance_matrix.cc:529
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
Block::mult
friend void mult(MatrixView, ConstMatrixView, const Block &)
Definition: covariance_matrix.cc:38
CovarianceMatrix::CovarianceMatrix
CovarianceMatrix()=default
bofstream
Binary output file stream class.
Definition: bofstream.h:42
CovarianceMatrix::invert_correlation_block
void invert_correlation_block(std::vector< Block > &inverses, std::vector< const Block * > &blocks) const
Definition: covariance_matrix.cc:399