Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (44,01 ko)

1
/** @file pobyso.c
2
 * Integration of Sollya to C programs
3
 *
4
 * @author S.T.
5
 * @date 2011-10-12
6
 *
7
 * @todo write pobyso_is_monomial function <br>
8
 *       write pobyso_is_free_var_int_poson_power function
9
 */
10
/******************************************************************************/
11
/* Headers, applying the "particular to general" convention.*/
12

    
13
#include "pobyso.h"
14

    
15
/* includes of local headers */
16

    
17
/* includes of project headers */
18

    
19
/* includes of system headers */
20
#include <string.h>
21
#include <stdlib.h>
22
#include <stdio.h>
23

    
24
/* Other declarations */
25

    
26
/* Internal prototypes */
27
void
28
pobyso_error_message(char *functionName, char *messageName, char* message);
29
/* Types, constants and macros definitions */
30

    
31
/* Global variables */
32

    
33
/* Functions */
34

    
35
/* @see pobyso.h#pobyso_autoprint */
36
void
37
pobyso_autoprint(sollya_obj_t objSo)
38
{
39
  sollya_lib_autoprint(objSo, NULL);
40
} /* End pobyso_autoprint. */
41

    
42
/* @see pobyso.h#pobyso_dirty_find_zeros */
43
mpfr_t*
44
pobyso_dirty_find_zeros_bounds(pobyso_func_exp_t funcExpSo,
45
                              mpfr_t lowerBound,
46
                              mpfr_t upperBound,
47
                              int* zerosCount)
48
{
49
  pobyso_range_t rangeSo;
50
  sollya_obj_t zerosListSo    = NULL;
51
  sollya_obj_t* zerosArraySo  = NULL;
52
  mpfr_t* zerosArrayMp        = NULL;
53
  pobyso_precision_t prec;
54

    
55
  int endEll = 0;
56
  int i,j;
57

    
58
  /* Arguments check. */
59
  *zerosCount = -1;
60
  if (funcExpSo == NULL )
61
  {
62
    pobyso_error_message("pobyso_dirty_find_zeros",
63
                        "NULL_POINTER_ARGUMENT",
64
                        "The funcExpSo argument is a NULL pointer");
65
    return NULL;
66
  }
67
  if (mpfr_cmp(lowerBound, upperBound) > 0)
68
  {
69
    pobyso_error_message("pobyso_dirty_find_zeros",
70
                        "INVALID_INTERVAL_ARGUMENT",
71
                        "The lower bound is larger than the upper bound");
72
    return NULL;
73
  }
74
  /* Make a range out of the bounds. */
75
  rangeSo = sollya_lib_range_from_bounds(lowerBound, upperBound);
76
  if (rangeSo == NULL)
77
  {
78
    return NULL;
79
  }
80
  zerosListSo = sollya_lib_dirtyfindzeros(funcExpSo, rangeSo);
81
  if (zerosListSo == NULL)
82
  {
83
    return NULL;
84
  }
85
  sollya_lib_clear_obj(rangeSo);
86
  /* Transform the Sollya list into an MPFR list. */
87
  if (! sollya_lib_get_list_elements(&zerosArraySo,
88
                                      zerosCount,
89
                                      &endEll,
90
                                      zerosListSo))
91
  {
92
    sollya_lib_clear_obj(zerosListSo);
93
    *zerosCount = -1;
94
    return NULL;
95
  }
96
  sollya_lib_clear_obj(zerosListSo);
97
  zerosArrayMp = (mpfr_t*) malloc(*zerosCount * sizeof(mpfr_t));
98
  if (zerosArrayMp == NULL)
99
  {
100
    pobyso_error_message("pobyso_dirty_find_zeros",
101
                          "MEMORY_ALLOCATION_ERROR",
102
                          "Could not allocate zeroes array");
103
    *zerosCount = -1;
104
    return NULL;
105
  }
106
  for (i = 0 ; i < *zerosCount ; i++)
107
  {
108
    if (! sollya_lib_get_prec_of_constant(&prec, zerosArraySo[i]))
109
    {
110
      /* Clean up the already allocated MPFRs. */
111
      for (j = 0 ; j < i ; j++) mpfr_clear(zerosArrayMp[j]);
112
      /* Clean up the zerosArrayMp array itself. */
113
      free(zerosArrayMp);
114
      /* Clean up what is left in the zerosArraySo. */
115
      for (j = i ; j < *zerosCount ; j++) sollya_lib_clear_obj(zerosArraySo[j]);
116
      /* Clean up the zerosArraySo array itself. */
117
      sollya_lib_free(zerosArraySo);
118
      *zerosCount = -1;
119
      return NULL;
120
    }
121
    mpfr_init2(zerosArrayMp[i], prec);
122
    if (! sollya_lib_get_constant(zerosArrayMp[i], zerosArraySo[i]))
123
    {
124
      /* Clean up the already allocated MPFRs. */
125
      for (j = 0 ; j <= i ; j++) mpfr_clear(zerosArrayMp[j]);
126
      /* Clean up the zerosArrayMp array itself. */
127
      free(zerosArrayMp);
128
      /* Clean up what is left in the zerosArraySo. */
129
      for (j = i ; j < *zerosCount ; j++) sollya_lib_clear_obj(zerosArraySo[j]);
130
      /* Clean up the zerosArraySo array itself. */
131
      sollya_lib_free(zerosArraySo);
132
      *zerosCount = -1;
133
      return NULL;
134
    }
135
    sollya_lib_clear_obj(zerosArraySo[i]);
136
  } /* End for i. */
137
  sollya_lib_free(zerosArraySo);
138
  return zerosArrayMp;
139
} /* End pobyso_dirty_find_zeros. */
140

    
141
/* @see pobyso.h#pobyso_evaluate_constant */
142
int
143
pobyso_evaluate_constant(pobyso_func_exp_t functionSo,
144
                          mpfr_t argumentMp,
145
                          mpfr_t evaluationMp)
146
{
147
  sollya_obj_t argumentSo   = NULL;
148
  sollya_obj_t evaluationSo = NULL;
149
  mpfr_t evaluationTmpMp1;
150
  mpfr_t evaluationTmpMp2;
151
  mpfr_prec_t evalPrec      = 0;
152

    
153
  /* Test argument. */
154
  if (functionSo == NULL || argumentMp == NULL || evaluationMp == NULL)
155
  {
156
    pobyso_error_message("pobyso_evaluate_constant",
157
                        "NULL_POINTER_ARGUMENT",
158
                        "One of the arguments is a NULL pointer");
159
    return 1;
160
  }
161
  if (! sollya_lib_obj_is_function(functionSo))
162
  {
163
    pobyso_error_message("pobyso_evaluate_constant",
164
                        "INVALID_TYPE_ARGUMENT",
165
                     "The functionSo argument is not a functional expression");
166
    return 1;
167
  }
168
  /* Function evaluation and checks. */
169
  argumentSo    = sollya_lib_constant(argumentMp);
170
  evaluationSo  = sollya_lib_evaluate(functionSo, argumentSo);
171
  /* Not needed any more. */
172
  //pobyso_autoprint(evaluationSo);
173
  sollya_lib_clear_obj(argumentSo);
174
  /* The range case: we return the mean of the bounds. The result
175
   * is not faithfully rounded. */
176
  if (sollya_lib_obj_is_range(evaluationSo))
177
  {
178
    //pobyso_autoprint(evaluationSo);
179
    if (sollya_lib_get_prec_of_range(&evalPrec, evaluationSo))
180
    {
181
      mpfr_init2(evaluationTmpMp1, evalPrec);
182
      mpfr_init2(evaluationTmpMp2, evalPrec);
183
      if (sollya_lib_get_bounds_from_range(evaluationTmpMp1,
184
                                       evaluationTmpMp2,
185
                                       evaluationSo))
186
      {
187
        /* Compute the mean of the bounds. */
188
        mpfr_clear(evaluationMp);
189
        mpfr_init2(evaluationMp, evalPrec);
190
        mpfr_add(evaluationMp,evaluationTmpMp1,evaluationTmpMp2,MPFR_RNDN);
191
        mpfr_div_2ui(evaluationMp, evaluationMp, 1, MPFR_RNDN);
192
        mpfr_clear(evaluationTmpMp1);
193
        mpfr_clear(evaluationTmpMp2);
194
        sollya_lib_clear_obj(evaluationSo);
195
        /* It may happen, in this case, when the bounds are -Infty  and
196
         * +Infty, that the average is NaN. */
197
        if (mpfr_nan_p(evaluationMp))
198
        {
199
          return POBYSO_NAN;
200
        }
201
        else
202
        {
203
          return POBYSO_UNFAITHFUL;
204
        }
205
      }
206
      else /* Could not get the values of the bounds. */
207
      {
208
        sollya_lib_clear_obj(evaluationSo);
209
        return 1;
210
      }
211
    }
212
    else /* Could not get the precision of the range. */
213
    {
214
      sollya_lib_clear_obj(evaluationSo);
215
      return 1;
216
    }
217
  } /* End the evaluation is a range. */
218
  /* From now on, we assume that the evaluation is constant. */
219
  if (sollya_lib_get_prec_of_constant(&evalPrec, evaluationSo))
220
  {
221
    mpfr_init2(evaluationTmpMp1, evalPrec);
222
    if (sollya_lib_get_constant(evaluationTmpMp1, evaluationSo))
223
    {
224
      /* Deal with the NaN case. */
225
      if (mpfr_nan_p(evaluationTmpMp1))
226
      {
227
        mpfr_clear(evaluationTmpMp1);
228
        sollya_lib_clear_obj(evaluationSo);
229
        return POBYSO_NAN;
230
      }
231
      else /* The evaluation is not NaN. */
232
      {
233
        mpfr_clear(evaluationMp);
234
        mpfr_init2(evaluationMp, evalPrec);
235
        mpfr_set(evaluationMp, evaluationTmpMp1, MPFR_RNDN);
236
        mpfr_clear(evaluationTmpMp1);
237
        sollya_lib_clear_obj(evaluationSo);
238
        return 0;
239
      }
240
    }
241
    else /* Could not recover the value of the evaluation. */
242
    {
243
      mpfr_clear(evaluationTmpMp1);
244
      sollya_lib_clear_obj(evaluationSo);
245
      return 1;
246
    }
247
  }
248
  else /* Could not get the precision of the evaluation. */
249
  {
250
    sollya_lib_clear_obj(evaluationSo);
251
    return 0;
252
  }
253
}
254
/* End pobyso_evaluate_constant. */
255

    
256
/* @see pobyso.h#pobyso_get_verbosity */
257
int
258
pobyso_get_verbosity()
259
{
260
  sollya_obj_t verbositySo = NULL;
261
  int verbosity            = 0;
262

    
263
  verbositySo = sollya_lib_get_verbosity();
264
  sollya_lib_get_constant_as_int(&verbosity,verbositySo);
265
  sollya_lib_clear_obj(verbositySo);
266
  return verbosity;
267
} /* End pobyso_get_verbosity. */
268

    
269
/** @see pobyso.h#pobyso_is_constant_expression
270
 * Strategy: rely on sollya_lib_get_constant. It return 1, when the
271
 * expression is constant.
272
 */
273
int
274
pobyso_is_constant_expression(sollya_obj_t obj_to_test)
275
{
276
  mpfr_t dummy;
277
  int test;
278
  int initial_verbosity_level      = 0;
279

    
280
  /* Test argument. */
281
  if (obj_to_test == NULL)
282
  {
283
    pobyso_error_message("pobyso_is_constant_expression",
284
                        "NULL_POINTER_ARGUMENT",
285
                        "The expression is a NULL pointer");
286
    return 0;
287
  }
288
  initial_verbosity_level = pobyso_set_verbosity_off();
289
  /* In Sollya, constant are functional expressions. */
290
  if (! sollya_lib_obj_is_function(obj_to_test))
291
  {
292
    pobyso_set_verbosity_to(initial_verbosity_level);
293
    return 0;
294
  }
295
  mpfr_init2(dummy,64);
296
  /* Call to previous Sollya function resets verbosity. */
297
  /* Todo: change verbosity suppression strategy with a message call back. */
298
  pobyso_set_verbosity_off();
299
  /* Try to convert the would be constant into an MPFR number. */
300
  /* If OK, we indeed have a constant. If the conversion fails, return 0. */
301
  test = sollya_lib_get_constant(dummy, obj_to_test);
302
  pobyso_set_verbosity_to(initial_verbosity_level);
303
  if (test)
304
  {
305
    if(mpfr_number_p(dummy) || mpfr_inf_p(dummy))
306
    {
307
      mpfr_clear(dummy);
308
      return test;
309
    }
310
    else /* We do not consider NaNs as constants. */
311
    {
312
      mpfr_clear(dummy);
313
      return 0;
314
    }
315
  }
316
  else
317
  {
318
    mpfr_clear(dummy);
319
    return 0;
320
  }
321
} /* End pobyso_is_constant_expression. */
322

    
323
/** @see pobyso.h#pobyso_is_monomial. */
324
int
325
pobyso_is_int(pobyso_func_exp_t exprSo)
326
{
327
  mpfr_t float1M;
328
  mpfr_t float2M;
329
  mpfr_t tempFloat1M;
330
  mpfr_t tempFloat2M;
331
  mpfr_prec_t prec;
332
  int64_t asInt;
333
  sollya_obj_t newConstantSo = NULL;
334
  /* Arguments check. */
335
  if (exprSo == NULL)
336
  {
337
    pobyso_error_message("pobyso_is_free_var_posze_int_power",
338
                        "NULL_POINTER_ARGUMENT",
339
                        "The expression is a NULL pointer");
340
    return 0;
341
  }
342
  //fprintf(stdout, "Not NULL.\n"); pobyso_autoprint(exprSo);
343
  if (! pobyso_is_constant_expression(exprSo)) return 0;
344
  if (! sollya_lib_get_constant_as_int64(&asInt, exprSo)) return 0;
345
  if (asInt == INT64_MIN || asInt == INT64_MAX) return 0;
346
  /* Some constant integer expression can't have their precision computed
347
   * (e.g. cos(pi). */
348
  if (! sollya_lib_get_prec_of_constant(&prec, exprSo))
349
  {
350
    mpfr_init2(tempFloat1M, 165);
351
    mpfr_init2(tempFloat2M, 165);
352
    mpfr_abs(tempFloat1M, tempFloat1M, MPFR_RNDN);
353
    mpfr_log2(tempFloat2M, tempFloat1M, MPFR_RNDU);
354
    mpfr_rint_ceil(tempFloat1M, tempFloat2M, MPFR_RNDU);
355
    prec = mpfr_get_si(tempFloat1M, MPFR_RNDN) + 10;
356
    if (prec < 1024) prec = 1024;
357
    mpfr_clear(tempFloat1M);
358
    mpfr_clear(tempFloat2M);
359
    mpfr_init2(float1M, prec);
360
    if (!sollya_lib_get_constant(float1M, exprSo))
361
    {
362
      mpfr_clear(float1M);
363
      return 0;
364
    }
365
  }
366
  else /* Precision could be given. */
367
  {
368
    mpfr_init2(float1M, prec);
369
    if (! sollya_lib_get_constant(float1M, exprSo))
370
    {
371
      mpfr_clear(float1M);
372
      return 0;
373
    }
374
  }
375
  if (mpfr_nan_p(float1M) || mpfr_inf_p(float1M))
376
  {
377
    mpfr_clear(float1M);
378
    return 0;
379
  }
380
  if ((newConstantSo = sollya_lib_constant_from_int64(asInt)) == NULL)
381
  {
382
    mpfr_clear(float1M);
383
    return 0;
384
  }
385
  sollya_lib_get_prec_of_constant(&prec, newConstantSo);
386
  mpfr_init2(float2M, prec);
387
  sollya_lib_get_constant(float2M, newConstantSo);
388
  if (mpfr_cmp(float1M, float2M) == 0)
389
  {
390
    mpfr_clear(float1M);
391
    mpfr_clear(float2M);
392
    sollya_lib_clear_obj(newConstantSo);
393
    return 1;
394
  }
395
  else
396
  {
397
    mpfr_clear(float1M);
398
    mpfr_clear(float2M);
399
    sollya_lib_clear_obj(newConstantSo);
400
    return 0;
401
  }
402
} /* End pobyso_is_int. */
403

    
404
/** @see pobyso.h#pobyso_is_monomial.
405
 * Strategy: check that the object is a functional expression and
406
 * if so check that it is made of cte * free_var ^ some_power where :
407
 * - cte is a constant expression (a per pobyso_is_constant_experession;
408
 * - some_power is a positive or null power. t*/
409
int
410
pobyso_is_monomial(sollya_obj_t objSo)
411
{
412
  int arity;
413
  sollya_obj_t subFun1So = NULL;
414
  sollya_obj_t subFun2So = NULL;
415
  sollya_obj_t subFun3So = NULL;
416
  sollya_base_function_t head = 0;
417
  long int exponent = 0;
418
  long int exprIntValue = 0;
419

    
420
  /* Arguments check. */
421
  if (objSo == NULL)
422
  {
423
    pobyso_error_message("pobyso_is_monomial",
424
                        "NULL_POINTER_ARGUMENT",
425
                        "The expression is a NULL pointer");
426
    return 0;
427
  }
428
  /* The object must be a function. */
429
  if (! sollya_lib_obj_is_function(objSo)) return 0;
430
  /* Check if it is the 1 constant. */
431
  if (pobyso_is_int(objSo))
432
  {
433
    if (! sollya_lib_get_constant_as_int64(&exprIntValue, objSo))
434
    {
435
      return 0;
436
    }
437
    else
438
    {
439
      if (exprIntValue == 1) return 1;
440
      else return 0;
441
    }
442
  }
443
  if (! sollya_lib_decompose_function(objSo,
444
                                      &head,
445
                                      &arity,
446
                                      &subFun1So,
447
                                      &subFun2So,
448
                                      NULL)) return 0;
449
  if (arity > 2)
450
  {
451
    if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
452
    if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
453
    return 0;
454
  }
455
  /* Arity == 1 must be free variable by itself. */
456
  if (arity == 1 && head == SOLLYA_BASE_FUNC_FREE_VARIABLE)
457
  {
458
    if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
459
    if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
460
    return 1;
461
  }
462
  else
463
  {
464
    /* Another expression. Must be: free variable  ^ poze integer. */
465
    if (arity == 2)
466
    {
467
      if (head != SOLLYA_BASE_FUNC_POW)
468
      {
469
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
470
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
471
        return 0;
472
      }
473
      if (! pobyso_is_int(subFun2So))
474
      {
475
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
476
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
477
        return 0;
478
      }
479
      if (! sollya_lib_get_constant_as_int64(&exponent, subFun2So))
480
      {
481
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
482
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
483
        return 0;
484
      }
485
      if (exponent < 0)
486
      {
487
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
488
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
489
        return 0;
490
      }
491
      if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
492
      /* Check that the first subfunction is the free variable. */
493
      if (! sollya_lib_decompose_function(subFun1So,
494
                                          &head,
495
                                          &arity,
496
                                          &subFun2So,
497
                                          &subFun3So,
498
                                          NULL))
499
      {
500
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
501
        return 0;
502
      }
503
      if (arity == 1 && head == SOLLYA_BASE_FUNC_FREE_VARIABLE)
504
      {
505
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
506
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
507
        if (subFun3So != NULL) sollya_lib_clear_obj(subFun3So);
508
        return 1;
509
      }
510
      else
511
      {
512
        if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
513
        if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
514
        return 0;
515
      }
516
    } /* End if arity == 2. */
517
  } /* End else if arity == 1. */
518
  return 0;
519
} /* End pobyso_is_monomial. */
520

    
521
/** @see pobyso.h#pobyso_is_polynomial_term.
522
 * Strategy: check that the object is a functional expression and
523
 * if so check that it is made of cte * monomial.
524
 */
525
int
526
pobyso_is_polynomial_term(sollya_obj_t objSo)
527
{
528
  int arity;
529
  sollya_obj_t subFun1So = NULL;
530
  sollya_obj_t subFun2So = NULL;
531
  sollya_base_function_t head = 0;
532

    
533
  /* Arguments check. */
534
  if (objSo == NULL)
535
  {
536
    pobyso_error_message("pobyso_is_polynomial_term",
537
                        "NULL_POINTER_ARGUMENT",
538
                        "The expression is a NULL pointer");
539
    return 0;
540
  }
541
  /* The object must be a function. */
542
  if (! sollya_lib_obj_is_function(objSo)) return 0;
543
  /* A constant expression is degree 0 polynomial term. */
544
  if (pobyso_is_constant_expression(objSo)) return 1;
545
  /* A monomial is a polynomial term. */
546
  if (pobyso_is_monomial(objSo)) return 1;
547
  /* Decompose the functional expression and study the elements. */
548
  if (! sollya_lib_decompose_function(objSo,
549
                                      &head,
550
                                      &arity,
551
                                      &subFun1So,
552
                                      &subFun2So,
553
                                      NULL)) return 0;
554
  /* Monomial case has been dealt with abobe. */
555
  if (arity != 2)
556
  {
557
    if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
558
    if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
559
    return 0;
560
  }
561
  /* The expression must be: cte  * monomial or monomial * cte. */
562
  if (head != SOLLYA_BASE_FUNC_MUL)
563
  {
564
    if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
565
    if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
566
    return 0;
567
  }
568
  if (! pobyso_is_monomial(subFun2So))
569
  {
570
    if (! pobyso_is_constant_expression(subFun2So) ||
571
        ! pobyso_is_monomial(subFun1So))
572
    {
573
      if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
574
      if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
575
      return 0;
576
    }
577
  }
578
  else
579
  {
580
    if (! pobyso_is_constant_expression(subFun1So))
581
    {
582
      if (subFun1So != NULL) sollya_lib_clear_obj(subFun1So);
583
      if (subFun2So != NULL) sollya_lib_clear_obj(subFun2So);
584
      return 0;
585
    }
586
  }
587
  return 1;
588
} /* End pobyso_is_polynomial_term. */
589
/** @see pobyso.h#pobyso_new_monomial. */
590
pobyso_func_exp_t
591
pobyso_new_monomial(pobyso_func_exp_t coefficientSo, long degree)
592
{
593
  sollya_obj_t degreeSo   = NULL;
594
  sollya_obj_t varToPowSo = NULL;
595
  sollya_obj_t monomialSo = NULL;
596
  int initial_verbosity_level = 0;
597

    
598
  /* Arguments check. */
599
  if (coefficientSo == NULL)
600
  {
601
    pobyso_error_message("pobyso_parse_string",
602
                        "NULL_POINTER_ARGUMENT",
603
                        "The expression is a NULL pointer");
604
    return NULL;
605
  }
606
  if (! pobyso_is_constant_expression(coefficientSo))
607
  {
608
    return NULL;
609
  }
610
  if (degree < 0)
611
  {
612
    pobyso_error_message("pobyso_new_monomial",
613
                        "NEGATIVE_DEGREE_ARGUMENT",
614
                        "The degree is a negative integer");
615
    return NULL;
616
  }
617
  /* If degree == 0, just return a copy of the coefficient. Do not
618
   * return the coefficient itself to avoid "double clear" issues. */
619
  if (degree == 0)
620
  {
621
    initial_verbosity_level = pobyso_set_verbosity_off();
622
    monomialSo = sollya_lib_copy_obj(coefficientSo);
623
    pobyso_set_verbosity_to(initial_verbosity_level);
624
  }
625
  degreeSo    = sollya_lib_constant_from_int64(degree);
626
  varToPowSo  = sollya_lib_build_function_pow(sollya_lib_free_variable(),
627
                                              degreeSo);
628
  /* Do not use the "build" version to avoid "eating up" the coefficient. */
629
  monomialSo = sollya_lib_mul(coefficientSo,varToPowSo);
630
  sollya_lib_clear_obj(varToPowSo);
631
  /* Do not clear degreeSa: it was "eaten" by sollya-lib_build_function. */
632
  return monomialSo;
633
} /* End pobyso_new_monomial. */
634

    
635
/* @see pobyso.h#pobyso_off */
636
pobyso_on_off_t
637
pobyso_off()
638
{
639
  return sollya_lib_off();
640
} /* End pobyso_off. */
641

    
642
/* @see pobyso.h#pobyso_off */
643
pobyso_on_off_t
644
pobyso_on()
645
{
646
  return sollya_lib_on();
647
} /* End pobyso_on. */
648

    
649

    
650
/* @see pobyso.h#pobyso_parse_string */
651
pobyso_func_exp_t
652
pobyso_parse_string(const char* expression)
653
{
654
  int expressionLength, i;
655
  char *expressionWithSemiCo;
656
  sollya_obj_t expressionSo;
657

    
658
  /* Arguments check. */
659
  if (expression == NULL)
660
  {
661
    pobyso_error_message("pobyso_parse_string",
662
                        "NULL_POINTER_ARGUMENT",
663
                        "The expression is a NULL pointer");
664
    return  NULL;
665
  }
666
  expressionLength = strlen(expression);
667
  if (expressionLength == 0)
668
  {
669
    pobyso_error_message("pobyso_parse_string",
670
                        "EMPTY_STRING_ARGUMENT",
671
                        "The expression is an empty string");
672
    return NULL;
673
  }
674
  /* Search from the last char of the expression until, whichever happens
675
   * first:
676
   * a ";" is found;
677
   * a char  != ';' is found the the ";" is appended.
678
   * If the head of the string is reached before any of these two events happens
679
   * return an error.
680
   */
681
  for (i = expressionLength - 1 ; i >= 0 ; i--)
682
  {
683
    if (expression[i] == ';') /* Nothing special to do:
684
                                 try to parse the string*/
685
    {
686
      expressionSo = sollya_lib_parse_string(expression);
687
      if (sollya_lib_obj_is_error(expressionSo))
688
      {
689
        sollya_lib_clear_obj(expressionSo);
690
        return NULL;
691
      }
692
      else
693
      {
694
        return expressionSo;
695
      }
696
    }
697
    else
698
    {
699
      if (expression[i] == ' ' || expression[i] == '\t') /* A blank,
700
                                                           just continue. */
701
      {
702
        continue;
703
      }
704
      else /* a character != ';' and from a blank: create the ';'ed string. */
705
      {
706
        /* Create a new string for the expression, add the ";" and
707
         * and call sollya_lib_parse_string. */
708
        expressionWithSemiCo = calloc(i + 3, sizeof(char));
709
        if (expressionWithSemiCo == NULL)
710
        {
711
          pobyso_error_message("pobyso_parse_string",
712
                                "MEMORY_ALLOCATION_ERROR",
713
                                "Could not allocate the expression string");
714
          return NULL;
715
        }
716
        strncpy(expressionWithSemiCo, expression, i+1);
717
        expressionWithSemiCo[i + 1] = ';';
718
        expressionWithSemiCo[i + 2] = '\0';
719
        expressionSo = sollya_lib_parse_string(expressionWithSemiCo);
720
        free(expressionWithSemiCo);
721
        if (sollya_lib_obj_is_error(expressionSo))
722
        {
723
          sollya_lib_clear_obj(expressionSo);
724
          return NULL;
725
        }
726
        else
727
        {
728
          return expressionSo;
729
        }
730
      } /* End character != ';' and from a blank. */
731
    /* Create a new string for the expression, add the ";" and
732
     * and call sollya_lib_parse_string. */
733
    } /* End else. */
734
  } /* End for. */
735
  /* We get here, it is because we did not find any char == anything different
736
   * from ' ' or '\t': the string is empty.
737
   */
738
  pobyso_error_message("pobyso_parse_string",
739
                       "ONLY_BLANK_ARGUMENT",
740
                        "The expression is only made of blanks");
741
  return NULL;
742
} /* pobyso_parse_string */
743

    
744
/** @see pobyso.h#pobyso_range_from_bounds */
745
pobyso_range_t
746
pobyso_range_from_bounds(mpfr_t lowerBound, mpfr_t upperBound)
747
{
748
  /* Supferficial check of arguments. */
749
  if (mpfr_cmp(lowerBound, upperBound) > 0)
750
  {
751
      return(NULL);
752
  }
753
  return(sollya_lib_range_from_bounds(lowerBound, upperBound));
754
}
755

    
756
pobyso_func_exp_t
757
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
758
                                      long int degree,
759
                                      pobyso_range_t interval,
760
                                      pobyso_func_exp_t weight,
761
                                      double quality,
762
                                      pobyso_range_t bounds)
763
{
764
  sollya_obj_t  degreeSo = NULL;
765
  sollya_obj_t qualitySo = NULL;
766

    
767
  degreeSo = sollya_lib_constant_from_int(degree);
768
  qualitySo = sollya_lib_constant_from_double(quality);
769

    
770
  sollya_lib_clear_obj(degreeSo);
771
  sollya_lib_clear_obj(qualitySo);
772
  return NULL;
773
} /* End pobyso_remez_canonical_monomials_base. */
774

    
775
int
776
pobyso_set_canonical_on()
777
{
778
  pobyso_on_off_t currentCanonicalModeSo;
779
  pobyso_on_off_t on;
780

    
781
  currentCanonicalModeSo = sollya_lib_get_canonical();
782
  if (sollya_lib_is_on(currentCanonicalModeSo))
783
  {
784
    sollya_lib_clear_obj(currentCanonicalModeSo);
785
    return POBYSO_ON;
786
  }
787
  else
788
  {
789
    on = sollya_lib_on();
790
    sollya_lib_set_canonical(on);
791
    sollya_lib_clear_obj(on);
792
    sollya_lib_clear_obj(currentCanonicalModeSo);
793
    return POBYSO_OFF;
794
  }
795
} /* End pobyso_set_canonical_on. */
796

    
797
int
798
pobyso_set_verbosity_off()
799
{
800
  sollya_obj_t verbosityLevelZeroSo;
801
  sollya_obj_t currentVerbosityLevelSo = NULL;
802
  int currentVerbosityLevel = 0;
803

    
804
  currentVerbosityLevelSo = sollya_lib_get_verbosity();
805
  sollya_lib_get_constant_as_int(&currentVerbosityLevel,
806
                                 currentVerbosityLevelSo);
807
  verbosityLevelZeroSo = sollya_lib_constant_from_int(0);
808
  sollya_lib_set_verbosity(verbosityLevelZeroSo);
809
  sollya_lib_clear_obj(verbosityLevelZeroSo);
810
  sollya_lib_clear_obj(currentVerbosityLevelSo);
811
  return currentVerbosityLevel;
812
} /* End of pobyso_set_verbosity_off. */
813

    
814
int
815
pobyso_set_verbosity_to(int newVerbosityLevel)
816
{
817
  int initialVerbosityLevel = 0;
818
  sollya_obj_t initialVerbosityLevelSo = NULL;
819
  sollya_obj_t newVerbosityLevelSo = NULL;
820

    
821
  initialVerbosityLevelSo = sollya_lib_get_verbosity();
822
  sollya_lib_get_constant_as_int(&initialVerbosityLevel,
823
                                 initialVerbosityLevelSo);
824
  sollya_lib_clear_obj(initialVerbosityLevelSo);
825
  if (newVerbosityLevel < 0)
826
  {
827
    pobyso_error_message("pobyso_set_verbosity_to",
828
                        "INVALID_VALUE",
829
                        "The new verbosity level is a negative number");
830
    return initialVerbosityLevel;
831
  }
832
  newVerbosityLevelSo = sollya_lib_constant_from_int(newVerbosityLevel);
833
  sollya_lib_set_verbosity(newVerbosityLevelSo);
834
  sollya_lib_clear_obj(newVerbosityLevelSo);
835
  return initialVerbosityLevel;
836
} /* End of pobyso_set_verbosity_to. */
837

    
838
/**
839
 * @see pobyso.h#pobyso_subpoly
840
 */
841
pobyso_func_exp_t
842
pobyso_subpoly(pobyso_func_exp_t polynomialSo, long expsNum, long int* expsList)
843
{
844
  sollya_obj_t  expsListSo    = NULL;
845
  sollya_obj_t* expsList_pso  = NULL;
846
  sollya_obj_t subpoly        = NULL;
847
  int i, j;
848

    
849
  /* Arguments check. */
850
  if (polynomialSo == NULL) return NULL;
851
  if (expsNum < 0) return NULL;
852
  if (expsNum == 0) return sollya_lib_copy_obj(polynomialSo);
853
  if (expsList == 0) return NULL;
854
  /* Create a list of Sollya constants. */
855
  expsList_pso = (sollya_obj_t*) malloc(expsNum * sizeof(sollya_obj_t));
856
  if (expsList_pso == NULL)
857
  {
858
    pobyso_error_message("pobyso_subpoly",
859
                          "MEMORY_ALLOCATION_ERROR",
860
                          "Could not allocate the Sollya exponents list");
861
    return NULL;
862
  }
863
  /* Fill up the list. */
864
  for (i = 0 ; i < expsNum ; i++)
865
  {
866
    /* Abort if an exponent is negative. */
867
    if (expsList[i] < 0 )
868
    {
869
      for (j = 0 ; j < i ; j++)
870
      {
871
        sollya_lib_clear_obj(expsList_pso[j]);
872
      }
873
      free(expsList_pso);
874
      return NULL;
875
    }
876
    expsList_pso[i] = sollya_lib_constant_from_int64(expsList[i]);
877
  } /* End for */
878
  expsListSo = sollya_lib_list(expsList_pso, expsNum);
879
  for (i = 0 ; i < expsNum ; i++)
880
  {
881
    sollya_lib_clear_obj(expsList_pso[i]);
882
  }
883
  free(expsList_pso);
884
  if (expsListSo == NULL)
885
  {
886
    pobyso_error_message("pobyso_subpoly",
887
                          "LIST_CREATIONERROR",
888
                          "Could not create the exponents list");
889
    return NULL;
890
  }
891
  subpoly = sollya_lib_subpoly(polynomialSo, expsListSo);
892
  pobyso_autoprint(expsListSo);
893
  sollya_lib_clear_obj(expsListSo);
894
  return subpoly;
895
} /* pobyso_subpoly. */
896

    
897
/* Attic from the sollya_lib < 4. */
898
#if 0
899
chain*
900
pobyso_create_canonical_monomials_base(const unsigned int degree)
901
{
902
  int i    = 0;
903
  chain *monomials  = NULL;
904
  node  *monomial   = NULL;
905

906
  for(i = degree ; i >= 0  ; i--)
907
  {
908
     monomial   = makePow(makeVariable(), makeConstantDouble((double)i));
909
     monomials  = addElement(monomials, monomial);
910
     fprintf(stderr, "pobyso_create_canonical_monomials_base: %u\n", i);
911
  }
912
  if (monomials == NULL)
913
  {
914
    pobyso_error_message("pobyso_create_canonical_monomial_base",
915
                        "CHAIN_CREATION_ERROR",
916
                        "Could not create the monomials chain");
917
    return(NULL);
918
  }
919
  return(monomials);
920
} /* End pobyso_create_canonical_monomials_base. */
921

922
chain*
923
pobyso_create_chain_from_int_array(int* intArray,
924
                                    const unsigned int arrayLength)
925
{
926
  int i = 0;
927
  chain *newChain = NULL;
928
  int *currentInt;
929

930
  if (arrayLength == 0) return(NULL);
931
  if (intArray == NULL)
932
  {
933
    pobyso_error_message("pobyso_create_chain_from_int_array",
934
                        "NULL_POINTER_ARGUMENT",
935
                        "The array is a NULL pointer");
936
    return(NULL);
937
  }
938
  for (i = arrayLength - 1 ; i >= 0 ; i--)
939
  {
940
    currentInt = malloc(sizeof(int));
941
    if (currentInt == NULL)
942
    {
943
      pobyso_error_message("pobyso_create_chain_from_int_array",
944
                          "MEMORY_ALLOCATION_ERROR",
945
                          "Could not allocate one of the integers");
946
      freeChain(newChain, free);
947
      return(NULL);
948
    }
949
    *currentInt = intArray[i];
950
    newChain = addElement(newChain, currentInt);
951
  }
952
  return(newChain);
953
} // End pobyso_create_chain_from_int_array. */
954

955
chain*
956
pobyso_create_chain_from_unsigned_int_array(unsigned int* intArray,
957
                                        const unsigned int arrayLength)
958
{
959
  int i = 0;
960
  chain *newChain = NULL;
961
  unsigned int *currentInt;
962

963
  /* Argument checking. */
964
  if (arrayLength == 0) return(NULL);
965
  if (intArray == NULL)
966
  {
967
    pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
968
                        "NULL_POINTER_ARGUMENT",
969
                        "The array is a NULL pointer");
970
    return(NULL);
971
  }
972
  for (i = arrayLength - 1 ; i >= 0 ; i--)
973
  {
974
    currentInt = malloc(sizeof(unsigned int));
975
    if (currentInt == NULL)
976
    {
977
      pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
978
                          "MEMORY_ALLOCATION_ERROR",
979
                          "Could not allocate one of the integers");
980
      freeChain(newChain, free);
981
      return(NULL);
982
    }
983
    *currentInt = intArray[i];
984
    newChain = addElement(newChain, currentInt);
985
  }
986
  return(newChain);
987
} // End pobyso_create_chain_from_unsigned_int_array. */
988

989
node*
990
pobyso_differentiate(node *functionNode)
991
{
992
  /* Argument checking. */
993
  node *differentialNode;
994
  if (functionNode == NULL)
995
  {
996
    pobyso_error_message("pobyso_differentiate",
997
                        "NULL_POINTER_ARGUMENT",
998
                        "The function to differentiate is a NULL pointer");
999
    return(NULL);
1000
  }
1001
  differentialNode = differentiate(functionNode);
1002
  if (differentialNode == NULL)
1003
  {
1004
    pobyso_error_message("pobyso_differentiate",
1005
                        "INTERNAL ERROR",
1006
                        "Sollya could not differentiate the function");
1007
  }
1008
  return(differentialNode);
1009
} // End pobyso_differentiate
1010

1011

1012
int
1013
pobyso_dirty_infnorm(mpfr_t infNorm,
1014
                      node *functionNode,
1015
                      mpfr_t lowerBound,
1016
                      mpfr_t upperBound,
1017
                      mp_prec_t precision)
1018
{
1019
  int functionCallResult;
1020
  /* Arguments checking. */
1021
  if (functionNode == NULL)
1022
  {
1023
    pobyso_error_message("pobyso_dirty_infnorm",
1024
                        "NULL_POINTER_ARGUMENT",
1025
                        "The function to compute is a NULL pointer");
1026
    return(1);
1027
  }
1028
  if (mpfr_cmp(lowerBound, upperBound) > 0)
1029
  {
1030
    pobyso_error_message("pobyso_dirty_infnorm",
1031
                        "INCOHERENT_INPUT_DATA",
1032
                        "The lower bond is greater than the upper bound");
1033
    return(1);
1034
  }
1035
  /* Particular cases. */
1036
  if (mpfr_cmp(lowerBound, upperBound) == 0)
1037
  {
1038
    functionCallResult = pobyso_evaluate_faithful(infNorm,
1039
                                                  functionNode,
1040
                                                  lowerBound,
1041
                                                  precision);
1042
    return(functionCallResult);
1043
  }
1044
  if (isConstant(functionNode))
1045
  {
1046
    functionCallResult = pobyso_evaluate_faithful(infNorm,
1047
                                                  functionNode,
1048
                                                  lowerBound,
1049
                                                  precision);
1050
    if (!functionCallResult)
1051
    {
1052
      mpfr_abs(infNorm, infNorm, MPFR_RNDN);
1053
    }
1054
    return(functionCallResult);
1055
  }
1056
  uncertifiedInfnorm(infNorm,
1057
                      functionNode,
1058
                      lowerBound,
1059
                      upperBound,
1060
                      POBYSO_DEFAULT_POINTS,
1061
                      precision);
1062
  return(0);
1063
} /* End pobyso_dirty_infnorm. */
1064

1065
int
1066
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
1067
                          node *nodeToEvaluate,
1068
                          mpfr_t argument,
1069
                          mpfr_prec_t precision)
1070
{
1071
  /* Check input arguments. */
1072
  if (nodeToEvaluate == NULL)
1073
  {
1074
    pobyso_error_message("pobyso_evaluate_faithful",
1075
                        "NULL_POINTER_ARGUMENT",
1076
                        "nodeToEvaluate is a NULL pointer");
1077
    return(1);
1078
  }
1079
  evaluateFaithful(faithfulEvaluation,
1080
                   nodeToEvaluate,
1081
                   argument,
1082
                   precision);
1083
  return(0);
1084
} /* End pobyso_evaluate_faithfull. */
1085

1086
chain*
1087
pobyso_find_zeros(node *function,
1088
                  mpfr_t *lower_bound,
1089
                  mpfr_t *upper_bound)
1090
{
1091
  mp_prec_t currentPrecision;
1092
  mpfr_t currentDiameter;
1093
  rangetype bounds;
1094

1095
  currentPrecision = getToolPrecision();
1096
  mpfr_init2(currentDiameter, currentPrecision);
1097

1098
  bounds.a = lower_bound;
1099
  bounds.b = upper_bound;
1100

1101
  if (bounds.a == NULL || bounds.b == NULL)
1102
  {
1103
    pobyso_error_message("pobyso_find_zeros",
1104
                        "MEMORY_ALLOCATION_ERROR",
1105
                        "Could not allocate one of the bounds");
1106
    return(NULL);
1107
  }
1108
  return(findZerosFunction(function,
1109
                            bounds,
1110
                            currentPrecision,
1111
                            currentDiameter));
1112
} /* End pobyso_find_zeros. */
1113

1114
void
1115
pobyso_free_chain_of_nodes(chain *theChainOfNodes)
1116
{
1117
  node *currentNode           = NULL;
1118
  chain *currentChainElement  = NULL;
1119
  chain *nextChainElement     = NULL;
1120

1121
  nextChainElement = theChainOfNodes;
1122

1123
  while(nextChainElement != NULL)
1124
  {
1125
    currentChainElement = nextChainElement;
1126
    currentNode = (node*)(currentChainElement->value);
1127
    nextChainElement = nextChainElement->next;
1128
    free_memory(currentNode);
1129
    free((void*)(currentChainElement));
1130
  }
1131
} /* End pobyso_free_chain_of_nodes. */
1132

1133
void
1134
pobyso_free_range(rangetype range)
1135
{
1136

1137
  mpfr_clear(*(range.a));
1138
  mpfr_clear(*(range.b));
1139
  free(range.a);
1140
  free(range.b);
1141
} /* End pobyso_free_range. */
1142

1143
node*
1144
pobyso_fp_minimax_canonical_monomials_base(node *function,
1145
                                      int degree,
1146
                                      chain *formats,
1147
                                      chain *points,
1148
                                      mpfr_t lowerBound,
1149
                                      mpfr_t upperBound,
1150
                                      int fpFixedArg,
1151
                                      int absRel,
1152
                                      node *constPart,
1153
                                      node *minimax)
1154
{
1155
  return(NULL);
1156
} /* End pobyso_fp_minimax_canonical_monomials_base. */
1157

1158
node*
1159
pobyso_parse_function(char *functionString,
1160
                      char *freeVariableNameString)
1161
{
1162
  if (functionString == NULL || freeVariableNameString == NULL)
1163
  {
1164
    pobyso_error_message("pobyso_parse_function",
1165
                        "NULL_POINTER_ARGUMENT",
1166
                        "One of the arguments is a NULL pointer");
1167
    return(NULL);
1168
  }
1169
  return(parseString(functionString));
1170

1171
} /* End pobyso_parse_function */
1172

1173
node*
1174
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
1175
                                                  unsigned int mode,
1176
                                                  mpfr_t lowerBound,
1177
                                                  mpfr_t upperBound,
1178
                                                  mpfr_t eps)
1179
{
1180
  node *weight              = NULL;
1181
  node *bestApproxPolyNode  = NULL;
1182
  node *bestApproxHorner    = NULL;
1183
  node *errorNode           = NULL;
1184
  rangetype degreeRange;
1185
  mpfr_t quality;
1186
  mpfr_t currentError;
1187
  unsigned int degree;
1188

1189
  /* Check the parameters. */
1190
  if (functionNode == NULL)
1191
  {
1192
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
1193
                        "NULL_POINTER_ARGUMENT",
1194
                        "functionNode is a NULL pointer");
1195
    return(NULL);
1196
  }
1197
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
1198
  {
1199
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
1200
                        "INCOHERENT_INPUT_DATA",
1201
                        "the lower_bound >= upper_bound");
1202
    return(NULL);
1203
  }
1204
  /* Set the weight. */
1205
  if (mode == POBYSO_ABSOLUTE)
1206
  {
1207
    /* Set the weight to 1 for the ABSOLUTE_MODE. */
1208
    weight = makeConstantDouble(1.0);
1209
  }
1210
  else
1211
  {
1212
    if (mode == POBYSO_RELATIVE)
1213
    {
1214
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1215
                          "NOT_IMPLEMENTED",
1216
                          "the search for relative error is not implemented yet");
1217
      return(NULL);
1218
    }
1219
    else
1220
    {
1221
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1222
                          "INCOHERENT_INPUT_DATA",
1223
                          "the mode is node of POBYSO_ABOLUTE or POBYSO_RELATIVE");
1224
      return(NULL);
1225
    }
1226
  }
1227
  //fprintf(stderr, "\n\n\n******* I'm here! ********\n\n\n");
1228
  degreeRange = guessDegree(functionNode,
1229
                            weight,
1230
                            lowerBound,
1231
                            upperBound,
1232
                            eps,
1233
                            POBYSO_GUESS_DEGREE_BOUND);
1234
  degree = mpfr_get_ui(*(degreeRange.a), MPFR_RNDN);
1235
  //fprintf(stderr, "\n\n\n******* I'm back! ********\n\n\n");
1236
  fprintf(stderr, "Guessed degree: ");
1237
  mpfr_out_str(stderr, 10, 17, *(degreeRange.a), MPFR_RNDN);
1238
  fprintf(stderr, " - as int: %u\n", degree);
1239
  /* Reduce the degree by 1 in the foolish hope it could work. */
1240
  if (degree > 0) degree--;
1241
  /* Both elements of degreeRange where "inited" within guessDegree. */
1242
  mpfr_clear(*(degreeRange.a));
1243
  mpfr_clear(*(degreeRange.b));
1244
  free(degreeRange.a);
1245
  free(degreeRange.b);
1246
  /* Mimic the default behavior of interactive Sollya. */
1247
  mpfr_init(quality);
1248
  mpfr_set_d(quality, 1e-5, MPFR_RNDN);
1249
  mpfr_init2(currentError, getToolPrecision());
1250
  mpfr_set_inf(currentError,1);
1251

1252
  /* Try to refine the initial guess: loop with increasing degrees until
1253
   * we find a satisfactory one. */
1254
  while(mpfr_cmp(currentError, eps) > 0)
1255
  {
1256
    /* Get rid of the previous polynomial, if any. */
1257
    if (bestApproxPolyNode != NULL)
1258
    {
1259
      free_memory(bestApproxPolyNode);
1260
    }
1261
    fprintf(stderr, "Degree: %u\n", degree);
1262
    fprintf(stderr, "Calling pobyso_remez_canonical_monomials_base...\n");
1263
    /* Try to find a polynomial with the guessed degree. */
1264
    bestApproxPolyNode = pobyso_remez_canonical_monomials_base(functionNode,
1265
                                                            weight,
1266
                                                            degree,
1267
                                                            lowerBound,
1268
                                                            upperBound,
1269
                                                            quality);
1270

1271
    if (bestApproxPolyNode == NULL)
1272
    {
1273
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1274
                          "INTERNAL_ERROR",
1275
                          "could not compute the bestApproxPolyNode");
1276
      mpfr_clear(currentError);
1277
      mpfr_clear(quality);
1278
      return(NULL);
1279
    }
1280

1281
    setDisplayMode(DISPLAY_MODE_DECIMAL);
1282
    fprintTree(stderr, bestApproxPolyNode);
1283
    fprintf(stderr, "\n\n");
1284

1285
    errorNode = makeSub(copyTree(functionNode), copyTree(bestApproxPolyNode));
1286
    /* Check the error with the computed polynomial. */
1287
    uncertifiedInfnorm(currentError,
1288
                        errorNode,
1289
                        lowerBound,
1290
                        upperBound,
1291
                        POBYSO_INF_NORM_NUM_POINTS,
1292
                        getToolPrecision());
1293
    fprintf(stderr, "Inf norm: ");
1294
    mpfr_out_str(stderr, 10, 17, currentError, MPFR_RNDN);
1295
    fprintf(stderr, "\n\n");
1296
    /* Free the errorNode but not the bestApproxPolyNode (we need it if
1297
     * we exit the loop at the next iteration). */
1298
    free_memory(errorNode);
1299
    degree++;
1300
  }
1301
  /* Use an intermediate variable, since horner() creates a new node
1302
   * and does not reorder the argument "in place". This allows for the memory
1303
   * reclaim of bestApproxHorner.
1304
   */
1305
  bestApproxHorner = horner(bestApproxPolyNode);
1306
  free_memory(bestApproxPolyNode);
1307
  mpfr_clear(currentError);
1308
  mpfr_clear(quality);
1309
  free_memory(weight);
1310
  return(bestApproxHorner);
1311
} /* End pobyso_remez_approx_canonical_monomials_base_for_error */
1312

1313
node*
1314
pobyso_remez_canonical_monomials_base(node *function,
1315
                                     node *weight,
1316
                                     unsigned int degree,
1317
                                     mpfr_t lowerBound,
1318
                                     mpfr_t upperBound,
1319
                                     mpfr_t quality)
1320
{
1321
  node  *bestApproxPoly = NULL;
1322
  chain *monomials      = NULL;
1323
  chain *curMonomial    = NULL;
1324

1325
  mpfr_t satisfying_error;
1326
  mpfr_t target_error;
1327

1328
  /* Argument checking */
1329
  /* Function tree. */
1330
  if (function == NULL)
1331
  {
1332
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1333
                        "NULL_POINTER_ARGUMENT",
1334
                        "the \"function\" argument is a NULL pointer");
1335
    return(NULL);
1336
  }
1337
  if (weight == NULL)
1338
  {
1339
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1340
                        "NULL_POINTER_ARGUMENT",
1341
                        "the \"weight\" argument is a NULL pointer");
1342
    return(NULL);
1343
  }
1344
  /* Check the bounds. */
1345
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
1346
  {
1347
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1348
                        "INCOHERENT_IMPUT_DATA",
1349
                        "the lower_bound >= upper_bound");
1350
    return(NULL);
1351
  }
1352
  /* The quality must be a non null positive number. */
1353
  if (mpfr_sgn(quality) <= 0)
1354
  {
1355
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1356
                        "INCOHERENT_INPUT_DATA",
1357
                        "the quality <= 0");
1358
  }
1359
  /* End argument checking. */
1360
  /* Create the monomials nodes chains. */
1361
  monomials = pobyso_create_canonical_monomials_base(degree);
1362
  fprintf(stderr, "monomials chain length = %d\n", lengthChain(monomials));
1363
  if (monomials == NULL || (lengthChain(monomials) != degree + 1))
1364
  {
1365
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1366
                        "CHAIN_CREATION_ERROR",
1367
                        "could not create the monomials chain");
1368
    return(NULL);
1369
  }
1370
  curMonomial = monomials;
1371

1372
  while (curMonomial != NULL)
1373
  {
1374
    fprintf(stderr, "monomial tree: ");
1375
    //mpfr_out_str(stderr, 10, 17, *((mpfr_t*)((node*)(curMonomial->value))->value), MPFR_RNDN);
1376
    fprintTree(stderr, (node*)(curMonomial->value));
1377
    fprintf(stderr, "\n");
1378
    curMonomial = curMonomial->next;
1379
  }
1380

1381
  /* Deal with NULL weight. */
1382
  if (weight == NULL)
1383
  {
1384
    weight = makeConstantDouble(1.0);
1385
  }
1386
  /* Compute the best polynomial with the required quality.
1387
     The behavior is as if satisfying error and target_error had
1388
     not been used.*/
1389
  mpfr_init(satisfying_error);
1390
  mpfr_init(target_error);
1391
  mpfr_set_str(satisfying_error, "0", 10, MPFR_RNDN);
1392
  mpfr_set_inf(target_error, 1);
1393

1394

1395
  fprintf(stderr, "satisfying_error: ");
1396
  mpfr_out_str(stderr, 10, 17, satisfying_error, MPFR_RNDN);
1397
  fprintf(stderr, ".\n");
1398
  fprintf(stderr, "target_error: ");
1399
  mpfr_out_str(stderr, 10, 17, target_error,MPFR_RNDN);
1400
  fprintf(stderr, ".\n");
1401

1402
  fprintf(stderr,
1403
          "current precision: %li\n", getToolPrecision());
1404
  /* Call the Sollya function. */
1405
  bestApproxPoly = remez(function,
1406
                          weight,
1407
                          monomials,
1408
                          lowerBound,
1409
                          upperBound,
1410
                          quality,
1411
                          satisfying_error,
1412
                          target_error,
1413
                          getToolPrecision());
1414

1415
  mpfr_clear(satisfying_error);
1416
  mpfr_clear(target_error);
1417
  pobyso_free_chain_of_nodes(monomials);
1418

1419
  return(bestApproxPoly);
1420
} /* End pobyso_remez_canonical_monomials_base. */
1421

1422
#endif
1423

    
1424
void
1425
pobyso_error_message(char *functionName, char *messageName, char* message)
1426
{
1427
  fprintf(stderr, "?%s: %s.\n%s.\n", functionName, messageName, message);
1428
} /* End pobyso_error_message */