From b3268a6e2fd985bd1abfda0f19f266eaed1c018a Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Fri, 29 Feb 2008 13:20:44 +0000 Subject: -merge patch from Gael Guennebaud adding NumTraits for long long and long double. -define scalar-multiple operators only for the current Scalar type; thanks to Gael for expaining how to make the compiler understand when automatic casting is needed. -take ScalarMultiple take only 1 template param, again. We lose some flexibility especially when dealing with complex numbers, but we gain a lot of extensibility to new scalar types. --- Eigen/src/Core/Dot.h | 2 +- Eigen/src/Core/ForwardDeclarations.h | 2 +- Eigen/src/Core/MatrixBase.h | 23 +++++---- Eigen/src/Core/MatrixStorage.h | 41 ++++++++-------- Eigen/src/Core/NumTraits.h | 20 ++++++++ Eigen/src/Core/ScalarMultiple.h | 92 ++++++++++++++---------------------- 6 files changed, 89 insertions(+), 91 deletions(-) (limited to 'Eigen') diff --git a/Eigen/src/Core/Dot.h b/Eigen/src/Core/Dot.h index f5862ae93..b4a24506f 100644 --- a/Eigen/src/Core/Dot.h +++ b/Eigen/src/Core/Dot.h @@ -121,7 +121,7 @@ typename NumTraits::Real MatrixBase::norm() const * \sa norm() */ template -const ScalarMultiple::Real, Derived> +const ScalarMultiple MatrixBase::normalized() const { return (*this) / norm(); diff --git a/Eigen/src/Core/ForwardDeclarations.h b/Eigen/src/Core/ForwardDeclarations.h index cb9c4eaf1..fe75ccefa 100644 --- a/Eigen/src/Core/ForwardDeclarations.h +++ b/Eigen/src/Core/ForwardDeclarations.h @@ -39,7 +39,7 @@ template class Opposite; template class Sum; template class Difference; template class Product; -template class ScalarMultiple; +template class ScalarMultiple; template class Random; template class Zero; template class Ones; diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index b550c63d7..74891bd18 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -215,7 +215,7 @@ template class MatrixBase Scalar dot(const OtherDerived& other) const; RealScalar norm2() const; RealScalar norm() const; - const ScalarMultiple normalized() const; + const ScalarMultiple normalized() const; template bool isOrtho(const OtherDerived& other, RealScalar prec = precision()) const; bool isOrtho(RealScalar prec = precision()) const; @@ -269,17 +269,16 @@ template class MatrixBase template Derived& operator*=(const MatrixBase& other); - Derived& operator*=(const int& other); - Derived& operator*=(const float& other); - Derived& operator*=(const double& other); - Derived& operator*=(const std::complex& other); - Derived& operator*=(const std::complex& other); - - Derived& operator/=(const int& other); - Derived& operator/=(const float& other); - Derived& operator/=(const double& other); - Derived& operator/=(const std::complex& other); - Derived& operator/=(const std::complex& other); + Derived& operator*=(const Scalar& other); + Derived& operator/=(const Scalar& other); + + const ScalarMultiple operator*(const Scalar& scalar) const; + const ScalarMultiple operator/(const Scalar& scalar) const; + + friend + const ScalarMultiple operator*(const Scalar& scalar, + const MatrixBase& matrix) + { return matrix*scalar; } Scalar coeff(int row, int col) const; Scalar operator()(int row, int col) const; diff --git a/Eigen/src/Core/MatrixStorage.h b/Eigen/src/Core/MatrixStorage.h index 7d32273b4..e441f0a77 100644 --- a/Eigen/src/Core/MatrixStorage.h +++ b/Eigen/src/Core/MatrixStorage.h @@ -61,14 +61,14 @@ template class MatrixStorage int m_rows; int m_cols; public: - MatrixStorage(int, int nbRows, int nbCols) : m_rows(nbRows), m_cols(nbCols) {} + MatrixStorage(int, int rows, int cols) : m_rows(rows), m_cols(cols) {} ~MatrixStorage() {} int rows(void) const {return m_rows;} int cols(void) const {return m_cols;} - void resize(int, int nbRows, int nbCols) + void resize(int, int rows, int cols) { - m_rows = nbRows; - m_cols = nbCols; + m_rows = rows; + m_cols = cols; } const T *data() const { return m_data; } T *data() { return m_data; } @@ -80,13 +80,13 @@ template class MatrixStorage class MatrixStorage class MatrixStorage int m_rows; int m_cols; public: - MatrixStorage(int size, int nbRows, int nbCols) : m_data(new T[size]), m_rows(nbRows), m_cols(nbCols) {} + MatrixStorage(int size, int rows, int cols) + : m_data(new T[size]), m_rows(rows), m_cols(cols) {} ~MatrixStorage() { delete[] m_data; } int rows(void) const {return m_rows;} int cols(void) const {return m_cols;} - void resize(int size, int nbRows, int nbCols) + void resize(int size, int rows, int cols) { if(size != m_rows*m_cols) { delete[] m_data; m_data = new T[size]; } - m_rows = nbRows; - m_cols = nbCols; + m_rows = rows; + m_cols = cols; } const T *data() const { return m_data; } T *data() { return m_data; } @@ -141,18 +142,18 @@ template class MatrixStorage T *m_data; int m_cols; public: - MatrixStorage(int size, int, int nbCols) : m_data(new T[size]), m_cols(nbCols) {} + MatrixStorage(int size, int, int cols) : m_data(new T[size]), m_cols(cols) {} ~MatrixStorage() { delete[] m_data; } static int rows(void) {return _Rows;} int cols(void) const {return m_cols;} - void resize(int size, int, int nbCols) + void resize(int size, int, int cols) { if(size != _Rows*m_cols) { delete[] m_data; m_data = new T[size]; } - m_cols = nbCols; + m_cols = cols; } const T *data() const { return m_data; } T *data() { return m_data; } @@ -164,18 +165,18 @@ template class MatrixStorage T *m_data; int m_rows; public: - MatrixStorage(int size, int nbRows, int) : m_data(new T[size]), m_rows(nbRows) {} + MatrixStorage(int size, int rows, int) : m_data(new T[size]), m_rows(rows) {} ~MatrixStorage() { delete[] m_data; } int rows(void) const {return m_rows;} static int cols(void) {return _Cols;} - void resize(int size, int nbRows, int) + void resize(int size, int rows, int) { if(size != m_rows*_Cols) { delete[] m_data; m_data = new T[size]; } - m_rows = nbRows; + m_rows = rows; } const T *data() const { return m_data; } T *data() { return m_data; } diff --git a/Eigen/src/Core/NumTraits.h b/Eigen/src/Core/NumTraits.h index 10a378d83..cc541af8f 100644 --- a/Eigen/src/Core/NumTraits.h +++ b/Eigen/src/Core/NumTraits.h @@ -87,4 +87,24 @@ template struct NumTraits > }; }; +template<> struct NumTraits +{ + typedef long long int Real; + typedef long double FloatingPoint; + enum { + IsComplex = 0, + HasFloatingPoint = 0 + }; +}; + +template<> struct NumTraits +{ + typedef long double Real; + typedef long double FloatingPoint; + enum { + IsComplex = 0, + HasFloatingPoint = 1 + }; +}; + #endif // EIGEN_NUMTRAITS_H diff --git a/Eigen/src/Core/ScalarMultiple.h b/Eigen/src/Core/ScalarMultiple.h index aaa0a429a..5ea664994 100644 --- a/Eigen/src/Core/ScalarMultiple.h +++ b/Eigen/src/Core/ScalarMultiple.h @@ -36,8 +36,8 @@ * It is the return type of the operator* between a matrix or vector and a scalar, and most * of the time this is the only way it is used. */ -template class ScalarMultiple : NoOperatorEquals, - public MatrixBase > +template class ScalarMultiple : NoOperatorEquals, + public MatrixBase > { public: typedef typename MatrixType::Scalar Scalar; @@ -45,7 +45,7 @@ template class ScalarMultiple : NoOper friend class MatrixBase; typedef MatrixBase Base; - ScalarMultiple(const MatRef& matrix, FactorType factor) + ScalarMultiple(const MatRef& matrix, Scalar factor) : m_matrix(matrix), m_factor(factor) {} private: @@ -67,62 +67,40 @@ template class ScalarMultiple : NoOper protected: const MatRef m_matrix; - const FactorType m_factor; + const Scalar m_factor; }; -#define EIGEN_MAKE_SCALAR_OPS(FactorType) \ -/** \relates MatrixBase \sa class ScalarMultiple */ \ -template \ -const ScalarMultiple \ -operator*(const MatrixBase& matrix, \ - FactorType scalar) \ -{ \ - return ScalarMultiple(matrix.ref(), scalar); \ -} \ - \ -/** \relates MatrixBase \sa class ScalarMultiple */ \ -template \ -const ScalarMultiple \ -operator*(FactorType scalar, \ - const MatrixBase& matrix) \ -{ \ - return ScalarMultiple(matrix.ref(), scalar); \ -} \ - \ -/** \relates MatrixBase \sa class ScalarMultiple */ \ -template \ -const ScalarMultiple::FloatingPoint, Derived> \ -operator/(const MatrixBase& matrix, \ - FactorType scalar) \ -{ \ - assert(NumTraits::HasFloatingPoint); \ - return matrix * (static_cast< \ - typename NumTraits::FloatingPoint \ - >(1) / scalar); \ -} \ - \ -/** \sa class ScalarMultiple */ \ -template \ -Derived & \ -MatrixBase::operator*=(const FactorType &other) \ -{ \ - return *this = *this * other; \ -} \ - \ -/** \sa class ScalarMultiple */ \ -template \ -Derived & \ -MatrixBase::operator/=(const FactorType &other) \ -{ \ - return *this = *this / other; \ +/** relates MatrixBase sa class ScalarMultiple */ +template +const ScalarMultiple +MatrixBase::operator*(const Scalar& scalar) const +{ + return ScalarMultiple(ref(), scalar); +} + +/** \relates MatrixBase \sa class ScalarMultiple */ +template +const ScalarMultiple +MatrixBase::operator/(const Scalar& scalar) const +{ + assert(NumTraits::HasFloatingPoint); + return ScalarMultiple(ref(), static_cast(1) / scalar); +} + +/** \sa ScalarMultiple */ +template +Derived& +MatrixBase::operator*=(const Scalar& other) +{ + return *this = *this * other; +} + +/** \sa ScalarMultiple */ +template +Derived& +MatrixBase::operator/=(const Scalar& other) +{ + return *this = *this / other; } - -EIGEN_MAKE_SCALAR_OPS(int) -EIGEN_MAKE_SCALAR_OPS(float) -EIGEN_MAKE_SCALAR_OPS(double) -EIGEN_MAKE_SCALAR_OPS(std::complex) -EIGEN_MAKE_SCALAR_OPS(std::complex) - -#undef EIGEN_MAKE_SCALAR_OPS #endif // EIGEN_SCALARMULTIPLE_H -- cgit v1.2.3