3 #include "lattice_point.h"
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
)
13 return a
[j
] < b
[j
] ? -1 : 1;
17 void bf_base::add_term(bfc_term_base
*t
, vec_ZZ
& num_orig
, vec_ZZ
& extra_num
)
20 int d
= num_orig
.length();
22 for (int l
= 0; l
< d
-1; ++l
)
23 num
[l
] = num_orig
[l
+1] + extra_num
[l
];
28 void bf_base::add_term(bfc_term_base
*t
, vec_ZZ
& num
)
30 int len
= t
->terms
.NumRows();
32 for (i
= 0; i
< len
; ++i
) {
33 r
= lex_cmp(t
->terms
[i
], num
);
37 if (i
== len
|| r
> 0) {
38 t
->terms
.SetDims(len
+1, num
.length());
47 bfc_term_base
* bf_base::find_bfc_term(bfc_vec
& v
, int *powers
, int len
)
50 for (i
= v
.begin(); i
!= v
.end(); ++i
) {
52 for (j
= 0; j
< len
; ++j
)
53 if ((*i
)->powers
[j
] != powers
[j
])
57 if ((*i
)->powers
[j
] > powers
[j
])
61 bfc_term_base
* t
= new_bf_term(len
);
63 memcpy(t
->powers
, powers
, len
* sizeof(int));
68 void bf_base::reduce(mat_ZZ
& factors
, bfc_vec
& v
)
71 unsigned nf
= factors
.NumRows();
72 unsigned d
= factors
.NumCols();
75 return base(factors
, v
);
77 bf_reducer
bfr(factors
, v
, this);
81 if (bfr
.vn
.size() > 0)
82 reduce(bfr
.nfactors
, bfr
.vn
);
85 int bf_base::setup_factors(const mat_ZZ
& rays
, mat_ZZ
& factors
,
86 bfc_term_base
* t
, int s
)
88 factors
.SetDims(dim
, dim
);
92 for (r
= 0; r
< dim
; ++r
)
95 for (r
= 0; r
< dim
; ++r
) {
98 for (k
= 0; k
< dim
; ++k
)
99 if (factors
[r
][k
] != 0)
101 if (factors
[r
][k
] < 0) {
102 factors
[r
] = -factors
[r
];
103 t
->terms
[0] += factors
[r
];
111 void bf_base::handle(const mat_ZZ
& rays
, Value
*vertex
, QQ c
, int *closed
)
113 bfc_term
* t
= new bfc_term(dim
);
114 vector
< bfc_term_base
* > v
;
119 t
->terms
.SetDims(1, dim
);
120 lattice_point(vertex
, rays
, t
->terms
[0], closed
);
122 // the elements of factors are always lexpositive
124 int s
= setup_factors(rays
, factors
, t
, 1);
132 bfc_term_base
* bfcounter_base::new_bf_term(int len
)
134 bfc_term
* t
= new bfc_term(len
);
139 void bfcounter_base::set_factor(bfc_term_base
*t
, int k
, int change
)
141 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
147 void bfcounter_base::set_factor(bfc_term_base
*t
, int k
, mpq_t
&f
, int change
)
149 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
150 value2zz(mpq_numref(f
), c
.n
);
151 value2zz(mpq_denref(f
), c
.d
);
157 void bfcounter_base::set_factor(bfc_term_base
*t
, int k
, const QQ
& c_factor
,
160 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
167 void bfcounter_base::insert_term(bfc_term_base
*t
, int i
)
169 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
170 int len
= t
->terms
.NumRows()-1; // already increased by one
172 bfct
->c
.SetLength(len
+1);
173 for (int j
= len
; j
> i
; --j
) {
174 bfct
->c
[j
] = bfct
->c
[j
-1];
175 t
->terms
[j
] = t
->terms
[j
-1];
180 void bfcounter_base::update_term(bfc_term_base
*t
, int i
)
182 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
187 void bf_reducer::compute_extra_num(int i
)
191 no_param
= 0; // r from text
192 only_param
= 0; // k-r-s from text
193 total_power
= 0; // k from text
195 for (int j
= 0; j
< nf
; ++j
) {
196 if (v
[i
]->powers
[j
] == 0)
199 total_power
+= v
[i
]->powers
[j
];
200 if (factors
[j
][0] == 0) {
201 only_param
+= v
[i
]->powers
[j
];
205 if (old2new
[j
] == -1)
206 no_param
+= v
[i
]->powers
[j
];
208 extra_num
+= -sign
[j
] * v
[i
]->powers
[j
] * nfactors
[old2new
[j
]];
209 changes
+= v
[i
]->powers
[j
];
213 void bf_reducer::update_powers(const std::vector
<int>& powers
)
215 for (int l
= 0; l
< nnf
; ++l
)
216 npowers
[l
] = bpowers
[l
];
218 l_extra_num
= extra_num
;
221 for (int l
= 0; l
< powers
.size(); ++l
) {
225 assert(old2new
[l
] != -1);
227 npowers
[old2new
[l
]] += n
;
228 // interpretation of sign has been inverted
229 // since we inverted the power for specialization
231 l_extra_num
+= n
* nfactors
[old2new
[l
]];
238 void bf_reducer::compute_reduced_factors()
240 unsigned nf
= factors
.NumRows();
241 unsigned d
= factors
.NumCols();
243 nfactors
.SetDims(nnf
, d
-1);
245 for (int i
= 0; i
< nf
; ++i
) {
248 for (j
= 0; j
< nnf
; ++j
) {
250 for (k
= 1; k
< d
; ++k
)
251 if (factors
[i
][k
] != 0 || nfactors
[j
][k
-1] != 0)
253 if (k
< d
&& factors
[i
][k
] == -nfactors
[j
][k
-1])
256 if (factors
[i
][k
] != s
* nfactors
[j
][k
-1])
264 for (k
= 1; k
< d
; ++k
)
265 if (factors
[i
][k
] != 0)
268 if (factors
[i
][k
] < 0)
270 nfactors
.SetDims(++nnf
, d
-1);
271 for (int k
= 1; k
< d
; ++k
)
272 nfactors
[j
][k
-1] = s
* factors
[i
][k
];
278 npowers
= new int[nnf
];
279 bpowers
= new int[nnf
];
282 void bf_reducer::reduce()
284 compute_reduced_factors();
286 for (int i
= 0; i
< v
.size(); ++i
) {
287 compute_extra_num(i
);
291 extra_num
.SetLength(d
-1);
294 for (int k
= 0; k
< nnf
; ++k
)
296 for (int k
= 0; k
< nf
; ++k
) {
297 assert(old2new
[k
] != -1);
298 npowers
[old2new
[k
]] += v
[i
]->powers
[k
];
300 extra_num
+= v
[i
]->powers
[k
] * nfactors
[old2new
[k
]];
301 changes
+= v
[i
]->powers
[k
];
305 bfc_term_base
* t
= bf
->find_bfc_term(vn
, npowers
, nnf
);
306 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
307 bf
->set_factor(v
[i
], k
, changes
% 2);
308 bf
->add_term(t
, v
[i
]->terms
[k
], extra_num
);
311 // powers of "constant" part
312 for (int k
= 0; k
< nnf
; ++k
)
314 for (int k
= 0; k
< nf
; ++k
) {
315 if (factors
[k
][0] != 0)
317 assert(old2new
[k
] != -1);
318 bpowers
[old2new
[k
]] += v
[i
]->powers
[k
];
320 extra_num
+= v
[i
]->powers
[k
] * nfactors
[old2new
[k
]];
321 changes
+= v
[i
]->powers
[k
];
326 for (j
= 0; j
< nf
; ++j
)
327 if (old2new
[j
] == -1 && v
[i
]->powers
[j
] > 0)
330 dpoly
D(no_param
, factors
[j
][0], 1);
331 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
332 dpoly
fact(no_param
, factors
[j
][0], 1);
336 if (old2new
[j
] == -1)
337 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
338 dpoly
fact(no_param
, factors
[j
][0], 1);
342 if (no_param
+ only_param
== total_power
&&
343 bf
->constant_vertex(d
)) {
344 bfc_term_base
* t
= NULL
;
349 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
350 dpoly
n(no_param
, v
[i
]->terms
[k
][0]);
351 mpq_set_si(bf
->tcount
, 0, 1);
352 n
.div(D
, bf
->tcount
, bf
->one
);
354 if (value_zero_p(mpq_numref(bf
->tcount
)))
358 t
= bf
->find_bfc_term(vn
, bpowers
, nnf
);
359 bf
->set_factor(v
[i
], k
, bf
->tcount
, changes
% 2);
360 bf
->add_term(t
, v
[i
]->terms
[k
], extra_num
);
363 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
364 dpoly
n(no_param
, v
[i
]->terms
[j
][0]);
367 if (no_param
+ only_param
== total_power
)
368 r
= new dpoly_r(n
, nf
);
370 for (int k
= 0; k
< nf
; ++k
) {
371 if (v
[i
]->powers
[k
] == 0)
373 if (factors
[k
][0] == 0 || old2new
[k
] == -1)
376 dpoly
pd(no_param
-1, factors
[k
][0], 1);
378 for (int l
= 0; l
< v
[i
]->powers
[k
]; ++l
) {
380 for (q
= 0; q
< k
; ++q
)
381 if (old2new
[q
] == old2new
[k
] &&
386 r
= new dpoly_r(n
, pd
, q
, nf
);
388 dpoly_r
*nr
= new dpoly_r(r
, pd
, q
, nf
);
395 dpoly_r
*rc
= r
->div(D
);
398 factor
.d
= rc
->denom
;
400 if (bf
->constant_vertex(d
)) {
401 dpoly_r_term_list
& final
= rc
->c
[rc
->len
-1];
403 dpoly_r_term_list::iterator k
;
404 for (k
= final
.begin(); k
!= final
.end(); ++k
) {
405 if ((*k
)->coeff
== 0)
408 update_powers((*k
)->powers
);
410 bfc_term_base
* t
= bf
->find_bfc_term(vn
, npowers
, nnf
);
411 factor
.n
= (*k
)->coeff
;
412 bf
->set_factor(v
[i
], j
, factor
, l_changes
% 2);
413 bf
->add_term(t
, v
[i
]->terms
[j
], l_extra_num
);
416 bf
->cum(this, v
[i
], j
, rc
);