Control of output format produced by blaze::operator<<
This code snippet:
StaticMatrix<float, 3, 3, rowMajor> m{1.0};
cout << m << endl;
produces
( 1 1 1 )
( 1 1 1 )
( 1 1 1 )
which is very liberal with white space, i.e. my larger matrices don’t display nicely. Is there a way to set the width of elements? Global setting would work for me.
Comments (4)
-
reporter -
Hi Paul!
Unfortunately there is no way (yet) to customize the spacing between values. Luckily, Blaze allows you to provide a custom output operator (similar to the one you’ve posted):
template< typename MT // Type of the matrix , bool SO > // Storage order inline std::ostream& operator<<( std::ostream& os, const DenseMatrix<MT,SO>& m ) { CompositeType_t<MT> tmp( *m ); for( size_t i=0UL; i<tmp.rows(); ++i ) { os << "( "; for( size_t j=0UL; j<tmp.columns(); ++j ) { os << std::setw(formattingWidth()) << tmp(i,j) << " "; } os << ")\n"; } return os; }
Note that this operator does not take a
Matrix
, but aDenseMatrix
. If you add this operator to your code, you have full control over the formatting of all dense matrices (no need to change Blaze code).However, we agree that it should be possible to more easily customize the output formatting of matrices. We’ll use this opportunity to rework this functionality. Thanks a lot creating this proposal,
Best regards,
Klaus!
-
I figure this is a good place to mention another option for operator<<: Have the newline, “\n”, at the end to be optional (or, simply, omitted). This would make it possible to write two row vectors on the same line.
-
Hi Norman!
Thanks for mentioning this. We agree, that would indeed be a reasonable change. For now, please use the
operator<<()
from above and customize it accordingly.Best regards,
Klaus!
- Log in to comment
My quick and dirty solution is:
placed in topmost header and changes of
inline std::ostream& operator<<
, e.g.:This allows me to change the format on demand with: