2008-02-29 14:35:14 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-02-29 14:35:14 +00:00
|
|
|
//
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-11-24 13:40:43 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2008-02-29 14:35:14 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-02-29 14:35:14 +00:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_CWISE_BINARY_OP_H
|
|
|
|
|
#define EIGEN_CWISE_BINARY_OP_H
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2008-02-29 14:35:14 +00:00
|
|
|
/** \class CwiseBinaryOp
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2008-02-29 14:35:14 +00:00
|
|
|
*
|
2010-04-18 22:14:55 -04:00
|
|
|
* \brief Generic expression where a coefficient-wise binary operator is applied to two expressions
|
2008-02-29 14:35:14 +00:00
|
|
|
*
|
|
|
|
|
* \param BinaryOp template functor implementing the operator
|
|
|
|
|
* \param Lhs the type of the left-hand side
|
|
|
|
|
* \param Rhs the type of the right-hand side
|
|
|
|
|
*
|
2010-04-18 22:14:55 -04:00
|
|
|
* This class represents an expression where a coefficient-wise binary operator is applied to two expressions.
|
|
|
|
|
* It is the return type of binary operators, by which we mean only those binary operators where
|
|
|
|
|
* both the left-hand side and the right-hand side are Eigen expressions.
|
|
|
|
|
* For example, the return type of matrix1+matrix2 is a CwiseBinaryOp.
|
2008-02-29 14:35:14 +00:00
|
|
|
*
|
2010-04-18 22:14:55 -04:00
|
|
|
* Most of the time, this is the only way that it is used, so you typically don't have to name
|
|
|
|
|
* CwiseBinaryOp types explicitly.
|
2008-03-03 10:52:44 +00:00
|
|
|
*
|
2008-07-08 00:49:10 +00:00
|
|
|
* \sa MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const, class CwiseUnaryOp, class CwiseNullaryOp
|
2008-02-29 14:35:14 +00:00
|
|
|
*/
|
2010-10-25 10:15:22 -04:00
|
|
|
|
|
|
|
|
namespace internal {
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename BinaryOp, typename Lhs, typename Rhs>
|
2010-10-25 10:15:22 -04:00
|
|
|
struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
|
2008-03-12 17:17:36 +00:00
|
|
|
{
|
2010-10-25 10:15:22 -04:00
|
|
|
// we must not inherit from traits<Lhs> since it has
|
2010-06-09 17:16:05 +02:00
|
|
|
// the potential to cause problems with MSVC
|
2010-10-26 16:47:01 +02:00
|
|
|
typedef typename remove_all<Lhs>::type Ancestor;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename traits<Ancestor>::XprKind XprKind;
|
2010-06-09 17:16:05 +02:00
|
|
|
enum {
|
2010-10-25 10:15:22 -04:00
|
|
|
RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
|
2010-06-09 17:16:05 +02:00
|
|
|
};
|
|
|
|
|
|
2008-12-03 21:01:55 +00:00
|
|
|
// even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
|
|
|
|
|
// we still want to handle the case when the result type is different.
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename result_of<
|
2008-03-12 17:17:36 +00:00
|
|
|
BinaryOp(
|
|
|
|
|
typename Lhs::Scalar,
|
|
|
|
|
typename Rhs::Scalar
|
|
|
|
|
)
|
|
|
|
|
>::type Scalar;
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
|
|
|
|
|
typename traits<Rhs>::StorageKind>::ret StorageKind;
|
|
|
|
|
typedef typename promote_index_type<typename traits<Lhs>::Index,
|
|
|
|
|
typename traits<Rhs>::Index>::type Index;
|
2008-05-01 18:58:30 +00:00
|
|
|
typedef typename Lhs::Nested LhsNested;
|
|
|
|
|
typedef typename Rhs::Nested RhsNested;
|
2010-10-25 22:13:49 +02:00
|
|
|
typedef typename remove_reference<LhsNested>::type _LhsNested;
|
|
|
|
|
typedef typename remove_reference<RhsNested>::type _RhsNested;
|
2008-03-12 17:17:36 +00:00
|
|
|
enum {
|
2008-05-01 18:58:30 +00:00
|
|
|
LhsFlags = _LhsNested::Flags,
|
|
|
|
|
RhsFlags = _RhsNested::Flags,
|
2010-10-25 22:13:49 +02:00
|
|
|
SameType = is_same<typename _LhsNested::Scalar,typename _RhsNested::Scalar>::value,
|
2009-11-18 17:20:39 -05:00
|
|
|
StorageOrdersAgree = (int(Lhs::Flags)&RowMajorBit)==(int(Rhs::Flags)&RowMajorBit),
|
2010-03-21 11:28:03 -04:00
|
|
|
Flags0 = (int(LhsFlags) | int(RhsFlags)) & (
|
2008-05-14 08:20:15 +00:00
|
|
|
HereditaryBits
|
2009-11-18 17:20:39 -05:00
|
|
|
| (int(LhsFlags) & int(RhsFlags) &
|
|
|
|
|
( AlignedBit
|
|
|
|
|
| (StorageOrdersAgree ? LinearAccessBit : 0)
|
2010-10-25 10:15:22 -04:00
|
|
|
| (functor_traits<BinaryOp>::PacketAccess && StorageOrdersAgree && SameType ? PacketAccessBit : 0)
|
2009-11-18 17:20:39 -05:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
),
|
2014-03-10 23:24:40 +01:00
|
|
|
Flags = (Flags0 & ~RowMajorBit) | (LhsFlags & RowMajorBit)
|
|
|
|
|
#ifndef EIGEN_TEST_EVALUATORS
|
|
|
|
|
,
|
|
|
|
|
LhsCoeffReadCost = _LhsNested::CoeffReadCost,
|
|
|
|
|
RhsCoeffReadCost = _RhsNested::CoeffReadCost,
|
2010-10-25 10:15:22 -04:00
|
|
|
CoeffReadCost = LhsCoeffReadCost + RhsCoeffReadCost + functor_traits<BinaryOp>::Cost
|
2014-03-10 23:24:40 +01:00
|
|
|
#endif
|
2008-03-12 17:17:36 +00:00
|
|
|
};
|
|
|
|
|
};
|
2010-10-25 10:15:22 -04:00
|
|
|
} // end namespace internal
|
2008-03-10 17:23:11 +00:00
|
|
|
|
2010-04-16 10:13:32 -04:00
|
|
|
template<typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
|
2009-11-17 10:11:27 +01:00
|
|
|
class CwiseBinaryOpImpl;
|
|
|
|
|
|
2008-03-03 10:52:44 +00:00
|
|
|
template<typename BinaryOp, typename Lhs, typename Rhs>
|
2010-10-25 10:15:22 -04:00
|
|
|
class CwiseBinaryOp : internal::no_assignment_operator,
|
2009-11-17 10:11:27 +01:00
|
|
|
public CwiseBinaryOpImpl<
|
|
|
|
|
BinaryOp, Lhs, Rhs,
|
2010-10-25 10:15:22 -04:00
|
|
|
typename internal::promote_storage_type<typename internal::traits<Lhs>::StorageKind,
|
|
|
|
|
typename internal::traits<Rhs>::StorageKind>::ret>
|
2008-02-29 14:35:14 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2008-03-12 17:17:36 +00:00
|
|
|
|
2009-11-17 10:11:27 +01:00
|
|
|
typedef typename CwiseBinaryOpImpl<
|
|
|
|
|
BinaryOp, Lhs, Rhs,
|
2010-10-25 10:15:22 -04:00
|
|
|
typename internal::promote_storage_type<typename internal::traits<Lhs>::StorageKind,
|
|
|
|
|
typename internal::traits<Rhs>::StorageKind>::ret>::Base Base;
|
2010-06-03 08:41:11 +02:00
|
|
|
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
|
2009-11-17 16:04:19 +01:00
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::nested<Lhs>::type LhsNested;
|
|
|
|
|
typedef typename internal::nested<Rhs>::type RhsNested;
|
2010-10-25 22:13:49 +02:00
|
|
|
typedef typename internal::remove_reference<LhsNested>::type _LhsNested;
|
|
|
|
|
typedef typename internal::remove_reference<RhsNested>::type _RhsNested;
|
2008-03-12 17:17:36 +00:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs, const BinaryOp& func = BinaryOp())
|
|
|
|
|
: m_lhs(aLhs), m_rhs(aRhs), m_functor(func)
|
2008-02-29 14:35:14 +00:00
|
|
|
{
|
2010-07-19 23:31:08 +02:00
|
|
|
EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp,typename Lhs::Scalar,typename Rhs::Scalar);
|
2008-12-05 14:45:42 +00:00
|
|
|
// require the sizes to match
|
2008-12-18 13:36:31 +00:00
|
|
|
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
|
2012-06-22 16:32:45 +02:00
|
|
|
eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
|
2008-02-29 14:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_STRONG_INLINE Index rows() const {
|
2010-03-13 13:15:27 +01:00
|
|
|
// return the fixed size type if available to enable compile time optimizations
|
2010-10-26 16:47:01 +02:00
|
|
|
if (internal::traits<typename internal::remove_all<LhsNested>::type>::RowsAtCompileTime==Dynamic)
|
2010-03-13 13:15:27 +01:00
|
|
|
return m_rhs.rows();
|
|
|
|
|
else
|
|
|
|
|
return m_lhs.rows();
|
|
|
|
|
}
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_STRONG_INLINE Index cols() const {
|
2010-03-13 13:15:27 +01:00
|
|
|
// return the fixed size type if available to enable compile time optimizations
|
2010-10-26 16:47:01 +02:00
|
|
|
if (internal::traits<typename internal::remove_all<LhsNested>::type>::ColsAtCompileTime==Dynamic)
|
2010-03-13 13:15:27 +01:00
|
|
|
return m_rhs.cols();
|
|
|
|
|
else
|
|
|
|
|
return m_lhs.cols();
|
|
|
|
|
}
|
2008-02-29 14:35:14 +00:00
|
|
|
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the left hand side nested expression */
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-11-17 10:11:27 +01:00
|
|
|
const _LhsNested& lhs() const { return m_lhs; }
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the right hand side nested expression */
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-11-17 10:11:27 +01:00
|
|
|
const _RhsNested& rhs() const { return m_rhs; }
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the functor representing the binary operation */
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2009-11-17 10:11:27 +01:00
|
|
|
const BinaryOp& functor() const { return m_functor; }
|
|
|
|
|
|
|
|
|
|
protected:
|
2012-02-03 23:18:26 +01:00
|
|
|
LhsNested m_lhs;
|
|
|
|
|
RhsNested m_rhs;
|
2009-11-17 10:11:27 +01:00
|
|
|
const BinaryOp m_functor;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename BinaryOp, typename Lhs, typename Rhs>
|
|
|
|
|
class CwiseBinaryOpImpl<BinaryOp, Lhs, Rhs, Dense>
|
2010-10-25 10:15:22 -04:00
|
|
|
: public internal::dense_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type
|
2009-11-17 10:11:27 +01:00
|
|
|
{
|
2010-01-22 10:15:41 +01:00
|
|
|
typedef CwiseBinaryOp<BinaryOp, Lhs, Rhs> Derived;
|
2009-11-17 10:11:27 +01:00
|
|
|
public:
|
|
|
|
|
|
2010-10-25 10:15:22 -04:00
|
|
|
typedef typename internal::dense_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >::type Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE( Derived )
|
2009-11-17 10:11:27 +01:00
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
|
2008-02-29 14:35:14 +00:00
|
|
|
{
|
2012-06-22 16:32:45 +02:00
|
|
|
return derived().functor()(derived().lhs().coeff(rowId, colId),
|
|
|
|
|
derived().rhs().coeff(rowId, colId));
|
2008-02-29 14:35:14 +00:00
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2008-05-05 10:23:29 +00:00
|
|
|
template<int LoadMode>
|
2012-06-22 16:32:45 +02:00
|
|
|
EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
|
2008-04-09 12:31:55 +00:00
|
|
|
{
|
2012-06-22 16:32:45 +02:00
|
|
|
return derived().functor().packetOp(derived().lhs().template packet<LoadMode>(rowId, colId),
|
|
|
|
|
derived().rhs().template packet<LoadMode>(rowId, colId));
|
2008-04-09 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 19:06:14 +01:00
|
|
|
EIGEN_DEVICE_FUNC
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
|
2008-06-26 16:06:41 +00:00
|
|
|
{
|
2009-11-17 10:11:27 +01:00
|
|
|
return derived().functor()(derived().lhs().coeff(index),
|
|
|
|
|
derived().rhs().coeff(index));
|
2008-06-26 16:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<int LoadMode>
|
2010-05-30 16:00:58 -04:00
|
|
|
EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
|
2008-06-26 16:06:41 +00:00
|
|
|
{
|
2009-11-17 10:11:27 +01:00
|
|
|
return derived().functor().packetOp(derived().lhs().template packet<LoadMode>(index),
|
|
|
|
|
derived().rhs().template packet<LoadMode>(index));
|
2008-06-26 16:06:41 +00:00
|
|
|
}
|
2008-02-29 14:35:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** replaces \c *this by \c *this - \a other.
|
|
|
|
|
*
|
|
|
|
|
* \returns a reference to \c *this
|
|
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-02-29 14:35:14 +00:00
|
|
|
template<typename OtherDerived>
|
2008-12-19 15:38:39 +00:00
|
|
|
EIGEN_STRONG_INLINE Derived &
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other)
|
2008-02-29 14:35:14 +00:00
|
|
|
{
|
2013-12-13 18:06:58 +01:00
|
|
|
#ifdef EIGEN_TEST_EVALUATORS
|
|
|
|
|
call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar>());
|
|
|
|
|
#else
|
2010-10-25 10:15:22 -04:00
|
|
|
SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
2010-09-06 11:51:42 +02:00
|
|
|
tmp = other.derived();
|
2013-12-13 18:06:58 +01:00
|
|
|
#endif
|
2009-11-20 15:39:38 +01:00
|
|
|
return derived();
|
2008-02-29 14:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** replaces \c *this by \c *this + \a other.
|
|
|
|
|
*
|
|
|
|
|
* \returns a reference to \c *this
|
|
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-02-29 14:35:14 +00:00
|
|
|
template<typename OtherDerived>
|
2008-12-19 15:38:39 +00:00
|
|
|
EIGEN_STRONG_INLINE Derived &
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other)
|
2008-02-29 14:35:14 +00:00
|
|
|
{
|
2013-12-13 18:06:58 +01:00
|
|
|
#ifdef EIGEN_TEST_EVALUATORS
|
|
|
|
|
call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar>());
|
|
|
|
|
#else
|
2010-10-25 10:15:22 -04:00
|
|
|
SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
2009-12-16 19:18:40 +01:00
|
|
|
tmp = other.derived();
|
2013-12-13 18:06:58 +01:00
|
|
|
#endif
|
2009-11-20 15:39:38 +01:00
|
|
|
return derived();
|
2008-02-29 14:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2008-02-29 14:35:14 +00:00
|
|
|
#endif // EIGEN_CWISE_BINARY_OP_H
|
2013-07-31 15:30:50 +02:00
|
|
|
|