diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2007-12-26 08:30:21 +0000 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2007-12-26 08:30:21 +0000 |
commit | 05a49547e155b85ff0a84c2e23ede166ca1ee179 (patch) | |
tree | 6e8d457bd2031debe304675bb183cc311e2f8f60 | |
parent | dad245af56b4597f27d7c219e5f5f0b38cd62d24 (diff) |
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().
-rw-r--r-- | Eigen/Core/Dot.h | 3 | ||||
-rw-r--r-- | Eigen/Core/MathFunctions.h | 17 | ||||
-rw-r--r-- | Eigen/Core/MatrixBase.h | 2 | ||||
-rw-r--r-- | Eigen/Core/ScalarMultiple.h | 38 | ||||
-rw-r--r-- | 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<Scalar>::Real MatrixBase<Scalar, Derived>::norm() const } template<typename Scalar, typename Derived> -ScalarMultiple<Derived> MatrixBase<Scalar, Derived>::normalized() const +const ScalarMultiple<typename NumTraits<Scalar>::Real, Derived> +MatrixBase<Scalar, Derived>::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<double>& a, const std::complex<double>& } // isApproxOrLessThan wouldn't make sense for complex numbers +#define EIGEN_MAKE_MORE_OVERLOADED_COMPLEX_OPERATOR_STAR(T,U) \ +inline std::complex<T> operator*(U a, const std::complex<T>& b) \ +{ \ + return std::complex<T>(static_cast<T>(a)*b.real(), \ + static_cast<T>(a)*b.imag()); \ +} \ +inline std::complex<T> operator*(const std::complex<T>& b, U a) \ +{ \ + return std::complex<T>(static_cast<T>(a)*b.real(), \ + static_cast<T>(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<typename Scalar, typename Derived> class MatrixBase Scalar dot(const OtherDerived& other) const; RealScalar norm2() const; RealScalar norm() const; - ScalarMultiple<Derived> normalized() const; + const ScalarMultiple<RealScalar, Derived> normalized() const; static Eval<Random<Derived> > random(int rows, int cols); static Eval<Random<Derived> > 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<typename MatrixType> class ScalarMultiple : NoOperatorEquals, - public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<MatrixType> > +template<typename FactorType, typename MatrixType> class ScalarMultiple : NoOperatorEquals, + public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<FactorType, MatrixType> > { public: typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Ref MatRef; - friend class MatrixBase<typename MatrixType::Scalar, ScalarMultiple<MatrixType> >; + friend class MatrixBase<Scalar, ScalarMultiple<FactorType, MatrixType> >; - 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<typename MatrixType> 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<typename Scalar, typename Derived> \ -const ScalarMultiple<Derived> \ +const ScalarMultiple<FactorType, Derived> \ operator*(const MatrixBase<Scalar, Derived>& matrix, \ - OtherScalar scalar) \ + FactorType scalar) \ { \ - return ScalarMultiple<Derived>(matrix.ref(), scalar); \ + return ScalarMultiple<FactorType, Derived>(matrix.ref(), scalar); \ } \ \ template<typename Scalar, typename Derived> \ -const ScalarMultiple<Derived> \ -operator*(OtherScalar scalar, \ +const ScalarMultiple<FactorType, Derived> \ +operator*(FactorType scalar, \ const MatrixBase<Scalar, Derived>& matrix) \ { \ - return ScalarMultiple<Derived>(matrix.ref(), scalar); \ + return ScalarMultiple<FactorType, Derived>(matrix.ref(), scalar); \ } \ \ template<typename Scalar, typename Derived> \ -const ScalarMultiple<Derived> \ +const ScalarMultiple<FactorType, Derived> \ operator/(const MatrixBase<Scalar, Derived>& matrix, \ - OtherScalar scalar) \ + FactorType scalar) \ { \ assert(NumTraits<Scalar>::HasFloatingPoint); \ return matrix * (static_cast<Scalar>(1) / scalar); \ @@ -86,14 +86,14 @@ operator/(const MatrixBase<Scalar, Derived>& matrix, \ \ template<typename Scalar, typename Derived> \ Derived & \ -MatrixBase<Scalar, Derived>::operator*=(const OtherScalar &other) \ +MatrixBase<Scalar, Derived>::operator*=(const FactorType &other) \ { \ return *this = *this * other; \ } \ \ template<typename Scalar, typename Derived> \ Derived & \ -MatrixBase<Scalar, Derived>::operator/=(const OtherScalar &other) \ +MatrixBase<Scalar, Derived>::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<typename MatrixType> class Opposite; template<typename Lhs, typename Rhs> class Sum; template<typename Lhs, typename Rhs> class Difference; template<typename Lhs, typename Rhs> class Product; -template<typename MatrixType> class ScalarMultiple; +template<typename FactorType, typename MatrixType> class ScalarMultiple; template<typename MatrixType> class Random; template<typename MatrixType> class Zero; template<typename MatrixType> class Ones; |