polylib 5.22.8
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 evalue *temp;
58 int i;
59 for (i = 0; i < p->size; i++) {
60 dehomogenize_evalue(&p->arr[i], nb_param);
61 }
62}
63
64/** return the 1st element of an enode representing a periodic **/
66 evalue *w;
67 assert(en->type == periodic);
68 assert(en->size > 1);
69 assert(value_notzero_p(en->arr[1].d));
70 w = (evalue *)malloc(sizeof(evalue));
71 value_init(w->d);
72 value_init(w->x.n);
73 value_assign(w->d, en->arr[1].d);
74 value_assign(w->x.n, en->arr[1].x.n);
75 return w;
76}
77
78/** dehomogenize a polynomial. Assume the enode contains a polynomial in
79 one variable, the homogenous parameter.
80 Returns an new evalue, representing a rational.
81 **/
83 evalue *enn;
84 evalue *ev;
85 int i;
86 double som;
87 Value num, den, gcd, f1, f2;
88 assert(en->type == polynomial);
89 som = 0;
90 value_init(num);
91 value_init(den);
92 value_init(gcd);
93 value_init(f1);
94 value_init(f2);
95 value_set_si(den, 1);
96
97 /** enumerate over all coefficients (which are either periodic or rational,
98 but not polynomial) **/
99 for (i = 0; i < en->size; i++) {
100 if (value_zero_p(en->arr[i].d)) {
101 if (en->arr[i].x.p->size > 1)
102 ev = &en->arr[i].x.p->arr[1];
103 else
104 ev = &en->arr[i].x.p->arr[0];
105 } else {
106 ev = &en->arr[i];
107 }
108 /** add ev (fraction) to num/den **/
109 value_multiply(f1, den, ev->x.n);
110 value_multiply(f2, num, ev->d);
111 value_addto(num, f1, f2);
112 value_multiply(den, den, ev->d);
113 }
114
115 /** simplify num/den **/
116 value_gcd(gcd, num, den);
117 value_divexact(num, num, gcd);
118 value_divexact(den, den, gcd);
119
120 /** create new evalue representing num/den**/
121 enn = (evalue *)malloc(sizeof(evalue));
122 value_init(enn->d);
123 value_init(enn->x.n);
124 value_assign(enn->d, den);
125 value_assign(enn->x.n, num);
126
127 /** cleanup **/
128 value_clear(gcd);
129 value_clear(f1);
130 value_clear(f2);
131 value_clear(num);
132 value_clear(den);
133
134 return enn;
135}
136
137/** dehomogenize a polyhedron. Assume the polyhedron p is homogenous.
138 Returns a new polyhedron.
139**/
141 Matrix *constr, *constrh;
142 Polyhedron *ph;
143 int i;
144 constr = Polyhedron2Constraints(p);
145 constrh = Matrix_Alloc(constr->NbRows, constr->NbColumns - 1);
146 for (i = 0; i < constr->NbRows; i++) {
147 Vector_Copy(constr->p[i], constrh->p[i], constr->NbColumns - 1);
148 }
149 ph = Constraints2Polyhedron(constrh, maxRays);
150 Matrix_Free(constr);
151 Matrix_Free(constrh);
152 return ph;
153}
154
155/** dehomogenize an enumeration. Replaces each validity domain and
156 Ehrhart polynomial in the Enumeration en with the dehomogenized form.
157 **/
158void dehomogenize_enumeration(Enumeration *en, int nb_params, int maxRays) {
159 Enumeration *en2;
160 Polyhedron *vd;
161 for (en2 = en; en2; en2 = en2->next) {
164 en2->ValidityDomain = vd;
165 dehomogenize_evalue(&en2->EP, nb_params);
166 }
167}
Matrix * AddANullColumn(Matrix *M)
Definition: Matop.c:268
#define value_notzero_p(val)
Definition: arithmetique.h:576
#define value_divexact(ref, val1, val2)
Definition: arithmetique.h:548
#define value_gcd(ref, val1, val2)
Definition: arithmetique.h:556
#define value_zero_p(val)
Definition: arithmetique.h:575
#define value_assign(v1, v2)
Definition: arithmetique.h:482
#define value_set_si(val, i)
Definition: arithmetique.h:483
#define value_clear(val)
Definition: arithmetique.h:485
#define value_multiply(ref, val1, val2)
Definition: arithmetique.h:543
#define value_addto(ref, val1, val2)
Definition: arithmetique.h:537
#define value_init(val)
Definition: arithmetique.h:481
#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:71
void Polyhedron_Free(Polyhedron *Pol)
Definition: polyhedron.c:1614
Matrix * Polyhedron2Constraints(Polyhedron *Pol)
Definition: polyhedron.c:2060
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1905
Definition: types.h:182
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
Definition: types.h:75
unsigned NbRows
Definition: types.h:76
Value ** p
Definition: types.h:77
unsigned NbColumns
Definition: types.h:76
Value * p_Init
Definition: types.h:78
unsigned Dimension
Definition: types.h:94
unsigned NbConstraints
Definition: types.h:94
Value * p_Init
Definition: types.h:97
Value ** Constraint
Definition: types.h:95
#define maxRays
@ periodic
Definition: types.h:165
@ polynomial
Definition: types.h:165
void Vector_Copy(Value *p1, Value *p2, unsigned length)
Definition: vector.c:256
#define MAXRAYS
Definition: verif_ehrhart.c:20