source: git/factory/templates/ftmpl_matrix.cc @ 19fc57b

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