source: git/Singular/mpr_complex.h @ a3bc95e

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