RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
matrixNorm.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_MATRIX_NORM_HH
22 #define RFFGEN_LINEAR_ALGEBRA_MATRIX_NORM_HH
23 
24 #include <type_traits>
25 
26 #include "../LinearAlgebra/extractRowsAndCols.hh"
27 #include "../MathematicalOperations/chain.hh"
28 #include "../CMath/pow.hh"
29 
30 namespace RFFGen
31 {
35  namespace Concepts { template <class> struct MatrixConceptCheck; }
40  namespace LinearAlgebra
41  {
46  template <class Matrix, class = Concepts::MatrixConceptCheck<Matrix> >
48  {
50  SquaredMatrixNorm() = default;
51 
56  explicit SquaredMatrixNorm(const Matrix& A) { update(A); }
57 
59  void update(const Matrix& A)
60  {
61  A_ = A;
62  resultOfD0 = computeSquareNorm(A_);
63  }
64 
66  auto operator()() const noexcept
67  {
68  return d0();
69  }
70 
72  auto d0() const noexcept
73  {
74  return resultOfD0;
75  }
76 
78  template <int>
79  auto d1(const Matrix& dA) const
80  {
81  return 2 * computeScalarProduct(A_,dA);
82  }
83 
85  template <int,int>
86  auto d2(const Matrix& dA1, const Matrix& dA2) const
87  {
88  return 2 * computeScalarProduct(dA1,dA2);
89  }
90 
91 
92  private:
93  auto computeSquareNorm(const Matrix& A) const
94  {
95  using Index = decltype(numberOfRows<Matrix>());
96  auto result = decltype(at(A,0,0)){0.};
97  for( Index i = 0; i < numberOfRows<Matrix>() ; ++i )
98  for( Index j = 0 ; j < numberOfColumns<Matrix>() ; ++j )
99  result += at(A,i,j) * at(A,i,j);
100  return result;
101  }
102 
103  auto computeScalarProduct(const Matrix& A, const Matrix& B) const
104  {
105  using Index = decltype(numberOfRows<Matrix>());
106  auto result = decltype(at(A,0,0)){0.};
107  for( Index i = 0; i < numberOfRows<Matrix>() ; ++i )
108  for( Index j = 0 ; j < numberOfColumns<Matrix>() ; ++j )
109  result += at(A,i,j) * at(B,i,j);
110  return result;
111  }
112 
113  Matrix A_;
114  std::remove_const_t< std::remove_reference_t< at_t<Matrix> > > resultOfD0;
115  };
116 
121  template <class Matrix>
123  }
124 }
125 
126 #endif // RFFGEN_LINEAR_ALGEBRA_MATRIX_NORM_HH
auto d0() const noexcept
Squared matrix norm.
Definition: matrixNorm.hh:72
SquaredMatrixNorm(const Matrix &A)
Constructor.
Definition: matrixNorm.hh:56
SquaredMatrixNorm()=default
Default constructor.
void update(const Matrix &A)
Reset matrix to compute squared norm from.
Definition: matrixNorm.hh:59
Base class for functions satisfying FunctionConcept. Required for enabling the operators in generate...
Definition: base.hh:27
Chain of functions and of type F resp. G (F and G must satisfy the requirements of Concepts::Funct...
Definition: chain.hh:61
auto operator()() const noexcept
Squared matrix norm. Convenient access to d0().
Definition: matrixNorm.hh:66
auto d1(const Matrix &dA) const
First directional derivative.
Definition: matrixNorm.hh:79
auto d2(const Matrix &dA1, const Matrix &dA2) const
Second directional derivative.
Definition: matrixNorm.hh:86
Compute squared matrix norm .
Definition: matrixNorm.hh:47