source: git/kernel/mpr_complex.h @ f12d4f6

spielwiese
Last change on this file since f12d4f6 was f12d4f6, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: better rounding for long reals git-svn-id: file:///usr/local/Singular/svn/trunk@7304 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.3 2004-07-29 18:13:28 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
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( 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    {
138      gmp_float tmp(*this); 
139      tmp+=gmp_float(0.5); 
140      return (int)mpf_get_si( tmp.t );
141    }
142    return 0;
143  };
144#endif 
145
146private:
147  mpf_t t;
148};
149
150
151// built-in functions of GMP
152gmp_float abs( const gmp_float & );
153gmp_float sqrt( const gmp_float & );
154gmp_float hypot( const gmp_float &, const gmp_float & );
155//gmp_float pow( const gmp_float &, int & );
156
157// simulated functions using double functions
158gmp_float sin( const gmp_float & );
159gmp_float cos( const gmp_float & );
160gmp_float log( const gmp_float & );
161gmp_float exp( const gmp_float & );
162
163gmp_float max( const gmp_float &, const gmp_float & );
164
165gmp_float numberToFloat( number num );
166gmp_float numberFieldToFloat( number num, int k );
167char *floatToStr( const gmp_float & r, const unsigned int oprec );
168//<-
169
170//-> class gmp_complex
171/**
172 * @short gmp_complex numbers based on
173 */
174class gmp_complex
175{
176private:
177  gmp_float r, i;
178
179public:
180  gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 )
181  {
182    r= re;
183    i= im;
184  }
185  gmp_complex( const mprfloat re, const mprfloat im = 0.0 )
186  {
187    r= re;
188    i= im;
189  }
190  gmp_complex( const long re, const long im )
191  {
192    r= re;
193    i= im;
194  }
195  gmp_complex( const gmp_complex & v )
196  {
197    r= v.r;
198    i= v.i;
199  }
200  ~gmp_complex() {}
201
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  friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b );
205  friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b );
206
207  // gmp_complex <operator> real
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  inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d );
211  inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d );
212
213  gmp_complex & operator += ( const gmp_complex & a );
214  gmp_complex & operator -= ( const gmp_complex & a );
215  gmp_complex & operator *= ( const gmp_complex & a );
216  gmp_complex & operator /= ( const gmp_complex & a );
217
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  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.