Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
CwiseBinaryOp.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_CWISE_BINARY_OP_H
13#define EIGEN_CWISE_BINARY_OP_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <typename BinaryOp>
22struct binary_op_compatibility {
23 using type = BinaryOp;
24};
25
26// The optimized multiply has the same scalar contract as scalar_product_op.
27template <typename LhsScalar, typename RhsScalar>
28struct binary_op_compatibility<fast_mult_op<LhsScalar, RhsScalar>> {
29 using type = scalar_product_op<LhsScalar, RhsScalar>;
30};
31
32template <typename BinaryOp, typename Lhs, typename Rhs>
33struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs>> {
34 // we must not inherit from traits<Lhs> since it has
35 // the potential to cause problems with MSVC
36 using Ancestor = remove_all_t<Lhs>;
37 using XprKind = typename traits<Ancestor>::XprKind;
38 enum {
39 RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
40 ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
41 MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
42 MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
43 };
44
45 // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
46 // we still want to handle the case when the result type is different.
47 using Scalar = typename result_of<BinaryOp(const typename Lhs::Scalar&, const typename Rhs::Scalar&)>::type;
48 using StorageKind = typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
49 typename traits<Rhs>::StorageKind, BinaryOp>::ret;
50 using StorageIndex =
51 typename promote_index_type<typename traits<Lhs>::StorageIndex, typename traits<Rhs>::StorageIndex>::type;
52 using LhsNested = typename Lhs::Nested;
53 using RhsNested = typename Rhs::Nested;
54 using LhsNested_ = std::remove_reference_t<LhsNested>;
55 using RhsNested_ = std::remove_reference_t<RhsNested>;
56 enum {
57 Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
58 LhsNested_::Flags & RowMajorBit, RhsNested_::Flags & RowMajorBit>::value
59 };
60};
61} // end namespace internal
62
63template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
64class CwiseBinaryOpImpl;
65
86template <typename BinaryOp, typename LhsType, typename RhsType>
87class CwiseBinaryOp : public CwiseBinaryOpImpl<BinaryOp, LhsType, RhsType,
88 typename internal::cwise_promote_storage_type<
89 typename internal::traits<LhsType>::StorageKind,
90 typename internal::traits<RhsType>::StorageKind, BinaryOp>::ret>,
91 internal::no_assignment_operator {
92 public:
93 using Functor = internal::remove_all_t<BinaryOp>;
94 using Lhs = internal::remove_all_t<LhsType>;
95 using Rhs = internal::remove_all_t<RhsType>;
96
97 using Base = typename CwiseBinaryOpImpl<
98 BinaryOp, LhsType, RhsType,
99 typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
100 typename internal::traits<Rhs>::StorageKind, BinaryOp>::ret>::Base;
101 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
102
103 EIGEN_CHECK_BINARY_COMPATIBILITY(typename internal::binary_op_compatibility<BinaryOp>::type, typename Lhs::Scalar,
104 typename Rhs::Scalar)
105 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
106
107 using LhsNested = typename internal::ref_selector<LhsType>::type;
108 using RhsNested = typename internal::ref_selector<RhsType>::type;
109 using LhsNested_ = std::remove_reference_t<LhsNested>;
110 using RhsNested_ = std::remove_reference_t<RhsNested>;
111
112 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs,
113 const BinaryOp& func = BinaryOp())
114 : m_lhs(aLhs), m_rhs(aRhs), m_functor(func) {
115 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(aLhs, aRhs);
116 }
117
118 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept {
119 // return the fixed size type if available to enable compile time optimizations
120 return internal::traits<internal::remove_all_t<LhsNested>>::RowsAtCompileTime == Dynamic ? m_rhs.rows()
121 : m_lhs.rows();
122 }
123 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept {
124 // return the fixed size type if available to enable compile time optimizations
125 return internal::traits<internal::remove_all_t<LhsNested>>::ColsAtCompileTime == Dynamic ? m_rhs.cols()
126 : m_lhs.cols();
127 }
128
130 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const LhsNested_& lhs() const { return m_lhs; }
132 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const RhsNested_& rhs() const { return m_rhs; }
134 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const BinaryOp& functor() const { return m_functor; }
135
136 protected:
137 LhsNested m_lhs;
138 RhsNested m_rhs;
139 const BinaryOp m_functor;
140};
141
142// Generic API dispatcher
143template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
144class CwiseBinaryOpImpl : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type {
145 public:
146 using Base = typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type;
147};
148
153template <typename Derived>
154template <typename OtherDerived>
155EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Derived& MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived>& other) {
156 call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
157 return derived();
158}
159
164template <typename Derived>
165template <typename OtherDerived>
166EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Derived& MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other) {
167 call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
168 return derived();
169}
170
171} // end namespace Eigen
172
173#endif // EIGEN_CWISE_BINARY_OP_H
constexpr const LhsNested_ & lhs() const
Definition CwiseBinaryOp.h:130
constexpr const BinaryOp & functor() const
Definition CwiseBinaryOp.h:134
constexpr const RhsNested_ & rhs() const
Definition CwiseBinaryOp.h:132
constexpr unsigned int RowMajorBit
Definition Constants.h:71