Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (12,77 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
/** A very thin wrapper around the sollya_lib_clear_obj
74
 * function.
75
 * @param objSo: the Sollya object to free.
76
 */
77
void
78
pobyso_clear_obj(sollya_obj_t objSo);
79

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

    
104
/**
105
 * Get the current verbosity level.
106
 * @return an integer at the current verbosity level.
107
 */
108
int
109
pobyso_get_verbosity();
110

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

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

    
144
/**
145
 * Check if an expression is a polynomial term (monome)
146
 * (a constant * the free variable to an integer positive or null power or
147
 *  the free variable to an integer positive or null power * a constant).
148
 * @param exprSo: a Sollya functional expression object;
149
 * @return 1 if exprSo is a polynomial term (as defined above), 0 otherwise.
150
 */
151
int pobyso_is_polynomial_term(pobyso_func_exp_t exprSo);
152

    
153
/**
154
 * Check if an expression is an integer.
155
 */
156
int
157
pobyso_is_int(pobyso_func_exp_t exprSo);
158

    
159
/**
160
 * Create a Sollya monomial from a Sollya constant,
161
 * the coefficient, and an integer, the exponent.
162
 * @param coefficient must be a non NULL constant expression;
163
 * @param degree must be a non negative integer;
164
 * @return a Sollya functional expression if successes, or a Sollya error
165
 * if fails.
166
 */
167
pobyso_func_exp_t
168
pobyso_new_monomial(pobyso_func_exp_t coefficient, long degree);
169

    
170
/**
171
 * Create a Sollya "off" object. */
172
pobyso_on_off_t
173
pobyso_on();
174

    
175
/**
176
 * Create a Sollya "on" object. */
177
pobyso_on_off_t
178
pobyso_off();
179

    
180
/**
181
 * A wrapper around the Sollya Remez function.
182
 */
183
/**
184
 * Parse a string to create a Sollya object.
185
 * A very thin wrapper around the sollya_lib_parse_string() function.
186
 * If the final ";" is forgotten in the expression, it is added by the
187
 * function.
188
 * @return a Sollya functional expression if successes, or a Sollya error
189
 * if fails.
190
 */
191
pobyso_func_exp_t
192
pobyso_parse_string(const char* expression);
193

    
194
/** Disable the console output from Sollya.
195
 * @return a Sollya constant holding the current verbosity level
196
 *         when Sollya was silenced or NULL, if something goes wrong.*/
197
pobyso_constant_t
198
pobyso_quiet();
199

    
200
/** Create a Sollya range from two MPFR bounds.
201
 * A wrapper around sollya_lib_range_from_bounds.
202
 */
203
pobyso_range_t
204
pobyso_range_from_bounds(mpfr_t lowerBound, mpfr_t upperBound);
205

    
206
/**
207
 * A wrapper around the Sollya Remez function with the canonical monomials
208
 * base.
209
 */
210

    
211
pobyso_func_exp_t
212
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
213
                                      long int degree,
214
                                      pobyso_range_t interval,
215
                                      pobyso_func_exp_t weight,
216
                                      double quality,
217
                                      pobyso_range_t bounds);
218

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

    
224
pobyso_func_exp_t
225
pobyso_remez_sparse_monomials_base(pobyso_func_exp_t);
226

    
227
/**
228
 * A wrapper around the Sollya Remez function with the canonical monomials
229
 * base.
230
 */
231

    
232
pobyso_func_exp_t
233
pobyso_remez_arbitrary_base(pobyso_func_exp_t);
234

    
235
/**
236
 * Set the canonical mode.
237
 */
238

    
239
int
240
pobyso_set_canonical_on(void);
241

    
242
/**
243
 * Set the verbosity mode off (level 0).
244
 * The current level of verbosity is returned.
245
 */
246
int
247
pobyso_set_verbosity_off(void);
248

    
249
/**
250
 * Set the verbosity level to newVerbosityLevel.
251
 * @param newVerbosityLevel must be a Sollya object corresponding to an
252
 *        integer constant.
253
 */
254
int
255
pobyso_set_verbosity_to(int newVerbosityLevel);
256

    
257
/**
258
 * Wrapper around the sollya_lib_subpoly Sollya function.
259
 */
260

    
261
pobyso_func_exp_t
262
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
263

    
264
#if 0
265
/**
266
 * Create the canonical (non sparse) base of monomials for a given degree.
267
 */
268
chain*
269
pobyso_create_canonical_monomials_base(const unsigned int degree);
270

271
/**
272
 * Create a chain from an array of int.
273
 */
274
sollya_int_list
275
pobyso_create_int_list_from_int_Array(int* intArray,
276
                                    const unsigned int arrayLength);
277

278
/**
279
 * Create a chain from an array of int.
280
 */
281
sollya_int_list_t
282
pobyso_create_int_list_from_unsigned_int_array(unsigned int* intArray,
283
                                            const unsigned int arrayLength);
284
/**
285
 * Differentiation of a function.
286
 * A slim wrapper around the Sollya differentiate function.
287
 * @param functionNode - the Sollya node to differentiate;
288
 * @return a node representing the function differentiated or NULL, if
289
 *         something goes wrong.
290
 */
291

292
sollya_obj_t
293
pobyso_diff(sollya_obj_t function);
294

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

313

314
/**
315
 * Faithful evaluation of an expression.
316
 *
317
 * @param faithfulEvaluation - holds the result, must be "inited" by the
318
 *                             caller;
319
 */
320
int
321
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
322
                          node *nodeToEvaluate,
323
                          mpfr_t argument,
324
                          mpfr_prec_t precision);
325

326
/**
327
 * Find the zeros of a function on a given interval.
328
 */
329
chain*
330
pobyso_find_zeros(node *function,
331
                  mpfr_t *lowerBound,
332
                  mpfr_t *upperBound);
333
/**
334
 * Free a chain of node.
335
 * All elements of the chain have to be nodes.
336
 */
337
void
338
pobyso_free_chain_of_nodes(chain *theChain);
339

340
/**
341
 * Free a range.
342
 * It involves clearing the mpfr_t elements and deallocating the
343
 * pointers.
344
 */
345
void
346
pobyso_free_range(rangetype range);
347

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

372
/** Compute a polynomial approximation in the canonical monomials basis for
373
 *  a function, for a given precision. The returned polynomial has the minimal
374
 *  degree to achieve the required precision.
375
 */
376
node*
377
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
378
                                                      unsigned int mode,
379
                                                      mpfr_t lowerBound,
380
                                                      mpfr_t upperBound,
381
                                                      mpfr_t eps);
382

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