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