1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
////////////////////////////////////////////////////////////////////////////////
//
// RegularityCheck.cc
//
// produced: 2001/10/28 jr
// last change: 2001/10/28 jr
//
////////////////////////////////////////////////////////////////////////////////
#include <set>
#include <unordered_map>
#include "RegularityCheck.hh"
namespace topcom {
#ifdef REGULARITY_CACHE
ConstraintCache RegularityCheck::_cache;
#endif
std::mutex RegularityCheck::_reg_mutex;
size_type RegularityCheck::cnt_calls = 0;
// constructors:
RegularityCheck::RegularityCheck(const PointConfiguration& points,
const Chirotope& chiro,
const Incidences& incidences,
const SimplicialComplex& t) :
_heights(points.no()),
_coeffs(),
_pointsptr(&points),
_chiroptr (&chiro),
_triangptr(&t),
_incidencesptr(&incidences) {
static const Vector zerovec(_chiroptr->no());
// // keep a vector of matrices:
// std::vector<StairCaseMatrix> matrices(_pointsptr->rank());
parameter_type no = _chiroptr->no();
parameter_type rank = _chiroptr->rank();
// we build the coefficient matrix from the chirotope and the triangulation t:
SimplicialComplex intfacets;
std::unordered_set<Simplex, Hash<Simplex> > unionsimps;
// we ignore duplicates (disabled):
size_type cnt_duplicates(0UL);
// for all simplices in the triangulation t collect interior facets:
for (SimplicialComplex::const_iterator iter = t.begin();
iter != t.end();
++iter) {
intfacets += _incidencesptr->intfacets(*iter);
}
size_type intfacets_cnt = intfacets.card();
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << intfacets_cnt << " interior facets in triangulation " << t << std::endl;
}
// prepare a matrix with a column for each interior facet
// (some may be irrelevant, but at this point we do not care):
_coeffs.resize(intfacets_cnt, zerovec);
parameter_type col_cnt = 0;
// for all interior facets ...
for (SimplicialComplex::const_iterator iter = intfacets.begin();
iter != intfacets.end();
++iter) {
const Simplex intfacet(*iter);
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "processing interior facet " << intfacet << " ..." << std::endl;
}
// find the two simplices in t (or fewer, if t is partial) containing intfacet:
const SimplicialComplex simppair(t * _incidencesptr->intcofacets(intfacet));
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "simplices in partial triangulation containing it are " << simppair << std::endl;
}
if (simppair.card() < 2) {
// intfacet is not interior in t, thus no constraint:
continue;
}
SimplicialComplex::const_iterator pairiter(simppair.begin());
const Simplex simp1(*pairiter);
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "simp1 = " << simp1 << std::endl;
}
++pairiter;
const Simplex simp2(*pairiter);
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "simp2 = " << simp2 << std::endl;
}
const Simplex unionsimp(simp1 + simp2);
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "simp1 union simp2 = " << unionsimp << std::endl;
}
if (unionsimps.find(unionsimp) != unionsimps.end()) {
// constraints on identical rank + 1 subsets will be identical:
continue;
}
unionsimps.insert(unionsimp);
#ifdef REGULARITY_CACHE
const ConstraintCacheEntry constraint_config(unionsimp, intfacet);
#endif
// grab reference to the right column in resulting matrix:
Vector& new_col = _coeffs[col_cnt];
++col_cnt;
#ifdef REGULARITY_CACHE
// check for cached values:
{
std::lock_guard<std::mutex> lock(_reg_mutex);
ConstraintCache::const_iterator find_iter = _cache.find(constraint_config);
if (find_iter != _cache.end()) {
new_col = find_iter->second;
continue;
}
}
#endif
// compute the index not in simp1:
const Simplex simpdiff = simp2 - intfacet;
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "vertex not in interior facet: " << simpdiff << std::endl;
}
if (simpdiff.empty()) {
continue;
}
const parameter_type new_index = *simpdiff.begin();
// simp1 is the calibration simplex --
// if point new_index receives infinite height, then new_index is folded above aff(simp1):
int calibrate = (*_chiroptr)(simp1);
// determinants can be taken from the chirotope, since
// RegularityCheck is only called when there are points available:
new_col[new_index] = calibrate * _chiroptr->det(simp1);
calibrate = -calibrate;
// build a permutation out of simp1, i.e., at first, new_index is missing:
Permutation simp1_perm(no, rank, simp1);
// the remaining coefficients now receive alternating signs,
// when the points i missing in new_index union simp1 minus i are traversed in increasing order;
// the first missing element is the min of simp1:
Simplex::const_iterator simp1_iter = simp1.begin();
// count iterations:
int cnt = 0;
// overwrite first element of simp1_perm with new_index,
// then the min of simp1 is missing:
simp1_perm[cnt] = new_index;
// iterate over further missing elements:
while (true) {
// fetch determinant from chirotope structure:
new_col[*simp1_iter] = calibrate * _chiroptr->det(simp1_perm);
calibrate = -calibrate;
// check if we are done already:
if (++cnt == rank) {
break;
}
// exchange the permutation element at cnt with the currently missing element:
simp1_perm[cnt] = *simp1_iter;
// increment iterator:
++simp1_iter;
}
#ifdef REGULARITY_CACHE
{
// cache the computational result:
std::lock_guard<std::mutex> lock(_reg_mutex);
_cache[constraint_config] = new_col;
}
#endif
//////////////////////////////////////////////////////////////////////////////
// obsolete section (matrix computations can now be avoided altogether):
//////////////////////////////////////////////////////////////////////////////
// // build the matrix of all points corresponding to the folding constraint
// // for the internal ridge:
// StairCaseMatrix basis_matrix;
// const Matrix raw_basis_matrix(*_pointsptr, simp1);
// basis_matrix.augment(raw_basis_matrix);
// // for (basis_type::const_iterator basis_iter = simp1.begin();
// // basis_iter != simp1.end();
// // ++basis_iter) {
// // basis_matrix.augment((*_pointsptr)[*basis_iter]);
// // }
// const parameter_type new_index = *((simp2 - intfacet).begin());
// // the orientation of simp1 is the calibration according to a point with
// // infinite height:
// Field det_basis = basis_matrix.det();
// int calibrate(sign(det_basis));
// new_col[new_index] = calibrate * det_basis;
// calibrate = -calibrate;
// // build the matrices with the point not in simp1 and the facets of simp1
// // matrix[i] is built from (new_index union simp1 minus i):
// int cnt(0);
// for (int i = 0; i < matrices.size(); ++i) {
// matrices[i] = StairCaseMatrix((*_pointsptr)[new_index]);
// }
// for (Simplex::const_iterator simp_iter = simp1.begin();
// simp_iter != simp1.end();
// ++simp_iter) {
// for (int i = 0; i < cnt; ++i) {
// matrices[i].augment((*_pointsptr)[*simp_iter]);
// }
// if (cnt + 1 < matrices.size()) {
// matrices[cnt + 1].augment((*_pointsptr)[*simp_iter]);
// for (int i = cnt + 2; i < matrices.size(); ++i) {
// matrices[i] = matrices[cnt + 1];
// }
// }
// ++cnt;
// }
//
// // collect the results:
// cnt = 0;
// for (Simplex::const_iterator simp_iter = simp1.begin();
// simp_iter != simp1.end();
// ++simp_iter) {
// new_col.at(*simp_iter) = calibrate * matrices.at(cnt).det();
// calibrate = -calibrate;
// ++cnt;
// }
//////////////////////////////////////////////////////////////////////////////
// end obsolete section
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// another obsolete section
//////////////////////////////////////////////////////////////////////////////
// // extract the matrix of simp1 and consider it to the right of column new_index:
// Matrix after_gap_matrix(*_pointsptr, simp1);
// // compute det with the help of a StairCaseMatrix:
// StairCaseMatrix comp_matrix;
// comp_matrix.augment(after_gap_matrix);
// Field det_simp1 = comp_matrix.det();
// int calibrate = sign(det_simp1);
// // calibrate the sign of simp1 to "plus":
// new_col[new_index] = calibrate * det_simp1;
// calibrate = -calibrate;
// // all other matrice start with column "new_index":
// StairCaseMatrix before_gap_matrix((*_pointsptr)[new_index]);
// // the matrices to the right of the missing column start with the calibration matrix:
// IntegerSet ignored_cols;
// parameter_type cnt = 0;
// for (Simplex::const_iterator simp_iter = simp1.begin();
// simp_iter != simp1.end();
// ++simp_iter) {
// // ignore the next column from the matrix after the missing column:
// ignored_cols += cnt;
// ++cnt;
// // reuse the eliminations performed on the part left to the missing column:
// comp_matrix = before_gap_matrix;
// comp_matrix.augment(after_gap_matrix, ignored_cols);
// // complete and eliminate by the matrix right of the missing column and store its determinant:
// new_col[*simp_iter] = calibrate * comp_matrix.det();
// if (cnt == rank) {
// break;
// }
// calibrate = -calibrate;
// // add the missing column to generate the matrix left of the next missing column and eliminate:
// before_gap_matrix.augment((*_pointsptr)[*simp_iter]);
// }
//////////////////////////////////////////////////////////////////////////////
// end obsolete section
//////////////////////////////////////////////////////////////////////////////
if (CommandlineOptions::debug()) {
// check the result:
Matrix simp1_matrix(*_pointsptr, simp1);
Field det_simp1 = simp1_matrix.det();
Vector check_col(_chiroptr->no());
int calibrate = sign(det_simp1);
const parameter_type new_index = *(simp2 - intfacet).begin();
check_col[new_index] = new_col[new_index];
calibrate = -calibrate;
basis_type basis(simp1);
for (Simplex::const_iterator simp_iter = simp1.begin();
simp_iter != simp1.end();
++simp_iter) {
const parameter_type idx(*simp_iter);
basis -= idx;
StairCaseMatrix basis_matrix;
basis_matrix.push_back((*_pointsptr)[new_index]);
for (basis_type::const_iterator basis_iter = basis.begin();
basis_iter != basis.end();
++basis_iter) {
basis_matrix.augment((*_pointsptr)[*basis_iter]);
}
check_col[idx] = calibrate * basis_matrix.det();
calibrate = -calibrate;
basis += idx;
}
if (new_col != check_col) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << "RegularityCheck::RegularityCheck(...):"
<< " constraint coefficient vector inconsistent with check:" << std::endl;
std::cerr << "simp1 : " << simp1 << std::endl;
std::cerr << "new_index: " << new_index << std::endl;
std::cerr << "fast : " << new_col << std::endl;
std::cerr << "old : " << check_col << std::endl;
std::cerr << "exiting" << std::endl;
exit(1);
}
}
// redundancy (which can only occur in the case of coplanar points) need not be removed here;
// redundancy removal is faster in the LP solver;
// thus, simply push back the vector:
// _coeffs.push_back(new_col);
//////////////////////////////////////////////////////////////////////////////
// another obsolete section
//////////////////////////////////////////////////////////////////////////////
// // finally, push_back the new column if it is new;
// // it corresponds to
// // local regularity at an internal facet
// // (we take a column for efficiency reasons,
// // in the LP solver it will be a row, of course):
// if (CommandlineOptions::debug()) {
// std::lock_guard<std::mutex> lock(IO_sync::mutex);
// std::cerr << "checking novelty of new_col from interior facet "
// << intfacet << ": " << new_col << std::endl;
// }
// if (!new_col.is_zero() && (old_columns.find(new_col) == old_columns.end())) {
// _coeffs.push_back(new_col);
// old_columns.insert(new_col);
// }
// else {
// ++cnt_duplicates;
// }
//////////////////////////////////////////////////////////////////////////////
// end obsolete section
//////////////////////////////////////////////////////////////////////////////
}
// remove trailing zero-columns that were not needed:
_coeffs.resize(col_cnt);
if (CommandlineOptions::debug()) {
std::lock_guard<std::mutex> lock(IO_sync::mutex);
std::cerr << _coeffs.size() << " columns found" << std::endl;
// std::cerr << cnt_duplicates << " duplicate columns found and ignored" << std::endl;
std::cerr << "the coefficient matrix for regularity check:" << std::endl;
if (_coeffs.coldim() == 0) {
std::cerr << "no constraints." << std::endl;
}
else {
_coeffs.transpose().pretty_print(std::cout);
}
}
}
}; // namespace topcom
// eof RegularityCheck.cc
|