1 /*! 2 * \file include/TFEL/Math/Matrix/TMatrixTVectorExpr.hxx 3 * \brief 4 * 5 * \author Thomas Helfer 6 * \date 06 avr 2008 7 * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights 8 * reserved. 9 * This project is publicly released under either the GNU GPL Licence 10 * or the CECILL-A licence. A copy of thoses licences are delivered 11 * with the sources of TFEL. CEA or EDF may also distribute this 12 * project under specific licensing conditions. 13 */ 14 15 #ifndef LIB_TFEL_MATH_TMATRIXTVECTOREXPR_HXX 16 #define LIB_TFEL_MATH_TMATRIXTVECTOREXPR_HXX 17 18 #include<cstddef> 19 #include<iterator> 20 #include<type_traits> 21 22 #include"TFEL/Config/TFELConfig.hxx" 23 #include"TFEL/Metaprogramming/StaticAssert.hxx" 24 #include"TFEL/Metaprogramming/Implements.hxx" 25 #include"TFEL/FSAlgorithm/inner_product.hxx" 26 #include"TFEL/Math/General/ResultType.hxx" 27 #include"TFEL/Math/General/BasicOperations.hxx" 28 #include"TFEL/Math/General/RunTimeCheck.hxx" 29 #include"TFEL/Math/General/ObjectObjectRandomAccessConstIterator.hxx" 30 #include"TFEL/Math/Vector/VectorConcept.hxx" 31 #include"TFEL/Math/tvector.hxx" 32 #include"TFEL/Math/tmatrix.hxx" 33 34 namespace tfel{ 35 36 namespace math { 37 38 /*! 39 * \brief A placeholder for the computation of product of a tiny 40 * matrix and a tiny vector. 41 * \tparam N : numbers of rows of the matrix 42 * \tparam M : numbers of columns of the matrix 43 * \tparam A : type of the tiny matrix object (can be a reference) 44 * \tparam B : type of the tiny vector object (can be a reference) 45 */ 46 template<unsigned short N,unsigned short M, 47 typename A, typename B> 48 struct TMatrixTVectorExpr 49 : public ExprBase 50 { 51 //! a simple alias 52 typedef EmptyRunTimeProperties RunTimeProperties; 53 //! \brief return the runtime properties 54 TFEL_MATH_INLINE RunTimeProperties getRunTimePropertiestfel::math::TMatrixTVectorExpr55 getRunTimeProperties() const 56 { 57 return RunTimeProperties(); 58 } // end of getRunTimeProperties 59 /*! 60 * a pseudo iterator allowing to iterate over values in the same 61 * row 62 */ 63 struct RowConstIterator 64 { 65 using MType = typename std::decay<A>::type; 66 using NumType = typename MatrixTraits<MType>::NumType; 67 //! default constructor RowConstIteratortfel::math::TMatrixTVectorExpr::RowConstIterator68 TFEL_MATH_INLINE RowConstIterator(const MType& m_, 69 const unsigned short i_) 70 : m(m_),i(i_) 71 {} 72 //! go to the next column operator ++tfel::math::TMatrixTVectorExpr::RowConstIterator73 TFEL_MATH_INLINE RowConstIterator& operator++ () 74 { 75 ++j; 76 return *this; 77 } // end of operator++ 78 //! \return the current matrix value 79 TFEL_MATH_INLINE NumType operator *tfel::math::TMatrixTVectorExpr::RowConstIterator80 operator *() const 81 { 82 return this->m(this->i,this->j); 83 } 84 private: 85 const MType& m; //! reference to the underlying matrix 86 const unsigned short i; //! row index 87 unsigned short j = 0; //! current column 88 }; // end of struc RowConstIterator 89 //! a pseudo iterator for the vector. This iterator works even if 90 // VType does not provide an iterator 91 struct VectorConstIterator 92 { 93 using VType = typename std::decay<B>::type; 94 using NumType = typename VectorTraits<VType>::NumType; VectorConstIteratortfel::math::TMatrixTVectorExpr::VectorConstIterator95 TFEL_MATH_INLINE VectorConstIterator(const VType& v_) 96 : v(v_) 97 {} operator ++tfel::math::TMatrixTVectorExpr::VectorConstIterator98 TFEL_MATH_INLINE VectorConstIterator& operator++ () 99 { 100 ++i; 101 return *this; 102 } // end of operator++ 103 TFEL_MATH_INLINE NumType operator *tfel::math::TMatrixTVectorExpr::VectorConstIterator104 operator *() const 105 { 106 return this->v(this->i); 107 } 108 private: 109 VType v; 110 unsigned short i = 0; 111 }; // end of struc VectorConstIterator 112 113 protected: 114 //! a simple alias 115 using Result = typename ComputeBinaryResult<typename std::decay<A>::type, 116 typename std::decay<B>::type,OpMult>::Result; 117 //! a simple alias 118 using NumType = typename VectorTraits<Result>::NumType; 119 //! a simple alias 120 using IndexType = typename VectorTraits<Result>::IndexType; 121 122 typedef NumType value_type; 123 typedef NumType* pointer; 124 typedef const NumType* const_pointer; 125 typedef NumType& reference; 126 typedef const NumType& const_reference; 127 typedef IndexType size_type; 128 typedef ptrdiff_t difference_type; 129 130 TMatrixTVectorExpr() = delete; 131 TMatrixTVectorExprtfel::math::TMatrixTVectorExpr132 TFEL_MATH_INLINE TMatrixTVectorExpr(A l,B r) 133 : a(l), b(r) 134 {} 135 136 TFEL_MATH_INLINE NumType operator ()tfel::math::TMatrixTVectorExpr137 operator()(const IndexType i) const 138 { 139 using namespace tfel::fsalgo; 140 return inner_product<M>::template exe<NumType>(RowConstIterator(a,i), 141 VectorConstIterator(b)); 142 } 143 144 ArgumentStorage<A> a; 145 ArgumentStorage<B> b; 146 TFEL_STATIC_ASSERT((tfel::meta::Implements<typename std::decay<A>::type,MatrixConcept>::cond)); 147 TFEL_STATIC_ASSERT((tfel::meta::Implements<typename std::decay<B>::type,VectorConcept>::cond)); 148 }; 149 150 } // end of namespace math 151 152 } // end of namespace tfel 153 154 #endif /* LIB_TFEL_MATH_TMATRIXTVECTOREXPR_HXX */ 155 156