Medial Code Documentation
Loading...
Searching...
No Matches
DenseCoeffsBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_DENSECOEFFSBASE_H
11#define EIGEN_DENSECOEFFSBASE_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename T> struct add_const_on_value_type_if_arithmetic
17{
18 typedef typename conditional<is_arithmetic<T>::value, T, typename add_const_on_value_type<T>::type>::type type;
19};
20}
21
33template<typename Derived>
34class DenseCoeffsBase<Derived,ReadOnlyAccessors> : public EigenBase<Derived>
35{
36 public:
37 typedef typename internal::traits<Derived>::StorageKind StorageKind;
38 typedef typename internal::traits<Derived>::Scalar Scalar;
39 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
40
41 // Explanation for this CoeffReturnType typedef.
42 // - This is the return type of the coeff() method.
43 // - The LvalueBit means exactly that we can offer a coeffRef() method, which means exactly that we can get references
44 // to coeffs, which means exactly that we can have coeff() return a const reference (as opposed to returning a value).
45 // - The is_artihmetic check is required since "const int", "const double", etc. will cause warnings on some systems
46 // while the declaration of "const T", where T is a non arithmetic type does not. Always returning "const Scalar&" is
47 // not possible, since the underlying expressions might not offer a valid address the reference could be referring to.
49 const Scalar&,
50 typename internal::conditional<internal::is_arithmetic<Scalar>::value, Scalar, const Scalar>::type
51 >::type CoeffReturnType;
52
54 typename internal::packet_traits<Scalar>::type
55 >::type PacketReturnType;
56
58 using Base::rows;
59 using Base::cols;
60 using Base::size;
61 using Base::derived;
62
64 EIGEN_STRONG_INLINE Index rowIndexByOuterInner(Index outer, Index inner) const
65 {
66 return int(Derived::RowsAtCompileTime) == 1 ? 0
67 : int(Derived::ColsAtCompileTime) == 1 ? inner
68 : int(Derived::Flags)&RowMajorBit ? outer
69 : inner;
70 }
71
73 EIGEN_STRONG_INLINE Index colIndexByOuterInner(Index outer, Index inner) const
74 {
75 return int(Derived::ColsAtCompileTime) == 1 ? 0
76 : int(Derived::RowsAtCompileTime) == 1 ? inner
77 : int(Derived::Flags)&RowMajorBit ? inner
78 : outer;
79 }
80
96 EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
97 {
98 eigen_internal_assert(row >= 0 && row < rows()
99 && col >= 0 && col < cols());
100 return internal::evaluator<Derived>(derived()).coeff(row,col);
101 }
102
104 EIGEN_STRONG_INLINE CoeffReturnType coeffByOuterInner(Index outer, Index inner) const
105 {
106 return coeff(rowIndexByOuterInner(outer, inner),
107 colIndexByOuterInner(outer, inner));
108 }
109
114 EIGEN_DEVICE_FUNC
115 EIGEN_STRONG_INLINE CoeffReturnType operator()(Index row, Index col) const
116 {
117 eigen_assert(row >= 0 && row < rows()
118 && col >= 0 && col < cols());
119 return coeff(row, col);
120 }
121
138 EIGEN_STRONG_INLINE CoeffReturnType
139 coeff(Index index) const
140 {
142 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
143 eigen_internal_assert(index >= 0 && index < size());
144 return internal::evaluator<Derived>(derived()).coeff(index);
145 }
146
147
157 EIGEN_STRONG_INLINE CoeffReturnType
158 operator[](Index index) const
159 {
160 EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
161 THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
162 eigen_assert(index >= 0 && index < size());
163 return coeff(index);
164 }
165
177 EIGEN_STRONG_INLINE CoeffReturnType
178 operator()(Index index) const
179 {
180 eigen_assert(index >= 0 && index < size());
181 return coeff(index);
182 }
183
187 EIGEN_STRONG_INLINE CoeffReturnType
188 x() const { return (*this)[0]; }
189
193 EIGEN_STRONG_INLINE CoeffReturnType
194 y() const { return (*this)[1]; }
195
199 EIGEN_STRONG_INLINE CoeffReturnType
200 z() const { return (*this)[2]; }
201
205 EIGEN_STRONG_INLINE CoeffReturnType
206 w() const { return (*this)[3]; }
207
218 template<int LoadMode>
219 EIGEN_STRONG_INLINE PacketReturnType packet(Index row, Index col) const
220 {
221 typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
222 eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
223 return internal::evaluator<Derived>(derived()).template packet<LoadMode,DefaultPacketType>(row,col);
224 }
225
226
228 template<int LoadMode>
229 EIGEN_STRONG_INLINE PacketReturnType packetByOuterInner(Index outer, Index inner) const
230 {
231 return packet<LoadMode>(rowIndexByOuterInner(outer, inner),
232 colIndexByOuterInner(outer, inner));
233 }
234
245 template<int LoadMode>
246 EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
247 {
248 EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
249 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
250 typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
251 eigen_internal_assert(index >= 0 && index < size());
252 return internal::evaluator<Derived>(derived()).template packet<LoadMode,DefaultPacketType>(index);
253 }
254
255 protected:
256 // explanation: DenseBase is doing "using ..." on the methods from DenseCoeffsBase.
257 // But some methods are only available in the DirectAccess case.
258 // So we add dummy methods here with these names, so that "using... " doesn't fail.
259 // It's not private so that the child class DenseBase can access them, and it's not public
260 // either since it's an implementation detail, so has to be protected.
261 void coeffRef();
262 void coeffRefByOuterInner();
263 void writePacket();
264 void writePacketByOuterInner();
265 void copyCoeff();
266 void copyCoeffByOuterInner();
267 void copyPacket();
268 void copyPacketByOuterInner();
269 void stride();
270 void innerStride();
271 void outerStride();
272 void rowStride();
273 void colStride();
274};
275
287template<typename Derived>
288class DenseCoeffsBase<Derived, WriteAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors>
289{
290 public:
291
293
294 typedef typename internal::traits<Derived>::StorageKind StorageKind;
295 typedef typename internal::traits<Derived>::Scalar Scalar;
296 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
297 typedef typename NumTraits<Scalar>::Real RealScalar;
298
299 using Base::coeff;
300 using Base::rows;
301 using Base::cols;
302 using Base::size;
303 using Base::derived;
304 using Base::rowIndexByOuterInner;
305 using Base::colIndexByOuterInner;
306 using Base::operator[];
307 using Base::operator();
308 using Base::x;
309 using Base::y;
310 using Base::z;
311 using Base::w;
312
328 EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
329 {
330 eigen_internal_assert(row >= 0 && row < rows()
331 && col >= 0 && col < cols());
332 return internal::evaluator<Derived>(derived()).coeffRef(row,col);
333 }
334
336 EIGEN_STRONG_INLINE Scalar&
337 coeffRefByOuterInner(Index outer, Index inner)
338 {
339 return coeffRef(rowIndexByOuterInner(outer, inner),
340 colIndexByOuterInner(outer, inner));
341 }
342
348 EIGEN_DEVICE_FUNC
349 EIGEN_STRONG_INLINE Scalar&
351 {
352 eigen_assert(row >= 0 && row < rows()
353 && col >= 0 && col < cols());
354 return coeffRef(row, col);
355 }
356
357
374 EIGEN_STRONG_INLINE Scalar&
376 {
378 THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
379 eigen_internal_assert(index >= 0 && index < size());
380 return internal::evaluator<Derived>(derived()).coeffRef(index);
381 }
382
391 EIGEN_STRONG_INLINE Scalar&
393 {
394 EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
395 THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
396 eigen_assert(index >= 0 && index < size());
397 return coeffRef(index);
398 }
399
410 EIGEN_STRONG_INLINE Scalar&
412 {
413 eigen_assert(index >= 0 && index < size());
414 return coeffRef(index);
415 }
416
420 EIGEN_STRONG_INLINE Scalar&
421 x() { return (*this)[0]; }
422
426 EIGEN_STRONG_INLINE Scalar&
427 y() { return (*this)[1]; }
428
432 EIGEN_STRONG_INLINE Scalar&
433 z() { return (*this)[2]; }
434
438 EIGEN_STRONG_INLINE Scalar&
439 w() { return (*this)[3]; }
440};
441
453template<typename Derived>
454class DenseCoeffsBase<Derived, DirectAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors>
455{
456 public:
457
459 typedef typename internal::traits<Derived>::Scalar Scalar;
460 typedef typename NumTraits<Scalar>::Real RealScalar;
461
462 using Base::rows;
463 using Base::cols;
464 using Base::size;
465 using Base::derived;
466
472 inline Index innerStride() const
473 {
474 return derived().innerStride();
475 }
476
483 inline Index outerStride() const
484 {
485 return derived().outerStride();
486 }
487
488 // FIXME shall we remove it ?
489 inline Index stride() const
490 {
491 return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
492 }
493
498 EIGEN_DEVICE_FUNC
499 inline Index rowStride() const
500 {
501 return Derived::IsRowMajor ? outerStride() : innerStride();
502 }
503
509 inline Index colStride() const
510 {
511 return Derived::IsRowMajor ? innerStride() : outerStride();
512 }
513};
514
526template<typename Derived>
528 : public DenseCoeffsBase<Derived, WriteAccessors>
529{
530 public:
531
533 typedef typename internal::traits<Derived>::Scalar Scalar;
534 typedef typename NumTraits<Scalar>::Real RealScalar;
535
536 using Base::rows;
537 using Base::cols;
538 using Base::size;
539 using Base::derived;
540
546 inline Index innerStride() const
547 {
548 return derived().innerStride();
549 }
550
557 inline Index outerStride() const
558 {
559 return derived().outerStride();
560 }
561
562 // FIXME shall we remove it ?
563 inline Index stride() const
564 {
565 return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
566 }
567
572 EIGEN_DEVICE_FUNC
573 inline Index rowStride() const
574 {
575 return Derived::IsRowMajor ? outerStride() : innerStride();
576 }
577
583 inline Index colStride() const
584 {
585 return Derived::IsRowMajor ? innerStride() : outerStride();
586 }
587};
588
589namespace internal {
590
591template<int Alignment, typename Derived, bool JustReturnZero>
593{
594 static inline Index run(const Derived&)
595 { return 0; }
596};
597
598template<int Alignment, typename Derived>
599struct first_aligned_impl<Alignment, Derived, false>
600{
601 static inline Index run(const Derived& m)
602 {
603 return internal::first_aligned<Alignment>(&m.const_cast_derived().coeffRef(0,0), m.size());
604 }
605};
606
614template<int Alignment, typename Derived>
615static inline Index first_aligned(const DenseBase<Derived>& m)
616{
617 enum { ReturnZero = (int(evaluator<Derived>::Alignment) >= Alignment) || !(Derived::Flags & DirectAccessBit) };
618 return first_aligned_impl<Alignment, Derived, ReturnZero>::run(m.derived());
619}
620
621template<typename Derived>
622static inline Index first_default_aligned(const DenseBase<Derived>& m)
623{
624 typedef typename Derived::Scalar Scalar;
625 typedef typename packet_traits<Scalar>::type DefaultPacketType;
626 return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
627}
628
629template<typename Derived, bool HasDirectAccess = has_direct_access<Derived>::ret>
634
635template<typename Derived>
637{
638 enum { ret = 0 };
639};
640
646
647template<typename Derived>
649{
650 enum { ret = 0 };
651};
652
653} // end namespace internal
654
655} // end namespace Eigen
656
657#endif // EIGEN_DENSECOEFFSBASE_H
EIGEN_DEVICE_FUNC Index innerStride() const
Definition DenseCoeffsBase.h:472
EIGEN_DEVICE_FUNC Index outerStride() const
Definition DenseCoeffsBase.h:483
EIGEN_DEVICE_FUNC Index rowStride() const
Definition DenseCoeffsBase.h:499
EIGEN_DEVICE_FUNC Index colStride() const
Definition DenseCoeffsBase.h:509
EIGEN_DEVICE_FUNC Index outerStride() const
Definition DenseCoeffsBase.h:557
EIGEN_DEVICE_FUNC Index innerStride() const
Definition DenseCoeffsBase.h:546
EIGEN_DEVICE_FUNC Index rowStride() const
Definition DenseCoeffsBase.h:573
EIGEN_DEVICE_FUNC Index colStride() const
Definition DenseCoeffsBase.h:583
Base class providing read-only coefficient access to matrices and arrays.
Definition DenseCoeffsBase.h:35
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType operator()(Index row, Index col) const
Definition DenseCoeffsBase.h:115
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType x() const
equivalent to operator[](0).
Definition DenseCoeffsBase.h:188
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType w() const
equivalent to operator[](3).
Definition DenseCoeffsBase.h:206
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType operator()(Index index) const
Definition DenseCoeffsBase.h:178
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType z() const
equivalent to operator[](2).
Definition DenseCoeffsBase.h:200
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType operator[](Index index) const
Definition DenseCoeffsBase.h:158
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
Short version: don't use this function, use operator()(Index,Index) const instead.
Definition DenseCoeffsBase.h:96
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Short version: don't use this function, use operator[](Index) const instead.
Definition DenseCoeffsBase.h:139
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType y() const
equivalent to operator[](1).
Definition DenseCoeffsBase.h:194
Base class providing read/write coefficient access to matrices and arrays.
Definition DenseCoeffsBase.h:289
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & x()
equivalent to operator[](0).
Definition DenseCoeffsBase.h:421
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator[](Index index)
Definition DenseCoeffsBase.h:392
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & y()
equivalent to operator[](1).
Definition DenseCoeffsBase.h:427
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index row, Index col)
Short version: don't use this function, use operator()(Index,Index) instead.
Definition DenseCoeffsBase.h:328
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index row, Index col)
Definition DenseCoeffsBase.h:350
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & z()
equivalent to operator[](2).
Definition DenseCoeffsBase.h:433
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & operator()(Index index)
Definition DenseCoeffsBase.h:411
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index index)
Short version: don't use this function, use operator[](Index) instead.
Definition DenseCoeffsBase.h:375
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & w()
equivalent to operator[](3).
Definition DenseCoeffsBase.h:439
Definition ForwardDeclarations.h:54
Pseudo expression representing a solving operation.
Definition Solve.h:63
@ DirectAccessors
Direct read-only access to the coefficients.
Definition Constants.h:370
@ ReadOnlyAccessors
Read-only access via a member function.
Definition Constants.h:366
@ WriteAccessors
Read/write access via member functions.
Definition Constants.h:368
@ DirectWriteAccessors
Direct read/write access to the coefficients.
Definition Constants.h:372
const unsigned int LinearAccessBit
Short version: means the expression can be seen as 1D vector.
Definition Constants.h:124
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array.
Definition Constants.h:149
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition Constants.h:138
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition EigenBase.h:29
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:37
Definition Meta.h:34
Definition CoreEvaluators.h:82
Definition DenseCoeffsBase.h:593
Definition DenseCoeffsBase.h:631
Definition DenseCoeffsBase.h:643
Definition ForwardDeclarations.h:17
Definition Meta.h:30