My Project
Loading...
Searching...
No Matches
mpr_complex.h
Go to the documentation of this file.
1#ifndef MPR_COMPLEX_H
2#define MPR_COMPLEX_H
3/****************************************
4* Computer Algebra System SINGULAR *
5****************************************/
6
7/*
8* ABSTRACT - multipolynomial resultants - real floating-point numbers using gmp
9* and complex numbers based on pairs of real floating-point numbers
10*
11*/
12
13//-> include & define stuff
14// must have gmp version >= 2
15#include "coeffs/si_gmp.h"
16#include "coeffs/mpr_global.h"
17
18#define ZTOF 1
19#define QTOF 2
20#define RTOF 3
21#define CTOF 4
22
23void setGMPFloatDigits( size_t digits, size_t rest );
24
25//-> class gmp_float
26/**
27 * @short wrapper class for GNU Multi Precision Floats
28 */
29class gmp_float;
30char *floatToStr( const gmp_float & r, const unsigned int oprec );
32{
33public:
34 gmp_float( const int v = 0 )
35 {
36 mpf_init_set_si( t, (long)v );
37 }
38 gmp_float( const long v )
39 {
40 mpf_init_set_si( t, v );
41 }
42 gmp_float( const mprfloat v ) // double
43 {
44 mpf_init_set_d( t, v );
45 }
46 gmp_float( const mpf_t v )
47 {
48 mpf_init_set( t, v );
49 }
50 gmp_float( const mpz_t v ) // gnu mp Z
51 {
52 mpf_init( t );
53 mpf_set_z( t, v );
54 }
55 gmp_float( const gmp_float & v ) // copy constructor
56 {
57 mpf_init_set( t, v.t );
58 }
59
61 {
62 mpf_clear( t );
63 }
64
65 inline gmp_float & operator = ( const gmp_float & a )
66 {
67 mpf_set( t, a.t );
68 return *this;
69 };
70 inline gmp_float & operator = ( const mpz_t & a )
71 {
72 mpf_set_z( t, a );
73 return *this;
74 };
75 inline gmp_float & operator = ( const mprfloat a )
76 {
77 mpf_set_d( t, (double) a );
78 return *this;
79 };
80 inline gmp_float & operator = ( const long a )
81 {
82 mpf_set_d( t, (double) a );
83 return *this;
84 };
85
86 gmp_float & operator += ( const gmp_float & a );
87 gmp_float & operator -= ( const gmp_float & a );
88 inline gmp_float & operator *= ( const gmp_float & a )
89 {
90 mpf_mul( t, t, a.t );
91 return *this;
92 };
93
94 inline gmp_float & operator /= ( const gmp_float & a )
95 {
96 mpf_div( t, t, a.t );
97 return *this;
98 };
99
100 inline gmp_float & neg ( ) { mpf_neg(t,t); return *this; };
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 inline gmp_float operator ^ ( const int exp ) const
108 {
109 mpf_t b;
110 mpf_init(b);
111 mpf_pow_ui( b, this->t, (unsigned long)exp );
112 return gmp_float(b);
113 };
114
115 friend bool operator == ( const gmp_float & a, const gmp_float & b );
116 friend bool operator > ( const gmp_float & a, const gmp_float & b );
117 friend bool operator < ( const gmp_float & a, const gmp_float & b );
118 friend bool operator >= ( const gmp_float & a, const gmp_float & b );
119 friend bool operator <= ( const gmp_float & a, const gmp_float & b );
120
121 friend gmp_float operator - ( const gmp_float & a );
122
123 inline int sign() // t>0:+1, t==0:0, t<0:-1
124 { return mpf_sgn( t ); };
125
126 bool isZero() const; // t == 0 ?
127 bool isOne() const; // t == 1 ?
128 bool isMOne() const; // t == -1 ?
129
130 void setFromStr(const char * in );
131
132 // access
133 inline const mpf_t *mpfp() const { return &t; };
134 inline mpf_t *_mpfp() { return &t; };
135
136 inline operator double() { return mpf_get_d( t ); };
137 inline operator double() const { return mpf_get_d( t ); };
138
139#if 0
140 inline operator int() { return (int)mpf_get_d( t ); };
141 inline operator int() const { return (int)mpf_get_d( t ); };
142//#else
143 inline operator int() const
144 { if (mpf_fits_sint_p(t))
145 { return (int)mpf_get_si( t ); }
146 return 0;
147 };
148#endif
149
150public:
151 mpf_t t;
152};
153
154
155// built-in functions of GMP
156gmp_float abs( const gmp_float & );
157gmp_float sqrt( const gmp_float & );
158gmp_float hypot( const gmp_float &, const gmp_float & );
159//gmp_float pow( const gmp_float &, int & );
160
161// simulated functions using double functions
162gmp_float sin( const gmp_float & );
163gmp_float cos( const gmp_float & );
164gmp_float log( const gmp_float & );
165gmp_float exp( const gmp_float & );
166
167gmp_float max( const gmp_float &, const gmp_float & );
168
169gmp_float numberToFloat( number num, const coeffs src );
170gmp_float numberFieldToFloat( number num, int src );
171//char *floatToStr( const gmp_float & r, const unsigned int oprec );
172//<-
173
174//-> class gmp_complex
175/**
176 * @short gmp_complex numbers based on
177 */
179{
180private:
182
183public:
184 gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 )
185 {
186 r= re;
187 i= im;
188 }
189 gmp_complex( const mprfloat re, const mprfloat im = 0.0 )
190 {
191 r= re;
192 i= im;
193 }
194 gmp_complex( const long re, const long im )
195 {
196 r= re;
197 i= im;
198 }
200 {
201 r= v.r;
202 i= v.i;
203 }
205
206 gmp_complex & neg ( );
207
208 friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b );
209 friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b );
210 friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b );
211 friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b );
212
213 // gmp_complex <operator> real
214 inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d );
215 inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d );
216 inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d );
217 inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d );
218
219 gmp_complex & operator += ( const gmp_complex & a );
220 gmp_complex & operator -= ( const gmp_complex & a );
221 gmp_complex & operator *= ( const gmp_complex & a );
222 gmp_complex & operator /= ( const gmp_complex & a );
223
224 inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b );
225 inline friend bool operator > ( const gmp_complex & a, const gmp_complex & b );
226 inline friend bool operator < ( const gmp_complex & a, const gmp_complex & b );
227 inline friend bool operator >= ( const gmp_complex & a, const gmp_complex & b );
228 inline friend bool operator <= ( const gmp_complex & a, const gmp_complex & b );
229
230 inline gmp_complex & operator = ( const gmp_complex & a );
231 inline gmp_complex & operator = ( const gmp_float & f );
232
233 // access to real and imaginary part
234 inline gmp_float real() const { return r; }
235 inline gmp_float imag() const { return i; }
236
237 inline void real( gmp_float val ) { r = val; }
238 inline void imag( gmp_float val ) { i = val; }
239
240
241 inline bool isZero() { return (r.isZero() && i.isZero()); }
242 void SmallToZero();
243};
244
245// <gmp_complex> = <gmp_complex> operator <gmp_float>
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 );
250}
251inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d )
252{
253 return gmp_complex( a.r - b_d, a.i );
254}
255inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d )
256{
257 return gmp_complex( a.r * b_d, a.i * b_d );
258}
259inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d )
260{
261 return gmp_complex( a.r / b_d, a.i / b_d );
262}
263
264// <gmp_complex> == <gmp_complex> ?
265inline bool operator == ( const gmp_complex & a, const gmp_complex & b )
266{
267 return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
268}
269inline bool operator > ( const gmp_complex & a, const gmp_complex & b )
270{
271 return ( a.real() > b.real() );
272}
273inline bool operator < ( const gmp_complex & a, const gmp_complex & b )
274{
275 return ( a.real() < b.real() );
276}
277inline bool operator >= ( const gmp_complex & a, const gmp_complex & b )
278{
279 return ( a.real() >= b.real() );
280}
281inline bool operator <= ( const gmp_complex & a, const gmp_complex & b )
282{
283 return ( a.real() <= b.real() );
284}
285
286
287// <gmp_complex> = <gmp_complex>
289{
290 r= a.r;
291 i= a.i;
292 return *this;
293}
294
295// <gmp_complex> = <gmp_complex>
297{
298 r= f;
299 i= (long int)0;
300 return *this;
301}
302
303// Returns absolute value of a gmp_complex number
304//
305inline gmp_float abs( const gmp_complex & c )
306{
307 return hypot(c.real(),c.imag());
308}
309
310gmp_complex sqrt( const gmp_complex & x );
311
312inline gmp_complex numberToComplex( number num, const coeffs r )
313{
314 if (nCoeff_is_long_C(r))
315 {
316 return *(gmp_complex*)num;
317 }
318 else
319 {
320 return gmp_complex( numberToFloat(num, r) );
321 }
322}
323
324char *complexToStr( gmp_complex & c, const unsigned int oprec, const coeffs src );
325//<-
326
327bool complexNearZero( gmp_complex * c, int digits );
328
329#endif /* MPR_COMPLEX_H */
330
331// local Variables: ***
332// folded-file: t ***
333// compile-command-1: "make installg" ***
334// compile-command-2: "make install" ***
335// End: ***
CanonicalForm num(const CanonicalForm &f)
Variable x
Definition: cfModGcd.cc:4082
CanonicalForm b
Definition: cfModGcd.cc:4103
FILE * f
Definition: checklibs.c:9
gmp_complex numbers based on
Definition: mpr_complex.h:179
friend bool operator<(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:273
gmp_float i
Definition: mpr_complex.h:181
gmp_complex(const mprfloat re, const mprfloat im=0.0)
Definition: mpr_complex.h:189
friend gmp_complex operator*(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.cc:627
friend bool operator==(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:265
gmp_complex(const gmp_complex &v)
Definition: mpr_complex.h:199
gmp_float imag() const
Definition: mpr_complex.h:235
void real(gmp_float val)
Definition: mpr_complex.h:237
friend gmp_complex operator+(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.cc:619
gmp_complex(const gmp_float re=0.0, const gmp_float im=0.0)
Definition: mpr_complex.h:184
friend bool operator>(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:269
friend bool operator<=(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:281
gmp_complex & operator*=(const gmp_complex &a)
Definition: mpr_complex.cc:653
gmp_complex & operator/=(const gmp_complex &a)
Definition: mpr_complex.cc:666
void SmallToZero()
Definition: mpr_complex.cc:784
friend gmp_complex operator-(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.cc:623
gmp_complex & operator=(const gmp_complex &a)
Definition: mpr_complex.h:288
friend gmp_complex operator/(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.cc:632
gmp_complex(const long re, const long im)
Definition: mpr_complex.h:194
gmp_complex & neg()
Definition: mpr_complex.cc:660
gmp_complex & operator+=(const gmp_complex &a)
Definition: mpr_complex.cc:641
void imag(gmp_float val)
Definition: mpr_complex.h:238
friend bool operator>=(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:277
gmp_float r
Definition: mpr_complex.h:181
gmp_float real() const
Definition: mpr_complex.h:234
gmp_complex & operator-=(const gmp_complex &a)
Definition: mpr_complex.cc:647
bool isZero()
Definition: mpr_complex.h:241
void setFromStr(const char *in)
Definition: mpr_complex.cc:78
gmp_float(const int v=0)
Definition: mpr_complex.h:34
bool isOne() const
Definition: mpr_complex.cc:257
gmp_float(const mprfloat v)
Definition: mpr_complex.h:42
gmp_float operator^(const int exp) const
Definition: mpr_complex.h:107
friend gmp_float operator*(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:179
friend bool operator<(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:294
friend bool operator<=(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:306
int sign()
Definition: mpr_complex.h:123
gmp_float(const gmp_float &v)
Definition: mpr_complex.h:55
bool isMOne() const
Definition: mpr_complex.cc:273
gmp_float & operator-=(const gmp_float &a)
Definition: mpr_complex.cc:214
mpf_t * _mpfp()
Definition: mpr_complex.h:134
gmp_float & operator/=(const gmp_float &a)
Definition: mpr_complex.h:94
friend gmp_float operator-(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:173
friend gmp_float operator+(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:167
gmp_float & operator+=(const gmp_float &a)
Definition: mpr_complex.cc:193
gmp_float & neg()
Definition: mpr_complex.h:100
gmp_float & operator=(const gmp_float &a)
Definition: mpr_complex.h:65
friend bool operator==(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:237
bool isZero() const
Definition: mpr_complex.cc:252
const mpf_t * mpfp() const
Definition: mpr_complex.h:133
gmp_float & operator*=(const gmp_float &a)
Definition: mpr_complex.h:88
gmp_float(const mpz_t v)
Definition: mpr_complex.h:50
friend gmp_float operator/(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:185
friend bool operator>=(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:300
gmp_float(const long v)
Definition: mpr_complex.h:38
friend bool operator>(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:288
gmp_float(const mpf_t v)
Definition: mpr_complex.h:46
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:891
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
bool operator<(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:273
bool operator==(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:265
gmp_float max(const gmp_float &, const gmp_float &)
Definition: mpr_complex.cc:362
char * complexToStr(gmp_complex &c, const unsigned int oprec, const coeffs src)
Definition: mpr_complex.cc:704
gmp_float exp(const gmp_float &)
Definition: mpr_complex.cc:357
gmp_float hypot(const gmp_float &, const gmp_float &)
Definition: mpr_complex.cc:348
gmp_complex operator/(const gmp_complex &a, const gmp_float b_d)
Definition: mpr_complex.h:259
bool operator>(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:269
bool operator<=(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:281
gmp_float log(const gmp_float &)
Definition: mpr_complex.cc:343
gmp_complex operator+(const gmp_complex &a, const gmp_float b_d)
Definition: mpr_complex.h:247
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition: mpr_complex.cc:578
bool complexNearZero(gmp_complex *c, int digits)
Definition: mpr_complex.cc:765
gmp_complex operator*(const gmp_complex &a, const gmp_float b_d)
Definition: mpr_complex.h:255
gmp_float cos(const gmp_float &)
Definition: mpr_complex.cc:338
gmp_float sqrt(const gmp_float &)
Definition: mpr_complex.cc:327
gmp_float numberToFloat(number num, const coeffs src)
Definition: mpr_complex.cc:372
bool operator>=(const gmp_complex &a, const gmp_complex &b)
Definition: mpr_complex.h:277
gmp_complex numberToComplex(number num, const coeffs r)
Definition: mpr_complex.h:312
gmp_float numberFieldToFloat(number num, int src)
Definition: mpr_complex.cc:438
gmp_complex operator-(const gmp_complex &a, const gmp_float b_d)
Definition: mpr_complex.h:251
gmp_float abs(const gmp_float &)
Definition: mpr_complex.cc:321
gmp_float sin(const gmp_float &)
Definition: mpr_complex.cc:333
void setGMPFloatDigits(size_t digits, size_t rest)
Set size of mantissa digits - the number of output digits (basis 10) the size of mantissa consists of...
Definition: mpr_complex.cc:60
double mprfloat
Definition: mpr_global.h:17
The main handler for Singular numbers which are suitable for Singular polynomials.