KASKADE 7 development version
scalarproducts.hh
Go to the documentation of this file.
1/*
2 * scalarproducts.hh
3 *
4 * Created on: Sep 16, 2011
5 * Author: bzflubko
6 */
7
8#ifndef SCALARPRODUCTS_HH_
9#define SCALARPRODUCTS_HH_
10
11#include <cmath>
12#include <dune/common/fvector.hh>
13#include <dune/common/fmatrix.hh>
14
15namespace Kaskade
16{
17 namespace {
18
20
24 template <class Type>
25 struct NormTraits{
26 typedef typename Type::value_type return_type;
27 typedef typename Type::const_iterator iterator;
28 };
29
30 template <class value_type, int rows>
31 struct NormTraits<Dune::FieldVector<value_type,rows> >{
32 typedef value_type return_type;
34 };
35
36 template <class value_type, int rows, int cols>
37 struct NormTraits<Dune::FieldMatrix<value_type,rows,cols> >{
38 typedef value_type return_type;
40 };
41
42 } // end of anonymous namespace
43 namespace LinAlg{
45
51 template <class Type>
52 typename NormTraits<Type>::return_type operator()(Type const& v1, Type const& v2) const
53 {
54 typename NormTraits<Type>::return_type result(0);
55 typename NormTraits<Type>::iterator iend = v1.end(), it1 = v1.begin(), it2 = v2.begin();
56 for(; it1 != iend; ++it1, ++it2)
57 result += (*it1) * (*it2);
58 return result;
59 }
60 };
61
62 // forward declaration
63 template <class> struct Norm;
64
66 template <class ScalarProduct_>
68 typedef ScalarProduct_ ScalarProduct;
70
71 NormSquared(ScalarProduct const sp=ScalarProduct()) : scalarProduct(sp){}
72
73 template <class Type>
74 typename NormTraits<Type>::return_type operator()(Type const& type) const
75 {
76 return scalarProduct(type,type);
77 }
78
79 private:
80 ScalarProduct const scalarProduct;
81 };
82
84 template <class ScalarProduct_>
85 struct Norm{
86 typedef ScalarProduct_ ScalarProduct;
88
89 Norm(ScalarProduct const sp=ScalarProduct()) : normSquared(sp){}
90
91 template <class Type>
92 typename NormTraits<Type>::return_type operator()(Type const& type) const
93 {
94 return sqrt(normSquared(type));
95 }
96
97 private:
99 };
100
103
106
108
114 template<class Type>
115 typename NormTraits<Type>::return_type operator()(Type const& type) const
116 {
117 typename NormTraits<Type>::return_type result(0);
118 typename NormTraits<Type>::iterator iend = type.end(), iter = type.begin();
119 for(; iter != iend; ++iter) if(result < fabs(*iter)) result = fabs(*iter);
120 return result;
121 }
122 };
123
125
130 struct OneNorm{
131 template<class Type>
132 typename NormTraits<Type>::return_type operator()(Type const& type) const
133 {
134 typename NormTraits<Type>::return_type result(0);
135 typename NormTraits<Type>::iterator iend = type.end(), iter = type.begin();
136 for(; iter != iend; ++iter)
137 result += fabs(*iter);
138 return result;
139 }
140 };
141
142 }
143}
144#endif /* SCALARPRODUCTS_HH_ */
Norm< EuclideanScalarProduct > EuclideanNorm
Euclidean norm.
NormSquared< EuclideanScalarProduct > EuclideanNormSquared
Euclidean norm squared.
NormTraits< Type >::return_type operator()(Type const &v1, Type const &v2) const
Infinity norm for stl-container, Dune::FieldVector and Dune::FieldMatrix.
NormTraits< Type >::return_type operator()(Type const &type) const
Norm defined via ScalarProdcut.
NormTraits< Type >::return_type operator()(Type const &type) const
NormSquared< ScalarProduct > AssociatedNormSquared
Norm(ScalarProduct const sp=ScalarProduct())
ScalarProduct_ ScalarProduct
Norm squared defined via ScalarProduct.
NormTraits< Type >::return_type operator()(Type const &type) const
NormSquared(ScalarProduct const sp=ScalarProduct())
Norm< ScalarProduct > AssociatedNorm
One norm for stl-container, Dune::FieldVector and Dune::FieldMatrix.
NormTraits< Type >::return_type operator()(Type const &type) const