CwiseUnaryView.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #ifndef EIGEN_CWISE_UNARY_VIEW_H
  10. #define EIGEN_CWISE_UNARY_VIEW_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename ViewOp, typename MatrixType>
  14. struct traits<CwiseUnaryView<ViewOp, MatrixType> >
  15. : traits<MatrixType>
  16. {
  17. typedef typename result_of<
  18. ViewOp(const typename traits<MatrixType>::Scalar&)
  19. >::type Scalar;
  20. typedef typename MatrixType::Nested MatrixTypeNested;
  21. typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
  22. enum {
  23. FlagsLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
  24. Flags = traits<_MatrixTypeNested>::Flags & (RowMajorBit | FlagsLvalueBit | DirectAccessBit), // FIXME DirectAccessBit should not be handled by expressions
  25. MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
  26. // need to cast the sizeof's from size_t to int explicitly, otherwise:
  27. // "error: no integral type can represent all of the enumerator values
  28. InnerStrideAtCompileTime = MatrixTypeInnerStride == Dynamic
  29. ? int(Dynamic)
  30. : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)),
  31. OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret == Dynamic
  32. ? int(Dynamic)
  33. : outer_stride_at_compile_time<MatrixType>::ret * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar))
  34. };
  35. };
  36. }
  37. template<typename ViewOp, typename MatrixType, typename StorageKind>
  38. class CwiseUnaryViewImpl;
  39. /** \class CwiseUnaryView
  40. * \ingroup Core_Module
  41. *
  42. * \brief Generic lvalue expression of a coefficient-wise unary operator of a matrix or a vector
  43. *
  44. * \tparam ViewOp template functor implementing the view
  45. * \tparam MatrixType the type of the matrix we are applying the unary operator
  46. *
  47. * This class represents a lvalue expression of a generic unary view operator of a matrix or a vector.
  48. * It is the return type of real() and imag(), and most of the time this is the only way it is used.
  49. *
  50. * \sa MatrixBase::unaryViewExpr(const CustomUnaryOp &) const, class CwiseUnaryOp
  51. */
  52. template<typename ViewOp, typename MatrixType>
  53. class CwiseUnaryView : public CwiseUnaryViewImpl<ViewOp, MatrixType, typename internal::traits<MatrixType>::StorageKind>
  54. {
  55. public:
  56. typedef typename CwiseUnaryViewImpl<ViewOp, MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
  57. EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
  58. typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
  59. typedef typename internal::remove_all<MatrixType>::type NestedExpression;
  60. explicit inline CwiseUnaryView(MatrixType& mat, const ViewOp& func = ViewOp())
  61. : m_matrix(mat), m_functor(func) {}
  62. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
  63. EIGEN_STRONG_INLINE Index rows() const { return m_matrix.rows(); }
  64. EIGEN_STRONG_INLINE Index cols() const { return m_matrix.cols(); }
  65. /** \returns the functor representing unary operation */
  66. const ViewOp& functor() const { return m_functor; }
  67. /** \returns the nested expression */
  68. const typename internal::remove_all<MatrixTypeNested>::type&
  69. nestedExpression() const { return m_matrix; }
  70. /** \returns the nested expression */
  71. typename internal::remove_reference<MatrixTypeNested>::type&
  72. nestedExpression() { return m_matrix.const_cast_derived(); }
  73. protected:
  74. MatrixTypeNested m_matrix;
  75. ViewOp m_functor;
  76. };
  77. // Generic API dispatcher
  78. template<typename ViewOp, typename XprType, typename StorageKind>
  79. class CwiseUnaryViewImpl
  80. : public internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType> >::type
  81. {
  82. public:
  83. typedef typename internal::generic_xpr_base<CwiseUnaryView<ViewOp, XprType> >::type Base;
  84. };
  85. template<typename ViewOp, typename MatrixType>
  86. class CwiseUnaryViewImpl<ViewOp,MatrixType,Dense>
  87. : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type
  88. {
  89. public:
  90. typedef CwiseUnaryView<ViewOp, MatrixType> Derived;
  91. typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type Base;
  92. EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
  93. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
  94. EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
  95. EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeff(0)); }
  96. EIGEN_DEVICE_FUNC inline Index innerStride() const
  97. {
  98. return derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
  99. }
  100. EIGEN_DEVICE_FUNC inline Index outerStride() const
  101. {
  102. return derived().nestedExpression().outerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
  103. }
  104. protected:
  105. EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
  106. };
  107. } // end namespace Eigen
  108. #endif // EIGEN_CWISE_UNARY_VIEW_H