Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (12,32 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 evaluation.
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
 * Create a Sollya "off" object. */
163
pobyso_on_off_t
164
pobyso_on();
165

    
166
/**
167
 * Create a Sollya "on" object. */
168
pobyso_on_off_t
169
pobyso_off();
170

    
171
/**
172
 * A wrapper around the Sollya Remez function.
173
 */
174
/**
175
 * Parse a string to create a Sollya object.
176
 * A very thin wrapper around the sollya_lib_parse_string() function.
177
 * If the final ";" is forgotten in the expression, it is added by the
178
 * function.
179
 * @return a Sollya functional expression if successes, or a Sollya error
180
 * if fails.
181
 */
182
pobyso_func_exp_t
183
pobyso_parse_string(const char* expression);
184

    
185
/** Create a Sollya range from two MPFR bounds.
186
 * A wrapper around sollya_lib_range_from_bounds.
187
 */
188
pobyso_range_t
189
pobyso_range_from_bounds(mpfr_t lowerBound, mpfr_t upperBound);
190

    
191
/**
192
 * A wrapper around the Sollya Remez function with the canonical monomials
193
 * base.
194
 */
195

    
196
pobyso_func_exp_t
197
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
198
                                      long int degree,
199
                                      pobyso_range_t interval,
200
                                      pobyso_func_exp_t weight,
201
                                      double quality,
202
                                      pobyso_range_t bounds);
203

    
204
/**
205
 * A wrapper around the Sollya Remez function with the a sparse monomials
206
 * base.
207
 */
208

    
209
pobyso_func_exp_t
210
pobyso_remez_sparse_monomials_base(pobyso_func_exp_t);
211

    
212
/**
213
 * A wrapper around the Sollya Remez function with the canonical monomials
214
 * base.
215
 */
216

    
217
pobyso_func_exp_t
218
pobyso_remez_arbitrary_base(pobyso_func_exp_t);
219

    
220
/**
221
 * Set the canonical mode.
222
 */
223

    
224
int
225
pobyso_set_canonical_on(void);
226

    
227
/**
228
 * Set the verbosity mode off (level 0).
229
 * The current level of verbosity is returned.
230
 */
231
int
232
pobyso_set_verbosity_off(void);
233

    
234
/**
235
 * Set the verbosity level to newVerbosityLevel.
236
 * @param newVerbosityLevel must be a Sollya object corresponding to an
237
 *        integer constant.
238
 */
239
int
240
pobyso_set_verbosity_to(int newVerbosityLevel);
241

    
242
/**
243
 * Wrapper around the sollya_lib_subpoly Sollya function.
244
 */
245

    
246
pobyso_func_exp_t
247
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
248

    
249
#if 0
250
/**
251
 * Create the canonical (non sparse) base of monomials for a given degree.
252
 */
253
chain*
254
pobyso_create_canonical_monomials_base(const unsigned int degree);
255

256
/**
257
 * Create a chain from an array of int.
258
 */
259
sollya_int_list
260
pobyso_create_int_list_from_int_Array(int* intArray,
261
                                    const unsigned int arrayLength);
262

263
/**
264
 * Create a chain from an array of int.
265
 */
266
sollya_int_list_t
267
pobyso_create_int_list_from_unsigned_int_array(unsigned int* intArray,
268
                                            const unsigned int arrayLength);
269
/**
270
 * Differentiation of a function.
271
 * A slim wrapper around the Sollya differentiate function.
272
 * @param functionNode - the Sollya node to differentiate;
273
 * @return a node representing the function differentiated or NULL, if
274
 *         something goes wrong.
275
 */
276

277
sollya_obj_t
278
pobyso_diff(sollya_obj_t function);
279

280
/**
281
 * A match to the Sollya dirtyinfnorm.
282
 * A slim wrapper around the Sollya function.
283
 * @param infnorm - out parameter to return the result, must be "inited"
284
 *                  and "cleared" by the caller;
285
 * @param functionNode - the Sollya node to compute the infinite norm of;
286
 * @param lowerBound - the lower bound of the interval;
287
 * @param upperBound - the upper bound of the interval;
288
 * @param precision  - the internal precision Sollya must use.
289
 * @return 0 if everything is OK, != 0 if something goes wrong.
290
 */
291
int
292
pobyso_dirty_infnorm(mpfr_t infNorm,
293
                      node *functionNode,
294
                      mpfr_t lowerBound,
295
                      mpfr_t upperBound,
296
                      mp_prec_t precision);
297

298

299
/**
300
 * Faithful evaluation of an expression.
301
 *
302
 * @param faitufulEvaluation - holds the result, must be "inited" by the
303
 *                             caller;
304
 */
305
int
306
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
307
                          node *nodeToEvaluate,
308
                          mpfr_t argument,
309
                          mpfr_prec_t precision);
310

311
/**
312
 * Find the zeros of a function on a given interval.
313
 */
314
chain*
315
pobyso_find_zeros(node *function,
316
                  mpfr_t *lowerBound,
317
                  mpfr_t *upperBound);
318
/**
319
 * Free a chain of node.
320
 * All elements of the chain have to be nodes.
321
 */
322
void
323
pobyso_free_chain_of_nodes(chain *theChain);
324

325
/**
326
 * Free a range.
327
 * It involves clearing the mpfr_t elements and deallocating the
328
 * pointers.
329
 */
330
void
331
pobyso_free_range(rangetype range);
332

333
/**
334
 * Computes a good polynomial approximation with fixed-point or floating-point
335
 * coefficients.
336
 */
337
node*
338
pobyso_fp_minimax_canonical_monomials_base(node *function,
339
                                          int degree,
340
                                          chain *formats,
341
                                          chain *points,
342
                                          mpfr_t lowerBound,
343
                                          mpfr_t upperBound,
344
                                          int fpFixedArg,
345
                                          int absRel,
346
                                          node *constPart,
347
                                          node *minimax);
348
/**
349
 * Parses a string to build the node representing the function.
350
 * In fact, does nothing for the moment: the string must be a correct function
351
 * definition. No error correction takes place here.
352
 */
353
node*
354
pobyso_parse_function(char *functionString,
355
                      char *freeVariableNameString);
356

357
/** Compute a polynomial approximation in the canonical monomials basis for
358
 *  a function, for a given precision. The returned polynomial has the minimal
359
 *  degree to achieve the required precision.
360
 */
361
node*
362
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
363
                                                      unsigned int mode,
364
                                                      mpfr_t lowerBound,
365
                                                      mpfr_t upperBound,
366
                                                      mpfr_t eps);
367

368
/**
369
 * Computes a the remez approximation of a function.
370
 * @param function   - the node holding the function to approximate;
371
 * @param weight     - the node holding the weight function, can be NULL. In
372
 *                     this case a default weight of "1" will be provided and
373
 *                     the approximation is related is with respect to the
374
 *                     absolute error.
375
 * @param degree     - the degree of the approximation polynomial;
376
 * @param lowerBound - the lower bound of the approximation interval;
377
 * @param upperBound - the upper bound of the approximation interval;
378
 * @param quality    - quality = (eps - eps*) / eps*; the search stop when the required
379
 *                     quality is achieved.
380
 *
381
 * @return a node holding the approximation polynomial in Horner form.
382
 */
383
node*
384
pobyso_remez_canonical_monomials_base(node *function,
385
                                       node *weight,
386
                                       unsigned int degree,
387
                                       mpfr_t lowerBound,
388
                                       mpfr_t upperBound,
389
                                       mpfr_t quality);
390
#endif
391
#define POBYSO_h  
392
  
393
#endif
394