1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: ftmpl_matrix.cc,v 1.2 1997-03-27 10:33:48 schmidt Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | * $Log: not supported by cvs2svn $ |
---|
6 | * Revision 1.1 1996/12/18 15:04:23 schmidt |
---|
7 | * Initial revision |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #include <templates/assert.h> |
---|
12 | |
---|
13 | #include <templates/matrix.h> |
---|
14 | |
---|
15 | template <class T> |
---|
16 | Matrix<T>::Matrix( int nr, int nc ) : NR(nr), NC(nc) |
---|
17 | { |
---|
18 | ASSERT( (nr > 0 && nc > 0) || (nr == 0 && nc == 0), "illegal index" ); |
---|
19 | if ( nr == 0 ) |
---|
20 | elems = 0; |
---|
21 | else { |
---|
22 | int i; |
---|
23 | elems = new (T*)[nr]; |
---|
24 | for ( i = 0; i < nr; i++ ) |
---|
25 | elems[i] = new T[nc]; |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | template <class T> |
---|
30 | Matrix<T>::Matrix( const Matrix<T>& M ) : NR(M.NR), NC(M.NC) |
---|
31 | { |
---|
32 | if ( NR == 0 ) |
---|
33 | elems = 0; |
---|
34 | else { |
---|
35 | int i, j; |
---|
36 | elems = new (T*)[NR]; |
---|
37 | for ( i = 0; i < NR; i++ ) { |
---|
38 | elems[i] = new T[NC]; |
---|
39 | for ( j = 0; j < NC; j++ ) |
---|
40 | elems[i][j] = M.elems[i][j]; |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | template <class T> |
---|
46 | Matrix<T>::~Matrix() |
---|
47 | { |
---|
48 | if ( elems != 0 ) { |
---|
49 | int i; |
---|
50 | for ( i = 0; i < NR; i++ ) |
---|
51 | delete [] elems[i]; |
---|
52 | delete [] elems; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | template <class T> |
---|
57 | Matrix<T>& Matrix<T>::operator= ( const Matrix<T>& M ) |
---|
58 | { |
---|
59 | if ( this != &M ) { |
---|
60 | int i, j; |
---|
61 | if ( NR != M.NR || NC != M.NC ) { |
---|
62 | for ( i = 0; i < NR; i++ ) |
---|
63 | delete [] elems[i]; |
---|
64 | delete elems; |
---|
65 | NR = M.NR; NC = M.NC; |
---|
66 | elems = new (T*)[NR]; |
---|
67 | for ( i = 0; i < NR; i++ ) |
---|
68 | elems[i] = new T[NC]; |
---|
69 | } |
---|
70 | for ( i = 0; i < NR; i++ ) |
---|
71 | for ( j = 0; j < NC; j++ ) |
---|
72 | elems[i][j] = M.elems[i][j]; |
---|
73 | } |
---|
74 | return *this; |
---|
75 | } |
---|
76 | |
---|
77 | template <class T> |
---|
78 | SubMatrix<T> Matrix<T>::operator[] ( int i ) |
---|
79 | { |
---|
80 | ASSERT( i > 0 && i <= NR, "illegal index" ); |
---|
81 | return SubMatrix<T>( i, i, 1, NC, *this ); |
---|
82 | } |
---|
83 | |
---|
84 | template <class T> |
---|
85 | const SubMatrix<T> Matrix<T>::operator[] ( int i ) const |
---|
86 | { |
---|
87 | ASSERT( i > 0 && i <= NR, "illegal index" ); |
---|
88 | return SubMatrix<T>( i, i, 1, NC, *this ); |
---|
89 | } |
---|
90 | |
---|
91 | template <class T> |
---|
92 | T& Matrix<T>::operator() ( int row, int col ) |
---|
93 | { |
---|
94 | ASSERT( row > 0 && col > 0 && row <= NR && col <= NC, "illegal index" ); |
---|
95 | return elems[row-1][col-1]; |
---|
96 | } |
---|
97 | |
---|
98 | template <class T> |
---|
99 | T Matrix<T>::operator() ( int row, int col ) const |
---|
100 | { |
---|
101 | ASSERT( row > 0 && col > 0 && row <= NR && col <= NC, "illegal index" ); |
---|
102 | return elems[row-1][col-1]; |
---|
103 | } |
---|
104 | |
---|
105 | template <class T> |
---|
106 | SubMatrix<T> Matrix<T>::operator() ( int rmin, int rmax, int cmin, int cmax ) |
---|
107 | { |
---|
108 | ASSERT( rmin > 0 && rmax <= NR && rmin <= rmax && cmin > 0 && cmax <= NC && cmin <= cmax , "illegal index" ); |
---|
109 | return SubMatrix<T>( rmin, rmax, cmin, cmax, *this ); |
---|
110 | } |
---|
111 | |
---|
112 | template <class T> |
---|
113 | const SubMatrix<T> Matrix<T>::operator() ( int rmin, int rmax, int cmin, int cmax ) const |
---|
114 | { |
---|
115 | ASSERT( rmin > 0 && rmax <= NR && rmin <= rmax && cmin > 0 && cmax <= NC && cmin <= cmax , "illegal index" ); |
---|
116 | return SubMatrix<T>( rmin, rmax, cmin, cmax, *this ); |
---|
117 | } |
---|
118 | |
---|
119 | template <class T> |
---|
120 | void Matrix<T>::swapRow ( int i, int j ) |
---|
121 | { |
---|
122 | ASSERT( i > 0 && i <= NR && j > 0 && j <= NR, "illegal index" ); |
---|
123 | if ( i != j ) { |
---|
124 | i--; j--; |
---|
125 | T * h = elems[i]; |
---|
126 | elems[i] = elems[j]; |
---|
127 | elems[j] = h; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | template <class T> |
---|
132 | void Matrix<T>::swapColumn ( int i, int j ) |
---|
133 | { |
---|
134 | ASSERT( i > 0 && i <= NC && j > 0 && j <= NC, "illegal index" ); |
---|
135 | if ( i != j ) { |
---|
136 | int k; |
---|
137 | i--; j--; |
---|
138 | for ( k = 0; k < NR; k++ ) { |
---|
139 | T h = elems[k][i]; |
---|
140 | elems[k][i] = elems[k][j]; |
---|
141 | elems[k][j] = h; |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | #ifndef NOSTREAMIO |
---|
147 | template <class T> |
---|
148 | void Matrix<T>::printrow ( ostream & s, int i ) const |
---|
149 | { |
---|
150 | s << "( " << elems[i][0]; |
---|
151 | for ( int j = 1; j < NC; j++ ) |
---|
152 | s << ", " << elems[i][j]; |
---|
153 | s << " )"; |
---|
154 | } |
---|
155 | |
---|
156 | template <class T> |
---|
157 | void Matrix<T>::print( ostream& s ) const |
---|
158 | { |
---|
159 | if ( NR == 0 ) |
---|
160 | s << "( )"; |
---|
161 | else if ( NR == 1 ) { |
---|
162 | s << "( "; |
---|
163 | printrow( s, 0 ); |
---|
164 | s << " )"; |
---|
165 | } |
---|
166 | else { |
---|
167 | int i; |
---|
168 | s << "(\n"; |
---|
169 | printrow( s, 0 ); |
---|
170 | for ( i = 1; i < NR; i++ ) { |
---|
171 | s << ",\n"; |
---|
172 | printrow( s, i ); |
---|
173 | } |
---|
174 | s << "\n)"; |
---|
175 | } |
---|
176 | } |
---|
177 | #endif /* NOSTREAMIO */ |
---|
178 | |
---|
179 | template <class T> |
---|
180 | Matrix<T> operator+ ( const Matrix<T>& lhs, const Matrix<T>& rhs ) |
---|
181 | { |
---|
182 | ASSERT( lhs.NR == rhs.NR && lhs.NC == rhs.NC, "incompatible matrices" ); |
---|
183 | Matrix<T> res( lhs.NR, rhs.NR ); |
---|
184 | int i, j; |
---|
185 | for ( i = 0; i < lhs.NR; i++ ) |
---|
186 | for ( j = 0; j < lhs.NC; j++ ) |
---|
187 | res.elems[i][j] = lhs.elems[i][j] + rhs.elems[i][j]; |
---|
188 | return res; |
---|
189 | } |
---|
190 | |
---|
191 | template <class T> |
---|
192 | Matrix<T> operator- ( const Matrix<T>& lhs, const Matrix<T>& rhs ) |
---|
193 | { |
---|
194 | ASSERT( lhs.NR == rhs.NR && lhs.NC == rhs.NC, "incompatible matrices" ); |
---|
195 | Matrix<T> res( lhs.NR, rhs.NR ); |
---|
196 | int i, j; |
---|
197 | for ( i = 0; i < lhs.NR; i++ ) |
---|
198 | for ( j = 0; j < lhs.NC; j++ ) |
---|
199 | res.elems[i][j] = lhs.elems[i][j] - rhs.elems[i][j]; |
---|
200 | return res; |
---|
201 | } |
---|
202 | |
---|
203 | template <class T> |
---|
204 | Matrix<T> operator* ( const Matrix<T>& lhs, const T& rhs ) |
---|
205 | { |
---|
206 | Matrix<T> res( lhs.NR, lhs.NC ); |
---|
207 | int i, j; |
---|
208 | for ( i = 0; i < lhs.NR; i++ ) |
---|
209 | for ( j = 0; j < lhs.NC; j++ ) |
---|
210 | res.elems[i][j] = lhs.elems[i][j] * rhs; |
---|
211 | return res; |
---|
212 | } |
---|
213 | |
---|
214 | template <class T> |
---|
215 | Matrix<T> operator* ( const T& lhs, const Matrix<T>& rhs ) |
---|
216 | { |
---|
217 | Matrix<T> res( rhs.NR, rhs.NC ); |
---|
218 | int i, j; |
---|
219 | for ( i = 0; i < rhs.NR; i++ ) |
---|
220 | for ( j = 0; j < rhs.NC; j++ ) |
---|
221 | res.elems[i][j] = rhs.elems[i][j] * lhs; |
---|
222 | return res; |
---|
223 | } |
---|
224 | |
---|
225 | template <class T> |
---|
226 | Matrix<T> operator* ( const Matrix<T>& lhs, const Matrix<T>& rhs ) |
---|
227 | { |
---|
228 | ASSERT( lhs.NC == rhs.NR, "incompatible matrices" ); |
---|
229 | Matrix<T> res( lhs.NR, rhs.NC ); |
---|
230 | int i, j, k; |
---|
231 | for ( i = 0; i < lhs.NR; i++ ) |
---|
232 | for ( j = 0; j < rhs.NC; j++ ) { |
---|
233 | res[i][j] = 0; |
---|
234 | for ( k = 0; k < lhs.NC; k++ ) |
---|
235 | res[i][j]+= lhs.elems[i][k] * rhs.elems[k][j]; |
---|
236 | } |
---|
237 | return res; |
---|
238 | } |
---|
239 | |
---|
240 | template <class T> |
---|
241 | SubMatrix<T>::SubMatrix( int rmin, int rmax, int cmin, int cmax, const Matrix<T> & m ) : r_min(rmin), r_max(rmax), c_min(cmin), c_max(cmax), M((Matrix<T>&)m) {} |
---|
242 | |
---|
243 | template <class T> |
---|
244 | SubMatrix<T>::SubMatrix( const SubMatrix<T> & S ) : r_min(S.r_min), r_max(S.r_max), c_min(S.c_min), c_max(S.c_max), M(S.M) {} |
---|
245 | |
---|
246 | template <class T> |
---|
247 | SubMatrix<T>& SubMatrix<T>::operator= ( const Matrix<T>& S ) |
---|
248 | { |
---|
249 | ASSERT( r_max - r_min + 1 == S.NR && c_max - c_min + 1 == S.NC, "incompatible matrices" ); |
---|
250 | if ( M.elems != S.elems ) { |
---|
251 | int i, j; |
---|
252 | for ( i = 0; i < S.NR; i++ ) |
---|
253 | for ( j = 0; j < S.NC; j++ ) |
---|
254 | M.elems[r_min+i-1][c_min+j-1] = S.elems[i][j]; |
---|
255 | } |
---|
256 | return *this; |
---|
257 | } |
---|
258 | |
---|
259 | template <class T> |
---|
260 | SubMatrix<T>& SubMatrix<T>::operator= ( const SubMatrix<T>& S ) |
---|
261 | { |
---|
262 | ASSERT( r_max - r_min == S.r_max - S.r_min && c_max - c_min == S.c_max - S.c_min, "incompatible matrices" ); |
---|
263 | int i, j, n, m; |
---|
264 | n = r_max - r_min + 1; |
---|
265 | m = c_max - c_min + 1; |
---|
266 | if ( M.elems == S.M.elems ) { |
---|
267 | if ( r_min < S.r_min ) { |
---|
268 | for ( i = 0; i < n; i++ ) |
---|
269 | for ( j = 0; j < m; j++ ) |
---|
270 | M.elems[r_min+i-1][c_min+j-1] = S.M.elems[S.r_min+i-1][S.c_min+j-1]; |
---|
271 | } |
---|
272 | else if ( r_min > S.r_min ) { |
---|
273 | for ( i = n-1; i >= 0; i-- ) |
---|
274 | for ( j = 0; j < m; j++ ) |
---|
275 | M.elems[r_min+i-1][c_min+j-1] = S.M.elems[S.r_min+i-1][S.c_min+j-1]; |
---|
276 | } |
---|
277 | else if ( c_min < S.c_min ) { |
---|
278 | for ( j = 0; j < m; j++ ) |
---|
279 | for ( i = 0; i < n; i++ ) |
---|
280 | M.elems[r_min+i-1][c_min+j-1] = S.M.elems[S.r_min+i-1][S.c_min+j-1]; |
---|
281 | } |
---|
282 | else if ( c_min > S.c_min ) { |
---|
283 | for ( j = m-1; j >= 0; j-- ) |
---|
284 | for ( i = 0; i < n; i++ ) |
---|
285 | M.elems[r_min+i-1][c_min+j-1] = S.M.elems[S.r_min+i-1][S.c_min+j-1]; |
---|
286 | } |
---|
287 | } |
---|
288 | else { |
---|
289 | for ( i = 0; i < n; i++ ) |
---|
290 | for ( j = 0; j < m; j++ ) |
---|
291 | M.elems[r_min+i-1][c_min+j-1] = S.M.elems[S.r_min+i-1][S.c_min+j-1]; |
---|
292 | } |
---|
293 | return *this; |
---|
294 | } |
---|
295 | |
---|
296 | template <class T> |
---|
297 | SubMatrix<T>::operator Matrix<T>() const |
---|
298 | { |
---|
299 | Matrix<T> res( r_max - r_min + 1, c_max - c_min + 1 ); |
---|
300 | int i, j; |
---|
301 | int n = r_max - r_min + 1, m = c_max - c_min + 1; |
---|
302 | for ( i = 0; i < n; i++ ) |
---|
303 | for ( j = 0; j < m; j++ ) |
---|
304 | res.elems[i][j] = M.elems[r_min+i-1][c_min+j-1]; |
---|
305 | return res; |
---|
306 | } |
---|
307 | |
---|
308 | template <class T> |
---|
309 | T SubMatrix<T>::operator[] ( int i ) const |
---|
310 | { |
---|
311 | ASSERT( r_min == r_max && i >= c_min && i <= c_max, "illegal index" ); |
---|
312 | return M.elems[r_min-1][i-1]; |
---|
313 | } |
---|
314 | |
---|
315 | template <class T> |
---|
316 | T& SubMatrix<T>::operator[] ( int i ) |
---|
317 | { |
---|
318 | ASSERT( r_min == r_max && i >= c_min && i <= c_max, "illegal index" ); |
---|
319 | return M.elems[r_min-1][i-1]; |
---|
320 | } |
---|