Matrix Class Reference

#include <matrix.h>

Collaboration diagram for Matrix:

## Public Member Functions

Matrix (int rowDim=0, int colDim=0)
Matrix::Matrix Default constructor - creates a Matrix of given dimension (0x0) Use resize(m,n) or zeromatrix(m,n) to resize it. More...

Matrix (const Matrix &b)
Matrix::Matrix Copy constructor. Creates a Matrix identical to Matrix b Allows Matrix a=b declaration Every MatrixRow object holds max_int=32762. More...

~Matrix ()
Matrix::~Matrix Destructor. More...

void clear ()
Clears data. More...

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 holds max_int=32762. More...

float item (int r, int c)
Returns the (r,c) matrix element. More...

void setItem (const int r, const int c, const float elem)
Sets the (r,c) matrix element calling the setColumn method. More...

float operator() (const int r, const int c)

MatrixRowoperator[] (const int &r)

void clearItem (int r, int c)
Clears the (r,c) matrix element. More...

int cols ()

int rows ()

int size ()

void findMinMaxValues (float &min, float &max, bool &hasRealNumbers)
finds Min-Max values in current Matrix More...

void NeighboursNearestFarthest (float &min, float &max, int &imin, int &jmin, int &imax, int &jmax)
Like Matrix::findMinMaxValues only it skips r==c. More...

void deleteRowColumn (int i)
Deletes row and column and shifts rows and cols accordingly. More...

void identityMatrix (int dim)
Makes this square matrix the identity square matrix I. More...

void zeroMatrix (const int m, const int n)
Makes this matrix the zero matrix of size mxn. More...

void fillMatrix (float value)
Fills a matrix with a given value. More...

MatrixsubtractFromI ()
Subtracts this matrix from I and returns. More...

Matrixoperator= (Matrix &a)
Matrix equality/assignment , operator = Allows copying a matrix onto another using b=a where b,a matrices Equals two matrices. More...

void sum (Matrix &a, Matrix &b)
Matrix addition Takes two (nxn) matrices and returns their sum as a reference to this Same algorithm as operator +, just different interface. In this case, you use something like: c.sum(a,b) More...

void operator+= (Matrix &b)
Matrix::operator += Matrix add another matrix: += Adds to this matrix another matrix B of the same dim and returns to this Allows A+=B. More...

Matrixoperator+ (Matrix &b)
Matrix addition, operator + Adds this matrix and B of the same dim and returns the sum S Allows S = A+B. More...

Matrixoperator- (Matrix &b)
Matrix subtraction, operator - Subtract this matrix - B of the same dim and returns the result S Allows S = A-B. More...

Matrixoperator* (Matrix &b)
Matrix multiplication, operator * Multiplies (right) this matrix with given matrix b. Allows P = A * B where A,B of same dimension and returns product as a reference to the calling object. More...

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 matrix which becomes an m x p matrix. This convenience operator *= allows A *= B. More...

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 the calling matrix which becomes an m x p matrix Allows P.product(A, B) More...

MatrixproductSym (Matrix &a, Matrix &b)
Takes two ( N x N ) matrices (symmetric) and outputs an upper triangular matrix. More...

void swapRows (int rowA, int rowB)
Swaps row A with row B of this matrix. More...

void multiplyScalar (const float &f)
Scalar Multiplication. Multiplies this by float f and returns the product matrix of the same dim Allows to use P.multiplyScalar(f) More...

void multiplyRow (int row, float value)
Multiply every element of row by value. More...

void productByVector (float in[], float out[], const bool &leftMultiply=false)
Calculates the matrix-by-vector product Ax of this matrix Default product: Ax if leftMultiply=true then it returns the left product xA. More...

Matrixpow (int n, bool symmetry=false)
Returns the n-nth power of this matrix. More...

MatrixexpBySquaring2 (Matrix &Y, Matrix &X, int n, bool symmetry=false)
Recursive algorithm implementing "Exponentiation by squaring". Also known as Fast Modulo Multiplication, this algorithm allows fast computation of a large power n of square matrix X. More...

float distanceManhattan (float x[], float y[], int n)
Helper function, takes to vectors and returns their Manhattan distance (also known as l1 norm, Taxicab or L1 distance) which is the sum of the absolute differences of their coordinates. More...

float distanceEuclidean (float x[], int n)
Helper function, computes the Euclideian length (also known as L2 distance) of a vector: if x = (x1 x2 ... xn), then ||x|| = square_root(x1*x1 + x2*x2 + ... + xn*xn) More...

void powerIteration (float x[], float &xsum, float &xmax, int &xmaxi, float &xmin, int &xmini, const float eps, const int &maxIter)
Implementation of the Power method which computes the leading eigenvector (eigenvector centrality) of this matrix and returns it as vector x. More...

MatrixdegreeMatrix ()
Returns the Degree Matrix of this matrix. The Degree Matrix is diagonal matrix which contains information about the degree of each graph vertex (row of the adjacency matrix) Allows S = A.degreeMatrix() More...

MatrixlaplacianMatrix ()
Returns the Laplacian of this matrix. The Laplacian is a NxN matrix L = D - A where D is the degree matrix of A Allows S = A.laplacianMatrix() More...

Matrixtranspose ()
Returns the Transpose of this matrix Allows T = A.transpose() More...

MatrixcocitationMatrix ()
Returns the Cocitation Matrix of this matrix (C = A * A^T) Allows T = A.cocitationMatrix() More...

MatrixinverseByGaussJordanElimination (Matrix &a)
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. More...

Matrixinverse (Matrix &a)
Computes and returns the inverse of given matrix a Allows b.inverse(a) More...

bool solve (float b[])
Computes and returns the solution of the set of n linear equations A·x = b Allows A.solve(b) More...

bool ludcmp (Matrix &a, const int &n, int indx[], float &d)
Given matrix a, it replaces a by the LU decomposition of a rowwise permutation of itself. Used in combination with lubksb to solve linear equations or invert a matrix. More...

void lubksb (Matrix &a, const int &n, int indx[], float b[])
Solves the set of n linear equations A·X = b, where A nxn matrix decomposed as L·U (L lower triangular and U upper triangular) by forward substitution and backsubstitution. More...

MatrixdistancesMatrix (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 user defined metric. More...

MatrixsimilarityMatrix (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, based on the given matching measure and returns the similarity matrix. More...

MatrixpearsonCorrelationCoefficients (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. More...

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.e. when we have deleted a node/vertex. More...

bool printMatrixConsole (bool debug=true)
Prints this matrix to stderr or stdout. More...

bool illDefined ()
Checks if matrix is ill-defined (contains at least an inf element) More...

MatrixRowrow

int m_rows

int m_cols

## Friends

QTextStream & operator<< (QTextStream &os, Matrix &m)
Prints matrix m to given textstream. More...

## Constructor & Destructor Documentation

 Matrix::Matrix ( int rowDim = 0, int colDim = 0 )

Matrix::Matrix Default constructor - creates a Matrix of given dimension (0x0) Use resize(m,n) or zeromatrix(m,n) to resize it.

default constructor - default rows = cols = 0

Parameters
 Actors
 Matrix::Matrix ( const Matrix & b )

Matrix::Matrix Copy constructor. Creates a Matrix identical to Matrix b Allows Matrix a=b declaration Every MatrixRow object holds max_int=32762.

Parameters
 b
 Matrix::~Matrix ( )

Matrix::~Matrix Destructor.

## Member Function Documentation

 void Matrix::clear ( )

Clears data.

 void Matrix::clearItem ( int r, int c )

Clears the (r,c) matrix element.

Parameters
 r c
 Matrix & Matrix::cocitationMatrix ( )

Returns the Cocitation Matrix of this matrix (C = A * A^T) Allows T = A.cocitationMatrix()

Parameters
 b
Returns
Matrix T
 int Matrix::cols ( )
inline
 Matrix & Matrix::degreeMatrix ( )

Returns the Degree Matrix of this matrix. The Degree Matrix is diagonal matrix which contains information about the degree of each graph vertex (row of the adjacency matrix) Allows S = A.degreeMatrix()

Parameters
 b
Returns
Matrix S
 void Matrix::deleteRowColumn ( int erased )

Deletes row and column and shifts rows and cols accordingly.

Parameters
 erased row/col to delete
 float Matrix::distanceEuclidean ( float x[], int n )

Helper function, computes the Euclideian length (also known as L2 distance) of a vector: if x = (x1 x2 ... xn), then ||x|| = square_root(x1*x1 + x2*x2 + ... + xn*xn)

Parameters
 x n
Returns
 float Matrix::distanceManhattan ( float x[], float y[], int n )

Helper function, takes to vectors and returns their Manhattan distance (also known as l1 norm, Taxicab or L1 distance) which is the sum of the absolute differences of their coordinates.

Parameters
 x y
Returns
 Matrix & 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 user defined metric.

Parameters
 metric varLocation diagonal considerWeights
Returns
 Matrix & Matrix::expBySquaring2 ( Matrix & Y, Matrix & X, int n, bool symmetry = false )

Recursive algorithm implementing "Exponentiation by squaring". Also known as Fast Modulo Multiplication, this algorithm allows fast computation of a large power n of square matrix X.

Parameters
 Y must be the Identity matrix on first call X the matrix to be powered n the power symmetry
Returns
Matrix&

On first call, parameters must be: Y=I, X the orginal matrix to power and n the power. Returns the power of matrix X to this object. For n > 4 it is more efficient than naively multiplying the base with itself repeatedly.

 void Matrix::fillMatrix ( float value )

Fills a matrix with a given value.

Parameters
 value
 void Matrix::findMinMaxValues ( float & min, float & max, bool & hasRealNumbers )

finds Min-Max values in current Matrix

Parameters
 min value in the matrix max value Complexity: O(n^2)
 void Matrix::identityMatrix ( int dim )

Makes this square matrix the identity square matrix I.

Parameters
 dim
 bool Matrix::illDefined ( )

Checks if matrix is ill-defined (contains at least an inf element)

Returns
 Matrix & Matrix::inverse ( Matrix & a )

Computes and returns the inverse of given matrix a Allows b.inverse(a)

Parameters
 a
Returns
 Matrix & Matrix::inverseByGaussJordanElimination ( Matrix & A )

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.

Parameters
 A
Returns
inverse matrix of A
 float Matrix::item ( int r, int c )

Returns the (r,c) matrix element.

Parameters
 r c
Returns
 Matrix & Matrix::laplacianMatrix ( )

Returns the Laplacian of this matrix. The Laplacian is a NxN matrix L = D - A where D is the degree matrix of A Allows S = A.laplacianMatrix()

Parameters
 b
Returns
Matrix S
 void Matrix::lubksb ( Matrix & a, const int & n, int indx[], float b[] )

Solves the set of n linear equations A·X = b, where A nxn matrix decomposed as L·U (L lower triangular and U upper triangular) by forward substitution and backsubstitution.

Given A = L·U we have A · x = (L · U) · x = L · (U · x) = b So, this routine first solves L · y = b for the vector y by forward substitution and then solves U · x = y for the vector x using backsubstitution

Parameters
 a input matrix a as the LU decomposition of A, returned by the routine ludcmp n input size of matrix indx input vector, records the row permutation, returned by the routine ludcmp b input array as the right-hand side vector B, and output with the solution vector X
Returns
:

a, n, and indx are not modified by this routine and can be left in place for successive calls with different right-hand sides b. This routine takes into account the possibility that b will begin with many zero elements, so it is efficient for use in matrix inversion.

Code adapted from Knuth's Numerical Recipes in C, pp 47

 bool Matrix::ludcmp ( Matrix & a, const int & n, int indx[], float & d )

Given matrix a, it replaces a by the LU decomposition of a rowwise permutation of itself. Used in combination with lubksb to solve linear equations or invert a matrix.

Parameters
 a input matrix n x n and output arranged as in Knuth's equation (2.3.14) n input size of matrix indx output vector, records the row permutation effected by the partial pivoting d output as ±1 depending on whether the number of row interchanges was even or odd
Returns
:

Code adapted from Knuth's Numerical Recipes in C, pp 46

 void Matrix::multiplyRow ( int row, float value )

Multiply every element of row by value.

Parameters
 row value
 void Matrix::multiplyScalar ( const float & f )

Scalar Multiplication. Multiplies this by float f and returns the product matrix of the same dim Allows to use P.multiplyScalar(f)

Parameters
 f
 void Matrix::NeighboursNearestFarthest ( float & min, float & max, int & imin, int & jmin, int & imax, int & jmax )

Like Matrix::findMinMaxValues only it skips r==c.

Parameters
 min value. If (r,c) = minimum, it mean that neighbors r and c are the nearest in the matrix/network max value Complexity: O(n^2)
 float Matrix::operator() ( const int r, const int c )
inline
 Matrix & Matrix::operator* ( Matrix & b )

Matrix multiplication, operator * Multiplies (right) this matrix with given matrix b. Allows P = A * B where A,B of same dimension and returns product as a reference to the calling object.

Parameters
 b symmetry
Returns
 void Matrix::operator*= ( Matrix & b )

Multiplies (right) this m x n matrix with given n x p matrix b and returns the product in the calling matrix which becomes an m x p matrix. This convenience operator *= allows A *= B.

Parameters
 b symmetry
Returns
 Matrix & Matrix::operator+ ( Matrix & b )

Matrix addition, operator + Adds this matrix and B of the same dim and returns the sum S Allows S = A+B.

Parameters
 b
Returns
Matrix S
 void Matrix::operator+= ( Matrix & b )

Matrix::operator += Matrix add another matrix: += Adds to this matrix another matrix B of the same dim and returns to this Allows A+=B.

Parameters
 b
Returns
this
 Matrix & Matrix::operator- ( Matrix & b )

Matrix subtraction, operator - Subtract this matrix - B of the same dim and returns the result S Allows S = A-B.

Parameters
 b
Returns
Matrix S
 Matrix & Matrix::operator= ( Matrix & a )

Matrix equality/assignment , operator = Allows copying a matrix onto another using b=a where b,a matrices Equals two matrices.

Parameters
 a
Returns
 MatrixRow& Matrix::operator[] ( const int & r )
inline
 Matrix & 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.

Parameters
 AM Matrix
Returns
Matrix nxn with PPC values for every pair of rows/columns of AM
 Matrix & Matrix::pow ( int n, bool symmetry = false )

Returns the n-nth power of this matrix.

Parameters
 n symmetry
Returns
Matrix
 void Matrix::powerIteration ( float x[], float & xsum, float & xmax, int & xmaxi, float & xmin, int & xmini, const float eps, const int & maxIter )

Implementation of the Power method which computes the leading eigenvector (eigenvector centrality) of this matrix and returns it as vector x.

Parameters
 n x
 bool Matrix::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.e. when we have deleted a node/vertex.

Parameters
 os debug
Returns
 bool Matrix::printMatrixConsole ( bool debug = true )

Prints this matrix to stderr or stdout.

Returns
 void Matrix::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 the calling matrix which becomes an m x p matrix Allows P.product(A, B)

Parameters
 A B symmetry
Returns
i x k matrix
 void Matrix::productByVector ( float in[], float out[], const bool & leftMultiply = false )

Calculates the matrix-by-vector product Ax of this matrix Default product: Ax if leftMultiply=true then it returns the left product xA.

Parameters
 in input array/vector out output array leftMultiply
 Matrix & Matrix::productSym ( Matrix & a, Matrix & b )

Takes two ( N x N ) matrices (symmetric) and outputs an upper triangular matrix.

Parameters
 a b
Returns
 void Matrix::resize ( const int m, const int n )

Resizes this matrix to m x n Called before every operation on new matrices. Every MatrixRow object holds max_int=32762.

Parameters
 Actors
 int Matrix::rows ( )
inline
 void Matrix::setItem ( const int r, const int c, const float elem )

Sets the (r,c) matrix element calling the setColumn method.

Parameters
 r c elem
 Matrix & 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, based on the given matching measure and returns the similarity matrix.

Parameters
 AM Matrix
Returns
Matrix nxn with matching scores for every pair of rows/columns of AM
 int Matrix::size ( )
inline
 bool Matrix::solve ( float b[] )

Computes and returns the solution of the set of n linear equations A·x = b Allows A.solve(b)

Parameters
 b vector
Returns
 Matrix & Matrix::subtractFromI ( )

Subtracts this matrix from I and returns.

Returns
I-this to this matrix
 void Matrix::sum ( Matrix & a, Matrix & b )

Matrix addition Takes two (nxn) matrices and returns their sum as a reference to this Same algorithm as operator +, just different interface. In this case, you use something like: c.sum(a,b)

Parameters
 a b
Returns
 void Matrix::swapRows ( int rowA, int rowB )

Swaps row A with row B of this matrix.

Parameters
 rowA rowB
 Matrix & Matrix::transpose ( )

Returns the Transpose of this matrix Allows T = A.transpose()

Parameters
 b
Returns
Matrix T
 void Matrix::zeroMatrix ( const int m, const int n )

Makes this matrix the zero matrix of size mxn.

Parameters
 m n

## Friends And Related Function Documentation

 QTextStream& operator<< ( QTextStream & os, Matrix & m )
friend

Prints matrix m to given textstream.

Parameters
 os m
Returns

## Member Data Documentation

 int Matrix::m_cols
private
 int Matrix::m_rows
private
 MatrixRow* Matrix::row
private

The documentation for this class was generated from the following files: