source: git/factory/templates/ftmpl_matrix.cc @ 27f57a6

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