2 #include "conversion.h"
3 #include "evalue_convert.h"
5 #include "lattice_point.h"
14 static struct argp_option argp_options
[] = {
15 { "convert", 'c', 0, 0, "convert fractionals to periodics" },
16 { "combine", 'C', 0, 0 },
17 { "floor", 'f', 0, 0, "convert fractionals to floorings" },
18 { "list", 'l', 0, 0 },
19 { "latex", 'L', 0, 0 },
20 { "range-reduction", 'R', 0, 0 },
24 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
26 struct convert_options
*options
= (struct convert_options
*)state
->input
;
58 return ARGP_ERR_UNKNOWN
;
63 struct argp convert_argp
= {
64 argp_options
, parse_opt
, 0, 0
67 static int type_offset(enode
*p
)
69 return p
->type
== fractional
? 1 :
70 p
->type
== flooring
? 1 : 0;
73 static Lattice
*extract_lattice(evalue
*e
, int nparam
)
79 /* For some mysterious reason, SolveDiophantine expects an extra
80 * [0 0 0 1] row in its input matrix.
82 Matrix
*M
= Matrix_Alloc(2, nparam
+1+1);
83 value_set_si(M
->p
[1][nparam
+1], 1);
84 evalue_extract_affine(e
, M
->p
[0], M
->p
[0]+nparam
+1, M
->p
[0]+nparam
);
86 value_set_si(M
->p
[0][nparam
+1], 0);
87 SolveDiophantine(M
, &U
, &X
);
90 L
= Matrix_Alloc(nparam
+1, nparam
+1);
91 for (i
= 0; i
< nparam
; ++i
)
92 Vector_Copy(U
->p
[i
], L
->p
[i
], nparam
);
93 value_set_si(L
->p
[nparam
][nparam
], 1);
98 /* Returns a lattice such that the quasi-polynomial e can be represented
99 * by a list of polynomials, one for each point in the fundamental
100 * parallelepiped of the lattice.
101 * If e is a polynomial, then this function returns NULL.
103 static Lattice
*extract_common_lattice(evalue
*e
, Lattice
*L
, int nparam
)
107 if (value_notzero_p(e
->d
))
110 assert(e
->x
.p
->type
!= partition
);
112 if (e
->x
.p
->type
== fractional
) {
113 Lattice
*L2
= extract_lattice(&e
->x
.p
->arr
[0], nparam
);
117 Lattice
*L3
= LatticeIntersection(L
, L2
);
124 offset
= type_offset(e
->x
.p
);
125 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
126 L
= extract_common_lattice(&e
->x
.p
->arr
[i
], L
, nparam
);
130 /* Construct an evalue dst from src corresponding to the coset represented
131 * by coset, a vector of size number of parameters plus one.
132 * The final value in this vector should be 1.
134 static void evalue_coset(const evalue
*src
, const Vector
*coset
, evalue
*dst
)
136 if (value_notzero_p(src
->d
)) {
137 value_assign(dst
->d
, src
->d
);
138 value_init(dst
->x
.n
);
139 value_assign(dst
->x
.n
, src
->x
.n
);
143 if (src
->x
.p
->type
== fractional
) {
146 Vector
*c
= Vector_Alloc(coset
->Size
);
149 evalue_extract_affine(&src
->x
.p
->arr
[0], c
->p
, c
->p
+c
->Size
-1, &f
.d
);
150 Inner_Product(coset
->p
, c
->p
, c
->Size
, &f
.x
.n
);
152 mpz_fdiv_r(f
.x
.n
, f
.x
.n
, f
.d
);
154 evalue_set_si(dst
, 0, 1);
155 for (int i
= src
->x
.p
->size
-1; i
>= 1; --i
) {
158 evalue_coset(&src
->x
.p
->arr
[i
], coset
, &t
);
160 free_evalue_refs(&t
);
162 free_evalue_refs(&f
);
166 assert(src
->x
.p
->type
== polynomial
);
167 value_set_si(dst
->d
, 0);
168 dst
->x
.p
= new_enode(src
->x
.p
->type
, src
->x
.p
->size
, src
->x
.p
->pos
);
169 for (int i
= 0; i
< src
->x
.p
->size
; ++i
)
170 evalue_coset(&src
->x
.p
->arr
[i
], coset
, &dst
->x
.p
->arr
[i
]);
173 static void evalue_print_list_evalue(FILE *out
, evalue
*e
, int nparam
,
177 L
= extract_common_lattice(e
, NULL
, nparam
);
179 print_evalue(out
, e
, params
);
181 fdostream
os(dup(fileno(out
)));
182 Vector
*coset
= Vector_Alloc(nparam
+1);
183 value_set_si(coset
->p
[nparam
], 1);
186 matrix2zz(L
, RT
, nparam
, nparam
);
188 fprintf(out
, "Lattice:\n");
191 lattice_point(coset
->p
, R
, vertices
, to_ulong(abs(determinant(R
))), NULL
);
193 for (int i
= 0; i
< vertices
.NumRows(); ++i
) {
195 os
<< vertices
[i
] << endl
;
196 zz2values(vertices
[i
], coset
->p
);
198 evalue_coset(e
, coset
, &t
);
199 print_evalue(out
, &t
, params
);
200 free_evalue_refs(&t
);
206 static void evalue_print_list(FILE *out
, evalue
*e
, int nparam
, char **params
)
209 assert(value_zero_p(e
->d
));
210 assert(e
->x
.p
->type
== partition
);
212 for (i
= 0; i
< e
->x
.p
->size
/2; i
++) {
213 Print_Domain(out
, EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), params
);
214 evalue_print_list_evalue(out
, &e
->x
.p
->arr
[2*i
+1], nparam
, params
);
218 static void print_domain_latex(std::ostream
& o
, Polyhedron
*D
, int nparam
,
222 for (int i
= 0; i
< D
->NbConstraints
; ++i
) {
223 if (First_Non_Zero(D
->Constraint
[i
]+1, D
->Dimension
) == -1)
228 for (int j
= 0; j
< D
->Dimension
; ++j
) {
229 if (value_zero_p(D
->Constraint
[i
][1+j
]))
232 if (!fc
&& value_pos_p(D
->Constraint
[i
][1+j
]))
234 if (value_mone_p(D
->Constraint
[i
][1+j
]))
236 else if (!value_one_p(D
->Constraint
[i
][1+j
]))
237 o
<< VALUE_TO_INT(D
->Constraint
[i
][1+j
]);
238 o
<< " " << params
[j
];
241 if (!fc
&& value_pos_p(D
->Constraint
[i
][1+D
->Dimension
]))
243 if (value_notzero_p(D
->Constraint
[i
][1+D
->Dimension
]))
244 o
<< VALUE_TO_INT(D
->Constraint
[i
][1+D
->Dimension
]);
245 if (value_zero_p(D
->Constraint
[i
][0]))
253 static void evalue_print_latex(std::ostream
& o
, const evalue
*e
,
254 int first
, int nested
,
255 const string
& suffix1
, const string
& suffix2
,
256 int nparam
, char **params
);
258 static void evalue_print_poly_latex1(std::ostream
& o
, const evalue
*e
,
259 int first
, int nested
, const string
& base
,
260 const string
& suffix1
, const string
& suffix2
,
261 int nparam
, char **params
)
263 int offset
= type_offset(e
->x
.p
);
264 for (int i
= e
->x
.p
->size
-1; i
>= offset
; --i
) {
265 std::ostringstream strm
;
270 strm
<< "^" << (i
-offset
);
271 evalue_print_latex(o
, &e
->x
.p
->arr
[i
], first
, nested
,
272 strm
.str(), suffix2
, nparam
, params
);
277 static void evalue_print_poly_latex2(std::ostream
& o
, const evalue
*e
,
278 int first
, int nested
, const string
& base
,
279 const string
& suffix1
, const string
& suffix2
,
280 int nparam
, char **params
)
282 int offset
= type_offset(e
->x
.p
);
283 for (int i
= e
->x
.p
->size
-1; i
>= offset
; --i
) {
284 std::ostringstream strm
;
289 strm
<< "^" << (i
-offset
);
290 evalue_print_latex(o
, &e
->x
.p
->arr
[i
], first
, nested
,
291 suffix1
, strm
.str(), nparam
, params
);
296 static void evalue_print_latex(std::ostream
& o
, const evalue
*e
,
297 int first
, int nested
,
298 const string
& suffix1
, const string
&suffix2
,
299 int nparam
, char **params
)
301 if (value_notzero_p(e
->d
)) {
302 if (value_zero_p(e
->x
.n
)) {
309 value_absolute(tmp
, e
->x
.n
);
310 if (!first
&& value_pos_p(e
->x
.n
))
312 if (value_neg_p(e
->x
.n
))
314 if (value_one_p(e
->d
)) {
315 if (!value_one_p(tmp
) ||
316 (suffix1
.length() == 0 && suffix2
.length() == 0))
317 o
<< VALUE_TO_INT(tmp
);
320 if (value_one_p(tmp
) && suffix1
.length() != 0)
323 o
<< VALUE_TO_INT(tmp
);
325 << VALUE_TO_INT(e
->d
) << "}";
327 if (!value_one_p(tmp
)) {
337 switch (e
->x
.p
->type
) {
339 o
<< "\\begin{cases}\n";
340 for (int i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
343 evalue_print_latex(o
, &e
->x
.p
->arr
[2*i
+1], 1, 0,
344 suffix1
, suffix2
, nparam
, params
);
345 o
<< "& \\text{if $";
346 print_domain_latex(o
, EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), nparam
, params
);
349 o
<< "\\end{cases}\n";
352 evalue_print_poly_latex1(o
, e
, first
, nested
, params
[e
->x
.p
->pos
-1],
353 suffix1
, suffix2
, nparam
, params
);
356 std::ostringstream strm
;
357 strm
<< "\\fractional{";
358 evalue_print_latex(strm
, &e
->x
.p
->arr
[0], 1, 1, "", "", nparam
, params
);
360 evalue_print_poly_latex2(o
, e
, first
, nested
,
361 strm
.str(), suffix1
, suffix2
, nparam
, params
);
369 static void evalue_print_latex(FILE *out
, const evalue
*e
, int nparam
,
372 fdostream
os(dup(fileno(out
)));
373 evalue_print_latex(os
, e
, 1, 0, "", "", nparam
, params
);
376 int evalue_convert(evalue
*EP
, struct convert_options
*options
,
377 int verbose
, unsigned nparam
, char **params
)
380 if (options
->combine
)
383 evalue_range_reduction(EP
);
385 print_evalue(stdout
, EP
, params
);
388 if (options
->floor
) {
389 fprintf(stderr
, "WARNING: floor conversion not supported\n");
390 evalue_frac2floor2(EP
, 0);
392 print_evalue(stdout
, EP
, params
);
393 } else if (options
->list
&& params
) {
394 evalue_print_list(stdout
, EP
, nparam
, params
);
396 } else if (options
->latex
&& params
) {
397 evalue_print_latex(stdout
, EP
, nparam
, params
);
399 } else if (options
->convert
) {
400 evalue_mod2table(EP
, nparam
);
402 print_evalue(stdout
, EP
, params
);