source: git/factory/templates/ftmpl_matrix.cc @ 2fb5762

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