matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  SocNetV: Social Network Visualizer
3  version: 2.2
4  Written in Qt
5 
6  matrix.h - description
7  -------------------
8  copyright : (C) 2005-2017 by Dimitris B. Kalamaras
9  project site : http://socnetv.org
10 
11  ***************************************************************************/
12 
13 
14 /*******************************************************************************
15 * This program is free software: you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation, either version 3 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
27 ********************************************************************************/
28 
29 
30 #ifndef MATRIX_H
31 #define MATRIX_H
32 
33 
34 #include <QtGlobal>
35 #include <QString> //for static const QString declares below
36 #include <utility> // std::pair, std::make_pair
37 
38 using namespace std; //or else compiler groans for nothrow
39 
40 class QTextStream;
41 
42 
43 #ifdef Q_OS_WIN32
44 static const QString infinity = "\u221E" ;
45 #else
46 static const QString infinity = QString("\xE2\x88\x9E") ;
47 #endif
48 
49 
50 static const int METRIC_NONE = -1;
51 static const int METRIC_SIMPLE_MATCHING = 0;
52 static const int METRIC_JACCARD_INDEX = 1;
53 static const int METRIC_HAMMING_DISTANCE = 2;
54 static const int METRIC_COSINE_SIMILARITY = 3;
55 static const int METRIC_EUCLIDEAN_DISTANCE = 4;
56 static const int METRIC_MANHATTAN_DISTANCE= 5;
57 static const int METRIC_PEARSON_COEFFICIENT = 6;
58 static const int METRIC_CHEBYSHEV_MAXIMUM= 7;
59 
60 
61 
62 
63 
64 class MatrixRow {
65 public:
66  MatrixRow (int cols=0) {
67  cell=new (nothrow) float [m_cols=cols];
68  Q_CHECK_PTR( cell );
69  for (int i=0;i<m_cols; i++) {
70  cell[i]=0;
71  }
72  }
73 
74  ~MatrixRow() { m_cols=0 ; delete [] cell;}
75 
76  MatrixRow& operator =(MatrixRow & a) {
77  if (this != &a){
78  if (a.m_cols!=m_cols) {
79  delete [] cell;
80  cell=new (nothrow) float[m_cols=a.m_cols];
81  Q_CHECK_PTR( cell);
82  }
83  for (int i=0;i<m_cols; i++) {
84  cell[i]=a.cell[i];
85  }
86  }
87  return *this;
88  }
89 
90  float column ( int c ) const {
91  return cell[c];
92  }
93 
94 
95  float& operator [] (const int k) { return cell[k]; }
96 
97 
98  void setColumn (int index, float elem) {
99  cell[index]=elem;
100  }
101 
102  void clearColumn(int index){
103  cell[index]=0;
104  }
105 
106 
107  void resize(int cols) {
108  delete [] cell;
109  cell=new (nothrow) float[m_cols=cols];
110  Q_CHECK_PTR( cell);
111  for (int i=0;i<m_cols; i++) {
112  cell[i]=0;
113  }
114  }
115 
116  void setSize(int cols){
117  m_cols=cols;
118  }
119 
120 private:
121  float *cell;
122  int m_cols;
123 };
124 
125 
126 
127 
128 class Matrix {
129 public:
131  Matrix (int rowDim=0, int colDim=0) ;
132 
133  Matrix(const Matrix &b) ; /* Copy constructor allows Matrix a=b */
134 
135  ~Matrix();
136 
137  void clear();
138 
139  void resize (const int m, const int n) ;
140 
141  float item( int r, int c ) ;
142 
143  void setItem(const int r, const int c, const float elem );
144 
145  //WARNING: operator() is slow! Avoid using it.
146  float operator () (const int r, const int c) { return row[r].column(c); }
147 
148  MatrixRow& operator [] (const int &r) { return row[r]; }
149 
150  void clearItem( int r, int c ) ;
151 
152  int cols() {return m_cols;}
153 
154  int rows() {return m_rows;}
155 
156  int size() { return m_rows * m_cols; }
157 
158  void findMinMaxValues(float&min, float&max, bool &hasRealNumbers);
159 
160  void NeighboursNearestFarthest(float&min,float&max,
161  int &imin, int &jmin,
162  int &imax, int &jmax);
163 
164  void deleteRowColumn(int i); /* deletes row i and column i */
165 
166  void identityMatrix (int dim);
167 
168  void zeroMatrix (const int m, const int n);
169 
170  void fillMatrix (float value );
171 
172  Matrix& subtractFromI () ;
173 
174 
175  Matrix& operator =(Matrix & a);
176 
177  void sum(Matrix &a, Matrix &b) ;
178 
179  void operator +=(Matrix & b);
180 
181  Matrix& operator +(Matrix & b);
182 
183  Matrix& operator -(Matrix & b);
184 
185  Matrix& operator *(Matrix & b);
186  void operator *=(Matrix & b);
187 
188  void product( Matrix &A, Matrix & B, bool symmetry=false) ;
189 
190  Matrix & productSym( Matrix &a, Matrix & b) ;
191 
192  void swapRows(int rowA,int rowB);
193 
194  void multiplyScalar(const float &f);
195  void multiplyRow(int row, float value);
196 
197  void productByVector (float in[], float out[], const bool &leftMultiply=false);
198 
199  Matrix & pow (int n, bool symmetry=false) ;
200  Matrix & expBySquaring2 (Matrix &Y, Matrix &X, int n, bool symmetry=false);
201 
202  float distanceManhattan(float x[], float y[] , int n);
203  float distanceEuclidean(float x[], int n);
204 
205  void powerIteration (float x[] ,
206  float &xsum,
207  float &xmax,
208  int &xmaxi,
209  float &xmin,
210  int &xmini,
211  const float eps, const int &maxIter);
212 
213  Matrix& degreeMatrix();
214 
215  Matrix& laplacianMatrix();
216 
217  Matrix& transpose();
218 
219  Matrix& cocitationMatrix();
220 
221 
222  Matrix& inverseByGaussJordanElimination(Matrix &a);
223 
224  Matrix& inverse(Matrix &a);
225 
226  bool ludcmp (Matrix &a, const int &n, int indx[], float &d ) ;
227 
228  void lubksb (Matrix &a, const int &n, int indx[], float b[]);
229 
230 
231  Matrix& distancesMatrix(const int &metric,
232  const QString varLocation,
233  const bool &diagonal,
234  const bool &considerWeights);
235 
236  Matrix& similarityMatrix(Matrix &AM,
237  const int &measure,
238  const QString varLocation="Rows",
239  const bool &diagonal=false,
240  const bool &considerWeights=true);
241 
242 
243  Matrix& pearsonCorrelationCoefficients(Matrix &AM,
244  const QString &varLocation="Rows",
245  const bool &diagonal=false);
246 
247 
248  friend QTextStream& operator << (QTextStream& os, Matrix& m);
249  bool printHTMLTable(QTextStream& os,
250  const bool markDiag=false,
251  const bool &plain=false,
252  const bool &printInfinity=true);
253  bool printMatrixConsole(bool debug=true);
254 
255 private:
257  int m_Actors;
258  int m_rows;
259  int m_cols;
260 
261 };
262 
263 
264 
265 
266 
267 #endif
void resize(int cols)
Definition: matrix.h:107
static const int METRIC_JACCARD_INDEX
Definition: matrix.h:52
float * cell
Definition: matrix.h:121
static const int METRIC_CHEBYSHEV_MAXIMUM
Definition: matrix.h:58
static const int METRIC_NONE
Definition: matrix.h:50
int size()
Definition: matrix.h:156
static const int METRIC_PEARSON_COEFFICIENT
Definition: matrix.h:57
static const int METRIC_MANHATTAN_DISTANCE
Definition: matrix.h:56
MatrixRow * row
Definition: matrix.h:256
int m_cols
Definition: matrix.h:259
void clearColumn(int index)
Definition: matrix.h:102
float column(int c) const
Definition: matrix.h:90
static const int METRIC_EUCLIDEAN_DISTANCE
Definition: matrix.h:55
int m_rows
Definition: matrix.h:258
static const QString infinity
Definition: matrix.h:46
int m_Actors
Definition: matrix.h:257
void setSize(int cols)
Definition: matrix.h:116
void setColumn(int index, float elem)
Definition: matrix.h:98
int rows()
Definition: matrix.h:154
Definition: matrix.h:64
static const int METRIC_COSINE_SIMILARITY
Definition: matrix.h:54
Definition: matrix.h:128
~MatrixRow()
Definition: matrix.h:74
MatrixRow(int cols=0)
Definition: matrix.h:66
int cols()
Definition: matrix.h:152
int m_cols
Definition: matrix.h:122
QTextStream & operator<<(QTextStream &os, Matrix &m)
Prints matrix m to given textstream.
Definition: matrix.cpp:2273
static const int METRIC_HAMMING_DISTANCE
Definition: matrix.h:53
static const int METRIC_SIMPLE_MATCHING
Definition: matrix.h:51