11#include <dune/common/fmatrix.hh>
12#include <boost/static_assert.hpp>
17namespace DeterminantDetail {
19 template <
typename MatrixType>
20 Dune::FieldMatrix<double,MatrixType::rows-1,MatrixType::cols-1> getMinor(MatrixType
const& matrix,
int rowIndex,
int colIndex)
22 BOOST_STATIC_ASSERT( (MatrixType::rows > 1) && (MatrixType::cols > 1) );
24 for(
int ii=0, iii=0; ii<MatrixType::rows; ++ii)
28 for(
int kk=0, kkk=0; kk<MatrixType::cols; ++kk)
32 subMatrix[iii][kkk]=matrix[ii][kk];
61template <
int dim,
class Source=Dune::FieldMatrix<
double,dim,dim> >
64template <
class Source>
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]); }
88 double d0()
const {
return matrix()[0][0]*matrix()[1][1] - matrix()[0][1]*matrix()[1][0]; }
90 double d1(
MatrixType const& dA)
const {
return composeResult(matrix(),dA); }
102template <
class Source>
112 template<
int a1,
int a2,
int b1,
int b2,
int c1,
int c2>
double tp(
MatrixType const& t)
const
114 return t[a1][a2]*t[b1][b2]*t[c1][c2];
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
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];
122 template<
int a1,
int a2,
int b1,
int b2,
int c1,
int c2>
double dtp(MatrixType
const& t, MatrixType
const& dv)
const
124 return circular<a1,a2,b1,b2,c1,c2>(t,t,dv);
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
129 return circular<a1,a2,b1,b2,c1,c2>(t,dv,dw)+circular<a1,a2,b1,b2,c1,c2>(t,dw,dv);
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
134 return circular<a1,a2,b1,b2,c1,c2>(du,dv,dw)+circular<a1,a2,b1,b2,c1,c2>(du,dw,dv);
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());
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);
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);
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);
181template <
int dim,
class Source=Dune::FieldMatrix<
double,dim,dim> >
class Adjugate;
183template <
int dim,
class Source=Dune::FieldMatrix<
double,dim,dim> >
class Cofactor;
185template <
class Source>
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];
199 explicit Adjugate(Source &s) : matrix(s), result(0){}
203 composeResult(matrix());
222 mutable MatrixType result;
228template <
class Source>
243 for(
int i=0; i<dim; ++i)
244 for(
int j=0; j<dim; ++j)
246 SubMatrixType tmp = DeterminantDetail::getMinor(matrix(),i,j);
248 if((i+j)%2)
sign = -1;
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];
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;
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];
293 mutable MatrixType result;
303template <
int dimension,
class Source>
323 {
return transpose( adj.d3(dv,dw,dx) ); }
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 operator()() const
Determinant(Source &s)
Constructor.
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 operator()() const
double d1(MatrixType const &dv) const
Determinant(Source &s)
Constructor.
A class for computing determinants and their derivatives.
This file contains various utility functions that augment the basic functionality of Dune.