File: RealChiro.cc

package info (click to toggle)
topcom 0.17.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,572 kB
  • sloc: cpp: 16,640; sh: 975; makefile: 345; ansic: 40
file content (327 lines) | stat: -rw-r--r-- 9,152 bytes parent folder | download
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
////////////////////////////////////////////////////////////////////////////////
// 
// RealChiro.cc
//
//    produced: 13 Mar 1998 jr
// last change: 30 Jun 2000 jr
// 
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#include "RealChiro.hh"

RealChiro::RealChiro(const PointConfiguration& points, bool preprocess) : 
  chirotope_data(), _no(points.coldim()), _rank(points.rowdim()) {
  for (parameter_type i = 0; i < _no - _rank + 1; ++i) {
    _recursive_chiro(points[i], points, basis_type(i), i + 1, 1, false);
  }
  if (CommandlineOptions::verbose()) {
    std::cerr << std::endl;
    std::cerr << load() << " signs in total." << std::endl;
  }
}

const int RealChiro::operator()(const basis_type&  prebasis,
				const Permutation& lex_extension_perm) const {
  assert(lex_extension_perm.n() == no());
  assert(lex_extension_perm.k() <= no());
  Permutation basis_perm(no(), rank() - 1, prebasis);
  basis_type basis(prebasis);
  for (size_type i = 0; i < lex_extension_perm.k(); ++i) {
    if (basis.contains(lex_extension_perm[i])) {
      continue;
    }
    basis += lex_extension_perm[i];
    const int basis_sign((*this)(basis));
    if (basis_sign == 0) {
      basis -= lex_extension_perm[i];
      continue;
    }
    basis_perm.append(lex_extension_perm[i]);
    int perm_sign = basis_perm.sign();
    return perm_sign * basis_sign;
  }
  return 0;
}
// functions:
void RealChiro::erase_random() {
#ifdef STL_CHIROTOPE
  size_type random_index(random() % this->size());
  chirotope_data::iterator iter = this->begin();
  chirotope_data::erase(iter);
#else
  chirotope_data::erase_random();
#endif
}

const basis_type RealChiro::find_non_deg_basis() const {
  basis_type result;
  if (_no == 0) {
    return result;
  }
  if (_rank == 0) {
    return result;
  }
  Permutation perm(_no, _rank);
  result = basis_type(perm);
  while ( ((*this)(result) == 0) && (perm.lexnext()) ) {
    result = basis_type(perm);
  }
  assert((*this)(result) != 0);
  return result;
}

RealChiro RealChiro::dual() const {
  static long count;
  const basis_type groundset(0, _no);
  RealChiro result;
  result._no = _no;
  result._rank = _no - _rank;
  for (const_iterator iter = begin(); iter != end(); ++iter) {
#ifdef STL_CHIROTOPE
    const basis_type basis = iter->first;
    const int basis_sign = iter->second;
#else
    const basis_type basis = iter->key();
    const int basis_sign = iter->data();
#endif
    const basis_type dualbasis(groundset - basis);
    Permutation perm(dualbasis);
    perm.append(Permutation(basis));
    int perm_sign = perm.sign(result._rank);
    result[dualbasis] = perm_sign * basis_sign;
#ifdef COMPUTATIONS_DEBUG
    if (perm.sign() != perm_sign) {
      std::cerr << "RealChiro RealChiro::dual() const: "
	   << "perm_sign error." << std::endl;
    }
#endif
#ifdef SUPER_VERBOSE
    std::cerr << perm_sign * (*this)(basis) << ',';
#endif
    if (CommandlineOptions::verbose() && (++count % 1000 == 0)) {
#ifdef SUPER_VERBOSE
      std::cerr << std::endl;
#endif
      std::cerr << count << " signs computed so far." << std::endl;
    }
  }
  if (CommandlineOptions::verbose()) {
    std::cerr << load() << " signs in total." << std::endl;
  }
  return result;
}

// stream output/input:
std::ostream& RealChiro::print_string(std::ostream& ost) const {
  size_type count(0);
  ost << _no << ',' << _rank << ':' << std::endl;
  Permutation perm(_no, _rank);
  do {
    int chiro_on_perm = (*this)(basis_type(perm));
#ifdef SUPER_VERBOSE
    std::cerr << "chiro(" << basis_type(perm) << ") = " << chiro_on_perm << std::endl;
#endif
    ost << int2sign(chiro_on_perm);
    if (++count % 100 == 0) {
      ost << '\n';
    }
  } while (perm.lexnext());
  ost << std::endl;
#ifdef WATCH_MAXCHAINLEN
  std::cerr << "max chain length of hash table: " << maxchainlen() << std::endl;
#endif
  return ost;
}

std::ostream& RealChiro::print_dualstring(std::ostream& ost) const {
  static long count;
  const basis_type groundset(0, _no);
  ost << _no << ',' << _no - _rank << ':' << std::endl;
  Permutation dualperm(_no, _no -_rank);
  do {
    const Permutation primalperm(groundset - basis_type(dualperm));
    const basis_type basis(primalperm);
    Permutation perm(dualperm);
    perm.append(primalperm);
    const int perm_sign = perm.sign(_no - _rank);
    const int chiro_on_perm = perm_sign * (*this)(basis);
    ost << int2sign(chiro_on_perm);
    if (++count % 100 == 0) {
      ost << '\n';
    }
#ifdef COMPUTATIONS_DEBUG
    if (perm.sign() != perm_sign) {
      std::cerr << "RealChiro RealChiro::print_dualstring() const: "
	   << "perm_sign error." << std::endl;
    }
#endif
#ifdef SUPER_VERBOSE
    std::cerr << perm_sign * (*this)(basis) << ',';
#endif
    if (count % 1000 == 0) {
      if (CommandlineOptions::verbose()) {
#ifdef SUPER_VERBOSE
	std::cerr << std::endl;
#endif
	std::cerr << count << " signs computed so far." << std::endl;
      }
    }
  } while (dualperm.lexnext());
  ost << std::endl;
  if (CommandlineOptions::verbose()) {
    std::cerr << count << " signs in total." << std::endl;
  }
  return ost;
}

std::istream& RealChiro::read_string(std::istream& ist) {
  char c;

  clear();
  if (!(ist >> std::ws >> _no)) {
#ifdef READ_DEBUG
    std::cerr << "Circuits::read_string(std::istream&): "
	 << "number of points not found." << std::endl;
#endif
    ist.clear(std::ios::failbit);
    return ist;
  }
  if (!(ist >> std::ws >> c)) {
#ifdef READ_DEBUG
    std::cerr << "Circuits::read_string(std::istream&): "
	 << "separator not found." << std::endl;
#endif
    ist.clear(std::ios::failbit);
    return ist;
  }
  if (!(ist >> std::ws >> _rank)) {
#ifdef READ_DEBUG
    std::cerr << "Circuits::read_string(std::istream&): "
	 << "rank not found." << std::endl;
#endif
    ist.clear(std::ios::failbit);
    return ist;
  }
  if (!(ist >> std::ws >> c)) {
#ifdef READ_DEBUG
    std::cerr << "Circuits::read_string(std::istream&): "
	 << "separator not found." << std::endl;
#endif
    ist.clear(std::ios::failbit);
    return ist;
  }
  if (_rank > _no) {
#ifdef READ_DEBUG
    std::cerr << "Circuits::read_string(std::istream&): "
	 << "rank must not be larger than number of points." << std::endl;
#endif
    ist.clear(std::ios::failbit);
    return ist;    
  }
  Permutation perm(_no, _rank);
  do {
    if (ist >> c) {
      (*this)[basis_type(perm)] = sign2int(c);
    }
    else {
#ifdef READ_DEBUG
      std::cerr << "Circuits::read_string(std::istream&): "
	   << "not enough signs." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
  } while (perm.lexnext());
  return ist;
}

// internal algorithms:
void RealChiro::_recursive_chiro(const StairCaseMatrix&    current,
				 const PointConfiguration& points,
				 const basis_type&         basis,
				 const parameter_type      start,
				 const parameter_type      step,
				 const bool                already_dependent) {
  static long count;
  if (already_dependent) {
    for (parameter_type i = start; i < _no - _rank + step + 1; ++i) {
      basis_type newbasis(basis);
      newbasis += i;
      if (step + 1 == _rank) {
	(*this)[newbasis] = sign(ZERO);
	if (CommandlineOptions::verbose()) {
#ifdef MEGA_VERBOSE
	  std::cerr << "inserting sign for basis: " << basis << std::endl;
#endif
	  if (++count % 1000 == 0) {
#ifdef SUPER_VERBOSE
	    std::cerr << std::endl;
#endif
	    std::cerr << count << " signs computed so far." << std::endl;
	  }
	}
      }
      else {
	_recursive_chiro(current, points, newbasis, i + 1, step + 1, true);
      }
    }
  }
  else {
    for (parameter_type i = start; i < _no - _rank + step + 1; ++i) {
      StairCaseMatrix next = current;
      next.augment(points[i]);
      if (CommandlineOptions::debug()) {
	Matrix raw(current);
	raw.augment(points[i]);
 	std::cerr << "matrix with new column:" << std::endl;
	raw.pretty_print(std::cerr);
	std::cerr << std::endl;
	std::cerr << "new staircase matrix:" << std::endl;
	next.pretty_print(std::cerr);
	std::cerr << std::endl;
      }
      basis_type newbasis(basis);
      newbasis += i;
      if (step + 1 == _rank) {
#ifdef SUPER_VERBOSE
	std::cerr << det(next) << ',';
#endif
	if (CommandlineOptions::debug()) {
	  std::cerr << "determinant at leaf: " << det(next) << std::endl;
	}
	(*this)[newbasis] = sign(det(next));
	if (CommandlineOptions::verbose()) {
#ifdef MEGA_VERBOSE
	  std::cerr << "inserting sign for basis: " << basis << std::endl;
#endif
	  if (++count % 1000 == 0) {
#ifdef SUPER_VERBOSE
	    std::cerr << std::endl;
#endif
	    std::cerr << count << " signs computed so far." << std::endl;
	  }
	}
      }
      else {
	if (next.has_full_rank()) {
	  _recursive_chiro(next, points, newbasis, i + 1, step + 1, false);
	}
	else {	  
#ifdef SUPER_VERBOSE
	  std::cerr << "premature dependence found in corank " << _rank - step << std::endl;
#endif
	  if (CommandlineOptions::debug()) {
	    std::cerr << "deadend because of dependency." << std::endl;
	  }
	  _recursive_chiro(next, points, newbasis, i + 1, step + 1, true);
	}
      }
    }
  }
}

// eof RealChiro.cc