FieldMatVec.hpp 8.39 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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace std
{
  template <class T, int N>
  struct common_type<Dune::FieldVector<T,N>, T>
  {
    using type = T;
  };

  template <class T, int N, int M>
  struct common_type<Dune::FieldMatrix<T,N,M>, T>
  {
    using type = T;
  };
}

24
namespace Dune
25
{
Praetorius, Simon's avatar
Praetorius, Simon committed
26
  // some arithmetic operations with FieldVector
27

28
29
30
  template <class T, int N>
  FieldVector<T,N> operator-(FieldVector<T,N> v);

31
  template <class T, int N, class S,
32
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
33
  FieldVector<T,N> operator*(FieldVector<T,N> v, S factor);
34

35
36
  template <class T, int N, class S,
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
37
  FieldVector<T,N> operator*(S factor, FieldVector<T,N> v);
38
39

  template <class T, int N, class S,
40
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
41
  FieldVector<T,N> operator/(FieldVector<T,N> v, S factor);
42

43
  template <class T>
44
  FieldVector<T,1> operator*(FieldVector<T,1> v, FieldVector<T,1> w);
45

46
47
48
49
  // ----------------------------------------------------------------------------

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

52
  /// Cross-product of two 3d-vectors
53
  template <class T>
54
  FieldVector<T, 3> cross(FieldVector<T, 3> const& a, FieldVector<T, 3> const& b);
55
56
57

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

60
61
62
  // template <class T, int N, int M,
  //   std::enable_if_t<( N!=1 && M!=1 ),int> = 0>
  // auto operator*(FieldVector<T,N> const& v, FieldVector<T,M> const& w);
63

64
65
66
  template <class T, class S, int N>
  auto dot(FieldMatrix<T,1,N> const& vec1, FieldMatrix<S,1,N> const& vec2);

67
68
69
70
  // ----------------------------------------------------------------------------

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

73
  template <class T, int N>
74
  T sum(FieldMatrix<T, 1, N> const& x);
75

76
77
78

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

81
  template <class T, int N>
82
  auto unary_dot(FieldMatrix<T, 1, N> const& x);
83

84
85
  /// Maximum over all vector entries
  template <class T, int N>
86
  auto max(FieldVector<T, N> const& x);
87

88
  template <class T, int N>
89
  auto max(FieldMatrix<T, 1, N> const& x);
90

91
92
  /// Minimum over all vector entries
  template <class T, int N>
93
  auto min(FieldVector<T, N> const& x);
94

95
  template <class T, int N>
96
  auto min(FieldMatrix<T, 1, N> const& x);
97

98
99
  /// Maximum of the absolute values of vector entries
  template <class T, int N>
100
  auto abs_max(FieldVector<T, N> const& x);
101

102
  template <class T, int N>
103
  auto abs_max(FieldMatrix<T, 1, N> const& x);
104

105
106
  /// Minimum of the absolute values of vector entries
  template <class T, int N>
107
  auto abs_min(FieldVector<T, N> const& x);
108

109
  template <class T, int N>
110
  auto abs_min(FieldMatrix<T, 1, N> const& x);
111

112
113
114
115
116
117
  // ----------------------------------------------------------------------------

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

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

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

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

132
133
134
135
  /** \ingroup vector_norms
   *  \brief The p-norm of a vector = ( sum_i |x_i|^p )^(1/p)
   **/
  template <int p, class T, int N>
136
  auto p_norm(FieldVector<T, N> const& x);
137

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

141
142
143
144
  /** \ingroup vector_norms
   *  \brief The infty-norm of a vector = max_i |x_i| = alias for \ref abs_max
   **/
  template <class T, int N>
145
  auto infty_norm(FieldVector<T, N> const& x);
146

147
  template <class T, int N>
148
  auto infty_norm(FieldMatrix<T, 1, N> const& x);
149

150
151
152
153
  // ----------------------------------------------------------------------------

  /// The euklidean distance between two vectors = |lhs-rhs|_2
  template <class T, int N>
154
  T distance(FieldVector<T, N> const& lhs, FieldVector<T, N> const& rhs);
155
156
157
158
159

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

  /// Outer product (vec1 * vec2^T)
  template <class T, class S, int N, int M, int K>
160
  auto outer(FieldMatrix<T,N,K> const& vec1, FieldMatrix<S,M,K> const& vec2);
161
162
163
164

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

  template <class T>
165
  T det(FieldMatrix<T, 0, 0> const& /*mat*/);
166
167
168

  /// Determinant of a 1x1 matrix
  template <class T>
169
  T det(FieldMatrix<T, 1, 1> const& mat);
170
171
172

  /// Determinant of a 2x2 matrix
  template <class T>
173
  T det(FieldMatrix<T, 2, 2> const& mat);
174
175
176

  /// Determinant of a 3x3 matrix
  template <class T>
177
  T det(FieldMatrix<T, 3, 3> const& mat);
178
179
180

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

183
184
185

  /// Return the inverse of the matrix `mat`
  template <class T, int N>
186
  auto inv(FieldMatrix<T, N, N> mat);
187
188
189

  /// Solve the linear system A*x = b
  template <class T, int N>
190
  void solve(FieldMatrix<T, N, N> const& A,  FieldVector<T, N>& x,  FieldVector<T, N> const& b);
191
192
193
194


  /// Gramian determinant = sqrt( det( DT^T * DF ) )
  template <class T, int N, int M>
195
  T gramian(FieldMatrix<T,N,M> const& DF);
196
197
198

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

201
202
203
  // ----------------------------------------------------------------------------
  // some arithmetic operations with FieldMatrix

204
  template <class T, int M, int N>
205
  FieldMatrix<T,N,M> trans(FieldMatrix<T, M, N> const& A);
206
207


208
  template <class T, int M, int N, class S,
209
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
210
  FieldMatrix<T,M,N> operator*(S scalar, FieldMatrix<T, M, N> A);
211

212
  template <class T, int M, int N, class S,
213
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
214
  FieldMatrix<T,M,N> operator*(FieldMatrix<T, M, N> A, S scalar);
215

216
217
  template <class T>
  FieldMatrix<T,1,1> operator*(FieldMatrix<T,1,1> lhs, FieldMatrix<T,1,1> rhs);
218
219


220
  template <class T, int M, int N, class S,
221
    std::enable_if_t<std::is_convertible<S,T>::value, int> = 0>
222
223
  FieldMatrix<T,M,N> operator/(FieldMatrix<T, M, N> A, S scalar);

224
225

  template <class T, int M, int N>
226
  FieldMatrix<T,M,N> operator+(FieldMatrix<T, M, N> A, FieldMatrix<T, M, N> const& B);
227
228

  template <class T, int M, int N>
229
  FieldMatrix<T,M,N> operator-(FieldMatrix<T, M, N> A, FieldMatrix<T, M, N> const& B);
230
231
232


  template <class T, int N, int M>
233
  FieldVector<T,N> operator*(FieldMatrix<T,N,M> const& mat, FieldVector<T,M> const& vec);
234
235
236



237
  template <class T, int M, int N, int L>
238
  FieldMatrix<T,M,N> multiplies(FieldMatrix<T, M, L> const& A,  FieldMatrix<T, L, N> const& B);
239
240
241
242



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

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

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

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

254

255
256
257
258
259
260
261
262
263
264
// -----------------------------------------------------------------------------

  template <class T>
  T operator*(FieldVector<T,1> lhs, FieldMatrix<T,1,1> rhs);

  template <class T>
  T operator*(FieldMatrix<T,1,1> lhs, FieldVector<T,1> rhs);

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

265
266
267
268
269
270
  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);

271
272
273
  template <class T>
  T const& at(FieldMatrix<T,1,1> const& vec, std::size_t i);

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

277
278
279
280
281
282
283
} // end namespace Dune

namespace AMDiS
{
  using Dune::FieldMatrix;
  using Dune::FieldVector;
}
284
285

#include "FieldMatVec.inc.hpp"