dune-common  2.2.1
gmpfield.hh
Go to the documentation of this file.
1 #ifndef DUNE_GMPFIELD_HH
2 #define DUNE_GMPFIELD_HH
3 
8 #include <iostream>
9 
10 #if HAVE_GMP
11 
12 #include <gmpxx.h>
13 
14 namespace Dune
15 {
16 
17  template< unsigned int precision >
18  class GMPField
19  : public mpf_class
20  {
21  typedef mpf_class Base;
22 
23  public:
24  GMPField ()
25  : Base(0,precision)
26  {}
27 
28  template< class T >
29  GMPField ( const T &v )
30  : Base( v,precision )
31  {}
32 
33  /*
34  GMPField &operator=(const GMPField &other)
35  {
36  Base(*this) = Base(other);
37  return *this;
38  }
39  */
40 
41  // type conversion operators
42  operator double () const
43  {
44  return this->get_d();
45  }
46 
47  operator float () const
48  {
49  return this->get_d();
50  }
51 
52  operator mpf_class () const
53  {
54  return static_cast<const mpf_class&>(*this);
55  }
56  };
57 
58 
59 
60  template< unsigned int precision >
61  inline GMPField< precision >
62  operator+ ( const GMPField< precision > &a, const GMPField< precision > &b )
63  {
64  typedef mpf_class F;
65  return ((const F &)a + (const F &)b);
66  }
67 
68  template< unsigned int precision >
69  inline GMPField< precision >
70  operator- ( const GMPField< precision > &a, const GMPField< precision > &b )
71  {
72  typedef mpf_class F;
73  return ((const F &)a - (const F &)b);
74  }
75 
76  template< unsigned int precision >
77  inline GMPField< precision >
78  operator- ( const GMPField< precision > &a )
79  {
80  typedef mpf_class F;
81  return -((const F &)a);
82  }
83 
84  template< unsigned int precision >
85  inline GMPField< precision >
86  operator* ( const GMPField< precision > &a, const GMPField< precision > &b )
87  {
88  typedef mpf_class F;
89  return ((const F &)a * (const F &)b);
90  }
91 
92  template< unsigned int precision >
93  inline GMPField< precision >
94  operator/ ( const GMPField< precision > &a, const GMPField< precision > &b )
95  {
96  typedef mpf_class F;
97  return ((const F &)a / (const F &)b);
98  }
99 
100 
101 
102  template< unsigned int precision >
103  inline std::ostream &
104  operator<< ( std::ostream &out, const GMPField< precision > &value )
105  {
106  return out << static_cast<const mpf_class&>(value);
107  }
108 
109 }
110 
111 namespace std
112 {
113 
114  template< unsigned int precision >
115  inline Dune::GMPField< precision >
116  sqrt ( const Dune::GMPField< precision > &a )
117  {
118  return Dune::GMPField< precision >(sqrt(static_cast<const mpf_class&>(a)));
119  }
120 
121  template< unsigned int precision >
122  inline Dune::GMPField< precision >
123  abs ( const Dune::GMPField< precision > &a )
124  {
125  return Dune::GMPField< precision >( abs( static_cast< const mpf_class & >( a ) ) );
126  }
127 
128 }
129 
130 #endif // HAVE_GMP
131 
132 #endif // #ifndef DUNE_GMPFIELD_HH