rheolef  6.3
rounder.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_ROUNDER_H
2 #define _RHEOLEF_ROUNDER_H
3 #include "rheolef/compiler.h"
4 #include <cmath>
5 
6 namespace rheolef {
7 
8 template <class T>
9 struct rounder_type : std::unary_function<T,T> {
10  rounder_type (const T& prec) : _prec(prec) {}
11  T operator() (const T& x) const {
12  T value = _prec*std::floor(x/_prec + 0.5);
13  if (1+value == 1) value = 0;
14  return value;
15  }
16  T _prec;
17 };
18 template <class T>
19 struct floorer_type : std::unary_function<T,T> {
20  floorer_type (const T& prec) : _prec(prec) {}
21  T operator() (const T& x) const {
22  T value = _prec*std::floor(x/_prec);
23  if (1+value == 1) value = 0;
24  return value;
25  }
26  T _prec;
27 };
28 template <class T>
29 struct ceiler_type : std::unary_function<T,T> {
30  ceiler_type (const T& prec) : _prec(prec) {}
31  T operator() (const T& x) const {
32  T value = _prec*std::ceil(x/_prec);
33  if (1+value == 1) value = 0;
34  return value;
35  }
36  T _prec;
37 };
38 
39 template <class T> rounder_type<T> rounder (const T& prec) { return rounder_type<T>(prec); }
40 template <class T> floorer_type<T> floorer (const T& prec) { return floorer_type<T>(prec); }
41 template <class T> ceiler_type<T> ceiler (const T& prec) { return ceiler_type<T>(prec); }
42 
43 template<class T, class M>
44 field_basic<T,M>
46  const field_basic<T,M>& uh,
47  const T& prec)
48 {
49  field_basic<T,M> vh (uh.get_space());
50  std::transform (uh.begin_dof(), uh.end_dof(), vh.begin_dof(), rounder(prec));
51  return vh;
52 }
53 template<class Expr, class T2>
54 field_basic<
55  typename field_expr<Expr>::scalar_type
56  ,typename field_expr<Expr>::memory_type
57 >
59  const field_expr<Expr>& expr,
60  const T2& prec)
61 {
62  typedef typename field_expr<Expr>::scalar_type T;
63  typedef typename field_expr<Expr>::memory_type M;
64  return round (field_basic<T,M>(expr), prec);
65 }
66 
67 }//namespace rheolef
68 #endif // _RHEOLEF_ROUNDER_H
69