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