source: git/coeffs/mpr_complex.h @ 8e0242

spielwiese
Last change on this file since 8e0242 was 8e0242, checked in by Hans Schoenemann <hannes@…>, 14 years ago
towards a better initialization of numbers
  • Property mode set to 100644
File size: 8.3 KB
Line 
1#ifndef MPR_COMPLEX_H
2#define MPR_COMPLEX_H
3/****************************************
4*  Computer Algebra System SINGULAR     *
5****************************************/
6/* $Id$ */
7
8/*
9* ABSTRACT - multipolynomial resultants - real floating-point numbers using gmp
10*            and complex numbers based on pairs of real floating-point numbers
11*
12*/
13
14//-> include & define stuff
15// must have gmp version >= 2
16#include "si_gmp.h"
17#include "mpr_global.h"
18
19#define ZTOF 1
20#define QTOF 2
21#define RTOF 3
22#define CTOF 4
23
24void setGMPFloatDigits( size_t digits, size_t rest );
25size_t getGMPFloatDigits();
26
27//-> class gmp_float
28/**
29 * @short wrapper class for GNU Multi Precision Floats
30 */
31class gmp_float;
32char *floatToStr( const gmp_float & r, const unsigned int oprec );
33class gmp_float
34{
35public:
36  gmp_float( const int v = 0 )
37  {
38    mpf_init_set_d( t, (double) v );
39  }
40  gmp_float( const long v )
41  {
42    mpf_init_set_d( t, (double) v );
43  }
44  gmp_float( const mprfloat v ) // double
45  {
46    mpf_init_set_d( t, (double) v );
47  }
48  gmp_float( const mpf_t v )
49  {
50    mpf_init_set( t, v );
51  }
52  gmp_float( const mpz_t v ) // gnu mp Z
53  {
54    mpf_init( t );
55    mpf_set_z( t, v );
56  }
57  gmp_float( const gmp_float & v ) // copy constructor
58  {
59    mpf_init_set( t, v.t );
60  }
61
62  ~gmp_float()
63  {
64    mpf_clear( t );
65  }
66
67  inline gmp_float & operator = ( const gmp_float & a )
68  {
69    mpf_set( t, a.t );
70    return *this;
71  };
72  inline gmp_float & operator = ( const mpz_t & a )
73  {
74    mpf_set_z( t, a );
75    return *this;
76  };
77  inline gmp_float & operator = ( const mprfloat a )
78  {
79    mpf_set_d( t, (double) a );
80    return *this;
81  };
82  inline gmp_float & operator = ( const long a )
83  {
84    mpf_set_d( t, (double) a );
85    return *this;
86  };
87
88  gmp_float & operator += ( const gmp_float & a );
89  gmp_float & operator -= ( const gmp_float & a );
90  inline gmp_float & operator *= ( const gmp_float & a )
91  {
92    mpf_mul( t, t, a.t );
93    return *this;
94  };
95
96  inline gmp_float & operator /= ( const gmp_float & a )
97  {
98    mpf_div( t, t, a.t );
99    return *this;
100  };
101
102  friend gmp_float operator + ( const gmp_float & a, const gmp_float & b );
103  friend gmp_float operator - ( const gmp_float & a, const gmp_float & b );
104  friend gmp_float operator * ( const gmp_float & a, const gmp_float & b );
105  friend gmp_float operator / ( const gmp_float & a, const gmp_float & b );
106
107  friend bool operator == ( const gmp_float & a, const gmp_float & b );
108  friend bool operator  > ( const gmp_float & a, const gmp_float & b );
109  friend bool operator  < ( const gmp_float & a, const gmp_float & b );
110  friend bool operator >= ( const gmp_float & a, const gmp_float & b );
111  friend bool operator <= ( const gmp_float & a, const gmp_float & b );
112
113  friend gmp_float operator - ( const gmp_float & a );
114
115  inline int sign()    // t>0:+1, t==0:0, t<0:-1
116  { return mpf_sgn( t ); };
117
118  bool isZero();  // t == 0 ?
119  bool isOne();   // t == 1 ?
120  bool isMOne();  // t == -1 ?
121
122  void setFromStr(const char * in );
123
124  // access
125  inline const mpf_t *mpfp() const { return &t; };
126  inline mpf_t *_mpfp() { return &t; };
127
128  inline operator double() { return mpf_get_d( t ); };
129  inline operator double() const { return mpf_get_d( t ); };
130
131#if 0
132  inline operator int() { return (int)mpf_get_d( t ); };
133  inline operator int() const { return (int)mpf_get_d( t ); };
134//#else
135  inline operator int() const
136  { if (mpf_fits_sint_p(t))
137    { return (int)mpf_get_si( t ); }
138    return 0;
139  };
140#endif
141
142private:
143  mpf_t t;
144};
145
146
147// built-in functions of GMP
148gmp_float abs( const gmp_float & );
149gmp_float sqrt( const gmp_float & );
150gmp_float hypot( const gmp_float &, const gmp_float & );
151//gmp_float pow( const gmp_float &, int & );
152
153// simulated functions using double functions
154gmp_float sin( const gmp_float & );
155gmp_float cos( const gmp_float & );
156gmp_float log( const gmp_float & );
157gmp_float exp( const gmp_float & );
158
159gmp_float max( const gmp_float &, const gmp_float & );
160
161gmp_float numberToFloat( number num );
162gmp_float numberFieldToFloat( number num, int k );
163//char *floatToStr( const gmp_float & r, const unsigned int oprec );
164//<-
165
166//-> class gmp_complex
167/**
168 * @short gmp_complex numbers based on
169 */
170class gmp_complex
171{
172private:
173  gmp_float r, i;
174
175public:
176  gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 )
177  {
178    r= re;
179    i= im;
180  }
181  gmp_complex( const mprfloat re, const mprfloat im = 0.0 )
182  {
183    r= re;
184    i= im;
185  }
186  gmp_complex( const long re, const long im )
187  {
188    r= re;
189    i= im;
190  }
191  gmp_complex( const gmp_complex & v )
192  {
193    r= v.r;
194    i= v.i;
195  }
196  ~gmp_complex() {}
197
198  friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b );
199  friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b );
200  friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b );
201  friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b );
202
203  // gmp_complex <operator> real
204  inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d );
205  inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d );
206  inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d );
207  inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d );
208
209  gmp_complex & operator += ( const gmp_complex & a );
210  gmp_complex & operator -= ( const gmp_complex & a );
211  gmp_complex & operator *= ( const gmp_complex & a );
212  gmp_complex & operator /= ( const gmp_complex & a );
213
214  inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b );
215  inline friend bool operator  > ( const gmp_complex & a, const gmp_complex & b );
216  inline friend bool operator  < ( const gmp_complex & a, const gmp_complex & b );
217  inline friend bool operator >= ( const gmp_complex & a, const gmp_complex & b );
218  inline friend bool operator <= ( const gmp_complex & a, const gmp_complex & b );
219
220  inline gmp_complex & operator = ( const gmp_complex & a );
221  inline gmp_complex & operator = ( const gmp_float & f );
222
223  // access to real and imaginary part
224  inline gmp_float real() const { return r; }
225  inline gmp_float imag() const { return i; }
226
227  inline void real( gmp_float val ) { r = val; }
228  inline void imag( gmp_float val ) { i = val; }
229
230
231  inline bool isZero() { return (r.isZero() && i.isZero()); }
232  void SmallToZero();
233};
234
235// <gmp_complex> = <gmp_complex> operator <gmp_float>
236//
237inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d )
238{
239  return gmp_complex( a.r + b_d, a.i );
240}
241inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d )
242{
243  return gmp_complex( a.r - b_d, a.i );
244}
245inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d )
246{
247  return gmp_complex( a.r * b_d, a.i * b_d );
248}
249inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d )
250{
251  return gmp_complex( a.r / b_d, a.i / b_d );
252}
253
254// <gmp_complex> == <gmp_complex> ?
255inline bool operator == ( const gmp_complex & a, const gmp_complex & b )
256{
257  return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
258}
259inline bool operator  > ( const gmp_complex & a, const gmp_complex & b )
260{
261  return ( a.real() > b.real() );
262}
263inline bool operator  < ( const gmp_complex & a, const gmp_complex & b )
264{
265  return ( a.real() < b.real() );
266}
267inline bool operator >= ( const gmp_complex & a, const gmp_complex & b )
268{
269  return ( a.real() >= b.real() );
270}
271inline bool operator <= ( const gmp_complex & a, const gmp_complex & b )
272{
273  return ( a.real() <= b.real() );
274}
275
276
277// <gmp_complex> = <gmp_complex>
278inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a )
279{
280  r= a.r;
281  i= a.i;
282  return *this;
283}
284
285// <gmp_complex> = <gmp_complex>
286inline gmp_complex & gmp_complex::operator = ( const gmp_float & f )
287{
288  r= f;
289  i= (long int)0;
290  return *this;
291}
292
293// Returns absolute value of a gmp_complex number
294//
295inline gmp_float abs( const gmp_complex & c )
296{
297  return hypot(c.real(),c.imag());
298}
299
300gmp_complex sqrt( const gmp_complex & x );
301
302inline gmp_complex numberToComplex( number num, const coeffs r )
303{
304  if (nField_is_long_C(r))
305  {
306    return *(gmp_complex*)num;
307  }
308  else
309  {
310    return gmp_complex( numberToFloat(num) );
311  }
312}
313
314char *complexToStr( gmp_complex & c, const  unsigned int oprec );
315//<-
316
317bool complexNearZero( gmp_complex * c, int digits );
318
319#endif /* MPR_COMPLEX_H */
320
321// local Variables: ***
322// folded-file: t ***
323// compile-command-1: "make installg" ***
324// compile-command-2: "make install" ***
325// End: ***
Note: See TracBrowser for help on using the repository browser.