FieldMatVec.hpp 8.54 KB
Newer Older
1
2
#pragma once

3
4
#include <type_traits>

5
#include <dune/common/diagonalmatrix.hh>
6
7
8
#include <dune/common/fmatrix.hh>
#include <dune/common/fvector.hh>

9
namespace Dune
10
{
Praetorius, Simon's avatar
Praetorius, Simon committed
11
  // some arithmetic operations with FieldVector
12

13
14
15
  template <class T, int N>
  FieldVector<T,N> operator-(FieldVector<T,N> v);

16
  template <class T, int N, class S,
17
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
18
  FieldVector<T,N> operator*(FieldVector<T,N> v, S factor);
19
20

  template <class S, class T, int N,
21
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
22
  FieldVector<T,N> operator*(S factor, FieldVector<T,N> v);
23
24

  template <class T, int N, class S,
25
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
26
  FieldVector<T,N> operator/(FieldVector<T,N> v, S factor);
27

28
  template <class T>
29
  FieldVector<T,1> operator*(FieldVector<T,1> const& v, FieldVector<T,1> const& w);
30

31
  template <class T, int N>
32
  FieldVector<T,N> operator*(FieldVector<T,1> const& factor, FieldVector<T,N> v);
33

34
  template <class T, int N>
35
  FieldVector<T,N> operator*(FieldVector<T,N> v, FieldVector<T,1> const& factor);
36

37
38
39
40
  // ----------------------------------------------------------------------------

  /// Cross-product a 2d-vector = orthogonal vector
  template <class T>
41
  FieldVector<T, 2> cross(FieldVector<T, 2> const& a);
42

43
  /// Cross-product of two 3d-vectors
44
  template <class T>
45
  FieldVector<T, 3> cross(FieldVector<T, 3> const& a, FieldVector<T, 3> const& b);
46
47
48

  /// Dot product (vec1^T * vec2)
  template <class T, class S, int N>
49
  auto dot(FieldVector<T,N> const& vec1, FieldVector<S,N> const& vec2);
50

51
  template <class T, int N, int M,
52
    std::enable_if_t<( N!=1 && M!=1 ),int> = 0>
53
  auto operator*(FieldVector<T,N> const& v, FieldVector<T,M> const& w);
54

55
56
57
  template <class T, class S, int N>
  auto dot(FieldMatrix<T,1,N> const& vec1, FieldMatrix<S,1,N> const& vec2);

58
59
60
61
  // ----------------------------------------------------------------------------

  /// Sum of vector entires.
  template <class T, int N>
62
  T sum(FieldVector<T, N> const& x);
63

64
  template <class T, int N>
65
  T sum(FieldMatrix<T, 1, N> const& x);
66

67
68
69

  /// Dot-product with the vector itself
  template <class T, int N>
70
  auto unary_dot(FieldVector<T, N> const& x);
71

72
  template <class T, int N>
73
  auto unary_dot(FieldMatrix<T, 1, N> const& x);
74

75
76
  /// Maximum over all vector entries
  template <class T, int N>
77
  auto max(FieldVector<T, N> const& x);
78

79
  template <class T, int N>
80
  auto max(FieldMatrix<T, 1, N> const& x);
81

82
83
  /// Minimum over all vector entries
  template <class T, int N>
84
  auto min(FieldVector<T, N> const& x);
85

86
  template <class T, int N>
87
  auto min(FieldMatrix<T, 1, N> const& x);
88

89
90
  /// Maximum of the absolute values of vector entries
  template <class T, int N>
91
  auto abs_max(FieldVector<T, N> const& x);
92

93
  template <class T, int N>
94
  auto abs_max(FieldMatrix<T, 1, N> const& x);
95

96
97
  /// Minimum of the absolute values of vector entries
  template <class T, int N>
98
  auto abs_min(FieldVector<T, N> const& x);
99

100
  template <class T, int N>
101
  auto abs_min(FieldMatrix<T, 1, N> const& x);
102

103
104
105
106
107
108
  // ----------------------------------------------------------------------------

  /** \ingroup vector_norms
   *  \brief The 1-norm of a vector = sum_i |x_i|
   **/
  template <class T, int N>
109
  auto one_norm(FieldVector<T, N> const& x);
110

111
  template <class T, int N>
112
  auto one_norm(FieldMatrix<T, 1, N> const& x);
113

114
115
116
117
  /** \ingroup vector_norms
   *  \brief The euklidean 2-norm of a vector = sqrt( sum_i |x_i|^2 )
   **/
  template <class T, int N>
118
  auto two_norm(FieldVector<T, N> const& x);
119

120
  template <class T, int N>
121
  auto two_norm(FieldMatrix<T, 1, N> const& x);
122

123
124
125
126
  /** \ingroup vector_norms
   *  \brief The p-norm of a vector = ( sum_i |x_i|^p )^(1/p)
   **/
  template <int p, class T, int N>
127
  auto p_norm(FieldVector<T, N> const& x);
128

129
  template <int p, class T, int N>
130
  auto p_norm(FieldMatrix<T, 1, N> const& x);
131

132
133
134
135
  /** \ingroup vector_norms
   *  \brief The infty-norm of a vector = max_i |x_i| = alias for \ref abs_max
   **/
  template <class T, int N>
136
  auto infty_norm(FieldVector<T, N> const& x);
137

138
  template <class T, int N>
139
  auto infty_norm(FieldMatrix<T, 1, N> const& x);
140

141
142
143
144
  // ----------------------------------------------------------------------------

  /// The euklidean distance between two vectors = |lhs-rhs|_2
  template <class T, int N>
145
  T distance(FieldVector<T, N> const& lhs, FieldVector<T, N> const& rhs);
146
147
148
149
150

  // ----------------------------------------------------------------------------

  /// Outer product (vec1 * vec2^T)
  template <class T, class S, int N, int M, int K>
151
  auto outer(FieldMatrix<T,N,K> const& vec1, FieldMatrix<S,M,K> const& vec2);
152
153
154
155

  // ----------------------------------------------------------------------------

  template <class T>
156
  T det(FieldMatrix<T, 0, 0> const& /*mat*/);
157
158
159

  /// Determinant of a 1x1 matrix
  template <class T>
160
  T det(FieldMatrix<T, 1, 1> const& mat);
161
162
163

  /// Determinant of a 2x2 matrix
  template <class T>
164
  T det(FieldMatrix<T, 2, 2> const& mat);
165
166
167

  /// Determinant of a 3x3 matrix
  template <class T>
168
  T det(FieldMatrix<T, 3, 3> const& mat);
169
170
171

  /// Determinant of a NxN matrix
  template <class T,  int N>
172
173
  T det(FieldMatrix<T, N, N> const& mat);

174
175
176

  /// Return the inverse of the matrix `mat`
  template <class T, int N>
177
  auto inv(FieldMatrix<T, N, N> mat);
178
179
180

  /// Solve the linear system A*x = b
  template <class T, int N>
181
  void solve(FieldMatrix<T, N, N> const& A,  FieldVector<T, N>& x,  FieldVector<T, N> const& b);
182
183
184
185


  /// Gramian determinant = sqrt( det( DT^T * DF ) )
  template <class T, int N, int M>
186
  T gramian(FieldMatrix<T,N,M> const& DF);
187
188
189

  /// Gramian determinant, specialization for 1 column matrices
  template <class T, int M>
190
  T gramian(FieldMatrix<T, 1, M> const& DF);
191

192
193
194
  // ----------------------------------------------------------------------------
  // some arithmetic operations with FieldMatrix

195
  template <class T, int M, int N>
196
  FieldMatrix<T,N,M> trans(FieldMatrix<T, M, N> const& A);
197
198


199
  template <class T, int M, int N, class S,
200
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
201
  FieldMatrix<T,M,N> operator*(S scalar, FieldMatrix<T, M, N> A);
202

203
  template <class T, int M, int N, class S,
204
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
205
  FieldMatrix<T,M,N> operator*(FieldMatrix<T, M, N> A, S scalar);
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  template <class T, int M, int N >
  FieldMatrix<T,M,N> operator*(FieldMatrix<T,1,1> scalar, FieldMatrix<T, M, N> A);

  template <class T, int M, int N >
  FieldMatrix<T,M,N> operator*(FieldMatrix<T, M, N> A, FieldMatrix<T,1,1> scalar);

  template <class T, int N, int M>
  FieldMatrix<T,N,M> operator*(FieldVector<T,1> scalar, FieldMatrix<T,N,M> mat);

  template <class T, int N>
  FieldMatrix<T,N,1> operator*(FieldVector<T,1> scalar, FieldMatrix<T,N,1> mat);

  template <class T, int N, int M>
  FieldMatrix<T,N,M> operator*(FieldMatrix<T,N,M> mat, FieldVector<T,1> scalar);

  template <class T, int N>
  FieldMatrix<T,N,1> operator*(FieldMatrix<T,N,1> mat, FieldVector<T,1> scalar);


226
  template <class T, int M, int N, class S,
227
    std::enable_if_t<std::is_arithmetic<S>::value,int> = 0 >
228
229
  FieldMatrix<T,M,N> operator/(FieldMatrix<T, M, N> A, S scalar);

230
231

  template <class T, int M, int N>
232
  FieldMatrix<T,M,N> operator+(FieldMatrix<T, M, N> A, FieldMatrix<T, M, N> const& B);
233
234

  template <class T, int M, int N>
235
  FieldMatrix<T,M,N> operator-(FieldMatrix<T, M, N> A, FieldMatrix<T, M, N> const& B);
236
237
238


  template <class T, int N, int M>
239
  FieldVector<T,N> operator*(FieldMatrix<T,N,M> const& mat, FieldVector<T,M> const& vec);
240
241
242



243
  template <class T, int M, int N, int L>
244
  FieldMatrix<T,M,N> multiplies(FieldMatrix<T, M, L> const& A,  FieldMatrix<T, L, N> const& B);
245
246
247
248



  template <class T, int M, int N, int L>
249
  FieldMatrix<T,M,N> multiplies_AtB(FieldMatrix<T, L, M> const& A,  FieldMatrix<T, N, L> const& B);
250
251

  template <class T, int M, int N, int L>
252
  FieldMatrix<T,M,N> multiplies_ABt(FieldMatrix<T, M, L> const& A,  FieldMatrix<T, N, L> const& B);
253
254

  template <class T, int M, int N, int L>
255
  FieldMatrix<T,M,N>& multiplies_ABt(FieldMatrix<T, M, L> const& A,  FieldMatrix<T, N, L> const& B, FieldMatrix<T,M,N>& C);
256

257
  template <class T, int M, int N>
258
  FieldMatrix<T,M,N>& multiplies_ABt(FieldMatrix<T, M, N> const& A,  DiagonalMatrix<T, N> const& B, FieldMatrix<T,M,N>& C);
259

260
261
262
263
264
265
266
267
268
269

  template <class T, int N>
  T const& at(FieldMatrix<T,N,1> const& vec, std::size_t i);

  template <class T, int M>
  T const& at(FieldMatrix<T,1,M> const& vec, std::size_t i);

  template <class T, int N>
  T const& at(FieldVector<T,N> const& vec, std::size_t i);

270
271
272
273
274
275
276
} // end namespace Dune

namespace AMDiS
{
  using Dune::FieldMatrix;
  using Dune::FieldVector;
}
277
278

#include "FieldMatVec.inc.hpp"