polylib 7.01
homogenization.c
Go to the documentation of this file.
1/** homogenization.c
2 copyright 2004-2005 Bavo Nootaert
3**/
4
5#include <assert.h>
7#include <polylib/polylib.h>
8#include <stdlib.h>
9#include <string.h>
10
13
15 Matrix M, *M2;
16 /* Pretend P is a Matrix for a second */
17 M.NbRows = P->NbConstraints;
18 M.NbColumns = P->Dimension + 2;
19 M.p_Init = P->p_Init;
20 M.p = P->Constraint;
21 M2 = AddANullColumn(&M);
23 Matrix_Free(M2);
24 return P;
25}
26
27/** dehomogenize an evalue. The last parameter (nb_param) is replaced by 1.
28 This function is mutually recursive with dehomogenize_enode.
29**/
30void dehomogenize_evalue(evalue *ep, int nb_param) {
31 evalue *w;
32
33 /** cannot dehomogenize rationals **/
34 if (value_zero_p(ep->d)) {
35
36 /** we need to replace the last parameter **/
37 if (ep->x.p->pos == nb_param) {
38 if (ep->x.p->type == periodic && ep->x.p->size > 1) {
39 w = dehomogenize_periodic(ep->x.p);
40 } else {
41 w = dehomogenize_polynomial(ep->x.p);
42 }
44 memcpy(ep, w, sizeof(evalue));
45 free(w);
46 } else {
47 /** Not the last parameter. Recurse **/
48 dehomogenize_enode(ep->x.p, nb_param);
49 }
50 }
51}
52
53/** dehomogenize all evalues in an enode.
54 This function is mutually recursive with dehomogenize_evalue.
55**/
56void dehomogenize_enode(enode *p, int nb_param) {
57 int i;
58 for (i = 0; i < p->size; i++) {
59 dehomogenize_evalue(&p->arr[i], nb_param);
60 }
61}
62
63/** return the 1st element of an enode representing a periodic **/
65 evalue *w;
66 assert(en->type == periodic);
67 assert(en->size > 1);
68 assert(value_notzero_p(en->arr[1].d));
69 w = (evalue *)malloc(sizeof(evalue));
70 value_init(w->d);
71 value_init(w->x.n);
72 value_assign(w->d, en->arr[1].d);
73 value_assign(w->x.n, en->arr[1].x.n);
74 return w;
75}
76
77/** dehomogenize a polynomial. Assume the enode contains a polynomial in
78 one variable, the homogenous parameter.
79 Returns an new evalue, representing a rational.
80 **/
82 evalue *enn;
83 evalue *ev;
84 int i;
85 Value num, den, gcd, f1, f2;
86 assert(en->type == polynomial);
87 value_init(num);
88 value_init(den);
89 value_init(gcd);
90 value_init(f1);
91 value_init(f2);
92 value_set_si(den, 1);
93
94 /** enumerate over all coefficients (which are either periodic or rational,
95 but not polynomial) **/
96 for (i = 0; i < en->size; i++) {
97 if (value_zero_p(en->arr[i].d)) {
98 if (en->arr[i].x.p->size > 1)
99 ev = &en->arr[i].x.p->arr[1];
100 else
101 ev = &en->arr[i].x.p->arr[0];
102 } else {
103 ev = &en->arr[i];
104 }
105 /** add ev (fraction) to num/den **/
106 value_multiply(f1, den, ev->x.n);
107 value_multiply(f2, num, ev->d);
108 value_addto(num, f1, f2);
109 value_multiply(den, den, ev->d);
110 }
111
112 /** simplify num/den **/
113 value_gcd(gcd, num, den);
114 value_divexact(num, num, gcd);
115 value_divexact(den, den, gcd);
116
117 /** create new evalue representing num/den**/
118 enn = (evalue *)malloc(sizeof(evalue));
119 value_init(enn->d);
120 value_init(enn->x.n);
121 value_assign(enn->d, den);
122 value_assign(enn->x.n, num);
123
124 /** cleanup **/
125 value_clear(gcd);
126 value_clear(f1);
127 value_clear(f2);
128 value_clear(num);
129 value_clear(den);
130
131 return enn;
132}
133
134/** dehomogenize a polyhedron. Assume the polyhedron p is homogenous.
135 Returns a new polyhedron.
136**/
138 Matrix *constr, *constrh;
139 Polyhedron *ph;
140 int i;
141 constr = Polyhedron2Constraints(p);
142 constrh = Matrix_Alloc(constr->NbRows, constr->NbColumns - 1);
143 for (i = 0; i < constr->NbRows; i++) {
144 Vector_Copy(constr->p[i], constrh->p[i], constr->NbColumns - 1);
145 }
146 ph = Constraints2Polyhedron(constrh, maxRays);
147 Matrix_Free(constr);
148 Matrix_Free(constrh);
149 return ph;
150}
151
152/** dehomogenize an enumeration. Replaces each validity domain and
153 Ehrhart polynomial in the Enumeration en with the dehomogenized form.
154 **/
155void dehomogenize_enumeration(Enumeration *en, int nb_params, int maxRays) {
156 Enumeration *en2;
157 Polyhedron *vd;
158 for (en2 = en; en2; en2 = en2->next) {
161 en2->ValidityDomain = vd;
162 dehomogenize_evalue(&en2->EP, nb_params);
163 }
164}
Matrix * AddANullColumn(Matrix *M)
Definition: Matop.c:238
#define value_notzero_p(val)
Definition: arithmetique.h:579
#define value_divexact(ref, val1, val2)
Definition: arithmetique.h:551
#define value_gcd(ref, val1, val2)
Definition: arithmetique.h:559
#define value_zero_p(val)
Definition: arithmetique.h:578
#define value_assign(v1, v2)
Definition: arithmetique.h:485
int Value
Definition: arithmetique.h:294
#define value_set_si(val, i)
Definition: arithmetique.h:486
#define value_clear(val)
Definition: arithmetique.h:488
#define value_multiply(ref, val1, val2)
Definition: arithmetique.h:546
#define value_addto(ref, val1, val2)
Definition: arithmetique.h:540
#define value_init(val)
Definition: arithmetique.h:484
#define assert(ex)
Definition: assert.h:16
void free_evalue_refs(evalue *e)
releases all memory referenced by e.
Definition: ehrhart.c:115
void dehomogenize_enumeration(Enumeration *en, int nb_params, int maxRays)
dehomogenize an enumeration.
Polyhedron * homogenize(Polyhedron *P, unsigned MAXRAYS)
homogenization.h – Bavo Nootaert
void dehomogenize_evalue(evalue *ep, int nb_param)
dehomogenize an evalue.
static evalue * dehomogenize_polynomial(enode *en)
dehomogenize a polynomial.
Polyhedron * dehomogenize_polyhedron(Polyhedron *p, int maxRays)
dehomogenize a polyhedron.
static evalue * dehomogenize_periodic(enode *en)
homogenization.c copyright 2004-2005 Bavo Nootaert
void dehomogenize_enode(enode *p, int nb_param)
dehomogenize all evalues in an enode.
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
Definition: matrix.c:24
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:69
void Polyhedron_Free(Polyhedron *Pol)
Definition: polyhedron.c:1621
Matrix * Polyhedron2Constraints(Polyhedron *Pol)
Definition: polyhedron.c:2067
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1912
Definition: types.h:202
evalue arr[1]
Definition: types.h:206
enode_type type
Definition: types.h:203
int size
Definition: types.h:204
Polyhedron * ValidityDomain
Definition: types.h:210
evalue EP
Definition: types.h:211
struct _enumeration * next
Definition: types.h:212
Definition: types.h:193
Value d
Definition: types.h:194
Definition: types.h:88
unsigned NbRows
Definition: types.h:89
Value ** p
Definition: types.h:90
unsigned NbColumns
Definition: types.h:89
Value * p_Init
Definition: types.h:91
unsigned Dimension
Definition: types.h:110
unsigned NbConstraints
Definition: types.h:110
Value * p_Init
Definition: types.h:113
Value ** Constraint
Definition: types.h:111
#define maxRays
@ periodic
Definition: types.h:185
@ polynomial
Definition: types.h:185
void Vector_Copy(Value *p1, Value *p2, unsigned length)
Definition: vector.c:276
#define MAXRAYS
Definition: verif_ehrhart.c:20