CoolProp  6.6.0
An open-source fluid property and humid air property database
MatrixMath.cpp
Go to the documentation of this file.
1 
2 #include "MatrixMath.h"
3 
4 #include "CoolPropTools.h"
5 #include "Exceptions.h"
6 
7 #include <string>
8 #include <sstream>
9 #include <vector>
10 #include <numeric>
11 #include <math.h>
12 
13 namespace CoolProp {}; /* namespace CoolProp */
14 
15 #ifdef ENABLE_CATCH
16 # include <math.h>
17 # include <iostream>
18 # include <catch2/catch_all.hpp>
19 
20 TEST_CASE("Internal consistency checks and example use cases for MatrixMath.h", "[MatrixMath]") {
21  bool PRINT = false;
22 
24  std::vector<double> cHeat;
25  cHeat.clear();
26  cHeat.push_back(+1.1562261074E+03);
27  cHeat.push_back(+2.0994549103E+00);
28  cHeat.push_back(+7.7175381057E-07);
29  cHeat.push_back(-3.7008444051E-20);
30 
31  std::vector<std::vector<double>> cHeat2D;
32  cHeat2D.push_back(cHeat);
33  cHeat2D.push_back(cHeat);
34 
35  SECTION("Pretty printing tests") {
36 
37  Eigen::MatrixXd matrix = Eigen::MatrixXd::Random(4, 1);
38  std::string tmpStr;
39  if (PRINT) std::cout << std::endl;
40 
41  CHECK_NOTHROW(tmpStr = CoolProp::vec_to_string(cHeat[0]));
42  if (PRINT) std::cout << tmpStr << std::endl;
43  CHECK_NOTHROW(tmpStr = CoolProp::vec_to_string(cHeat));
44  if (PRINT) std::cout << tmpStr << std::endl;
45  CHECK_NOTHROW(tmpStr = CoolProp::vec_to_string(cHeat2D));
46  if (PRINT) std::cout << tmpStr << std::endl;
47 
48  CHECK_NOTHROW(tmpStr = CoolProp::mat_to_string(CoolProp::vec_to_eigen(cHeat[0])));
49  if (PRINT) std::cout << tmpStr << std::endl;
50  CHECK_NOTHROW(tmpStr = CoolProp::mat_to_string(CoolProp::vec_to_eigen(cHeat, 1)));
51  if (PRINT) std::cout << tmpStr << std::endl;
52  CHECK_THROWS(tmpStr = CoolProp::mat_to_string(CoolProp::vec_to_eigen(cHeat, 2)));
53  if (PRINT) std::cout << tmpStr << std::endl;
54  CHECK_NOTHROW(tmpStr = CoolProp::mat_to_string(CoolProp::vec_to_eigen(cHeat2D)));
55  if (PRINT) std::cout << tmpStr << std::endl;
56  }
57 
58  SECTION("Matrix modifications") {
59  Eigen::MatrixXd matrix = CoolProp::vec_to_eigen(cHeat2D);
60 
61  if (PRINT) std::cout << CoolProp::mat_to_string(matrix) << std::endl;
62 
63  CHECK_NOTHROW(CoolProp::removeColumn(matrix, 1));
64  if (PRINT) std::cout << CoolProp::mat_to_string(matrix) << std::endl;
65 
66  CHECK_NOTHROW(CoolProp::removeRow(matrix, 1));
67  if (PRINT) std::cout << CoolProp::mat_to_string(matrix) << std::endl;
68 
69  CHECK_THROWS(CoolProp::removeColumn(matrix, 10));
70  CHECK_THROWS(CoolProp::removeRow(matrix, 10));
71  }
72 
73  SECTION("std::vector to Eigen::Matrix and back") {
74  std::vector<std::vector<double>> vec2D(cHeat2D);
75  Eigen::MatrixXd matrix = CoolProp::vec_to_eigen(vec2D);
76  for (size_t i = 0; i < static_cast<size_t>(matrix.cols()); ++i) {
77  for (size_t j = 0; j < static_cast<size_t>(matrix.rows()); ++j) {
78  CHECK(std::abs(matrix(j, i) - vec2D[j][i]) <= 1e-10);
79  }
80  }
81  vec2D = CoolProp::eigen_to_vec(matrix);
82  for (size_t i = 0; i < static_cast<size_t>(matrix.cols()); ++i) {
83  for (size_t j = 0; j < static_cast<size_t>(matrix.rows()); ++j) {
84  CHECK(std::abs(matrix(j, i) - vec2D[j][i]) <= 1e-10);
85  }
86  }
87  std::vector<double> vec1D(cHeat);
88  matrix = CoolProp::vec_to_eigen(vec1D);
89  for (size_t i = 0; i < static_cast<size_t>(matrix.cols()); ++i) {
90  for (size_t j = 0; j < static_cast<size_t>(matrix.rows()); ++j) {
91  CHECK(std::abs(matrix(j, i) - vec1D[j]) <= 1e-10);
92  }
93  }
94  vec1D = CoolProp::eigen_to_vec1D(matrix);
95  for (size_t i = 0; i < static_cast<size_t>(matrix.cols()); ++i) {
96  for (size_t j = 0; j < static_cast<size_t>(matrix.rows()); ++j) {
97  CHECK(std::abs(matrix(j, i) - vec1D[j]) <= 1e-10);
98  }
99  }
100  }
101 }
102 
103 #endif /* ENABLE_CATCH */
104 
105 //#include <unsupported/Eigen/Polynomials>
106 //#include <iostream>
109 //
110 //#include <vector>
111 //#include <string>
112 //#include <MatrixMath.h>
113 //
114 //int main()
115 //{
116 //Eigen::Vector4d roots = Eigen::Vector4d::Random();
117 //std::cout << "Roots: " << roots.transpose() << std::endl;
118 //Eigen::Matrix<double,5,1> polynomial;
119 //Eigen::roots_to_monicPolynomial( roots, polynomial );
120 //std::cout << "Polynomial: ";
121 //for( int i=0; i<4; ++i ){ std::cout << polynomial[i] << ".x^" << i << "+ "; }
122 //std::cout << polynomial[4] << ".x^4" << std::endl;
123 //Eigen::Vector4d evaluation;
124 //for( int i=0; i<4; ++i ){
125 //evaluation[i] = Eigen::poly_eval( polynomial, roots[i] ); }
126 //std::cout << "Evaluation of the polynomial at the roots: " << evaluation.transpose() << std::endl;
127 //std::cout << std::endl;
131 //Eigen::Vector4d coeffs = Eigen::Vector4d::Random()*1e2;
132 //double input = 1.9e0;
133 //std::cout << "Coeffs: " << std::endl << coeffs.transpose() << std::endl;
134 //double eval = Eigen::poly_eval( coeffs, input);
135 //std::cout << "Evaluation of the polynomial at " << input << std::endl;
136 //std::cout << eval << std::endl;
137 //
138 //double vec0 = 0.1;
139 //std::vector<double> vec1(2,0.44);
140 //std::vector< std::vector<double> > vec2;
141 //vec2.push_back(std::vector<double>(2,0.2));
142 //vec2.push_back(std::vector<double>(2,0.3));
143 //
144 //std::cout << CoolProp::vec_to_string(vec0) << std::endl;
145 //std::cout << CoolProp::vec_to_string(vec1) << std::endl;
146 //std::cout << CoolProp::vec_to_string(vec2) << std::endl;
147 //
148 //Eigen::Matrix<double,2,2> mat;
149 //mat.setConstant(2,2,0.25);
150 //std::vector< std::vector<double> > vec;
151 //
152 //CoolProp::convert(mat, vec);
153 //std::cout << CoolProp::vec_to_string(vec) << std::endl;
154 //
157 //
158 //Eigen::Matrix<double,2,2> mat2;
159 //CoolProp::convert(vec2, mat2);
160 //CoolProp::convert(mat2, vec);
161 //std::cout << CoolProp::vec_to_string(vec) << std::endl;
162 //
163 //Eigen::Matrix<double,2,1> mat1;
164 //CoolProp::convert(vec1, mat1);
165 //std::vector<double> vec3;
166 //CoolProp::convert(mat1, vec);
167 //std::cout << CoolProp::vec_to_string(vec) << std::endl;
168 //
171 //
173 //
175 //
178 //
180 //
181 //}