[eigen] Patch for diagonal matrix output

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi eigen users and developers,

   I found I couldn't use the << operator to write a diagonal matrix to cout 
without having to convert to a dense matrix intermediate. So I wrote the 
attached patch. Please consider this for inclusion if it passes tests.

   The patch was created against the default branch from the root of the 
working copy directory.

Thanks,
Manoj
diff -r 307ef81e6a3b Eigen/src/Core/DiagonalMatrix.h
--- a/Eigen/src/Core/DiagonalMatrix.h	Fri Apr 02 21:33:34 2010 +0100
+++ b/Eigen/src/Core/DiagonalMatrix.h	Fri Apr 02 23:48:36 2010 -0400
@@ -73,6 +73,19 @@ class DiagonalBase : public EigenBase<De
     {
       return diagonal().cwiseInverse();
     }
+
+	/** \returns the matrix or vector obtained by evaluating this expression.
+      *
+      * Notice that in the case of a plain matrix or vector (not an expression) this function just returns
+      * a const reference, in order to avoid a useless copy.
+      */
+    inline const typename ei_eval<Derived>::type eval() const
+    { 
+      // MSVC cannot honor strong inlining when the return type 
+      // is a dynamic matrix
+      return typename ei_eval<Derived>::type(derived()); 
+    }
+	inline const WithFormat<Derived> format(const IOFormat& fmt) const;
 };
 
 template<typename Derived>
diff -r 307ef81e6a3b Eigen/src/Core/IO.h
--- a/Eigen/src/Core/IO.h	Fri Apr 02 21:33:34 2010 +0100
+++ b/Eigen/src/Core/IO.h	Fri Apr 02 23:48:36 2010 -0400
@@ -126,6 +126,20 @@ DenseBase<Derived>::format(const IOForma
   return WithFormat<Derived>(derived(), fmt);
 }
 
+/** \returns a WithFormat proxy object allowing to print a matrix the with given
+  * format \a fmt.
+  *
+  * See class IOFormat for some examples.
+  *
+  * \sa class IOFormat, class WithFormat
+  */
+template<typename Derived>
+inline const WithFormat<Derived>
+DiagonalBase<Derived>::format(const IOFormat& fmt) const
+{
+  return WithFormat<Derived>(derived(), fmt);
+}
+
 template<typename Scalar>
 struct ei_significant_decimals_impl
 {
@@ -224,4 +238,23 @@ std::ostream & operator <<
   return ei_print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
 }
 
+/** \relates DiagonalBase
+  *
+  * Outputs the diagonal matrix, to the given stream.
+  *
+  * If you wish to print the matrix with a format different than the default, use DiagonalBase::format().
+  *
+  * It is also possible to change the default format by defining EIGEN_DEFAULT_IO_FORMAT before including Eigen headers.
+  * If not defined, this will automatically be defined to Eigen::IOFormat(), that is the Eigen::IOFormat with default parameters.
+  *
+  * \sa DiagonalBase::format()
+  */
+template<typename Derived>
+std::ostream & operator <<
+(std::ostream & s,
+  const DiagonalBase<Derived> & m)
+{
+  return ei_print_matrix(s, m.eval(), EIGEN_DEFAULT_IO_FORMAT);
+}
+
 #endif // EIGEN_IO_H


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/