From 05a49547e155b85ff0a84c2e23ede166ca1ee179 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Wed, 26 Dec 2007 08:30:21 +0000 Subject: [PATCH] in ScalarMultiple, make the factor type independent from the matrix scalar type. This is an optimization for complex matrices, allowing to do only a real multiplication when a complex multiplication is not needed, e.g. in normalized(). --- Eigen/Core/Dot.h | 3 ++- Eigen/Core/MathFunctions.h | 17 +++++++++++++++++ Eigen/Core/MatrixBase.h | 2 +- Eigen/Core/ScalarMultiple.h | 38 ++++++++++++++++++------------------- Eigen/Core/Util.h | 2 +- 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Eigen/Core/Dot.h b/Eigen/Core/Dot.h index ff6852af2..32cab5bbb 100644 --- a/Eigen/Core/Dot.h +++ b/Eigen/Core/Dot.h @@ -89,7 +89,8 @@ typename NumTraits::Real MatrixBase::norm() const } template -ScalarMultiple MatrixBase::normalized() const +const ScalarMultiple::Real, Derived> +MatrixBase::normalized() const { return (*this) / norm(); } diff --git a/Eigen/Core/MathFunctions.h b/Eigen/Core/MathFunctions.h index 0647e943f..2cf28e8a7 100644 --- a/Eigen/Core/MathFunctions.h +++ b/Eigen/Core/MathFunctions.h @@ -172,4 +172,21 @@ inline bool isApprox(const std::complex& a, const std::complex& } // isApproxOrLessThan wouldn't make sense for complex numbers +#define EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(T,U) \ +inline std::complex operator*(U a, const std::complex& b) \ +{ \ + return std::complex(static_cast(a)*b.real(), \ + static_cast(a)*b.imag()); \ +} \ +inline std::complex operator*(const std::complex& b, U a) \ +{ \ + return std::complex(static_cast(a)*b.real(), \ + static_cast(a)*b.imag()); \ +} + +EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(int, float) +EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(int, double) +EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(float, double) +EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(double, float) + #endif // EIGEN_MATHFUNCTIONS_H diff --git a/Eigen/Core/MatrixBase.h b/Eigen/Core/MatrixBase.h index 4550deabf..c15167291 100644 --- a/Eigen/Core/MatrixBase.h +++ b/Eigen/Core/MatrixBase.h @@ -149,7 +149,7 @@ template class MatrixBase Scalar dot(const OtherDerived& other) const; RealScalar norm2() const; RealScalar norm() const; - ScalarMultiple normalized() const; + const ScalarMultiple normalized() const; static Eval > random(int rows, int cols); static Eval > random(int size); diff --git a/Eigen/Core/ScalarMultiple.h b/Eigen/Core/ScalarMultiple.h index 95c44fc0f..06c6e295f 100644 --- a/Eigen/Core/ScalarMultiple.h +++ b/Eigen/Core/ScalarMultiple.h @@ -26,19 +26,19 @@ #ifndef EIGEN_SCALARMULTIPLE_H #define EIGEN_SCALARMULTIPLE_H -template class ScalarMultiple : NoOperatorEquals, - public MatrixBase > +template class ScalarMultiple : NoOperatorEquals, + public MatrixBase > { public: typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Ref MatRef; - friend class MatrixBase >; + friend class MatrixBase >; - ScalarMultiple(const MatRef& matrix, Scalar scalar) - : m_matrix(matrix), m_scalar(scalar) {} + ScalarMultiple(const MatRef& matrix, FactorType factor) + : m_matrix(matrix), m_factor(factor) {} ScalarMultiple(const ScalarMultiple& other) - : m_matrix(other.m_matrix), m_scalar(other.m_scalar) {} + : m_matrix(other.m_matrix), m_factor(other.m_factor) {} private: static const int _RowsAtCompileTime = MatrixType::RowsAtCompileTime, @@ -50,35 +50,35 @@ template class ScalarMultiple : NoOperatorEquals, Scalar _coeff(int row, int col) const { - return m_matrix.coeff(row, col) * m_scalar; + return m_factor * m_matrix.coeff(row, col); } protected: const MatRef m_matrix; - const Scalar m_scalar; + const FactorType m_factor; }; -#define EIGEN_MAKE_SCALAR_OPS(OtherScalar) \ +#define EIGEN_MAKE_SCALAR_OPS(FactorType) \ template \ -const ScalarMultiple \ +const ScalarMultiple \ operator*(const MatrixBase& matrix, \ - OtherScalar scalar) \ + FactorType scalar) \ { \ - return ScalarMultiple(matrix.ref(), scalar); \ + return ScalarMultiple(matrix.ref(), scalar); \ } \ \ template \ -const ScalarMultiple \ -operator*(OtherScalar scalar, \ +const ScalarMultiple \ +operator*(FactorType scalar, \ const MatrixBase& matrix) \ { \ - return ScalarMultiple(matrix.ref(), scalar); \ + return ScalarMultiple(matrix.ref(), scalar); \ } \ \ template \ -const ScalarMultiple \ +const ScalarMultiple \ operator/(const MatrixBase& matrix, \ - OtherScalar scalar) \ + FactorType scalar) \ { \ assert(NumTraits::HasFloatingPoint); \ return matrix * (static_cast(1) / scalar); \ @@ -86,14 +86,14 @@ operator/(const MatrixBase& matrix, \ \ template \ Derived & \ -MatrixBase::operator*=(const OtherScalar &other) \ +MatrixBase::operator*=(const FactorType &other) \ { \ return *this = *this * other; \ } \ \ template \ Derived & \ -MatrixBase::operator/=(const OtherScalar &other) \ +MatrixBase::operator/=(const FactorType &other) \ { \ return *this = *this / other; \ } diff --git a/Eigen/Core/Util.h b/Eigen/Core/Util.h index c1576af00..0bccc1f75 100644 --- a/Eigen/Core/Util.h +++ b/Eigen/Core/Util.h @@ -97,7 +97,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;