diff options
author | Gael Guennebaud <g.gael@free.fr> | 2013-04-09 11:12:35 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2013-04-09 11:12:35 +0200 |
commit | 3cb6e21f80150345d8d8eeaf75b185e66a4a3c95 (patch) | |
tree | 3ccc58c1ce23e905aa48e5e1bea896fc7d89e6ed /Eigen/src/Core/VectorwiseOp.h | |
parent | d8f103535522dcc98c4e328e27ffea1e8450e526 (diff) |
Fix bug #562: add vector-wise normalized and normalize functions
Diffstat (limited to 'Eigen/src/Core/VectorwiseOp.h')
-rw-r--r-- | Eigen/src/Core/VectorwiseOp.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Eigen/src/Core/VectorwiseOp.h b/Eigen/src/Core/VectorwiseOp.h index f48ec884c..4549641dd 100644 --- a/Eigen/src/Core/VectorwiseOp.h +++ b/Eigen/src/Core/VectorwiseOp.h @@ -233,6 +233,28 @@ template<typename ExpressionType, int Direction> class VectorwiseOp Direction==Vertical ? 1 : m_matrix.rows(), Direction==Horizontal ? 1 : m_matrix.cols()); } + + template<typename OtherDerived> struct OppositeExtendedType { + typedef Replicate<OtherDerived, + Direction==Horizontal ? 1 : ExpressionType::RowsAtCompileTime, + Direction==Vertical ? 1 : ExpressionType::ColsAtCompileTime> Type; + }; + + /** \internal + * Replicates a vector in the opposite direction to match the size of \c *this */ + template<typename OtherDerived> + typename OppositeExtendedType<OtherDerived>::Type + extendedToOpposite(const DenseBase<OtherDerived>& other) const + { + EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Horizontal, OtherDerived::MaxColsAtCompileTime==1), + YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED) + EIGEN_STATIC_ASSERT(EIGEN_IMPLIES(Direction==Vertical, OtherDerived::MaxRowsAtCompileTime==1), + YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED) + return typename OppositeExtendedType<OtherDerived>::Type + (other.derived(), + Direction==Horizontal ? 1 : m_matrix.rows(), + Direction==Vertical ? 1 : m_matrix.cols()); + } public: @@ -504,6 +526,23 @@ template<typename ExpressionType, int Direction> class VectorwiseOp EIGEN_STATIC_ASSERT_SAME_XPR_KIND(ExpressionType, OtherDerived) return m_matrix / extendedTo(other.derived()); } + + /** \returns an expression where each column of row of the referenced matrix are normalized. + * The referenced matrix is \b not modified. + * \sa MatrixBase::normalized(), normalize() + */ + CwiseBinaryOp<internal::scalar_quotient_op<Scalar>, + const ExpressionTypeNestedCleaned, + const typename OppositeExtendedType<typename ReturnType<internal::member_norm,RealScalar>::Type>::Type> + normalized() const { return m_matrix.cwiseQuotient(extendedToOpposite(this->norm())); } + + + /** Normalize in-place each row or columns of the referenced matrix. + * \sa MatrixBase::normalize(), normalized() + */ + void normalize() { + m_matrix = this->normalized(); + } /////////// Geometry module /////////// |