root / pobysoC-4.0 / src / pobyso.c @ 50
Historique | Voir | Annoter | Télécharger (18,79 ko)
1 | 26 | storres | /** @file pobyso.c
|
---|---|---|---|
2 | 26 | storres | * Name & purpose
|
3 | 26 | storres | *
|
4 | 26 | storres | * @author S.T.
|
5 | 26 | storres | * @date 2011-10-12
|
6 | 26 | storres | *
|
7 | 27 | storres | *
|
8 | 26 | storres | */
|
9 | 50 | storres | |
10 | 26 | storres | /******************************************************************************/
|
11 | 26 | storres | /* Headers, applying the "particular to general" convention.*/
|
12 | 26 | storres | |
13 | 26 | storres | #include "pobyso.h" |
14 | 26 | storres | |
15 | 26 | storres | /* includes of local headers */
|
16 | 26 | storres | |
17 | 26 | storres | /* includes of project headers */
|
18 | 26 | storres | |
19 | 26 | storres | /* includes of system headers */
|
20 | 26 | storres | |
21 | 26 | storres | /* Other declarations */
|
22 | 26 | storres | |
23 | 26 | storres | /* Internal prototypes */
|
24 | 26 | storres | void
|
25 | 26 | storres | pobyso_error_message(char *functionName, char *messageName, char* message); |
26 | 26 | storres | /* Types, constants and macros definitions */
|
27 | 26 | storres | |
28 | 26 | storres | /* Global variables */
|
29 | 26 | storres | |
30 | 26 | storres | /* Functions */
|
31 | 26 | storres | void
|
32 | 26 | storres | pobyso_autoprint(sollya_obj_t solObj, ...) |
33 | 26 | storres | { |
34 | 26 | storres | va_list va; |
35 | 26 | storres | va_start(va, solObj); |
36 | 26 | storres | sollya_lib_v_autoprint(solObj, va); |
37 | 26 | storres | va_end(va); |
38 | 26 | storres | } /* End pobyso_autoprint. */
|
39 | 26 | storres | |
40 | 26 | storres | sollya_obj_t |
41 | 26 | storres | pobyso_parse_string(const char* expression) |
42 | 26 | storres | { |
43 | 26 | storres | if (expression == NULL) |
44 | 26 | storres | { |
45 | 26 | storres | pobyso_error_message("pobyso_parse_string",
|
46 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
47 | 26 | storres | "The expression is a NULL pointer");
|
48 | 26 | storres | return(NULL); |
49 | 26 | storres | } |
50 | 26 | storres | return(sollya_lib_parse_string(expression));
|
51 | 26 | storres | } /* pobyso_parse_string */
|
52 | 26 | storres | |
53 | 26 | storres | void
|
54 | 26 | storres | pobyso_set_verbosity_off(sollya_obj_t* currentVerbosityLevel) |
55 | 26 | storres | { |
56 | 26 | storres | sollya_obj_t verbosityLevelZero; |
57 | 26 | storres | if (currentVerbosityLevel != NULL) |
58 | 26 | storres | { |
59 | 26 | storres | *currentVerbosityLevel = sollya_lib_get_verbosity(); |
60 | 26 | storres | } |
61 | 26 | storres | verbosityLevelZero = sollya_lib_constant_from_int(0);
|
62 | 26 | storres | sollya_lib_set_verbosity(verbosityLevelZero); |
63 | 26 | storres | sollya_lib_clear_obj(verbosityLevelZero); |
64 | 26 | storres | } /* End of pobyso_set_verbosity_off. */
|
65 | 26 | storres | |
66 | 26 | storres | void
|
67 | 26 | storres | pobyso_set_verbosity_to(sollya_obj_t newVerbosityLevel) |
68 | 26 | storres | { |
69 | 26 | storres | if (newVerbosityLevel == NULL) |
70 | 26 | storres | { |
71 | 26 | storres | pobyso_error_message("pobyso_set_verbosity_to",
|
72 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
73 | 26 | storres | "The new verbosity level is a NULL pointer");
|
74 | 26 | storres | return;
|
75 | 26 | storres | } |
76 | 26 | storres | sollya_lib_set_verbosity(newVerbosityLevel); |
77 | 26 | storres | } /* End of pobyso_set_verbosity_to. */
|
78 | 26 | storres | |
79 | 26 | storres | /* Attic from the sollya_lib < 4. */
|
80 | 26 | storres | #if 0
|
81 | 26 | storres | chain*
|
82 | 26 | storres | pobyso_create_canonical_monomials_base(const unsigned int degree)
|
83 | 26 | storres | {
|
84 | 26 | storres | int i = 0;
|
85 | 26 | storres | chain *monomials = NULL;
|
86 | 26 | storres | node *monomial = NULL;
|
87 | 26 | storres | |
88 | 26 | storres | for(i = degree ; i >= 0 ; i--)
|
89 | 26 | storres | {
|
90 | 26 | storres | monomial = makePow(makeVariable(), makeConstantDouble((double)i));
|
91 | 26 | storres | monomials = addElement(monomials, monomial);
|
92 | 26 | storres | fprintf(stderr, "pobyso_create_canonical_monomials_base: %u\n", i);
|
93 | 26 | storres | }
|
94 | 26 | storres | if (monomials == NULL)
|
95 | 26 | storres | {
|
96 | 26 | storres | pobyso_error_message("pobyso_create_canonical_monomial_base",
|
97 | 26 | storres | "CHAIN_CREATION_ERROR",
|
98 | 26 | storres | "Could not create the monomials chain");
|
99 | 26 | storres | return(NULL);
|
100 | 26 | storres | }
|
101 | 26 | storres | return(monomials);
|
102 | 26 | storres | } /* End pobyso_create_canonical_monomials_base. */
|
103 | 26 | storres | |
104 | 26 | storres | chain*
|
105 | 26 | storres | pobyso_create_chain_from_int_array(int* intArray,
|
106 | 26 | storres | const unsigned int arrayLength)
|
107 | 26 | storres | {
|
108 | 26 | storres | int i = 0;
|
109 | 26 | storres | chain *newChain = NULL;
|
110 | 26 | storres | int *currentInt;
|
111 | 26 | storres | |
112 | 26 | storres | if (arrayLength == 0) return(NULL);
|
113 | 26 | storres | if (intArray == NULL)
|
114 | 26 | storres | {
|
115 | 26 | storres | pobyso_error_message("pobyso_create_chain_from_int_array",
|
116 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
117 | 26 | storres | "The array is a NULL pointer");
|
118 | 26 | storres | return(NULL);
|
119 | 26 | storres | }
|
120 | 26 | storres | for (i = arrayLength - 1 ; i >= 0 ; i--)
|
121 | 26 | storres | {
|
122 | 26 | storres | currentInt = malloc(sizeof(int));
|
123 | 26 | storres | if (currentInt == NULL)
|
124 | 26 | storres | {
|
125 | 26 | storres | pobyso_error_message("pobyso_create_chain_from_int_array",
|
126 | 26 | storres | "MEMORY_ALLOCATION_ERROR",
|
127 | 26 | storres | "Could not allocate one of the integers");
|
128 | 26 | storres | freeChain(newChain, free);
|
129 | 26 | storres | return(NULL);
|
130 | 26 | storres | }
|
131 | 26 | storres | *currentInt = intArray[i];
|
132 | 26 | storres | newChain = addElement(newChain, currentInt);
|
133 | 26 | storres | }
|
134 | 26 | storres | return(newChain);
|
135 | 26 | storres | } // End pobyso_create_chain_from_int_array. */
|
136 | 26 | storres | |
137 | 26 | storres | chain*
|
138 | 26 | storres | pobyso_create_chain_from_unsigned_int_array(unsigned int* intArray,
|
139 | 26 | storres | const unsigned int arrayLength)
|
140 | 26 | storres | {
|
141 | 26 | storres | int i = 0;
|
142 | 26 | storres | chain *newChain = NULL;
|
143 | 26 | storres | unsigned int *currentInt;
|
144 | 26 | storres | |
145 | 26 | storres | /* Argument checking. */
|
146 | 26 | storres | if (arrayLength == 0) return(NULL);
|
147 | 26 | storres | if (intArray == NULL)
|
148 | 26 | storres | {
|
149 | 26 | storres | pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
|
150 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
151 | 26 | storres | "The array is a NULL pointer");
|
152 | 26 | storres | return(NULL);
|
153 | 26 | storres | }
|
154 | 26 | storres | for (i = arrayLength - 1 ; i >= 0 ; i--)
|
155 | 26 | storres | {
|
156 | 26 | storres | currentInt = malloc(sizeof(unsigned int));
|
157 | 26 | storres | if (currentInt == NULL)
|
158 | 26 | storres | {
|
159 | 26 | storres | pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
|
160 | 26 | storres | "MEMORY_ALLOCATION_ERROR",
|
161 | 26 | storres | "Could not allocate one of the integers");
|
162 | 26 | storres | freeChain(newChain, free);
|
163 | 26 | storres | return(NULL);
|
164 | 26 | storres | }
|
165 | 26 | storres | *currentInt = intArray[i];
|
166 | 26 | storres | newChain = addElement(newChain, currentInt);
|
167 | 26 | storres | }
|
168 | 26 | storres | return(newChain);
|
169 | 26 | storres | } // End pobyso_create_chain_from_unsigned_int_array. */
|
170 | 26 | storres | |
171 | 26 | storres | node*
|
172 | 26 | storres | pobyso_differentiate(node *functionNode)
|
173 | 26 | storres | {
|
174 | 26 | storres | /* Argument checking. */
|
175 | 26 | storres | node *differentialNode;
|
176 | 26 | storres | if (functionNode == NULL)
|
177 | 26 | storres | {
|
178 | 26 | storres | pobyso_error_message("pobyso_differentiate",
|
179 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
180 | 26 | storres | "The function to differentiate is a NULL pointer");
|
181 | 26 | storres | return(NULL);
|
182 | 26 | storres | }
|
183 | 26 | storres | differentialNode = differentiate(functionNode);
|
184 | 26 | storres | if (differentialNode == NULL)
|
185 | 26 | storres | {
|
186 | 26 | storres | pobyso_error_message("pobyso_differentiate",
|
187 | 26 | storres | "INTERNAL ERROR",
|
188 | 26 | storres | "Sollya could not differentiate the function");
|
189 | 26 | storres | }
|
190 | 26 | storres | return(differentialNode);
|
191 | 26 | storres | } // End pobyso_differentiate
|
192 | 26 | storres | |
193 | 26 | storres | |
194 | 26 | storres | int
|
195 | 26 | storres | pobyso_dirty_infnorm(mpfr_t infNorm,
|
196 | 26 | storres | node *functionNode,
|
197 | 26 | storres | mpfr_t lowerBound,
|
198 | 26 | storres | mpfr_t upperBound,
|
199 | 26 | storres | mp_prec_t precision)
|
200 | 26 | storres | {
|
201 | 26 | storres | int functionCallResult;
|
202 | 26 | storres | /* Arguments checking. */
|
203 | 26 | storres | if (functionNode == NULL)
|
204 | 26 | storres | {
|
205 | 26 | storres | pobyso_error_message("pobyso_dirty_infnorm",
|
206 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
207 | 26 | storres | "The function to compute is a NULL pointer");
|
208 | 26 | storres | return(1);
|
209 | 26 | storres | }
|
210 | 26 | storres | if (mpfr_cmp(lowerBound, upperBound) > 0)
|
211 | 26 | storres | {
|
212 | 26 | storres | pobyso_error_message("pobyso_dirty_infnorm",
|
213 | 26 | storres | "INCOHERENT_INPUT_DATA",
|
214 | 26 | storres | "The lower bond is greater than the upper bound");
|
215 | 26 | storres | return(1);
|
216 | 26 | storres | }
|
217 | 26 | storres | /* Particular cases. */
|
218 | 26 | storres | if (mpfr_cmp(lowerBound, upperBound) == 0)
|
219 | 26 | storres | {
|
220 | 26 | storres | functionCallResult = pobyso_evaluate_faithful(infNorm,
|
221 | 26 | storres | functionNode,
|
222 | 26 | storres | lowerBound,
|
223 | 26 | storres | precision);
|
224 | 26 | storres | return(functionCallResult);
|
225 | 26 | storres | }
|
226 | 26 | storres | if (isConstant(functionNode))
|
227 | 26 | storres | {
|
228 | 26 | storres | functionCallResult = pobyso_evaluate_faithful(infNorm,
|
229 | 26 | storres | functionNode,
|
230 | 26 | storres | lowerBound,
|
231 | 26 | storres | precision);
|
232 | 26 | storres | if (!functionCallResult)
|
233 | 26 | storres | {
|
234 | 26 | storres | mpfr_abs(infNorm, infNorm, MPFR_RNDN);
|
235 | 26 | storres | }
|
236 | 26 | storres | return(functionCallResult);
|
237 | 26 | storres | }
|
238 | 26 | storres | uncertifiedInfnorm(infNorm,
|
239 | 26 | storres | functionNode,
|
240 | 26 | storres | lowerBound,
|
241 | 26 | storres | upperBound,
|
242 | 26 | storres | POBYSO_DEFAULT_POINTS,
|
243 | 26 | storres | precision);
|
244 | 26 | storres | return(0);
|
245 | 26 | storres | } /* End pobyso_dirty_infnorm. */
|
246 | 26 | storres | |
247 | 26 | storres | int
|
248 | 26 | storres | pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
|
249 | 26 | storres | node *nodeToEvaluate,
|
250 | 26 | storres | mpfr_t argument,
|
251 | 26 | storres | mpfr_prec_t precision)
|
252 | 26 | storres | {
|
253 | 26 | storres | /* Check input arguments. */
|
254 | 26 | storres | if (nodeToEvaluate == NULL)
|
255 | 26 | storres | {
|
256 | 26 | storres | pobyso_error_message("pobyso_evaluate_faithful",
|
257 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
258 | 26 | storres | "nodeToEvaluate is a NULL pointer");
|
259 | 26 | storres | return(1);
|
260 | 26 | storres | }
|
261 | 26 | storres | evaluateFaithful(faithfulEvaluation,
|
262 | 26 | storres | nodeToEvaluate,
|
263 | 26 | storres | argument,
|
264 | 26 | storres | precision);
|
265 | 26 | storres | return(0);
|
266 | 26 | storres | } /* End pobyso_evaluate_faithfull. */
|
267 | 26 | storres | |
268 | 26 | storres | chain*
|
269 | 26 | storres | pobyso_find_zeros(node *function,
|
270 | 26 | storres | mpfr_t *lower_bound,
|
271 | 26 | storres | mpfr_t *upper_bound)
|
272 | 26 | storres | {
|
273 | 26 | storres | mp_prec_t currentPrecision;
|
274 | 26 | storres | mpfr_t currentDiameter;
|
275 | 26 | storres | rangetype bounds;
|
276 | 26 | storres | |
277 | 26 | storres | currentPrecision = getToolPrecision();
|
278 | 26 | storres | mpfr_init2(currentDiameter, currentPrecision);
|
279 | 26 | storres | |
280 | 26 | storres | bounds.a = lower_bound;
|
281 | 26 | storres | bounds.b = upper_bound;
|
282 | 26 | storres | |
283 | 26 | storres | if (bounds.a == NULL || bounds.b == NULL)
|
284 | 26 | storres | {
|
285 | 26 | storres | pobyso_error_message("pobyso_find_zeros",
|
286 | 26 | storres | "MEMORY_ALLOCATION_ERROR",
|
287 | 26 | storres | "Could not allocate one of the bounds");
|
288 | 26 | storres | return(NULL);
|
289 | 26 | storres | }
|
290 | 26 | storres | return(findZerosFunction(function,
|
291 | 26 | storres | bounds,
|
292 | 26 | storres | currentPrecision,
|
293 | 26 | storres | currentDiameter));
|
294 | 26 | storres | } /* End pobyso_find_zeros. */
|
295 | 26 | storres | |
296 | 26 | storres | void
|
297 | 26 | storres | pobyso_free_chain_of_nodes(chain *theChainOfNodes)
|
298 | 26 | storres | {
|
299 | 26 | storres | node *currentNode = NULL;
|
300 | 26 | storres | chain *currentChainElement = NULL;
|
301 | 26 | storres | chain *nextChainElement = NULL;
|
302 | 26 | storres | |
303 | 26 | storres | nextChainElement = theChainOfNodes;
|
304 | 26 | storres | |
305 | 26 | storres | while(nextChainElement != NULL)
|
306 | 26 | storres | {
|
307 | 26 | storres | currentChainElement = nextChainElement;
|
308 | 26 | storres | currentNode = (node*)(currentChainElement->value);
|
309 | 26 | storres | nextChainElement = nextChainElement->next;
|
310 | 26 | storres | free_memory(currentNode);
|
311 | 26 | storres | free((void*)(currentChainElement));
|
312 | 26 | storres | }
|
313 | 26 | storres | } /* End pobyso_free_chain_of_nodes. */
|
314 | 26 | storres | |
315 | 26 | storres | void
|
316 | 26 | storres | pobyso_free_range(rangetype range)
|
317 | 26 | storres | {
|
318 | 26 | storres | |
319 | 26 | storres | mpfr_clear(*(range.a));
|
320 | 26 | storres | mpfr_clear(*(range.b));
|
321 | 26 | storres | free(range.a);
|
322 | 26 | storres | free(range.b);
|
323 | 26 | storres | } /* End pobyso_free_range. */
|
324 | 26 | storres | |
325 | 26 | storres | node*
|
326 | 26 | storres | pobyso_fp_minimax_canonical_monomials_base(node *function,
|
327 | 26 | storres | int degree,
|
328 | 26 | storres | chain *formats,
|
329 | 26 | storres | chain *points,
|
330 | 26 | storres | mpfr_t lowerBound,
|
331 | 26 | storres | mpfr_t upperBound,
|
332 | 26 | storres | int fpFixedArg,
|
333 | 26 | storres | int absRel,
|
334 | 26 | storres | node *constPart,
|
335 | 26 | storres | node *minimax)
|
336 | 26 | storres | {
|
337 | 26 | storres | return(NULL);
|
338 | 26 | storres | } /* End pobyso_fp_minimax_canonical_monomials_base. */
|
339 | 26 | storres | |
340 | 26 | storres | node*
|
341 | 26 | storres | pobyso_parse_function(char *functionString,
|
342 | 26 | storres | char *freeVariableNameString)
|
343 | 26 | storres | {
|
344 | 26 | storres | if (functionString == NULL || freeVariableNameString == NULL)
|
345 | 26 | storres | {
|
346 | 26 | storres | pobyso_error_message("pobyso_parse_function",
|
347 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
348 | 26 | storres | "One of the arguments is a NULL pointer");
|
349 | 26 | storres | return(NULL);
|
350 | 26 | storres | }
|
351 | 26 | storres | return(parseString(functionString));
|
352 | 26 | storres | |
353 | 26 | storres | } /* End pobyso_parse_function */
|
354 | 26 | storres | |
355 | 26 | storres | node*
|
356 | 26 | storres | pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
|
357 | 26 | storres | unsigned int mode,
|
358 | 26 | storres | mpfr_t lowerBound,
|
359 | 26 | storres | mpfr_t upperBound,
|
360 | 26 | storres | mpfr_t eps)
|
361 | 26 | storres | {
|
362 | 26 | storres | node *weight = NULL;
|
363 | 26 | storres | node *bestApproxPolyNode = NULL;
|
364 | 26 | storres | node *bestApproxHorner = NULL;
|
365 | 26 | storres | node *errorNode = NULL;
|
366 | 26 | storres | rangetype degreeRange;
|
367 | 26 | storres | mpfr_t quality;
|
368 | 26 | storres | mpfr_t currentError;
|
369 | 26 | storres | unsigned int degree;
|
370 | 26 | storres | |
371 | 26 | storres | /* Check the parameters. */
|
372 | 26 | storres | if (functionNode == NULL)
|
373 | 26 | storres | {
|
374 | 26 | storres | pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
|
375 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
376 | 26 | storres | "functionNode is a NULL pointer");
|
377 | 26 | storres | return(NULL);
|
378 | 26 | storres | }
|
379 | 26 | storres | if (mpfr_cmp(lowerBound, upperBound) >= 0)
|
380 | 26 | storres | {
|
381 | 26 | storres | pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
|
382 | 26 | storres | "INCOHERENT_INPUT_DATA",
|
383 | 26 | storres | "the lower_bound >= upper_bound");
|
384 | 26 | storres | return(NULL);
|
385 | 26 | storres | }
|
386 | 26 | storres | /* Set the weight. */
|
387 | 26 | storres | if (mode == POBYSO_ABSOLUTE)
|
388 | 26 | storres | {
|
389 | 26 | storres | /* Set the weight to 1 for the ABSOLUTE_MODE. */
|
390 | 26 | storres | weight = makeConstantDouble(1.0);
|
391 | 26 | storres | }
|
392 | 26 | storres | else
|
393 | 26 | storres | {
|
394 | 26 | storres | if (mode == POBYSO_RELATIVE)
|
395 | 26 | storres | {
|
396 | 26 | storres | pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
|
397 | 26 | storres | "NOT_IMPLEMENTED",
|
398 | 26 | storres | "the search for relative error is not implemented yet");
|
399 | 26 | storres | return(NULL);
|
400 | 26 | storres | }
|
401 | 26 | storres | else
|
402 | 26 | storres | {
|
403 | 26 | storres | pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
|
404 | 26 | storres | "INCOHERENT_INPUT_DATA",
|
405 | 26 | storres | "the mode is node of POBYSO_ABOLUTE or POBYSO_RELATIVE");
|
406 | 26 | storres | return(NULL);
|
407 | 26 | storres | }
|
408 | 26 | storres | }
|
409 | 26 | storres | //fprintf(stderr, "\n\n\n******* I'm here! ********\n\n\n");
|
410 | 26 | storres | degreeRange = guessDegree(functionNode,
|
411 | 26 | storres | weight,
|
412 | 26 | storres | lowerBound,
|
413 | 26 | storres | upperBound,
|
414 | 26 | storres | eps,
|
415 | 26 | storres | POBYSO_GUESS_DEGREE_BOUND);
|
416 | 26 | storres | degree = mpfr_get_ui(*(degreeRange.a), MPFR_RNDN);
|
417 | 26 | storres | //fprintf(stderr, "\n\n\n******* I'm back! ********\n\n\n");
|
418 | 26 | storres | fprintf(stderr, "Guessed degree: ");
|
419 | 26 | storres | mpfr_out_str(stderr, 10, 17, *(degreeRange.a), MPFR_RNDN);
|
420 | 26 | storres | fprintf(stderr, " - as int: %u\n", degree);
|
421 | 26 | storres | /* Reduce the degree by 1 in the foolish hope it could work. */
|
422 | 26 | storres | if (degree > 0) degree--;
|
423 | 26 | storres | /* Both elements of degreeRange where "inited" within guessDegree. */
|
424 | 26 | storres | mpfr_clear(*(degreeRange.a));
|
425 | 26 | storres | mpfr_clear(*(degreeRange.b));
|
426 | 26 | storres | free(degreeRange.a);
|
427 | 26 | storres | free(degreeRange.b);
|
428 | 26 | storres | /* Mimic the default behavior of interactive Sollya. */
|
429 | 26 | storres | mpfr_init(quality);
|
430 | 26 | storres | mpfr_set_d(quality, 1e-5, MPFR_RNDN);
|
431 | 26 | storres | mpfr_init2(currentError, getToolPrecision());
|
432 | 26 | storres | mpfr_set_inf(currentError,1);
|
433 | 26 | storres | |
434 | 26 | storres | /* Try to refine the initial guess: loop with increasing degrees until
|
435 | 26 | storres | * we find a satisfactory one. */
|
436 | 26 | storres | while(mpfr_cmp(currentError, eps) > 0)
|
437 | 26 | storres | {
|
438 | 26 | storres | /* Get rid of the previous polynomial, if any. */
|
439 | 26 | storres | if (bestApproxPolyNode != NULL)
|
440 | 26 | storres | {
|
441 | 26 | storres | free_memory(bestApproxPolyNode);
|
442 | 26 | storres | }
|
443 | 26 | storres | fprintf(stderr, "Degree: %u\n", degree);
|
444 | 26 | storres | fprintf(stderr, "Calling pobyso_remez_canonical_monomials_base...\n");
|
445 | 26 | storres | /* Try to find a polynomial with the guessed degree. */
|
446 | 26 | storres | bestApproxPolyNode = pobyso_remez_canonical_monomials_base(functionNode,
|
447 | 26 | storres | weight,
|
448 | 26 | storres | degree,
|
449 | 26 | storres | lowerBound,
|
450 | 26 | storres | upperBound,
|
451 | 26 | storres | quality);
|
452 | 26 | storres | |
453 | 26 | storres | if (bestApproxPolyNode == NULL)
|
454 | 26 | storres | {
|
455 | 26 | storres | pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
|
456 | 26 | storres | "INTERNAL_ERROR",
|
457 | 26 | storres | "could not compute the bestApproxPolyNode");
|
458 | 26 | storres | mpfr_clear(currentError);
|
459 | 26 | storres | mpfr_clear(quality);
|
460 | 26 | storres | return(NULL);
|
461 | 26 | storres | }
|
462 | 26 | storres | |
463 | 26 | storres | setDisplayMode(DISPLAY_MODE_DECIMAL);
|
464 | 26 | storres | fprintTree(stderr, bestApproxPolyNode);
|
465 | 26 | storres | fprintf(stderr, "\n\n");
|
466 | 26 | storres | |
467 | 26 | storres | errorNode = makeSub(copyTree(functionNode), copyTree(bestApproxPolyNode));
|
468 | 26 | storres | /* Check the error with the computed polynomial. */
|
469 | 26 | storres | uncertifiedInfnorm(currentError,
|
470 | 26 | storres | errorNode,
|
471 | 26 | storres | lowerBound,
|
472 | 26 | storres | upperBound,
|
473 | 26 | storres | POBYSO_INF_NORM_NUM_POINTS,
|
474 | 26 | storres | getToolPrecision());
|
475 | 26 | storres | fprintf(stderr, "Inf norm: ");
|
476 | 26 | storres | mpfr_out_str(stderr, 10, 17, currentError, MPFR_RNDN);
|
477 | 26 | storres | fprintf(stderr, "\n\n");
|
478 | 26 | storres | /* Free the errorNode but not the bestApproxPolyNode (we need it if
|
479 | 26 | storres | * we exit the loop at the next iteration). */
|
480 | 26 | storres | free_memory(errorNode);
|
481 | 26 | storres | degree++;
|
482 | 26 | storres | }
|
483 | 26 | storres | /* Use an intermediate variable, since horner() creates a new node
|
484 | 26 | storres | * and does not reorder the argument "in place". This allows for the memory
|
485 | 26 | storres | * reclaim of bestApproxHorner.
|
486 | 26 | storres | */
|
487 | 26 | storres | bestApproxHorner = horner(bestApproxPolyNode);
|
488 | 26 | storres | free_memory(bestApproxPolyNode);
|
489 | 26 | storres | mpfr_clear(currentError);
|
490 | 26 | storres | mpfr_clear(quality);
|
491 | 26 | storres | free_memory(weight);
|
492 | 26 | storres | return(bestApproxHorner);
|
493 | 26 | storres | } /* End pobyso_remez_approx_canonical_monomials_base_for_error */
|
494 | 26 | storres | |
495 | 26 | storres | node*
|
496 | 26 | storres | pobyso_remez_canonical_monomials_base(node *function,
|
497 | 26 | storres | node *weight,
|
498 | 26 | storres | unsigned int degree,
|
499 | 26 | storres | mpfr_t lowerBound,
|
500 | 26 | storres | mpfr_t upperBound,
|
501 | 26 | storres | mpfr_t quality)
|
502 | 26 | storres | {
|
503 | 26 | storres | node *bestApproxPoly = NULL;
|
504 | 26 | storres | chain *monomials = NULL;
|
505 | 26 | storres | chain *curMonomial = NULL;
|
506 | 26 | storres | |
507 | 26 | storres | mpfr_t satisfying_error;
|
508 | 26 | storres | mpfr_t target_error;
|
509 | 26 | storres | |
510 | 26 | storres | /* Argument checking */
|
511 | 26 | storres | /* Function tree. */
|
512 | 26 | storres | if (function == NULL)
|
513 | 26 | storres | {
|
514 | 26 | storres | pobyso_error_message("pobyso_remez_canonical_monomials_base",
|
515 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
516 | 26 | storres | "the \"function\" argument is a NULL pointer");
|
517 | 26 | storres | return(NULL);
|
518 | 26 | storres | }
|
519 | 26 | storres | if (weight == NULL)
|
520 | 26 | storres | {
|
521 | 26 | storres | pobyso_error_message("pobyso_remez_canonical_monomials_base",
|
522 | 26 | storres | "NULL_POINTER_ARGUMENT",
|
523 | 26 | storres | "the \"weight\" argument is a NULL pointer");
|
524 | 26 | storres | return(NULL);
|
525 | 26 | storres | }
|
526 | 26 | storres | /* Check the bounds. */
|
527 | 26 | storres | if (mpfr_cmp(lowerBound, upperBound) >= 0)
|
528 | 26 | storres | {
|
529 | 26 | storres | pobyso_error_message("pobyso_remez_canonical_monomials_base",
|
530 | 26 | storres | "INCOHERENT_IMPUT_DATA",
|
531 | 26 | storres | "the lower_bound >= upper_bound");
|
532 | 26 | storres | return(NULL);
|
533 | 26 | storres | }
|
534 | 26 | storres | /* The quality must be a non null positive number. */
|
535 | 26 | storres | if (mpfr_sgn(quality) <= 0)
|
536 | 26 | storres | {
|
537 | 26 | storres | pobyso_error_message("pobyso_remez_canonical_monomials_base",
|
538 | 26 | storres | "INCOHERENT_INPUT_DATA",
|
539 | 26 | storres | "the quality <= 0");
|
540 | 26 | storres | }
|
541 | 26 | storres | /* End argument checking. */
|
542 | 26 | storres | /* Create the monomials nodes chains. */
|
543 | 26 | storres | monomials = pobyso_create_canonical_monomials_base(degree);
|
544 | 26 | storres | fprintf(stderr, "monomials chain length = %d\n", lengthChain(monomials));
|
545 | 26 | storres | if (monomials == NULL || (lengthChain(monomials) != degree + 1))
|
546 | 26 | storres | {
|
547 | 26 | storres | pobyso_error_message("pobyso_remez_canonical_monomials_base",
|
548 | 26 | storres | "CHAIN_CREATION_ERROR",
|
549 | 26 | storres | "could not create the monomials chain");
|
550 | 26 | storres | return(NULL);
|
551 | 26 | storres | }
|
552 | 26 | storres | curMonomial = monomials;
|
553 | 26 | storres | |
554 | 26 | storres | while (curMonomial != NULL)
|
555 | 26 | storres | {
|
556 | 26 | storres | fprintf(stderr, "monomial tree: ");
|
557 | 26 | storres | //mpfr_out_str(stderr, 10, 17, *((mpfr_t*)((node*)(curMonomial->value))->value), MPFR_RNDN);
|
558 | 26 | storres | fprintTree(stderr, (node*)(curMonomial->value));
|
559 | 26 | storres | fprintf(stderr, "\n");
|
560 | 26 | storres | curMonomial = curMonomial->next;
|
561 | 26 | storres | }
|
562 | 26 | storres | |
563 | 26 | storres | /* Deal with NULL weight. */
|
564 | 26 | storres | if (weight == NULL)
|
565 | 26 | storres | {
|
566 | 26 | storres | weight = makeConstantDouble(1.0);
|
567 | 26 | storres | }
|
568 | 26 | storres | /* Compute the best polynomial with the required quality.
|
569 | 26 | storres | The behavior is as if satisfying error and target_error had
|
570 | 26 | storres | not been used.*/
|
571 | 26 | storres | mpfr_init(satisfying_error);
|
572 | 26 | storres | mpfr_init(target_error);
|
573 | 26 | storres | mpfr_set_str(satisfying_error, "0", 10, MPFR_RNDN);
|
574 | 26 | storres | mpfr_set_inf(target_error, 1);
|
575 | 26 | storres | |
576 | 26 | storres | |
577 | 26 | storres | fprintf(stderr, "satisfying_error: ");
|
578 | 26 | storres | mpfr_out_str(stderr, 10, 17, satisfying_error, MPFR_RNDN);
|
579 | 26 | storres | fprintf(stderr, ".\n");
|
580 | 26 | storres | fprintf(stderr, "target_error: ");
|
581 | 26 | storres | mpfr_out_str(stderr, 10, 17, target_error,MPFR_RNDN);
|
582 | 26 | storres | fprintf(stderr, ".\n");
|
583 | 26 | storres | |
584 | 26 | storres | fprintf(stderr,
|
585 | 26 | storres | "current precision: %li\n", getToolPrecision());
|
586 | 26 | storres | /* Call the Sollya function. */
|
587 | 26 | storres | bestApproxPoly = remez(function,
|
588 | 26 | storres | weight,
|
589 | 26 | storres | monomials,
|
590 | 26 | storres | lowerBound,
|
591 | 26 | storres | upperBound,
|
592 | 26 | storres | quality,
|
593 | 26 | storres | satisfying_error,
|
594 | 26 | storres | target_error,
|
595 | 26 | storres | getToolPrecision());
|
596 | 26 | storres | |
597 | 26 | storres | mpfr_clear(satisfying_error);
|
598 | 26 | storres | mpfr_clear(target_error);
|
599 | 26 | storres | pobyso_free_chain_of_nodes(monomials);
|
600 | 26 | storres | |
601 | 26 | storres | return(bestApproxPoly);
|
602 | 26 | storres | } /* End pobyso_remez_canonical_monomials_base. */
|
603 | 26 | storres | |
604 | 26 | storres | #endif
|
605 | 26 | storres | |
606 | 26 | storres | void
|
607 | 26 | storres | pobyso_error_message(char *functionName, char *messageName, char* message) |
608 | 26 | storres | { |
609 | 26 | storres | fprintf(stderr, "?%s: %s.\n%s.\n", functionName, messageName, message);
|
610 | 26 | storres | } /* End pobyso_error_message */ |