File: RealChiro.cc

package info (click to toggle)
topcom 1.2.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,596 kB
  • sloc: cpp: 40,956; sh: 4,663; makefile: 679; ansic: 55
file content (437 lines) | stat: -rw-r--r-- 13,574 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
////////////////////////////////////////////////////////////////////////////////
// 
// RealChiro.cc
//
//    produced: 13 Mar 1998 jr
// 
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#include "RealChiro.hh"

namespace topcom {

  const char    RealChiro::start_char = '[';
  const char    RealChiro::end_char   = ']';
  const char    RealChiro::delim_char = ',';
  const char*   RealChiro::map_chars = "->";

  RealChiro::RealChiro(const PointConfiguration& points, bool preprocess) : 
    chirotope_data(), _no(points.coldim()), _rank(points.rowdim()) {
    _recursive_chiro(PointConfiguration(), points, basis_type(), 0, 0, false);
    _has_dets = true;
    MessageStreams::verbose() << message::lock
			      << '\n'
			      << size() << " signs in total." << std::endl
			      << message::unlock;
  }

  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.push_back(lex_extension_perm[i]);
      int perm_sign = basis_perm.sign();
      return perm_sign * basis_sign;
    }
    return 0;
  }
  // functions:
  void RealChiro::erase_random() {
#ifdef TOPCOM_CHIROTOPE
    chirotope_data::erase_random();
#else
    chirotope_data::iterator iter = this->begin();
    chirotope_data::erase(iter);
#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 TOPCOM_CHIROTOPE
      const basis_type basis = iter->key();
      const int basis_sign = iter->data().first;
#else
      const basis_type basis = iter->first;
      const int basis_sign = iter->second.first;
#endif
      const basis_type dualbasis(groundset - basis);
      Permutation perm(dualbasis);
      perm.push_back(Permutation(basis));
      int perm_sign = perm.sign(result._rank);
      result[dualbasis] = std::pair<int, Field>(perm_sign * basis_sign, Field(perm_sign * basis_sign));
#ifdef COMPUTATIONS_DEBUG
      if (perm.sign() != perm_sign) {
	MessageStreams::forced() << message::lock
				 << "RealChiro RealChiro::dual() const: "
				 << "perm_sign error - exiting" << std::endl
				 << message::unlock;
	exit(1);
      }
#endif
#ifdef SUPER_VERBOSE
      MessageStreams::debug() << message::lock
			      << perm_sign * (*this)(basis) << ','
			      << message::unlock;
#endif
      if (++count % CommandlineOptions::report_frequency() == 0) {
#ifdef SUPER_VERBOSE
	MessageStreams::debug() << message::lock
				<< std::endl
				<< message::unlock;
#endif
	MessageStreams::verbose() << message::lock
				  << count << " signs computed so far." << std::endl
				  << message::unlock;
      }
    }
    MessageStreams::verbose() << message::lock
			      << size() << " signs in total." << std::endl
			      << message::unlock;
    return result;
  }

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

  Message& RealChiro::print_dualstring(Message& msg) const {
    static long count;
    const basis_type groundset(0, _no);
    msg << _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.push_back(primalperm);
      const int perm_sign = perm.sign(_no - _rank);
      const int chiro_on_perm = perm_sign * (*this)(basis);
      msg << int2sign(chiro_on_perm);
      if (++count % 100 == 0) {
	msg << '\n';
      }
#ifdef COMPUTATIONS_DEBUG
      if (perm.sign() != perm_sign) {
	MessageStreams::forced() << "RealChiro RealChiro::print_dualstring() const: "
				 << "perm_sign error - exiting" << std::endl;
	exit(1);
      }
#endif
#ifdef SUPER_VERBOSE
      MessageStreams::debug() << perm_sign * (*this)(basis) << ',';
#endif
      if (count % CommandlineOptions::report_frequency() == 0) {
#ifdef SUPER_VERBOSE
	MessageStreams::debug() << std::endl;
#endif
	MessageStreams::verbose() << count << " signs computed so far." << std::endl;
      }
    } while (dualperm.lexnext());
    msg << std::endl;
    MessageStreams::verbose() << count << " signs in total." << std::endl;
    return msg;
  }

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

    clear();
    if (!(ist >> std::ws >> _no)) {
#ifdef READ_DEBUG
      MessageStreams::forced() << "RealChiro::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
      MessageStreams::forced() << "RealChiro::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
      MessageStreams::forced() << "RealChiro::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
      MessageStreams::forced() << "RealChiro::read_string(std::istream&): "
			       << "separator not found." << std::endl;
#endif
      ist.clear(std::ios::failbit);
      return ist;
    }
#ifdef MAXNOIS64
    if (_no > block_len) {
      MessageStreams::forced() << "RealChiro::read(std::istream& ist): " << std::endl
			       << "Code was compiled for at most "
			       << block_len << " elements, " << std::endl
			       << "but the input chirotope has "
			       << no() << " elements." << std::endl;
      exit(1);
    }
#endif

    if (_rank > _no) {
#ifdef READ_DEBUG
      MessageStreams::forced() << "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) {
	const int new_val = sign2int(c);
	(*this)[basis_type(perm)] = std::pair<int, Field>(new_val, Field(new_val));
      }
      else {
#ifdef READ_DEBUG
	MessageStreams::forced() << "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 = 0;
    if (already_dependent) {
      for (parameter_type i = start; i < _no - _rank + step + 1; ++i) {
	basis_type newbasis(basis);
	newbasis += i;
	if (step + 1 == _rank) {
	  const Field next_det = FieldConstants::ZERO;
	  (*this)[newbasis] = std::pair<int, Field>(0, FieldConstants::ZERO);
#ifdef MEGA_VERBOSE
	  MessageStreams::debug() << "inserting sign for basis: " << basis << std::endl;
#endif
	  if (++count % CommandlineOptions::report_frequency() == 0) {
#ifdef SUPER_VERBOSE
	    MessageStreams::debug() << std::endl;
#endif
	    MessageStreams::verbose() << 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]);
	  MessageStreams::debug() << "matrix with new column:" << std::endl;
	  raw.pretty_print(MessageStreams::debug());
	}
	MessageStreams::debug() << std::endl;
	MessageStreams::debug() << "new staircase matrix:" << std::endl;
	next.pretty_print(MessageStreams::debug());
	MessageStreams::debug() << std::endl;
	basis_type newbasis(basis);
	newbasis += i;
	if (step + 1 == _rank) {
#ifdef SUPER_VERBOSE
	  MessageStreams::debug() << next.det() << ',';
#endif
	  const Field next_det = next.det();
	  MessageStreams::debug() << "determinant at leaf: " << next_det << std::endl;
	  (*this)[newbasis] = std::pair<int, Field>(sign(next_det), next_det);
#ifdef MEGA_VERBOSE
	  MessageStreams::debug() << "inserting sign for basis: " << basis << std::endl;
#endif
	  if (++count % CommandlineOptions::report_frequency() == 0) {
#ifdef SUPER_VERBOSE
	    MessageStreams::debug() << std::endl;
#endif
	    MessageStreams::verbose() << count << " signs computed so far." << std::endl;
	  }
	}
	else {
	  if (next.has_no_zerocol()) {
	    _recursive_chiro(next, points, newbasis, i + 1, step + 1, false);
	  }
	  else {	  
#ifdef SUPER_VERBOSE
	    MessageStreams::debug() << "premature dependence found in corank " << _rank - step << std::endl;
#endif
	    MessageStreams::debug() << "deadend because of dependency." << std::endl;
	    _recursive_chiro(next, points, newbasis, i + 1, step + 1, true);
	  }
	}
      }
    }
  }

  // stream input/output:
  std::istream& RealChiro::read(std::istream& ist) {
    char dash;
    char arrow;
    char opening_bracket;
    char closing_bracket;
    char comma;

    basis_type basis_reader;
    int        int_reader;
    Field      field_reader;

    char c;

    clear();
    if (!(ist >> std::ws >> _no >> std::ws >> c >> std::ws >> _rank >> std::ws >> c >> std::ws >> c)) {
      MessageStreams::forced() << "std::istream& RealChiro::read(std::istream&): "
			       << "`no, rank:' not found." << std::endl;
      ist.clear(std::ios::failbit);
      return ist;
    }
    if (c == start_char) {
      while (ist >> std::ws >> c) {
	if (c == end_char) {
	  break;
	}
	if (c == delim_char) {
	  continue;
	}
	ist.putback(c);
	if (!(ist >> std::ws >> basis_reader)) {
	  MessageStreams::forced() << "std::istream& operator>>(std::istream&, chirotope_data&): "
				   << "key not found." << std::endl;
	  ist.clear(std::ios::failbit);
	  return ist;
	}
	if (!(ist >> std::ws >> dash >> arrow)) {
	  MessageStreams::forced() << "std::istream& operator>>(std::istream&, chirotope_data&): "
				   << "`" << map_chars << "' not found." << std::endl;
	  ist.clear(std::ios::failbit);
	  return ist;
	}
	if (!(ist >> std::ws >> opening_bracket >> int_reader)) {
	  MessageStreams::forced() << "std::istream& operator>>(std::istream&, chirotope_data&): "
				   << "data not found." << std::endl;
	  ist.clear(std::ios::failbit);
	  return ist;
	}
	if (!(ist >> std::ws >> comma >> field_reader >> closing_bracket)) {
	  MessageStreams::forced() << "std::istream& operator>>(std::istream&, chirotope_data&): "
				   << "data not found." << std::endl;
	  ist.clear(std::ios::failbit);
	  return ist;
	}
#ifdef TOPCOM_CHIROTOPE
	insert(basis_reader, Pair<int, Field>(int_reader, field_reader));
#else
	insert(std::pair<basis_type, std::pair<int, Field> >(basis_reader, std::pair<int, Field>(int_reader, field_reader)));
#endif
      }
    }
    else {
      MessageStreams::forced() << "std::istream& operator>>(std::istream&, chirotope_data&): "
			       << "missing `" << start_char << "'." << std::endl;
      ist.clear(std::ios::failbit);
      return ist;
    }
    ist.clear(std::ios::goodbit);
    return ist;
  }

  std::ostream& RealChiro::write(std::ostream& ost) const {
    ost << no() << ',' << rank() << ':' << start_char;
    if (!empty()) {
      chirotope_data::const_iterator iter = begin();
#ifdef TOPCOM_CHIROTOPE
      ost << iter->key() << map_chars << iter->data();
#else
      ost << iter->first << map_chars << iter->second;
#endif
      while (++iter != end()) {
#ifdef TOPCOM_CHIROTOPE
	ost << delim_char << iter->key() << map_chars << iter->data();
#else
	ost << delim_char << iter->first << map_chars << iter->second;
#endif
      }
    }
    ost << end_char;
    return ost;
  }

}; // namespace topcom

// eof RealChiro.cc