"support" floor conversion for parametric polytopes
[barvinok.git] / genfun.cc
blob4df57e330d6b35196b1b10c744a36008b21cad28
1 #include <iostream>
2 #include <assert.h>
3 #include <genfun.h>
5 using std::cout;
7 static int lex_cmp(vec_ZZ& a, vec_ZZ& b)
9 assert(a.length() == b.length());
11 for (int j = 0; j < a.length(); ++j)
12 if (a[j] != b[j])
13 return a[j] < b[j] ? -1 : 1;
14 return 0;
17 static int lex_cmp(mat_ZZ& a, mat_ZZ& b)
19 assert(a.NumCols() == b.NumCols());
20 int alen = a.NumRows();
21 int blen = b.NumRows();
22 int len = alen < blen ? alen : blen;
24 for (int i = 0; i < len; ++i) {
25 int s = lex_cmp(a[i], b[i]);
26 if (s)
27 return s;
29 return alen-blen;
32 void gen_fun::add(ZZ& cn, ZZ& cd, vec_ZZ& num, mat_ZZ& den)
34 if (cn == 0)
35 return;
37 short_rat * r = new short_rat;
38 r->n.coeff.SetDims(1, 2);
39 r->n.coeff[0][0] = cn;
40 r->n.coeff[0][1] = cd;
41 r->n.power.SetDims(1, num.length());
42 r->n.power[0] = num;
43 r->d.power = den;
45 for (int i = 0; i < r->d.power.NumRows(); ++i) {
46 int j;
47 for (j = 0; j < r->d.power.NumCols(); ++j)
48 if (r->d.power[i][j] != 0)
49 break;
50 if (r->d.power[i][j] < 0) {
51 r->d.power[i] = -r->d.power[i];
52 r->n.coeff[0][0] = -r->n.coeff[0][0];
53 r->n.power[0] += r->d.power[i];
57 for (int i = 0; i < term.size(); ++i)
58 if (lex_cmp(term[i]->d.power, r->d.power) == 0) {
59 int len = term[i]->n.coeff.NumRows();
60 int dim = term[i]->n.power.NumCols();
61 term[i]->n.coeff.SetDims(len+1, 2);
62 term[i]->n.power.SetDims(len+1, dim);
63 term[i]->n.coeff[len] = r->n.coeff[0];
64 term[i]->n.power[len] = r->n.power[0];
65 delete r;
66 return;
69 term.push_back(r);
72 static void print_power(vec_ZZ& c, vec_ZZ& p,
73 unsigned int nparam, char **param_name)
75 bool first = true;
77 for (int i = 0; i < p.length(); ++i) {
78 if (p[i] == 0)
79 continue;
80 if (first) {
81 if (c[0] == -1 && c[1] == 1)
82 cout << "-";
83 else if (c[0] != 1 || c[1] != 1) {
84 cout << c[0];
85 if (c[1] != 1)
86 cout << " / " << c[1];
87 cout << " * ";
89 first = false;
90 } else
91 cout << " * ";
92 if (i < nparam)
93 cout << param_name[i];
94 else
95 cout << "x" << i;
96 if (p[i] == 1)
97 continue;
98 if (p[i] < 0)
99 cout << "^(" << p[i] << ")";
100 else
101 cout << "^" << p[i];
103 if (first) {
104 cout << c[0];
105 if (c[1] != 1)
106 cout << " / " << c[1];
110 void gen_fun::print(unsigned int nparam, char **param_name)
112 vec_ZZ mone;
113 mone.SetLength(1);
114 mone[0] = -1;
115 mone[1] = 1;
116 for (int i = 0; i < term.size(); ++i) {
117 if (i != 0)
118 cout << " + ";
119 cout << "(";
120 for (int j = 0; j < term[i]->n.coeff.NumRows(); ++j) {
121 if (j != 0 && term[i]->n.coeff[j][0] > 0)
122 cout << "+";
123 print_power(term[i]->n.coeff[j], term[i]->n.power[j],
124 nparam, param_name);
126 cout << ")/(";
127 for (int j = 0; j < term[i]->d.power.NumRows(); ++j) {
128 if (j != 0)
129 cout << " * ";
130 cout << "(1";
131 print_power(mone, term[i]->d.power[j],
132 nparam, param_name);
133 cout << ")";
135 cout << ")";