RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
principalInvariants.hh
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the C++-library RFFGen. */
4 /* Copyright 2015 Lars Lubkoll */
5 /* */
6 /* RFFGen is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* RFFGen is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with RFFGen. If not, see <http://www.gnu.org/licenses/>. */
18 /* */
19 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20 
21 #ifndef RFFGEN_LINEAR_ALGEBRA_PRINCIPAL_INVARIANTS_HH
22 #define RFFGEN_LINEAR_ALGEBRA_PRINCIPAL_INVARIANTS_HH
23 
24 #include <type_traits>
25 
26 #include "dimension.hh"
27 #include "trace.hh"
28 #include "cofactor.hh"
29 #include "determinant.hh"
30 #include "shiftedInvariant.hh"
31 #include "../Util/base.hh"
32 
33 namespace RFFGen
34 {
38  namespace Concepts { template <class> struct SymmetricMatrixConceptCheck; }
43  namespace LinearAlgebra
44  {
48  namespace Detail
49  {
50  template <class Matrix>
51  auto computeCofactors(const Matrix& A, std::integral_constant<int,2>)
52  {
53  return computeCofactor<0,0>(A) + computeCofactor<1,1>(A);
54  }
55 
56  template <class Matrix>
57  auto computeCofactors(const Matrix& A, std::integral_constant<int,3>)
58  {
59  return computeCofactor<0,0>(A) + computeCofactor<1,1>(A) + computeCofactor<2,2>(A);
60  }
61 
62  template < class Matrix >
63  auto computeCofactors(const Matrix& A, std::integral_constant<int,-1>)
64  {
65  if(rows(A) == 2) return computeCofactors(A,std::integral_constant<int,2>());
66  /*if(rows(A) == 3)*/ return computeCofactors(A,std::integral_constant<int,3>());
67  }
68 
69  template <int row, class Matrix>
70  auto symmetricCofactorDerivative(const Matrix& A, const Matrix& dA)
71  {
72  return computeCofactorDirectionalDerivative<row,row>(A,dA) + computeCofactorDirectionalDerivative<row,row>(dA,A);
73  }
74 
75 
76  template <class Matrix>
77  auto computeSymmetricCofactorDerivatives(const Matrix& A, const Matrix& B, std::integral_constant<int,2>)
78  {
79  return symmetricCofactorDerivative<0>(A,B) + symmetricCofactorDerivative<1>(A,B);
80  }
81 
82  template <class Matrix>
83  auto computeSymmetricCofactorDerivatives(const Matrix& A, const Matrix& B, std::integral_constant<int,3>)
84  {
85  return symmetricCofactorDerivative<0>(A,B) + symmetricCofactorDerivative<1>(A,B) + symmetricCofactorDerivative<2>(A,B);
86  }
87 
88 
89  template < class Matrix >
90  auto computeSymmetricCofactorDerivatives(const Matrix& A, const Matrix& B, std::integral_constant<int,-1>)
91  {
92  if(rows(A) == 2) return computeSymmetricCofactorDerivatives(A,B,std::integral_constant<int,2>());
93  /*if(rows(A) == 3)*/ return computeSymmetricCofactorDerivatives(A,B,std::integral_constant<int,3>());
94 
95  }
96  }
106  template <class Matrix>
108 
113  template <class Matrix, class = Concepts::SymmetricMatrixConceptCheck<Matrix> >
115  {
116  using DimIdentifier = std::integral_constant<int,dimension<Matrix>()>;
117  public:
119  SecondPrincipalInvariant() = default;
120 
125  SecondPrincipalInvariant(const Matrix& A) { update(A); }
126 
128  void update(const Matrix& A)
129  {
130  A_ = A;
131  resultOfD0 = Detail::computeCofactors(A,dimIdentifier);
132  }
133 
135  auto d0() const
136  {
137  return resultOfD0;
138  }
139 
144  template <int,int>
145  auto d1(const Matrix& dA1) const
146  {
147  return Detail::computeSymmetricCofactorDerivatives(A_,dA1,dimIdentifier);
148  }
149 
155  template <int,int>
156  auto d2(const Matrix& dA1, const Matrix& dA2) const
157  {
158  return Detail::computeSymmetricCofactorDerivatives(dA1,dA2,dimIdentifier);
159  }
160 
161  private:
162  Matrix A_;
163  std::decay_t< at_t<Matrix> > resultOfD0 = 0.;
164  DimIdentifier dimIdentifier;
165  };
166 
171  template <class Matrix>
173 
174 
179  template < class Matrix , int offset = dimension<Matrix>() >
181 
186  template < class Matrix , int offset = dimension<Matrix>() >
188 
193  template < class Matrix >
195  }
196 }
197 
198 #endif // RFFGEN_LINEAR_ALGEBRA_PRINCIPAL_INVARIANTS_HH
auto d2(const Matrix &dA1, const Matrix &dA2) const
Second directional derivative.
Definition: principalInvariants.hh:156
void update(const Matrix &A)
Reset matrix to compute second principal invariant from.
Definition: principalInvariants.hh:128
SecondPrincipalInvariant(const Matrix &A)
Constructor.
Definition: principalInvariants.hh:125
auto d0() const
Value of the second principal invariant.
Definition: principalInvariants.hh:135
Base class for functions satisfying FunctionConcept. Required for enabling the operators in generate...
Definition: base.hh:27
Possibly scaled, shifted invariant , where for the first two (principal,modified) invariants and fo...
Definition: shiftedInvariant.hh:43
SecondPrincipalInvariant()=default
Default constructor.
Second principal invariant for .
Definition: principalInvariants.hh:114
Determinant< Matrix > ThirdPrincipalInvariant
Third principal invariant for .
Definition: principalInvariants.hh:172
auto d1(const Matrix &dA1) const
First directional derivative.
Definition: principalInvariants.hh:145
std::conditional_t< Checks::isConstantSizeMatrix< Matrix >(), ConstantSizeTrace< Matrix >, DynamicSizeTrace< Matrix > > Trace
Trace of a matrix (sum of diagonal elements).
Definition: trace.hh:213
std::conditional_t< Checks::isConstantSizeMatrix< Matrix >(), ConstantSizeDeterminant< Matrix >, DynamicSizeDeterminant< Matrix > > Determinant
Determinant with first three derivatives.
Definition: determinant.hh:230
Trace< Matrix > FirstPrincipalInvariant
First principal invariant for .
Definition: principalInvariants.hh:107