Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1#ifndef MATRIX_H
2#define MATRIX_H
3
4#include <gsl/gsl_matrix.h>
5#include <gsl/gsl_linalg.h>
6#include <gsl/gsl_eigen.h>
7#include <vector>
8#include <array>
9#include <memory>
10#include <stdexcept>
11#include <algorithm>
12
13#include "../special/special_generic.h"
14
45// -----------------------------------------------------------------------------
46// RAII wrappers around GSL resources
47// -----------------------------------------------------------------------------
48
50using Vector = std::vector<double>;
51
53using gsl_matrix_sptr = std::unique_ptr<gsl_matrix, decltype(&gsl_matrix_free)>;
54
56using gsl_vector_sptr = std::unique_ptr<gsl_vector, decltype(&gsl_vector_free)>;
57
59using gsl_permutation_sptr = std::unique_ptr<gsl_permutation, decltype(&gsl_permutation_free)>;
60
62using gsl_eigen_workspace_sptr = std::unique_ptr<gsl_eigen_symmv_workspace, decltype(&gsl_eigen_symmv_free)>;
63
70inline gsl_matrix_sptr make_gsl_matrix(size_t rows, size_t cols) {
71 return gsl_matrix_sptr(gsl_matrix_alloc(rows, cols), &gsl_matrix_free);
72}
73
79inline gsl_vector_sptr make_gsl_vector(size_t size) {
80 return gsl_vector_sptr(gsl_vector_alloc(size), &gsl_vector_free);
81}
82
89 return gsl_permutation_sptr(gsl_permutation_alloc(size), &gsl_permutation_free);
90}
91
98 return { gsl_eigen_symmv_alloc(n), &gsl_eigen_symmv_free };
99}
100
101
102// -------------------------------------------------------
103
104struct EigenSystem;
105
118 double logdet;
119 int sign;
120};
121
145public:
149 RealMatrix() : rows_(0), cols_(0) {}
150
160 RealMatrix(std::vector<double> data_, std::size_t rows, std::size_t cols);
161
170 RealMatrix(std::vector<std::vector<double>> data_);
171
178 RealMatrix(std::size_t rows, std::size_t cols);
179
180 // -------------------------------------------------------------------------
181 // Element access / shape
182 // -------------------------------------------------------------------------
183
193 double& at(size_t i, size_t j);
194
204 double& unchecked_at(size_t i, size_t j);
205
215 const double& at(size_t i, size_t j) const;
216
226 const double& unchecked_at(size_t i, size_t j) const;
227
231 std::size_t rows() const;
232
236 std::size_t cols() const;
237
238 // -------------------------------------------------------------------------
239 // Row / column manipulation
240 // -------------------------------------------------------------------------
241
252 void remove_row(std::size_t row_idx);
253
264 void remove_column(std::size_t col_idx);
265
278 void remove_row_and_column(std::size_t dim_idx);
279
280 // -------------------------------------------------------------------------
281 // GSL support
282 // -------------------------------------------------------------------------
283
290 static RealMatrix from_gsl_copy(const gsl_matrix* A);
291
297 gsl_matrix_sptr to_gsl_matrix() const;
298
299 // -------------------------------------------------------------------------
300 // Linear algebra
301 // -------------------------------------------------------------------------
302
310 bool is_symmetric() const;
311
317 RealMatrix transpose() const;
318
331 EigenSystem eig() const;
332
340 SignedLogDet slogdet() const;
341
350 RealMatrix inv() const;
351
352 // -------------------------------------------------------------------------
353 // Arithmetic
354 // -------------------------------------------------------------------------
355
360 RealMatrix operator-() const;
361
370 RealMatrix& operator+=(const RealMatrix& rhs);
371
380 RealMatrix& operator-=(const RealMatrix& rhs);
381
392 RealMatrix& operator*=(const RealMatrix& rhs);
393
400 RealMatrix& operator*=(double scalar);
401
410 RealMatrix& operator/=(double scalar);
411
415 friend RealMatrix operator+(RealMatrix lhs, const RealMatrix& rhs) {
416 lhs += rhs;
417 return lhs;
418 }
419
423 friend RealMatrix operator-(RealMatrix lhs, const RealMatrix& rhs) {
424 lhs -= rhs;
425 return lhs;
426 }
427
431 friend RealMatrix operator*(RealMatrix lhs, const RealMatrix& rhs) {
432 lhs *= rhs;
433 return lhs;
434 }
435
439 friend RealMatrix operator*(RealMatrix lhs, double scalar) {
440 lhs *= scalar;
441 return lhs;
442 }
443
447 friend RealMatrix operator*(double scalar, RealMatrix rhs) {
448 rhs *= scalar;
449 return rhs;
450 }
451
455 friend RealMatrix operator/(RealMatrix lhs, double scalar) {
456 lhs /= scalar;
457 return lhs;
458 }
459
469 friend std::ostream& operator<<(std::ostream& os, RealMatrix A);
470
471private:
472 std::vector<double> data;
473 std::size_t rows_, cols_;
474};
475
493
500RealMatrix eye(std::size_t n);
501
508RealMatrix diag(const gsl_vector* X);
509
529RealMatrix nearest_psd(RealMatrix R, double thr = 1e-12);
530
543
552RealMatrix block_diag(const std::vector<RealMatrix>& blocks);
553
561template<typename... Ms>
562RealMatrix block_diag(const Ms&... ms) {
563 return block_diag(std::vector<RealMatrix>{ms...});
564}
565
566#endif // MATRIX_H
std::ostream & operator<<(std::ostream &os, BinnedObservableId const &id)
Streams a human-readable binned observable identifier.
std::shared_ptr< Parameter > & operator+=(std::shared_ptr< Parameter > &lhs, const std::shared_ptr< Parameter > &rhs)
Adds the payload of one shared parameter into another.
gsl_vector_sptr make_gsl_vector(size_t size)
Allocates a GSL vector with automatic RAII destruction.
Definition Matrix.h:79
RealMatrix nearest_psd(RealMatrix R, double thr=1e-12)
Projects a matrix to the nearest positive semi-definite correlation-like matrix.
Definition Matrix.cpp:475
gsl_permutation_sptr make_gsl_permutation(size_t size)
Allocates a GSL permutation with automatic RAII destruction.
Definition Matrix.h:88
std::unique_ptr< gsl_matrix, decltype(&gsl_matrix_free)> gsl_matrix_sptr
Owning smart pointer for a GSL matrix.
Definition Matrix.h:53
std::unique_ptr< gsl_vector, decltype(&gsl_vector_free)> gsl_vector_sptr
Owning smart pointer for a GSL vector.
Definition Matrix.h:56
RealMatrix eye(std::size_t n)
Returns the identity matrix of size n.
Definition Matrix.cpp:453
RealMatrix diag(const gsl_vector *X)
Builds a diagonal matrix from a GSL vector.
Definition Matrix.cpp:464
RealMatrix cholesky_L(RealMatrix R)
Returns the lower-triangular Cholesky factor of a PSD matrix.
Definition Matrix.cpp:508
RealMatrix block_diag(const std::vector< RealMatrix > &blocks)
Builds a block-diagonal matrix from several square blocks.
Definition Matrix.cpp:529
gsl_eigen_workspace_sptr make_eigen_workspace(size_t n)
Allocates a GSL symmetric eigensolver workspace with RAII destruction.
Definition Matrix.h:97
gsl_matrix_sptr make_gsl_matrix(size_t rows, size_t cols)
Allocates a GSL matrix with automatic RAII destruction.
Definition Matrix.h:70
std::unique_ptr< gsl_eigen_symmv_workspace, decltype(&gsl_eigen_symmv_free)> gsl_eigen_workspace_sptr
Owning smart pointer for a GSL symmetric-eigensystem workspace.
Definition Matrix.h:62
std::unique_ptr< gsl_permutation, decltype(&gsl_permutation_free)> gsl_permutation_sptr
Owning smart pointer for a GSL permutation.
Definition Matrix.h:59
friend RealMatrix operator*(double scalar, RealMatrix rhs)
Left scalar multiplication.
Definition Matrix.h:447
friend RealMatrix operator*(RealMatrix lhs, const RealMatrix &rhs)
Matrix product.
Definition Matrix.h:431
friend RealMatrix operator/(RealMatrix lhs, double scalar)
Scalar division.
Definition Matrix.h:455
RealMatrix()
Constructs an empty 0×0 matrix.
Definition Matrix.h:149
friend RealMatrix operator+(RealMatrix lhs, const RealMatrix &rhs)
Matrix addition.
Definition Matrix.h:415
friend RealMatrix operator*(RealMatrix lhs, double scalar)
Right scalar multiplication.
Definition Matrix.h:439
friend RealMatrix operator-(RealMatrix lhs, const RealMatrix &rhs)
Matrix subtraction.
Definition Matrix.h:423
std::vector< double > Vector
scalar_t operator-(scalar_t lhs, const scalar_t &rhs)
Definition scalar.cpp:36
Container for an eigendecomposition.
Definition Matrix.h:489
RealMatrix D
Definition Matrix.h:490
RealMatrix P
Diagonal matrix of eigenvalues.
Definition Matrix.h:491
Signed logarithmic determinant representation.
Definition Matrix.h:117
int sign
Natural logarithm of the absolute determinant.
Definition Matrix.h:119
double logdet
Definition Matrix.h:118