diff options
author | 2007-12-26 09:25:00 +0000 | |
---|---|---|
committer | 2007-12-26 09:25:00 +0000 | |
commit | 6b9370e0f0608f203337539c72fe4821d925a978 (patch) | |
tree | bae234805f376b78e7022fd603aa2e02f37005cf | |
parent | 05a49547e155b85ff0a84c2e23ede166ca1ee179 (diff) |
more changes in ScalarMultiple, reintroduce FloatingPoint in NumTraits, improve
examples
-rw-r--r-- | Eigen/Core/NumTraits.h | 4 | ||||
-rw-r--r-- | Eigen/Core/ScalarMultiple.h | 6 | ||||
-rw-r--r-- | doc/examples/class_Block.cpp | 15 | ||||
-rw-r--r-- | doc/examples/class_Cast.cpp | 18 | ||||
-rw-r--r-- | doc/examples/class_Column.cpp | 13 | ||||
-rw-r--r-- | doc/examples/class_DynBlock.cpp | 13 | ||||
-rw-r--r-- | 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<typename T> struct NumTraits; template<> struct NumTraits<int> { typedef int Real; + typedef double FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = false; }; @@ -38,6 +39,7 @@ template<> struct NumTraits<int> template<> struct NumTraits<float> { typedef float Real; + typedef float FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = true; }; @@ -45,6 +47,7 @@ template<> struct NumTraits<float> template<> struct NumTraits<double> { typedef double Real; + typedef double FloatingPoint; static const bool IsComplex = false; static const bool HasFloatingPoint = true; }; @@ -52,6 +55,7 @@ template<> struct NumTraits<double> template<typename _Real> struct NumTraits<std::complex<_Real> > { typedef _Real Real; + typedef std::complex<_Real> FloatingPoint; static const bool IsComplex = true; static const bool HasFloatingPoint = NumTraits<Real>::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<typename Scalar, typename Derived> \ -const ScalarMultiple<FactorType, Derived> \ +const ScalarMultiple<typename NumTraits<FactorType>::FloatingPoint, Derived> \ operator/(const MatrixBase<Scalar, Derived>& matrix, \ FactorType scalar) \ { \ assert(NumTraits<Scalar>::HasFloatingPoint); \ - return matrix * (static_cast<Scalar>(1) / scalar); \ + return matrix * (static_cast< \ + typename NumTraits<FactorType>::FloatingPoint \ + >(1) / scalar); \ } \ \ template<typename Scalar, typename Derived> \ 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<Derived, 2, 2> topLeft2x2Corner(MatrixBase<Scalar, Derived>& m) { return Eigen::Block<Derived, 2, 2>(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<typename Scalar, typename Derived> +const Eigen::Block<Derived, 2, 2> +topLeft2x2Corner(const MatrixBase<Scalar, Derived>& m) +{ + return Eigen::Block<Derived, 2, 2>(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<typename Scalar, typename Derived> -Eigen::Cast<double, Derived> -castToDouble(const MatrixBase<Scalar, Derived>& m) +const Eigen::Cast< + typename Eigen::NumTraits<Scalar>::FloatingPoint, + Derived +> +castToFloatingPoint(const MatrixBase<Scalar, Derived>& m) { - return Eigen::Cast<double, Derived>(m.ref()); - // note: tempting as it is, writing "m.cast<double>()" 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<Scalar>::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<typename Scalar, typename Derived> Eigen::Column<Derived> firstColumn(MatrixBase<Scalar, Derived>& m) { - return m.col(0); + return Eigen::Column<Derived>(m.ref(), 0); +} + +template<typename Scalar, typename Derived> +const Eigen::Column<Derived> +firstColumn(const MatrixBase<Scalar, Derived>& m) +{ + return Eigen::Column<Derived>(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<typename Scalar, typename Derived> Eigen::DynBlock<Derived> topLeftCorner(MatrixBase<Scalar, Derived>& m, int rows, int cols) { - return m.dynBlock(0, 0, rows, cols); + return Eigen::DynBlock<Derived>(m.ref(), 0, 0, rows, cols); +} + +template<typename Scalar, typename Derived> +const Eigen::DynBlock<Derived> +topLeftCorner(const MatrixBase<Scalar, Derived>& m, int rows, int cols) +{ + return Eigen::DynBlock<Derived>(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<typename Scalar, typename Derived> Eigen::Row<Derived> firstRow(MatrixBase<Scalar, Derived>& m) { - return m.row(0); + return Eigen::Row<Derived>(m.ref(), 0); +} + +template<typename Scalar, typename Derived> +const Eigen::Row<Derived> +firstRow(const MatrixBase<Scalar, Derived>& m) +{ + return Eigen::Row<Derived>(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; } |