Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (43,49 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_parse_string*/
636
pobyso_func_exp_t
637
pobyso_parse_string(const char* expression)
638
{
639
  int expressionLength, i;
640
  char *expressionWithSemiCo;
641
  sollya_obj_t expressionSo;
642

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

    
729
pobyso_func_exp_t
730
pobyso_remez_canonical_monomials_base(pobyso_func_exp_t function,
731
                                      long int degree,
732
                                      pobyso_range_t interval,
733
                                      pobyso_func_exp_t weight,
734
                                      double quality,
735
                                      pobyso_range_t bounds)
736
{
737
  sollya_obj_t  degreeSo = NULL;
738
  sollya_obj_t qualitySo = NULL;
739

    
740
  degreeSo = sollya_lib_constant_from_int(degree);
741
  qualitySo = sollya_lib_constant_from_double(quality);
742

    
743
  sollya_lib_clear_obj(degreeSo);
744
  sollya_lib_clear_obj(qualitySo);
745
  return NULL;
746
} /* End pobyso_remez_canonical_monomials_base. */
747

    
748
int
749
pobyso_set_canonical_on()
750
{
751
  pobyso_on_off_t currentCanonicalModeSo;
752
  pobyso_on_off_t on;
753

    
754
  currentCanonicalModeSo = sollya_lib_get_canonical();
755
  if (sollya_lib_is_on(currentCanonicalModeSo))
756
  {
757
    sollya_lib_clear_obj(currentCanonicalModeSo);
758
    return POBYSO_ON;
759
  }
760
  else
761
  {
762
    on = sollya_lib_on();
763
    sollya_lib_set_canonical(on);
764
    sollya_lib_clear_obj(on);
765
    sollya_lib_clear_obj(currentCanonicalModeSo);
766
    return POBYSO_OFF;
767
  }
768
} /* End pobyso_set_canonical_on. */
769

    
770
int
771
pobyso_set_verbosity_off()
772
{
773
  sollya_obj_t verbosityLevelZeroSo;
774
  sollya_obj_t currentVerbosityLevelSo = NULL;
775
  int currentVerbosityLevel = 0;
776

    
777
  currentVerbosityLevelSo = sollya_lib_get_verbosity();
778
  sollya_lib_get_constant_as_int(&currentVerbosityLevel,
779
                                 currentVerbosityLevelSo);
780
  verbosityLevelZeroSo = sollya_lib_constant_from_int(0);
781
  sollya_lib_set_verbosity(verbosityLevelZeroSo);
782
  sollya_lib_clear_obj(verbosityLevelZeroSo);
783
  sollya_lib_clear_obj(currentVerbosityLevelSo);
784
  return currentVerbosityLevel;
785
} /* End of pobyso_set_verbosity_off. */
786

    
787
int
788
pobyso_set_verbosity_to(int newVerbosityLevel)
789
{
790
  int initialVerbosityLevel = 0;
791
  sollya_obj_t initialVerbosityLevelSo = NULL;
792
  sollya_obj_t newVerbosityLevelSo = NULL;
793

    
794
  initialVerbosityLevelSo = sollya_lib_get_verbosity();
795
  sollya_lib_get_constant_as_int(&initialVerbosityLevel,
796
                                 initialVerbosityLevelSo);
797
  sollya_lib_clear_obj(initialVerbosityLevelSo);
798
  if (newVerbosityLevel < 0)
799
  {
800
    pobyso_error_message("pobyso_set_verbosity_to",
801
                        "INVALID_VALUE",
802
                        "The new verbosity level is a negative number");
803
    return initialVerbosityLevel;
804
  }
805
  newVerbosityLevelSo = sollya_lib_constant_from_int(newVerbosityLevel);
806
  sollya_lib_set_verbosity(newVerbosityLevelSo);
807
  sollya_lib_clear_obj(newVerbosityLevelSo);
808
  return initialVerbosityLevel;
809
} /* End of pobyso_set_verbosity_to. */
810

    
811
/**
812
 * @see pobyso.h#pobyso_subpoly
813
 */
814
pobyso_func_exp_t
815
pobyso_subpoly(pobyso_func_exp_t polynomialSo, long expsNum, long int* expsList)
816
{
817
  sollya_obj_t  expsListSo    = NULL;
818
  sollya_obj_t* expsList_pso  = NULL;
819
  sollya_obj_t subpoly        = NULL;
820
  int i, j;
821

    
822
  /* Arguments check. */
823
  if (polynomialSo == NULL) return NULL;
824
  if (expsNum < 0) return NULL;
825
  if (expsNum == 0) return sollya_lib_copy_obj(polynomialSo);
826
  if (expsList == 0) return NULL;
827
  /* Create a list of Sollya constants. */
828
  expsList_pso = (sollya_obj_t*) malloc(expsNum * sizeof(sollya_obj_t));
829
  if (expsList_pso == NULL)
830
  {
831
    pobyso_error_message("pobyso_subpoly",
832
                          "MEMORY_ALLOCATION_ERROR",
833
                          "Could not allocate the Sollya exponents list");
834
    return NULL;
835
  }
836
  /* Fill up the list. */
837
  for (i = 0 ; i < expsNum ; i++)
838
  {
839
    /* Abort if an exponent is negative. */
840
    if (expsList[i] < 0 )
841
    {
842
      for (j = 0 ; j < i ; j++)
843
      {
844
        sollya_lib_clear_obj(expsList_pso[j]);
845
      }
846
      free(expsList_pso);
847
      return NULL;
848
    }
849
    expsList_pso[i] = sollya_lib_constant_from_int64(expsList[i]);
850
  } /* End for */
851
  expsListSo = sollya_lib_list(expsList_pso, expsNum);
852
  for (i = 0 ; i < expsNum ; i++)
853
  {
854
    sollya_lib_clear_obj(expsList_pso[i]);
855
  }
856
  free(expsList_pso);
857
  if (expsListSo == NULL)
858
  {
859
    pobyso_error_message("pobyso_subpoly",
860
                          "LIST_CREATIONERROR",
861
                          "Could not create the exponents list");
862
    return NULL;
863
  }
864
  subpoly = sollya_lib_subpoly(polynomialSo, expsListSo);
865
  pobyso_autoprint(expsListSo);
866
  sollya_lib_clear_obj(expsListSo);
867
  return subpoly;
868
} /* pobyso_subpoly. */
869

    
870
/* Attic from the sollya_lib < 4. */
871
#if 0
872
chain*
873
pobyso_create_canonical_monomials_base(const unsigned int degree)
874
{
875
  int i    = 0;
876
  chain *monomials  = NULL;
877
  node  *monomial   = NULL;
878

879
  for(i = degree ; i >= 0  ; i--)
880
  {
881
     monomial   = makePow(makeVariable(), makeConstantDouble((double)i));
882
     monomials  = addElement(monomials, monomial);
883
     fprintf(stderr, "pobyso_create_canonical_monomials_base: %u\n", i);
884
  }
885
  if (monomials == NULL)
886
  {
887
    pobyso_error_message("pobyso_create_canonical_monomial_base",
888
                        "CHAIN_CREATION_ERROR",
889
                        "Could not create the monomials chain");
890
    return(NULL);
891
  }
892
  return(monomials);
893
} /* End pobyso_create_canonical_monomials_base. */
894

895
chain*
896
pobyso_create_chain_from_int_array(int* intArray,
897
                                    const unsigned int arrayLength)
898
{
899
  int i = 0;
900
  chain *newChain = NULL;
901
  int *currentInt;
902

903
  if (arrayLength == 0) return(NULL);
904
  if (intArray == NULL)
905
  {
906
    pobyso_error_message("pobyso_create_chain_from_int_array",
907
                        "NULL_POINTER_ARGUMENT",
908
                        "The array is a NULL pointer");
909
    return(NULL);
910
  }
911
  for (i = arrayLength - 1 ; i >= 0 ; i--)
912
  {
913
    currentInt = malloc(sizeof(int));
914
    if (currentInt == NULL)
915
    {
916
      pobyso_error_message("pobyso_create_chain_from_int_array",
917
                          "MEMORY_ALLOCATION_ERROR",
918
                          "Could not allocate one of the integers");
919
      freeChain(newChain, free);
920
      return(NULL);
921
    }
922
    *currentInt = intArray[i];
923
    newChain = addElement(newChain, currentInt);
924
  }
925
  return(newChain);
926
} // End pobyso_create_chain_from_int_array. */
927

928
chain*
929
pobyso_create_chain_from_unsigned_int_array(unsigned int* intArray,
930
                                        const unsigned int arrayLength)
931
{
932
  int i = 0;
933
  chain *newChain = NULL;
934
  unsigned int *currentInt;
935

936
  /* Argument checking. */
937
  if (arrayLength == 0) return(NULL);
938
  if (intArray == NULL)
939
  {
940
    pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
941
                        "NULL_POINTER_ARGUMENT",
942
                        "The array is a NULL pointer");
943
    return(NULL);
944
  }
945
  for (i = arrayLength - 1 ; i >= 0 ; i--)
946
  {
947
    currentInt = malloc(sizeof(unsigned int));
948
    if (currentInt == NULL)
949
    {
950
      pobyso_error_message("pobyso_create_chain_from_unsigned_int_array",
951
                          "MEMORY_ALLOCATION_ERROR",
952
                          "Could not allocate one of the integers");
953
      freeChain(newChain, free);
954
      return(NULL);
955
    }
956
    *currentInt = intArray[i];
957
    newChain = addElement(newChain, currentInt);
958
  }
959
  return(newChain);
960
} // End pobyso_create_chain_from_unsigned_int_array. */
961

962
node*
963
pobyso_differentiate(node *functionNode)
964
{
965
  /* Argument checking. */
966
  node *differentialNode;
967
  if (functionNode == NULL)
968
  {
969
    pobyso_error_message("pobyso_differentiate",
970
                        "NULL_POINTER_ARGUMENT",
971
                        "The function to differentiate is a NULL pointer");
972
    return(NULL);
973
  }
974
  differentialNode = differentiate(functionNode);
975
  if (differentialNode == NULL)
976
  {
977
    pobyso_error_message("pobyso_differentiate",
978
                        "INTERNAL ERROR",
979
                        "Sollya could not differentiate the function");
980
  }
981
  return(differentialNode);
982
} // End pobyso_differentiate
983

984

985
int
986
pobyso_dirty_infnorm(mpfr_t infNorm,
987
                      node *functionNode,
988
                      mpfr_t lowerBound,
989
                      mpfr_t upperBound,
990
                      mp_prec_t precision)
991
{
992
  int functionCallResult;
993
  /* Arguments checking. */
994
  if (functionNode == NULL)
995
  {
996
    pobyso_error_message("pobyso_dirty_infnorm",
997
                        "NULL_POINTER_ARGUMENT",
998
                        "The function to compute is a NULL pointer");
999
    return(1);
1000
  }
1001
  if (mpfr_cmp(lowerBound, upperBound) > 0)
1002
  {
1003
    pobyso_error_message("pobyso_dirty_infnorm",
1004
                        "INCOHERENT_INPUT_DATA",
1005
                        "The lower bond is greater than the upper bound");
1006
    return(1);
1007
  }
1008
  /* Particular cases. */
1009
  if (mpfr_cmp(lowerBound, upperBound) == 0)
1010
  {
1011
    functionCallResult = pobyso_evaluate_faithful(infNorm,
1012
                                                  functionNode,
1013
                                                  lowerBound,
1014
                                                  precision);
1015
    return(functionCallResult);
1016
  }
1017
  if (isConstant(functionNode))
1018
  {
1019
    functionCallResult = pobyso_evaluate_faithful(infNorm,
1020
                                                  functionNode,
1021
                                                  lowerBound,
1022
                                                  precision);
1023
    if (!functionCallResult)
1024
    {
1025
      mpfr_abs(infNorm, infNorm, MPFR_RNDN);
1026
    }
1027
    return(functionCallResult);
1028
  }
1029
  uncertifiedInfnorm(infNorm,
1030
                      functionNode,
1031
                      lowerBound,
1032
                      upperBound,
1033
                      POBYSO_DEFAULT_POINTS,
1034
                      precision);
1035
  return(0);
1036
} /* End pobyso_dirty_infnorm. */
1037

1038
int
1039
pobyso_evaluate_faithful(mpfr_t faithfulEvaluation,
1040
                          node *nodeToEvaluate,
1041
                          mpfr_t argument,
1042
                          mpfr_prec_t precision)
1043
{
1044
  /* Check input arguments. */
1045
  if (nodeToEvaluate == NULL)
1046
  {
1047
    pobyso_error_message("pobyso_evaluate_faithful",
1048
                        "NULL_POINTER_ARGUMENT",
1049
                        "nodeToEvaluate is a NULL pointer");
1050
    return(1);
1051
  }
1052
  evaluateFaithful(faithfulEvaluation,
1053
                   nodeToEvaluate,
1054
                   argument,
1055
                   precision);
1056
  return(0);
1057
} /* End pobyso_evaluate_faithfull. */
1058

1059
chain*
1060
pobyso_find_zeros(node *function,
1061
                  mpfr_t *lower_bound,
1062
                  mpfr_t *upper_bound)
1063
{
1064
  mp_prec_t currentPrecision;
1065
  mpfr_t currentDiameter;
1066
  rangetype bounds;
1067

1068
  currentPrecision = getToolPrecision();
1069
  mpfr_init2(currentDiameter, currentPrecision);
1070

1071
  bounds.a = lower_bound;
1072
  bounds.b = upper_bound;
1073

1074
  if (bounds.a == NULL || bounds.b == NULL)
1075
  {
1076
    pobyso_error_message("pobyso_find_zeros",
1077
                        "MEMORY_ALLOCATION_ERROR",
1078
                        "Could not allocate one of the bounds");
1079
    return(NULL);
1080
  }
1081
  return(findZerosFunction(function,
1082
                            bounds,
1083
                            currentPrecision,
1084
                            currentDiameter));
1085
} /* End pobyso_find_zeros. */
1086

1087
void
1088
pobyso_free_chain_of_nodes(chain *theChainOfNodes)
1089
{
1090
  node *currentNode           = NULL;
1091
  chain *currentChainElement  = NULL;
1092
  chain *nextChainElement     = NULL;
1093

1094
  nextChainElement = theChainOfNodes;
1095

1096
  while(nextChainElement != NULL)
1097
  {
1098
    currentChainElement = nextChainElement;
1099
    currentNode = (node*)(currentChainElement->value);
1100
    nextChainElement = nextChainElement->next;
1101
    free_memory(currentNode);
1102
    free((void*)(currentChainElement));
1103
  }
1104
} /* End pobyso_free_chain_of_nodes. */
1105

1106
void
1107
pobyso_free_range(rangetype range)
1108
{
1109

1110
  mpfr_clear(*(range.a));
1111
  mpfr_clear(*(range.b));
1112
  free(range.a);
1113
  free(range.b);
1114
} /* End pobyso_free_range. */
1115

1116
node*
1117
pobyso_fp_minimax_canonical_monomials_base(node *function,
1118
                                      int degree,
1119
                                      chain *formats,
1120
                                      chain *points,
1121
                                      mpfr_t lowerBound,
1122
                                      mpfr_t upperBound,
1123
                                      int fpFixedArg,
1124
                                      int absRel,
1125
                                      node *constPart,
1126
                                      node *minimax)
1127
{
1128
  return(NULL);
1129
} /* End pobyso_fp_minimax_canonical_monomials_base. */
1130

1131
node*
1132
pobyso_parse_function(char *functionString,
1133
                      char *freeVariableNameString)
1134
{
1135
  if (functionString == NULL || freeVariableNameString == NULL)
1136
  {
1137
    pobyso_error_message("pobyso_parse_function",
1138
                        "NULL_POINTER_ARGUMENT",
1139
                        "One of the arguments is a NULL pointer");
1140
    return(NULL);
1141
  }
1142
  return(parseString(functionString));
1143

1144
} /* End pobyso_parse_function */
1145

1146
node*
1147
pobyso_remez_approx_canonical_monomials_base_for_error(node *functionNode,
1148
                                                  unsigned int mode,
1149
                                                  mpfr_t lowerBound,
1150
                                                  mpfr_t upperBound,
1151
                                                  mpfr_t eps)
1152
{
1153
  node *weight              = NULL;
1154
  node *bestApproxPolyNode  = NULL;
1155
  node *bestApproxHorner    = NULL;
1156
  node *errorNode           = NULL;
1157
  rangetype degreeRange;
1158
  mpfr_t quality;
1159
  mpfr_t currentError;
1160
  unsigned int degree;
1161

1162
  /* Check the parameters. */
1163
  if (functionNode == NULL)
1164
  {
1165
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
1166
                        "NULL_POINTER_ARGUMENT",
1167
                        "functionNode is a NULL pointer");
1168
    return(NULL);
1169
  }
1170
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
1171
  {
1172
    pobyso_error_message("remezApproxCanonicalMonomialsBaseForError",
1173
                        "INCOHERENT_INPUT_DATA",
1174
                        "the lower_bound >= upper_bound");
1175
    return(NULL);
1176
  }
1177
  /* Set the weight. */
1178
  if (mode == POBYSO_ABSOLUTE)
1179
  {
1180
    /* Set the weight to 1 for the ABSOLUTE_MODE. */
1181
    weight = makeConstantDouble(1.0);
1182
  }
1183
  else
1184
  {
1185
    if (mode == POBYSO_RELATIVE)
1186
    {
1187
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1188
                          "NOT_IMPLEMENTED",
1189
                          "the search for relative error is not implemented yet");
1190
      return(NULL);
1191
    }
1192
    else
1193
    {
1194
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1195
                          "INCOHERENT_INPUT_DATA",
1196
                          "the mode is node of POBYSO_ABOLUTE or POBYSO_RELATIVE");
1197
      return(NULL);
1198
    }
1199
  }
1200
  //fprintf(stderr, "\n\n\n******* I'm here! ********\n\n\n");
1201
  degreeRange = guessDegree(functionNode,
1202
                            weight,
1203
                            lowerBound,
1204
                            upperBound,
1205
                            eps,
1206
                            POBYSO_GUESS_DEGREE_BOUND);
1207
  degree = mpfr_get_ui(*(degreeRange.a), MPFR_RNDN);
1208
  //fprintf(stderr, "\n\n\n******* I'm back! ********\n\n\n");
1209
  fprintf(stderr, "Guessed degree: ");
1210
  mpfr_out_str(stderr, 10, 17, *(degreeRange.a), MPFR_RNDN);
1211
  fprintf(stderr, " - as int: %u\n", degree);
1212
  /* Reduce the degree by 1 in the foolish hope it could work. */
1213
  if (degree > 0) degree--;
1214
  /* Both elements of degreeRange where "inited" within guessDegree. */
1215
  mpfr_clear(*(degreeRange.a));
1216
  mpfr_clear(*(degreeRange.b));
1217
  free(degreeRange.a);
1218
  free(degreeRange.b);
1219
  /* Mimic the default behavior of interactive Sollya. */
1220
  mpfr_init(quality);
1221
  mpfr_set_d(quality, 1e-5, MPFR_RNDN);
1222
  mpfr_init2(currentError, getToolPrecision());
1223
  mpfr_set_inf(currentError,1);
1224

1225
  /* Try to refine the initial guess: loop with increasing degrees until
1226
   * we find a satisfactory one. */
1227
  while(mpfr_cmp(currentError, eps) > 0)
1228
  {
1229
    /* Get rid of the previous polynomial, if any. */
1230
    if (bestApproxPolyNode != NULL)
1231
    {
1232
      free_memory(bestApproxPolyNode);
1233
    }
1234
    fprintf(stderr, "Degree: %u\n", degree);
1235
    fprintf(stderr, "Calling pobyso_remez_canonical_monomials_base...\n");
1236
    /* Try to find a polynomial with the guessed degree. */
1237
    bestApproxPolyNode = pobyso_remez_canonical_monomials_base(functionNode,
1238
                                                            weight,
1239
                                                            degree,
1240
                                                            lowerBound,
1241
                                                            upperBound,
1242
                                                            quality);
1243

1244
    if (bestApproxPolyNode == NULL)
1245
    {
1246
      pobyso_error_message("computeRemezApproxCanonicalMonomialsBaseForError",
1247
                          "INTERNAL_ERROR",
1248
                          "could not compute the bestApproxPolyNode");
1249
      mpfr_clear(currentError);
1250
      mpfr_clear(quality);
1251
      return(NULL);
1252
    }
1253

1254
    setDisplayMode(DISPLAY_MODE_DECIMAL);
1255
    fprintTree(stderr, bestApproxPolyNode);
1256
    fprintf(stderr, "\n\n");
1257

1258
    errorNode = makeSub(copyTree(functionNode), copyTree(bestApproxPolyNode));
1259
    /* Check the error with the computed polynomial. */
1260
    uncertifiedInfnorm(currentError,
1261
                        errorNode,
1262
                        lowerBound,
1263
                        upperBound,
1264
                        POBYSO_INF_NORM_NUM_POINTS,
1265
                        getToolPrecision());
1266
    fprintf(stderr, "Inf norm: ");
1267
    mpfr_out_str(stderr, 10, 17, currentError, MPFR_RNDN);
1268
    fprintf(stderr, "\n\n");
1269
    /* Free the errorNode but not the bestApproxPolyNode (we need it if
1270
     * we exit the loop at the next iteration). */
1271
    free_memory(errorNode);
1272
    degree++;
1273
  }
1274
  /* Use an intermediate variable, since horner() creates a new node
1275
   * and does not reorder the argument "in place". This allows for the memory
1276
   * reclaim of bestApproxHorner.
1277
   */
1278
  bestApproxHorner = horner(bestApproxPolyNode);
1279
  free_memory(bestApproxPolyNode);
1280
  mpfr_clear(currentError);
1281
  mpfr_clear(quality);
1282
  free_memory(weight);
1283
  return(bestApproxHorner);
1284
} /* End pobyso_remez_approx_canonical_monomials_base_for_error */
1285

1286
node*
1287
pobyso_remez_canonical_monomials_base(node *function,
1288
                                     node *weight,
1289
                                     unsigned int degree,
1290
                                     mpfr_t lowerBound,
1291
                                     mpfr_t upperBound,
1292
                                     mpfr_t quality)
1293
{
1294
  node  *bestApproxPoly = NULL;
1295
  chain *monomials      = NULL;
1296
  chain *curMonomial    = NULL;
1297

1298
  mpfr_t satisfying_error;
1299
  mpfr_t target_error;
1300

1301
  /* Argument checking */
1302
  /* Function tree. */
1303
  if (function == NULL)
1304
  {
1305
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1306
                        "NULL_POINTER_ARGUMENT",
1307
                        "the \"function\" argument is a NULL pointer");
1308
    return(NULL);
1309
  }
1310
  if (weight == NULL)
1311
  {
1312
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1313
                        "NULL_POINTER_ARGUMENT",
1314
                        "the \"weight\" argument is a NULL pointer");
1315
    return(NULL);
1316
  }
1317
  /* Check the bounds. */
1318
  if (mpfr_cmp(lowerBound, upperBound) >= 0)
1319
  {
1320
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1321
                        "INCOHERENT_IMPUT_DATA",
1322
                        "the lower_bound >= upper_bound");
1323
    return(NULL);
1324
  }
1325
  /* The quality must be a non null positive number. */
1326
  if (mpfr_sgn(quality) <= 0)
1327
  {
1328
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1329
                        "INCOHERENT_INPUT_DATA",
1330
                        "the quality <= 0");
1331
  }
1332
  /* End argument checking. */
1333
  /* Create the monomials nodes chains. */
1334
  monomials = pobyso_create_canonical_monomials_base(degree);
1335
  fprintf(stderr, "monomials chain length = %d\n", lengthChain(monomials));
1336
  if (monomials == NULL || (lengthChain(monomials) != degree + 1))
1337
  {
1338
    pobyso_error_message("pobyso_remez_canonical_monomials_base",
1339
                        "CHAIN_CREATION_ERROR",
1340
                        "could not create the monomials chain");
1341
    return(NULL);
1342
  }
1343
  curMonomial = monomials;
1344

1345
  while (curMonomial != NULL)
1346
  {
1347
    fprintf(stderr, "monomial tree: ");
1348
    //mpfr_out_str(stderr, 10, 17, *((mpfr_t*)((node*)(curMonomial->value))->value), MPFR_RNDN);
1349
    fprintTree(stderr, (node*)(curMonomial->value));
1350
    fprintf(stderr, "\n");
1351
    curMonomial = curMonomial->next;
1352
  }
1353

1354
  /* Deal with NULL weight. */
1355
  if (weight == NULL)
1356
  {
1357
    weight = makeConstantDouble(1.0);
1358
  }
1359
  /* Compute the best polynomial with the required quality.
1360
     The behavior is as if satisfying error and target_error had
1361
     not been used.*/
1362
  mpfr_init(satisfying_error);
1363
  mpfr_init(target_error);
1364
  mpfr_set_str(satisfying_error, "0", 10, MPFR_RNDN);
1365
  mpfr_set_inf(target_error, 1);
1366

1367

1368
  fprintf(stderr, "satisfying_error: ");
1369
  mpfr_out_str(stderr, 10, 17, satisfying_error, MPFR_RNDN);
1370
  fprintf(stderr, ".\n");
1371
  fprintf(stderr, "target_error: ");
1372
  mpfr_out_str(stderr, 10, 17, target_error,MPFR_RNDN);
1373
  fprintf(stderr, ".\n");
1374

1375
  fprintf(stderr,
1376
          "current precision: %li\n", getToolPrecision());
1377
  /* Call the Sollya function. */
1378
  bestApproxPoly = remez(function,
1379
                          weight,
1380
                          monomials,
1381
                          lowerBound,
1382
                          upperBound,
1383
                          quality,
1384
                          satisfying_error,
1385
                          target_error,
1386
                          getToolPrecision());
1387

1388
  mpfr_clear(satisfying_error);
1389
  mpfr_clear(target_error);
1390
  pobyso_free_chain_of_nodes(monomials);
1391

1392
  return(bestApproxPoly);
1393
} /* End pobyso_remez_canonical_monomials_base. */
1394

1395
#endif
1396

    
1397
void
1398
pobyso_error_message(char *functionName, char *messageName, char* message)
1399
{
1400
  fprintf(stderr, "?%s: %s.\n%s.\n", functionName, messageName, message);
1401
} /* End pobyso_error_message */