30 #ifndef CORE_DATATYPES_MATRIX_IO_H
31 #define CORE_DATATYPES_MATRIX_IO_H
50 std::vector<std::vector<T> >
values;
54 while (!std::getline(istr, line,
'\n').eof())
56 std::istringstream reader(line);
58 std::vector<T> lineData;
71 lineData.push_back(val);
74 if (!lineData.empty())
76 if (!values.empty() && values.back().size() != lineData.size())
77 THROW_INVALID_ARGUMENT(
"Improper format of matrix text stream: not every line contains the same amount of numbers.");
79 values.push_back(lineData);
83 m.resize(values.size(), values[0].size());
85 for (
int i = 0; i < m.rows(); ++i)
87 for (
int j = 0; j < m.cols(); ++j)
89 m(i,j) = values[i][j];
104 #define DENSEMATRIX_VERSION 4
106 template <
typename T>
115 int nrows =
static_cast<int>(this->rows());
116 int ncols =
static_cast<int>(this->cols());
119 this->resize(nrows, ncols);
123 long long nrows =
static_cast<long long>(this->rows());
124 long long ncols =
static_cast<long long>(this->cols());
129 this->resize(nrows, ncols);
140 split = this->separate_raw_;
142 if (this->separate_raw_)
144 Pio(stream, this->raw_filename_);
145 FILE *f=fopen(this->raw_filename_.c_str(),
"r");
148 fread(this->
data(),
sizeof(T), this->rows() * this->cols(), f);
153 const std::string errmsg =
"Error reading separated file '" + this->raw_filename_ +
"'";
154 std::cerr << errmsg <<
"\n";
161 this->separate_raw_ =
false;
163 split = this->separate_raw_;
167 std::string filename = this->raw_filename_;
168 split = this->separate_raw_;
175 size_t pos = stream.
file_name.rfind(
'.');
176 if (pos == std::string::npos) filename = stream.
file_name +
".raw";
177 else filename = stream.
file_name.substr(0,pos) +
".raw";
188 Pio(stream, filename);
189 FILE *f = fopen(filename.c_str(),
"w");
190 fwrite(this->
data(),
sizeof(T), this->rows() * this->cols(), f);
197 size_t block_size = this->rows() * this->cols();
198 if (!stream.
block_io(this->data(),
sizeof(T), block_size))
200 for (
size_t i = 0; i < block_size; i++)
202 stream.
io(this->
data()[i]);
211 #define SPARSEROWMATRIX_VERSION 2
213 template <
typename T>
222 int r =
static_cast<int>(this->nrows());
223 int c =
static_cast<int>(this->ncols());
224 int n =
static_cast<int>(this->nonZeros());
229 this->resizeNonZeros(n);
233 auto r = this->nrows();
235 auto c = this->ncols();
237 auto n = this->nonZeros();
242 this->resizeNonZeros(
n);
247 Pio_index(stream, this->outerIndexPtr(), this->nrows() + 1);
251 Pio_index(stream, this->innerIndexPtr(), this->nonZeros());
255 Pio(stream, this->valuePtr(), this->nonZeros());
261 #define COLUMNMATRIX_VERSION 3
263 template <
typename T>
276 int nrows =
static_cast<int>(this->nrows());
278 this->resize(static_cast<size_type>(nrows), 1);
282 long long nrows=
static_cast<long long>(this->nrows());
286 this->resize(static_cast<size_type>(nrows), 1);
290 if (!stream.
block_io(this->data(),
sizeof(T), this->nrows()))
292 for (
size_t i=0; i<this->nrows(); i++)
293 stream.
io(this->data()[i]);
virtual void io(Piostream &)
Definition: MatrixIO.h:107
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
Definition: DenseMatrix.h:44
virtual void io(Piostream &)
Definition: Matrix.cc:38
virtual void io(bool &)
Definition: Persistent.cc:193
Definition: Persistent.h:89
#define COLUMNMATRIX_VERSION
Definition: MatrixIO.h:261
virtual void io(Piostream &)
Definition: MatrixIO.h:264
virtual void begin_cheap_delim()
Definition: Persistent.cc:183
#define DENSEMATRIX_VERSION
Definition: MatrixIO.h:104
list values
Definition: readAllFields.py:22
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
Manage dynamic properties of persistent objects.
#define THROW_INVALID_ARGUMENT(message)
Definition: Exception.h:71
Definition: Exception.h:46
void Pio_index(Piostream &stream, index_type *data, size_type size)
Definition: Persistent.cc:606
dictionary data
Definition: eabLatVolData.py:11
virtual void io(Piostream &)
Definition: MatrixIO.h:214
virtual void end_cheap_delim()
Definition: Persistent.cc:188
std::istream & operator>>(std::istream &istr, DenseMatrixGeneric< T > &m)
Definition: MatrixIO.h:48
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
virtual void end_class()
Definition: Persistent.cc:178
boost::error_info< struct tag_file_not_found, std::string > FileNotFound
Definition: Exception.h:54
int n
Definition: eab.py:9
std::string file_name
Definition: Persistent.h:135
std::string matrix_to_string(const T &m)
Definition: MatrixIO.h:97
void Pio_size(Piostream &stream, Size &size)
Definition: Persistent.h:273
#define SPARSEROWMATRIX_VERSION
Definition: MatrixIO.h:211