23 #ifndef CPP_INCLUDE_UTIL_H_ 24 #define CPP_INCLUDE_UTIL_H_ 56 int num_rows,
int num_cols,
57 const std::string delimiter =
" ") {
59 std::ifstream input_file(input_file_path, std::ifstream::in);
65 while ( !input_file.eof() ) {
67 getline(input_file, line);
68 if (line.find_first_not_of(
' ') == std::string::npos)
70 if (delimiter ==
" " && line.find_first_of(
',') != std::string::npos) {
71 std::cerr <<
"File uses different delimiter than parameter! Use ','!";
73 }
else if (delimiter ==
",") {
74 if (line.find_first_of(
',') == std::string::npos) {
75 std::cerr <<
"File uses different delimiter than parameter! Use ' '!";
79 std::replace(line.begin(), line.end(),
',',
' ');
80 }
else if (delimiter !=
" ") {
82 std::cerr <<
"'" + delimiter +
"' isn't an accepted delimiter";
86 std::stringstream stream(line);
90 while (stream >> coef) {
99 std::cerr <<
"Cannot open file " + input_file_path +
", exiting...";
115 const std::string delimiter =
" ") {
117 std::ifstream input_file(input_file_path, std::ifstream::in);
120 std::vector<T> temp_buffer;
127 while (!input_file.eof()) {
128 getline(input_file, line);
129 if (line.find_first_not_of(
' ') == std::string::npos)
131 if (delimiter ==
" " && line.find_first_of(
',') != std::string::npos) {
132 std::cerr <<
"File uses different delimiter than parameter! Use ','!";
134 }
else if (delimiter ==
",") {
135 if (line.find_first_of(
',') == std::string::npos) {
136 std::cerr <<
"File uses different delimiter than parameter! Use ' '!";
140 std::replace(line.begin(), line.end(),
',',
' ');
141 }
else if (delimiter !=
" ") {
143 std::cerr <<
"'" + delimiter +
"' isn't an accepted delimiter";
147 std::stringstream stream(line);
150 while (stream >> coef) {
151 temp_buffer.push_back(coef);
157 num_cols = cols_in_row;
159 }
else if (num_cols != cols_in_row) {
160 std::cerr <<
"Problem with Matrix in: " + input_file_path +
168 m.resize(num_rows, num_cols);
169 for (
int i = 0; i < num_rows; ++i)
170 for (
int j = 0; j < num_cols; ++j)
171 m(i, j) = temp_buffer[i * num_cols + j];
176 std::cerr <<
"Cannot open file " + input_file_path +
", exiting...";
184 std::ifstream input_file(input_file_path, std::ifstream::in);
187 for (
int i = 0; i < num_elements; i++)
191 std::cerr <<
"Cannot open file " + input_file_path +
", exiting..." <<
198 static T reciprocal(T x) {
206 #endif // CPP_INCLUDE_UTIL_H_ Definition: cpu_operations.h:36
Matrix< T > FromFile(const std::string &input_file_path, int num_rows, int num_cols, const std::string delimiter=" ")
Definition: util.h:55
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
Definition: vector.h:31
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition: matrix.h:31