KASKADE 7 development version
utilities/linalg/determinant.hh
Go to the documentation of this file.
1/*
2 * determinant.hh
3 *
4 * Created on: Sep 21, 2011
5 * Author: bzflubko
6 */
7
8#ifndef DETERMINANT_HH_
9#define DETERMINANT_HH_
10
11#include <dune/common/fmatrix.hh>
12#include <boost/static_assert.hpp>
13#include "utilities/get.hh"
14#include "fem/fixdune.hh"
15
17namespace DeterminantDetail {
18
19 template <typename MatrixType>
20 Dune::FieldMatrix<double,MatrixType::rows-1,MatrixType::cols-1> getMinor(MatrixType const& matrix, int rowIndex, int colIndex)
21 {
22 BOOST_STATIC_ASSERT( (MatrixType::rows > 1) && (MatrixType::cols > 1) );
23 Dune::FieldMatrix<double,MatrixType::rows-1,MatrixType::cols-1> subMatrix(0);
24 for(int ii=0, iii=0; ii<MatrixType::rows; ++ii)
25 {
26 if(ii!=rowIndex)
27 {
28 for(int kk=0, kkk=0; kk<MatrixType::cols; ++kk)
29 {
30 if(kk!=colIndex)
31 {
32 subMatrix[iii][kkk]=matrix[ii][kk];
33 ++kkk;
34 }
35 }
36 ++iii;
37 }
38 }
39
40 return subMatrix;
41 }
42
43}
45
46namespace Kaskade {
47
48/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49/* DETERMINANT */
50/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51
52
61template <int dim, class Source=Dune::FieldMatrix<double,dim,dim> >
62class Determinant;
63
64template <class Source>
65class Determinant<2,Source>
66{
67public:
68 typedef Source source_type;
70
71private:
73
74 double composeResult(MatrixType const& m1, MatrixType const& m2) const { return m1[0][0]*m2[1][1] + m1[1][1]*m2[0][0] - (m1[0][1]*m2[1][0] + m1[1][0]*m2[0][1]); }
75
76public:
84 explicit Determinant(Source& s) : matrix(s){}
85
86 double operator()() const { return d0(); }
87
88 double d0() const { return matrix()[0][0]*matrix()[1][1] - matrix()[0][1]*matrix()[1][0]; }
89
90 double d1(MatrixType const& dA) const { return composeResult(matrix(),dA); }
91
92 double d2(MatrixType const& dA, MatrixType const& dB) const { return composeResult(dB,dA); }
93
94 double d3(MatrixType const&, MatrixType const&, MatrixType const&) const { return 0; }
95
96private:
97 GetMatrix matrix;
98 //MatrixType &matrix;
99};
100
102template <class Source>
103class Determinant<3,Source>
104{
105public:
106 typedef Source source_type;
108
109private:
111
112 template<int a1, int a2, int b1, int b2, int c1, int c2> double tp(MatrixType const& t) const
113 {
114 return t[a1][a2]*t[b1][b2]*t[c1][c2];
115 }
116
117 template<int a1, int a2, int b1, int b2, int c1, int c2> double circular(MatrixType const& t, MatrixType const& u, MatrixType const& v) const
118 {
119 return t[a1][a2]*u[b1][b2]*v[c1][c2]+v[a1][a2]*t[b1][b2]*u[c1][c2]+u[a1][a2]*v[b1][b2]*t[c1][c2];
120 }
121
122 template<int a1, int a2, int b1, int b2, int c1, int c2> double dtp(MatrixType const& t, MatrixType const& dv) const
123 {
124 return circular<a1,a2,b1,b2,c1,c2>(t,t,dv);
125 }
126
127 template<int a1, int a2, int b1, int b2, int c1, int c2> double ddtp(MatrixType const& t, MatrixType const& dv, MatrixType const& dw) const
128 {
129 return circular<a1,a2,b1,b2,c1,c2>(t,dv,dw)+circular<a1,a2,b1,b2,c1,c2>(t,dw,dv);
130 }
131
132 template<int a1, int a2, int b1, int b2, int c1, int c2> double dddtp(MatrixType const& du, MatrixType const& dv, MatrixType const& dw) const
133 {
134 return circular<a1,a2,b1,b2,c1,c2>(du,dv,dw)+circular<a1,a2,b1,b2,c1,c2>(du,dw,dv);
135 }
136
137public:
145 explicit Determinant(Source &s): matrix(s){}
146
147 double operator()() const { return d0(); }
148
149 double d0() const
150 {
151 return tp<0,0,1,1,2,2>(matrix())+tp<0,1,1,2,2,0>(matrix())+tp<0,2,1,0,2,1>(matrix())
152 - tp<2,0,1,1,0,2>(matrix())-tp<2,1,1,2,0,0>(matrix())-tp<2,2,1,0,0,1>(matrix());
153 }
154
155 double d1(MatrixType const& dv) const
156 {
157 return dtp<0,0,1,1,2,2>(matrix(),dv)+dtp<0,1,1,2,2,0>(matrix(),dv)+dtp<0,2,1,0,2,1>(matrix(),dv)
158 -dtp<2,0,1,1,0,2>(matrix(),dv)-dtp<2,1,1,2,0,0>(matrix(),dv)-dtp<2,2,1,0,0,1>(matrix(),dv);
159 }
160
161 double d2(MatrixType const& dv, MatrixType const& dw) const
162 {
163 return ddtp<0,0,1,1,2,2>(matrix(),dv,dw)+ddtp<0,1,1,2,2,0>(matrix(),dv,dw)+ddtp<0,2,1,0,2,1>(matrix(),dv,dw)
164 - ddtp<2,0,1,1,0,2>(matrix(),dv,dw)-ddtp<2,1,1,2,0,0>(matrix(),dv,dw)-ddtp<2,2,1,0,0,1>(matrix(),dv,dw);
165 }
166
167 double d3(MatrixType const& du, MatrixType const& dv, MatrixType const& dw) const
168 {
169 return dddtp<0,0,1,1,2,2>(du,dv,dw)+dddtp<0,1,1,2,2,0>(du,dv,dw)+dddtp<0,2,1,0,2,1>(du,dv,dw)
170 - dddtp<2,0,1,1,0,2>(du,dv,dw)-dddtp<2,1,1,2,0,0>(du,dv,dw)-dddtp<2,2,1,0,0,1>(du,dv,dw);
171 }
172
173private:
174 GetMatrix matrix;
175// MatrixType &matrix;
176};
177
178/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
179/* ADJUGATE */
180/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
181template <int dim, class Source=Dune::FieldMatrix<double,dim,dim> > class Adjugate;
182
183template <int dim, class Source=Dune::FieldMatrix<double,dim,dim> > class Cofactor;
184
185template <class Source>
186class Adjugate<2, Source>{
187public:
189private:
191
192 void composeResult(MatrixType const& m) const
193 {
194 result[0][0] = m[1][1]; result[0][1] = -m[1][0];
195 result[1][0] = -m[0][1]; result[1][1] = m[0][0];
196 }
197
198public:
199 explicit Adjugate(Source &s) : matrix(s), result(0){}
200
202 {
203 composeResult(matrix());
204 return result;
205 }
206
207 MatrixType d1(MatrixType const& dA) const
208 {
209 composeResult(dA);
210 return result;
211 }
212
213 MatrixType d2(MatrixType const&, MatrixType const&) const { return 0.0*result; }
214
215 MatrixType d3(MatrixType const&, MatrixType const&, MatrixType const&) { return 0.0*result; }
216
217 //Cofactor<2,Source> transpose() const { return Cofactor<2,Source>(matrix); }
218
219private:
220 GetMatrix matrix;
221// MatrixType &matrix;
222 mutable MatrixType result;
223};
224
228template <class Source>
229class Adjugate<3,Source>
230{
231public:
232 enum{ dim=3 };
235private:
237
238public:
239 explicit Adjugate(Source &s) :matrix(s){}
240
242 {
243 for(int i=0; i<dim; ++i)
244 for(int j=0; j<dim; ++j)
245 {
246 SubMatrixType tmp = DeterminantDetail::getMinor(matrix(),i,j);
247 int sign = 1;
248 if((i+j)%2) sign = -1;
249 result[j][i] = sign*Determinant<dim-1,SubMatrixType>(tmp).d0();
250 }
251 return result;
252 }
253
254 MatrixType d1(MatrixType const& dv) const{
255 for(int i=0; i<dim; ++i){
256 for(int j=0; j<dim; ++j){
257 int i1 = (i+1)%dim; int i2 = (i+2)%dim;
258 int j1 = (j+1)%dim; int j2 = (j+2)%dim;
259 result[j][i] = matrix()[i1][j1]*dv[i2][j2] + matrix()[i2][j2]*dv[i1][j1] -
260 matrix()[i1][j2]*dv[i2][j1] - matrix()[i2][j1]*dv[i1][j2];
261 //result[j][i] *= ((i+j)%2==0) ? 1. : -1.;
262 }
263 }
264 return result;
265 }
266
267 MatrixType d2(MatrixType const& dv, MatrixType const& dw) const {
268 result = MatrixType(0);
269 for(int i=0; i<dim; ++i){
270 for(int j=0; j<dim; ++j){
271 int i1 = (i+1)%dim; int i2 = (i+2)%dim;
272 int j1 = (j+1)%dim; int j2 = (j+2)%dim;
273
274 result[j][i] += dv[i2][j2]*dw[i1][j1] + dv[i1][j1]*dw[i2][j2];
275 result[j][i] -= dv[i2][j1]*dw[i1][j2] + dv[i1][j2]*dw[i2][j1];
276 //result[j][i] *= ((i+j)%2==0) ? 1. : -1.;
277 }
278 }
279
280 return result;
281 }
282
283 MatrixType d3(MatrixType const&, MatrixType const&, MatrixType const&) const
284 {
285 return 0.0*matrix();
286 }
287
288 //Cofactor<3,Source> transpose() const { return Cofactor<3,Source>(matrix); }
289
290private:
291 GetMatrix matrix;
292// MatrixType &matrix;
293 mutable MatrixType result;
294};
295
296
297/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
298/* Cofactor matrix */
299/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
303template <int dimension, class Source>
304class Cofactor{
305public:
306 enum{ dim=dimension };
309private:
311
312public:
313 explicit Cofactor(Source& s) : adj(s){}
314
315 MatrixType const d0() const { return transpose( adj.d0() ); }
316
317 MatrixType const d1(MatrixType const& dv) const { return transpose( adj.d1(dv) ); }
318
319 MatrixType const d2(MatrixType const& dv, MatrixType const& dw) const
320 { return transpose( adj.d2(dv,dw) ); }
321
322 MatrixType const d3(MatrixType const& dv, MatrixType const& dw, MatrixType const& dx) const
323 { return transpose( adj.d3(dv,dw,dx) ); }
324
325 //Adjugate<dim,Source> transpose() const { return adj; }
326
327private:
328 Adjugate<dim,Source> const adj;
329};
330
331} // end of namespace Kaskade
332
333#endif /* DETERMINANT_HH_ */
MatrixType d2(MatrixType const &, MatrixType const &) const
MatrixType d1(MatrixType const &dA) const
Dune::FieldMatrix< double, 2, 2 > MatrixType
MatrixType d3(MatrixType const &, MatrixType const &, MatrixType const &)
MatrixType d1(MatrixType const &dv) const
Dune::FieldMatrix< double, dim, dim > MatrixType
GetSubType< double, dim, MatrixType >::type SubMatrixType
MatrixType d2(MatrixType const &dv, MatrixType const &dw) const
MatrixType d3(MatrixType const &, MatrixType const &, MatrixType const &) const
MatrixType const d1(MatrixType const &dv) const
Dune::FieldMatrix< double, dim, dim > MatrixType
MatrixType const d3(MatrixType const &dv, MatrixType const &dw, MatrixType const &dx) const
MatrixType const d0() const
MatrixType const d2(MatrixType const &dv, MatrixType const &dw) const
Dune::FieldMatrix< double, dim-1, dim-1 > SubMatrixType
double d3(MatrixType const &, MatrixType const &, MatrixType const &) const
double d1(MatrixType const &dA) const
Dune::FieldMatrix< double, 2, 2 > MatrixType
double d2(MatrixType const &dA, MatrixType const &dB) const
double d2(MatrixType const &dv, MatrixType const &dw) const
Dune::FieldMatrix< double, 3, 3 > MatrixType
double d3(MatrixType const &du, MatrixType const &dv, MatrixType const &dw) const
double d1(MatrixType const &dv) const
A class for computing determinants and their derivatives.
This file contains various utility functions that augment the basic functionality of Dune.
T transpose(T x)
T sign(T x)
Definition: sign.hh:21
Type::SubType type
Definition: get.hh:61