6 #include <ginac/ginac.h>
10 #include <bernstein/bernstein.h>
11 #include <bernstein/maximize.h>
12 #include <bernstein/piecewise_lst.h>
17 using namespace GiNaC
;
18 using namespace bernstein
;
20 static ex
readPolynomial(const exvector
& vars
, const exvector
& params
);
21 static void printCoefficients(lst coeffs
);
22 static int printMaxMinCoefficient(Polyhedron
*VD
, lst coeffs
, const exvector
& Params
);
24 static char *readLine(void)
26 static char buffer
[1024];
30 s
= fgets(buffer
, 1024, stdin
);
31 while (s
&& (s
[0] == '#' || s
[0] == '\n'));
36 static void readExpected(const exvector
& vars
, const exvector
& params
,
37 piecewise_lst
**exp_all
,
38 piecewise_lst
**exp_min
,
39 piecewise_lst
**exp_max
)
43 piecewise_lst
*pl_all
, *pl_min
, *pl_max
;
45 pl_all
= new piecewise_lst(params
);
46 pl_min
= new piecewise_lst(params
);
47 pl_max
= new piecewise_lst(params
);
54 for (int i
= 0 ; i
< n
; ++i
) {
57 Matrix
*M
= Matrix_Read();
59 D
= Constraints2Polyhedron(M
, MAXRAYS
);
62 all
= readPolynomial(vars
, params
);
63 min
= readPolynomial(vars
, params
);
64 max
= readPolynomial(vars
, params
);
65 assert(is_a
<lst
>(all
));
66 assert(is_a
<lst
>(min
));
67 assert(is_a
<lst
>(max
));
68 pl_all
->add_guarded_lst(Polyhedron_Copy(D
), ex_to
<lst
>(all
));
69 pl_min
->add_guarded_lst(Polyhedron_Copy(D
), ex_to
<lst
>(min
));
70 pl_max
->add_guarded_lst(D
, ex_to
<lst
>(max
));
78 int main(int argc
, char *argv
[])
81 Polyhedron
*A
, *B
; // initial matrices
82 const char **param_name
; // name of the parameters
83 exvector params
, vars
;
85 piecewise_lst
*exp_all
, *exp_min
, *exp_max
;
86 piecewise_lst
*got_all
, *got_min
, *got_max
;
91 unsigned int nb_param
, nb_var
;
96 while ((c
= getopt(argc
, argv
, "T")) != -1) {
104 printf("\n===============================================\n");
107 A
= Constraints2Polyhedron(a
,200);
110 B
= Constraints2Polyhedron(b
,200);
112 /* Set the number of parameters */
113 nb_param
= B
->Dimension
;
114 nb_var
= A
->Dimension
- B
->Dimension
;
116 /* Read the name of the parameters */
117 param_name
= Read_ParamNames(stdin
, nb_var
+nb_param
);
118 vars
= constructParameterVector(param_name
, nb_var
);
119 params
= constructParameterVector(param_name
+nb_var
, nb_param
);
121 polynomial
= readPolynomial(vars
, params
);
124 readExpected(vars
, params
, &exp_all
, &exp_min
, &exp_max
);
125 got_all
= new piecewise_lst(params
);
126 got_min
= new piecewise_lst(params
);
127 got_max
= new piecewise_lst(params
);
133 /* Find the parametrized domains */
134 PP
= Polyhedron2Param_Domain(A
,B
,MAXRAYS
);
135 for(Q
=PP
->D
;Q
;Q
=Q
->next
) {
139 printf("\nDomain: \n");
140 VD
= DomainSimplify(Q
->Domain
, B
, MAXRAYS
);
141 Print_Domain(stdout
, VD
, param_name
+nb_var
);
142 matrix VM
= domainVertices(PP
, Q
, params
);
143 coeffs
= bernsteinExpansion(VM
, polynomial
, vars
, params
);
144 printCoefficients(coeffs
);
145 printMaxMinCoefficient(Q
->Domain
, coeffs
, params
);
147 printf("\n\n===============================================\n");
152 got_all
->add_guarded_lst(Polyhedron_Copy(Q
->Domain
), coeffs
);
153 got_min
->add_guarded_lst(Polyhedron_Copy(Q
->Domain
),
154 minimize(Q
->Domain
, coeffs
, params
));
155 got_max
->add_guarded_lst(Polyhedron_Copy(Q
->Domain
),
156 maximize(Q
->Domain
, coeffs
, params
));
161 Param_Polyhedron_Free(PP
);
165 if (!got_all
->is_equal(*exp_all
)) {
166 cerr
<< "expected:" << endl
;
167 cerr
<< *exp_all
<< endl
;
168 cerr
<< "got:" << endl
;
169 cerr
<< *got_all
<< endl
;
172 if (!got_min
->is_equal(*exp_min
)) {
173 cerr
<< "expected:" << endl
;
174 cerr
<< *exp_min
<< endl
;
175 cerr
<< "got:" << endl
;
176 cerr
<< *got_min
<< endl
;
179 if (!got_max
->is_equal(*exp_max
)) {
180 cerr
<< "expected:" << endl
;
181 cerr
<< *exp_max
<< endl
;
182 cerr
<< "got:" << endl
;
183 cerr
<< *got_max
<< endl
;
199 void printCoefficients(lst coeffs
)
201 cout
<< "-----------------------------------------------" << endl
;
202 cout
<< "Coefficients: " << endl
<< endl
;
204 for (lst::const_iterator i
= coeffs
.begin(); i
!= coeffs
.end(); ++i
)
205 cout
<< "\t" << *i
<< endl
;
209 /* Reads the polynomial matrix, converts it to long long precision and calls ginac functions */
210 ex
readPolynomial(const exvector
& vars
, const exvector
& params
)
216 for (int i
= 0; i
< vars
.size(); ++i
)
217 allvars
.append(vars
[i
]);
218 for (int i
= 0; i
< params
.size(); ++i
)
219 allvars
.append(params
[i
]);
226 p
= ex(string(s
), allvars
);
227 } catch (exception
&p
) {
228 cerr
<< p
.what() << endl
;
236 int printMaxMinCoefficient(Polyhedron
*VD
, lst coeffs
, const exvector
& Params
)
238 cout
<< "\tMinimum coefficient(s): " << minimize(VD
, coeffs
, Params
) << endl
;
239 cout
<< "\tMaximum coefficient(s): " << maximize(VD
, coeffs
, Params
) << endl
;