Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_MATH_BIG_IO_BIG_WRITE_DLM_H
00002 #define OPENTISSUE_CORE_MATH_BIG_IO_BIG_WRITE_DLM_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_value_traits.h>
00013
00014 #include <boost/cast.hpp>
00015
00016 #include <fstream>
00017 #include <stdexcept>
00018
00019 namespace OpenTissue
00020 {
00021 namespace math
00022 {
00023 namespace big
00024 {
00033 template<typename matrix_type>
00034 inline bool write_dlm_matrix(std::string const & filename, matrix_type &A)
00035 {
00036 using std::fabs;
00037
00038 typedef typename matrix_type::value_type value_type;
00039 typedef OpenTissue::math::ValueTraits<value_type> value_traits;
00040
00041 static value_type const tiny = boost::numeric_cast<value_type>(1e-10);
00042
00043 std::ofstream file;
00044 file.open( filename.c_str() );
00045 if ( !file.is_open() || file.bad() )
00046 {
00047 throw std::logic_error("could not open the specified file");
00048 return false;
00049 }
00050
00051 file << A.size1() << " " << A.size2() << " " << A.nnz() << std::endl;
00052
00053 for(size_t i=0;i<A.size1();++i)
00054 {
00055 for(size_t j=0;j<A.size2();++j)
00056 {
00057 value_type value = A(i,j);
00058 if(value>value_traits::zero() || value<value_traits::zero())
00059 file << i << " " << j << " " << value << std::endl;
00060 }
00061 }
00062
00063 file.flush();
00064 file.close();
00065
00066 return true;
00067 }
00068
00078 template<typename vector_type>
00079 inline bool write_dlm_vector(std::string const & filename, vector_type & x)
00080 {
00081 std::ofstream file;
00082 file.open( filename.c_str() );
00083 if ( !file.is_open() || file.bad() )
00084 {
00085 throw std::logic_error("could not open file?");
00086 return false;
00087 }
00088 file << x.size() << std::endl;
00089 for(size_t i = 0;i<x.size();++i)
00090 {
00091 file << i << " " << x(i) << std::endl;
00092 }
00093 file.flush();
00094 file.close();
00095 return true;
00096 }
00097
00098 }
00099 }
00100 }
00101
00102
00103 #endif