Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_BIG_IO_READ_DLM_H
00002 #define OPENTISSUE_CORE_MATH_BIG_IO_READ_DLM_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <boost/cast.hpp>
00013
00014 #include <fstream>
00015 #include <stdexcept>
00016
00017 namespace OpenTissue
00018 {
00019 namespace math
00020 {
00021 namespace big
00022 {
00041 template<typename matrix_type>
00042 inline bool read_dlm_matrix(std::string const & filename, matrix_type &A)
00043 {
00044 using std::fabs;
00045
00046 typedef typename matrix_type::value_type real_type;
00047 typedef typename matrix_type::size_type size_type;
00048 std::ifstream ifs;
00049 ifs.open( filename.c_str() );
00050 if ( !ifs.is_open() || ifs.bad() )
00051 {
00052 throw std::logic_error("could not open the specified file");
00053 return false;
00054 }
00055 size_type m,n,nnz;
00056 ifs >> m; ifs >> n; ifs >> nnz;
00057 matrix_type M(m,n,nnz);
00058 static real_type const tiny = boost::numeric_cast<real_type>(1e-10);
00059 size_type i, j;
00060 while ( ifs.good() )
00061 {
00062 ifs >> i;
00063 ifs >> j;
00064 real_type s;
00065 ifs >> s;
00066 if( fabs(s) > tiny)
00067 M(i,j) = s;
00068 }
00069 A = M;
00070 return true;
00071 }
00072
00088 template<typename vector_type>
00089 inline bool read_dlm_vector(std::string const & filename, vector_type & x)
00090 {
00091 typedef typename vector_type::size_type size_type;
00092
00093 std::ifstream ifs;
00094
00095 ifs.open( filename.c_str() );
00096
00097 if ( !ifs.is_open() || ifs.bad() )
00098 {
00099 throw std::logic_error("could not open file?");
00100 return false;
00101 }
00102
00103 size_type i, size;
00104
00105 ifs >> size;
00106 x.resize(size);
00107
00108 while ( ifs.good() )
00109 {
00110 ifs >> i;
00111 ifs >> x(i);
00112 }
00113 return true;
00114 }
00115
00116 }
00117 }
00118 }
00119
00120
00121 #endif