dune-fem  2.4.1-rc
space/basisfunctionset/functor.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_BASEFUNCTIONSET_FUNCTOR_HH
2 #define DUNE_FEM_BASEFUNCTIONSET_FUNCTOR_HH
3 
4 #include <dune/common/fmatrix.hh>
5 #include <dune/common/fvector.hh>
6 
8 
9 namespace Dune
10 {
11 
12  namespace Fem
13  {
14 
15  // Internal Forward Declarations
16  // -----------------------------
17 
18  template< class T >
19  inline void axpy ( const T &a, const T &x, T &y );
20 
21  template< class K, int SIZE >
22  inline void axpy ( const typename FieldTraits< K >::field_type &a,
23  const FieldVector< K, SIZE > &x,
24  FieldVector< K, SIZE > &y );
25 
26  template< class K, int ROWS, int COLS >
27  inline void axpy ( const typename FieldTraits< K >::field_type &a,
28  const FieldMatrix< K, ROWS, COLS > &x,
29  FieldMatrix< K, ROWS, COLS > &y );
30 
31 
32 
33  // axpy
34  // ----
35 
36  template< class T >
37  inline void axpy ( const T &a, const T &x, T &y )
38  {
39  y += a*x;
40  }
41 
42  template< class K, int SIZE >
43  inline void axpy ( const typename FieldTraits< K >::field_type &a,
44  const FieldVector< K, SIZE > &x,
45  FieldVector< K, SIZE > &y )
46  {
47  for( int i = 0; i < SIZE; ++i )
48  axpy( a, x[ i ], y[ i ] );
49  }
50 
51  template< class K, int ROWS, int COLS >
52  inline void axpy ( const typename FieldTraits< K >::field_type &a,
53  const FieldMatrix< K, ROWS, COLS > &x,
54  FieldMatrix< K, ROWS, COLS > &y )
55  {
56  y.axpy( a, x );
57  }
58 
59 
60 
61  // scalarProduct
62  // -------------
63 
64  inline double scalarProduct ( const double &a, const double &b ) { return a * b; }
65 
66  template< class T >
67  inline typename T::field_type scalarProduct ( const T &a, const T &b )
68  {
69  return a * b;
70  }
71 
72  template< class K, int ROWS, int COLS >
73  inline K scalarProduct ( const FieldMatrix< K, ROWS, COLS > &a, const FieldMatrix< K, ROWS, COLS > &b )
74  {
75  K s( 0 );
76  for( int r = 0; r < ROWS; ++r )
77  s += a[ r ] * b[ r ];
78  return s;
79  }
80 
81 
82 
83  // AxpyFunctor
84  // -----------
85 
86  template< class Vector, class Value >
87  struct AxpyFunctor
88  {
89  AxpyFunctor ( const Vector &vector, Value &value )
90  : vector_( vector ),
91  value_( value )
92  {}
93 
94  template< class V >
95  void operator() ( const std::size_t i, const V &v )
96  {
97  axpy( vector_[ i ], v, value_ );
98  }
99 
100  private:
101  const Vector &vector_;
102  Value &value_;
103  };
104 
105 
106 
107  // FunctionalAxpyFunctor
108  // ---------------------
109 
110  template< class Value, class Vector >
112  {
113  FunctionalAxpyFunctor ( const Value &value, Vector &vector )
114  : value_( value ),
115  vector_( vector )
116  {}
117 
118  template< class V >
119  void operator() ( const std::size_t i, const V &v )
120  {
121  vector_[ i ] += scalarProduct( v, value_ );
122  }
123 
124  private:
125  const Value &value_;
126  Vector &vector_;
127  };
128 
129  } // namespace Fem
130 
131 } // namespace Dune
132 
133 #endif // #ifndef DUNE_FEM_BASEFUNCTIONSET_FUNCTOR_HH
Definition: space/basisfunctionset/functor.hh:87
void axpy(const T &a, const T &x, T &y)
Definition: space/basisfunctionset/functor.hh:37
Definition: coordinate.hh:4
void operator()(const std::size_t i, const V &v)
Definition: space/basisfunctionset/functor.hh:95
Definition: space/basisfunctionset/functor.hh:111
AxpyFunctor(const Vector &vector, Value &value)
Definition: space/basisfunctionset/functor.hh:89
FunctionalAxpyFunctor(const Value &value, Vector &vector)
Definition: space/basisfunctionset/functor.hh:113
double scalarProduct(const double &a, const double &b)
Definition: space/basisfunctionset/functor.hh:64