CwiseTernaryOp.h
Go to the documentation of this file.
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 // Copyright (C) 2016 Eugene Brevdo <ebrevdo@gmail.com>
7 //
8 // This Source Code Form is subject to the terms of the Mozilla
9 // Public License v. 2.0. If a copy of the MPL was not distributed
10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 
12 #ifndef EIGEN_CWISE_TERNARY_OP_H
13 #define EIGEN_CWISE_TERNARY_OP_H
14 
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3>
21 struct traits<CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> > {
22  // we must not inherit from traits<Arg1> since it has
23  // the potential to cause problems with MSVC
24  typedef remove_all_t<Arg1> Ancestor;
25  typedef typename traits<Ancestor>::XprKind XprKind;
26  enum {
27  RowsAtCompileTime = traits<Ancestor>::RowsAtCompileTime,
28  ColsAtCompileTime = traits<Ancestor>::ColsAtCompileTime,
29  MaxRowsAtCompileTime = traits<Ancestor>::MaxRowsAtCompileTime,
30  MaxColsAtCompileTime = traits<Ancestor>::MaxColsAtCompileTime
31  };
32 
33  // even though we require Arg1, Arg2, and Arg3 to have the same scalar type
34  // (see CwiseTernaryOp constructor),
35  // we still want to handle the case when the result type is different.
36  typedef typename result_of<TernaryOp(
37  const typename Arg1::Scalar&, const typename Arg2::Scalar&,
38  const typename Arg3::Scalar&)>::type Scalar;
39 
40  typedef typename internal::traits<Arg1>::StorageKind StorageKind;
41  typedef typename internal::traits<Arg1>::StorageIndex StorageIndex;
42 
43  typedef typename Arg1::Nested Arg1Nested;
44  typedef typename Arg2::Nested Arg2Nested;
45  typedef typename Arg3::Nested Arg3Nested;
46  typedef std::remove_reference_t<Arg1Nested> Arg1Nested_;
47  typedef std::remove_reference_t<Arg2Nested> Arg2Nested_;
48  typedef std::remove_reference_t<Arg3Nested> Arg3Nested_;
49  enum { Flags = Arg1Nested_::Flags & RowMajorBit };
50 };
51 } // end namespace internal
52 
53 template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3,
54  typename StorageKind>
55 class CwiseTernaryOpImpl;
56 
84 template <typename TernaryOp, typename Arg1Type, typename Arg2Type,
85  typename Arg3Type>
87  TernaryOp, Arg1Type, Arg2Type, Arg3Type,
88  typename internal::traits<Arg1Type>::StorageKind>,
89  internal::no_assignment_operator
90 {
91  public:
95 
96  // require the sizes to match
99 
100  // The index types should match
101  EIGEN_STATIC_ASSERT((internal::is_same<
102  typename internal::traits<Arg1Type>::StorageKind,
103  typename internal::traits<Arg2Type>::StorageKind>::value),
104  STORAGE_KIND_MUST_MATCH)
105  EIGEN_STATIC_ASSERT((internal::is_same<
106  typename internal::traits<Arg1Type>::StorageKind,
107  typename internal::traits<Arg3Type>::StorageKind>::value),
108  STORAGE_KIND_MUST_MATCH)
109 
110  typedef typename CwiseTernaryOpImpl<
111  TernaryOp, Arg1Type, Arg2Type, Arg3Type,
112  typename internal::traits<Arg1Type>::StorageKind>::Base Base;
114 
115  typedef typename internal::ref_selector<Arg1Type>::type Arg1Nested;
116  typedef typename internal::ref_selector<Arg2Type>::type Arg2Nested;
117  typedef typename internal::ref_selector<Arg3Type>::type Arg3Nested;
118  typedef std::remove_reference_t<Arg1Nested> Arg1Nested_;
119  typedef std::remove_reference_t<Arg2Nested> Arg2Nested_;
120  typedef std::remove_reference_t<Arg3Nested> Arg3Nested_;
121 
123  EIGEN_STRONG_INLINE CwiseTernaryOp(const Arg1& a1, const Arg2& a2,
124  const Arg3& a3,
125  const TernaryOp& func = TernaryOp())
126  : m_arg1(a1), m_arg2(a2), m_arg3(a3), m_functor(func) {
127  eigen_assert(a1.rows() == a2.rows() && a1.cols() == a2.cols() &&
128  a1.rows() == a3.rows() && a1.cols() == a3.cols());
129  }
130 
132  EIGEN_STRONG_INLINE Index rows() const {
133  // return the fixed size type if available to enable compile time
134  // optimizations
135  if (internal::traits<internal::remove_all_t<Arg1Nested>>::
136  RowsAtCompileTime == Dynamic &&
137  internal::traits<internal::remove_all_t<Arg2Nested>>::
138  RowsAtCompileTime == Dynamic)
139  return m_arg3.rows();
140  else if (internal::traits<internal::remove_all_t<Arg1Nested>>::
141  RowsAtCompileTime == Dynamic &&
142  internal::traits<internal::remove_all_t<Arg3Nested>>::
143  RowsAtCompileTime == Dynamic)
144  return m_arg2.rows();
145  else
146  return m_arg1.rows();
147  }
149  EIGEN_STRONG_INLINE Index cols() const {
150  // return the fixed size type if available to enable compile time
151  // optimizations
152  if (internal::traits<internal::remove_all_t<Arg1Nested>>::
153  ColsAtCompileTime == Dynamic &&
154  internal::traits<internal::remove_all_t<Arg2Nested>>::
155  ColsAtCompileTime == Dynamic)
156  return m_arg3.cols();
157  else if (internal::traits<internal::remove_all_t<Arg1Nested>>::
158  ColsAtCompileTime == Dynamic &&
159  internal::traits<internal::remove_all_t<Arg3Nested>>::
160  ColsAtCompileTime == Dynamic)
161  return m_arg2.cols();
162  else
163  return m_arg1.cols();
164  }
165 
168  const Arg1Nested_& arg1() const { return m_arg1; }
171  const Arg2Nested_& arg2() const { return m_arg2; }
174  const Arg3Nested_& arg3() const { return m_arg3; }
177  const TernaryOp& functor() const { return m_functor; }
178 
179  protected:
183  const TernaryOp m_functor;
184 };
185 
186 // Generic API dispatcher
187 template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3,
188  typename StorageKind>
190  : public internal::generic_xpr_base<
191  CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >::type {
192  public:
193  typedef typename internal::generic_xpr_base<
195 };
196 
197 } // end namespace Eigen
198 
199 #endif // EIGEN_CWISE_TERNARY_OP_H
#define EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1149
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:86
internal::generic_xpr_base< CwiseTernaryOp< TernaryOp, Arg1Type, Arg2Type, Arg3Type > >::type Base
Generic expression where a coefficient-wise ternary operator is applied to two expressions.
internal::remove_all_t< Arg3Type > Arg3
internal::remove_all_t< Arg2Type > Arg2
const Arg3Nested_ & arg3() const
internal::ref_selector< Arg3Type >::type Arg3Nested
internal::remove_all_t< Arg1Type > Arg1
std::remove_reference_t< Arg2Nested > Arg2Nested_
const Arg1Nested_ & arg1() const
std::remove_reference_t< Arg3Nested > Arg3Nested_
const TernaryOp & functor() const
std::remove_reference_t< Arg1Nested > Arg1Nested_
EIGEN_STATIC_ASSERT((internal::is_same< typename internal::traits< Arg1Type >::StorageKind, typename internal::traits< Arg2Type >::StorageKind >::value), STORAGE_KIND_MUST_MATCH) EIGEN_STATIC_ASSERT((internal typedef internal::ref_selector< Arg1Type >::type Arg1Nested
internal::ref_selector< Arg2Type >::type Arg2Nested
const TernaryOp m_functor
const Arg2Nested_ & arg2() const
const unsigned int RowMajorBit
Definition: Constants.h:68
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
Definition: BFloat16.h:222