1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_WRITER_H_
2 #define GENESIS_UTILS_CONTAINERS_MATRIX_WRITER_H_
73 : separator_(separator)
104 std::shared_ptr<utils::BaseOutputTarget> target,
105 std::vector<std::string> row_names = {},
106 std::vector<std::string> col_names = {},
107 std::string corner =
""
109 if( row_names.size() == matrix.
rows() + 1 ) {
110 if( corner.empty() ) {
111 corner = row_names[0];
112 row_names.erase( row_names.begin() );
114 throw std::runtime_error(
"Number of row names is different from Matrix row size." );
116 }
else if( col_names.size() == matrix.
cols() + 1 ) {
117 if( corner.empty() ) {
118 corner = col_names[0];
119 col_names.erase( col_names.begin() );
121 throw std::runtime_error(
"Number of col names is different from Matrix col size." );
124 to_stream_( matrix, target->ostream(), row_names, col_names, corner );
155 write_value_ = functor;
167 std::vector<std::string>
const& row_names,
168 std::vector<std::string>
const& col_names,
169 std::string
const& corner
173 if( ! row_names.empty() && row_names.size() != mat.
rows() ) {
174 throw std::invalid_argument(
"Number of row names is different from Matrix row size." );
176 if( ! col_names.empty() && col_names.size() != mat.
cols() ) {
177 throw std::invalid_argument(
"Number of col names is different from Matrix col size." );
183 to_matrix_( mat, os, row_names, col_names, corner );
187 to_list_( mat, os, row_names, col_names );
191 to_triangular_( mat, os, row_names, col_names, corner );
195 throw std::invalid_argument(
"Invalid enum value for MatrixWriter::Format" );
201 Matrix<T>
const& mat,
203 std::vector<std::string>
const& row_names,
204 std::vector<std::string>
const& col_names,
205 std::string
const& corner
209 if( ! row_names.empty() && ! col_names.empty() ) {
210 os << corner << separator_;
214 if( ! col_names.empty() ) {
215 for(
size_t c = 0; c < col_names.size(); ++c ) {
225 for(
size_t r = 0; r < mat.rows(); ++r ) {
226 if( ! row_names.empty() ) {
227 os << row_names[r] << separator_;
230 for(
size_t c = 0; c < mat.cols(); ++c ) {
235 os << write_value_( mat( r, c ));
246 Matrix<T>
const& mat,
248 std::vector<std::string>
const& row_names,
249 std::vector<std::string>
const& col_names
253 for(
size_t r = 0; r < mat.rows(); ++r ) {
254 for(
size_t c = 0; c < mat.cols(); ++c ) {
255 if( ! row_names.empty() ) {
256 os << row_names[r] << separator_;
258 if( ! col_names.empty() ) {
259 os << col_names[c] << separator_;
268 Matrix<T>
const& mat,
270 std::vector<std::string>
const& row_names,
271 std::vector<std::string>
const& col_names,
272 std::string
const& corner
276 if( mat.rows() != mat.cols() ) {
277 throw std::invalid_argument(
"Cannot write triangular matrix, as it is not quadratic." );
281 if( ! row_names.empty() && ! col_names.empty() ) {
282 os << corner << separator_;
286 if( ! col_names.empty() ) {
287 for(
size_t cb = 0; cb < col_names.size(); ++cb ) {
288 auto const c = col_names.size() - cb - 1;
298 for(
size_t r = 0; r < mat.rows(); ++r ) {
299 if( ! row_names.empty() ) {
300 os << row_names[r] << separator_;
304 for(
size_t cb = 0; cb < mat.cols() - r; ++cb ) {
305 auto const c = mat.cols() - cb - 1;
306 assert( c < mat.cols() );
312 os << write_value_( mat( r, c ));
328 std::string separator_ = std::string(
"\t" );
331 std::function<std::string( T
const& )> write_value_;
338 #endif // include guard