the big Array/Cwise rework as discussed on the mailing list. The new API

can be seen in Eigen/src/Core/Cwise.h.
This commit is contained in:
Benoit Jacob
2008-07-08 00:49:10 +00:00
parent c910c517b3
commit f5791eeb70
26 changed files with 465 additions and 389 deletions

View File

@@ -1,152 +0,0 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.
#ifndef EIGEN_ARRAY_H
#define EIGEN_ARRAY_H
/** \class Array
*
* \brief Pseudo expression offering additional features to an expression
*
* \param ExpressionType the type of the object of which we want array related features
*
* This class represents an expression with additional, array related, features.
* It is the return type of MatrixBase::array()
* and most of the time this is the only way it is used.
*
* \sa MatrixBase::array()
*/
template<typename ExpressionType> class Array
{
public:
typedef typename ei_traits<ExpressionType>::Scalar Scalar;
typedef typename ei_meta_if<ei_must_nest_by_value<ExpressionType>::ret,
ExpressionType, const ExpressionType&>::ret ExpressionTypeNested;
// typedef NestByValue<typename ExpressionType::ConstantReturnType> ConstantReturnType;
typedef CwiseUnaryOp<ei_scalar_add_op<Scalar>, ExpressionType> ScalarAddReturnType;
inline Array(const ExpressionType& matrix) : m_matrix(matrix) {}
/** \internal */
inline const ExpressionType& _expression() const { return m_matrix; }
const ScalarAddReturnType
operator+(const Scalar& scalar) const;
/** \relates Array */
friend const ScalarAddReturnType
operator+(const Scalar& scalar, const Array& mat)
{ return mat + scalar; }
ExpressionType& operator+=(const Scalar& scalar);
const ScalarAddReturnType
operator-(const Scalar& scalar) const;
ExpressionType& operator-=(const Scalar& scalar);
/** \returns true if each coeff of \c *this is less than its respective coeff of \a other */
template<typename OtherDerived> bool operator<(const Array<OtherDerived>& other) const
{ return m_matrix.cwiseLessThan(other._expression()).all(); }
/** \returns true if each coeff of \c *this is less or equal to its respective coeff of \a other */
template<typename OtherDerived> bool operator<=(const Array<OtherDerived>& other) const
{ return m_matrix.cwiseLessEqual(other._expression()).all(); }
/** \returns true if each coeff of \c *this is greater to its respective coeff of \a other */
template<typename OtherDerived>
bool operator>(const Array<OtherDerived>& other) const
{ return m_matrix.cwiseGreaterThan(other._expression()).all(); }
/** \returns true if each coeff of \c *this is greater or equal to its respective coeff of \a other */
template<typename OtherDerived>
bool operator>=(const Array<OtherDerived>& other) const
{ return m_matrix.cwiseGreaterEqual(other._expression()).all(); }
protected:
ExpressionTypeNested m_matrix;
};
/** \returns an expression of \c *this with each coeff incremented by the constant \a scalar */
template<typename ExpressionType>
const typename Array<ExpressionType>::ScalarAddReturnType
Array<ExpressionType>::operator+(const Scalar& scalar) const
{
return CwiseUnaryOp<ei_scalar_add_op<Scalar>, ExpressionType>(m_matrix, ei_scalar_add_op<Scalar>(scalar));
}
/** \see operator+ */
template<typename ExpressionType>
ExpressionType& Array<ExpressionType>::operator+=(const Scalar& scalar)
{
m_matrix.const_cast_derived() = *this + scalar;
return m_matrix.const_cast_derived();
}
/** \returns an expression of \c *this with each coeff decremented by the constant \a scalar */
template<typename ExpressionType>
const typename Array<ExpressionType>::ScalarAddReturnType
Array<ExpressionType>::operator-(const Scalar& scalar) const
{
return *this + (-scalar);
}
/** \see operator- */
template<typename ExpressionType>
ExpressionType& Array<ExpressionType>::operator-=(const Scalar& scalar)
{
m_matrix.const_cast_derived() = *this - scalar;
return m_matrix.const_cast_derived();
}
/** \array_module
*
* \returns an Array expression of *this providing additional,
* array related, features.
*
* \sa class Array
*/
template<typename Derived>
inline const Array<Derived>
MatrixBase<Derived>::array() const
{
return derived();
}
/** \array_module
*
* \returns an Array expression of *this providing additional,
* array related, features.
*
* \sa class Array
*/
template<typename Derived>
inline Array<Derived>
MatrixBase<Derived>::array()
{
return derived();
}
#endif // EIGEN_FLAGGED_H

View File

@@ -30,74 +30,97 @@
/** \array_module
*
* \returns an expression of the coefficient-wise square root of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_sqrt_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseSqrt() const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_sqrt_op>::ReturnType
Cwise<ExpressionType>::sqrt() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise exponential of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_exp_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseExp() const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_exp_op>::ReturnType
Cwise<ExpressionType>::exp() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise logarithm of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_log_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseLog() const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_log_op>::ReturnType
Cwise<ExpressionType>::log() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise cosine of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_cos_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseCos() const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_cos_op>::ReturnType
Cwise<ExpressionType>::cos() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise sine of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_sin_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseSin() const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_sin_op>::ReturnType
Cwise<ExpressionType>::sin() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise power of *this to the given exponent. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_pow_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwisePow(const Scalar& exponent) const
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_pow_op>::ReturnType
Cwise<ExpressionType>::pow(const Scalar& exponent) const
{
return CwiseUnaryOp<ei_scalar_pow_op<Scalar>, Derived>
(derived(), ei_scalar_pow_op<Scalar>(exponent));
return typename UnOp<ei_scalar_pow_op>::ReturnType(_expression(), ei_scalar_pow_op<Scalar>(exponent));
}
/** \array_module
*
* \returns an expression of the coefficient-wise reciprocal of *this. */
template<typename Derived>
inline const CwiseUnaryOp<ei_scalar_inverse_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::cwiseInverse() const
* \returns an expression of the coefficient-wise inverse of *this. */
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_inverse_op>::ReturnType
Cwise<ExpressionType>::inverse() const
{
return derived();
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise square of *this. */
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_square_op>::ReturnType
Cwise<ExpressionType>::square() const
{
return _expression();
}
/** \array_module
*
* \returns an expression of the coefficient-wise cube of *this. */
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::template UnOp<ei_scalar_cube_op>::ReturnType
Cwise<ExpressionType>::cube() const
{
return _expression();
}
// -- binary operators --
/** \array_module
@@ -106,12 +129,12 @@ MatrixBase<Derived>::cwiseInverse() const
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::less<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseLessThan(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::less, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator<(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::less<Scalar>());
return typename BinOp<std::less, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \array_module
@@ -120,12 +143,12 @@ MatrixBase<Derived>::cwiseLessThan(const MatrixBase<OtherDerived> &other) const
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::less_equal<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseLessEqual(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::less_equal, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator<=(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::less_equal<Scalar>());
return typename BinOp<std::less_equal, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \array_module
@@ -134,12 +157,12 @@ MatrixBase<Derived>::cwiseLessEqual(const MatrixBase<OtherDerived> &other) const
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::greater<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseGreaterThan(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::greater, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator>(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::greater<Scalar>());
return typename BinOp<std::greater, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \array_module
@@ -148,12 +171,12 @@ MatrixBase<Derived>::cwiseGreaterThan(const MatrixBase<OtherDerived> &other) con
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::greater_equal<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseGreaterEqual(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::greater_equal, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator>=(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::greater_equal<Scalar>());
return typename BinOp<std::greater_equal, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \array_module
@@ -162,12 +185,12 @@ MatrixBase<Derived>::cwiseGreaterEqual(const MatrixBase<OtherDerived> &other) co
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::equal_to<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseEqualTo(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::equal_to, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator==(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::equal_to<Scalar>());
return typename BinOp<std::equal_to, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \array_module
@@ -176,12 +199,43 @@ MatrixBase<Derived>::cwiseEqualTo(const MatrixBase<OtherDerived> &other) const
*
* \sa class CwiseBinaryOp
*/
template<typename Derived>
template<typename ExpressionType>
template<typename OtherDerived>
inline const CwiseBinaryOp<std::not_equal_to<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
MatrixBase<Derived>::cwiseNotEqualTo(const MatrixBase<OtherDerived> &other) const
inline const typename Cwise<ExpressionType>::template BinOp<std::not_equal_to, OtherDerived>::ReturnType
Cwise<ExpressionType>::operator!=(const MatrixBase<OtherDerived> &other) const
{
return cwise(other, std::not_equal_to<Scalar>());
return typename BinOp<std::not_equal_to, OtherDerived>::ReturnType(_expression(), other.derived());
}
/** \returns an expression of \c *this with each coeff incremented by the constant \a scalar */
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::ScalarAddReturnType
Cwise<ExpressionType>::operator+(const Scalar& scalar) const
{
return typename Cwise<ExpressionType>::ScalarAddReturnType(m_matrix, ei_scalar_add_op<Scalar>(scalar));
}
/** \see operator+ */
template<typename ExpressionType>
inline ExpressionType& Cwise<ExpressionType>::operator+=(const Scalar& scalar)
{
return m_matrix.const_cast_derived() = *this + scalar;
}
/** \returns an expression of \c *this with each coeff decremented by the constant \a scalar */
template<typename ExpressionType>
inline const typename Cwise<ExpressionType>::ScalarAddReturnType
Cwise<ExpressionType>::operator-(const Scalar& scalar) const
{
return *this + (-scalar);
}
/** \see operator- */
template<typename ExpressionType>
inline ExpressionType& Cwise<ExpressionType>::operator-=(const Scalar& scalar)
{
return m_matrix.const_cast_derived() = *this - scalar;
}
#endif // EIGEN_ARRAY_CWISE_OPERATORS_H

View File

@@ -25,15 +25,6 @@
#ifndef EIGEN_ARRAY_FUNCTORS_H
#define EIGEN_ARRAY_FUNCTORS_H
/** \internal
* \array_module
*
* \brief Template functor to add a scalar to a fixed other one
*
* \sa class CwiseUnaryOp, Array::operator+
*/
template<typename Scalar, bool PacketAccess = (int(ei_packet_traits<Scalar>::size)>1?true:false) > struct ei_scalar_add_op;
template<typename Scalar>
struct ei_scalar_add_op<Scalar,true> {
typedef typename ei_packet_traits<Scalar>::type PacketScalar;
@@ -150,17 +141,59 @@ struct ei_functor_traits<ei_scalar_pow_op<Scalar> >
*
* \array_module
*
* \brief Template functor to compute the reciprocal of a scalar
* \brief Template functor to compute the inverse of a scalar
*
* \sa class CwiseUnaryOp, MatrixBase::cwiseInverse
* \sa class CwiseUnaryOp, Cwise::inverse()
*/
template<typename Scalar>
struct ei_scalar_inverse_op {
inline Scalar operator() (const Scalar& a) const { return Scalar(1)/a; }
template<typename PacketScalar>
inline const PacketScalar packetOp(const PacketScalar& a) const
{ return ei_div(ei_pset1(Scalar(1)),a); }
};
template<typename Scalar>
struct ei_functor_traits<ei_scalar_inverse_op<Scalar> >
{ enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = false }; };
{ enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = int(ei_packet_traits<Scalar>::size)>1 }; };
/** \internal
*
* \array_module
*
* \brief Template functor to compute the square of a scalar
*
* \sa class CwiseUnaryOp, Cwise::square()
*/
template<typename Scalar>
struct ei_scalar_square_op {
inline Scalar operator() (const Scalar& a) const { return a*a; }
template<typename PacketScalar>
inline const PacketScalar packetOp(const PacketScalar& a) const
{ return ei_pmul(a,a); }
};
template<typename Scalar>
struct ei_functor_traits<ei_scalar_square_op<Scalar> >
{ enum { Cost = NumTraits<Scalar>::MulCost, PacketAccess = int(ei_packet_traits<Scalar>::size)>1 }; };
/** \internal
*
* \array_module
*
* \brief Template functor to compute the cube of a scalar
*
* \sa class CwiseUnaryOp, Cwise::cube()
*/
template<typename Scalar>
struct ei_scalar_cube_op {
inline Scalar operator() (const Scalar& a) const { return a*a*a; }
template<typename PacketScalar>
inline const PacketScalar packetOp(const PacketScalar& a) const
{ return ei_pmul(a,ei_pmul(a,a)); }
};
template<typename Scalar>
struct ei_functor_traits<ei_scalar_cube_op<Scalar> >
{ enum { Cost = 2*NumTraits<Scalar>::MulCost, PacketAccess = int(ei_packet_traits<Scalar>::size)>1 }; };
// default ei_functor_traits for STL functors:

View File

@@ -53,7 +53,7 @@ template<typename Derived>
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::random(int rows, int cols)
{
return create(rows, cols, ei_scalar_random_op<Scalar>());
return NullaryExpr(rows, cols, ei_scalar_random_op<Scalar>());
}
/** \array_module
@@ -78,7 +78,7 @@ template<typename Derived>
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::random(int size)
{
return create(size, ei_scalar_random_op<Scalar>());
return NullaryExpr(size, ei_scalar_random_op<Scalar>());
}
/** \array_module
@@ -98,7 +98,7 @@ template<typename Derived>
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
MatrixBase<Derived>::random()
{
return create(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_random_op<Scalar>());
return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_random_op<Scalar>());
}
/** \array_module