Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (12,57 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_constant_t;
25
typedef sollya_obj_t pobyso_error_t;
26
typedef sollya_obj_t pobyso_func_exp_t;
27
typedef sollya_obj_t pobyso_on_off_t;
28
typedef mpfr_prec_t  pobyso_precision_t;
29
typedef sollya_obj_t pobyso_range_t;
30

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

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

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

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

    
45

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

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

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

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

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

    
70
void
71
pobyso_autoprint_v(va_list va);
72

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

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

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

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

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

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

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

    
162
/**
163
 * Create a Sollya "off" object. */
164
pobyso_on_off_t
165
pobyso_on();
166

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

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

    
186
/** Disable the console output from Sollya.
187
 * @return a Sollya constant holding the current verbosity level
188
 *         when Sollya was silenced or NULL, if something goes wrong.*/
189
pobyso_constant_t
190
pobyso_quiet();
191

    
192
/** Create a Sollya range from two MPFR bounds.
193
 * A wrapper around sollya_lib_range_from_bounds.
194
 */
195
pobyso_range_t
196
pobyso_range_from_bounds(mpfr_t lowerBound, mpfr_t upperBound);
197

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

    
203
pobyso_func_exp_t
204
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
205
                                      long int degree,
206
                                      pobyso_range_t interval,
207
                                      pobyso_func_exp_t weight,
208
                                      double quality,
209
                                      pobyso_range_t bounds);
210

    
211
/**
212
 * A wrapper around the Sollya Remez function with the a sparse monomials
213
 * base.
214
 */
215

    
216
pobyso_func_exp_t
217
pobyso_remez_sparse_monomials_base(pobyso_func_exp_t);
218

    
219
/**
220
 * A wrapper around the Sollya Remez function with the canonical monomials
221
 * base.
222
 */
223

    
224
pobyso_func_exp_t
225
pobyso_remez_arbitrary_base(pobyso_func_exp_t);
226

    
227
/**
228
 * Set the canonical mode.
229
 */
230

    
231
int
232
pobyso_set_canonical_on(void);
233

    
234
/**
235
 * Set the verbosity mode off (level 0).
236
 * The current level of verbosity is returned.
237
 */
238
int
239
pobyso_set_verbosity_off(void);
240

    
241
/**
242
 * Set the verbosity level to newVerbosityLevel.
243
 * @param newVerbosityLevel must be a Sollya object corresponding to an
244
 *        integer constant.
245
 */
246
int
247
pobyso_set_verbosity_to(int newVerbosityLevel);
248

    
249
/**
250
 * Wrapper around the sollya_lib_subpoly Sollya function.
251
 */
252

    
253
pobyso_func_exp_t
254
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
255

    
256
#if 0
257
/**
258
 * Create the canonical (non sparse) base of monomials for a given degree.
259
 */
260
chain*
261
pobyso_create_canonical_monomials_base(const unsigned int degree);
262

263
/**
264
 * Create a chain from an array of int.
265
 */
266
sollya_int_list
267
pobyso_create_int_list_from_int_Array(int* intArray,
268
                                    const unsigned int arrayLength);
269

270
/**
271
 * Create a chain from an array of int.
272
 */
273
sollya_int_list_t
274
pobyso_create_int_list_from_unsigned_int_array(unsigned int* intArray,
275
                                            const unsigned int arrayLength);
276
/**
277
 * Differentiation of a function.
278
 * A slim wrapper around the Sollya differentiate function.
279
 * @param functionNode - the Sollya node to differentiate;
280
 * @return a node representing the function differentiated or NULL, if
281
 *         something goes wrong.
282
 */
283

284
sollya_obj_t
285
pobyso_diff(sollya_obj_t function);
286

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

305

306
/**
307
 * Faithful evaluation of an expression.
308
 *
309
 * @param faitufulEvaluation - holds the result, must be "inited" by the
310
 *                             caller;
311
 */
312
int
313
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
314
                          node *nodeToEvaluate,
315
                          mpfr_t argument,
316
                          mpfr_prec_t precision);
317

318
/**
319
 * Find the zeros of a function on a given interval.
320
 */
321
chain*
322
pobyso_find_zeros(node *function,
323
                  mpfr_t *lowerBound,
324
                  mpfr_t *upperBound);
325
/**
326
 * Free a chain of node.
327
 * All elements of the chain have to be nodes.
328
 */
329
void
330
pobyso_free_chain_of_nodes(chain *theChain);
331

332
/**
333
 * Free a range.
334
 * It involves clearing the mpfr_t elements and deallocating the
335
 * pointers.
336
 */
337
void
338
pobyso_free_range(rangetype range);
339

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

364
/** Compute a polynomial approximation in the canonical monomials basis for
365
 *  a function, for a given precision. The returned polynomial has the minimal
366
 *  degree to achieve the required precision.
367
 */
368
node*
369
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
370
                                                      unsigned int mode,
371
                                                      mpfr_t lowerBound,
372
                                                      mpfr_t upperBound,
373
                                                      mpfr_t eps);
374

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