3#include "Headers/matrix_util.hpp"
17 std::ifstream ifs(str);
24 std::cerr <<
"Can't open a file " << filename <<
"." << std::endl;
25 std::exit(EX_CANTCREAT);
31 std::cerr <<
"Can't open a file " << filename <<
"." << std::endl;
32 std::exit(EX_CANTCREAT);
39static inline void skip_header(std::ifstream& Infs){
40 Infs.exceptions(std::fstream::eofbit);
41 std::string line(
"#");
42 std::ifstream::pos_type pos;
43 while( line[0]==
'#' || line.length()==0 ) {
51static inline void readMatrix(std::ifstream& Infs,
int const dim1,
int const dim2, matrix<
Complex_t<double>>& mat, std::string UPLO =
"All") {
54 while(std::getline(Infs, str,
'\n')) {
55 debug_print(
"(" << (str.find(
"[")!=std::string::npos) <<
"): " << str);
56 if(str.find(
"[") != std::string::npos) { flag =
true;
break; };
59 std::cerr <<
"Error: Can't read matrix from a file." << std::endl;
60 std::exit(EX_DATAERR);
63 mat.resize(dim1,dim2);
66 std::string::size_type pos;
70 for(index1 = 0;std::getline(Infs, str); ++index1) {
71 if( str.find(
"]") != std::string::npos )
break;
72 std::replace(str.begin(), str.end(),
';',
' ');
73 std::regex_replace(str, std::regex(
" *"),
" ");
74 str.erase(std::remove(str.begin(), str.end(),
'\t'), str.end());
75 if( (pos = str.find_first_not_of(
' ')) != std::string::npos ) str.erase(std::remove(str.begin(), str.begin()+pos,
' '), str.begin()+pos);
78 ss.str(str); ss.clear(std::stringstream::goodbit);
79 for(index2 = 0; std::getline(ss, str,
' ') && index2<dim2; ) {
80 if( (pos = str.find_first_not_of(
' ')) != std::string::npos ) str.erase(std::remove(str.begin(), str.begin()+pos,
' '), str.begin()+pos);
84 if( (pos = str.find(
"i")) == std::string::npos ) {
85 if(UPLO==
"Upper" || UPLO==
"U") mat.at(index1,index1+(index2++)) = DoubleComplexOne*std::stod(str);
86 else mat.at(index1,index2++) = DoubleComplexOne*std::stod(str);
88 str.replace(pos, 1,
" ");
89 if( (pos = str.find(
'*') ) != std::string::npos ) str.replace(pos, 1,
" ");
92 flag =
false; pos = str.size();
93 while( (pos = str.rfind(
'+',pos-1)) != std::string::npos ) {
95 if(str[pos-1]!=
'e' && str[pos-1]!=
'E') { flag=
true;
break; }
100 while( (pos = str.rfind(
'-', pos-1)) != std::string::npos ) {
101 if(pos == 0) { std::cerr <<
"Can't seperate real and imaginary parts. " << std::endl; std::exit(EX_DATAERR); }
102 if(str[pos-1]!=
'e' && str[pos-1]!=
'E') { flag=
true;
break; }
108 real = std::stod(str.substr(0,pos).c_str());
109 imag = std::stod(str.substr(pos).c_str());
111 if(UPLO==
"Upper" || UPLO==
"U") mat.at(index1,index1+(index2++)) = real + DoubleComplexI*imag;
112 else mat.at(index1,index2++) = real + DoubleComplexI*imag;
115 if(UPLO==
"Upper" || UPLO==
"U") {
116 debug_print(
"# index1=" << index1 <<
", index2 = " << index2);
117 if(index1+index2 == dim2)
break;
120 debug_print(
"index1 = " << index1 <<
", index2 = " << index2);
121 if(UPLO==
"Upper" || UPLO==
"U" || UPLO==
"Lower" || UPLO==
"L") assert(index1<dim1 && index1+index2==dim2);
122 else assert(index1<dim1 && index2==dim2);
124 assert(index1 == dim1);
131static inline std::tuple<int,int,std::string> readMatrix(std::ifstream& Infs, matrix<
Complex_t<double>>& mat) {
135 auto oldpos1 = Infs.tellg(), oldpos2=oldpos1;
136 while(std::getline(Infs, str,
'\n')) {
137 debug_print(oldpos2 <<
"(" << (str.find(
"[")!=std::string::npos) <<
"): " << str);
138 if(str.find(
"[") != std::string::npos) { flag =
true;
break; };
139 oldpos2 = oldpos1; oldpos1 = Infs.tellg();
142 std::cerr <<
"Error: Can't read matrix from a file." << std::endl;
143 std::exit(EX_DATAERR);
145 Infs.seekg(oldpos2); std::getline(Infs, str);
147 std::string UPLO =
"All";
148 std::stringstream ss(str);
150 if(ss >> UPLO)
debug_print(
"# Read UPLO=" << UPLO);
151 debug_print(
"# dim1=" << dim1 <<
", dim2=" << dim2);
152 readMatrix(Infs, dim1, dim2, mat, UPLO);
153 return {dim1, dim2, UPLO};
156static inline std::tuple<int,int,std::string> readDiagonal(std::ifstream& Infs, std::vector<double>& vec) {
158 int dim1, dim2, index;
160 auto oldpos1 = Infs.tellg(), oldpos2=oldpos1;
162 while(std::getline(Infs, str,
'\n')) {
164 if(str.find(
"[") != std::string::npos) { flag =
true;
break; };
165 oldpos2 = oldpos1; oldpos1 = Infs.tellg();
168 std::cerr <<
"Error: Can't read matrix from a file." << std::endl;
169 std::exit(EX_DATAERR);
171 Infs.seekg(oldpos2); std::getline(Infs, str);
173 std::string UPLO =
"All";
174 std::stringstream ss(str);
176 if(ss >> UPLO)
debug_print(
"# Read UPLO=" << UPLO);
177 debug_print(
"# dim1=" << dim1 <<
", dim2=" << dim2);
178 vec.resize(dim1<=dim2 ? dim1: dim2);
182 for(flag =
false; std::getline(Infs, str,
'\n'); ) {
183 debug_print(
"(" << (str.find(
"[")!=std::string::npos) <<
"): " << str);
184 if(str.find(
"[") != std::string::npos) { flag =
true;
break; };
187 std::cerr <<
"Error: Can't read matrix from a file." << std::endl;
188 std::exit(EX_DATAERR);
192 for(index = 0;index < dim1 && std::getline(Infs, str); ++index) {
193 std::cout <<
"Reading diagonal elements... " << (double)index/(
double)dim1*100 <<
" %\t\r";
194 if( str.find(
"]") != std::string::npos )
break;
195 std::replace(str.begin(), str.end(),
';',
' ');
196 std::regex_replace(str, std::regex(
" *"),
" ");
197 str.erase(std::remove(str.begin(), str.end(),
'\t'), str.end());
198 if( (pos = str.find_first_not_of(
' ')) != std::string::npos )
199 str.erase(std::remove(str.begin(), str.begin()+pos,
' '), str.begin()+pos);
200 if( (pos = str.find_last_not_of(
' ')) != std::string::npos && (
size_t)pos+1 < str.size())
201 str.erase(std::remove(str.begin()+pos+1, str.end(),
' '), str.end());
202 ss.str(str); ss.clear(std::stringstream::goodbit);
204 if(UPLO ==
"Upper") {
216 for(
int i = 0;i <= index; ++i) std::getline(ss, str,
' ');
219 vec.at(index) = std::stod(str);
221 return {dim1, dim2, UPLO};
Definition mytypes.hpp:147
bool checkIsFileOpen(std::ifstream &file, std::string const &filename)
Definition file_util.hpp:22
bool checkFileExistence(std::string const &str)
Definition file_util.hpp:16
debug_print("# Determining GPU configuration.")