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