matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  SocNetV: Social Network Visualizer
3  version: 2.5
4  Written in Qt
5 
6  matrix.h - description
7  -------------------
8  copyright : (C) 2005-2019 by Dimitris B. Kalamaras
9  project site : https://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 #include <vector>
38 
39 using namespace std; //or else compiler groans for nothrow
40 
41 class QTextStream;
42 
43 
44 #ifdef Q_OS_WIN32
45 static const QString infinity = "\u221E" ;
46 #else
47 static const QString infinity = QString("\xE2\x88\x9E") ;
48 #endif
49 
50 
51 static const int METRIC_NONE = -1;
52 static const int METRIC_SIMPLE_MATCHING = 0;
53 static const int METRIC_JACCARD_INDEX = 1;
54 static const int METRIC_HAMMING_DISTANCE = 2;
55 static const int METRIC_COSINE_SIMILARITY = 3;
56 static const int METRIC_EUCLIDEAN_DISTANCE = 4;
57 static const int METRIC_MANHATTAN_DISTANCE= 5;
58 static const int METRIC_PEARSON_COEFFICIENT = 6;
59 static const int METRIC_CHEBYSHEV_MAXIMUM= 7;
60 
61 
62 
63 
64 
65 class MatrixRow {
66 public:
67  MatrixRow (int cols=0) {
68  cell=new (nothrow) qreal [m_cols=cols];
69  Q_CHECK_PTR( cell );
70  for (int i=0;i<m_cols; i++) {
71  cell[i]=0;
72  }
73  }
74 
75  ~MatrixRow() { if (m_cols) delete [] cell; m_cols=0 ; }
76 
77  MatrixRow& operator =(MatrixRow & a) {
78  if (this != &a){
79  if (a.m_cols!=m_cols) {
80  delete [] cell;
81  cell=new (nothrow) qreal[m_cols=a.m_cols];
82  Q_CHECK_PTR( cell);
83  }
84  for (int i=0;i<m_cols; i++) {
85  cell[i]=a.cell[i];
86  }
87  }
88  return *this;
89  }
90 
91  qreal column ( int c ) const {
92  return cell[c];
93  }
94 
95 
96  qreal& operator [] (const int k) { return cell[k]; }
97 
98 
99  void setColumn (int index, qreal elem) {
100  cell[index]=elem;
101  }
102 
103  void clearColumn(int index){
104  cell[index]=0;
105  }
106 
107 
108  void resize(int cols) {
109  delete [] cell;
110  cell=new (nothrow) qreal[m_cols=cols];
111  Q_CHECK_PTR( cell);
112  for (int i=0;i<m_cols; i++) {
113  cell[i]=0;
114  }
115  }
116 
117  void setSize(int cols){
118  m_cols=cols;
119  }
120 
121 private:
122  qreal *cell;
123  int m_cols;
124 };
125 
126 
127 
128 
129 class Matrix {
130 public:
132  Matrix (int rowDim=0, int colDim=0) ;
133 
134  Matrix(const Matrix &b) ; /* Copy constructor allows Matrix a=b */
135 
136  ~Matrix();
137 
138  void clear();
139 
140  void resize (const int m, const int n) ;
141 
142  qreal item( int r, int c ) ;
143 
144  void setItem(const int r, const int c, const qreal elem );
145 
146  //WARNING: operator() is slow! Avoid using it.
147  qreal operator () (const int r, const int c) { return row[r].column(c); }
148 
149  MatrixRow& operator [] (const int &r) { return row[r]; }
150 
151  void clearItem( int r, int c ) ;
152 
153  int cols() {return m_cols;}
154 
155  int rows() {return m_rows;}
156 
157  int size() { return m_rows * m_cols; }
158 
159  void findMinMaxValues(qreal&min, qreal&max, bool &hasRealNumbers);
160 
161  void NeighboursNearestFarthest(qreal&min,qreal&max,
162  int &imin, int &jmin,
163  int &imax, int &jmax);
164 
165  void deleteRowColumn(int i); /* deletes row i and column i */
166 
167  void identityMatrix (int dim);
168 
169  void zeroMatrix (const int m, const int n);
170 
171  void fillMatrix (qreal value );
172 
173  Matrix& subtractFromI () ;
174 
175 
176  Matrix& operator =(Matrix & a);
177 
178  void sum(Matrix &a, Matrix &b) ;
179 
180  void operator +=(Matrix & b);
181 
182  Matrix& operator +(Matrix & b);
183 
184  Matrix& operator -(Matrix & b);
185 
186  Matrix& operator *(Matrix & b);
187  void operator *=(Matrix & b);
188 
189  void product( Matrix &A, Matrix & B, bool symmetry=false) ;
190 
191  Matrix & productSym( Matrix &a, Matrix & b) ;
192 
193  void swapRows(int rowA,int rowB);
194 
195  void multiplyScalar(const qreal &f);
196  void multiplyRow(int row, qreal value);
197 
198  void productByVector (
199  qreal in[],
200  qreal out[],
201  const bool &leftMultiply=false);
202 
203  Matrix & pow (int n, bool symmetry=false) ;
204  Matrix & expBySquaring2 (Matrix &Y, Matrix &X, int n, bool symmetry=false);
205 
206  qreal distanceManhattan(
207  qreal x[],
208  qreal y[],
209  int n);
210  qreal distanceEuclidean(
211  qreal x[],
212  int n);
213 
214  void powerIteration (
215  qreal x[] ,
216  qreal &xsum,
217  qreal &xmax,
218  int &xmaxi,
219  qreal &xmin,
220  int &xmini,
221  const qreal eps, const int &maxIter);
222 
223  Matrix& degreeMatrix();
224 
225  Matrix& laplacianMatrix();
226 
227  Matrix& transpose();
228 
229  Matrix& cocitationMatrix();
230 
231 
232  Matrix& inverseByGaussJordanElimination(Matrix &a);
233 
234  Matrix& inverse(Matrix &a);
235 
236  bool solve(qreal b[]);
237 
238  bool ludcmp (Matrix &a, const int &n, int indx[], qreal &d ) ;
239 
240  void lubksb (Matrix &a, const int &n, int indx[], qreal b[]);
241 
242 
243  Matrix& distancesMatrix(const int &metric,
244  const QString varLocation,
245  const bool &diagonal,
246  const bool &considerWeights);
247 
248  Matrix& similarityMatrix(Matrix &AM,
249  const int &measure,
250  const QString varLocation="Rows",
251  const bool &diagonal=false,
252  const bool &considerWeights=true);
253 
254 
255  Matrix& pearsonCorrelationCoefficients(Matrix &AM,
256  const QString &varLocation="Rows",
257  const bool &diagonal=false);
258 
259 
260  friend QTextStream& operator << (QTextStream& os, Matrix& m);
261  bool printHTMLTable(QTextStream& os,
262  const bool markDiag=false,
263  const bool &plain=false,
264  const bool &printInfinity=true);
265  bool printMatrixConsole(bool debug=true);
266 
267  bool illDefined();
268 
269 private:
271  int m_rows;
272  int m_cols;
273 
274 };
275 
276 
277 
278 
279 
280 #endif
void resize(int cols)
Definition: matrix.h:108
static const int METRIC_JACCARD_INDEX
Definition: matrix.h:53
static const int METRIC_CHEBYSHEV_MAXIMUM
Definition: matrix.h:59
static const int METRIC_NONE
Definition: matrix.h:51
int size()
Definition: matrix.h:157
static const int METRIC_PEARSON_COEFFICIENT
Definition: matrix.h:58
static const int METRIC_MANHATTAN_DISTANCE
Definition: matrix.h:57
MatrixRow * row
Definition: matrix.h:270
int m_cols
Definition: matrix.h:272
qreal column(int c) const
Definition: matrix.h:91
void clearColumn(int index)
Definition: matrix.h:103
static const int METRIC_EUCLIDEAN_DISTANCE
Definition: matrix.h:56
void setColumn(int index, qreal elem)
Definition: matrix.h:99
int m_rows
Definition: matrix.h:271
static const QString infinity
Definition: matrix.h:47
void setSize(int cols)
Definition: matrix.h:117
int rows()
Definition: matrix.h:155
Definition: matrix.h:65
static const int METRIC_COSINE_SIMILARITY
Definition: matrix.h:55
Definition: matrix.h:129
qreal * cell
Definition: matrix.h:122
~MatrixRow()
Definition: matrix.h:75
MatrixRow(int cols=0)
Definition: matrix.h:67
int cols()
Definition: matrix.h:153
int m_cols
Definition: matrix.h:123
QTextStream & operator<<(QTextStream &os, Matrix &m)
Prints matrix m to given textstream.
Definition: matrix.cpp:2384
static const int METRIC_HAMMING_DISTANCE
Definition: matrix.h:54
static const int METRIC_SIMPLE_MATCHING
Definition: matrix.h:52