File: StairCaseMatrix.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 (282 lines) | stat: -rw-r--r-- 9,488 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
////////////////////////////////////////////////////////////////////////////////
// 
// StairCaseMatrix.cc
//
//    produced: 13/03/98 jr
// 
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>

#include "StairCaseMatrix.hh"

namespace topcom {

  void StairCaseMatrix::_eliminate(const parameter_type ridx, const parameter_type cidx, const parameter_type cno) {
    if (cno == 0) {
      return;
    }
    const parameter_type n = rowdim();
  
    if (is_zero((*this)(ridx,ridx))) {
#ifdef SUPER_VERBOSE
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "eraser = " << (*this)(ridx,ridx) << " == FieldConstants::ZERO -> try colperm; row : " << ridx << ", col: " << ridx << std::endl;
      MessageStreams::debug() << message::unlock;
#endif
      for (parameter_type k = cidx; k < cidx + cno; ++k) {
	if (!is_zero((*this)(ridx,k))) {
	  Matrix::swap_cols(ridx,k);
	  _coefficient *= FieldConstants::MINUSONE;
#ifdef SUPER_VERBOSE
	  MessageStreams::debug() << message::lock;
	  MessageStreams::debug() << "eraser = " << (*this)(ridx,ridx) << " == FieldConstants::ZERO -> colperm successful; row : " << ridx << ", col: " << ridx << std::endl;
	  MessageStreams::debug() << message::unlock;
#endif
	  break;
	}
      }
      if (is_zero((*this)(ridx,ridx))) {
#ifdef SUPER_VERBOSE
	MessageStreams::debug() << message::lock;
	MessageStreams::debug() << "eraser = " << (*this)(ridx,ridx) << " == FieldConstants::ZERO -> colperm unsuccessful; row : " << ridx << ", col: " << ridx << std::endl;
	MessageStreams::debug() << message::unlock;
#endif
	return;
      }
#ifdef SUPER_VERBOSE
      else {
	MessageStreams::debug() << message::lock;
	MessageStreams::debug() << "eraser = " << (*this)(ridx,ridx) << " != FieldConstants::ZERO; row : " << ridx << ", col: " << ridx << std::endl;
	MessageStreams::debug() << message::unlock;
      }
#endif
    }
#ifdef SUPER_VERBOSE
    else {
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "eraser = " << (*this)(ridx,ridx) << " != FieldConstants::ZERO; row : " << ridx << ", col: " << ridx << std::endl;
      MessageStreams::debug() << message::unlock;
    }
#endif
    const Field& eraser = (*this)(ridx,ridx);
#ifdef SUPER_VERBOSE
    MessageStreams::debug() << message::lock;
    MessageStreams::debug() << "eraser = " << eraser << "; row : " << ridx << ", col: " << ridx << std::endl;
    MessageStreams::debug() << message::unlock;
#endif
    for (parameter_type j = cidx; j < cidx + cno; ++j) {
      const Field& delinquent = (*this)(ridx,j);
#ifdef SUPER_VERBOSE
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "delinquent = " << delinquent << "; row : " << ridx << ", col: " << j << std::endl;
      MessageStreams::debug() << message::unlock;
#endif
      if (is_zero(delinquent)) {
	continue;
      }
      // An alternative division-free elimination step (slightly slower for large numbers):
      // if (eraser < FieldConstants::ZERO) {
      //   _coefficient /= -eraser;
      //   for (parameter_type k = ridx + 1; k < n; ++k) {
      // 	(*this)(k,j) *= -eraser;
      // 	(*this)(k,j) += delinquent * (*this)(k,ridx);
      //   }
      // }
      // else {
      //   _coefficient /= eraser;
      //   for (parameter_type k = ridx + 1; k < n; ++k) {
      // 	(*this)(k,j) *= eraser;
      // 	(*this)(k,j) -= delinquent * (*this)(k,ridx);
      //   }
      // }
      const Field multiplier = delinquent / eraser;
      for (parameter_type k = ridx + 1; k < n; ++k) {
	(*this)(k,j) -= multiplier * (*this)(k,ridx);
      }
      (*this)(ridx,j) = FieldConstants::ZERO;
    }
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "after step " << ridx << " of stair case transformation: " << std::endl;
      (*this).pretty_print(MessageStreams::debug());
      MessageStreams::debug() << message::unlock;
    }
#endif  
  }

  StairCaseMatrix& StairCaseMatrix::augment(const Matrix& matrix, const IntegerSet& ignored_cols) {

    // the resulting matrix will have matrix.rowdim() rows;
    // note that (*this) may be empty resulting in rowdim() = 0:
    if (matrix.coldim() == 0) {
      return *this;
    }
    if (empty()) {
      parameter_type j = 0;
      while(j < matrix.coldim()) {
	if (!ignored_cols.contains(j)) {
	  this->push_back(matrix.col(j));
	  return this->augment(matrix, ignored_cols + j);
	}
	else {
	  ++j;
	}
      }
      return *this;
    }
#ifdef INDEX_CHECK
    if (coldim() > 0) {
      assert(rowdim() == matrix.rowdim());
    }
#endif
    const parameter_type m = matrix.rowdim();

    // so many columns are already in stair-case form:
    const parameter_type n = coldim();

    // augment the matrix without elimination:
    Matrix::augment(matrix, ignored_cols);

    // so many columns must be in stair-case form after the transformations:
    const parameter_type n_new = coldim();

    // cnt1 many column elimination are needed with old pivot columns:
    const parameter_type cnt1 = std::min<parameter_type>(n, m);

    // cnt2 - cnt1 many column eliminations are needed with already modified new columns:
    const parameter_type cnt2 = std::min<parameter_type>(n_new, m);
  
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "before stair case transformation:" << std::endl;
      pretty_print(MessageStreams::debug());
      MessageStreams::debug() << message::unlock;
    }
#endif
    
    // eliminate new columns using all old columns:
    for (parameter_type i = 0; i < cnt1; ++i) {

      // use pivot (i, i) to eliminate (n_new - n) many columns starting at column n:
      _eliminate(i, n, n_new - n);
#ifdef SUPER_VERBOSE
      if (CommandlineOptions::debug()) {
	MessageStreams::debug() << message::lock;
	MessageStreams::debug() << "after elimination with pivot " << i
				<< ", startcol " << n
				<< ", no " << n_new - n << ":" << std::endl;
	(*this).pretty_print(MessageStreams::debug());
	MessageStreams::debug() << message::unlock;
      }
#endif
    }
    
    // eliminate new columns using new columns:
    for (parameter_type i = cnt1; i < cnt2; ++i) {

      // use pivot (i, i) to eliminate (n_new - i - 1) many columns starting at column i + 1:
      _eliminate(i, i + 1, n_new - i - 1);
#ifdef SUPER_VERBOSE
      if (CommandlineOptions::debug()) {
	MessageStreams::debug() << message::lock;
	MessageStreams::debug() << "after elimination with pivot " << i
				<< ", startcol " << i + 1
				<< ", no " << n_new - i - 1 << ":" << std::endl;
	(*this).pretty_print(MessageStreams::debug());
	MessageStreams::debug() << message::unlock;
      }
#endif
    }    
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      MessageStreams::debug() << message::lock;
      MessageStreams::debug() << "after stair case transformation: " << std::endl;
      (*this).pretty_print(MessageStreams::debug());
    MessageStreams::debug() << message::unlock;
    }
#endif
    return *this;
  }

  // this special form of "det" computes the determinant of the left-top-most
  // square matrix:
  const Field StairCaseMatrix::left_upper_det() const {
    parameter_type n = coldim();
    if (coldim() > rowdim()) {
      n = rowdim();
    }
    Field result(FieldConstants::ONE);
    for (parameter_type i = 0; i < n; ++i) {
      result *= (*this)(i,i);
      if (is_zero(result)) {
	return result;
      }
    }
    return result * _coefficient;
  }

  const Field StairCaseMatrix::det() const {
#ifdef INDEX_CHECK
    assert(coldim() == rowdim());
#endif
    return left_upper_det();
  }

  Message& StairCaseMatrix::pretty_print(Message& msg) const {
    if (msg.is_active()) {
      msg << "matrix: " << '\n';
      Matrix::pretty_print(msg);
      msg << "coefficient: " << _coefficient << '\n';
    }
    return msg;
  }
  
  std::ostream& StairCaseMatrix::pretty_print(std::ostream& ost) const {
    Message msg(ost);
    pretty_print(msg);
    return ost;
  }
  
  std::istream& StairCaseMatrix::read(std::istream& ist) {
    char c;
    
    ist >> std::ws;
    if (!Matrix::read(ist)) {
      MessageStreams::forced() << message::lock;
      MessageStreams::forced() << "StairCaseMatrix::read(std::istream&): "
			       << "error while reading matrix - exiting" << std::endl;
      MessageStreams::forced() << message::unlock;
      exit(1);
    }
    if (!(ist >> std::ws >> c >> std::ws) || (c != ContainerChars::divide_char)) {
      MessageStreams::forced() << message::lock;
      MessageStreams::forced() << "StairCaseMatrix::read(std::istream&): "
			       << "expected `" << ContainerChars::divide_char
			       << "' instead of `" << c << "' - exiting" << std::endl;
      MessageStreams::forced() << message::unlock;
      exit(1);
    }
    if (!(ist >> _coefficient)) {
      MessageStreams::forced() << message::lock;
      MessageStreams::forced() << "StairCaseMatrix::read(std::istream&): "
			       << "error while reading _coefficient - exiting" << std::endl;
      MessageStreams::forced() << message::unlock;
      exit(1);
    }
    return ist;
  }
  std::ostream& StairCaseMatrix::write(std::ostream& ost) const {
    Matrix::write(ost);
    ost << ContainerChars::divide_char
	<< this->_coefficient;
    return ost;
  }
  

}; // namespace topcom

// eof StairCaseMatrix.cc