Eigen  3.4.90 (git rev 21cd3fe20990a5ac1d683806f605110962aac3f1)
 
Loading...
Searching...
No Matches
CwiseTernaryOp.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// 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
17namespace Eigen {
18
19namespace internal {
20template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3>
21struct 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
53template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3,
54 typename StorageKind>
55class CwiseTernaryOpImpl;
56
84template <typename TernaryOp, typename Arg1Type, typename Arg2Type,
85 typename Arg3Type>
86class CwiseTernaryOp : public CwiseTernaryOpImpl<
87 TernaryOp, Arg1Type, Arg2Type, Arg3Type,
88 typename internal::traits<Arg1Type>::StorageKind>,
89 internal::no_assignment_operator
90{
91 public:
92 typedef internal::remove_all_t<Arg1Type> Arg1;
93 typedef internal::remove_all_t<Arg2Type> Arg2;
94 typedef internal::remove_all_t<Arg3Type> Arg3;
95
96 // require the sizes to match
97 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Arg1, Arg2)
98 EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Arg1, Arg3)
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;
113 EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseTernaryOp)
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
122 EIGEN_DEVICE_FUNC
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
131 EIGEN_DEVICE_FUNC
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 }
148 EIGEN_DEVICE_FUNC
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
167 EIGEN_DEVICE_FUNC
168 const Arg1Nested_& arg1() const { return m_arg1; }
170 EIGEN_DEVICE_FUNC
171 const Arg2Nested_& arg2() const { return m_arg2; }
173 EIGEN_DEVICE_FUNC
174 const Arg3Nested_& arg3() const { return m_arg3; }
176 EIGEN_DEVICE_FUNC
177 const TernaryOp& functor() const { return m_functor; }
178
179 protected:
180 Arg1Nested m_arg1;
181 Arg2Nested m_arg2;
182 Arg3Nested m_arg3;
183 const TernaryOp m_functor;
184};
185
186// Generic API dispatcher
187template <typename TernaryOp, typename Arg1, typename Arg2, typename Arg3,
188 typename StorageKind>
189class CwiseTernaryOpImpl
190 : public internal::generic_xpr_base<
191 CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >::type {
192 public:
193 typedef typename internal::generic_xpr_base<
194 CwiseTernaryOp<TernaryOp, Arg1, Arg2, Arg3> >::type Base;
195};
196
197} // end namespace Eigen
198
199#endif // EIGEN_CWISE_TERNARY_OP_H
Generic expression where a coefficient-wise ternary operator is applied to two expressions.
Definition CwiseTernaryOp.h:90
const Arg3Nested_ & arg3() const
Definition CwiseTernaryOp.h:174
const Arg2Nested_ & arg2() const
Definition CwiseTernaryOp.h:171
const TernaryOp & functor() const
Definition CwiseTernaryOp.h:177
const Arg1Nested_ & arg1() const
Definition CwiseTernaryOp.h:168
const unsigned int RowMajorBit
Definition Constants.h:68
Namespace containing all symbols from the Eigen library.
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