diff --git a/Eigen/src/Core/Functors.h b/Eigen/src/Core/Functors.h index dfe19a07f..38e1b57dc 100644 --- a/Eigen/src/Core/Functors.h +++ b/Eigen/src/Core/Functors.h @@ -813,6 +813,20 @@ template struct functor_traits > { enum { Cost = 5 * NumTraits::MulCost, PacketAccess = false }; }; +/** \internal + * \brief Template functor to compute the quotient between a scalar and array entries. + * \sa class CwiseUnaryOp, Cwise::inverse() + */ +template +struct scalar_inverse_mult_op { + scalar_inverse_mult_op(const Scalar& other) : m_other(other) {} + inline Scalar operator() (const Scalar& a) const { return m_other / a; } + template + inline const Packet packetOp(const Packet& a) const + { return internal::pdiv(pset1(m_other),a); } + Scalar m_other; +}; + /** \internal * \brief Template functor to compute the inverse of a scalar * \sa class CwiseUnaryOp, Cwise::inverse() diff --git a/Eigen/src/Core/GlobalFunctions.h b/Eigen/src/Core/GlobalFunctions.h index 144145a95..fba675ffc 100644 --- a/Eigen/src/Core/GlobalFunctions.h +++ b/Eigen/src/Core/GlobalFunctions.h @@ -71,6 +71,19 @@ namespace std } } +/** +* \brief Component-wise division of a scalar by array elements. +**/ +template +inline const Eigen::CwiseUnaryOp, const Derived> + operator/(typename Derived::Scalar s, const Eigen::ArrayBase& a) +{ + return Eigen::CwiseUnaryOp, const Derived>( + a.derived(), + Eigen::internal::scalar_inverse_mult_op(s) + ); +} + namespace Eigen { namespace internal diff --git a/test/array.cpp b/test/array.cpp index 662368c62..481862266 100644 --- a/test/array.cpp +++ b/test/array.cpp @@ -43,7 +43,10 @@ template void array(const ArrayType& m) RowVectorType rv1 = RowVectorType::Random(cols); Scalar s1 = internal::random(), - s2 = internal::random(); + s2 = internal::random(); + + // scalar by array division + VERIFY_IS_APPROX(s1/m1, s1 * m1.inverse()); // scalar addition VERIFY_IS_APPROX(m1 + s1, s1 + m1);