Statistiques
| Révision :

root / pobysoC-4.0 / src / pobyso.c @ 29

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 26 storres
/******************************************************************************/
10 26 storres
/* Headers, applying the "particular to general" convention.*/
11 26 storres
12 26 storres
#include "pobyso.h"
13 26 storres
14 26 storres
/* includes of local headers */
15 26 storres
16 26 storres
/* includes of project headers */
17 26 storres
18 26 storres
/* includes of system headers */
19 26 storres
20 26 storres
/* Other declarations */
21 26 storres
22 26 storres
/* Internal prototypes */
23 26 storres
void
24 26 storres
pobyso_error_message(char *functionName, char *messageName, char* message);
25 26 storres
/* Types, constants and macros definitions */
26 26 storres
27 26 storres
/* Global variables */
28 26 storres
29 26 storres
/* Functions */
30 26 storres
void
31 26 storres
pobyso_autoprint(sollya_obj_t solObj, ...)
32 26 storres
{
33 26 storres
  va_list va;
34 26 storres
  va_start(va, solObj);
35 26 storres
  sollya_lib_v_autoprint(solObj, va);
36 26 storres
  va_end(va);
37 26 storres
} /* End pobyso_autoprint. */
38 26 storres
39 26 storres
sollya_obj_t
40 26 storres
pobyso_parse_string(const char* expression)
41 26 storres
{
42 26 storres
  if (expression == NULL)
43 26 storres
  {
44 26 storres
    pobyso_error_message("pobyso_parse_string",
45 26 storres
                        "NULL_POINTER_ARGUMENT",
46 26 storres
                        "The expression is a NULL pointer");
47 26 storres
    return(NULL);
48 26 storres
  }
49 26 storres
  return(sollya_lib_parse_string(expression));
50 26 storres
} /* pobyso_parse_string */
51 26 storres
52 26 storres
void
53 26 storres
pobyso_set_verbosity_off(sollya_obj_t* currentVerbosityLevel)
54 26 storres
{
55 26 storres
  sollya_obj_t verbosityLevelZero;
56 26 storres
  if (currentVerbosityLevel != NULL)
57 26 storres
  {
58 26 storres
     *currentVerbosityLevel = sollya_lib_get_verbosity();
59 26 storres
  }
60 26 storres
  verbosityLevelZero = sollya_lib_constant_from_int(0);
61 26 storres
  sollya_lib_set_verbosity(verbosityLevelZero);
62 26 storres
  sollya_lib_clear_obj(verbosityLevelZero);
63 26 storres
} /* End of pobyso_set_verbosity_off. */
64 26 storres
65 26 storres
void
66 26 storres
pobyso_set_verbosity_to(sollya_obj_t newVerbosityLevel)
67 26 storres
{
68 26 storres
  if (newVerbosityLevel == NULL)
69 26 storres
  {
70 26 storres
    pobyso_error_message("pobyso_set_verbosity_to",
71 26 storres
                        "NULL_POINTER_ARGUMENT",
72 26 storres
                        "The new verbosity level is a NULL pointer");
73 26 storres
    return;
74 26 storres
  }
75 26 storres
  sollya_lib_set_verbosity(newVerbosityLevel);
76 26 storres
} /* End of pobyso_set_verbosity_to. */
77 26 storres
78 26 storres
/* Attic from the sollya_lib < 4. */
79 26 storres
#if 0
80 26 storres
chain*
81 26 storres
pobyso_create_canonical_monomials_base(const unsigned int degree)
82 26 storres
{
83 26 storres
  int i    = 0;
84 26 storres
  chain *monomials  = NULL;
85 26 storres
  node  *monomial   = NULL;
86 26 storres

87 26 storres
  for(i = degree ; i >= 0  ; i--)
88 26 storres
  {
89 26 storres
     monomial   = makePow(makeVariable(), makeConstantDouble((double)i));
90 26 storres
     monomials  = addElement(monomials, monomial);
91 26 storres
     fprintf(stderr, "pobyso_create_canonical_monomials_base: %u\n", i);
92 26 storres
  }
93 26 storres
  if (monomials == NULL)
94 26 storres
  {
95 26 storres
    pobyso_error_message("pobyso_create_canonical_monomial_base",
96 26 storres
                        "CHAIN_CREATION_ERROR",
97 26 storres
                        "Could not create the monomials chain");
98 26 storres
    return(NULL);
99 26 storres
  }
100 26 storres
  return(monomials);
101 26 storres
} /* End pobyso_create_canonical_monomials_base. */
102 26 storres

103 26 storres
chain*
104 26 storres
pobyso_create_chain_from_int_array(int* intArray,
105 26 storres
                                    const unsigned int arrayLength)
106 26 storres
{
107 26 storres
  int i = 0;
108 26 storres
  chain *newChain = NULL;
109 26 storres
  int *currentInt;
110 26 storres

111 26 storres
  if (arrayLength == 0) return(NULL);
112 26 storres
  if (intArray == NULL)
113 26 storres
  {
114 26 storres
    pobyso_error_message("pobyso_create_chain_from_int_array",
115 26 storres
                        "NULL_POINTER_ARGUMENT",
116 26 storres
                        "The array is a NULL pointer");
117 26 storres
    return(NULL);
118 26 storres
  }
119 26 storres
  for (i = arrayLength - 1 ; i >= 0 ; i--)
120 26 storres
  {
121 26 storres
    currentInt = malloc(sizeof(int));
122 26 storres
    if (currentInt == NULL)
123 26 storres
    {
124 26 storres
      pobyso_error_message("pobyso_create_chain_from_int_array",
125 26 storres
                          "MEMORY_ALLOCATION_ERROR",
126 26 storres
                          "Could not allocate one of the integers");
127 26 storres
      freeChain(newChain, free);
128 26 storres
      return(NULL);
129 26 storres
    }
130 26 storres
    *currentInt = intArray[i];
131 26 storres
    newChain = addElement(newChain, currentInt);
132 26 storres
  }
133 26 storres
  return(newChain);
134 26 storres
} // End pobyso_create_chain_from_int_array. */
135 26 storres

136 26 storres
chain*
137 26 storres
pobyso_create_chain_from_unsigned_int_array(unsigned int* intArray,
138 26 storres
                                        const unsigned int arrayLength)
139 26 storres
{
140 26 storres
  int i = 0;
141 26 storres
  chain *newChain = NULL;
142 26 storres
  unsigned int *currentInt;
143 26 storres

144 26 storres
  /* Argument checking. */
145 26 storres
  if (arrayLength == 0) return(NULL);
146 26 storres
  if (intArray == NULL)
147 26 storres
  {
148 26 storres
    pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
149 26 storres
                        "NULL_POINTER_ARGUMENT",
150 26 storres
                        "The array is a NULL pointer");
151 26 storres
    return(NULL);
152 26 storres
  }
153 26 storres
  for (i = arrayLength - 1 ; i >= 0 ; i--)
154 26 storres
  {
155 26 storres
    currentInt = malloc(sizeof(unsigned int));
156 26 storres
    if (currentInt == NULL)
157 26 storres
    {
158 26 storres
      pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
159 26 storres
                          "MEMORY_ALLOCATION_ERROR",
160 26 storres
                          "Could not allocate one of the integers");
161 26 storres
      freeChain(newChain, free);
162 26 storres
      return(NULL);
163 26 storres
    }
164 26 storres
    *currentInt = intArray[i];
165 26 storres
    newChain = addElement(newChain, currentInt);
166 26 storres
  }
167 26 storres
  return(newChain);
168 26 storres
} // End pobyso_create_chain_from_unsigned_int_array. */
169 26 storres

170 26 storres
node*
171 26 storres
pobyso_differentiate(node *functionNode)
172 26 storres
{
173 26 storres
  /* Argument checking. */
174 26 storres
  node *differentialNode;
175 26 storres
  if (functionNode == NULL)
176 26 storres
  {
177 26 storres
    pobyso_error_message("pobyso_differentiate",
178 26 storres
                        "NULL_POINTER_ARGUMENT",
179 26 storres
                        "The function to differentiate is a NULL pointer");
180 26 storres
    return(NULL);
181 26 storres
  }
182 26 storres
  differentialNode = differentiate(functionNode);
183 26 storres
  if (differentialNode == NULL)
184 26 storres
  {
185 26 storres
    pobyso_error_message("pobyso_differentiate",
186 26 storres
                        "INTERNAL ERROR",
187 26 storres
                        "Sollya could not differentiate the function");
188 26 storres
  }
189 26 storres
  return(differentialNode);
190 26 storres
} // End pobyso_differentiate
191 26 storres

192 26 storres

193 26 storres
int
194 26 storres
pobyso_dirty_infnorm(mpfr_t infNorm,
195 26 storres
                      node *functionNode,
196 26 storres
                      mpfr_t lowerBound,
197 26 storres
                      mpfr_t upperBound,
198 26 storres
                      mp_prec_t precision)
199 26 storres
{
200 26 storres
  int functionCallResult;
201 26 storres
  /* Arguments checking. */
202 26 storres
  if (functionNode == NULL)
203 26 storres
  {
204 26 storres
    pobyso_error_message("pobyso_dirty_infnorm",
205 26 storres
                        "NULL_POINTER_ARGUMENT",
206 26 storres
                        "The function to compute is a NULL pointer");
207 26 storres
    return(1);
208 26 storres
  }
209 26 storres
  if (mpfr_cmp(lowerBound, upperBound) > 0)
210 26 storres
  {
211 26 storres
    pobyso_error_message("pobyso_dirty_infnorm",
212 26 storres
                        "INCOHERENT_INPUT_DATA",
213 26 storres
                        "The lower bond is greater than the upper bound");
214 26 storres
    return(1);
215 26 storres
  }
216 26 storres
  /* Particular cases. */
217 26 storres
  if (mpfr_cmp(lowerBound, upperBound) == 0)
218 26 storres
  {
219 26 storres
    functionCallResult = pobyso_evaluate_faithful(infNorm,
220 26 storres
                                                  functionNode,
221 26 storres
                                                  lowerBound,
222 26 storres
                                                  precision);
223 26 storres
    return(functionCallResult);
224 26 storres
  }
225 26 storres
  if (isConstant(functionNode))
226 26 storres
  {
227 26 storres
    functionCallResult = pobyso_evaluate_faithful(infNorm,
228 26 storres
                                                  functionNode,
229 26 storres
                                                  lowerBound,
230 26 storres
                                                  precision);
231 26 storres
    if (!functionCallResult)
232 26 storres
    {
233 26 storres
      mpfr_abs(infNorm, infNorm, MPFR_RNDN);
234 26 storres
    }
235 26 storres
    return(functionCallResult);
236 26 storres
  }
237 26 storres
  uncertifiedInfnorm(infNorm,
238 26 storres
                      functionNode,
239 26 storres
                      lowerBound,
240 26 storres
                      upperBound,
241 26 storres
                      POBYSO_DEFAULT_POINTS,
242 26 storres
                      precision);
243 26 storres
  return(0);
244 26 storres
} /* End pobyso_dirty_infnorm. */
245 26 storres

246 26 storres
int
247 26 storres
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
248 26 storres
                          node *nodeToEvaluate,
249 26 storres
                          mpfr_t argument,
250 26 storres
                          mpfr_prec_t precision)
251 26 storres
{
252 26 storres
  /* Check input arguments. */
253 26 storres
  if (nodeToEvaluate == NULL)
254 26 storres
  {
255 26 storres
    pobyso_error_message("pobyso_evaluate_faithful",
256 26 storres
                        "NULL_POINTER_ARGUMENT",
257 26 storres
                        "nodeToEvaluate is a NULL pointer");
258 26 storres
    return(1);
259 26 storres
  }
260 26 storres
  evaluateFaithful(faithfulEvaluation,
261 26 storres
                   nodeToEvaluate,
262 26 storres
                   argument,
263 26 storres
                   precision);
264 26 storres
  return(0);
265 26 storres
} /* End pobyso_evaluate_faithfull. */
266 26 storres

267 26 storres
chain*
268 26 storres
pobyso_find_zeros(node *function,
269 26 storres
                  mpfr_t *lower_bound,
270 26 storres
                  mpfr_t *upper_bound)
271 26 storres
{
272 26 storres
  mp_prec_t currentPrecision;
273 26 storres
  mpfr_t currentDiameter;
274 26 storres
  rangetype bounds;
275 26 storres

276 26 storres
  currentPrecision = getToolPrecision();
277 26 storres
  mpfr_init2(currentDiameter, currentPrecision);
278 26 storres

279 26 storres
  bounds.a = lower_bound;
280 26 storres
  bounds.b = upper_bound;
281 26 storres

282 26 storres
  if (bounds.a == NULL || bounds.b == NULL)
283 26 storres
  {
284 26 storres
    pobyso_error_message("pobyso_find_zeros",
285 26 storres
                        "MEMORY_ALLOCATION_ERROR",
286 26 storres
                        "Could not allocate one of the bounds");
287 26 storres
    return(NULL);
288 26 storres
  }
289 26 storres
  return(findZerosFunction(function,
290 26 storres
                            bounds,
291 26 storres
                            currentPrecision,
292 26 storres
                            currentDiameter));
293 26 storres
} /* End pobyso_find_zeros. */
294 26 storres

295 26 storres
void
296 26 storres
pobyso_free_chain_of_nodes(chain *theChainOfNodes)
297 26 storres
{
298 26 storres
  node *currentNode           = NULL;
299 26 storres
  chain *currentChainElement  = NULL;
300 26 storres
  chain *nextChainElement     = NULL;
301 26 storres

302 26 storres
  nextChainElement = theChainOfNodes;
303 26 storres

304 26 storres
  while(nextChainElement != NULL)
305 26 storres
  {
306 26 storres
    currentChainElement = nextChainElement;
307 26 storres
    currentNode = (node*)(currentChainElement->value);
308 26 storres
    nextChainElement = nextChainElement->next;
309 26 storres
    free_memory(currentNode);
310 26 storres
    free((void*)(currentChainElement));
311 26 storres
  }
312 26 storres
} /* End pobyso_free_chain_of_nodes. */
313 26 storres

314 26 storres
void
315 26 storres
pobyso_free_range(rangetype range)
316 26 storres
{
317 26 storres

318 26 storres
  mpfr_clear(*(range.a));
319 26 storres
  mpfr_clear(*(range.b));
320 26 storres
  free(range.a);
321 26 storres
  free(range.b);
322 26 storres
} /* End pobyso_free_range. */
323 26 storres

324 26 storres
node*
325 26 storres
pobyso_fp_minimax_canonical_monomials_base(node *function,
326 26 storres
                                      int degree,
327 26 storres
                                      chain *formats,
328 26 storres
                                      chain *points,
329 26 storres
                                      mpfr_t lowerBound,
330 26 storres
                                      mpfr_t upperBound,
331 26 storres
                                      int fpFixedArg,
332 26 storres
                                      int absRel,
333 26 storres
                                      node *constPart,
334 26 storres
                                      node *minimax)
335 26 storres
{
336 26 storres
  return(NULL);
337 26 storres
} /* End pobyso_fp_minimax_canonical_monomials_base. */
338 26 storres

339 26 storres
node*
340 26 storres
pobyso_parse_function(char *functionString,
341 26 storres
                      char *freeVariableNameString)
342 26 storres
{
343 26 storres
  if (functionString == NULL || freeVariableNameString == NULL)
344 26 storres
  {
345 26 storres
    pobyso_error_message("pobyso_parse_function",
346 26 storres
                        "NULL_POINTER_ARGUMENT",
347 26 storres
                        "One of the arguments is a NULL pointer");
348 26 storres
    return(NULL);
349 26 storres
  }
350 26 storres
  return(parseString(functionString));
351 26 storres

352 26 storres
} /* End pobyso_parse_function */
353 26 storres

354 26 storres
node*
355 26 storres
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
356 26 storres
                                                  unsigned int mode,
357 26 storres
                                                  mpfr_t lowerBound,
358 26 storres
                                                  mpfr_t upperBound,
359 26 storres
                                                  mpfr_t eps)
360 26 storres
{
361 26 storres
  node *weight              = NULL;
362 26 storres
  node *bestApproxPolyNode  = NULL;
363 26 storres
  node *bestApproxHorner    = NULL;
364 26 storres
  node *errorNode           = NULL;
365 26 storres
  rangetype degreeRange;
366 26 storres
  mpfr_t quality;
367 26 storres
  mpfr_t currentError;
368 26 storres
  unsigned int degree;
369 26 storres

370 26 storres
  /* Check the parameters. */
371 26 storres
  if (functionNode == NULL)
372 26 storres
  {
373 26 storres
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
374 26 storres
                        "NULL_POINTER_ARGUMENT",
375 26 storres
                        "functionNode is a NULL pointer");
376 26 storres
    return(NULL);
377 26 storres
  }
378 26 storres
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
379 26 storres
  {
380 26 storres
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
381 26 storres
                        "INCOHERENT_INPUT_DATA",
382 26 storres
                        "the lower_bound >= upper_bound");
383 26 storres
    return(NULL);
384 26 storres
  }
385 26 storres
  /* Set the weight. */
386 26 storres
  if (mode == POBYSO_ABSOLUTE)
387 26 storres
  {
388 26 storres
    /* Set the weight to 1 for the ABSOLUTE_MODE. */
389 26 storres
    weight = makeConstantDouble(1.0);
390 26 storres
  }
391 26 storres
  else
392 26 storres
  {
393 26 storres
    if (mode == POBYSO_RELATIVE)
394 26 storres
    {
395 26 storres
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
396 26 storres
                          "NOT_IMPLEMENTED",
397 26 storres
                          "the search for relative error is not implemented yet");
398 26 storres
      return(NULL);
399 26 storres
    }
400 26 storres
    else
401 26 storres
    {
402 26 storres
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
403 26 storres
                          "INCOHERENT_INPUT_DATA",
404 26 storres
                          "the mode is node of POBYSO_ABOLUTE or POBYSO_RELATIVE");
405 26 storres
      return(NULL);
406 26 storres
    }
407 26 storres
  }
408 26 storres
  //fprintf(stderr, "\n\n\n******* I'm here! ********\n\n\n");
409 26 storres
  degreeRange = guessDegree(functionNode,
410 26 storres
                            weight,
411 26 storres
                            lowerBound,
412 26 storres
                            upperBound,
413 26 storres
                            eps,
414 26 storres
                            POBYSO_GUESS_DEGREE_BOUND);
415 26 storres
  degree = mpfr_get_ui(*(degreeRange.a), MPFR_RNDN);
416 26 storres
  //fprintf(stderr, "\n\n\n******* I'm back! ********\n\n\n");
417 26 storres
  fprintf(stderr, "Guessed degree: ");
418 26 storres
  mpfr_out_str(stderr, 10, 17, *(degreeRange.a), MPFR_RNDN);
419 26 storres
  fprintf(stderr, " - as int: %u\n", degree);
420 26 storres
  /* Reduce the degree by 1 in the foolish hope it could work. */
421 26 storres
  if (degree > 0) degree--;
422 26 storres
  /* Both elements of degreeRange where "inited" within guessDegree. */
423 26 storres
  mpfr_clear(*(degreeRange.a));
424 26 storres
  mpfr_clear(*(degreeRange.b));
425 26 storres
  free(degreeRange.a);
426 26 storres
  free(degreeRange.b);
427 26 storres
  /* Mimic the default behavior of interactive Sollya. */
428 26 storres
  mpfr_init(quality);
429 26 storres
  mpfr_set_d(quality, 1e-5, MPFR_RNDN);
430 26 storres
  mpfr_init2(currentError, getToolPrecision());
431 26 storres
  mpfr_set_inf(currentError,1);
432 26 storres

433 26 storres
  /* Try to refine the initial guess: loop with increasing degrees until
434 26 storres
   * we find a satisfactory one. */
435 26 storres
  while(mpfr_cmp(currentError, eps) > 0)
436 26 storres
  {
437 26 storres
    /* Get rid of the previous polynomial, if any. */
438 26 storres
    if (bestApproxPolyNode != NULL)
439 26 storres
    {
440 26 storres
      free_memory(bestApproxPolyNode);
441 26 storres
    }
442 26 storres
    fprintf(stderr, "Degree: %u\n", degree);
443 26 storres
    fprintf(stderr, "Calling pobyso_remez_canonical_monomials_base...\n");
444 26 storres
    /* Try to find a polynomial with the guessed degree. */
445 26 storres
    bestApproxPolyNode = pobyso_remez_canonical_monomials_base(functionNode,
446 26 storres
                                                            weight,
447 26 storres
                                                            degree,
448 26 storres
                                                            lowerBound,
449 26 storres
                                                            upperBound,
450 26 storres
                                                            quality);
451 26 storres

452 26 storres
    if (bestApproxPolyNode == NULL)
453 26 storres
    {
454 26 storres
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
455 26 storres
                          "INTERNAL_ERROR",
456 26 storres
                          "could not compute the bestApproxPolyNode");
457 26 storres
      mpfr_clear(currentError);
458 26 storres
      mpfr_clear(quality);
459 26 storres
      return(NULL);
460 26 storres
    }
461 26 storres

462 26 storres
    setDisplayMode(DISPLAY_MODE_DECIMAL);
463 26 storres
    fprintTree(stderr, bestApproxPolyNode);
464 26 storres
    fprintf(stderr, "\n\n");
465 26 storres

466 26 storres
    errorNode = makeSub(copyTree(functionNode), copyTree(bestApproxPolyNode));
467 26 storres
    /* Check the error with the computed polynomial. */
468 26 storres
    uncertifiedInfnorm(currentError,
469 26 storres
                        errorNode,
470 26 storres
                        lowerBound,
471 26 storres
                        upperBound,
472 26 storres
                        POBYSO_INF_NORM_NUM_POINTS,
473 26 storres
                        getToolPrecision());
474 26 storres
    fprintf(stderr, "Inf norm: ");
475 26 storres
    mpfr_out_str(stderr, 10, 17, currentError, MPFR_RNDN);
476 26 storres
    fprintf(stderr, "\n\n");
477 26 storres
    /* Free the errorNode but not the bestApproxPolyNode (we need it if
478 26 storres
     * we exit the loop at the next iteration). */
479 26 storres
    free_memory(errorNode);
480 26 storres
    degree++;
481 26 storres
  }
482 26 storres
  /* Use an intermediate variable, since horner() creates a new node
483 26 storres
   * and does not reorder the argument "in place". This allows for the memory
484 26 storres
   * reclaim of bestApproxHorner.
485 26 storres
   */
486 26 storres
  bestApproxHorner = horner(bestApproxPolyNode);
487 26 storres
  free_memory(bestApproxPolyNode);
488 26 storres
  mpfr_clear(currentError);
489 26 storres
  mpfr_clear(quality);
490 26 storres
  free_memory(weight);
491 26 storres
  return(bestApproxHorner);
492 26 storres
} /* End pobyso_remez_approx_canonical_monomials_base_for_error */
493 26 storres

494 26 storres
node*
495 26 storres
pobyso_remez_canonical_monomials_base(node *function,
496 26 storres
                                     node *weight,
497 26 storres
                                     unsigned int degree,
498 26 storres
                                     mpfr_t lowerBound,
499 26 storres
                                     mpfr_t upperBound,
500 26 storres
                                     mpfr_t quality)
501 26 storres
{
502 26 storres
  node  *bestApproxPoly = NULL;
503 26 storres
  chain *monomials      = NULL;
504 26 storres
  chain *curMonomial    = NULL;
505 26 storres

506 26 storres
  mpfr_t satisfying_error;
507 26 storres
  mpfr_t target_error;
508 26 storres

509 26 storres
  /* Argument checking */
510 26 storres
  /* Function tree. */
511 26 storres
  if (function == NULL)
512 26 storres
  {
513 26 storres
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
514 26 storres
                        "NULL_POINTER_ARGUMENT",
515 26 storres
                        "the \"function\" argument is a NULL pointer");
516 26 storres
    return(NULL);
517 26 storres
  }
518 26 storres
  if (weight == NULL)
519 26 storres
  {
520 26 storres
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
521 26 storres
                        "NULL_POINTER_ARGUMENT",
522 26 storres
                        "the \"weight\" argument is a NULL pointer");
523 26 storres
    return(NULL);
524 26 storres
  }
525 26 storres
  /* Check the bounds. */
526 26 storres
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
527 26 storres
  {
528 26 storres
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
529 26 storres
                        "INCOHERENT_IMPUT_DATA",
530 26 storres
                        "the lower_bound >= upper_bound");
531 26 storres
    return(NULL);
532 26 storres
  }
533 26 storres
  /* The quality must be a non null positive number. */
534 26 storres
  if (mpfr_sgn(quality) <= 0)
535 26 storres
  {
536 26 storres
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
537 26 storres
                        "INCOHERENT_INPUT_DATA",
538 26 storres
                        "the quality <= 0");
539 26 storres
  }
540 26 storres
  /* End argument checking. */
541 26 storres
  /* Create the monomials nodes chains. */
542 26 storres
  monomials = pobyso_create_canonical_monomials_base(degree);
543 26 storres
  fprintf(stderr, "monomials chain length = %d\n", lengthChain(monomials));
544 26 storres
  if (monomials == NULL || (lengthChain(monomials) != degree + 1))
545 26 storres
  {
546 26 storres
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
547 26 storres
                        "CHAIN_CREATION_ERROR",
548 26 storres
                        "could not create the monomials chain");
549 26 storres
    return(NULL);
550 26 storres
  }
551 26 storres
  curMonomial = monomials;
552 26 storres

553 26 storres
  while (curMonomial != NULL)
554 26 storres
  {
555 26 storres
    fprintf(stderr, "monomial tree: ");
556 26 storres
    //mpfr_out_str(stderr, 10, 17, *((mpfr_t*)((node*)(curMonomial->value))->value), MPFR_RNDN);
557 26 storres
    fprintTree(stderr, (node*)(curMonomial->value));
558 26 storres
    fprintf(stderr, "\n");
559 26 storres
    curMonomial = curMonomial->next;
560 26 storres
  }
561 26 storres

562 26 storres
  /* Deal with NULL weight. */
563 26 storres
  if (weight == NULL)
564 26 storres
  {
565 26 storres
    weight = makeConstantDouble(1.0);
566 26 storres
  }
567 26 storres
  /* Compute the best polynomial with the required quality.
568 26 storres
     The behavior is as if satisfying error and target_error had
569 26 storres
     not been used.*/
570 26 storres
  mpfr_init(satisfying_error);
571 26 storres
  mpfr_init(target_error);
572 26 storres
  mpfr_set_str(satisfying_error, "0", 10, MPFR_RNDN);
573 26 storres
  mpfr_set_inf(target_error, 1);
574 26 storres

575 26 storres

576 26 storres
  fprintf(stderr, "satisfying_error: ");
577 26 storres
  mpfr_out_str(stderr, 10, 17, satisfying_error, MPFR_RNDN);
578 26 storres
  fprintf(stderr, ".\n");
579 26 storres
  fprintf(stderr, "target_error: ");
580 26 storres
  mpfr_out_str(stderr, 10, 17, target_error,MPFR_RNDN);
581 26 storres
  fprintf(stderr, ".\n");
582 26 storres

583 26 storres
  fprintf(stderr,
584 26 storres
          "current precision: %li\n", getToolPrecision());
585 26 storres
  /* Call the Sollya function. */
586 26 storres
  bestApproxPoly = remez(function,
587 26 storres
                          weight,
588 26 storres
                          monomials,
589 26 storres
                          lowerBound,
590 26 storres
                          upperBound,
591 26 storres
                          quality,
592 26 storres
                          satisfying_error,
593 26 storres
                          target_error,
594 26 storres
                          getToolPrecision());
595 26 storres

596 26 storres
  mpfr_clear(satisfying_error);
597 26 storres
  mpfr_clear(target_error);
598 26 storres
  pobyso_free_chain_of_nodes(monomials);
599 26 storres

600 26 storres
  return(bestApproxPoly);
601 26 storres
} /* End pobyso_remez_canonical_monomials_base. */
602 26 storres

603 26 storres
#endif
604 26 storres
605 26 storres
void
606 26 storres
pobyso_error_message(char *functionName, char *messageName, char* message)
607 26 storres
{
608 26 storres
  fprintf(stderr, "?%s: %s.\n%s.\n", functionName, messageName, message);
609 26 storres
} /* End pobyso_error_message */