1 /* mpfi.h -- Include file for mpfi.
2 
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010, 2012, 2018,
4                      Spaces project, Inria Lorraine
5                      and Salsa project, INRIA Rocquencourt,
6                      and Arenaire project, Inria Rhone-Alpes, France
7                      and Lab. ANO, USTL (Univ. of Lille),  France
8 
9 
10 This file is part of the MPFI Library.
11 
12 The MPFI Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or (at your
15 option) any later version.
16 
17 The MPFI Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20 License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public License
23 along with the MPFI Library; see the file COPYING.LIB.  If not, write to
24 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 MA 02110-1301, USA. */
26 
27 
28 #ifndef __MPFI_H__
29 #define __MPFI_H__
30 
31 /* Machine independent implementations */
32 
33 #include <gmp.h>
34 #include <mpfr.h>
35 
36 /* Define MPFI version number */
37 #define MPFI_VERSION_MAJOR 1
38 #define MPFI_VERSION_MINOR 5
39 #define MPFI_VERSION_PATCHLEVEL 4
40 #define MPFI_VERSION_STRING "1.5.4"
41 
42 typedef struct {
43   __mpfr_struct left;
44   __mpfr_struct right;
45 }__mpfi_struct;
46 
47 typedef __mpfi_struct mpfi_t[1];
48 typedef __mpfi_struct *mpfi_ptr;
49 typedef const __mpfi_struct *mpfi_srcptr;
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 /* Rounding                                     */
56 int     mpfi_round_prec (mpfi_ptr, mpfr_prec_t prec);
57 
58 
59 /* Initialization, destruction and assignment   */
60 
61 /* initializations */
62 void    mpfi_init       (mpfi_ptr);
63 void    mpfi_init2      (mpfi_ptr, mpfr_prec_t);
64 void 	mpfi_inits 	(mpfi_ptr x, ...);
65 void 	mpfi_inits2 	(mpfr_prec_t p, mpfi_ptr x, ...);
66 
67 void    mpfi_clear      (mpfi_ptr);
68 void 	mpfi_clears 	(mpfi_ptr x, ...);
69 
70 /* mpfi bounds have the same precision */
71 mpfr_prec_t mpfi_get_prec (mpfi_srcptr);
72 void    mpfi_set_prec   (mpfi_ptr, mpfr_prec_t);
73 
74 
75 /* assignment functions                         */
76 int     mpfi_set        (mpfi_ptr, mpfi_srcptr);
77 int     mpfi_set_si     (mpfi_ptr, const long);
78 int     mpfi_set_ui     (mpfi_ptr, const unsigned long);
79 /* int     mpfi_set_sj     (mpfi_ptr, const intmax_t); */
80 /* int     mpfi_set_uj     (mpfi_ptr, const uintmax_t); */
81 int     mpfi_set_d      (mpfi_ptr, const double);
82 int     mpfi_set_flt    (mpfi_ptr, const float);
83 int     mpfi_set_ld     (mpfi_ptr, const long double);
84 int     mpfi_set_z      (mpfi_ptr, mpz_srcptr);
85 int     mpfi_set_q      (mpfi_ptr, mpq_srcptr);
86 int     mpfi_set_fr     (mpfi_ptr, mpfr_srcptr);
87 int     mpfi_set_str    (mpfi_ptr, const char *, int);
88 
89 /* combined initialization and assignment functions */
90 int     mpfi_init_set       (mpfi_ptr, mpfi_srcptr);
91 int     mpfi_init_set_si    (mpfi_ptr, const long);
92 int     mpfi_init_set_ui    (mpfi_ptr, const unsigned long);
93 int     mpfi_init_set_d     (mpfi_ptr, const double);
94 int     mpfi_init_set_flt   (mpfi_ptr, const float);
95 int     mpfi_init_set_ld    (mpfi_ptr, const long double);
96 int     mpfi_init_set_z     (mpfi_ptr, mpz_srcptr);
97 int     mpfi_init_set_q     (mpfi_ptr, mpq_srcptr);
98 int     mpfi_init_set_fr    (mpfi_ptr, mpfr_srcptr);
99 int     mpfi_init_set_str   (mpfi_ptr, const char *, int);
100 
101 /* swapping two intervals */
102 void    mpfi_swap (mpfi_ptr, mpfi_ptr);
103 
104 
105 /* Various useful interval functions            */
106 /* with scalar or interval results              */
107 
108 /* absolute diameter                            */
109 int     mpfi_diam_abs   (mpfr_ptr, mpfi_srcptr);
110 /* relative diameter                            */
111 int     mpfi_diam_rel   (mpfr_ptr, mpfi_srcptr);
112 /* diameter: relative if the interval does not contain 0 */
113 /* absolute otherwise                                    */
114 int     mpfi_diam       (mpfr_ptr, mpfi_srcptr);
115 /* magnitude: the largest absolute value of any element */
116 int     mpfi_mag        (mpfr_ptr, mpfi_srcptr);
117 /* mignitude: the smallest absolute value of any element */
118 int     mpfi_mig        (mpfr_ptr, mpfi_srcptr);
119 /* middle of y                                           */
120 int     mpfi_mid        (mpfr_ptr, mpfi_srcptr);
121 /* picks randomly a point m in y */
122 void    mpfi_alea       (mpfr_ptr, mpfi_srcptr);
123 void    mpfi_urandom    (mpfr_ptr, mpfi_srcptr, gmp_randstate_t);
124 void    mpfi_nrandom    (mpfr_ptr, mpfi_srcptr, gmp_randstate_t);
125 void    mpfi_erandom    (mpfr_ptr, mpfi_srcptr, gmp_randstate_t);
126 
127 
128 /* Conversions                                  */
129 
130 double  mpfi_get_d      (mpfi_srcptr);
131 void    mpfi_get_fr     (mpfr_ptr, mpfi_srcptr);
132 
133 
134 /* Basic arithmetic operations                  */
135 
136 /* arithmetic operations between two interval operands */
137 int     mpfi_add        (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
138 int     mpfi_sub        (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
139 int     mpfi_mul        (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
140 int     mpfi_div        (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
141 
142 /* arithmetic operations between an interval operand and a double prec. floating-point */
143 int     mpfi_add_d      (mpfi_ptr, mpfi_srcptr, const double);
144 int     mpfi_sub_d      (mpfi_ptr, mpfi_srcptr, const double);
145 int     mpfi_d_sub      (mpfi_ptr, const double, mpfi_srcptr);
146 int     mpfi_mul_d      (mpfi_ptr, mpfi_srcptr, const double);
147 int     mpfi_div_d      (mpfi_ptr, mpfi_srcptr, const double);
148 int     mpfi_d_div      (mpfi_ptr, const double, mpfi_srcptr);
149 
150 /* arithmetic operations between an interval operand and an unsigned long integer */
151 int     mpfi_add_ui     (mpfi_ptr, mpfi_srcptr, const unsigned long);
152 int     mpfi_sub_ui     (mpfi_ptr, mpfi_srcptr, const unsigned long);
153 int     mpfi_ui_sub     (mpfi_ptr, const unsigned long, mpfi_srcptr);
154 int     mpfi_mul_ui     (mpfi_ptr, mpfi_srcptr, const unsigned long);
155 int     mpfi_div_ui     (mpfi_ptr, mpfi_srcptr, const unsigned long);
156 int     mpfi_ui_div     (mpfi_ptr, const unsigned long, mpfi_srcptr);
157 
158 /* arithmetic operations between an interval operand and a long integer */
159 int     mpfi_add_si     (mpfi_ptr, mpfi_srcptr, const long);
160 int     mpfi_sub_si     (mpfi_ptr, mpfi_srcptr, const long);
161 int     mpfi_si_sub     (mpfi_ptr, const long, mpfi_srcptr);
162 int     mpfi_mul_si     (mpfi_ptr, mpfi_srcptr, const long);
163 int     mpfi_div_si     (mpfi_ptr, mpfi_srcptr, const long);
164 int     mpfi_si_div     (mpfi_ptr, const long, mpfi_srcptr);
165 
166 /* arithmetic operations between an interval operand and a multiple prec. integer */
167 int     mpfi_add_z      (mpfi_ptr, mpfi_srcptr, mpz_srcptr);
168 int     mpfi_sub_z      (mpfi_ptr, mpfi_srcptr, mpz_srcptr);
169 int     mpfi_z_sub      (mpfi_ptr, mpz_srcptr, mpfi_srcptr);
170 int     mpfi_mul_z      (mpfi_ptr, mpfi_srcptr, mpz_srcptr);
171 int     mpfi_div_z      (mpfi_ptr, mpfi_srcptr, mpz_srcptr);
172 int     mpfi_z_div      (mpfi_ptr, mpz_srcptr, mpfi_srcptr);
173 
174 /* arithmetic operations between an interval operand and a multiple prec. rational */
175 int     mpfi_add_q      (mpfi_ptr, mpfi_srcptr, mpq_srcptr);
176 int     mpfi_sub_q      (mpfi_ptr, mpfi_srcptr, mpq_srcptr);
177 int     mpfi_q_sub      (mpfi_ptr, mpq_srcptr, mpfi_srcptr);
178 int     mpfi_mul_q      (mpfi_ptr, mpfi_srcptr, mpq_srcptr);
179 int     mpfi_div_q      (mpfi_ptr, mpfi_srcptr, mpq_srcptr);
180 int     mpfi_q_div      (mpfi_ptr, mpq_srcptr, mpfi_srcptr);
181 
182 /* arithmetic operations between an interval operand and a mult. prec. floating-pt nb */
183 int     mpfi_add_fr     (mpfi_ptr, mpfi_srcptr, mpfr_srcptr);
184 int     mpfi_sub_fr     (mpfi_ptr, mpfi_srcptr, mpfr_srcptr);
185 int     mpfi_fr_sub     (mpfi_ptr, mpfr_srcptr, mpfi_srcptr);
186 int     mpfi_mul_fr     (mpfi_ptr, mpfi_srcptr, mpfr_srcptr);
187 int     mpfi_div_fr     (mpfi_ptr, mpfi_srcptr, mpfr_srcptr);
188 int     mpfi_fr_div     (mpfi_ptr, mpfr_srcptr, mpfi_srcptr);
189 
190 /* arithmetic operations taking a single interval operand */
191 int     mpfi_neg        (mpfi_ptr, mpfi_srcptr);
192 int     mpfi_sqr        (mpfi_ptr, mpfi_srcptr);
193 /* the inv function generates the whole real interval
194    if 0 is in the interval defining the divisor */
195 int     mpfi_inv        (mpfi_ptr, mpfi_srcptr);
196 /* the sqrt of a (partially) negative interval is a NaN */
197 int     mpfi_sqrt       (mpfi_ptr, mpfi_srcptr);
198 int     mpfi_cbrt       (mpfi_ptr, mpfi_srcptr);
199 /* the first interval contains the absolute values of */
200 /* every element of the second interval */
201 int     mpfi_abs        (mpfi_ptr, mpfi_srcptr);
202 
203 /* extended division: returns 2 intervals if the denominator contains 0 */
204 int	mpfi_div_ext	(mpfi_ptr, mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
205 
206 /* various operations */
207 int     mpfi_mul_2exp   (mpfi_ptr, mpfi_srcptr, unsigned long);
208 int     mpfi_mul_2ui    (mpfi_ptr, mpfi_srcptr, unsigned long);
209 int     mpfi_mul_2si    (mpfi_ptr, mpfi_srcptr, long);
210 int     mpfi_div_2exp   (mpfi_ptr, mpfi_srcptr, unsigned long);
211 int     mpfi_div_2ui    (mpfi_ptr, mpfi_srcptr, unsigned long);
212 int     mpfi_div_2si    (mpfi_ptr, mpfi_srcptr, long);
213 
214 /* Special functions                                        */
215 int     mpfi_log        (mpfi_ptr, mpfi_srcptr);
216 int     mpfi_exp        (mpfi_ptr, mpfi_srcptr);
217 int     mpfi_exp2       (mpfi_ptr, mpfi_srcptr);
218 
219 int     mpfi_cos        (mpfi_ptr, mpfi_srcptr);
220 int     mpfi_sin        (mpfi_ptr, mpfi_srcptr);
221 int     mpfi_tan        (mpfi_ptr, mpfi_srcptr);
222 int     mpfi_acos       (mpfi_ptr, mpfi_srcptr);
223 int     mpfi_asin       (mpfi_ptr, mpfi_srcptr);
224 int     mpfi_atan       (mpfi_ptr, mpfi_srcptr);
225 int     mpfi_atan2      (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
226 
227 int     mpfi_sec        (mpfi_ptr, mpfi_srcptr);
228 int     mpfi_csc        (mpfi_ptr, mpfi_srcptr);
229 int     mpfi_cot        (mpfi_ptr, mpfi_srcptr);
230 
231 int     mpfi_cosh       (mpfi_ptr, mpfi_srcptr);
232 int     mpfi_sinh       (mpfi_ptr, mpfi_srcptr);
233 int     mpfi_tanh       (mpfi_ptr, mpfi_srcptr);
234 int     mpfi_acosh      (mpfi_ptr, mpfi_srcptr);
235 int     mpfi_asinh      (mpfi_ptr, mpfi_srcptr);
236 int     mpfi_atanh      (mpfi_ptr, mpfi_srcptr);
237 
238 int     mpfi_sech       (mpfi_ptr, mpfi_srcptr);
239 int     mpfi_csch       (mpfi_ptr, mpfi_srcptr);
240 int     mpfi_coth       (mpfi_ptr, mpfi_srcptr);
241 
242 int     mpfi_log1p      (mpfi_ptr, mpfi_srcptr);
243 int     mpfi_expm1      (mpfi_ptr, mpfi_srcptr);
244 
245 int     mpfi_log2       (mpfi_ptr, mpfi_srcptr);
246 int     mpfi_log10      (mpfi_ptr, mpfi_srcptr);
247 
248 int     mpfi_hypot      (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
249 
250 int     mpfi_const_log2         (mpfi_ptr);
251 int     mpfi_const_pi           (mpfi_ptr);
252 int     mpfi_const_euler        (mpfi_ptr);
253 int     mpfi_const_catalan      (mpfi_ptr);
254 
255 /* Comparison functions                                     */
256 /* Warning: the meaning of interval comparison is not clearly defined */
257 /* customizable comparison functions */
258 
259 extern int    (*mpfi_cmp)       (mpfi_srcptr, mpfi_srcptr);
260 
261 extern int    (*mpfi_cmp_d)     (mpfi_srcptr, const double);
262 extern int    (*mpfi_cmp_ui)    (mpfi_srcptr, const unsigned long);
263 extern int    (*mpfi_cmp_si)    (mpfi_srcptr, const long);
264 extern int    (*mpfi_cmp_z)     (mpfi_srcptr, mpz_srcptr);
265 extern int    (*mpfi_cmp_q)     (mpfi_srcptr, mpq_srcptr);
266 extern int    (*mpfi_cmp_fr)    (mpfi_srcptr, mpfr_srcptr);
267 
268 extern int    (*mpfi_is_pos)    (mpfi_srcptr);
269 extern int    (*mpfi_is_nonneg) (mpfi_srcptr);
270 extern int    (*mpfi_is_neg)    (mpfi_srcptr);
271 extern int    (*mpfi_is_nonpos) (mpfi_srcptr);
272 extern int    (*mpfi_is_zero)   (mpfi_srcptr);
273 extern int    (*mpfi_is_strictly_pos) (mpfi_srcptr);
274 extern int    (*mpfi_is_strictly_neg) (mpfi_srcptr);
275 
276 int     mpfi_has_zero   (mpfi_srcptr);
277 
278 int     mpfi_nan_p      (mpfi_srcptr);
279 int     mpfi_inf_p      (mpfi_srcptr);
280 int     mpfi_bounded_p  (mpfi_srcptr);
281 
282 /* Interval manipulation */
283 
284 /* operations related to the internal representation by endpoints */
285 
286 /* get left or right bound of the interval defined by the
287    second argument and put the result in the first one */
288 int     mpfi_get_left   (mpfr_ptr, mpfi_srcptr);
289 int     mpfi_get_right  (mpfr_ptr, mpfi_srcptr);
290 
291 int     mpfi_revert_if_needed  (mpfi_ptr);
292 
293 /* Set operations on intervals */
294 /* "Convex hulls" */
295 /* extends the interval defined by the first argument
296    so that it contains the second one */
297 
298 int     mpfi_put        (mpfi_ptr, mpfi_srcptr);
299 int     mpfi_put_d      (mpfi_ptr, const double);
300 int     mpfi_put_si     (mpfi_ptr, const long);
301 int     mpfi_put_ui     (mpfi_ptr, const unsigned long);
302 int     mpfi_put_z      (mpfi_ptr, mpz_srcptr);
303 int     mpfi_put_q      (mpfi_ptr, mpq_srcptr);
304 int     mpfi_put_fr     (mpfi_ptr, mpfr_srcptr);
305 
306 /* builds an interval whose left bound is the lower (round -infty)
307    than the second argument and the right bound is greater
308    (round +infty) than the third one */
309 
310 int     mpfi_interv_d   (mpfi_ptr, const double, const double);
311 int     mpfi_interv_si  (mpfi_ptr, const long, const long);
312 int     mpfi_interv_ui  (mpfi_ptr, const unsigned long, const unsigned long);
313 int     mpfi_interv_z   (mpfi_ptr, mpz_srcptr, mpz_srcptr);
314 int     mpfi_interv_q   (mpfi_ptr, mpq_srcptr, mpq_srcptr);
315 int     mpfi_interv_fr  (mpfi_ptr, mpfr_srcptr, mpfr_srcptr);
316 
317 /* Inclusion tests */
318 /* tests if the first argument is inside the interval
319    defined by the second one */
320 int     mpfi_is_strictly_inside (mpfi_srcptr, mpfi_srcptr);
321 int     mpfi_is_inside        	(mpfi_srcptr, mpfi_srcptr);
322 int     mpfi_is_inside_d      	(const double, mpfi_srcptr);
323 int     mpfi_is_inside_ui     	(const unsigned long, mpfi_srcptr);
324 int     mpfi_is_inside_si     	(const long, mpfi_srcptr);
325 int     mpfi_is_inside_z      	(mpz_srcptr, mpfi_srcptr);
326 int     mpfi_is_inside_q      	(mpq_srcptr, mpfi_srcptr);
327 int     mpfi_is_inside_fr   	(mpfr_srcptr, mpfi_srcptr);
328 
329 /* set operations */
330 int     mpfi_is_empty   (mpfi_srcptr);
331 int     mpfi_intersect  (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
332 int     mpfi_union      (mpfi_ptr, mpfi_srcptr, mpfi_srcptr);
333 
334 /* complement... : to do later */
335 
336 
337 /* Miscellaneous */
338 
339 /* adds the second argument to the right bound of the first one
340    and subtracts the second argument to the left bound of
341    the first one */
342 int     mpfi_increase   (mpfi_ptr, mpfr_srcptr);
343 /* keeps the same center and multiply the radius by 2*(1+fact) */
344 int     mpfi_blow       (mpfi_ptr, mpfi_srcptr, double);
345 /* splits the interval into 2 halves */
346 int     mpfi_bisect     (mpfi_ptr, mpfi_ptr, mpfi_srcptr);
347 
348 const char * mpfi_get_version (void);
349 
350 /* Error handling */
351 
352 extern int mpfi_error;
353 void    mpfi_reset_error (void);
354 void    mpfi_set_error   (const int);
355 int     mpfi_is_error    (void);
356 #ifdef __cplusplus
357 }
358 #endif
359 
360 #define MPFI_ERROR(s)                           \
361   do {                                          \
362     if(!mpfi_error) {                           \
363       mpfi_error = 1;                           \
364       fprintf(stderr, "\n%s\n", s);             \
365     }                                           \
366   } while (0)
367 
368 #define MPFI_FLAGS_BOTH_ENDPOINTS_EXACT       0
369 #define MPFI_FLAGS_LEFT_ENDPOINT_INEXACT      1
370 #define MPFI_FLAGS_RIGHT_ENDPOINT_INEXACT     2
371 #define MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT     3
372 
373 #define MPFI_BOTH_ARE_EXACT(x) ( (int)(x) == 0 )
374 #define MPFI_LEFT_IS_INEXACT(x) ( (int)(x)%2 )
375 #define MPFI_RIGHT_IS_INEXACT(x) ( (int)(x)/2 )
376 #define MPFI_BOTH_ARE_INEXACT(x) ( (int)(x) == 3 )
377 
378 #define MPFI_REVERT_INEXACT_FLAGS(x) \
379   ( ((x)==1) ? 2 : ((x)==2) ? 1 : x )
380 
381 #define MPFI_NAN_P(a) ( MPFR_IS_NAN(&(a->left)) || MPFR_IS_NAN (&(a->right)) )
382 #define MPFI_INF_P(a) ( MPFR_IS_INF(&(a->left)) || MPFR_IS_INF (&(a->right)) )
383 #define MPFI_IS_ZERO(a)  (MPFI_NAN_P(a) ? 0 : ((mpfr_sgn(&(a->right))==0) && (mpfr_sgn(&(a->left))==0)))
384 
385 #define MPFI_CLEAR(m) {mpfr_clear(&(m->right)); mpfr_clear(&(m->left));}
386 
387 #endif /* __MPFI_H__ */
388