From 6b9370e0f0608f203337539c72fe4821d925a978 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Wed, 26 Dec 2007 09:25:00 +0000 Subject: [PATCH] more changes in ScalarMultiple, reintroduce FloatingPoint in NumTraits, improve examples --- Eigen/Core/NumTraits.h | 4 ++++ Eigen/Core/ScalarMultiple.h | 6 ++++-- doc/examples/class_Block.cpp | 15 +++++++++------ doc/examples/class_Cast.cpp | 18 ++++++++++-------- doc/examples/class_Column.cpp | 13 ++++++++++--- doc/examples/class_DynBlock.cpp | 13 ++++++++++--- doc/examples/class_Row.cpp | 13 ++++++++++--- 7 files changed, 57 insertions(+), 25 deletions(-) diff --git a/Eigen/Core/NumTraits.h b/Eigen/Core/NumTraits.h index 9269fbcdc..d020c60f4 100644 --- a/Eigen/Core/NumTraits.h +++ b/Eigen/Core/NumTraits.h @@ -31,6 +31,7 @@ template struct NumTraits; template<> struct NumTraits { typedef int Real; + typedef double FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = false; }; @@ -38,6 +39,7 @@ template<> struct NumTraits template<> struct NumTraits { typedef float Real; + typedef float FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = true; }; @@ -45,6 +47,7 @@ template<> struct NumTraits template<> struct NumTraits { typedef double Real; + typedef double FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = true; }; @@ -52,6 +55,7 @@ template<> struct NumTraits template struct NumTraits > { typedef _Real Real; + typedef std::complex<_Real> FloatingPoint; static const bool IsComplex = true; static const bool HasFloatingPoint = NumTraits::HasFloatingPoint; }; diff --git a/Eigen/Core/ScalarMultiple.h b/Eigen/Core/ScalarMultiple.h index 06c6e295f..ef424f79a 100644 --- a/Eigen/Core/ScalarMultiple.h +++ b/Eigen/Core/ScalarMultiple.h @@ -76,12 +76,14 @@ operator*(FactorType scalar, \ } \ \ template \ -const ScalarMultiple \ +const ScalarMultiple::FloatingPoint, Derived> \ operator/(const MatrixBase& matrix, \ FactorType scalar) \ { \ assert(NumTraits::HasFloatingPoint); \ - return matrix * (static_cast(1) / scalar); \ + return matrix * (static_cast< \ + typename NumTraits::FloatingPoint \ + >(1) / scalar); \ } \ \ template \ diff --git a/doc/examples/class_Block.cpp b/doc/examples/class_Block.cpp index 17dc4bce1..efd90ddbb 100644 --- a/doc/examples/class_Block.cpp +++ b/doc/examples/class_Block.cpp @@ -7,17 +7,20 @@ Eigen::Block topLeft2x2Corner(MatrixBase& m) { return Eigen::Block(m.ref(), 0, 0); - // note: tempting as it is, writing "m.block<2,2>(0,0)" here - // causes a compile error with g++ 4.2, apparently due to - // g++ getting confused by the many template types and - // template arguments involved. +} + +template +const Eigen::Block +topLeft2x2Corner(const MatrixBase& m) +{ + return Eigen::Block(m.ref(), 0, 0); } int main(int, char**) { Matrix3d m = Matrix3d::identity(); - cout << topLeft2x2Corner(m) << endl; - topLeft2x2Corner(m) *= 2; + cout << topLeft2x2Corner(4*m) << endl; // calls the const version + topLeft2x2Corner(m) *= 2; // calls the non-const version cout << "Now the matrix m is:" << endl << m << endl; return 0; } diff --git a/doc/examples/class_Cast.cpp b/doc/examples/class_Cast.cpp index d1dfdc8be..5ac9ed3f9 100644 --- a/doc/examples/class_Cast.cpp +++ b/doc/examples/class_Cast.cpp @@ -3,14 +3,16 @@ USING_PART_OF_NAMESPACE_EIGEN using namespace std; template -Eigen::Cast -castToDouble(const MatrixBase& m) +const Eigen::Cast< + typename Eigen::NumTraits::FloatingPoint, + Derived +> +castToFloatingPoint(const MatrixBase& m) { - return Eigen::Cast(m.ref()); - // note: tempting as it is, writing "m.cast()" here - // causes a compile error with g++ 4.2, apparently due to - // g++ getting confused by the many template types and - // template arguments involved. + return Eigen::Cast< + typename Eigen::NumTraits::FloatingPoint, + Derived + >(m.ref()); } int main(int, char**) @@ -18,6 +20,6 @@ int main(int, char**) Matrix2i m = Matrix2i::random(); cout << "Here's the matrix m. It has coefficients of type int." << endl << m << endl; - cout << "Here's 0.05*m:" << endl << 0.05 * castToDouble(m) << endl; + cout << "Here's m/20:" << endl << castToFloatingPoint(m)/20 << endl; return 0; } diff --git a/doc/examples/class_Column.cpp b/doc/examples/class_Column.cpp index ec8628ab7..83501f3d6 100644 --- a/doc/examples/class_Column.cpp +++ b/doc/examples/class_Column.cpp @@ -6,14 +6,21 @@ template Eigen::Column firstColumn(MatrixBase& m) { - return m.col(0); + return Eigen::Column(m.ref(), 0); +} + +template +const Eigen::Column +firstColumn(const MatrixBase& m) +{ + return Eigen::Column(m.ref(), 0); } int main(int, char**) { Matrix4d m = Matrix4d::identity(); - cout << firstColumn(m) << endl; - firstColumn(m) *= 5; + cout << firstColumn(2*m) << endl; // calls the const version + firstColumn(m) *= 5; // calls the non-const version cout << "Now the matrix m is:" << endl << m << endl; return 0; } diff --git a/doc/examples/class_DynBlock.cpp b/doc/examples/class_DynBlock.cpp index 3a8b607da..638055fa9 100644 --- a/doc/examples/class_DynBlock.cpp +++ b/doc/examples/class_DynBlock.cpp @@ -6,14 +6,21 @@ template Eigen::DynBlock topLeftCorner(MatrixBase& m, int rows, int cols) { - return m.dynBlock(0, 0, rows, cols); + return Eigen::DynBlock(m.ref(), 0, 0, rows, cols); +} + +template +const Eigen::DynBlock +topLeftCorner(const MatrixBase& m, int rows, int cols) +{ + return Eigen::DynBlock(m.ref(), 0, 0, rows, cols); } int main(int, char**) { Matrix4d m = Matrix4d::identity(); - cout << topLeftCorner(m, 2, 3) << endl; - topLeftCorner(m, 2, 3) *= 5; + cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version + topLeftCorner(m, 2, 3) *= 5; // calls the non-const version cout << "Now the matrix m is:" << endl << m << endl; return 0; } diff --git a/doc/examples/class_Row.cpp b/doc/examples/class_Row.cpp index 70a388015..cd928772d 100644 --- a/doc/examples/class_Row.cpp +++ b/doc/examples/class_Row.cpp @@ -6,14 +6,21 @@ template Eigen::Row firstRow(MatrixBase& m) { - return m.row(0); + return Eigen::Row(m.ref(), 0); +} + +template +const Eigen::Row +firstRow(const MatrixBase& m) +{ + return Eigen::Row(m.ref(), 0); } int main(int, char**) { Matrix4d m = Matrix4d::identity(); - cout << firstRow(m) << endl; - firstRow(m) *= 5; + cout << firstRow(2*m) << endl; // calls the const version + firstRow(m) *= 5; // calls the non-const version cout << "Now the matrix m is:" << endl << m << endl; return 0; }