CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
EigenWrapper.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
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 
36 #include "EigenWrapper.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 using namespace wrapper;
42 using namespace eigen;
43 
44 
45 // ============================================================================
46 
47 
48 std::vector<double> cbl::wrapper::eigen::EigenToVector (const Eigen::MatrixXd vec)
49 {
50  vector<double> vector(vec.data(), vec.data()+vec.rows()*vec.cols());
51  return vector;
52 }
53 
54 
55 // ============================================================================
56 
57 
58 vector<vector<double>> cbl::wrapper::eigen::EigenToMatrix (const Eigen::MatrixXd mat)
59 {
60  vector<vector<double>> matrix;
61 
62  for (int i=0; i<mat.rows(); i++)
63  matrix.push_back(EigenToVector(mat.row(i)));
64 
65  return matrix;
66 }
67 
68 
69 // ============================================================================
70 
71 
72 Eigen::MatrixXd cbl::wrapper::eigen::VectorToEigen (const std::vector<double> vec)
73 {
74  return Eigen::VectorXd::Map(vec.data(), vec.size());
75 }
76 
77 
78 // ============================================================================
79 
80 
81 Eigen::MatrixXd cbl::wrapper::eigen::MatrixToEigen (const std::vector<std::vector<double>> mat)
82 {
83  const int rows = static_cast<int>(mat.size());
84  const int cols = static_cast<int>(mat[0].size());
85 
86  Eigen::MatrixXd matrix(rows, cols);
87 
88  for (int i=0; i<matrix.rows(); i++)
89  matrix.row(i) = Eigen::VectorXd::Map(mat[i].data(), mat.size());
90 
91  return matrix;
92 }
93 
94 
95 // ============================================================================
96 
97 
98 Eigen::MatrixXd cbl::wrapper::eigen::SquareMatrixToEigen (const std::vector<double> mat)
99 {
100  int order = sqrt(mat.size());
101  return MatrixToEigen(reshape(mat, order, order));
102 }
functions that wrap Eigen routines
Eigen::MatrixXd MatrixToEigen(const std::vector< std::vector< double >> mat)
convert a std::vector<std::vector<double>> to an Eigen::MatrixXd object
Eigen::MatrixXd VectorToEigen(const std::vector< double > vec)
convert a std::vector<double> to an Eigen::MatrixXd object
std::vector< double > EigenToVector(const Eigen::MatrixXd vec)
convert an Eigen::MatrixXd to a std::vector<double>
Eigen::MatrixXd SquareMatrixToEigen(const std::vector< double > mat)
convert a std::vector<double> to an Eigen::MatrixXd object
std::vector< std::vector< double > > EigenToMatrix(const Eigen::MatrixXd mat)
convert an Eigen::MatrixXd to a std::vector<std::vector<double>>
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::vector< std::vector< T > > reshape(std::vector< T > vec, const int size1, const int size2)
reshape a vector into a matrix of given number of rows and columns
Definition: Kernel.h:1707