qupid 0.2
Quasi-Signal Generation Model for Pixelized Detector
Loading...
Searching...
No Matches
QPMatrix Class Reference

Dense m×n matrix of double with hand-rolled linear algebra. More...

#include <QPMatrix.hpp>

Public Member Functions

 QPMatrix ()
 Null matrix: 0×0, no storage.
 
 QPMatrix (const int n)
 n×n zero matrix.
 
 QPMatrix (const int m, const int n)
 m×n zero matrix.
 
 QPMatrix (const int m, const int n, const int npassed,...)
 m×n matrix filled row-major from the variadic arguments.
 
 QPMatrix (QPVector &vec)
 Column vector from a QPVector.
 
 QPMatrix (const QPMatrix &other)
 Deep copy.
 
 QPMatrix (QPVector &vec, int ncomp)
 ncomp×1 column vector from the first min(ncomp, 3) components of vec.
 
QPMatrix & operator= (const QPMatrix &)
 Deep copy assignment; reallocates the storage.
 
 ~QPMatrix ()
 Frees the element storage.
 
QPMatrix Transpose ()
 
double Det ()
 Determinant by recursive Laplace expansion along the first row.
 
double Trace ()
 Sum of the diagonal elements up to min(m, n).
 
QPMatrix SubMatrix (const int i, const int j)
 Minor matrix: this matrix with row i and column j removed.
 
double Cofactor (const int i, const int j)
 Cofactor $ C_{ij} = (-1)^{i+j} \det M_{ij} $ where $ M_{ij} $ is the minor.
 
QPMatrix Inverse ()
 Inverse by the adjugate formula $ A^{-1} = \mathrm{adj}(A)/\det A $.
 
double At (int i)
 Value of linear element i of the row-major storage.
 
double At (int i, int j)
 Value at row i, column j: storage index i·n + j (row-major).
 
double & operator[] (int i)
 Reference to linear element i of the row-major storage.
 
double & operator() (int i)
 Same as operator[].
 
double & operator() (int i, int j)
 Reference to the element addressed as i·m + j.
 
bool IsInside (int i)
 
bool IsInside (int i, int j)
 
bool IsSameShape (QPMatrix &other)
 
bool IsTransShape (QPMatrix &other)
 
std::string Print (bool quite=false)
 Formats the matrix as tab-separated rows.
 
QPMatrix & operator*= (const double &other)
 Multiply every element in place.
 

Protected Member Functions

void Freeing ()
 Releases the element storage.
 

Friends

QPMatrix operator* (QPMatrix, double)
 Scaled copy.
 
QPMatrix operator* (double, QPMatrix)
 Scaled copy.
 
QPMatrix operator* (QPMatrix, QPMatrix)
 Matrix product.
 
QPMatrix operator+ (QPMatrix &, QPMatrix &)
 Element-wise sum.
 
QPMatrix operator- (QPMatrix &, QPMatrix &)
 Element-wise difference.
 

Detailed Description

Dense m×n matrix of double with hand-rolled linear algebra.

Storage is a single row-major array; element (i, j) sits at index i·n + j, which is what At(int,int) uses.

Warning
operator()(int,int) addresses the storage with the wrong stride (the row count m instead of the column count n); the two accessors agree only for square matrices. See operator()(int,int).

Definition at line 15 of file QPMatrix.hpp.

Constructor & Destructor Documentation

◆ QPMatrix() [1/7]

QPMatrix::QPMatrix ( )

Null matrix: 0×0, no storage.

Definition at line 10 of file QPMatrix.cpp.

Referenced by Inverse(), and SubMatrix().

◆ QPMatrix() [2/7]

QPMatrix::QPMatrix ( const int  n)

n×n zero matrix.

(The historical comment called this a unit matrix; the implementation zero-fills and never sets the diagonal.)

Parameters
nnumber of rows and columns

Definition at line 14 of file QPMatrix.cpp.

◆ QPMatrix() [3/7]

QPMatrix::QPMatrix ( const int  m,
const int  n 
)

m×n zero matrix.

Parameters
mnumber of rows
nnumber of columns

Definition at line 18 of file QPMatrix.cpp.

◆ QPMatrix() [4/7]

QPMatrix::QPMatrix ( const int  m,
const int  n,
const int  npassed,
  ... 
)

m×n matrix filled row-major from the variadic arguments.

Parameters
mnumber of rows
nnumber of columns
npassednumber of variadic values that follow
Warning
The variadic values are read as double: pass double literals (0. not 0), or the values are garbage.

Definition at line 23 of file QPMatrix.cpp.

◆ QPMatrix() [5/7]

QPMatrix::QPMatrix ( QPVector &  vec)

Column vector from a QPVector.

Warning
Declared but never defined (no implementation in QPMatrix.cpp): using it fails at link time. Use QPMatrix(QPVector&, int) instead.
Parameters
vecsource vector

◆ QPMatrix() [6/7]

QPMatrix::QPMatrix ( const QPMatrix &  other)

Deep copy.

Parameters
othersource matrix

Definition at line 43 of file QPMatrix.cpp.

◆ QPMatrix() [7/7]

QPMatrix::QPMatrix ( QPVector &  vec,
int  ncomp 
)

ncomp×1 column vector from the first min(ncomp, 3) components of vec.

Parameters
vecsource vector
ncompnumber of rows

Definition at line 36 of file QPMatrix.cpp.

◆ ~QPMatrix()

QPMatrix::~QPMatrix ( )

Frees the element storage.

Definition at line 66 of file QPMatrix.cpp.

References Freeing().

Member Function Documentation

◆ operator=()

QPMatrix & QPMatrix::operator= ( const QPMatrix &  other)

Deep copy assignment; reallocates the storage.

Parameters
othersource matrix
Returns
this matrix

Definition at line 52 of file QPMatrix.cpp.

References Freeing().

◆ Transpose()

QPMatrix QPMatrix::Transpose ( )
Returns
transposed copy

Definition at line 227 of file QPMatrix.cpp.

References At().

Referenced by QPMath::Gaussian::MatrixForm2D().

◆ Det()

double QPMatrix::Det ( )

Determinant by recursive Laplace expansion along the first row.

Square matrices only: otherwise prints a message and returns 0. A 0×0 matrix also returns 0.

Returns
determinant

Definition at line 146 of file QPMatrix.cpp.

References At(), Det(), and SubMatrix().

Referenced by Cofactor(), Det(), Inverse(), and QPMath::Gaussian::MatrixForm2D().

◆ Trace()

double QPMatrix::Trace ( )

Sum of the diagonal elements up to min(m, n).

Reads through operator()(int,int), so non-square matrices are affected by its stride defect.

Returns
trace

Definition at line 167 of file QPMatrix.cpp.

References operator()().

◆ SubMatrix()

QPMatrix QPMatrix::SubMatrix ( const int  i,
const int  j 
)

Minor matrix: this matrix with row i and column j removed.

Writes through operator()(int,int) while reading through At, so the result is correct only for square input.

Parameters
irow to remove
jcolumn to remove
Returns
(m-1)×(n-1) matrix

Definition at line 177 of file QPMatrix.cpp.

References At(), and QPMatrix().

Referenced by Cofactor(), and Det().

◆ Cofactor()

double QPMatrix::Cofactor ( const int  i,
const int  j 
)

Cofactor $ C_{ij} = (-1)^{i+j} \det M_{ij} $ where $ M_{ij} $ is the minor.

Square matrices only: otherwise prints and returns 0.

Parameters
irow index
jcolumn index
Returns
cofactor

Definition at line 192 of file QPMatrix.cpp.

References Det(), and SubMatrix().

Referenced by Inverse().

◆ Inverse()

QPMatrix QPMatrix::Inverse ( )

Inverse by the adjugate formula $ A^{-1} = \mathrm{adj}(A)/\det A $.

Returns a null matrix (and prints a message) when the matrix is not square or $ |\det A| < 10^{-10} $.

Returns
inverse, or a null matrix on failure

Definition at line 200 of file QPMatrix.cpp.

References Cofactor(), Det(), and QPMatrix().

Referenced by QPMath::Gaussian::MatrixForm2D().

◆ At() [1/2]

double QPMatrix::At ( int  i)

Value of linear element i of the row-major storage.

Out of range prints a message and returns -1.

Parameters
ilinear index in [0, m·n)
Returns
element value, or -1 when out of range

Definition at line 96 of file QPMatrix.cpp.

References IsInside().

Referenced by Det(), Print(), SubMatrix(), and Transpose().

◆ At() [2/2]

double QPMatrix::At ( int  i,
int  j 
)

Value at row i, column j: storage index i·n + j (row-major).

Out of range prints a message and returns -1.

Parameters
irow index
jcolumn index
Returns
element value, or -1 when out of range

Definition at line 104 of file QPMatrix.cpp.

References IsInside().

◆ operator[]()

double & QPMatrix::operator[] ( int  i)

Reference to linear element i of the row-major storage.

Out of range prints a message and returns a reference to a shared internal sentinel; writes through it are lost.

Parameters
ilinear index in [0, m·n)
Returns
reference to the element (or the sentinel)

Definition at line 122 of file QPMatrix.cpp.

References IsInside().

◆ operator()() [1/2]

double & QPMatrix::operator() ( int  i)

Same as operator[].

Parameters
ilinear index in [0, m·n)
Returns
reference to the element (or the sentinel)

Definition at line 130 of file QPMatrix.cpp.

References IsInside().

Referenced by Trace().

◆ operator()() [2/2]

double & QPMatrix::operator() ( int  i,
int  j 
)

Reference to the element addressed as i·m + j.

Warning
The stride is the row count m, not the column count n: for non-square matrices this addresses a different element than At(int,int) (which uses i·n + j). The two agree only for square matrices. Out of range behaves like operator[].
Parameters
irow index
jcolumn index
Returns
reference to the addressed element (or the sentinel)

Definition at line 138 of file QPMatrix.cpp.

References IsInside().

◆ IsInside() [1/2]

bool QPMatrix::IsInside ( int  i)
Parameters
ilinear index
Returns
true when 0 <= i < m·n

Definition at line 84 of file QPMatrix.cpp.

Referenced by At(), At(), operator()(), operator()(), and operator[]().

◆ IsInside() [2/2]

bool QPMatrix::IsInside ( int  i,
int  j 
)
Parameters
irow index
jcolumn index
Returns
true when 0 <= i < m and 0 <= j < n

Definition at line 89 of file QPMatrix.cpp.

◆ IsSameShape()

bool QPMatrix::IsSameShape ( QPMatrix &  other)
Parameters
othermatrix to compare
Returns
true when both dimensions match

Definition at line 112 of file QPMatrix.cpp.

◆ IsTransShape()

bool QPMatrix::IsTransShape ( QPMatrix &  other)
Parameters
othermatrix to compare
Returns
true when this is m×n and other is n×m

Definition at line 117 of file QPMatrix.cpp.

◆ Print()

std::string QPMatrix::Print ( bool  quite = false)

Formats the matrix as tab-separated rows.

Parameters
quitewhen true, only returns the string; when false, also prints it to stdout
Returns
the formatted string

Definition at line 304 of file QPMatrix.cpp.

References At().

◆ operator*=()

QPMatrix & QPMatrix::operator*= ( const double &  other)
inline

Multiply every element in place.

Parameters
otherscale factor
Returns
this matrix

Definition at line 182 of file QPMatrix.hpp.

◆ Freeing()

void QPMatrix::Freeing ( )
protected

Releases the element storage.

Does not reset the pointer; only the destructor and operator= (which reallocates) may call it.

Definition at line 77 of file QPMatrix.cpp.

Referenced by operator=(), and ~QPMatrix().

Friends And Related Symbol Documentation

◆ operator* [1/3]

QPMatrix operator* ( QPMatrix  self,
double  other 
)
friend

Scaled copy.

Returns
matrix with every element multiplied by the scalar

Definition at line 238 of file QPMatrix.cpp.

◆ operator* [2/3]

QPMatrix operator* ( double  other,
QPMatrix  self 
)
friend

Scaled copy.

Returns
matrix with every element multiplied by the scalar

Definition at line 246 of file QPMatrix.cpp.

◆ operator* [3/3]

QPMatrix operator* ( QPMatrix  self,
QPMatrix  other 
)
friend

Matrix product.

Inner dimensions are checked; on mismatch prints a message and returns a null matrix.

Returns
matrix product

Definition at line 254 of file QPMatrix.cpp.

◆ operator+

QPMatrix operator+ ( QPMatrix &  self,
QPMatrix &  other 
)
friend

Element-wise sum.

Shapes are checked; on mismatch prints a message and returns a null matrix.

Returns
element-wise sum

Definition at line 270 of file QPMatrix.cpp.

◆ operator-

QPMatrix operator- ( QPMatrix &  self,
QPMatrix &  other 
)
friend

Element-wise difference.

Shapes are checked; on mismatch prints a message and returns a null matrix.

Returns
element-wise difference

Definition at line 287 of file QPMatrix.cpp.


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