RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
euclideanNorm.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_EUCLIDEAN_NORM_HH
22 #define RFFGEN_LINEAR_ALGEBRA_EUCLIDEAN_NORM_HH
23 
24 #include <type_traits>
25 
26 #include "../LinearAlgebra/extractRowsAndCols.hh"
27 #include "../MathematicalOperations/chain.hh"
28 #include "../CMath/pow.hh"
29 #include "../Util/staticChecks.hh"
30 
31 namespace RFFGen
32 {
36  namespace Concepts { template <class> struct MatrixConceptCheck; }
41  namespace LinearAlgebra
42  {
46  namespace Detail
47  {
48  template < class Matrix , std::enable_if_t<Checks::isConstantSizeMatrix<Matrix>()> >
49  inline auto computeSquareEuclideanNorm(const Matrix& A)
50  {
51  using Index = decltype(numberOfRows<Matrix>());
52  auto result = decltype(at(A,0,0)){0.};
53  for( Index i = 0; i < numberOfRows<Matrix>() ; ++i )
54  for( Index j = 0 ; j < numberOfColumns<Matrix>() ; ++j )
55  result += at(A,i,j) * at(A,i,j);
56  return result;
57  }
58 
59  template < class Vector , std::enable_if_t<Checks::isStaticVector<Vector>() && !Checks::isConstantSizeMatrix<Vector>()> >
60  inline auto computeSquareEuclideanNorm(const Vector& A)
61  {
62  using Index = decltype(numberOfRows<Vector>());
63  auto result = decltype(at(A,0)){0.};
64  for( Index i = 0; i < numberOfRows<Vector>() ; ++i )
65  result += at(A,i) * at(A,i);
66  return result;
67  }
68 
69  template < class Matrix , std::enable_if_t<Checks::isConstantSizeMatrix<Matrix>()> >
70  inline auto computeEuclideanScalarProduct(const Matrix& A, const Matrix& B)
71  {
72  using Index = decltype(numberOfRows<Matrix>());
73  auto result = decltype(at(A,0,0)){0.};
74  for( Index i = 0; i < numberOfRows<Matrix>() ; ++i )
75  for( Index j = 0 ; j < numberOfColumns<Matrix>() ; ++j )
76  result += at(A,i,j) * at(B,i,j);
77  return result;
78  }
79 
80  template < class Vector , std::enable_if_t<Checks::isStaticVector<Vector>() && !Checks::isConstantSizeMatrix<Vector>()> >
81  inline auto computeEuclideanScalarProduct(const Vector& v, const Vector& w)
82  {
83  using Index = decltype(numberOfRows<Vector>());
84  auto result = at(v,0) * at(w,1);
85  for( Index i = 1; i < numberOfRows<Vector>() ; ++i )
86  result += at(v,i) * at(w,i);
87  return result;
88  }
89  }
99  template <class Matrix, class = Concepts::MatrixConceptCheck<Matrix> >
101  {
103  SquaredEuclideanNorm() = default;
104 
109  explicit SquaredEuclideanNorm(const Matrix& A) { update(A); }
110 
112  void update(const Matrix& A)
113  {
114  A_ = A;
115  resultOfD0 = computeSquareNorm(A_);
116  }
117 
119  auto operator()() const noexcept
120  {
121  return d0();
122  }
123 
125  auto d0() const noexcept
126  {
127  return resultOfD0;
128  }
129 
131  template <int>
132  auto d1(const Matrix& dA) const
133  {
134  return 2 * computeScalarProduct(A_,dA);
135  }
136 
138  template <int,int>
139  auto d2(const Matrix& dA1, const Matrix& dA2) const
140  {
141  return 2 * computeScalarProduct(dA1,dA2);
142  }
143 
144 
145  private:
146  Matrix A_;
147  std::remove_const_t< std::remove_reference_t< at_t<Matrix> > > resultOfD0;
148  };
149 
154  template <class Matrix>
156  }
157 }
158 
159 #endif // RFFGEN_LINEAR_ALGEBRA_EUCLIDEAN_NORM_HH
auto d0() const noexcept
Squared matrix norm.
Definition: euclideanNorm.hh:125
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 d2(const Matrix &dA1, const Matrix &dA2) const
Second directional derivative.
Definition: euclideanNorm.hh:139
void update(const Matrix &A)
Reset matrix to compute squared norm from.
Definition: euclideanNorm.hh:112
auto d1(const Matrix &dA) const
First directional derivative.
Definition: euclideanNorm.hh:132
SquaredEuclideanNorm(const Matrix &A)
Constructor.
Definition: euclideanNorm.hh:109
Compute squared matrix norm .
Definition: euclideanNorm.hh:100
auto operator()() const noexcept
Squared matrix norm. Convenient access to d0().
Definition: euclideanNorm.hh:119
SquaredEuclideanNorm()=default
Default constructor.