Statistiques
| Révision :

root / pobysoC-4.0 / src / pobyso.h @ 146

Historique | Voir | Annoter | Télécharger (12 ko)

1
/** @file pobyso.h
2
 * Integration of Sollya to C programs
3
 * @author S.T.
4
 * @date 2011-10-11
5
 * @note pobyso stands for POwered BY SOllya.
6
 * @todo                        --      -- --
7
 */
8
/******************************************************************************/
9
 
10
/*
11
 * Add below all the headers needed to get this header work.
12
 */
13
/* <stdio.h> is needed *before* <mpfr.h> for all MPFR input/output functions
14
 * prototypes be defined. */
15
#include <string.h>
16
#include <stdio.h>
17
#include <sollya.h>
18
#include <mpfr.h>
19

    
20
#ifndef POBYSO_h
21

    
22
/* Typedefs to make code more readable. */
23

    
24
typedef sollya_obj_t pobyso_error_t;
25
typedef sollya_obj_t pobyso_func_exp_t;
26
typedef sollya_obj_t pobyso_on_off_t;
27
typedef mpfr_prec_t  pobyso_precision_t;
28
typedef sollya_obj_t pobyso_range_t;
29

    
30
#define POBYSO_ABSOLUTE (1)
31
#define POBYSO_RELATIVE (2)
32

    
33
#define POBYSO_UNFAITHFUL (129)
34
#define POBYSO_NAN (130)
35

    
36
#define POBYSO_OFF (0)
37
#define POBYSO_ON (1)
38

    
39
/* Mimic the default behavior of interactive Sollya. */
40
#define POBYSO_DEFAULT_POINTS 501
41
#define POBYSO_INF_NORM_NUM_POINTS (POBYSO_DEFAULT_POINTS)
42
#define POBYSO_GUESS_DEGREE_BOUND 1024
43

    
44

    
45
/* Very thin wrappers around a lot of Sollya functions.
46
 */
47
static inline pobyso_error_t pobyso_error(void)
48
{return(sollya_lib_error());}
49

    
50
static inline int pobyso_is_error(sollya_obj_t errorCandidate)
51
{return(sollya_lib_obj_is_error(errorCandidate));}
52

    
53
static inline int pobyso_is_function(sollya_obj_t functionCandidate)
54
{return(sollya_lib_obj_is_function(functionCandidate));}
55

    
56
/**
57
 * Print an object to stdout.
58
 * A very thin wrapper around the lib_sollya_autoprint() function.
59
 */
60
void
61
pobyso_autoprint(sollya_obj_t objSo);
62

    
63
/**
64
 * Print object(s) to stdout: the va_list companion function.
65
 * A very thin wrapper around the lib_sollya_v_autoprint() function.
66
 * The last argument in the va_list should be NULL.
67
 */
68

    
69
void
70
pobyso_autoprint_v(va_list va);
71

    
72
/**
73
 * A wrapper around the Sollya dirtyfindzeros function.
74
 * Find the numerical values of the zeroes of the funcExpSo expression over
75
 * the [lowerBoundMp, upperBoundMp] interval.
76
 * @param funcExpSo:    a Sollya functional expression;
77
 * @param lowerBoundMp: the lower bound as an MPFR number;
78
 * @param upperBoundMp: the upper bound as an MPFR number;
79
 * @param zerosCount: a pointer to the int where the number of zeros will be
80
 *                    stored;
81
 * @return a (possibly empty) list of the zeroes of the function or NULL if
82
 *         something goes wrong or there are no zeroes. *zerosCount should
83
 *         always be tested first.
84
 *         If *zeroCounts == 0 the function returns NULL. No deallocation is
85
 *         needed.
86
 *         If *zeroCounts >= 0, the list must be deallocated by the caller
87
 *         after each of the elements has been "mpfr_cleared".
88
 *         if something goes wrong *zeroCounts < 0.
89
 */
90
mpfr_t *
91
pobyso_dirty_find_zeros_bounds(pobyso_func_exp_t funcExpSo,
92
                              mpfr_t lowerBoundMp,
93
                              mpfr_t upperBoundMp,
94
                              int* zerosCount);
95

    
96
/**
97
 * Get the current verbosity level.
98
 * @return an integer at the current verbosity level.
99
 */
100
int
101
pobyso_get_verbosity();
102

    
103
/**
104
 * Evaluate an expression for a constant.
105
 *
106
 * The result of the evaluation must be "inited" by the caller.
107
 * Its contents is modified only if the evaluation yields some useful
108
 * result. In this case, its precision may change too.
109
 *@param functionSo : the function to evaluate;
110
 *@param argumentMp : the argument used for the evaluation;
111
 *@param evalutionMp: the result of the evalution.
112
 *@retun 0 if 0K, 1 in case of a "generic" error, POBYSO_UNFAITHFULL if the
113
 *       result is the mean of the two bound of the range encompassing it
114
 *       and POBYSO_NAN if the result of the evaluation is not a number.
115
 */
116
int
117
pobyso_evaluate_constant(pobyso_func_exp_t functionSo,
118
                         mpfr_t argumentMp,
119
                         mpfr_t evaluationMp);
120
/**
121
 * Check if a sollya object is a constant expression.
122
 * @return 1 if true and zero otherwise
123
 */
124
int
125
pobyso_is_constant_expression(sollya_obj_t obj_to_text);
126

    
127
/**
128
 * Check if an expression is a monomial (the free variable to an integer
129
 * positive or null power.
130
 * @param exprSo: a Sollya functional expression object;
131
 * @return 1 if exprSo is a monomial (as defined above), 0 otherwise.
132
 */
133
int pobyso_is_monomial(pobyso_func_exp_t exprSo);
134

    
135
/**
136
 * Check if an expression is a polynomial term (monome)
137
 * (a constant * the free variable to an integer positive or null power or
138
 *  the free variable to an integer positive or null power * a constant).
139
 * @param exprSo: a Sollya functional expression object;
140
 * @return 1 if exprSo is a polynomial term (as defined above), 0 otherwise.
141
 */
142
int pobyso_is_polynomial_term(pobyso_func_exp_t exprSo);
143

    
144
/**
145
 * Check if an expression is an integer.
146
 */
147
int
148
pobyso_is_int(pobyso_func_exp_t exprSo);
149

    
150
/**
151
 * Create a Sollya monomial from a Sollya constant,
152
 * the coefficient, and an integer, the exponent.
153
 * @param coefficient must be a non NULL constant expression;
154
 * @param degree must be a non negative integer;
155
 * @return a Sollya functional expression if successes, or a Sollya error
156
 * if fails.
157
 */
158
pobyso_func_exp_t
159
pobyso_new_monomial(pobyso_func_exp_t coefficient, long degree);
160

    
161
/**
162
 * A wrapper around the Sollya Remez function.
163
 */
164
/**
165
 * Parse a string to create a Sollya object.
166
 * A very thin wrapper around the sollya_lib_parse_string() function.
167
 * If the final ";" is forgotten in the expression, it is added by the
168
 * function.
169
 * @return a Sollya functional expression if successes, or a Sollya error
170
 * if fails.
171
 */
172
pobyso_func_exp_t
173
pobyso_parse_string(const char* expression);
174

    
175
/**
176
 * A wrapper around the Sollya Remez function with the canonical monomials
177
 * base.
178
 */
179

    
180
pobyso_func_exp_t
181
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
182
                                      long int degree,
183
                                      pobyso_range_t interval,
184
                                      pobyso_func_exp_t weight,
185
                                      double quality,
186
                                      pobyso_range_t bounds);
187

    
188
/**
189
 * A wrapper around the Sollya Remez function with the a sparse monomials
190
 * base.
191
 */
192

    
193
pobyso_func_exp_t
194
pobyso_remez_sparse_monomials_base(pobyso_func_exp_t);
195

    
196
/**
197
 * A wrapper around the Sollya Remez function with the canonical monomials
198
 * base.
199
 */
200

    
201
pobyso_func_exp_t
202
pobyso_remez_arbitrary_base(pobyso_func_exp_t);
203

    
204
/**
205
 * Set the canonical mode.
206
 */
207

    
208
int
209
pobyso_set_canonical_on(void);
210

    
211
/**
212
 * Set the verbosity mode off (level 0).
213
 * The current level of verbosity is returned.
214
 */
215
int
216
pobyso_set_verbosity_off(void);
217

    
218
/**
219
 * Set the verbosity level to newVerbosityLevel.
220
 * @param newVerbosityLevel must be a Sollya object corresponding to an
221
 *        integer constant.
222
 */
223
int
224
pobyso_set_verbosity_to(int newVerbosityLevel);
225

    
226
/**
227
 * Wrapper around the sollya_lib_subpoly Sollya function.
228
 */
229

    
230
pobyso_func_exp_t
231
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
232

    
233
#if 0
234
/**
235
 * Create the canonical (non sparse) base of monomials for a given degree.
236
 */
237
chain*
238
pobyso_create_canonical_monomials_base(const unsigned int degree);
239

240
/**
241
 * Create a chain from an array of int.
242
 */
243
sollya_int_list
244
pobyso_create_int_list_from_int_Array(int* intArray,
245
                                    const unsigned int arrayLength);
246

247
/**
248
 * Create a chain from an array of int.
249
 */
250
sollya_int_list_t
251
pobyso_create_int_list_from_unsigned_int_array(unsigned int* intArray,
252
                                            const unsigned int arrayLength);
253
/**
254
 * Differentiation of a function.
255
 * A slim wrapper around the Sollya differentiate function.
256
 * @param functionNode - the Sollya node to differentiate;
257
 * @return a node representing the function differentiated or NULL, if
258
 *         something goes wrong.
259
 */
260

261
sollya_obj_t
262
pobyso_diff(sollya_obj_t function);
263

264
/**
265
 * A match to the Sollya dirtyinfnorm.
266
 * A slim wrapper around the Sollya function.
267
 * @param infnorm - out parameter to return the result, must be "inited"
268
 *                  and "cleared" by the caller;
269
 * @param functionNode - the Sollya node to compute the infinite norm of;
270
 * @param lowerBound - the lower bound of the interval;
271
 * @param upperBound - the upper bound of the interval;
272
 * @param precision  - the internal precision Sollya must use.
273
 * @return 0 if everything is OK, != 0 if something goes wrong.
274
 */
275
int
276
pobyso_dirty_infnorm(mpfr_t infNorm,
277
                      node *functionNode,
278
                      mpfr_t lowerBound,
279
                      mpfr_t upperBound,
280
                      mp_prec_t precision);
281

282

283
/**
284
 * Faithful evaluation of an expression.
285
 *
286
 * @param faitufulEvaluation - holds the result, must be "inited" by the
287
 *                             caller;
288
 */
289
int
290
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
291
                          node *nodeToEvaluate,
292
                          mpfr_t argument,
293
                          mpfr_prec_t precision);
294

295
/**
296
 * Find the zeros of a function on a given interval.
297
 */
298
chain*
299
pobyso_find_zeros(node *function,
300
                  mpfr_t *lowerBound,
301
                  mpfr_t *upperBound);
302
/**
303
 * Free a chain of node.
304
 * All elements of the chain have to be nodes.
305
 */
306
void
307
pobyso_free_chain_of_nodes(chain *theChain);
308

309
/**
310
 * Free a range.
311
 * It involves clearing the mpfr_t elements and deallocating the
312
 * pointers.
313
 */
314
void
315
pobyso_free_range(rangetype range);
316

317
/**
318
 * Computes a good polynomial approximation with fixed-point or floating-point
319
 * coefficients.
320
 */
321
node*
322
pobyso_fp_minimax_canonical_monomials_base(node *function,
323
                                          int degree,
324
                                          chain *formats,
325
                                          chain *points,
326
                                          mpfr_t lowerBound,
327
                                          mpfr_t upperBound,
328
                                          int fpFixedArg,
329
                                          int absRel,
330
                                          node *constPart,
331
                                          node *minimax);
332
/**
333
 * Parses a string to build the node representing the function.
334
 * In fact, does nothing for the moment: the string must be a correct function
335
 * definition. No error correction takes place here.
336
 */
337
node*
338
pobyso_parse_function(char *functionString,
339
                      char *freeVariableNameString);
340

341
/** Compute a polynomial approximation in the canonical monomials basis for
342
 *  a function, for a given precision. The returned polynomial has the minimal
343
 *  degree to achieve the required precision.
344
 */
345
node*
346
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
347
                                                      unsigned int mode,
348
                                                      mpfr_t lowerBound,
349
                                                      mpfr_t upperBound,
350
                                                      mpfr_t eps);
351

352
/**
353
 * Computes a the remez approximation of a function.
354
 * @param function   - the node holding the function to approximate;
355
 * @param weight     - the node holding the weight function, can be NULL. In
356
 *                     this case a default weight of "1" will be provided and
357
 *                     the approximation is related is with respect to the
358
 *                     absolute error.
359
 * @param degree     - the degree of the approximation polynomial;
360
 * @param lowerBound - the lower bound of the approximation interval;
361
 * @param upperBound - the upper bound of the approximation interval;
362
 * @param quality    - quality = (eps - eps*) / eps*; the search stop when the required
363
 *                     quality is achieved.
364
 *
365
 * @return a node holding the approximation polynomial in Horner form.
366
 */
367
node*
368
pobyso_remez_canonical_monomials_base(node *function,
369
                                       node *weight,
370
                                       unsigned int degree,
371
                                       mpfr_t lowerBound,
372
                                       mpfr_t upperBound,
373
                                       mpfr_t quality);
374
#endif
375
#define POBYSO_h  
376
  
377
#endif
378