Eigen  5.0.1
 
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, typename Lhs, typename Rhs>
22struct traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs>> {
23 // we must not inherit from traits<Lhs> since it has
24 // the potential to cause problems with MSVC
25 using Ancestor = remove_all_t<Lhs>;
26 using XprKind = typename traits<Ancestor>::XprKind;
27 enum {
28 RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
29 ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
30 MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
31 MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
32 };
33
34 // even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
35 // we still want to handle the case when the result type is different.
36 using Scalar = typename result_of<BinaryOp(const typename Lhs::Scalar&, const typename Rhs::Scalar&)>::type;
37 using StorageKind = typename cwise_promote_storage_type<typename traits<Lhs>::StorageKind,
38 typename traits<Rhs>::StorageKind, BinaryOp>::ret;
39 using StorageIndex =
40 typename promote_index_type<typename traits<Lhs>::StorageIndex, typename traits<Rhs>::StorageIndex>::type;
41 using LhsNested = typename Lhs::Nested;
42 using RhsNested = typename Rhs::Nested;
43 using LhsNested_ = std::remove_reference_t<LhsNested>;
44 using RhsNested_ = std::remove_reference_t<RhsNested>;
45 enum {
46 Flags = cwise_promote_storage_order<typename traits<Lhs>::StorageKind, typename traits<Rhs>::StorageKind,
47 LhsNested_::Flags & RowMajorBit, RhsNested_::Flags & RowMajorBit>::value
48 };
49};
50} // end namespace internal
51
52template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
53class CwiseBinaryOpImpl;
54
75template <typename BinaryOp, typename LhsType, typename RhsType>
76class CwiseBinaryOp : public CwiseBinaryOpImpl<BinaryOp, LhsType, RhsType,
77 typename internal::cwise_promote_storage_type<
78 typename internal::traits<LhsType>::StorageKind,
79 typename internal::traits<RhsType>::StorageKind, BinaryOp>::ret>,
80 internal::no_assignment_operator {
81 public:
82 using Functor = internal::remove_all_t<BinaryOp>;
83 using Lhs = internal::remove_all_t<LhsType>;
84 using Rhs = internal::remove_all_t<RhsType>;
85
86 using Base = typename CwiseBinaryOpImpl<
87 BinaryOp, LhsType, RhsType,
88 typename internal::cwise_promote_storage_type<typename internal::traits<LhsType>::StorageKind,
89 typename internal::traits<Rhs>::StorageKind, BinaryOp>::ret>::Base;
90 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
91
92 EIGEN_CHECK_BINARY_COMPATIBILITY(BinaryOp, typename Lhs::Scalar, typename Rhs::Scalar)
93 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
94
95 using LhsNested = typename internal::ref_selector<LhsType>::type;
96 using RhsNested = typename internal::ref_selector<RhsType>::type;
97 using LhsNested_ = std::remove_reference_t<LhsNested>;
98 using RhsNested_ = std::remove_reference_t<RhsNested>;
99
100 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& aLhs, const Rhs& aRhs,
101 const BinaryOp& func = BinaryOp())
102 : m_lhs(aLhs), m_rhs(aRhs), m_functor(func) {
103 eigen_assert(aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols());
104 }
105
106 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept {
107 // return the fixed size type if available to enable compile time optimizations
108 return internal::traits<internal::remove_all_t<LhsNested>>::RowsAtCompileTime == Dynamic ? m_rhs.rows()
109 : m_lhs.rows();
110 }
111 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept {
112 // return the fixed size type if available to enable compile time optimizations
113 return internal::traits<internal::remove_all_t<LhsNested>>::ColsAtCompileTime == Dynamic ? m_rhs.cols()
114 : m_lhs.cols();
115 }
116
118 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const LhsNested_& lhs() const { return m_lhs; }
120 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const RhsNested_& rhs() const { return m_rhs; }
122 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const BinaryOp& functor() const { return m_functor; }
123
124 protected:
125 LhsNested m_lhs;
126 RhsNested m_rhs;
127 const BinaryOp m_functor;
128};
129
130// Generic API dispatcher
131template <typename BinaryOp, typename Lhs, typename Rhs, typename StorageKind>
132class CwiseBinaryOpImpl : public internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type {
133 public:
134 using Base = typename internal::generic_xpr_base<CwiseBinaryOp<BinaryOp, Lhs, Rhs>>::type;
135};
136
141template <typename Derived>
142template <typename OtherDerived>
143EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Derived& MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived>& other) {
144 call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar, typename OtherDerived::Scalar>());
145 return derived();
146}
147
152template <typename Derived>
153template <typename OtherDerived>
154EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Derived& MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other) {
155 call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar, typename OtherDerived::Scalar>());
156 return derived();
157}
158
159} // end namespace Eigen
160
161#endif // EIGEN_CWISE_BINARY_OP_H
constexpr const LhsNested_ & lhs() const
Definition CwiseBinaryOp.h:118
constexpr const BinaryOp & functor() const
Definition CwiseBinaryOp.h:122
constexpr const RhsNested_ & rhs() const
Definition CwiseBinaryOp.h:120
constexpr unsigned int RowMajorBit
Definition Constants.h:71