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
|
|
|
//
|
2014-06-23 10:40:03 +02:00
|
|
|
// Copyright (C) 2008-2014 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
|
|
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
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>> {
|
|
|
|
|
// 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
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef remove_all_t<Lhs> 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.
|
2016-01-28 12:12:06 +01:00
|
|
|
typedef typename result_of<BinaryOp(const typename Lhs::Scalar&, const typename Rhs::Scalar&)>::type Scalar;
|
2014-07-01 17:51:53 +02:00
|
|
|
typedef typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
|
|
|
|
|
BinaryOp>::ret StorageKind;
|
2014-12-04 22:48:53 +01:00
|
|
|
typedef typename promote_index_type<typename traits<Lhs>::StorageIndex, typename traits<Rhs>::StorageIndex>::type
|
|
|
|
|
StorageIndex;
|
2008-05-01 18:58:30 +00:00
|
|
|
typedef typename Lhs::Nested LhsNested;
|
|
|
|
|
typedef typename Rhs::Nested RhsNested;
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef std::remove_reference_t<LhsNested> LhsNested_;
|
|
|
|
|
typedef std::remove_reference_t<RhsNested> RhsNested_;
|
2008-03-12 17:17:36 +00:00
|
|
|
enum {
|
2022-01-10 20:53:29 +00:00
|
|
|
Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
|
|
|
|
|
LhsNested_::Flags & RowMajorBit, RhsNested_::Flags & RowMajorBit>::value
|
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;
|
|
|
|
|
|
2016-01-01 21:45:06 +01:00
|
|
|
/** \class CwiseBinaryOp
|
|
|
|
|
* \ingroup Core_Module
|
|
|
|
|
*
|
|
|
|
|
* \brief Generic expression where a coefficient-wise binary operator is applied to two expressions
|
|
|
|
|
*
|
|
|
|
|
* \tparam BinaryOp template functor implementing the operator
|
|
|
|
|
* \tparam LhsType the type of the left-hand side
|
|
|
|
|
* \tparam RhsType the type of the right-hand side
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Most of the time, this is the only way that it is used, so you typically don't have to name
|
|
|
|
|
* CwiseBinaryOp types explicitly.
|
|
|
|
|
*
|
|
|
|
|
* \sa MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const, class CwiseUnaryOp, class
|
|
|
|
|
* CwiseNullaryOp
|
|
|
|
|
*/
|
2014-06-20 15:39:38 +02:00
|
|
|
template <typename BinaryOp, typename LhsType, typename RhsType>
|
|
|
|
|
class CwiseBinaryOp : public CwiseBinaryOpImpl<BinaryOp, LhsType, RhsType,
|
2014-07-01 17:51:53 +02:00
|
|
|
typename internal::cwise_promote_storage_type<
|
|
|
|
|
typename internal::traits<LhsType>::StorageKind,
|
|
|
|
|
typename internal::traits<RhsType>::StorageKind, BinaryOp>::ret>,
|
2014-12-02 14:40:19 +01:00
|
|
|
internal::no_assignment_operator {
|
2008-02-29 14:35:14 +00:00
|
|
|
public:
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef internal::remove_all_t<BinaryOp> Functor;
|
|
|
|
|
typedef internal::remove_all_t<LhsType> Lhs;
|
|
|
|
|
typedef internal::remove_all_t<RhsType> Rhs;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2009-11-17 10:11:27 +01:00
|
|
|
typedef typename CwiseBinaryOpImpl<
|
2014-06-20 15:39:38 +02:00
|
|
|
BinaryOp, LhsType, RhsType,
|
2014-07-01 17:51:53 +02:00
|
|
|
typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
|
|
|
|
|
typename internal::traits<Rhs>::StorageKind, BinaryOp>::ret>::Base
|
|
|
|
|
Base;
|
2010-06-03 08:41:11 +02:00
|
|
|
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2021-09-16 20:43:54 +00:00
|
|
|
EIGEN_CHECK_BINARY_COMPATIBILIY(BinaryOp, typename Lhs::Scalar, typename Rhs::Scalar)
|
|
|
|
|
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-06-19 17:56:39 +02:00
|
|
|
typedef typename internal::ref_selector<LhsType>::type LhsNested;
|
|
|
|
|
typedef typename internal::ref_selector<RhsType>::type RhsNested;
|
2022-03-16 16:43:40 +00:00
|
|
|
typedef std::remove_reference_t<LhsNested> LhsNested_;
|
|
|
|
|
typedef std::remove_reference_t<RhsNested> RhsNested_;
|
2008-03-12 17:17:36 +00:00
|
|
|
|
2021-11-24 20:08:49 +00:00
|
|
|
#if EIGEN_COMP_MSVC
|
2019-02-15 16:35:35 +01:00
|
|
|
// Required for Visual Studio or the Copy constructor will probably not get inlined!
|
|
|
|
|
EIGEN_STRONG_INLINE CwiseBinaryOp(const CwiseBinaryOp<BinaryOp, LhsType, RhsType>&) = default;
|
2019-02-16 09:44:05 +01:00
|
|
|
#endif
|
2019-02-15 16:35:35 +01:00
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs,
|
|
|
|
|
const BinaryOp& func = BinaryOp())
|
2012-06-22 16:32:45 +02:00
|
|
|
: m_lhs(aLhs), m_rhs(aRhs), m_functor(func) {
|
|
|
|
|
eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
|
2008-02-29 14:35:14 +00:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT {
|
2010-03-13 13:15:27 +01:00
|
|
|
// return the fixed size type if available to enable compile time optimizations
|
2022-03-16 16:43:40 +00:00
|
|
|
return internal::traits<internal::remove_all_t<LhsNested>>::RowsAtCompileTime == Dynamic ? m_rhs.rows()
|
|
|
|
|
: m_lhs.rows();
|
2010-03-13 13:15:27 +01:00
|
|
|
}
|
2021-03-08 12:39:11 -05:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT {
|
2010-03-13 13:15:27 +01:00
|
|
|
// return the fixed size type if available to enable compile time optimizations
|
2022-03-16 16:43:40 +00:00
|
|
|
return internal::traits<internal::remove_all_t<LhsNested>>::ColsAtCompileTime == Dynamic ? m_rhs.cols()
|
|
|
|
|
: m_lhs.cols();
|
2010-03-13 13:15:27 +01:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the left hand side nested expression */
|
2022-01-10 20:53:29 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const LhsNested_& lhs() const { return m_lhs; }
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the right hand side nested expression */
|
2022-01-10 20:53:29 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const RhsNested_& rhs() const { return m_rhs; }
|
2010-01-22 10:15:41 +01:00
|
|
|
/** \returns the functor representing the binary operation */
|
2009-11-17 10:11:27 +01:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const BinaryOp& functor() const { return m_functor; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2009-11-17 10:11:27 +01:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-20 15:39:38 +02:00
|
|
|
// Generic API dispatcher
|
|
|
|
|
template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
|
|
|
|
|
class CwiseBinaryOpImpl : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type {
|
|
|
|
|
public:
|
|
|
|
|
typedef typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type Base;
|
|
|
|
|
};
|
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-03-10 17:23:11 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived>& other) {
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 15:11:41 +02:00
|
|
|
call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
|
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-03-10 17:23:11 +00:00
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other) {
|
Relax mixing-type constraints for binary coefficient-wise operators:
- Replace internal::scalar_product_traits<A,B> by Eigen::ScalarBinaryOpTraits<A,B,OP>
- Remove the "functor_is_product_like" helper (was pretty ugly)
- Currently, OP is not used, but it is available to the user for fine grained tuning
- Currently, only the following operators have been generalized: *,/,+,-,=,*=,/=,+=,-=
- TODO: generalize all other binray operators (comparisons,pow,etc.)
- TODO: handle "scalar op array" operators (currently only * is handled)
- TODO: move the handling of the "void" scalar type to ScalarBinaryOpTraits
2016-06-06 15:11:41 +02:00
|
|
|
call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
|
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
|