source: git/Singular/mpr_complex.h @ 5c67581

spielwiese
Last change on this file since 5c67581 was 5c67581, checked in by Moritz Wenk <wenk@…>, 24 years ago
wenk: changes in solve.lib git-svn-id: file:///usr/local/Singular/svn/trunk@3820 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.9 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.10 1999-11-14 21:35:09 wenk 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#include <gmp.h>
18}
19#include "numbers.h"
20#include "ring.h"
21#include "mpr_global.h"
22
23#define ZTOF 1
24#define QTOF 2
25#define RTOF 3
26#define CTOF 4
27
28#define DEFPREC        20         // minimum number of digits (output operations)
29
30#define GMP_DEFAULT_PREC_BITS 512 // size of mantissa of floating-point number
31#define GMP_NEEDEQUAL_BITS    512-64 // a == b for the first gmp_equalupto_bits bits
32//<-
33
34void setGMPFloatPrecBytes( unsigned long int bytes );
35unsigned long int getGMPFloatPrecBytes();
36
37void setGMPFloatDigits( size_t digits );
38size_t getGMPFloatDigits();
39
40//-> class gmp_float
41/**
42 * @short wrapper class for GNU Multi Precision Floats
43 */
44class gmp_float
45{
46public:
47  gmp_float( const int v = 0 )
48  {
49    mpf_init2( t, gmp_default_prec_bits );
50    mpf_set_si( t, (signed long int) v );
51  }
52  gmp_float( const long v )
53  {
54    mpf_init2( t, gmp_default_prec_bits );
55    mpf_set_si( t, (signed long int) v );
56  }
57  gmp_float( const mprfloat v ) // double
58  {
59    mpf_init2( t, gmp_default_prec_bits );
60    mpf_set_d( t, (double) v );
61  }
62  gmp_float( const mpf_t v )
63  {
64    mpf_init2( t, gmp_default_prec_bits );
65    mpf_set( t, v );
66  }
67  gmp_float( const mpz_t v ) // gnu mp Z
68  {
69    mpf_init2( t, gmp_default_prec_bits );
70    mpf_set_z( t, v );
71  }
72  gmp_float( const gmp_float & v ) // copy constructor
73  {
74    //mpf_init2( t, mpf_get_prec( v.t ) );
75    mpf_init2( t, gmp_default_prec_bits );
76    mpf_set( t, v.t );
77  }
78
79  ~gmp_float()
80  {
81    mpf_clear( t );
82  }
83
84  friend gmp_float operator + ( const gmp_float & a, const gmp_float & b );
85  friend gmp_float operator - ( const gmp_float & a, const gmp_float & b );
86  friend gmp_float operator * ( const gmp_float & a, const gmp_float & b );
87  friend gmp_float operator / ( const gmp_float & a, const gmp_float & b );
88
89  inline gmp_float & operator += ( const gmp_float & a );
90  inline gmp_float & operator -= ( const gmp_float & a );
91  inline gmp_float & operator *= ( const gmp_float & a );
92  inline gmp_float & operator /= ( const gmp_float & a );
93
94  friend bool operator == ( const gmp_float & a, const gmp_float & b );
95  friend bool operator  > ( const gmp_float & a, const gmp_float & b );
96  friend bool operator  < ( const gmp_float & a, const gmp_float & b );
97  friend bool operator >= ( const gmp_float & a, const gmp_float & b );
98  friend bool operator <= ( const gmp_float & a, const gmp_float & b );
99
100  friend gmp_float operator - ( const gmp_float & a );
101
102  gmp_float & operator = ( const gmp_float & a );
103  gmp_float & operator = ( const mpz_t & a );
104  gmp_float & operator = ( const mprfloat a );
105  gmp_float & operator = ( const long a );
106
107  inline int sign();    // t>0:+1, t==0:0, t<0:-1
108  inline bool isZero();  // t == 0 ?
109  inline bool isOne();   // t == 1 ?
110  inline bool isMOne();  // t == -1 ?
111
112  bool setFromStr( char * in );
113
114  // access
115  inline const mpf_t *mpfp() const;
116
117  inline operator double();
118  inline operator double() const;
119
120  inline operator int();
121  inline operator int() const;
122
123public:
124  static void setPrecision( const unsigned long int prec ) {
125    gmp_default_prec_bits= prec;
126  }
127  static void setEqualBits( const unsigned long int prec ) {
128    gmp_needequal_bits= prec;
129  }
130
131  static const unsigned long int getPrecision() {
132    return gmp_default_prec_bits;
133  }
134  static const unsigned long int getEqualBits() {
135    return gmp_needequal_bits;
136  }
137
138private:
139  static unsigned long int gmp_default_prec_bits;
140  static unsigned long int gmp_needequal_bits;
141
142  mpf_t t;
143};
144
145extern const gmp_float  gmpOne;
146extern const gmp_float gmpMOne;
147extern const gmp_float gmpZero;
148
149// <gmp_float> operator <gmp_float>
150inline gmp_float & gmp_float::operator += ( const gmp_float & a )
151{
152  mpf_add( t, t, a.t );
153  return *this;
154}
155inline gmp_float & gmp_float::operator -= ( const gmp_float & a )
156{
157  mpf_sub( t, t, a.t );
158  return *this;
159}
160inline gmp_float & gmp_float::operator *= ( const gmp_float & a )
161{
162  mpf_mul( t, t, a.t );
163  return *this;
164}
165inline gmp_float & gmp_float::operator /= ( const gmp_float & a )
166{
167  mpf_div( t, t, a.t );
168  return *this;
169}
170
171// <gmp_float> = <*>
172inline gmp_float & gmp_float::operator = ( const gmp_float & a )
173{
174  mpf_set( t, a.t );
175  return *this;
176}
177inline gmp_float & gmp_float::operator = ( const mpz_t & a )
178{
179  mpf_set_z( t, a );
180  return *this;
181}
182inline gmp_float & gmp_float::operator = ( const mprfloat a )
183{
184  mpf_set_d( t, (double) a );
185  return *this;
186}
187inline gmp_float & gmp_float::operator = ( const long a )
188{
189  mpf_set_si( t, (signed long int) a );
190  return *this;
191}
192
193// cast to double
194inline gmp_float::operator double()
195{
196  return mpf_get_d( t );
197}
198inline gmp_float::operator double() const
199{
200  return mpf_get_d( t );
201}
202
203// cast to int
204inline gmp_float::operator int()
205{
206  return (int)mpf_get_d( t );
207}
208inline gmp_float::operator int() const
209{
210  return (int)mpf_get_d( t );
211}
212
213// get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 )
214inline int gmp_float::sign()
215{
216  return mpf_sgn( t );
217}
218// t == 0 ?
219inline bool gmp_float::isZero()
220{
221#ifdef  VARIANTE_1
222  return (mpf_sgn( t ) == 0);
223#else
224  return  mpf_eq( t , gmpZero.t , gmp_float::gmp_needequal_bits );
225#endif
226}
227// t == 1 ?
228inline bool gmp_float::isOne()
229{
230#ifdef  VARIANTE_1
231  return (mpf_cmp_ui( t , 1 ) == 0);
232#else
233  return mpf_eq( t , gmpOne.t , gmp_float::gmp_needequal_bits );
234#endif
235}
236// t == -1 ?
237inline bool gmp_float::isMOne()
238{
239#ifdef VARIANTE_1
240  return (mpf_cmp_si( t , -1 ) == 0);
241#else
242  return mpf_eq( t , gmpMOne.t , gmp_float::gmp_needequal_bits );
243#endif
244}
245
246// access pointer
247inline const mpf_t *gmp_float::mpfp() const
248{
249  return &t;
250}
251
252// built-in functions of GMP
253gmp_float abs( const gmp_float & );
254gmp_float sqrt( const gmp_float & );
255gmp_float hypot( const gmp_float &, const gmp_float & );
256//gmp_float pow( const gmp_float &, int & );
257
258// simulated functions using double functions
259gmp_float sin( const gmp_float & );
260gmp_float cos( const gmp_float & );
261gmp_float log( const gmp_float & );
262gmp_float exp( const gmp_float & );
263
264gmp_float max( const gmp_float &, const gmp_float & );
265
266gmp_float numberToFloat( number num );
267gmp_float numberFieldToFloat( number num, int k );
268char *floatToStr( const gmp_float & r, const unsigned int oprec );
269//<-
270
271//-> class gmp_complex
272/**
273 * @short gmp_complex numbers based on
274 */
275class gmp_complex
276{
277private:
278  gmp_float r, i;
279
280public:
281  gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 )
282  {
283    r= re;
284    i= im;
285  }
286  gmp_complex( const mprfloat re, const mprfloat im = 0.0 )
287  {
288    r= re;
289    i= im;
290  }
291  gmp_complex( const long re, const long im )
292  {
293    r= re;
294    i= im;
295  }
296  gmp_complex( const gmp_complex & v )
297  {
298    r= v.r;
299    i= v.i;
300  }
301  ~gmp_complex() {}
302
303  friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b );
304  friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b );
305  friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b );
306  friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b );
307
308  // gmp_complex <operator> real
309  inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d );
310  inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d );
311  inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d );
312  inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d );
313
314  gmp_complex & operator += ( const gmp_complex & a );
315  gmp_complex & operator -= ( const gmp_complex & a );
316  gmp_complex & operator *= ( const gmp_complex & a );
317  gmp_complex & operator /= ( const gmp_complex & a );
318
319  inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b );
320
321  inline gmp_complex & operator = ( const gmp_complex & a );
322  inline gmp_complex & operator = ( const gmp_float & f );
323
324  // access to real and imaginary part
325  inline gmp_float real() const { return r; }
326  inline gmp_float imag() const { return i; }
327
328  inline void real( gmp_float val ) { r = val; }
329  inline void imag( gmp_float val ) { i = val; }
330};
331
332// <gmp_complex> = <gmp_complex> operator <gmp_float>
333//
334inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d )
335{
336  return gmp_complex( a.r + b_d, a.i );
337}
338inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d )
339{
340  return gmp_complex( a.r - b_d, a.i );
341}
342inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d )
343{
344  return gmp_complex( a.r * b_d, a.i * b_d );
345}
346inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d )
347{
348  return gmp_complex( a.r / b_d, a.i / b_d );
349}
350
351// <gmp_complex> == <gmp_complex> ?
352inline bool operator == ( const gmp_complex & a, const gmp_complex & b )
353{
354  return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
355}
356
357// <gmp_complex> = <gmp_complex>
358inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a )
359{
360  r= a.r;
361  i= a.i;
362  return *this;
363}
364
365// <gmp_complex> = <gmp_complex>
366inline gmp_complex & gmp_complex::operator = ( const gmp_float & f )
367{
368  r= f;
369  i= (long int)0;
370  return *this;
371}
372
373// Returns absolute value of a gmp_complex number
374//
375inline gmp_float abs( const gmp_complex & c )
376{
377  return hypot(c.real(),c.imag());
378}
379
380gmp_complex sqrt( const gmp_complex & x );
381
382inline gmp_complex numberToComplex( number num )
383{
384  if (rField_is_long_C()) {
385    return *(gmp_complex*)num;
386  } else {
387    return gmp_complex( numberToFloat(num) );
388  }
389}
390
391char *complexToStr( const gmp_complex & c, const  unsigned int oprec );
392//<-
393
394bool complexNearZero( gmp_complex * c, int digits );
395
396#endif MPR_COMPLEX_H
397
398// local Variables: ***
399// folded-file: t ***
400// compile-command-1: "make installg" ***
401// compile-command-2: "make install" ***
402// End: ***
Note: See TracBrowser for help on using the repository browser.