Go to the documentation of this file.00001 #ifndef matfileinput_hh
00002 #define matfileinput_hh
00003
00004 #include <sys/time.h>
00005 #include "operator.hh"
00006
00007 #include "matfile_concepts/Input"
00008 #include "sparseIWrapper.hh"
00009 #include "denseIWrapper.hh"
00010 #include <typeinfo>
00011
00012 using namespace matfile;
00013
00014
00015 namespace concepts{
00016
00017 class MatfileInput {
00018 public:
00022 MatfileInput(){
00023 idevice_=0;
00024 }
00025
00026
00027
00031 MatfileInput(const std::string filename){
00032 idevice_= new matfile::InputDevice(matfileEnding_(filename.c_str()));
00033 }
00034
00038 virtual ~MatfileInput(){
00039 delete(idevice_);
00040 }
00041
00042
00043
00047 void openMatfileI(const std::string filename){
00048 delete(idevice_);
00049 idevice_ = new matfile::InputDevice( matfileEnding_(filename.c_str()) );
00050 }
00051
00052
00057 template<class T>
00058 concepts::SparseMatrix<T>* getSparse(const std::string matname){
00059
00060
00061 MatrixInfo minfo = (*idevice_)[matname];
00062
00063
00064 if ( ( minfo.isComplex() && typeid(T)==typeid(concepts::Real) )
00065 || (!minfo.isComplex() && typeid(T)==typeid(concepts::Cmplx))
00066 || ( minfo.getClass() == 6)
00067 )
00068 return NULL;
00069
00070 uint dim[2] = { minfo.getDimensions()[0], minfo.getDimensions()[1] };
00071
00072
00073 concepts::Array<T> val;
00074 concepts::Array<miUINT32_t> col_ptr, row;
00075
00076 SparseIWrapper<T> wrapper_sparse(val, col_ptr, row);
00077 MatfileInput::idevice_->readArray(matname.c_str(), wrapper_sparse);
00078 wrapper_sparse.get_matrix();
00079
00080
00081
00082 SparseMatrix<T>*
00083 matrix = new concepts::SparseMatrix<T>(dim[0],col_ptr.size()-1);
00084 const T* iv = (const T*)val;
00085 const miUINT32_t* ir = (const miUINT32_t*)row;
00086 const miUINT32_t* ic = (const miUINT32_t*)col_ptr;
00087
00088 for(uint j = 0; j < col_ptr.size()-1; ++j)
00089 for(uint k = *ic++; k < *ic; ++k)
00090 (*matrix)(*ir++,j)=*iv++;
00091
00092 return matrix;
00093
00094 }
00095
00096
00101 template<class T>
00102 concepts::DenseMatrix<T>* getDense(const std::string matname) {
00103
00104 MatrixInfo minfo = (*idevice_)[matname];
00105
00106
00107
00108 if ( ( minfo.isComplex() && typeid(T)==typeid(concepts::Real) )
00109 || (!minfo.isComplex() && typeid(T)==typeid(concepts::Cmplx))
00110 || ( minfo.getClass() == 5)
00111 )
00112 return NULL;
00113
00114
00115 uint dim[2] = { minfo.getDimensions()[0], minfo.getDimensions()[1] };
00116
00117
00118 concepts::DenseMatrix<T>*
00119 matrix = new concepts::DenseMatrix<T>(dim[0], dim[1], true);
00120 DenseIWrapper<T> wrapper_dense(*matrix);
00121 idevice_->readArray(matname.c_str(),wrapper_dense);
00122 wrapper_dense.get_matrix();
00123 return matrix;
00124 }
00125
00126 private:
00130 std::string matfileEnding_(const std::string& filename) {
00131 uint size = filename.size();
00132 if (size < 4 || std::string(filename, size - 4, 4) != ".mat")
00133 return (filename + ".mat");
00134 return filename;
00135 }
00136
00140 matfile::InputDevice* idevice_;
00141 };
00142
00143 }
00144
00145 #endif