From ca34ddcea8e141c04fcc240865a81e86efbfdfab Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 29 Jan 2012 11:26:25 +0100 Subject: [PATCH 1/3] templatize everything to work with non-standard types like mpreal (of the mpfrc++ library) --- test.cpp | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/test.cpp b/test.cpp index 6e6a019..38a2c8f 100644 --- a/test.cpp +++ b/test.cpp @@ -28,24 +28,28 @@ Authors: #include #include +#include +using namespace mpfr; + #include "cholesky.h" +template void setformat( std::ostream& out ) { out << std::right; out << std::setfill(' '); - out << std::setw( 5 + std::numeric_limits::digits10); - out << std::setprecision(std::numeric_limits::digits10); + out << std::setw( 5 + std::numeric_limits::digits10); + out << std::setprecision(std::numeric_limits::digits10); out << std::setiosflags(std::ios_base::showpoint); } -template +template std::string format(const MT& mat ) { std::ostringstream out; - setformat(out); + setformat(out); for( unsigned int i=0; i -bool equal( const MT& M1, const MT& M2, double prec /* = 1/std::numeric_limits::digits10 ???*/ ) +template +bool equal( const MT& M1, const MT& M2, T prec ) { if( M1.size1() != M2.size1() || M1.size2() != M2.size2() ) { return false; @@ -88,12 +92,12 @@ bool equal( const MT& M1, const MT& M2, double prec /* = 1/std::numeric_limits +template MT error( const MT& M1, const MT& M2 ) { assert( M1.size1() == M2.size1() && M1.size1() == M2.size2() ); - MT Err = ublas::zero_matrix(M1.size1(),M1.size2()); + MT Err = ublas::zero_matrix(M1.size1(),M1.size2()); for( unsigned int i=0; i -double trigsum( const MT& M ) +template +T trigsum( const MT& M ) { - double sum = 0; + T sum = 0; for( unsigned int i=0; i -double sum( const T& c ) +template +T sum( const V& c ) { - return std::accumulate(c.begin(), c.end(), 0); + return std::accumulate(c.begin(), c.end(), static_cast(0) ); } @@ -145,10 +149,10 @@ void test( unsigned int M, unsigned int N, unsigned int F, unsigned int R, unsig // init data structures on the same keys than given algorithms std::map fails; // triangular errors sum - std::map > errors; + std::map > errors; for( typename AlgoMap::iterator ialgo = algos.begin(); ialgo != algos.end(); ++ialgo ) { fails[ialgo->first] = 0; - errors[ialgo->first] = std::vector(); + errors[ialgo->first] = std::vector(); } for( unsigned int n=0; n(V) << std::endl; } #endif for( typename AlgoMap::iterator ialgo = algos.begin(); ialgo != algos.end(); ++ialgo ) { @@ -179,7 +183,7 @@ void test( unsigned int M, unsigned int N, unsigned int F, unsigned int R, unsig try { L = (*ialgo->second)(V); CovarMat Vn = ublas::prod( L, ublas::trans(L) ); - errors[ialgo->first].push_back( trigsum(error(V,Vn)) ); + errors[ialgo->first].push_back( trigsum(error(V,Vn)) ); } catch( cholesky::NotDefinitePositive & error ) { fails[ialgo->first]++; @@ -201,7 +205,7 @@ void test( unsigned int M, unsigned int N, unsigned int F, unsigned int R, unsig std::cout << "NAN"; } else { assert( errors[a].size() == R - fails[a] ); - std::cout << sum(errors[a])/R; + std::cout << sum(errors[a])/R; } std::cout << std::endl; } // for a in algos @@ -237,6 +241,7 @@ int main(int argc, char** argv) std::clog << "Legend:" << std::endl; std::clog << "\tAlgo: (failures/runs)\tAverage error" << std::endl; + /* std::cout << std::endl << "FLOAT" << std::endl; test(M,N,F,R,seed); @@ -245,4 +250,10 @@ int main(int argc, char** argv) std::cout << std::endl << "LONG DOUBLE" << std::endl; test(M,N,F,R,seed); + */ + + std::cout << std::endl << "MPREAL 128" << std::endl; + mpreal::set_default_prec(128); + test(M,N,F,R,seed); } + From 384c2c31b3d710341c91d6e256bd14e1cb7e1497 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 29 Jan 2012 18:18:28 +0100 Subject: [PATCH 2/3] build with cmake --- CMakeLists.txt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..40bdbb3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ + +PROJECT(Cholesky) + +cmake_minimum_required(VERSION 2.8) +FIND_PACKAGE(Boost 1.33.0) + +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) +LINK_DIRECTORIES(${Boost_LIBRARY_DIRS}) + +INCLUDE_DIRECTORIES("~/apps/mpfrc++/") +LINK_DIRECTORIES("~/apps/mpfrc++/") +LINK_DIRECTORIES("/usr/lib/") + +ADD_DEFINITIONS( -Wall -W -Wextra ) + +ADD_EXECUTABLE(test test.cpp) +TARGET_LINK_LIBRARIES(test ${Boost_LIBRARIES} gmp mpfr mpreal) + From 26d2857733c7e89354f5fe202c8865de69afc637 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 29 Jan 2012 18:31:47 +0100 Subject: [PATCH 3/3] pick templatization from mpreal branch, but do not use mpfrc++ in master --- CMakeLists.txt | 6 +----- test.cpp | 9 --------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40bdbb3..6ff97c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,12 +9,8 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) LINK_DIRECTORIES(${Boost_LIBRARY_DIRS}) -INCLUDE_DIRECTORIES("~/apps/mpfrc++/") -LINK_DIRECTORIES("~/apps/mpfrc++/") -LINK_DIRECTORIES("/usr/lib/") - ADD_DEFINITIONS( -Wall -W -Wextra ) ADD_EXECUTABLE(test test.cpp) -TARGET_LINK_LIBRARIES(test ${Boost_LIBRARIES} gmp mpfr mpreal) +TARGET_LINK_LIBRARIES(test ${Boost_LIBRARIES}) diff --git a/test.cpp b/test.cpp index 38a2c8f..044d35c 100644 --- a/test.cpp +++ b/test.cpp @@ -28,9 +28,6 @@ Authors: #include #include -#include -using namespace mpfr; - #include "cholesky.h" @@ -241,7 +238,6 @@ int main(int argc, char** argv) std::clog << "Legend:" << std::endl; std::clog << "\tAlgo: (failures/runs)\tAverage error" << std::endl; - /* std::cout << std::endl << "FLOAT" << std::endl; test(M,N,F,R,seed); @@ -250,10 +246,5 @@ int main(int argc, char** argv) std::cout << std::endl << "LONG DOUBLE" << std::endl; test(M,N,F,R,seed); - */ - - std::cout << std::endl << "MPREAL 128" << std::endl; - mpreal::set_default_prec(128); - test(M,N,F,R,seed); }