const madness

This commit is contained in:
jowr
2014-06-05 18:08:21 +02:00
parent 7fe0755044
commit bdf92d95a9
4 changed files with 155 additions and 26 deletions

View File

@@ -55,7 +55,7 @@ file(GLOB_RECURSE APP_HEADERS "include/*.h" "src/*.h") # "externals/*.hpp")
list(REMOVE_ITEM APP_SOURCES "${CMAKE_SOURCE_DIR}/src/Tests/Tests.cpp")
set (APP_INCLUDE_DIRS "")
set (APP_INCLUDE_DIRS "externals/eigen")
foreach (_headerFile ${APP_HEADERS})
get_filename_component(_dir ${_headerFile} PATH)
list (APPEND APP_INCLUDE_DIRS ${_dir})

View File

@@ -10,6 +10,25 @@
#include <sstream>
#include "float.h"
/// A wrapper around std::vector
/** This wrapper makes the standard vector multi-dimensional.
* A useful thing even though we might not need it that
* much. However, it makes the code look better and the
* polynomial class really is a mess...
* Source: http://stackoverflow.com/questions/13105514/n-dimensional-vector
*/
template<size_t dimcount, typename T> struct VectorNd {
typedef std::vector< typename VectorNd<dimcount-1, T>::type > type;
};
//template<typename T> struct VectorNd<1,T> {
// typedef std::vector< T > type;
//};
template<typename T> struct VectorNd<0,T> {
typedef T type;
};
namespace CoolProp{
///// Publish the linear algebra solver
@@ -398,42 +417,106 @@ template<class T> std::vector< std::vector<T> > invert(std::vector<std::vecto
return linsolve(in,identity);
};
template<class T> std::string vec_to_string( T const& a){
/// Template classes
const char* stdFmt = "%7.3f";
/// Template class for turning numbers (0D-matrices) into strings
template<class T> std::string vec_to_string( T const& a, const char *fmt) {
//std::vector<T> vec;
//vec.push_back(a);
//return vec_to_string(vec, fmt);
std::stringstream out;
out << format("[ %7.3f ]",a);
out << "[ " << format(fmt,a);
return out.str();
};
template<class T> std::string vec_to_string( T const& a){
return vec_to_string(a, stdFmt);
};
///Template classes for turning vectors (1D-matrices) into strings
template<class T> std::string vec_to_string( std::vector<T> const& a) {
return vec_to_string(a,"%7.3g");
return vec_to_string(a,stdFmt);
};
template<class T> std::string vec_to_string( std::vector<T> const& a, const char *fmt) {
if (a.size()<1) {
return std::string("");
} else {
std::stringstream out;
out << format("[ ");
out << format(fmt,a[0]);
for (size_t j = 1; j < a.size(); j++) {
out << ", ";
out << format(fmt,a[j]);
}
out << " ]";
return out.str();
}
if (a.size()<1) return std::string("");
std::stringstream out;
out << "[ " << format(fmt,a[0]);
for (size_t j = 1; j < a.size(); j++) {
out << ", " << format(fmt, a[j]);
}
out << " ]";
return out.str();
};
///Template classes for turning 2D-matrices into strings
template<class T> std::string vec_to_string(std::vector<std::vector<T> > const& A) {
return vec_to_string(A, "%7.3g");
}
return vec_to_string(A, stdFmt);
};
template<class T> std::string vec_to_string(std::vector<std::vector<T> > const& A, const char *fmt) {
if (A.size()<1) return std::string("");
std::stringstream out;
for (size_t j = 0; j < A.size(); j++) {
out << vec_to_string(A[j], fmt);
out << "[ " << format(fmt,A[0]);
for (size_t j = 1; j < A.size(); j++) {
out << ", " << std::endl << " " << vec_to_string(A[j], fmt);
}
out << " ]";
return out.str();
}
};
///// Template class for turning numbers (0D-matrices) into strings
////template<class T> std::string vec_to_string(const T &a){
//// return vec_to_string(a, stdFmt);
//// std::stringstream out;
//// out << format("[ %7.3f ]",a);
//// return out.str();
////};
////template<class T> std::string vec_to_string(const VectorNd<0, T> &a){
//// return vec_to_string(a, stdFmt);
////};
////template<class T> std::string vec_to_string(const VectorNd<0, T> &a, const char *fmt) {
//// VectorNd<1, T> vec;
//// vec.push_back(a);
//// return vec_to_string(vec, fmt);
////};
//
/////Template classes for turning vectors (1D-matrices) into strings
//template<class T> std::string vec_to_string(const VectorNd<1, T> &a) {
// return vec_to_string(a, stdFmt);
//};
//template<class T> std::string vec_to_string(const VectorNd<1, T> &a, const char *fmt) {
// if (a.size()<1) {
// return std::string("");
// } else {
// std::stringstream out;
// out << "[ ";
// out << format(fmt,a[0]);
// for (size_t j = 1; j < a.size(); j++) {
// out << ", ";
// out << format(fmt,a[j]);
// }
// out << " ]";
// return out.str();
// }
//};
//
/////Template classes for turning 2D-matrices into strings
//template<class T> std::string vec_to_string(const VectorNd<2, T> &A) {
// return vec_to_string(A, stdFmt);
//}
//template<class T> std::string vec_to_string(const VectorNd<2, T> &A, const char *fmt) {
// if (A.size()<1) return std::string("");
// std::stringstream out;
// out << "[ " << format(fmt,A[0]);
// for (size_t j = 1; j < A.size(); j++) {
// out << ", " << std::endl << " " << vec_to_string(A[j], fmt);
// }
// out << " ]";
// return out.str();
//}
}; /* namespace CoolProp */
#endif

View File

@@ -11,14 +11,18 @@
//#include <sstream>
//#include "float.h"
#include <unsupported/Eigen/Polynomials>
namespace CoolProp{
/// The base class for Polynomials
class BasePolynomial{
protected:
bool POLYMATH_DEBUG;
public:
// Constructor
BasePolynomial();
@@ -29,6 +33,10 @@ public:
/// Basic checks for coefficient vectors.
/** Starts with only the first coefficient dimension
* and checks the vector length against parameter n. */
bool checkCoefficients(const Eigen::VectorXd &coefficients, const unsigned int n);
bool checkCoefficients(const Eigen::MatrixXd &coefficients, const unsigned int rows, const unsigned int columns);
bool checkCoefficients(const vectorNd<1, double>::type &coefficients, const unsigned int n);
bool checkCoefficients(const vectorNd<2, double>::type &coefficients, const unsigned int rows, const unsigned int columns);
bool checkCoefficients(const std::vector<double> &coefficients, const unsigned int n);
bool checkCoefficients(const std::vector< std::vector<double> > &coefficients, const unsigned int rows, const unsigned int columns);

38
src/Tests/eigenTest.cpp Normal file
View File

@@ -0,0 +1,38 @@
#include <unsupported/Eigen/Polynomials>
#include <iostream>
//using namespace Eigen;
//using namespace std;
#include <vector>
#include <MatrixMath.h>
int main()
{
Eigen::Vector4d roots = Eigen::Vector4d::Random();
std::cout << "Roots: " << roots.transpose() << std::endl;
Eigen::Matrix<double,5,1> polynomial;
Eigen::roots_to_monicPolynomial( roots, polynomial );
std::cout << "Polynomial: ";
for( int i=0; i<4; ++i ){ std::cout << polynomial[i] << ".x^" << i << "+ "; }
std::cout << polynomial[4] << ".x^4" << std::endl;
Eigen::Vector4d evaluation;
for( int i=0; i<4; ++i ){
evaluation[i] = Eigen::poly_eval( polynomial, roots[i] ); }
std::cout << "Evaluation of the polynomial at the roots: " << evaluation.transpose() << std::endl;
std::cout << std::endl;
//
//Eigen::MatrixXd coeffs = Eigen::MatrixXd::Random(5,1);
//Eigen::MatrixXd input = Eigen::MatrixXd::Random(2,1)*1e0;
Eigen::Vector4d coeffs = Eigen::Vector4d::Random()*1e2;
double input = 1.9e0;
std::cout << "Coeffs: " << std::endl << coeffs.transpose() << std::endl;
double eval = Eigen::poly_eval( coeffs, input);
std::cout << "Evaluation of the polynomial at " << input << std::endl;
std::cout << eval << std::endl;
//std::vector<double> vec(2,0.2);
//std::cout << CoolProp::vec_to_string(vec) << std::endl;
std::cout << CoolProp::vec_to_string(0.3) << std::endl;
}