source: git/kernel/mpr_complex.h @ 899741

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