1 | /* =================================================================== |
---|
2 | Dateiname: lgs.cc |
---|
3 | =================================================================== */ |
---|
4 | #ifdef HAVE_BIFAC |
---|
5 | |
---|
6 | #include "lgs.h" |
---|
7 | #include "bifacConfig.h" |
---|
8 | |
---|
9 | |
---|
10 | //--<>--------------------------------- |
---|
11 | LGS::LGS( int r, int c, bool inv )// KONSTRUKTOR |
---|
12 | //--<>--------------------------------- |
---|
13 | { |
---|
14 | INVERSE = inv; |
---|
15 | |
---|
16 | max_columns = c; |
---|
17 | max_rows = (r>c) ? c : r ; |
---|
18 | |
---|
19 | if( INVERSE ) |
---|
20 | { |
---|
21 | c *=2; |
---|
22 | } |
---|
23 | |
---|
24 | CFMatrix AA(max_rows, c); |
---|
25 | CFMatrix bb(max_rows, 1); |
---|
26 | A=AA; |
---|
27 | b=bb; |
---|
28 | pivot = new int [max_columns+1]; // We start with index 1 (easier) |
---|
29 | for( int i=0; i<=max_columns; i++) pivot[i]=0; |
---|
30 | now_row = 1; // We start counting in the first row |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | //--<>--------------------------------- |
---|
35 | LGS::~LGS( void )// DESTRUKTOR |
---|
36 | //--<>--------------------------------- |
---|
37 | { |
---|
38 | delete[] pivot; |
---|
39 | } |
---|
40 | |
---|
41 | //--<>--------------------------------- |
---|
42 | void LGS::reset(void) |
---|
43 | //--<>--------------------------------- |
---|
44 | { // Clear the matrix for a new computation |
---|
45 | now_row=1; |
---|
46 | for( int i=0; i<=max_columns; i++) |
---|
47 | pivot[i]=0; |
---|
48 | } |
---|
49 | |
---|
50 | //--<>--------------------------------- |
---|
51 | bool LGS::new_row( const CFMatrix Z, const CanonicalForm bb) |
---|
52 | //--<>--------------------------------- |
---|
53 | { // Insert a new row |
---|
54 | ASSERT ( (1 <= now_row && now_row <=max_rows), "wrong number of rows => Matrix has max. rank"); |
---|
55 | int i; |
---|
56 | |
---|
57 | // if (INVERSE) |
---|
58 | // cout << "* Integriere Zeile "<<now_row << " (max " <<max_rows<<" x " |
---|
59 | // << max_columns << ")\n" |
---|
60 | // << "Z = " << Z << "\nb = " << bb << endl << flush; |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | // === Insert a new row === |
---|
66 | for(i=1; i<=max_columns; i++) |
---|
67 | A(now_row, i) = Z(1,i); |
---|
68 | b(now_row, 1) = bb; |
---|
69 | |
---|
70 | |
---|
71 | // cout << "* reduzierte Matrix (vor lin_dep) " << A << endl; |
---|
72 | // === check linear dependency === |
---|
73 | if ( ! lin_dep() ) |
---|
74 | now_row++; |
---|
75 | else |
---|
76 | return(false); |
---|
77 | |
---|
78 | // === Reduce the previous rows === |
---|
79 | for(i=1; i<now_row-1; i++) |
---|
80 | reduce(now_row-1,i ); |
---|
81 | |
---|
82 | |
---|
83 | // cout << "\n* Zeile Z =" << Z << " ist integriert!\n" << A << flush; |
---|
84 | |
---|
85 | // if( INVERSE ) cout << A; |
---|
86 | // cout << "* Verlasse new_row!\n" << "* Z = " << Z << endl << flush; |
---|
87 | return(true); // row was linear independent |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | //--<>--------------------------------- |
---|
92 | bool LGS::lin_dep( void ) |
---|
93 | //--<>--------------------------------- |
---|
94 | { // check if new row is linear dependent of the former ones |
---|
95 | |
---|
96 | int i; |
---|
97 | |
---|
98 | // === Reduce the actual row === |
---|
99 | for(i=1; i<now_row; i++) |
---|
100 | reduce(i, now_row); |
---|
101 | |
---|
102 | // cerr << "* Bin noch in [lin_dep]\n" << flush; |
---|
103 | |
---|
104 | // === Quest for a pivot === |
---|
105 | for ( i=1; i<=max_columns; i++) |
---|
106 | if( A(now_row,i) != 0 ) |
---|
107 | { |
---|
108 | pivot[now_row] = i; |
---|
109 | break;// i = max_columns; |
---|
110 | |
---|
111 | } |
---|
112 | // cout << "* pivot["<<now_row<<"] = " << pivot[now_row] << endl << flush; |
---|
113 | |
---|
114 | // === Is the reduced row (0,...,0)? ==== |
---|
115 | if( pivot[now_row] == 0 ) |
---|
116 | { |
---|
117 | if( INVERSE ) |
---|
118 | for ( i=1; i<=max_columns; i++) |
---|
119 | A(now_row, max_columns+i) = 0; |
---|
120 | return (true); |
---|
121 | } |
---|
122 | // === The row is linear independent ==== |
---|
123 | // cout << "* reduzierte Matrix (1) ist " << A << endl; |
---|
124 | if( INVERSE ) // Identity matrix |
---|
125 | A(now_row, now_row+max_columns) = 1; |
---|
126 | // cout << "* reduzierte Matrix (2) ist " << A << endl; |
---|
127 | return(false); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | //--<>--------------------------------- |
---|
132 | void LGS::reduce(int fix, int row) |
---|
133 | //--<>--------------------------------- |
---|
134 | { // Reduce the `rowŽ by means of row `fixŽ |
---|
135 | |
---|
136 | if( A(row, pivot[fix]) == 0 ) return; |
---|
137 | |
---|
138 | CanonicalForm mul = A(row, pivot[fix]) / A(fix, pivot[fix]); |
---|
139 | |
---|
140 | // cout << "* Multiplikationsfaktor ist " << mul << endl; |
---|
141 | // cout << "* Aktuelle Matrix ist " << A |
---|
142 | // << "\n b = " << b << endl; |
---|
143 | // cout << "max_columns = "<< max_columns << endl; |
---|
144 | // cout << "* now_row = " << now_row <<", row=" << row << endl << flush; |
---|
145 | // cout << "* Pivot[" << row << "] = " << pivot[row] << endl << flush; |
---|
146 | // cout << "* Pivot[" << fix << "] = " << pivot[fix] << endl << flush; |
---|
147 | |
---|
148 | |
---|
149 | for (int i=1; i<=max_columns; i++) |
---|
150 | if( A(fix,i) != 0 ) |
---|
151 | A(row, i) -= mul* A(fix,i); |
---|
152 | if( b(fix,1) != 0 ) |
---|
153 | b(row,1) -= mul * b(fix,1); |
---|
154 | |
---|
155 | |
---|
156 | if ( INVERSE ) |
---|
157 | for (int i=max_columns+1; i<=2*max_columns; i++) |
---|
158 | if( A(fix,i) != 0 ) |
---|
159 | A(row, i) -= (mul* A(fix,i)); |
---|
160 | // A(row,i) = 777; |
---|
161 | |
---|
162 | // cout << "* reduzierte Matrix ist " << A << endl; |
---|
163 | // cout << "* Verlasse [reduce]\n" << flush; |
---|
164 | |
---|
165 | } |
---|
166 | /////////////////////////////////////////////////////// |
---|
167 | // The Matrix (A | I) has maximal rank and |
---|
168 | // the ŽAŽ part has to be normalized only |
---|
169 | //--<>--------------------------------- |
---|
170 | void LGS::inverse( CFMatrix & I ) |
---|
171 | //--<>--------------------------------- |
---|
172 | { |
---|
173 | int i,j; |
---|
174 | CanonicalForm mul; |
---|
175 | |
---|
176 | // cout << "* PRÄ-Invers:" << A << endl; |
---|
177 | for(i=1; i<=max_rows; i++) |
---|
178 | { |
---|
179 | for( j=max_columns+1; j<=2*max_columns; j++) |
---|
180 | if( A(i,j)!=0 ) |
---|
181 | A(i,j) /= A( i, pivot[i]); |
---|
182 | A( i, pivot[i]) = 1; |
---|
183 | } |
---|
184 | // cout << "* Inverse Matrix ist " << A << endl; |
---|
185 | |
---|
186 | for(i=1; i<=max_rows; i++) |
---|
187 | { |
---|
188 | // cout << "pivot["<<i<<"] = " << pivot[i]<< endl; |
---|
189 | for(j=1; j<=max_columns; j++) |
---|
190 | I(pivot[i],j) = A(i, j+max_columns); |
---|
191 | } |
---|
192 | } |
---|
193 | //--<>--------------------------------- |
---|
194 | int LGS::rank(void) |
---|
195 | //--<>--------------------------------- |
---|
196 | { // Return the current rank of the matrix |
---|
197 | return( now_row-1); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | //--<>--------------------------------- |
---|
202 | void LGS::print(void) |
---|
203 | //--<>--------------------------------- |
---|
204 | { // Return the current rank of the matrix |
---|
205 | #ifdef NOSTREAMIO |
---|
206 | printf("LGS::print??\n"); |
---|
207 | #else |
---|
208 | cout << "A = " << A << "\nb = " << b << endl; |
---|
209 | #endif |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | //--<>--------------------------------- |
---|
216 | int LGS::corank(void) |
---|
217 | //--<>--------------------------------- |
---|
218 | { // Return the current rank of the matrix |
---|
219 | return( ( (max_rows < max_columns ) ? max_rows : max_columns )-now_row+1 ); |
---|
220 | } |
---|
221 | |
---|
222 | //--<>--------------------------------- |
---|
223 | int LGS::ErgCol(int row, int basis[]) |
---|
224 | //--<>--------------------------------- |
---|
225 | { |
---|
226 | bool state = false; |
---|
227 | |
---|
228 | for( int i=1; i<=max_columns; i++) |
---|
229 | { |
---|
230 | if( A(row,i) != 0 ) |
---|
231 | { |
---|
232 | state = true; |
---|
233 | for( int j=1; j<=basis[0]; j++) |
---|
234 | if( i==basis[j] ) |
---|
235 | state = false; |
---|
236 | if( state == true ) |
---|
237 | return (i); |
---|
238 | } |
---|
239 | } |
---|
240 | // A row contains only pivot entry/-ies |
---|
241 | for( int j=1; j<=basis[0]; j++) |
---|
242 | if( A(row, basis[j]) != 0 ) |
---|
243 | return( -basis[j] ); |
---|
244 | |
---|
245 | |
---|
246 | AUSGABE_LGS("Zeile ist " << row << endl); |
---|
247 | AUSGABE_ERR("* Mistake in [lgs.cc]! Impossible result. Aborting!\n"); |
---|
248 | exit (1); |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | //--<>--------------------------------- |
---|
253 | CFMatrix LGS::GetKernelBasis(void) |
---|
254 | //--<>--------------------------------- |
---|
255 | { |
---|
256 | int i,z; |
---|
257 | int dim = corank(); |
---|
258 | |
---|
259 | bool* tmp_vec = new bool[max_columns+1]; |
---|
260 | int* basis = new int [dim+1]; |
---|
261 | basis[0]=1; |
---|
262 | CFMatrix erg (dim, max_columns); // Each row is one solution |
---|
263 | |
---|
264 | // === Searching free parameters === |
---|
265 | for( i=1; i<=max_rows; i++) |
---|
266 | tmp_vec[i]=false; |
---|
267 | for( i=1; i<now_row; i++) |
---|
268 | if( pivot[i] != 0 ) |
---|
269 | tmp_vec[pivot[i]] = true; |
---|
270 | |
---|
271 | for( i=1; i<=max_columns; i++) |
---|
272 | if(tmp_vec[i] == false) |
---|
273 | { |
---|
274 | basis[basis[0]]=i; |
---|
275 | basis[0]++; |
---|
276 | } |
---|
277 | ASSERT(basis[0]-1==dim, "Wrong dimensions"); |
---|
278 | |
---|
279 | |
---|
280 | // cout << "* Freie Parameter sind:"; |
---|
281 | // for ( int k=1; k<basis[0]; k++) cout << basis[k] << ", "; |
---|
282 | // cout << endl; |
---|
283 | |
---|
284 | // === Creating the basis vectors === |
---|
285 | for(z=1; z<now_row; z++) |
---|
286 | for(i=1; i<=dim; i++) |
---|
287 | { |
---|
288 | erg(i, basis[i]) = 1; |
---|
289 | erg(i, pivot[z]) = -(A(z, basis[i]) / A(z,pivot[z])); |
---|
290 | } |
---|
291 | |
---|
292 | // cout << "Kernbasis ist " << erg; |
---|
293 | return erg; |
---|
294 | } |
---|
295 | |
---|
296 | //--<>--------------------------------- |
---|
297 | CFMatrix LGS::GetSolutionVector(void) |
---|
298 | //--<>--------------------------------- |
---|
299 | { // Ax=b has exactly one solution vector x |
---|
300 | int z; |
---|
301 | CFMatrix erg (1, max_columns); |
---|
302 | |
---|
303 | // === Creating the basis vectors === |
---|
304 | for(z=1; z<=max_columns; z++) |
---|
305 | { |
---|
306 | erg(1,pivot[z]) = b(z,1); // Vector b |
---|
307 | erg(1,pivot[z]) /= A(z,pivot[z]); |
---|
308 | } |
---|
309 | return erg; |
---|
310 | } |
---|
311 | #endif |
---|