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

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