| 1 | /* |
| 2 | * Matrix.hpp |
| 3 | * |
| 4 | * Created on: Jun 3, 2015 |
| 5 | * Author: Pietro Incardona |
| 6 | */ |
| 7 | |
| 8 | #ifndef MATRIX_HPP_ |
| 9 | #define MATRIX_HPP_ |
| 10 | |
| 11 | |
| 12 | #include <boost/fusion/sequence/intrinsic/at_c.hpp> |
| 13 | #include <boost/fusion/include/at_c.hpp> |
| 14 | #include <boost/fusion/container/vector.hpp> |
| 15 | #include <boost/fusion/include/vector.hpp> |
| 16 | #include <boost/fusion/container/vector/vector_fwd.hpp> |
| 17 | #include <boost/fusion/include/vector_fwd.hpp> |
| 18 | #include "boost/multi_array.hpp" |
| 19 | #include "Grid/grid_key.hpp" |
| 20 | |
| 21 | /*! \brief This class implement an NxN (dense) matrix |
| 22 | * |
| 23 | * Be carefull when you allocate locally it take memory from the stack |
| 24 | * |
| 25 | * \tparam dim dimensionality |
| 26 | * \tparam T type of the space |
| 27 | * |
| 28 | * \warning Matrix is untested so it remain as concept usage until test are not introduced |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | template<unsigned int dim ,typename T> class Matrix |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | typedef T coord_type; |
| 37 | |
| 38 | //! boost fusion that store the point |
| 39 | typedef boost::fusion::vector<T[dim][dim]> type; |
| 40 | |
| 41 | //! structure that store the data of the point |
| 42 | type data; |
| 43 | |
| 44 | //! Property id of the point |
| 45 | static const unsigned int mat = 0; |
| 46 | |
| 47 | /*! \brief Get coordinate |
| 48 | * |
| 49 | * \param i row |
| 50 | * \param j colums |
| 51 | * \return the matrix element |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | inline T get(size_t i,size_t j) const |
| 56 | { |
| 57 | return boost::fusion::at_c<mat>(data)[i][j]; |
| 58 | } |
| 59 | |
| 60 | /*! \brief Get coordinate |
| 61 | * |
| 62 | * \param i row |
| 63 | * \param j colum |
| 64 | * \return the value |
| 65 | * |
| 66 | */ |
| 67 | |
| 68 | inline T& get(size_t i,size_t j) |
| 69 | { |
| 70 | return boost::fusion::at_c<mat>(data)[i][j]; |
| 71 | } |
| 72 | |
| 73 | /*! \brief operator= between Matrix |
| 74 | * |
| 75 | * \param m Matrix |
| 76 | * |
| 77 | */ |
| 78 | inline Matrix<dim,T> & operator=(const Matrix<dim,T> & m) |
| 79 | { |
| 80 | for (size_t i = 0 ; i < dim ; i++) |
| 81 | { |
| 82 | for (size_t j = 0 ; j < dim ; j++) |
| 83 | get(i,j) = m.get(i,j); |
| 84 | } |
| 85 | |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | /*! \brief Set to zero the point coordinate |
| 90 | * |
| 91 | * |
| 92 | */ |
| 93 | void zero() |
| 94 | { |
| 95 | for (size_t i = 0 ; i < dim ; i++) |
| 96 | { |
| 97 | for (size_t j = 0 ; j < dim ; j++) |
| 98 | { |
| 99 | get(i,j) = 0; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /*! \brief Point constructor from point |
| 105 | * |
| 106 | * \param p the point |
| 107 | * |
| 108 | */ |
| 109 | Matrix(const Matrix<dim,T> && p) |
| 110 | { |
| 111 | for(size_t i = 0; i < dim ; i++) |
| 112 | { |
| 113 | for (size_t j = 0 ; j < dim ; j++) |
| 114 | { |
| 115 | get(i,j) = p.get(i,j); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /*! \brief Point constructor from point |
| 121 | * |
| 122 | * \param p the point |
| 123 | * |
| 124 | */ |
| 125 | Matrix(const Matrix<dim,T> & p) |
| 126 | { |
| 127 | for(size_t i = 0; i < dim ; i++) |
| 128 | { |
| 129 | for (size_t j = 0 ; j < dim ; j++) |
| 130 | { |
| 131 | get(i,j) = p.get(i,j); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /*! \brief Constructor from an array |
| 137 | * |
| 138 | * \param p array with the coordinate of the point |
| 139 | * |
| 140 | */ |
| 141 | Matrix(const T (&p)[dim][dim]) |
| 142 | { |
| 143 | for(size_t i = 0; i < dim ; i++) |
| 144 | { |
| 145 | for (size_t j = 0 ; j < dim ; j++) |
| 146 | { |
| 147 | get(i,j) = p[i][j]; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | //! Default contructor |
| 153 | Matrix() |
| 154 | {} |
| 155 | |
| 156 | /*! \brief Identity matrix |
| 157 | * |
| 158 | * \return the identity matrix |
| 159 | * |
| 160 | */ |
| 161 | inline static Matrix<dim,T> identity() |
| 162 | { |
| 163 | Matrix<dim,T> ret; |
| 164 | |
| 165 | for (size_t i = 0 ; i < dim ; i++) |
| 166 | { |
| 167 | for (size_t j = 0 ; j < dim ; j++) |
| 168 | { |
| 169 | /* coverity[dead_error_line] */ |
| 170 | ret.get(i,j) = (i == j)?1:0; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | //! 1 property |
| 178 | static const unsigned int max_prop = 1; |
| 179 | |
| 180 | //! dimension of the matrix (it is a square matrix) |
| 181 | static const unsigned int dims = dim; |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | #endif /* MATRIX_HPP_ */ |
| 186 | |