FieldMatVec.hpp 7.13 KB
 Praetorius, Simon committed Nov 13, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 #pragma once #include #include #include #include #include #include namespace AMDiS { // some arithmetic operations with Dune::FieldVector template ) > Dune::FieldVector operator*(Dune::FieldVector v, S factor) { return v *= factor; } template ) > Dune::FieldVector operator*(S factor, Dune::FieldVector v) { return v *= factor; } template ) > Dune::FieldVector operator/(Dune::FieldVector v, S factor) { return v /= factor; } // some arithmetic operations with Dune::FieldMatrix template Dune::FieldMatrix operator+(Dune::FieldMatrix A, Dune::FieldMatrix const& B) { return A += B; } template Dune::FieldMatrix operator-(Dune::FieldMatrix A, Dune::FieldMatrix const& B) { return A -= B; } // ---------------------------------------------------------------------------- /// Cross-product a 2d-vector = orthogonal vector template Dune::FieldVector cross(Dune::FieldVector const& a) { return {{ a[1], -a[0] }}; } /// Cross-product of two vectors (in 3d only) template Dune::FieldVector cross(Dune::FieldVector const& a, Dune::FieldVector const& b) { return {{ a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0] }}; } /// Dot product (vec1^T * vec2) template auto dot(Dune::FieldVector const& vec1, Dune::FieldVector const& vec2) { return vec1.dot(vec2); } // ---------------------------------------------------------------------------- namespace Impl { template T accumulate(Dune::FieldVector const& x, Operation op) { T result = 0; for (int i = 0; i < N; ++i) result = op(result, x[i]); return result; } } // end namespace Impl /// Sum of vector entires. template T sum(Dune::FieldVector const& x) { return Impl::accumulate(x, Operation::plus{}); } /// Dot-product with the vector itself template auto unary_dot(Dune::FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + sqr(std::abs(b)); }; return Impl::accumulate(x, op); } /// Maximum over all vector entries template auto max(Dune::FieldVector const& x) { return Impl::accumulate(x, Operation::maximum{}); } /// Minimum over all vector entries template auto min(Dune::FieldVector const& x) { return Impl::accumulate(x, Operation::minimum{}); } /// Maximum of the absolute values of vector entries template auto abs_max(Dune::FieldVector const& x) { return Impl::accumulate(x, Operation::abs_max{}); } /// Minimum of the absolute values of vector entries template auto abs_min(Dune::FieldVector const& x) { return Impl::accumulate(x, Operation::abs_min{}); } // ---------------------------------------------------------------------------- /** \ingroup vector_norms * \brief The 1-norm of a vector = sum_i |x_i| **/ template auto one_norm(Dune::FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + std::abs(b); }; return Impl::accumulate(x, op); } /** \ingroup vector_norms * \brief The euklidean 2-norm of a vector = sqrt( sum_i |x_i|^2 ) **/ template auto two_norm(Dune::FieldVector const& x) { return std::sqrt(unary_dot(x)); } /** \ingroup vector_norms * \brief The p-norm of a vector = ( sum_i |x_i|^p )^(1/p) **/ template auto p_norm(Dune::FieldVector const& x) { auto op = [](auto const& a, auto const& b) { return a + Math::pow
(std::abs(b)); }; return std::pow( Impl::accumulate(x, op), 1.0/p ); } /** \ingroup vector_norms * \brief The infty-norm of a vector = max_i |x_i| = alias for \ref abs_max **/ template auto infty_norm(Dune::FieldVector const& x) { return abs_max(x); } // ---------------------------------------------------------------------------- /// The euklidean distance between two vectors = |lhs-rhs|_2 template T distance(Dune::FieldVector const& lhs, Dune::FieldVector const& rhs) { T result = 0; for (int i = 0; i < N; ++i) result += sqr(lhs[i] - rhs[i]); return std::sqrt(result); } // ---------------------------------------------------------------------------- /// Outer product (vec1 * vec2^T) template auto outer(Dune::FieldMatrix const& vec1, Dune::FieldMatrix const& vec2) { using result_type = Dune::FieldMatrix() * std::declval() ), N, M>; result_type mat; for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) mat[i][j] = vec1[i].dot(vec2[j]); return mat; } // ---------------------------------------------------------------------------- template T det(Dune::FieldMatrix const& /*mat*/) { return 0; } /// Determinant of a 1x1 matrix template T det(Dune::FieldMatrix const& mat) { return mat[0][0]; } /// Determinant of a 2x2 matrix template T det(Dune::FieldMatrix const& mat) { return mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0]; } /// Determinant of a 3x3 matrix template T det(Dune::FieldMatrix const& mat) { return mat[0][0]*mat[1][1]*mat[2][2] + mat[0][1]*mat[1][2]*mat[2][0] + mat[0][2]*mat[1][0]*mat[2][1] - (mat[0][2]*mat[1][1]*mat[2][0] + mat[0][1]*mat[1][0]*mat[2][2] + mat[0][0]*mat[1][2]*mat[2][1]); } /// Determinant of a NxN matrix template T det(Dune::FieldMatrix const& mat) { return mat.determinant(); } /// Return the inverse of the matrix mat template auto inv(Dune::FieldMatrix mat) { mat.invert(); return mat; } /// Solve the linear system A*x = b template void solve(Dune::FieldMatrix const& A, Dune::FieldVector& x, Dune::FieldVector const& b) { A.solve(x, b); } /// Gramian determinant = sqrt( det( DT^T * DF ) ) template T gramian(Dune::FieldMatrix const& DF) { using std::sqrt; return sqrt( det(outer(DF, DF)) ); } /// Gramian determinant, specialization for 1 column matrices template T gramian(Dune::FieldMatrix const& DF) { using std::sqrt; return sqrt(dot(DF[0], DF[0])); } } // end namespace AMDiS