source: git/Singular/mpr_complex.h @ 1dca8ad

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