-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMatrix.h
290 lines (220 loc) · 5.59 KB
/
AMatrix.h
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
/* Matrix Template Class
$$$$$$$$$$$$$$$$$$$$$$$
$ Matrix.h - defs $
$$$$$$$$$$$$$$$$$$$$$$$
Copyright (C) 2011 W.B. Yates
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
History:
*/
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <iostream>
#include <vector>
template <typename T>
class Matrix
{
public:
Matrix( void ): m_rows(0), m_cols(0), m_rawData() {}
Matrix( int r, int c, const T &x = T() ): m_rows(r), m_cols(c), m_rawData(r, std::vector<T>(c, x)) {}
explicit Matrix( const std::vector<std::vector<T>>& values ): m_rows(0), m_cols(0), m_rawData()
{
setMatrix( values );
}
~Matrix( void ) { m_rows = 0; m_cols = 0; m_rawData.clear(); }
void
clear( void ) { m_rows = 0; m_cols = 0; m_rawData.clear(); }
Matrix<T>&
operator=( const T &a ); // assign a to every element
void
setMatrix( int r, int c )
{
m_rows = r;
m_cols = c;
m_rawData.resize( r, std::vector<T>(c, T()) );
}
void
setMatrix( const std::vector< std::vector<T> >& values )
{
m_rows = values.size();
m_cols = (m_rows) ? values[0].size() : 0;
m_rawData = values;
}
std::vector<T>&
operator[]( int i ) { return m_rawData[i]; } // return row i
const std::vector<T>&
operator[]( int i ) const { return m_rawData[i]; }
std::vector<T>
column( int colIdx ) const; // extract column copy c as a (row) vector
void
column( int colIdx, const std::vector<T>& c );
int rows( void ) const { return m_rows; }
int cols( void ) const { return m_cols; }
void
resize( int r, int c, const T& = T() ); // will preserve/truncate existing data accordingly
const std::vector< std::vector<T> >&
data( void ) const { return m_rawData; }
std::vector< std::vector<T> >&
data( void ) { return m_rawData; }
private:
int m_rows;
int m_cols;
std::vector< std::vector<T> > m_rawData;
};
template <class T>
std::ostream&
operator<<( std::ostream& ostr, const Matrix<T>& m );
template <class T>
std::istream&
operator>>( std::istream& istr, Matrix<T>& m );
template <class T>
std::ostream&
operator<<( std::ostream& ostr, const Matrix<T>& m )
{
ostr << m.rows() << ' ' << m.cols() << '\n';
for (int i = 0; i < m.rows(); ++i)
{
for (int j = 0; j < m.cols(); ++j)
{
ostr << m[i][j] << ' ';
}
ostr << '\n';
}
ostr << '\n';
return ostr;
}
template <class T>
std::istream&
operator>>( std::istream& istr, Matrix<T>& m )
{
int r = 0, c = 0;
istr >> r;
istr >> c;
m.resize(r,c);
for (int i = 0; i < m.rows(); ++i)
{
for (int j = 0; j < m.cols(); ++j)
{
istr >> m[i][j];
}
}
return istr;
}
template <class T>
void
Matrix<T>::resize( int r, int c, const T& v )
{
if (r == m_rows && c == m_cols)
return; // no resize needed
if (c != m_cols) // resize cols of existing rows
{
typename std::vector< std::vector<T> >::iterator end = m_rawData.end();
for (typename std::vector< std::vector<T> >::iterator i = m_rawData.begin(); i != end; ++i)
{
i->resize(c, v);
}
}
m_rows = r;
m_cols = c;
// add in extra rows
m_rawData.resize( r, std::vector<T>(c, v) );
}
template <class T>
Matrix<T>&
Matrix<T>::operator=( const T &a )
// assign a to every element
{
for (int i = 0; i< m_rows; ++i)
{
for (int j = 0; j < m_cols; ++j)
{
m_rawData[i][j] = a;
}
}
return *this;
}
// extract a copy of column c from matrix as a vector; use transpose(v) to construct a column vector (see below)
template <class T>
std::vector<T>
Matrix<T>::column( int colIdx ) const
{
assert(colIdx < m_cols);
std::vector<T> retVal(m_rows);
for (int j = 0; j < m_rows; ++j)
{
retVal[j] = m_rawData[j][colIdx];
}
return retVal;
}
template <class T>
void
Matrix<T>::column( int colIdx, const std::vector<T>& col )
{
assert(col.size() == m_rows && colIdx < m_cols);
for (int j = 0; j < m_rows; ++j)
{
m_rawData[j][colIdx] = col[j];
}
}
template <class T>
bool
operator!=( const Matrix<T> &m1, const Matrix<T> &m2 );
template <class T>
bool
operator==( const Matrix<T> &m1, const Matrix<T> &m2 );
template <class T>
bool
operator!=( const Matrix<T> &m1, const Matrix<T> &m2 )
{
assert((m1.rows() == m2.rows()) && (m1.cols() == m2.cols()));
for (int i = 0; i < m1.rows(); ++i)
{
for (int j = 0; j < m1.cols(); ++j)
{
if (m1[i][j] != m2[i][j])
return true;
}
}
return false;
}
template <class T>
bool
operator==( const Matrix<T> &m1, const Matrix<T> &m2 )
{
assert((m1.rows() == m2.rows()) && (m1.cols() == m2.cols()));
for (int i = 0; i < m1.rows(); ++i)
{
for (int j = 0; j < m1.cols(); ++j)
{
if (m1[i][j] != m2[i][j])
return false;
}
}
return true;
}
template <class T>
Matrix<T>
transpose( const Matrix<T>& m );
template <class T>
Matrix<T>
transpose( const Matrix<T>& m )
{
Matrix<T> retVal(m.cols(), m.rows());
for (int i = 0; i < retVal.rows(); ++i)
{
for (int j = 0; j < retVal.cols(); ++j)
{
retVal[i][j] = m[j][i];
}
}
return retVal;
}
#endif // __MATRIX_H__