source: git/libpolys/coeffs/mpr_complex.h @ 45cc512

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