source: git/Singular/mpr_complex.h @ 0f5091

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