Control of output format produced by blaze::operator<<

Issue #387 new
Paul Jurczak created an issue

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)

  1. Paul Jurczak reporter

    My quick and dirty solution is:

    namespace blaze {
    
    inline int& formattingWidth()
    {
      static int fw = 12;
    
      return fw;
    }
    
    }
    

    placed in topmost header and changes of inline std::ostream& operator<<, e.g.:

    template< typename MT  // Type of the matrix
            , bool SO >    // Storage order
    inline std::ostream& operator<<( std::ostream& os, const Matrix<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;
    }
    

    This allows me to change the format on demand with:

      formattingWidth() = 5;
    

  2. Klaus Iglberger

    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 a DenseMatrix. 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!

  3. Norman Goldstein

    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.

  4. Klaus Iglberger

    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!

  5. Log in to comment