File: matrix.cpp

package info (click to toggle)
socnetv 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,028 kB
  • sloc: cpp: 12,953; makefile: 75
file content (407 lines) | stat: -rwxr-xr-x 11,849 bytes parent folder | download
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
/***************************************************************************
 SocNetV: Social Networks Visualizer 
 version: 0.90
 Written in Qt 4.4

                        matrix  -  description
                             -------------------
    copyright            : (C) 2005-2010 by Dimitris B. Kalamaras
    email                : dimitris.kalamaras@gmail.com
 ***************************************************************************/

/*******************************************************************************
*     This program is free software: you can redistribute it and/or modify     *
*     it under the terms of the GNU General Public License as published by     *
*     the Free Software Foundation, either version 3 of the License, or        *
*     (at your option) any later version.                                      *
*                                                                              *
*     This program is distributed in the hope that it will be useful,          *
*     but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
*     GNU General Public License for more details.                             *
*                                                                              *
*     You should have received a copy of the GNU General Public License        *
*     along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
********************************************************************************/

#include "matrix.h"

#include <iostream>		//used for cout

#include <math.h>		//need for fabs function

//constructor - every Row object holds max_int=32762
Matrix::Matrix(const Matrix &b) {
	row = new Row[m_Actors=b.m_Actors];
	for (register int i=0;i<m_Actors; i++) {
			row[i].resize(m_Actors);
		}
	for (register int i=0; i<m_Actors; i++)
		row[i]=b.row[i];
}


// set the size of the matrix
void Matrix::setSize (int Actors){
	for (register int i=0;i<m_Actors; i++) {
		row[i].setSize(Actors); 
	}
	m_Actors=Actors;

}


//assigment allows copying a matrix onto another using b=a where b,a matrices
 Matrix& Matrix::operator =(Matrix & a) {
	if (this != &a){
		if (a.m_Actors!=m_Actors) {
			delete [] row;
			row=new Row[m_Actors=a.m_Actors];
			Q_CHECK_PTR( row );
			for (int i=0;i<m_Actors; i++) {
				row[i].resize(m_Actors); //every Row object holds max_int=32762 actors
			}
		}
			for (int i=0;i<m_Actors; i++) row[i]=a.row[i];
	}
	return *this;
}


 //WARNING: this operator is slow! Avoid using it.
float  Matrix::operator ()  (const int r, const int c){
	return  row[r].column(c);
}


/**  	Outputs matrix m to a text stream
*	Used when exporting SM to a file in slotExportSM of MainWindow class.
*/
QTextStream& operator <<  (QTextStream& os, Matrix& m){
	qDebug() << "Matrix: << Matrix";
	for (register int r = 0; r < m.rows(); ++r) {
		for (register int c = 0; c < m.cols(); ++c)
			os << m(r,c) << ' ';
		os << '\n';
	}
	return os;
}



//Resize this matrix. Every Row object holds max_int=32762
void Matrix::resize (int Actors) {
	qDebug() << "Matrix: resize() -- deleting old rows";
	delete [] row;
	row = new Row [m_Actors=Actors];
	Q_CHECK_PTR( row );
	qDebug() << "Matrix: resize() -- resizing each row";
	for (register int i=0;i<m_Actors; i++) {
	    row[i].resize(m_Actors);
	}
}



// makes this matrix the identity matrix I
void Matrix::identityMatrix(int Actors) {
	qDebug() << "Matrix: identityMatrix() -- deleting old rows";
	delete [] row;
	row = new Row [m_Actors=Actors];
	Q_CHECK_PTR( row );
	qDebug() << "Matrix: resize() -- resizing each row";
	for (int i=0;i<m_Actors; i++) {
		row[i].resize(m_Actors);
		setItem(i,i, 1);
	}

}


// returns the (r,c) matrix element
float Matrix::item( int r, int c ){
	return row[r].column(c);
}

// sets the (r,c) matrix element calling the setColumn method
void Matrix::setItem( int r, int c, float elem ) {
	 row [ r ].setColumn(c, elem);

}

// clears the (r,c) matrix element
void Matrix::clearItem( int r, int c ) 	{
	row[r].clearColumn(c); 
}


//returns the number of edges starting from r
int Matrix::edgesFrom(int r){
	qDebug() << "Matrix: edgesFrom() " << r << " = "<< row[r].outEdges();
	return row[r].outEdges();
}


int Matrix::edgesTo(int t){
	int m_inEdges=0;
	for (register int i = 0; i < rows(); ++i) {
		if ( item(i, t) != 0 )
			m_inEdges++;
	}
	qDebug()<< "Matrix: edgesTo() " << t << " = " << m_inEdges;
	return m_inEdges;
}


int Matrix::totalEdges(){
	int m_totalEdges=0;
	for (register int r = 0; r < rows(); ++r) {
		m_totalEdges+=edgesFrom(r);
	}
	qDebug() << "Matrix: totalEdges " << m_totalEdges;
	return m_totalEdges;
}


bool Matrix::printMatrixConsole(){
	qDebug() << "Matrix: printMatrixConsole";
	for (register int r = 0; r < rows(); ++r) {
		for (register int c = 0; c < cols(); ++c)
			cout<< item(r,c) <<' ';
			cout<<'\n';
	}
	return true;
}



void Matrix::deleteRowColumn(int erased){
	qDebug() << "Matrix: deleteRowColumn() : "<< erased;
	qDebug() << "Matrix: mActors before " <<  m_Actors;

	--m_Actors;
	qDebug() << "Matrix: mActors now " << m_Actors << ". Resizing...";
	for (register int i=0;i<m_Actors+1; i++) { 
		for (register int j=0;j<m_Actors+1; j++) { 
		    qDebug() << "Matrix: (" <<  i << ", " << j << ")="<< item(i, j) ;
			if ( j==erased && item(i,erased) ){
			 	clearItem(i,j);
				qDebug() << i << "  connected to " << erased << ". Clearing..." ;
			}	
			if (i<erased && j< erased) {
				qDebug() << "i, j < erased. Skipping. Item remains";
			}
			if (i<erased && j>=erased) {
				setItem( i, j, item(i,j+1) ) ;
				qDebug() << "case 2";
			}			
			if (i>=erased && j<erased) {
				setItem( i, j, item(i+1,j) ) ;
				qDebug() <<"case 3";
			}
			if (i>=erased && j>=erased) {
				setItem( i, j, item(i+1,j+1) ) ;
				qDebug() <<"case 4";
			}
			if (i>=m_Actors || j>=m_Actors) {
				setItem( i, j, 0) ;
				qDebug() <<"case 5 (border)";
			}	
			qDebug() << "Matrix: new value (" <<  i << ", " << j << ")="<< item(i, j) ;
		}	
	}
	for (register int i=0;i<m_Actors; i++) 
		row[i].updateOutEdges();
	
}


// fills a matrix with a given valut
void Matrix::fillMatrix(float value )   {
	for (int i=0;i<m_Actors; i++) 
		for (int j=0;j<m_Actors; j++) 
			setItem(i,j, value);
}

//takes two (ActorsXActors) matrices and returns their product as a reference to this
Matrix& Matrix::product( Matrix &a, Matrix & b, bool symmetry)  {
	for (register int i=0;i< rows();i++)
		for (register int j=0;j<cols();j++) {
			setItem(i,j,0);
			for (register int k=0;k<m_Actors;k++)
				if  ( k > j && symmetry) {
					if (a.item(i,k)!=0 && b.item(j,k)!=0)
						setItem(i,j, item(i,j)+a.item(i,k)*b.item(j,k));
				}
				else{
				    setItem(i,j, item(i,j)+a.item(i,k)*b.item(k,j));
				}
		}
		return *this;
}
		
//takes two (AXA) matrices (symmetric) and outputs an upper triangular matrix
Matrix& Matrix::productSym( Matrix &a, Matrix & b)  {
	for (register int i=0;i<m_Actors;i++)
	    for (register int j=0;j<m_Actors;j++) {
			setItem(i,j,0);
			if (i>=j) continue;
			for (register int k=0;k<m_Actors;k++)
				if  ( k > j ) {
					if (a.item(i,k)!=0 && b.item(j,k)!=0)
						setItem(i,j, item(i,j)+a.item(i,k)*b.item(j,k));
				}
				else  //k <= j  && i<j
					if ( i>k ) {
						if (a.item(k,i)!=0 && b.item(k,j)!=0)
							setItem(i,j, item(i,j)+a.item(k,i)*b.item(k,j));
						}
						else {
							if (a.item(i,k)!=0 && b.item(k,j)!=0)
								setItem(i,j, item(i,j)+a.item(i,k)*b.item(k,j));
						}
	    }
	    return *this;
}
	
Matrix& Matrix::pow (int power, bool symmetry)  {
	Matrix t=*this;
	for (register int k=1; k<power; k++){
		product(*this, t, symmetry);
	}
	return *this;
}


//takes two (nxn) matrices and returns their product as a reference to this
Matrix& Matrix::sum( Matrix &a, Matrix & b)  {
	for (register int i=0;i< rows();i++)
		for (register int j=0;j<cols();j++)
		    setItem(i,j, a.item(i,j)*b.item(i,j));
	return *this;
}



Matrix& Matrix::subtractFromI ()  {
	for (register int i=0;i< rows();i++)
	    for (register int j=0;j<cols();j++) {
		if (i==j)
		    setItem(i,j, 1.0 - item(i,j));
		else
		    setItem(i,j, item(i,j));
	    }
	return *this;
}


/* Swaps row A with row B of this matrix */
void Matrix::swapRows(int rowA,int rowB){
    qDebug()<<"   swapRow() "<< rowA+1 << " with " << rowB+1;
    float *tempRow = new  float [ rows() ];
    for ( register int j=0; j<  rows(); j++) {
	  tempRow[j] = item (rowB, j);
	  setItem ( rowB, j, item ( rowA, j ) );
	  setItem ( rowA, j,  tempRow[j] );
      }
    delete [] tempRow;
}




/* Multiply every element of row A by value */
void Matrix::multiplyRow(int row, float value) {
    qDebug()<<"   multiplyRow() "<< row+1 << " by value " << value;
    for ( register int j=0; j<  rows(); j++) {
	  setItem ( row, j,  value * item (row, j) );
	  qDebug()<<"   item("<< row+1 << ","<< j+1 << ") = " <<  item(row,j);
    }
}







/* Inverts given matrix A by Gauss Jordan elimination
   Input:  matrix A
   Output: matrix A becomes unit matrix
   *this becomes the invert of A and is returned back.
*/
Matrix& Matrix::inverseByGaussJordanElimination(Matrix &A){
	qDebug()<< "Matrix::inverseByGaussJordanElimination()";
	int n=A.cols();
	qDebug()<<"Matrix::inverseByGaussJordanElimination() - starting with the identity Matrix; this will become A^-1 in the end";
	identityMatrix( n );
	int l=0, m_pivotLine=0;
	float m_pivot=0, temp_pivot=0, elim_coef=0;

	for ( register int j=0; j< n; j++) { // for n, it is the last diagonal element of A
	    l=j+1;
	    m_pivotLine=-1;
	    m_pivot = A.item(j,j);
	    qDebug() << "inverseByGaussJordanElimination() at column " << j+1
		    << " Initial pivot " << m_pivot ;
	    for ( register int i=l; i<n; i++) {
		temp_pivot = A.item(i,j);
		if ( fabs( temp_pivot ) > fabs ( m_pivot ) ) {
		    qDebug() << " A("<< i+1 << ","<< j+1  << ") = " <<  temp_pivot
			    << " absolutely larger than current pivot "<< m_pivot
			    << ". Marking new pivot line: " << i+1;
		    m_pivotLine=i;
		    m_pivot = temp_pivot ;
		}
	    }
	    if ( m_pivotLine != -1 ) {
		A.swapRows(m_pivotLine,j);
		swapRows(m_pivotLine,j);
	    }


	    qDebug()<<"   multiplyRow() "<< j+1 << " by value " << 1/m_pivot ;
	    for ( register int k=0; k<  rows(); k++) {
		A.setItem ( j, k,  (1/m_pivot) * A.item (j, k) );
		setItem ( j, k,  (1/m_pivot) * item (j, k) );
		qDebug()<<"   A.item("<< j+1 << ","<< k+1 << ") = " <<  A.item(j,k);
		qDebug()<<"   item("<< j+1 << ","<< k+1 << ") = " <<  item(j,k);
	    }

	    qDebug() << "eliminate variables FromRowsBelow()" << j+1 ;
	    for ( register int i=0; i<  rows(); i++) {
		 qDebug()<<"   Eliminating item("<< i+1 << ","<< j+1 << ") = "
			 <<  A.item(i,j) << " while at column j="<<j+1;
		 if ( A.item(i,j)==0 ){
		    qDebug()<< " ...already eliminated - continue";
		    continue;
		}
		 if ( i == j){
		     qDebug()<< " ...skip pivotline - continue";
		    continue;
		}
		elim_coef=A.item (i, j);
		for ( register int k=0; k<  cols(); k++) {
		    qDebug()<<"   A.item("<< i+1 << ","<< k+1 << ") = " <<  A.item(i,k)
			    << " will be subtracted by " << " A.item ("<< i+1
			    << ", "<< j+1 << ") x A.item(" << j+1 << ","<<k+1
			    <<") =" << elim_coef * A.item(j,k) ;
		    A.setItem ( i, k,   A.item (i, k) -  elim_coef * A.item(j, k)  );
		    qDebug()<<"   A.item("<< i+1 << ","<< k+1 << ") = " <<  A.item(i,k);

		    qDebug()<<"   item("<< i+1 << ","<< k+1 << ") = " <<  item(i,k)
			    << " will be subtracted by " << " A.item ("<< i+1
			    << ", "<< j+1 << ") x item(" << j+1 << ","<<k+1
			    <<") =" << elim_coef * item(j,k)  <<  " = "
			    << elim_coef << " x " << item(j,k) ;

		    setItem ( i, k,   item (i, k) -  elim_coef * item(j, k)  );
		    qDebug()<<"   item("<< i+1 << ","<< k+1 << ") = " <<  item(i,k);

		}
	    }

	}

	return *this;
}