polylib 5.22.8
eval_ehrhart.c
Go to the documentation of this file.
1/************************************************/
2/* eval_ehrhart.c */
3/* functions to evaluate an Ehrhart polynomial. */
4/* written by Emmanuel Jeannot (c) 1997. */
5/* Emmanuel.Jeannot@ens-lyon.fr */
6/* http://www.ens-lyon.fr/~ejeannot */
7/* */
8/* modified 1998, 2000, Vincent Loechner */
9/* (ArithmetiqueLib, Param_Names) */
10/************************************************/
11
12#include <assert.h>
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16
17#include <polylib/polylib.h>
18
19/* #define EVAL_EHRHART_DEBUG */
20
21/********************************************************/
22/* function in domain */
23/* check if the parameters in list_args */
24/* verifies the constraints of Domain P */
25/********************************************************/
26int in_domain(Polyhedron *P, Value *list_args) {
27
28 int col, row;
29 Value v; /* value of the constraint of a row when
30 parameters are instanciated*/
31
32 if (!P)
33 return (0);
34
36
37 value_init(v);
38
39 /* P->Constraint constraint matrice of polyhedron P */
40 for (row = 0; row < P->NbConstraints; row++) {
41 value_assign(v, P->Constraint[row][P->Dimension + 1]); /*constant part*/
42 for (col = 1; col < P->Dimension + 1; col++) {
43 value_addmul(v, P->Constraint[row][col], list_args[col - 1]);
44 }
45 if (value_notzero_p(P->Constraint[row][0])) {
46
47 /*if v is not >=0 then this constraint is not respected */
48 if (value_neg_p(v)) {
49 value_clear(v);
50 return (in_domain(P->next, list_args));
51 }
52 } else {
53
54 /*if v is not = 0 then this constraint is not respected */
55 if (value_notzero_p(v)) {
56 value_clear(v);
57 return (in_domain(P->next, list_args));
58 }
59 }
60 }
61
62 /* if not return before this point => all the constraints are respected */
63 value_clear(v);
64 return 1;
65} /* in_domain */
66
67/****************************************************/
68/* function compute enode */
69/* compute the value of enode p with parameters */
70/* list "list_args */
71/* compute the polynomial or the periodic */
72/****************************************************/
73
74static double compute_enode(enode *p, Value *list_args) {
75
76 int i;
77 Value m, param;
78 double res = 0.0;
79
80 if (!p)
81 return (0.);
82
84 value_init(param);
85
86 if (p->type == polynomial) {
87 if (p->size > 1)
88 value_assign(param, list_args[p->pos - 1]);
89
90 /* Compute the polynomial using Horner's rule */
91 for (i = p->size - 1; i > 0; i--) {
92 res += compute_evalue(&p->arr[i], list_args);
93 res *= VALUE_TO_DOUBLE(param);
94 }
95 res += compute_evalue(&p->arr[0], list_args);
96 } else if (p->type == periodic) {
97 value_assign(m, list_args[p->pos - 1]);
98
99 /* Choose the right element of the periodic */
100 value_set_si(param, p->size);
101 value_pmodulus(m, m, param);
102 res = compute_evalue(&p->arr[VALUE_TO_INT(m)], list_args);
103 }
104 value_clear(m);
105 value_clear(param);
106 return res;
107} /* compute_enode */
108
109/*************************************************/
110/* return the value of Ehrhart Polynomial */
111/* It returns a double, because since it is */
112/* a recursive function, some intermediate value */
113/* might not be integral */
114/*************************************************/
115
116double compute_evalue(evalue *e, Value *list_args) {
117
118 double res;
119
120 if (value_notzero_p(e->d)) {
121 if (value_notone_p(e->d))
122 res = VALUE_TO_DOUBLE(e->x.n) / VALUE_TO_DOUBLE(e->d);
123 else
124 res = VALUE_TO_DOUBLE(e->x.n);
125 } else
126 res = compute_enode(e->x.p, list_args);
127 return res;
128} /* compute_evalue */
129
130/****************************************************/
131/* function compute_poly : */
132/* Check for the good validity domain */
133/* return the number of point in the Polyhedron */
134/* in allocated memory */
135/* Using the Ehrhart pseudo-polynomial */
136/****************************************************/
137Value *compute_poly(Enumeration *en, Value *list_args) {
138
139 Value *tmp;
140 /* double d; int i; */
141
142 tmp = (Value *)malloc(sizeof(Value));
143 assert(tmp != NULL);
144 value_init(*tmp);
145 value_set_si(*tmp, 0);
146
147 if (!en)
148 return (tmp); /* no ehrhart polynomial */
149 if (en->ValidityDomain) {
150 if (!en->ValidityDomain->Dimension) { /* no parameters */
151 value_set_double(*tmp, compute_evalue(&en->EP, list_args) + .25);
152 return (tmp);
153 }
154 } else
155 return (tmp); /* no Validity Domain */
156 while (en) {
157 if (in_domain(en->ValidityDomain, list_args)) {
158
159#ifdef EVAL_EHRHART_DEBUG
160 Print_Domain(stdout, en->ValidityDomain, NULL);
161 print_evalue(stdout, &en->EP, NULL);
162#endif
163
164 /* d = compute_evalue(&en->EP,list_args);
165 i = d;
166 printf("(double)%lf = %d\n", d, i ); */
167 value_set_double(*tmp, compute_evalue(&en->EP, list_args) + .25);
168 return (tmp);
169 } else
170 en = en->next;
171 }
172 value_set_si(*tmp, 0);
173 return (tmp); /* no compatible domain with the arguments */
174} /* compute_poly */
#define value_set_double(val, d)
Definition: arithmetique.h:484
#define value_notzero_p(val)
Definition: arithmetique.h:576
#define value_notone_p(val)
Definition: arithmetique.h:578
#define value_pmodulus(ref, val1, val2)
Definition: arithmetique.h:551
#define value_assign(v1, v2)
Definition: arithmetique.h:482
#define value_set_si(val, i)
Definition: arithmetique.h:483
#define value_addmul(ref, val1, val2)
Definition: arithmetique.h:539
#define value_clear(val)
Definition: arithmetique.h:485
#define value_neg_p(val)
Definition: arithmetique.h:572
#define value_init(val)
Definition: arithmetique.h:481
#define assert(ex)
Definition: assert.h:16
void print_evalue(FILE *DST, evalue *e, const char **pname)
Definition: ehrhart.c:169
Value * compute_poly(Enumeration *en, Value *list_args)
Definition: eval_ehrhart.c:137
double compute_evalue(evalue *e, Value *list_args)
Definition: eval_ehrhart.c:116
int in_domain(Polyhedron *P, Value *list_args)
Definition: eval_ehrhart.c:26
static double compute_enode(enode *p, Value *list_args)
Definition: eval_ehrhart.c:74
#define POL_ENSURE_INEQUALITIES(P)
Definition: polyhedron.h:5
static int m
Definition: polyparam.c:274
void Print_Domain(FILE *DST, Polyhedron *D, const char **pname)
Definition: polyparam.c:1680
Definition: types.h:182
int pos
Definition: types.h:185
evalue arr[1]
Definition: types.h:186
enode_type type
Definition: types.h:183
int size
Definition: types.h:184
Polyhedron * ValidityDomain
Definition: types.h:191
evalue EP
Definition: types.h:192
struct _enumeration * next
Definition: types.h:193
Definition: types.h:173
Value d
Definition: types.h:174
unsigned Dimension
Definition: types.h:94
unsigned NbConstraints
Definition: types.h:94
struct polyhedron * next
Definition: types.h:99
Value ** Constraint
Definition: types.h:95
@ periodic
Definition: types.h:165
@ polynomial
Definition: types.h:165