CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
TaperedCovarianceMatrix.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2014 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #include "Data.h"
35 #include "EigenWrapper.h"
36 #include "CovarianceMatrix.h"
38 
39 using namespace std;
40 
41 using namespace cbl;
42 using namespace data;
43 
44 // ======================================================================================
45 
46 
47 void cbl::data::TaperedCovarianceMatrix::m_set_tapering(const double tapering_factor)
48 {
49  m_tapering_factor = tapering_factor;
50 
51  m_tapering_function.resize(m_order, m_order);
52 
53  for (int i=0; i<static_cast<int>(m_order); i++)
54  for (int j=0; j<static_cast<int>(m_order); j++) {
55  double xx = abs(i-j);
56  if (xx<m_tapering_factor)
57  m_tapering_function(i, j) = pow(1-xx/m_tapering_factor, 4)*(4*xx/m_tapering_factor+1);
58  else
59  m_tapering_function(i, j) = 0;
60  }
61 }
62 
63 
64 // ======================================================================================
65 
66 
67 void cbl::data::TaperedCovarianceMatrix::m_set (const vector<double> covariance, const double nmeasures, const double prec)
68 {
69  (void)prec;
70 
71  m_hartlap_factor = hartlap_factor(m_order, nmeasures);
72 
73  m_matrix = cbl::wrapper::eigen::SquareMatrixToEigen(covariance).cwiseProduct(m_tapering_function);
74 
75  m_precision = m_matrix.inverse().cwiseProduct(m_tapering_function);
76 
77  m_variance = m_matrix.diagonal();
78 
79  m_std = m_variance.cwiseSqrt();
80 
81  m_correlation = m_matrix.cwiseQuotient((m_std * m_std.transpose()));
82 }
83 
84 
85 // ======================================================================================
86 
87 
88 void cbl::data::TaperedCovarianceMatrix::set(const double tapering_factor, const cbl::data::CovarianceMatrix covariance)
89 {
90  m_order = covariance.order();
91  m_set_tapering(tapering_factor);
92  m_set(flatten(covariance()));
93 }
The class CovarianceMatrix.
The class Data.
functions that wrap Eigen routines
The class TaperedCovarianceMatrix.
The class CovarianceMatrix.
size_t order() const
return the covariance matrix order
void m_set_tapering(const double tapering_factor)
privat member that sets the tapering matrix
void m_set(const std::vector< double > matrix, const double nmeasures=-1, const double prec=1.e-10)
set internal attributes
void set(const double tapering_factor, const CovarianceMatrix covariance)
Apply covariance tapering.
Eigen::MatrixXd SquareMatrixToEigen(const std::vector< double > mat)
convert a std::vector<double> to an Eigen::MatrixXd object
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::vector< T > flatten(std::vector< std::vector< T >> matrix)
flatten a matrix in a vector of size
Definition: Kernel.h:1686