FieldMatVec.hpp 9.33 KB
Newer Older
 Praetorius, Simon committed Nov 13, 2017 1 2 #pragma once  Praetorius, Simon committed Dec 16, 2017 3 4 #include  Praetorius, Simon committed Nov 13, 2017 5 6 7 #include #include  Praetorius, Simon committed Dec 28, 2017 8 #include  Praetorius, Simon committed Nov 13, 2017 9 10 #include #include  Praetorius, Simon committed Dec 28, 2017 11 12 #include #include  Praetorius, Simon committed Nov 13, 2017 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  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; }  Praetorius, Simon committed Nov 19, 2017 53 54 55 56 57 58  template Dune::FieldVector operator*(Dune::FieldMatrix const& mat, Dune::FieldVector const& vec) { return Dune::FMatrixHelp::mult(mat, vec); }  Praetorius, Simon committed Nov 13, 2017 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  // ---------------------------------------------------------------------------- /// 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) {  Praetorius, Simon committed Dec 28, 2017 103  return Impl::accumulate(x, Operation::Plus{});  Praetorius, Simon committed Nov 13, 2017 104 105 106 107 108 109 110  } /// Dot-product with the vector itself template auto unary_dot(Dune::FieldVector const& x) {  Praetorius, Simon committed Dec 20, 2017 111  auto op = [](auto const& a, auto const& b) { return a + Math::sqr(std::abs(b)); };  Praetorius, Simon committed Nov 13, 2017 112 113 114 115 116 117 118  return Impl::accumulate(x, op); } /// Maximum over all vector entries template auto max(Dune::FieldVector const& x) {  Praetorius, Simon committed Dec 28, 2017 119  return Impl::accumulate(x, Operation::Max{});  Praetorius, Simon committed Nov 13, 2017 120 121 122 123 124 125  } /// Minimum over all vector entries template auto min(Dune::FieldVector const& x) {  Praetorius, Simon committed Dec 28, 2017 126  return Impl::accumulate(x, Operation::Min{});  Praetorius, Simon committed Nov 13, 2017 127 128 129 130 131 132  } /// Maximum of the absolute values of vector entries template auto abs_max(Dune::FieldVector const& x) {  Praetorius, Simon committed Dec 28, 2017 133  return Impl::accumulate(x, Operation::AbsMax{});  Praetorius, Simon committed Nov 13, 2017 134 135 136 137 138 139  } /// Minimum of the absolute values of vector entries template auto abs_min(Dune::FieldVector const& x) {  Praetorius, Simon committed Dec 28, 2017 140  return Impl::accumulate(x, Operation::AbsMin{});  Praetorius, Simon committed Nov 13, 2017 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  } // ---------------------------------------------------------------------------- /** \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)  Praetorius, Simon committed Dec 20, 2017 191  result += Math::sqr(lhs[i] - rhs[i]);  Praetorius, Simon committed Nov 13, 2017 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 270 271 272 273 274 275 276 277  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])); }  Praetorius, Simon committed Dec 20, 2017 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340  template Dune::FieldMatrix trans(Dune::FieldMatrix const& A) { Dune::FieldMatrix At; for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) At[j][i] = A[i][j]; return At; } template Dune::FieldMatrix multiplies(Dune::FieldMatrix const& A, Dune::FieldMatrix const& B) { return A.rightmultiplyany(B); } template Dune::FieldMatrix multiplies_AtB(Dune::FieldMatrix const& A, Dune::FieldMatrix const& B) { Dune::FieldMatrix C; for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { C[m][n] = 0; for (int l = 0; l < L; ++l) C[m][n] += A[l][m] * B[n][l]; } } return C; } template Dune::FieldMatrix multiplies_ABt(Dune::FieldMatrix const& A, Dune::FieldMatrix const& B) { Dune::FieldMatrix C; for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { C[m][n] = 0; for (int l = 0; l < L; ++l) C[m][n] += A[m][l] * B[n][l]; } } return C; } template Dune::FieldMatrix& multiplies_ABt(Dune::FieldMatrix const& A, Dune::FieldMatrix const& B, Dune::FieldMatrix& C) { for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { C[m][n] = 0; for (int l = 0; l < L; ++l) C[m][n] += A[m][l] * B[n][l]; } } return C; }  Praetorius, Simon committed Jan 20, 2018 341 342 343 344 345 346 347 348 349 350 351  template Dune::FieldMatrix& multiplies_ABt(Dune::FieldMatrix const& A, Dune::DiagonalMatrix const& B, Dune::FieldMatrix& C) { for (int m = 0; m < M; ++m) { for (int n = 0; n < N; ++n) { C[m][n] = A[m][n] * B.diagonal(n); } } return C; }  Praetorius, Simon committed Nov 13, 2017 352 } // end namespace AMDiS