File: Matrix.hh

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 (282 lines) | stat: -rw-r--r-- 7,698 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
////////////////////////////////////////////////////////////////////////////////
// 
// Matrix.hh 
//
//    produced: 21/08/97 jr
// last change: 30/10/97 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef MATRIX_HH
#define MATRIX_HH

#include <assert.h>
#include <iostream>
#include <limits>

#include "Permutation.hh"

#include "CommandlineOptions.hh"

#include "Vector.hh"
#include "IntegerSet.hh"
#include "IntegerSet64.hh"

#ifndef STL_CONTAINERS
#include "Array.hh"
namespace topcom {
  typedef Array<Vector> matrix_data;
};
#else
#include <vector>
namespace topcom {
  typedef std::vector<Vector> matrix_data;
}
#endif

namespace topcom {

  class Matrix : public matrix_data {
  private:
    static const char start_char;
    static const char end_char;
    static const char delim_char;
  public:
    static const char col_delim_char;
  public:
    static const parameter_type col_capacity;
  public:

    // constructors:
    inline Matrix();
    inline Matrix(const Matrix&);
    inline Matrix(Matrix&&);
    inline Matrix(const Matrix&,
		  const parameter_type min_col,
		  const parameter_type max_col);
    inline Matrix(const Matrix&,
		  const IntegerSet&);
    inline Matrix(const Matrix&,
		  const IntegerSet64&);
    inline Matrix(const Vector&);
    inline Matrix(const parameter_type,
		  const parameter_type,
		  const Field& init_entry = FieldConstants::ZERO);
    inline Matrix(const Permutation& perm);

    // destructor:
    inline ~Matrix();

    // assignment:
    inline Matrix& operator=(const Matrix&);
    inline Matrix& operator=(Matrix&&);

    // accessors:
    inline const parameter_type rowdim() const;
    inline const parameter_type coldim() const;
    const        Vector row(const parameter_type) const;

    // resizing:
    void        row_resize(const parameter_type, const Field& init_entry = FieldConstants::ZERO);
    inline void col_resize(const parameter_type, const Field& init_entry = FieldConstants::ZERO);

    // indexing:
    inline       Vector& col(const parameter_type);
    inline const Vector& col(const parameter_type) const;
    inline       Field& operator()(const parameter_type, const parameter_type);
    inline const Field& operator()(const parameter_type, const parameter_type) const;

    // in place operations:
    Matrix& canonicalize();
    Matrix& add(const Matrix&);
    Matrix& scale(const Field&);
    Matrix& augment(const Matrix&, const IntegerSet& = IntegerSet());
    Matrix& stack(const Matrix&);
    Matrix& swap_cols(const parameter_type, const parameter_type);
    Matrix& swap_rows(const parameter_type, const parameter_type);
    Matrix& row_echelon_form(const parameter_type start_row = 0, const parameter_type start_col = 0, const Field& scale = 1);

    // out of place operations:
    const        Field left_upper_det() const;
    inline const Field det() const;
    Matrix transpose() const;
    friend const Matrix multiply(const Matrix&, const Matrix&);

    // transformation:
    Vector StackOfAllColumns() const;

    // keys for containers:
    inline const size_type keysize() const;
    inline const size_type key(const size_type) const;

    // iostream:
    std::ostream& pretty_print(std::ostream& ost) const;

    // overload of istream with canonicalize:
    std::istream& read(std::istream&);
    inline friend std::istream& operator>>(std::istream&, Matrix&);
    std::ostream& write(std::ostream&) const;
    inline friend std::ostream& operator<<(std::ostream&, const Matrix&);
  };

  inline Matrix::Matrix() : matrix_data() {
    reserve(col_capacity);
  }

  inline Matrix::Matrix(const Matrix& matrix) : matrix_data(matrix) {}

  inline Matrix::Matrix(Matrix&& matrix) : matrix_data(std::move(matrix)) {}

  inline Matrix::Matrix(const Matrix& matrix, parameter_type min_col, parameter_type max_col) :
#ifndef STL_CONTAINERS
    matrix_data(matrix, min_col, max_col)
#else
    matrix_data(matrix.begin() + std::min<parameter_type>(min_col, matrix.coldim()),
		matrix.begin() + std::min<parameter_type>(max_col, matrix.coldim()))
#endif
  {}

  inline Matrix::Matrix(const Matrix& matrix, const IntegerSet& colsubset) :
    matrix_data() {
    parameter_type colidx = 0;
    const parameter_type m_coldim = matrix.coldim();
    for (IntegerSet::const_iterator iter = colsubset.begin();
	 iter != colsubset.end();
	 ++iter) {
      if (*iter < m_coldim) {
	this->push_back(matrix.at(*iter));
	++colidx;
      }
      else {
	break;
      }
    }
  }

  inline Matrix::Matrix(const Matrix& matrix, const IntegerSet64& colsubset) :
    matrix_data() {
    parameter_type colidx = 0;
    const parameter_type m_coldim = matrix.coldim();
    for (IntegerSet64::const_iterator iter = colsubset.begin();
	 iter != colsubset.end();
	 ++iter) {
      if (*iter < m_coldim) {
	this->push_back(matrix.at(*iter));
	++colidx;
      }
      else {
	break;
      }
    }
  }

  inline Matrix::Matrix(const Vector& vector) : matrix_data(1, vector) {
  
  }
  
  inline Matrix::Matrix(const parameter_type init_rowdim, 
			const parameter_type init_coldim, 
			const Field& init_entry) : 
    matrix_data(init_coldim) {
    for (parameter_type i = 0; i < init_coldim; ++i) {
      this->col(i) = Vector(init_rowdim, init_entry);
    }
  }
  
  inline Matrix::Matrix(const Permutation& perm) : matrix_data(perm.n(), Vector(perm.k(), FieldConstants::ZERO)) {
    for (parameter_type i = 0; i < perm.k(); ++i) {
      (*this)(i, perm[i]) = FieldConstants::ONE;
    }
  }

  inline Matrix::~Matrix() {
#ifdef CONSTRUCTOR_DEBUG
    std::cerr << "destroying Matrix " << *this << std::endl;
#endif
  }

  // assignment:
  inline Matrix& Matrix::operator=(const Matrix& matrix) {
    if (this == &matrix) {
      return *this;
    }
    matrix_data::operator=(matrix);
    return *this;
  }

  inline Matrix& Matrix::operator=(Matrix&& matrix) {
    if (this == &matrix) {
      return *this;
    }
    matrix_data::operator=(std::move(matrix));
    return *this;
  }

  inline const parameter_type Matrix::rowdim() const {
    if (size() == 0) {
      return 0;
    }
    else {
      return this->col(0).size();
    }
  }
  inline const parameter_type Matrix::coldim() const {
    return size();
  }

  inline void Matrix::col_resize(const parameter_type new_memsize, const Field& init_entry) {
    matrix_data::resize(new_memsize, Vector(rowdim(), init_entry));
  }

  inline Vector& Matrix::col(const parameter_type index){
    return this->at(index);
  }

  inline const Vector& Matrix::col(const parameter_type index) const {
    return this->at(index);
  }

  inline Field& Matrix::operator()(const parameter_type row, const parameter_type col) {
    return this->at(col).at(row);
  }

  inline const Field& Matrix::operator()(const parameter_type row, const parameter_type col) const {
    return this->at(col).at(row);
  }

  inline const Field Matrix::det() const {
#ifdef INDEX_CHECK
    assert(coldim() == rowdim());
#endif
    return this->left_upper_det();
  }

  // keys for containers:
  inline const size_type Matrix::keysize() const {
    return (size_type)coldim() * (size_type)rowdim();
  }

  inline const size_type Matrix::key(const size_type n) const {
    return hash<Field>()((*this)(n % (size_type)coldim(), n / (size_type)rowdim()));
  }

  inline std::istream& operator>>(std::istream& ist, Matrix& m) {
    m.read(ist);
    return ist;
  }

  inline std::ostream& operator<<(std::ostream& ost, const Matrix& m) {
    m.write(ost);
    return ost;
  }

  // special matrices:
  inline Matrix identity_matrix(const parameter_type dim) {
    return Matrix(Permutation(dim, dim));
  }


}; // namespace topcom

#endif

// eof Matrix.hh