Code Documentation 3.1
Social Network Visualizer
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1
17#ifndef MATRIX_H
18#define MATRIX_H
19
20#include <QtGlobal>
21#include <QString> //for static const QString declares below
22#include <utility> // std::pair, std::make_pair
23#include <vector>
24
25using namespace std; //or else compiler groans for nothrow
26
27class QTextStream;
28
29
30#ifdef Q_OS_WIN32
31static const QString infinity = "\u221E" ;
32#else
33static const QString infinity = QString("\xE2\x88\x9E") ;
34#endif
35
36
37static const int METRIC_NONE = -1;
38static const int METRIC_SIMPLE_MATCHING = 0;
39static const int METRIC_JACCARD_INDEX = 1;
40static const int METRIC_HAMMING_DISTANCE = 2;
41static const int METRIC_COSINE_SIMILARITY = 3;
42static const int METRIC_EUCLIDEAN_DISTANCE = 4;
43static const int METRIC_MANHATTAN_DISTANCE= 5;
44static const int METRIC_PEARSON_COEFFICIENT = 6;
45static const int METRIC_CHEBYSHEV_MAXIMUM= 7;
46
47
48
49
50
51class MatrixRow {
52public:
53 MatrixRow (int cols=0) {
54 cell=new (nothrow) qreal [m_cols=cols];
55 Q_CHECK_PTR( cell );
56 for (int i=0;i<m_cols; i++) {
57 cell[i]=0;
58 }
59 }
60
61 ~MatrixRow() { if (m_cols) delete [] cell; m_cols=0 ; }
62
64 if (this != &a){
65 if (a.m_cols!=m_cols) {
66 delete [] cell;
67 cell=new (nothrow) qreal[m_cols=a.m_cols];
68 Q_CHECK_PTR( cell);
69 }
70 for (int i=0;i<m_cols; i++) {
71 cell[i]=a.cell[i];
72 }
73 }
74 return *this;
75 }
76
77 qreal column ( int c ) const {
78 return cell[c];
79 }
80
81
82 qreal& operator [] (const int k) { return cell[k]; }
83
84
85 void setColumn (int index, qreal elem) {
86 cell[index]=elem;
87 }
88
89 void clearColumn(int index){
90 cell[index]=0;
91 }
92
93
94 void resize(int cols) {
95 delete [] cell;
96 cell=new (nothrow) qreal[m_cols=cols];
97 Q_CHECK_PTR( cell);
98 for (int i=0;i<m_cols; i++) {
99 cell[i]=0;
100 }
101 }
102
103 void setSize(int cols){
104 m_cols=cols;
105 }
106
107private:
108 qreal *cell;
110};
111
112
113
114
115class Matrix {
116public:
118 Matrix (int rowDim=0, int colDim=0) ;
119
120 Matrix(const Matrix &b) ; /* Copy constructor allows Matrix a=b */
121
122 ~Matrix();
123
124 void clear();
125
126 void resize (const int m, const int n) ;
127
128 qreal item( int r, int c ) ;
129
130 void setItem(const int r, const int c, const qreal elem );
131
132 //WARNING: operator() is slow! Avoid using it.
133 qreal operator () (const int r, const int c) { return row[r].column(c); }
134
135 MatrixRow& operator [] (const int &r) { return row[r]; }
136
137 void clearItem( int r, int c ) ;
138
139 int cols() {return m_cols;}
140
141 int rows() {return m_rows;}
142
143 int size() { return m_rows * m_cols; }
144
145 void findMinMaxValues(qreal&min, qreal&max, bool &hasRealNumbers);
146
147 void NeighboursNearestFarthest(qreal&min,qreal&max,
148 int &imin, int &jmin,
149 int &imax, int &jmax);
150
151 void deleteRowColumn(int i); /* deletes row i and column i */
152
153 void identityMatrix (int dim);
154
155 void zeroMatrix (const int m, const int n);
156
157 void fillMatrix (qreal value );
158
160
161
163
164 void sum(Matrix &a, Matrix &b) ;
165
166 void operator +=(Matrix & b);
167
169
171
173 void operator *=(Matrix & b);
174
175 void product( Matrix &A, Matrix & B, bool symmetry=false) ;
176
177 Matrix & productSym( Matrix &a, Matrix & b) ;
178
179 void swapRows(int rowA,int rowB);
180
181 void multiplyScalar(const qreal &f);
182 void multiplyRow(int row, qreal value);
183
184 void productByVector (
185 qreal in[],
186 qreal out[],
187 const bool &leftMultiply=false);
188
189 Matrix & pow (int n, bool symmetry=false) ;
190 Matrix & expBySquaring2 (Matrix &Y, Matrix &X, int n, bool symmetry=false);
191
192 qreal distanceManhattan(
193 qreal x[],
194 qreal y[],
195 int n);
196 qreal distanceEuclidean(
197 qreal x[],
198 int n);
199
200 void powerIteration (
201 qreal x[] ,
202 qreal &xsum,
203 qreal &xmax,
204 int &xmaxi,
205 qreal &xmin,
206 int &xmini,
207 const qreal eps, const int &maxIter);
208
210
212
213 Matrix& transpose();
214
216
217
219
220 Matrix& inverse(Matrix &a);
221
222 bool solve(qreal b[]);
223
224 bool ludcmp (Matrix &a, const int &n, int indx[], qreal &d ) ;
225
226 void lubksb (Matrix &a, const int &n, int indx[], qreal b[]);
227
228
229 Matrix& distancesMatrix(const int &metric,
230 const QString varLocation,
231 const bool &diagonal,
232 const bool &considerWeights);
233
235 const int &measure,
236 const QString varLocation="Rows",
237 const bool &diagonal=false,
238 const bool &considerWeights=true);
239
240
242 const QString &varLocation="Rows",
243 const bool &diagonal=false);
244
245
246 friend QTextStream& operator << (QTextStream& os, Matrix& m);
247 bool printHTMLTable(QTextStream& os,
248 const bool markDiag=false,
249 const bool &plain=false,
250 const bool &printInfinity=true);
251 bool printMatrixConsole(bool debug=true);
252
253 bool illDefined();
254
255private:
259
260};
261
262
263
264
265
266#endif
Definition matrix.h:51
void setSize(int cols)
Definition matrix.h:103
void setColumn(int index, qreal elem)
Definition matrix.h:85
qreal * cell
Definition matrix.h:108
qreal & operator[](const int k)
Definition matrix.h:82
MatrixRow(int cols=0)
Definition matrix.h:53
qreal column(int c) const
Definition matrix.h:77
~MatrixRow()
Definition matrix.h:61
void clearColumn(int index)
Definition matrix.h:89
int m_cols
Definition matrix.h:109
void resize(int cols)
Definition matrix.h:94
MatrixRow & operator=(MatrixRow &a)
Definition matrix.h:63
Definition matrix.h:115
void operator*=(Matrix &b)
Multiplies (right) this m x n matrix with given n x p matrix b and returns the product in the calling...
Definition matrix.cpp:533
friend QTextStream & operator<<(QTextStream &os, Matrix &m)
Prints matrix m to given textstream.
Definition matrix.cpp:2375
Matrix & expBySquaring2(Matrix &Y, Matrix &X, int n, bool symmetry=false)
Recursive algorithm implementing "Exponentiation by squaring". Also known as Fast Modulo Multiplicati...
Definition matrix.cpp:675
void sum(Matrix &a, Matrix &b)
Matrix addition Takes two (nxn) matrices and returns their sum as a reference to this Same algorithm ...
Definition matrix.cpp:428
bool printHTMLTable(QTextStream &os, const bool markDiag=false, const bool &plain=false, const bool &printInfinity=true)
Prints this matrix as HTML table This has the problem that the real actorNumber != elementLabel i....
Definition matrix.cpp:2498
void NeighboursNearestFarthest(qreal &min, qreal &max, int &imin, int &jmin, int &imax, int &jmax)
Like Matrix::findMinMaxValues only it skips r==c.
Definition matrix.cpp:144
void multiplyRow(int row, qreal value)
Multiply every element of row by value.
Definition matrix.cpp:376
Matrix & operator*(Matrix &b)
Matrix multiplication, operator * Multiplies (right) this matrix with given matrix b....
Definition matrix.cpp:498
void powerIteration(qreal x[], qreal &xsum, qreal &xmax, int &xmaxi, qreal &xmin, int &xmini, const qreal eps, const int &maxIter)
Implementation of the Power method which computes the leading eigenvector x of this matrix,...
Definition matrix.cpp:808
Matrix & inverse(Matrix &a)
Computes and returns the inverse of given matrix a Allows b.inverse(a)
Definition matrix.cpp:1256
void fillMatrix(qreal value)
Fills a matrix with a given value.
Definition matrix.cpp:307
int cols()
Definition matrix.h:139
Matrix & cocitationMatrix()
Returns the Cocitation Matrix of this matrix (C = A * A^T) Allows T = A.cocitationMatrix()
Definition matrix.cpp:936
void resize(const int m, const int n)
Resizes this matrix to m x n Called before every operation on new matrices. Every MatrixRow object ho...
Definition matrix.cpp:94
Matrix & laplacianMatrix()
Returns the Laplacian of this matrix. The Laplacian is a NxN matrix L = D - A where D is the degree m...
Definition matrix.cpp:981
void lubksb(Matrix &a, const int &n, int indx[], qreal b[])
Solves the set of n linear equations A·X = b, where A nxn matrix decomposed as L·U (L lower triangula...
Definition matrix.cpp:1221
void identityMatrix(int dim)
Makes this square matrix the identity square matrix I.
Definition matrix.cpp:170
int m_rows
Definition matrix.h:257
int m_cols
Definition matrix.h:258
MatrixRow * row
Definition matrix.h:256
void setItem(const int r, const int c, const qreal elem)
Sets the (r,c) matrix element calling the setColumn method.
Definition matrix.cpp:228
bool illDefined()
Checks if matrix is ill-defined (contains at least an inf element)
Definition matrix.cpp:2667
qreal distanceEuclidean(qreal x[], int n)
Helper function, computes the Euclideian length (also known as L2 distance) of a vector: if x = (x1 x...
Definition matrix.cpp:778
bool ludcmp(Matrix &a, const int &n, int indx[], qreal &d)
Given matrix a, it replaces a by the LU decomposition of a rowwise permutation of itself....
Definition matrix.cpp:1095
Matrix & distancesMatrix(const int &metric, const QString varLocation, const bool &diagonal, const bool &considerWeights)
Computes the dissimilarities matrix of the variables (rows, columns, both) of this matrix using the u...
Definition matrix.cpp:1355
qreal distanceManhattan(qreal x[], qreal y[], int n)
Helper function, takes to vectors and returns their Manhattan distance (also known as l1 norm,...
Definition matrix.cpp:758
Matrix & productSym(Matrix &a, Matrix &b)
Takes two ( N x N ) matrices (symmetric) and outputs an upper triangular matrix.
Definition matrix.cpp:612
int size()
Definition matrix.h:143
Matrix & pow(int n, bool symmetry=false)
Returns the n-nth power of this matrix.
Definition matrix.cpp:642
Matrix & similarityMatrix(Matrix &AM, const int &measure, const QString varLocation="Rows", const bool &diagonal=false, const bool &considerWeights=true)
Computes the pair-wise matching score of the rows, columns or both of the given matrix AM,...
Definition matrix.cpp:1738
void product(Matrix &A, Matrix &B, bool symmetry=false)
Matrix Multiplication. Given two matrices A (mxn) and B (nxp) computes their product and stores it to...
Definition matrix.cpp:571
Matrix & operator-(Matrix &b)
Matrix subtraction, operator - Subtract this matrix - B of the same dim and returns the result S Allo...
Definition matrix.cpp:478
~Matrix()
Matrix::~Matrix Destructor.
Definition matrix.cpp:69
bool solve(qreal b[])
Computes and returns the solution of the set of n linear equations A·x = b Allows A....
Definition matrix.cpp:1308
Matrix & subtractFromI()
Subtracts this matrix from I and returns.
Definition matrix.cpp:320
MatrixRow & operator[](const int &r)
Definition matrix.h:135
Matrix & inverseByGaussJordanElimination(Matrix &a)
Inverts given matrix A by Gauss Jordan elimination Input: matrix A Output: matrix A becomes unit matr...
Definition matrix.cpp:1003
Matrix & degreeMatrix()
Returns the Degree Matrix of this matrix. The Degree Matrix is diagonal matrix which contains informa...
Definition matrix.cpp:956
Matrix & pearsonCorrelationCoefficients(Matrix &AM, const QString &varLocation="Rows", const bool &diagonal=false)
Computes the Pearson Correlation Coefficient of the rows or the columns of the given matrix AM.
Definition matrix.cpp:2091
void findMinMaxValues(qreal &min, qreal &max, bool &hasRealNumbers)
finds Min-Max values in current Matrix
Definition matrix.cpp:116
void clearItem(int r, int c)
Clears the (r,c) matrix element.
Definition matrix.cpp:239
void operator+=(Matrix &b)
Matrix::operator += Matrix add another matrix: += Adds to this matrix another matrix B of the same di...
Definition matrix.cpp:446
void clear()
Clears data.
Definition matrix.cpp:78
void swapRows(int rowA, int rowB)
Swaps row A with row B of this matrix.
Definition matrix.cpp:339
void zeroMatrix(const int m, const int n)
Makes this matrix the zero matrix of size mxn.
Definition matrix.cpp:191
qreal item(int r, int c)
Returns the (r,c) matrix element.
Definition matrix.cpp:216
void deleteRowColumn(int i)
Deletes row and column and shifts rows and cols accordingly.
Definition matrix.cpp:253
Matrix & transpose()
Returns the Transpose of this matrix Allows T = A.transpose()
Definition matrix.cpp:912
bool printMatrixConsole(bool debug=true)
Prints this matrix to stderr or stdout.
Definition matrix.cpp:2636
int rows()
Definition matrix.h:141
Matrix & operator+(Matrix &b)
Matrix addition, operator + Adds this matrix and B of the same dim and returns the sum S Allows S = A...
Definition matrix.cpp:461
qreal operator()(const int r, const int c)
Definition matrix.h:133
Matrix & operator=(Matrix &a)
Matrix equality/assignment , operator = Allows copying a matrix onto another using b=a where b,...
Definition matrix.cpp:398
void multiplyScalar(const qreal &f)
Scalar Multiplication. Multiplies this by qreal f and returns the product matrix of the same dim Allo...
Definition matrix.cpp:361
void productByVector(qreal in[], qreal out[], const bool &leftMultiply=false)
Calculates the matrix-by-vector product Ax of this matrix Default product: Ax if leftMultiply=true th...
Definition matrix.cpp:724
static const int METRIC_EUCLIDEAN_DISTANCE
Definition matrix.h:42
static const int METRIC_HAMMING_DISTANCE
Definition matrix.h:40
static const int METRIC_NONE
Definition matrix.h:37
static const int METRIC_CHEBYSHEV_MAXIMUM
Definition matrix.h:45
static const QString infinity
Definition matrix.h:33
static const int METRIC_MANHATTAN_DISTANCE
Definition matrix.h:43
static const int METRIC_COSINE_SIMILARITY
Definition matrix.h:41
static const int METRIC_PEARSON_COEFFICIENT
Definition matrix.h:44
static const int METRIC_JACCARD_INDEX
Definition matrix.h:39
static const int METRIC_SIMPLE_MATCHING
Definition matrix.h:38