source: git/coeffs/mpr_complex.h @ 3b3b0e

fieker-DuValspielwiese
Last change on this file since 3b3b0e was f945d2b, checked in by Oleksandr Motsak <motsak@…>, 14 years ago
no mpf_pow? using mpf_pow_ui... + correct operator^ overloading (resp. its usage)
  • Property mode set to 100644
File size: 8.6 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  inline gmp_float operator ^ ( const int exp ) const
108  {
109    mpf_t b;
110    mpf_init(b);
111    mpf_pow_ui( b, this->t, (unsigned long)exp );
112    return gmp_float(b); 
113  };
114
115  friend bool operator == ( const gmp_float & a, const gmp_float & b );
116  friend bool operator  > ( const gmp_float & a, const gmp_float & b );
117  friend bool operator  < ( const gmp_float & a, const gmp_float & b );
118  friend bool operator >= ( const gmp_float & a, const gmp_float & b );
119  friend bool operator <= ( const gmp_float & a, const gmp_float & b );
120
121  friend gmp_float operator - ( const gmp_float & a );
122
123  inline int sign()    // t>0:+1, t==0:0, t<0:-1
124  { return mpf_sgn( t ); };
125
126  bool isZero();  // t == 0 ?
127  bool isOne();   // t == 1 ?
128  bool isMOne();  // t == -1 ?
129
130  void setFromStr(const char * in );
131
132  // access
133  inline const mpf_t *mpfp() const { return &t; };
134  inline mpf_t *_mpfp() { return &t; };
135
136  inline operator double() { return mpf_get_d( t ); };
137  inline operator double() const { return mpf_get_d( t ); };
138
139#if 0
140  inline operator int() { return (int)mpf_get_d( t ); };
141  inline operator int() const { return (int)mpf_get_d( t ); };
142//#else
143  inline operator int() const
144  { if (mpf_fits_sint_p(t))
145    { return (int)mpf_get_si( t ); }
146    return 0;
147  };
148#endif
149
150private:
151  mpf_t t;
152};
153
154
155// built-in functions of GMP
156gmp_float abs( const gmp_float & );
157gmp_float sqrt( const gmp_float & );
158gmp_float hypot( const gmp_float &, const gmp_float & );
159//gmp_float pow( const gmp_float &, int & );
160
161// simulated functions using double functions
162gmp_float sin( const gmp_float & );
163gmp_float cos( const gmp_float & );
164gmp_float log( const gmp_float & );
165gmp_float exp( const gmp_float & );
166
167gmp_float max( const gmp_float &, const gmp_float & );
168
169gmp_float numberToFloat( number num, const coeffs src );
170gmp_float numberFieldToFloat( number num, int k, const coeffs src );
171//char *floatToStr( const gmp_float & r, const unsigned int oprec );
172//<-
173
174//-> class gmp_complex
175/**
176 * @short gmp_complex numbers based on
177 */
178class gmp_complex
179{
180private:
181  gmp_float r, i;
182
183public:
184  gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 )
185  {
186    r= re;
187    i= im;
188  }
189  gmp_complex( const mprfloat re, const mprfloat im = 0.0 )
190  {
191    r= re;
192    i= im;
193  }
194  gmp_complex( const long re, const long im )
195  {
196    r= re;
197    i= im;
198  }
199  gmp_complex( const gmp_complex & v )
200  {
201    r= v.r;
202    i= v.i;
203  }
204  ~gmp_complex() {}
205
206  friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b );
207  friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b );
208  friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b );
209  friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b );
210
211  // gmp_complex <operator> real
212  inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d );
213  inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d );
214  inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d );
215  inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d );
216
217  gmp_complex & operator += ( const gmp_complex & a );
218  gmp_complex & operator -= ( const gmp_complex & a );
219  gmp_complex & operator *= ( const gmp_complex & a );
220  gmp_complex & operator /= ( const gmp_complex & a );
221
222  inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b );
223  inline friend bool operator  > ( const gmp_complex & a, const gmp_complex & b );
224  inline friend bool operator  < ( const gmp_complex & a, const gmp_complex & b );
225  inline friend bool operator >= ( const gmp_complex & a, const gmp_complex & b );
226  inline friend bool operator <= ( const gmp_complex & a, const gmp_complex & b );
227
228  inline gmp_complex & operator = ( const gmp_complex & a );
229  inline gmp_complex & operator = ( const gmp_float & f );
230
231  // access to real and imaginary part
232  inline gmp_float real() const { return r; }
233  inline gmp_float imag() const { return i; }
234
235  inline void real( gmp_float val ) { r = val; }
236  inline void imag( gmp_float val ) { i = val; }
237
238
239  inline bool isZero() { return (r.isZero() && i.isZero()); }
240  void SmallToZero();
241};
242
243// <gmp_complex> = <gmp_complex> operator <gmp_float>
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 );
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 );
252}
253inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d )
254{
255  return gmp_complex( a.r * b_d, a.i * b_d );
256}
257inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d )
258{
259  return gmp_complex( a.r / b_d, a.i / b_d );
260}
261
262// <gmp_complex> == <gmp_complex> ?
263inline bool operator == ( const gmp_complex & a, const gmp_complex & b )
264{
265  return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
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}
275inline bool operator >= ( const gmp_complex & a, const gmp_complex & b )
276{
277  return ( a.real() >= b.real() );
278}
279inline bool operator <= ( const gmp_complex & a, const gmp_complex & b )
280{
281  return ( a.real() <= b.real() );
282}
283
284
285// <gmp_complex> = <gmp_complex>
286inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a )
287{
288  r= a.r;
289  i= a.i;
290  return *this;
291}
292
293// <gmp_complex> = <gmp_complex>
294inline gmp_complex & gmp_complex::operator = ( const gmp_float & f )
295{
296  r= f;
297  i= (long int)0;
298  return *this;
299}
300
301// Returns absolute value of a gmp_complex number
302//
303inline gmp_float abs( const gmp_complex & c )
304{
305  return hypot(c.real(),c.imag());
306}
307
308gmp_complex sqrt( const gmp_complex & x );
309
310inline gmp_complex numberToComplex( number num, const coeffs r )
311{
312  if (nField_is_long_C(r))
313  {
314    return *(gmp_complex*)num;
315  }
316  else
317  {
318    return gmp_complex( numberToFloat(num, r) );
319  }
320}
321
322char *complexToStr( gmp_complex & c, const  unsigned int oprec, const coeffs src );
323//<-
324
325bool complexNearZero( gmp_complex * c, int digits );
326
327#endif /* MPR_COMPLEX_H */
328
329// local Variables: ***
330// folded-file: t ***
331// compile-command-1: "make installg" ***
332// compile-command-2: "make install" ***
333// End: ***
Note: See TracBrowser for help on using the repository browser.