3 #include <boost/log/trivial.hpp> 4 #include <boost/program_options.hpp> 11 const unsigned int nC) {
18 arma::sp_mat MMat(nR, nC);
20 if (nR >= Mat.n_rows && nC >= Mat.n_cols) {
21 if (Mat.n_rows >= 1 && Mat.n_cols >= 1)
22 MMat.submat(0, 0, Mat.n_rows - 1, Mat.n_cols - 1) = Mat;
24 if (nR <= Mat.n_rows && nC <= Mat.n_cols)
25 MMat = Mat.submat(0, 0, nR, nC);
28 "Error in resize() - the patch for arma::resize. Either both " 29 "dimension should be larger or both should be smaller!");
36 const unsigned int nC) {
43 arma::mat MMat(nR, nC);
45 if (nR >= Mat.n_rows && nC >= Mat.n_cols) {
46 if (Mat.n_rows >= 1 && Mat.n_cols >= 1)
47 MMat.submat(0, 0, Mat.n_rows - 1, Mat.n_cols - 1) = Mat;
49 if (nR <= Mat.n_rows && nC <= Mat.n_cols)
50 MMat = Mat.submat(0, 0, nR, nC);
53 "Error in resize() - the patch for arma::resize. Either both " 54 "dimension should be larger or both should be smaller!");
70 MMat.subvec(0, Mat.n_rows - 1) = Mat;
72 MMat = Mat.subvec(0, nR);
88 unsigned int nR{0}, nC{0}, nnz{0};
90 ofstream outfile(out, erase ? ios::out : ios::app);
94 nnz = matrix.n_nonzero;
96 outfile << header <<
"\n";
97 outfile << nR <<
"\t" << nC <<
"\t" << nnz <<
"\n";
98 for (
auto it = matrix.begin(); it != matrix.end(); ++it)
99 outfile << it.row() <<
"\t" << it.col() <<
"\t" << (*it)
117 unsigned int nR, nC, nnz;
119 ifstream infile(in, ios::in);
122 string header_checkwith;
123 infile >> header_checkwith;
125 if (header !=
"" && header != header_checkwith)
127 "Error in Utils::appendRead<sp_mat>. Wrong header. Expected: " +
128 header +
" Found: " + header_checkwith);
130 infile >> nR >> nC >> nnz;
131 if (nR == 0 || nC == 0)
132 matrix.set_size(nR, nC);
134 arma::umat locations(2, nnz);
135 arma::vec values(nnz);
140 for (
unsigned int i = 0; i < nnz; ++i) {
141 infile >> r >> c >> val;
146 matrix = arma::sp_mat(locations, values, nR, nC);
149 pos = infile.tellg();
155 void appendSave(
const vector<double> v,
const string out,
const string header,
160 ofstream outfile(out, erase ? ios::out : ios::app);
161 outfile << header <<
"\n" << v.size() <<
"\n";
162 for (
const double x : v)
163 outfile << x <<
"\n";
167 long int appendRead(vector<double> &v,
const string in,
long int pos,
168 const string header) {
169 unsigned long int size;
170 ifstream infile(in, ios::in);
177 string header_checkwith;
178 infile >> header_checkwith;
180 if (header !=
"" && header != header_checkwith)
182 "Error in Utils::appendRead<sp_mat>. Wrong header. Expected: " +
183 header +
" Found: " + header_checkwith);
188 for (
unsigned int i = 0; i < size; ++i)
190 pos = infile.tellg();
208 ofstream outfile(out, erase ? ios::out : ios::app);
212 outfile << header <<
"\n";
214 outfile << nR <<
"\n";
215 for (
auto it = matrix.begin(); it != matrix.end(); ++it)
216 outfile << (*it) <<
"\n";
235 ifstream in_file(in, ios::in);
238 in_file >> checkwith;
239 if (header !=
"" && checkwith != header)
240 throw string(
"Error in Utils::appendRead<vec>. Wrong header. Expected: " +
241 header +
" Found: " + checkwith);
244 for (
unsigned int i = 0; i < nR; ++i) {
250 pos = in_file.tellg();
262 ofstream outfile(out, erase ? ios::out : ios::app);
263 outfile << header <<
"\n";
264 outfile << v <<
"\n";
269 const string header) {
274 ifstream infile(in, ios::in);
277 string header_checkwith;
278 infile >> header_checkwith;
280 if (header !=
"" && header != header_checkwith)
282 "Error in Utils::appendRead<long int>. Wrong header. Expected: " +
283 header +
" Found: " + header_checkwith);
289 pos = infile.tellg();
296 const string header,
bool erase)
301 ofstream outfile(out, erase ? ios::out : ios::app);
302 outfile << header <<
"\n";
303 outfile << v <<
"\n";
308 const string header) {
309 ifstream infile(in, ios::in);
312 string header_checkwith;
313 infile >> header_checkwith;
315 if (header !=
"" && header != header_checkwith)
317 "Error in Utils::appendRead<unsigned int>. Wrong header. Expected: " +
318 header +
" Found: " + header_checkwith);
324 pos = infile.tellg();
335 ofstream outfile(out, erase ? ios::out : ios::app);
336 outfile << v <<
"\n";
345 ifstream infile(in, ios::in);
352 pos = infile.tellg();
358 unsigned long int number = 0;
359 unsigned int posn = 1;
360 while (!binary.empty()) {
361 short int bit = (binary.back() + 1) / 2;
362 number += (bit * posn);
370 const unsigned int &nCompl) {
371 std::vector<short int> binary{};
372 for (
unsigned int vv = 0; vv < nCompl; vv++) {
373 binary.push_back(number % 2);
376 std::for_each(binary.begin(), binary.end(),
377 [](
short int &vv) { vv = (vv == 0 ? -1 : 1); });
378 std::reverse(binary.begin(), binary.end());
long int appendRead(vec &matrix, const std::string in, long int pos, const std::string header="")
unsigned long int vec_to_num(std::vector< short int > binary)
void appendSave(const vec &matrix, const std::string out, const std::string header="", bool erase=false)
void appendSave(const vector< double > v, const string out, const string header, bool erase)
arma::sp_mat resize_patch(const arma::sp_mat &Mat, const unsigned int nR, const unsigned int nC)
long int appendRead(vector< double > &v, const string in, long int pos, const string header)
std::vector< short int > num_to_vec(unsigned long int number, const unsigned int &nCompl)