Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (10,83 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 sollya_obj_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_UNFAITHFULL (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
 * Get the current verbosity level.
74
 * @return an integer at the current verbosity level.
75
 */
76
int
77
pobyso_get_verbosity();
78

    
79
/**
80
 * Evaluate an expression for a constant.
81
 *
82
 * The result of the evaluation must be "inited" by the caller.
83
 * Its contents is modified only if the evaluation yields some usefull
84
 * result.
85
 *@param functionSo : the function to evaluate;
86
 *@param argumentMp : the argument used for the evaluation;
87
 *@param evalutionMp: the result of the evalution.
88
 *@retun 0 if 0K, 1 in case of a "generic" error, POBYSO_UNFAITHFULL if the
89
 *       result is the mean of the two bound of the range encompassing it
90
 *       and POBYSO_NAN if the result of the evaluation is not a number.
91
 */
92
int
93
pobyso_evaluate_constant(pobyso_func_exp_t functionSo,
94
                         mpfr_t argumentMp,
95
                         mpfr_t evaluationMp);
96
/**
97
 * Check if a sollya object is a constant expression.
98
 * @return 1 if true and zero otherwise
99
 */
100
int
101
pobyso_is_constant_expression(sollya_obj_t obj_to_text);
102

    
103
/**
104
 * Check if an expression is a monomial (the free variable to an integer
105
 * positive or null power.
106
 * @param exprSo: a Sollya functional expression object;
107
 * @return 1 if exprSo is a monomial (as defined above), 0 otherwise.
108
 */
109
int pobyso_is_monomial(pobyso_func_exp_t exprSo);
110

    
111
/**
112
 * Check if an expression is a polynomial term (monome)
113
 * (a constant * the free variable to an integer positive or null power or
114
 *  the free variable to an integer positive or null power * a constant).
115
 * @param exprSo: a Sollya functional expression object;
116
 * @return 1 if exprSo is a polynomial term (as defined above), 0 otherwise.
117
 */
118
int pobyso_is_polynomial_term(pobyso_func_exp_t exprSo);
119

    
120
/**
121
 * Check if an expression is an integer.
122
 */
123
int
124
pobyso_is_int(pobyso_func_exp_t exprSo);
125

    
126
/**
127
 * Create a Sollya monomial from a Sollya constant,
128
 * the coefficient, and an integer, the exponent.
129
 * @param coefficient must be a non NULL constant expression;
130
 * @param degree must be a non negative integer;
131
 * @return a Sollya functional expression if successes, or a Sollya error
132
 * if fails.
133
 */
134
pobyso_func_exp_t
135
pobyso_new_monomial(pobyso_func_exp_t coefficient, long degree);
136

    
137
/**
138
 * A wrapper around the Sollya Remez function.
139
 */
140
/**
141
 * Parse a string to create a Sollya object.
142
 * A very thin wrapper around the sollya_lib_parse_string() function.
143
 * If the final ";" is forgotten in the expression, it is added by the
144
 * function.
145
 * @return a Sollya functional expression if successes, or a Sollya error
146
 * if fails.
147
 */
148
pobyso_func_exp_t
149
pobyso_parse_string(const char* expression);
150

    
151
/**
152
 * A wrapper around the Sollya Remez function with the canonical monomials
153
 * base.
154
 */
155

    
156
pobyso_func_exp_t
157
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
158
                                      long int degree,
159
                                      pobyso_range_t interval,
160
                                      pobyso_func_exp_t weight,
161
                                      double quality,
162
                                      pobyso_range_t bounds);
163

    
164
/**
165
 * A wrapper around the Sollya Remez function with the a sparse monomials
166
 * base.
167
 */
168

    
169
pobyso_func_exp_t
170
pobyso_remez_sparse_monomials_base(pobyso_func_exp_t);
171

    
172
/**
173
 * A wrapper around the Sollya Remez function with the canonical monomials
174
 * base.
175
 */
176

    
177
pobyso_func_exp_t
178
pobyso_remez_arbitrary_base(pobyso_func_exp_t);
179

    
180
/**
181
 * Set the canonical mode.
182
 */
183

    
184
int
185
pobyso_set_canonical_on(void);
186

    
187
/**
188
 * Set the verbosity mode off (level 0).
189
 * The current level of verbosity is returned.
190
 */
191
int
192
pobyso_set_verbosity_off(void);
193

    
194
/**
195
 * Set the verbosity level to newVerbosityLevel.
196
 * @param newVerbosityLevel must be a Sollya object corresponding to an
197
 *        integer constant.
198
 */
199
int
200
pobyso_set_verbosity_to(int newVerbosityLevel);
201

    
202
/**
203
 * Wrapper around the sollya_lib_subpoly Sollya function.
204
 */
205

    
206
pobyso_func_exp_t
207
pobyso_subpoly(pobyso_func_exp_t polynomial, long expsNum, long* expsList);
208

    
209
#if 0
210
/**
211
 * Create the canonical (non sparse) base of monomials for a given degree.
212
 */
213
chain*
214
pobyso_create_canonical_monomials_base(const unsigned int degree);
215

216
/**
217
 * Create a chain from an array of int.
218
 */
219
sollya_int_list
220
pobyso_create_int_list_from_int_Array(int* intArray,
221
                                    const unsigned int arrayLength);
222

223
/**
224
 * Create a chain from an array of int.
225
 */
226
sollya_int_list_t
227
pobyso_create_int_list_from_unsigned_int_array(unsigned int* intArray,
228
                                            const unsigned int arrayLength);
229
/**
230
 * Differentiation of a function.
231
 * A slim wrapper around the Sollya differentiate function.
232
 * @param functionNode - the Sollya node to differentiate;
233
 * @return a node representing the function differentiated or NULL, if
234
 *         something goes wrong.
235
 */
236

237
sollya_obj_t
238
pobyso_diff(sollya_obj_t function);
239

240
/**
241
 * A match to the Sollya dirtyinfnorm.
242
 * A slim wrapper around the Sollya function.
243
 * @param infnorm - out parameter to return the result, must be "inited"
244
 *                  and "cleared" by the caller;
245
 * @param functionNode - the Sollya node to compute the infinite norm of;
246
 * @param lowerBound - the lower bound of the interval;
247
 * @param upperBound - the upper bound of the interval;
248
 * @param precision  - the internal precision Sollya must use.
249
 * @return 0 if everything is OK, != 0 if something goes wrong.
250
 */
251
int
252
pobyso_dirty_infnorm(mpfr_t infNorm,
253
                      node *functionNode,
254
                      mpfr_t lowerBound,
255
                      mpfr_t upperBound,
256
                      mp_prec_t precision);
257

258

259
/**
260
 * Faithful evaluation of an expression.
261
 *
262
 * @param faitufulEvaluation - holds the result, must be "inited" by the
263
 *                             caller;
264
 */
265
int
266
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
267
                          node *nodeToEvaluate,
268
                          mpfr_t argument,
269
                          mpfr_prec_t precision);
270

271
/**
272
 * Find the zeros of a function on a given interval.
273
 */
274
chain*
275
pobyso_find_zeros(node *function,
276
                  mpfr_t *lowerBound,
277
                  mpfr_t *upperBound);
278
/**
279
 * Free a chain of node.
280
 * All elements of the chain have to be nodes.
281
 */
282
void
283
pobyso_free_chain_of_nodes(chain *theChain);
284

285
/**
286
 * Free a range.
287
 * It involves clearing the mpfr_t elements and deallocating the
288
 * pointers.
289
 */
290
void
291
pobyso_free_range(rangetype range);
292

293
/**
294
 * Computes a good polynomial approximation with fixed-point or floating-point
295
 * coefficients.
296
 */
297
node*
298
pobyso_fp_minimax_canonical_monomials_base(node *function,
299
                                          int degree,
300
                                          chain *formats,
301
                                          chain *points,
302
                                          mpfr_t lowerBound,
303
                                          mpfr_t upperBound,
304
                                          int fpFixedArg,
305
                                          int absRel,
306
                                          node *constPart,
307
                                          node *minimax);
308
/**
309
 * Parses a string to build the node representing the function.
310
 * In fact, does nothing for the moment: the string must be a correct function
311
 * definition. No error correction takes place here.
312
 */
313
node*
314
pobyso_parse_function(char *functionString,
315
                      char *freeVariableNameString);
316

317
/** Compute a polynomial approximation in the canonical monomials basis for
318
 *  a function, for a given precision. The returned polynomial has the minimal
319
 *  degree to achieve the required precision.
320
 */
321
node*
322
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
323
                                                      unsigned int mode,
324
                                                      mpfr_t lowerBound,
325
                                                      mpfr_t upperBound,
326
                                                      mpfr_t eps);
327

328
/**
329
 * Computes a the remez approximation of a function.
330
 * @param function   - the node holding the function to approximate;
331
 * @param weight     - the node holding the weight function, can be NULL. In
332
 *                     this case a default weight of "1" will be provided and
333
 *                     the approximation is related is with respect to the
334
 *                     absolute error.
335
 * @param degree     - the degree of the approximation polynomial;
336
 * @param lowerBound - the lower bound of the approximation interval;
337
 * @param upperBound - the upper bound of the approximation interval;
338
 * @param quality    - quality = (eps - eps*) / eps*; the search stop when the required
339
 *                     quality is achieved.
340
 *
341
 * @return a node holding the approximation polynomial in Horner form.
342
 */
343
node*
344
pobyso_remez_canonical_monomials_base(node *function,
345
                                       node *weight,
346
                                       unsigned int degree,
347
                                       mpfr_t lowerBound,
348
                                       mpfr_t upperBound,
349
                                       mpfr_t quality);
350
#endif
351
#define POBYSO_h  
352
  
353
#endif
354