source: git/factory/templates/ftmpl_matrix.cc @ ca44e5

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