File: Matrix.cc

package info (click to toggle)
topcom 1.1.2%2Bds-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,788 kB
  • sloc: cpp: 37,616; sh: 4,262; makefile: 497; ansic: 49
file content (378 lines) | stat: -rw-r--r-- 10,355 bytes parent folder | download | duplicates (2)
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
////////////////////////////////////////////////////////////////////////////////
// 
// Matrix.cc
//
//    produced: 13/03/98 jr
// last change: 13/03/98 jr
// 
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <ctype.h>
#include <string.h>
#include <iostream>

#include "Matrix.hh"

namespace topcom {

  const char Matrix::col_delim_char = '\t';

  const char Matrix::start_char = '[';
  const char Matrix::end_char   = ']';
  const char Matrix::delim_char = ',';

  const parameter_type Matrix::col_capacity = 0;

  const Vector Matrix::row(const parameter_type index) const {
    Vector result(coldim());
    for (parameter_type i = 0; i < coldim(); ++i) {
      result[i] = (*this)(index, i);
    }
    return result;
  }
  void Matrix::row_resize(const parameter_type new_memsize, const Field& init_entry) {
    for (parameter_type i = 0; i < coldim(); ++i) {
      this->col(i).resize(new_memsize,init_entry);
    }
  }
  Matrix& Matrix::canonicalize() {
    for (parameter_type i = 0; i < coldim(); ++i) {
      this->col(i).canonicalize();
    }
    return *this;  
  }

  Matrix& Matrix::add(const Matrix& matrix) {
#ifdef INDEX_CHECK
    assert((rowdim() == matrix.rowdim()) && (coldim() == matrix.coldim()));
#endif
    for (parameter_type i = 0; i < coldim(); ++i) {
      this->col(i).add(matrix.col(i));
    }
    return *this;
  }
  Matrix& Matrix::scale(const Field& entry) {
    for (parameter_type i = 0; i < coldim(); ++i) {
      this->col(i).scale(entry);
    }
    return *this;
  }

  Matrix& Matrix::augment(const Matrix& matrix, const IntegerSet& ignored_cols) {
#ifdef INDEX_CHECK
    if (coldim() > 0) {
      assert(rowdim() == matrix.rowdim());
    }
#endif
#ifndef STL_CONTAINERS  
    push_back(matrix);
#else
    // reserve(coldim() + matrix.coldim());
    for (parameter_type col = 0; col < matrix.coldim(); ++col) {
      if (ignored_cols.contains(col)) {
	continue;
      }
      push_back(matrix.col(col));
    }
#endif
    return *this;
  }

  Matrix& Matrix::stack(const Matrix& matrix) {
#ifdef INDEX_CHECK
    assert(coldim() == matrix.coldim());
#endif
    for (parameter_type i = 0; i < coldim(); ++i) {
      this->col(i).stack(matrix.col(i));
    }
    return *this;
  }

  Matrix& Matrix::swap_cols(const parameter_type i, const parameter_type j) {
#ifdef INDEX_CHECK
    assert((i < coldim()) && (j < coldim()));
#endif
    this->col(i).swap(this->col(j));
    // const Vector tmp = (*this)[i];
    // (*this)[i] = (*this)[j];
    // (*this)[j] = tmp;
    return *this;
  }
  
  Matrix& Matrix::swap_rows(const parameter_type i, const parameter_type j) {
#ifdef INDEX_CHECK
    assert((i < rowdim()) && (j < rowdim()));
#endif
    for (parameter_type k = 0; k < coldim(); ++k) {
      std::swap((*this)(i, k), (*this)(j, k));
    }
    return *this;
  }
  
  Matrix& Matrix::row_echelon_form(const parameter_type start_row, const parameter_type start_col, const Field& scale) {
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      std::cerr << "calling elimination of" << std::endl;
      this->pretty_print(std::cerr);
      std::cerr << "below row " << start_row << " to the right of column " << start_col << std::endl;
    }
#endif

    // swap row with left-most non-zero to the top at start_row:
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      std::cerr << "swapping left-most non-zero to the top ..." << std::endl;
    }
#endif
    if ((start_row == rowdim()) || (start_col == coldim())) {
      return *this;
    }
    parameter_type minnonzerocol(coldim());
    parameter_type minnonzerocolrow(rowdim());
    Field new_scale(scale);
    for (parameter_type i = start_row; i < rowdim(); ++i) {
      for (parameter_type j = start_col; j < coldim(); ++j) {
	if (((*this)(i, j) != FieldConstants::ZERO) && (j < minnonzerocol)) {
	  minnonzerocol = j;
	  minnonzerocolrow = i;
	}
      }
    }
    if (minnonzerocolrow != start_row) {
#ifdef SUPER_VERBOSE
      if (CommandlineOptions::debug()) {
	std::cerr << "... by swapping row " << start_row << " and row " << minnonzerocolrow << " ..." << std::endl;
      }
#endif
      swap_rows(minnonzerocolrow, start_row);
      new_scale = new_scale * FieldConstants::MINUSONE;
    }
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      this->pretty_print(std::cerr);
      std::cerr << "... done." << std::endl;
    }
#endif
  
    // eliminate under left-most non-zero:
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      std::cerr << "eliminating all non-zeros below element at index (" << start_row << "," << minnonzerocol << ") ..." << std::endl;
    }
#endif
    const Field eraser((*this)(start_row, minnonzerocol));
    for (parameter_type i = start_row + 1; i < rowdim(); ++i) {
      if ((*this)(i, minnonzerocol) != FieldConstants::ZERO) {
#ifdef SUPER_VERBOSE
	if (CommandlineOptions::verbose()) {
	  std::cerr << "\t eliminating row " << i << " ..." << std::endl;
	}
#endif
	const Field delinquent((*this)(i, minnonzerocol));
	const Field multiplier = delinquent / eraser;

	for (parameter_type j = minnonzerocol; j < coldim(); ++j) {
	  (*this)(i,j) -= multiplier * (*this)(start_row,j);
	}
#ifdef SUPER_VERBOSE
	if (CommandlineOptions::verbose()) {
	  this->pretty_print(std::cerr);
	  std::cerr << "\t ... done." << std::endl;
	}
#endif
      }
    }
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      this->pretty_print(std::cerr);
      std::cerr << "... done." << std::endl;
    }
#endif

    // delete trailing zero rows below start_row:
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      std::cerr << "deleting all-zero rows ..." << std::endl;
    }
#endif
    while (row(rowdim() - 1).is_zero()) {
#ifdef SUPER_VERBOSE
      if (CommandlineOptions::debug()) {
	std::cerr << "\t deleting row " << rowdim() - 1 << " ..." << std::endl;
      }
#endif
      row_resize(rowdim() - 1);
    }
#ifdef SUPER_VERBOSE
    if (CommandlineOptions::debug()) {
      this->pretty_print(std::cerr);
      std::cerr << "... done." << std::endl;
    }
#endif
    return row_echelon_form(start_row + 1, minnonzerocol + 1, new_scale);
  }

  const Field Matrix::left_upper_det() const {
    parameter_type n = coldim();
    if (coldim() > rowdim()) {
      n = rowdim();
    }
    Matrix tmp(*this);
    tmp.resize(n);
    Field scale(FieldConstants::ONE);
    for (parameter_type i = 0; i < n - 1; ++i) {
      if (tmp(i,i) == FieldConstants::ZERO) {
	for (parameter_type k = i + 1; k < n; ++k) {
	  if (tmp(i,k) != FieldConstants::ZERO) {
	    tmp.swap_cols(i,k);
	    scale *= -1;
	    continue;
	  }
	}
	if (tmp(i,i) == FieldConstants::ZERO) {
	  return FieldConstants::ZERO;
	}
      }
      Field eraser = tmp(i,i);
#ifdef SUPER_VERBOSE
      std::cerr << "eraser = " << eraser << std::endl;
#endif
      for (parameter_type j = i + 1; j < n; ++j) {
	Field delinquent = tmp(i,j);
	if (delinquent == FieldConstants::ZERO) {
	  continue;
	}
#ifdef SUPER_VERBOSE
	std::cerr << "delinquent = " << delinquent << std::endl;
#endif
	for (parameter_type k = i + 1; k < n; ++k) {
	  tmp(k,j) -= (tmp(k,i) * delinquent / eraser);
	}
	tmp(i,j) = FieldConstants::ZERO;
      }
#ifdef SUPER_VERBOSE
      std::cerr << "current tmp = " << std::endl;
      tmp.pretty_print(std::cerr);
#endif
    }
#ifdef SUPER_VERBOSE
    std::cerr << "Triangular transformation is:" << std::endl;
    tmp.pretty_print(std::cerr);
    std::cerr << "Scale factor is " << scale << std::endl;
#endif
    Field result(FieldConstants::ONE);
    for (parameter_type i = 0; i < n; ++i) {
      result *= tmp(i,i);
    }
#ifdef SUPER_VERBOSE
    std::cerr << "Triangular determinant is " << result << std::endl;
#endif
    return result * scale;
  }

  Matrix Matrix::transpose() const {
    Matrix result(coldim(), rowdim());
    for (parameter_type i = 0; i < coldim(); ++i) {
      for (parameter_type j = 0; j < rowdim(); ++j) {
	result(i, j) = (*this)(j, i);
      }
    }
    return result;
  }
  
  const Matrix multiply(const Matrix& matrix1, const Matrix& matrix2) {
#ifdef INDEX_CHECK
    assert(matrix1.coldim() == matrix2.rowdim());
#endif
    Matrix result(matrix1.rowdim(), matrix2.coldim());
    for (parameter_type i = 0; i < matrix1.rowdim(); ++i) {
      const Vector row = matrix1.row(i);
      for (parameter_type j = 0; j < matrix2.coldim(); ++j) {
	result(i, j) = inner_product(row, matrix2.col(j));
      }
    }
    return result;
  }

  // transformation:
  Vector Matrix::StackOfAllColumns() const {
    Vector result;
    for (parameter_type j = 0; j < this->coldim(); ++j) {
      result.stack(this->col(j));
    }
    return result;
  }

  std::ostream& Matrix::pretty_print(std::ostream& ost) const {
    for (parameter_type i = 0; i < rowdim(); ++i) {
      const Vector row_vector = row(i);
      for (parameter_type j = 0; j < coldim(); ++j) {
	ost << row_vector(j) << col_delim_char;
      }
      ost << std::endl;
    }
    return ost;
  }

  std::istream& Matrix::read(std::istream& ist) {
    if (CommandlineOptions::debug()) {
      std::cerr << "Matrix::read(std::istream& ist): "
		<< "current matrix: " << *this << std::endl;
    }
  
    char c;
    Vector column;

    matrix_data::clear();
    ist >> std::ws >> c;
    if (c == start_char) {
      while (ist >> std::ws >> c) {
	if (c == end_char) {
	  break;
	}
	if (c == delim_char) {
	  continue;
	}
	ist.putback(c);
	if (ist >> column) {
	  matrix_data::push_back(column);	
	}
	else {
	  if (CommandlineOptions::debug()) {
	    std::cerr << "Matrix::read(std::istream& ist):"
		      << c << " not of appropriate type." << std::endl;
	  }
	  ist.clear(std::ios::failbit);
	  return ist;
	}
      }
    }
    else {
      if (CommandlineOptions::debug()) {
	std::cerr << "Matrix::read(std::istream& ist):"
		  << " missing `" << start_char << "'." << std::endl;
      }
      ist.clear(std::ios::failbit);
      return ist;
    }
    ist.clear(std::ios::goodbit);
    return ist;
  }

  std::ostream& Matrix::write(std::ostream& ost) const {
    ost << start_char;
    if (coldim() > 0) {
      for (parameter_type i = 0; i < coldim() - 1; ++i) {
	ost << (*this)[i] << delim_char;
      }
      if (coldim() > 0) {
	ost << (*this)[coldim() - 1];
      }
    }
    ost << end_char;
    return ost;
  }

}; // namespace topcom

// eof Matrix.cc