-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
507 lines (505 loc) · 20.3 KB
/
Matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//---------------------------------------------------------------------------
#ifndef matrixH
#define matrixH
#include <vector>
#include <math.h>
#include <system.hpp>
using namespace std;
extern void err(unsigned, String);
//---------------------------------------------------------------------------
typedef long double Float; // âåùåñòâåííûé ðåçóëüòàò
//---------------------------------------------------------------------------
template<class T> class Matrix{
private:
unsigned __fastcall GetRows(){return rows;};
unsigned __fastcall GetCols(){return cols;};
T **A; // ýëåìåíòû ìàòðèöû
unsigned int rows; // ÷èñëî ñòðîê
unsigned int cols; // ÷èñëî ñòîëáöîâ
void Connected_Nodes(unsigned Node, bool used_node[], Matrix &Tree, unsigned &NumWritedBranch); // ïîèñê ñîñåäíèõ óçëîâ äëÿ ïîñòðîåíèÿ äåðåâà
public:
Matrix(unsigned int num_rows, unsigned int num_columns); // íóëåâàÿ ìàòðèöà
Matrix(unsigned int num_rows, unsigned int num_columns, T *B);// èíèöèàëèçèðóåì ìàòðèöó
Matrix(Matrix &M); // íîâàÿ ìàòðèöà - êîïèÿ ñòàðîé
Matrix(const Matrix<T> &ob);//êîíñòðóêòîð êîïèè
~Matrix();
T *operator[](unsigned int i);
Matrix<T> operator=(Matrix<T> op2);
Matrix<T> operator=(T *op2);
Matrix<T> operator+(Matrix<T> &op2);
Matrix<T> operator-(Matrix<T> &op2);
Matrix<T> operator*(Matrix<T> &op2);
void link_to_massiv(T** to);
friend Matrix<T> operator*(T op1, Matrix<T> op2);
Matrix<T> transpose();
unsigned triangular(Matrix &temp);
Matrix<T> triangular();
T det();
Matrix<Float> inverse();
void Delete_Row(unsigned row);
Matrix tree(bool deleted_branch[]); // èç ìàòðèöû èíöèäåíöèé ãðàôà ïîëó÷èòü ìàò.èíö. äåðåâà, âûáðàñûâàÿ íåíóæíûå âåòêè (deleted_branch[i]==true, åñëè óäàëèëè)
Matrix tree_with_null_branch(); // èç ìàòðèöû èíöèäåíöèé ãðàôà ïîëó÷èòü ìàò.èíö. äåðåâà, îáíóëÿåì íå íóæíûå âåòêè
__property unsigned Rows = { read=GetRows };
__property unsigned Cols = { read=GetCols };
void Distance_From_Node(unsigned Start_Node, unsigned Distance[]); // ïîèñê êðàò÷àéøåãî ðàññòîÿíèÿ îò óçëà äî óçëà
Matrix<char> Way(unsigned Start_Node, unsigned End_Node); // Êðàò÷àéøèé ïóòü îò óçëà äî óçëà
friend void Multiply_Matrix(Matrix<Float> *op1, Float op2[], Float *result[]);
};
//-------------------------------------------------------------------------
template < class T > Matrix<T>::Matrix(unsigned int num_rows, unsigned int num_columns){
rows = num_rows;
cols = num_columns;
A = new T*[rows];
for(unsigned k=0; k<rows;k++)
A[k] = new T[cols];
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
A[i][j]=0;
}
//-------------------------------------------------------------------------
template < class T > Matrix<T>::Matrix(unsigned int num_rows, unsigned int num_columns, T *B){
rows = num_rows;
cols = num_columns;
A = new T*[rows];
for(unsigned k=0; k<rows;k++)
A[k] = new T[cols];
for(unsigned int i=0; i<rows; i++){
T *p = (B+i*cols);
for(unsigned int j=0; j<cols; j++)
A[i][j]=p[j];
}
}
//---------------------------------------------------------------------------
template < class T > Matrix<T>::Matrix(Matrix &M){
rows = M.rows;
cols = M.cols;
A = new T*[rows];
for(unsigned k=0; k<rows;k++)
A[k] = new T[cols];
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
A[i][j]=M.A[i][j];
}
//---------------------------------------------------------------------------
// ýòî êîíñòðóêòîð êîïèè
template < class T > Matrix<T>::Matrix(const Matrix<T> &ob){
rows = ob.rows;
cols = ob.cols;
A = new T*[rows];
for(unsigned k=0; k<rows;k++){
try{ A[k] = new T[cols];}
catch(std::bad_alloc xa) {err(0, "Íåõâàòêà ïàìÿòè");}
}
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
A[i][j]=ob.A[i][j];
}
//---------------------------------------------------------------------------
template <class T> Matrix<T>::~Matrix(){
for(unsigned k=0; k<rows;k++)
{delete [] A[k]; A[k] = NULL;}
delete [] A; A = NULL;
}
//---------------------------------------------------------------------------
template <class T> Matrix<T> Matrix<T>::operator+(Matrix<T> &op2){
Matrix<T> temp(rows, cols);
if(rows!=op2.rows || cols!=op2.cols) {err( 0, "Ñëîæåíèå ìàòðèö: ìàòðèöû èìåþò ðàçíûé ðàçìåð __FILE__:__LINE__" ); return temp;}
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
temp.A[i][j] = A[i][j] + op2.A[i][j];
return temp;
}
//---------------------------------------------------------------------------
template <class T> T* Matrix<T>::operator[](unsigned int i){
return A[i];
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::operator=(Matrix<T> op2){
if(rows!=op2.rows || cols!=op2.cols) {err( 0, "Ïðèñâàèâàíèå ìàòðèöû: ìàòðèöû èìåþò ðàçíûé ðàçìåð" ); return *this;}
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
A[i][j] = op2.A[i][j];
return *this;
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::operator=(T *op2){
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
A[i][j]=*(op2+i*cols+j);
return *this;
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::operator-(Matrix<T> &op2){
Matrix<T> temp(rows, cols);
if(rows!=op2.rows || cols!=op2.cols) {err( 0, "Âû÷èòàíèå ìàòðèö: ìàòðèöû èìåþò ðàçíûé ðàçìåð"); return temp;}
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
temp.A[i][j] = A[i][j] - op2.A[i][j];
return temp;
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::operator*(Matrix<T> &op2){
Matrix<T> temp(rows, op2.cols);
if(cols!=op2.rows) {err( 0, "Óìíîæåíèå ìàòðèö: ÷èñëî ñòðîê è ñòîëáöîâ íå ñîâïàäàåò"); return temp;}
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<op2.cols; j++){
temp.A[i][j] = 0;
for(unsigned int k=0; k<cols; k++)
temp.A[i][j] += A[i][k] * op2.A[k][j];
}
return temp;
}
//---------------------------------------------------------------------------
// Òåïåðü ìàññèâ óêàçûâàåò íà ìàòðèöó-ñòîëáåó
template<class T> void Matrix<T>::link_to_massiv(T** to){
if(cols!=1) {err( 0, "Ïðåîáàçîâàíèå ìàòðèöû ñòîëáöà â ìàññèâ: ÷èñëî ñòîëáöîâ â ìàòðèöå íå ðàâíî 1"); return;}
for(unsigned int j=0; j<rows; j++)
to[j] = &A[j][0];
}
//---------------------------------------------------------------------------
// Òðàíñïîíèðóåì ìàòðèöó
template<class T> Matrix<T> Matrix<T>::transpose(){
Matrix<T> temp(cols, rows);
for(unsigned int i=0; i<rows; i++)
for(unsigned int j=0; j<cols; j++)
temp.A[j][i] = A[i][j];
return temp;
}
//---------------------------------------------------------------------------
/* Ïðèâåäåíèå ìàòðèöû ê òðåóãîëüíîìó âèäó (âîçâðàùàåì ÷èñëî ïåðåñòàíîâîê)*/
template<class T> unsigned Matrix<T>::triangular(Matrix &temp){
if(temp.cols != temp.rows) {err( 0, "Ïðèâèäåíèå ìàòðèöû ê òðåóãîëüíîìó âèäó: ÷èñëî ñòðîê è ñòîëáöîâ íå ñîâïàäàåò"); return 0;}
// íà ïåðâîé ñòðîêå íå âñå íóëè?
unsigned h;
for(unsigned h=0; h< rows; h++) if(fabs(temp.A[h][0])>1e-300) break;
if(h==rows){err( 0, "Ïîëó÷åíèå îáðàòíîé ìàòðèöû: âñå ýëåìåíòû â ïåðâîì ñòîëáöå ìàòðèöû íóëåâûå"); return 0;}
unsigned u=0; // ÷èñëî ïåðåñòàíîâîê ñòðîê
for(unsigned i=0; i<rows-1; i++){
// åñëè äèàãîíàëüíûé ýëåìåíò íîëü ìåíÿåì ñòðîêè ìåñòàìè
if(fabs(temp.A[i][i]) < 1e-300){
//ïîèñê ñòðîêè ñ íåíóëåâûì äèàãîíàëüíûì ýëåìåíòîì
unsigned NotNull; for(NotNull=i+1; NotNull<rows; NotNull++) if(fabs(temp.A[NotNull][i])>1e-300) break;
if(NotNull==rows) {err( 0, "Ïðèâèäåíèå ìàòðèöû ê òðåóãîëüíîìó âèäó: îøèáêà â ïîèñêå íåíóëåâîãî äèàãîíàëüíîãî ýëåìåíòà"); return 0;}
T *tmp = temp.A[i];
temp.A[i] = temp.A[NotNull];
temp.A[NotNull] = tmp;
u++;
}
for(unsigned k=i+1; k<rows; k++){
if(temp.A[k][i] != 0){
// âû÷èñëÿåì ìíîæèòåëü
T d = -1.0*temp.A[k][i]/temp.A[i][i];
// ïðîèçâîäèì ïðèâåäåíèå ñòðîêè
temp.A[k][i] = 0;
for(unsigned j=i+1; j<cols; j++)
temp.A[k][j] = d*temp.A[i][j] + temp.A[k][j];
}
}
}
return u;
}
//---------------------------------------------------------------------------
/* Ïðèâåäåíèå ìàòðèöû ê òðåóãîëüíîìó âèäó */
template<class T> Matrix<T> Matrix<T>::triangular(){
Matrix<T> temp(*this);
triangular(temp);
return temp;
}
//---------------------------------------------------------------------------
/* Íàõîäèì îïðåäåëèòåëü ìàòðèöû */
template<class T> T Matrix<T>::det(){
T d;
if(cols != rows) {err( 0, "Ïîèñê îïðåäåëèòåëÿ: ìàòðèöà íå êâàäðàòíàÿ"); return 0;}
Matrix temp(*this);
unsigned u = triangular(temp);
if( u%2==0 ) d=1; else d=-1;
for(unsigned i=0; i<rows; i++)
d *= temp.A[i][i];
return d;
}
//---------------------------------------------------------------------------
/* Íàõîäèì îáðàòíóþ ìàòðèöó ìåòîäîì Ãàóññà Æîðäàíà */
template<class T> Matrix<Float> Matrix<T>::inverse(){
Matrix temp(*this);
Matrix E(rows, cols);
if(temp.cols != temp.rows) {err( 0, "Ïîëó÷åíèå îáðàòíîé ìàòðèöû: èñõîäíàÿ ìàòðèöà íå êâàäðàòíàÿ"); return E;}
// íà ïåðâîé ñòðîêå íå âñå íóëè?
unsigned h;
for(h=0; h< rows; h++) if(fabs(A[h][0])>1e-300) break;
if(h==rows){err( 0, "Ïîëó÷åíèå îáðàòíîé ìàòðèöû: âñå ýëåìåíòû â ïåðâîì ñòîëáöå ìàòðèöû íóëåâûå"); return E;}
// ôîðìèðóåì åäèíè÷íóþ ìàòðèöó
for(unsigned i=0; i<rows; i++) E.A[i][i] = 1;
unsigned u=0; // ÷èñëî ïåðåñòàíîâîê ñòðîê
// â ïðÿìîì íàïðàâëåíèè
for(unsigned i=0; i<rows-1; i++){
// íàõîäèì ìàêñèìàëüíûé ýëåìåíò â ñòîëáöå
unsigned max_i = i;
Float max = fabs(temp.A[i][i]);
for(unsigned r=i+1; r<rows-1; r++)
if(fabs(temp.A[r][i]) > max){
max = fabs(temp.A[r][i]);
max_i = r;
}
if(max_i != i){ // ìåíÿåì ñòðîêè ìåñòàìè
Float *tmp = temp.A[i];
temp.A[i] = temp.A[max_i];
temp.A[max_i] = tmp;
u++;
// è äëÿ åäèíè÷íîé
tmp = E.A[i];
E.A[i] = E.A[max_i];
E.A[max_i] = tmp;
}
// åñëè äèàãîíàëüíûé ýëåìåíò íîëü ìåíÿåì ñòðîêè ìåñòàìè
if(fabs(temp.A[i][i]) < 1e-300){
//ïîèñê ñòðîêè ñ íåíóëåâûì äèàãîíàëüíûì ýëåìåíòîì
unsigned NotNull; for(NotNull=i+1; NotNull<rows; NotNull++) if(fabs(temp.A[NotNull][i])>1e-300) break;
if(NotNull==rows) {err( 0, "Ïðèâèäåíèå ìàòðèöû ê òðåóãîëüíîìó âèäó: îøèáêà â ïîèñêå íå íóëåâîãî äèàãîíàëüíîãî ýëåìåíòà"); return E;}
Float *tmp = temp.A[i];
temp.A[i] = temp.A[NotNull];
temp.A[NotNull] = tmp;
u++;
// è äëÿ åäèíè÷íîé
tmp = E.A[i];
E.A[i] = E.A[NotNull];
E.A[NotNull] = tmp;
}
// ïîëó÷àåì íà äèàãîíàëüíîì ýëåìåíòå "1"
Float d = temp.A[i][i];
for(unsigned j=i; j<cols; j++)
temp.A[i][j] = temp.A[i][j]/d;
for(unsigned j=0; j<cols; j++)
E.A[i][j] = E.A[i][j]/d;
for(unsigned k=i+1; k<rows; k++){
// âû÷èñëÿåì ìíîæèòåëü
Float d = -temp.A[k][i];
// ïðîèçâîäèì ïðèâåäåíèå ñòðîêè
temp.A[k][i] = 0;
for(unsigned j=i+1; j<cols; j++)
temp.A[k][j] = d*temp.A[i][j] + temp.A[k][j];
for(unsigned j=0; j<cols; j++)
E.A[k][j] = d*E.A[i][j] + E.A[k][j];
}
}
// ïîëó÷àåì íà êðàéíåì äèàãîíàëüíîì ýëåìåíòå "1"
Float d = 1.0*temp.A[rows-1][rows-1];
if(d==0){
err(0,"Matrix::inverse(): äåëåíèå íà íîëü");
d=1e-100;}
temp.A[rows-1][rows-1] = 1;
for(unsigned j=0; j<cols; j++)
E.A[rows-1][j] = E.A[rows-1][j]/d;
//Â îáðàòíîì íàïðàâëåíèè
for(unsigned i=rows-1; i>0; i--){
for(unsigned k=0; k<i; k++){
if(fabs(temp.A[k][i])>1e-300){
// âû÷èñëÿåì ìíîæèòåëü
Float d = -temp.A[k][i];
// ïðîèçâîäèì ïðèâåäåíèå ñòðîêè
temp.A[k][i] = 0;
for(unsigned j=0; j<cols; j++)
E.A[k][j] = d*E.A[i][j] + E.A[k][j];
}
}
}
return E;
}
//---------------------------------------------------------------------------
template<class T> Matrix<char> Matrix<T>::Way(unsigned Start_Node, unsigned End_Node){
Matrix<char> way(1,cols);
if(Start_Node==End_Node) return way;
unsigned *Distance = new unsigned[cols];
for(unsigned i=0;i<cols;i++) Distance[i] = -1;
// ïîèñê ðàññòîÿíèé îò óçëà
Distance[Start_Node] = 0;
Distance_From_Node(Start_Node, Distance);
//unsigned g[200];
//for(unsigned i=0; i<cols;i++)g[i]=Distance[i];
// ñîñòàâëÿåì ïóòü
unsigned nB = cols;
unsigned nN = rows;
// èäåì îò êîíå÷íîé âåðøèíû
unsigned s = End_Node;
while( s != Start_Node){
unsigned Dis = Distance[s]-1; // èùåì ñîñåäíèé óçåë íà òàêîì ðàññòîÿíèè îò ñòàðòîâîãî
// èùåì âåòêè äî ñîñåäíåé âåðøèíû
unsigned i;
for(i=0;i<nB;i++){
if(A[s][i]!=0){
// â êàêóþ íåèñïîëüçîâàííóþ âåðøèíó ïîïàäàåì
unsigned j;
for(j=0;j<nN;j++)
if(A[j][i]!=0 && Dis == Distance[j]){
way[0][i] = A[s][i]; // 1 èëè -1 -â çàâèñèìîñòè ïî íàïðÿâëåíèþ èëè ïðîòèâ âåòêè
s = j;
break;
}
if(j<nN) break; // ïåðåøëè â äðóãîé óçåë, ïîèñê âåòâåé ïðåêðàùàåì
}
}
// íåò ïóòè
if(i >= nB){
for(unsigned x=0;x<nB;x++) way[0][x]=0;
break;
}
}
delete [] Distance;
return way;
}
//---------------------------------------------------------------------------
// ðåêóðñèâíûé ïîèñê ðàññòîÿíèÿ îò óçëà
template<class T> void Matrix<T>::Distance_From_Node(unsigned Start_Node, unsigned Distance[]){ //, End_Node
vector<unsigned> Cocedi;
Float Find_Dist = Distance[Start_Node]+1;
//èùåì ñîñåäíèå íå èñïîëüçîâàííûå óçëû
for(unsigned j=0; j<cols; j++)
if(fabs(A[Start_Node][j])>1e-300) // åñëè åñòü âåòêà, íàéäåì äî êàêîãî îíà óçëà
for(unsigned i=0; i<rows; i++)
if(fabs(A[i][j])>1e-300 && Find_Dist<Distance[i]){ // åñëè äî íå óçëà ðàñòòîÿíèå ìåíüøå
Distance[i] = Find_Dist; // ïîìå÷àåì óçåë
Cocedi.push_back(i);
}
// èùåì ðåêóðñèâíî
for(unsigned i=0, n=Cocedi.size(); i<n; i++)
Distance_From_Node(Cocedi[i], Distance);//, End_Node ïîèñê ñîñåäíèõ óçëîâ
}
//---------------------------------------------------------------------------
// ðåêóðñèâíûé ïîèñê ðàññòîÿíèÿ îò óçëà
template<> void Matrix<char>::Distance_From_Node(unsigned Start_Node, unsigned Distance[]){
std::vector <unsigned> Cocedi; // std::vector
unsigned Find_Dist = Distance[Start_Node]+1;
//èùåì ñîñåäíèå íå èñïîëüçîâàííûå óçëû
for(unsigned j=0; j<cols; j++)
if(A[Start_Node][j]!=0) // åñëè åñòü âåòêà, íàéäåì äî êàêîãî îíà óçëà
for(unsigned i=0; i<rows; i++)
if(A[i][j]!=0 && Find_Dist<Distance[i]){ // åñëè äî íå óçëà ðàñòòîÿíèå ìåíüøå
Distance[i] = Find_Dist; // ïîìå÷àåì óçåë
Cocedi.push_back(i);
}
// èùåì ðåêóðñèâíî
for(unsigned i=0, n=Cocedi.size(); i<n; i++)
Distance_From_Node(Cocedi[i], Distance);// ïîèñê ñîñåäíèõ óçëîâ
}
//---------------------------------------------------------------------------
// ðåêóðñèâíûé ïîèñê ñîñåäíèõ óçëîâ
template<class T> void Matrix<T>::Connected_Nodes(unsigned Node, bool used_node[], Matrix &Tree, unsigned &NumWritedBranch){
//èùåì ñîñåäíèå íå èñïîëüçîâàííûå óçëû
for(unsigned j=0; j<cols; j++)
if(fabs(A[Node][j])>1e-300) // åñëè åñòü âåòêà, íàéäåì äî êàêîãî îíà óçëà
for(unsigned i=0; i<rows; i++)
if(fabs(A[i][j])>1e-300 && !used_node[i]){ // åñëè äî íå èñïîëüçîâàííîãî óçëà
used_node[i] = true; // ïîìå÷àåì óçåë
for(unsigned s=0; s<rows; s++) Tree.A[s][j] = A[s][j]; // êîïèðóåì ñîäåðæèìîå
NumWritedBranch++;
if(NumWritedBranch < (Tree.rows-1)) // âåòîê íå äîñòàòî÷íî
Connected_Nodes(i, used_node, Tree, NumWritedBranch);// ïîèñê ñîñåäíèõ óçëîâ
break;
}
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::tree(bool deleted_branch[]){
// ÷èñëî óçëîâ â äåðåâå âñåãäà áîëå íà 1 ÷åì âåòâåé
Matrix Res(rows, rows-1);
Matrix Tree(rows, cols);
// èùåì äåðåâî
Tree = tree_with_null_branch();
// óäàëÿåì íóëåâûå ñòîëáöû
for(unsigned i=0; i<cols;i++) deleted_branch[i]=true; // èçíà÷àëüíî ãîâîðèì, ÷òî âñå âåòêè óäàëåíû
unsigned write_col=0;
for(unsigned h=0;h<Tree.cols;h++){
unsigned i;
for(i=0;i<Tree.rows;i++)if(fabs(Tree.A[i][h])>1e-300) break;
if(i!=Tree.rows){ // â ñòîëáöå íå âñå íóëè
deleted_branch[h] = false; // âåòêà íå óäàëåíà
for(unsigned r=0;r<Tree.rows;r++) Res.A[r][write_col]=Tree.A[r][h];
write_col++;
}
}
if(write_col != Res.Cols){err(0, "Matrix::tree: ïðè íàõîæäåíèè äåðåâà íå ñîáëþäàåòñÿ ×ÈÑËÎ_ÂÅÒÂÅÉ == (×ÈÑËÎ_ÓÇËÎÂ-1)");}
return Res;
/*
// äîáàâëÿåì ïåðâóþ âåòêó
unsigned k=0; // ÷èñëî ñîõðàíåííûõ âåòîê â äåðåâå
unsigned j; // òåêóùàÿ âåòêà
for(j=0; j<cols; j++){
unsigned h=0;
// íå âñå ëè ýëåìåíòû ñòîëáöà == 0 ?
for(h=0; h<rows; h++) if(fabs(A[h][j])>1e-300) break;
if(h!=rows){
for(unsigned i=0; i<rows; i++){
if(fabs(A[i][j])>1e-300) used_node[i]=true;
temp.A[i][k] = A[i][j];
}
deleted_branch[j] = false; // ñîõðàíèëè j-âåòêó
k++;
break;
}
}
// äîáàâëÿåì âåòêè óêàçûâàþùèå íà íîâûé óçåë
for(j++; j<cols; j++){
if(k<temp.cols) // åñëè íóæíû åùå âåòêè
for(unsigned i=0; i<rows; i++)
if(fabs(A[i][j])>1e-300 && !used_node[i]){
// äîáàâëÿåì
for(unsigned s=0; s<rows; s++) temp.A[s][k] = A[s][j];
deleted_branch[j] = false; // ñîõðàíèëè j-âåòêó
used_node[i] = true; // ãîâîðèì ÷òî èñïîëüçîâàëè âåðøèíó
k++; // â ãðàôå äåðåâà ñòàëî íà îäíó âåòêó áîëüøå
break;
}
} */
}
//---------------------------------------------------------------------------
template<class T> Matrix<T> Matrix<T>::tree_with_null_branch(){
// ÷èñëî óçëîâ â äåðåâå âñåãäà áîëå íà 1 ÷åì âåòâåé
Matrix Tree(rows, cols);
bool *used_node = new bool[Tree.rows];
for(unsigned i=0; i<Tree.rows;i++) used_node[i]=false;
// äîáàâëÿåì íóëåâîé óçåë
static unsigned Uz=0; // òåêóùèé óçåë
used_node[Uz]=true;
unsigned k=0; // âñåãî íàéäåíî âåòâåé â äåðåâå
//èùåì ñîñåäíèå íå èñïîëüçîâàííûå óçëû
Connected_Nodes(Uz, used_node, Tree, k);
delete [] used_node;
return Tree;
/*/ ÷èñëî óçëîâ â äåðåâå âñåãäà áîëå íà 1 ÷åì âåòâåé
Matrix temp(rows, cols);
bool *used_node = new bool[temp.rows];
for(unsigned i=0; i<temp.rows;i++) used_node[i]=false;
// äîáàâëÿåì âåòêè óêàçûâàþùèå íà íîâûé óçåë
for(unsigned j=0; j<cols; j++){
unsigned i;
for(i=0; i<rows; i++)
if(fabs(A[i][j])>1e-300 && !used_node[i]){ // äîáàâëÿåò ëè âåòêà íîâûé óçåë?
// äà
for(unsigned s=0; s<rows; s++) temp.A[s][j] = A[s][j];
break;
}
if(i==rows) // íåò
for(unsigned s=0; s<rows; s++) temp.A[s][j] = 0;
}
return temp;*/
}
//---------------------------------------------------------------------------
template<class T> void Matrix<T>::Delete_Row(unsigned row){
for(unsigned i=row; i<(rows-1);i++) A[i] = A[i+1];
delete A[rows-1];
rows--;
}
//---------------------------------------------------------------------------
template<class T> Matrix<Float> Multiply_Matrix(Matrix<T> *op1, Float ***op2, unsigned op2_rows, unsigned op2_cols)
{
Matrix<Float> temp(op1->Rows, op2_cols);
if(op1->Cols!=op2_rows) {err( 0, "Óìíîæåíèå ìàòðèö: ÷èñëî ñòðîê è ñòîëáöîâ íå ñîâïàäàåò"); return temp;}
for(unsigned int i=0; i<op1->Rows; i++)
for(unsigned int j=0; j<op2_cols; j++){
temp[i][j] = 0;
for(unsigned int k=0; k<op1->Cols; k++)
temp[i][j] += (Float)(*op1)[i][k] * (*op2[k][j]);
}
return temp;
}
//---------------------------------------------------------------------------
bool Copy_Matrix(Matrix<char> *from, Matrix<Float> *to);
#endif