Engauge Digitizer  2
Public Member Functions | List of all members
Matrix Class Reference

Matrix class that supports arbitrary NxN size. More...

#include <Matrix.h>

Collaboration diagram for Matrix:
Collaboration graph

Public Member Functions

 Matrix (int N)
 Simple constructor of square matrix with initialization to identity matrix. More...
 
 Matrix (int rows, int cols)
 Simple constructor of rectangular matrix with initialization to zero matrix. More...
 
 Matrix (const Matrix &other)
 Copy constructor. More...
 
Matrixoperator= (const Matrix &matrix)
 Assignment operator. More...
 
int cols () const
 Width of matrix. More...
 
double determinant () const
 Return the determinant of this matrix. More...
 
double get (int row, int col) const
 Return (row, col) element. More...
 
Matrix inverse (int significantDigits, MatrixConsistent &matrixConsistent) const
 Return the inverse of this matrix. More...
 
Matrix minorReduced (int rowOmit, int colOmit) const
 Return minor matrix which is the original with the specified row and column omitted. The name 'minor' is a reserved word. More...
 
Matrix operator* (const Matrix &other) const
 Multiplication operator with a matrix. More...
 
QVector< double > operator* (const QVector< double > other) const
 Multiplication operator with a vector. More...
 
int rows () const
 Height of matrix. More...
 
void set (int row, int col, double value)
 Set (row, col) element. More...
 
QString toString () const
 Dump matrix to a string. More...
 
Matrix transpose () const
 Return the transpose of the current matrix. More...
 

Detailed Description

Matrix class that supports arbitrary NxN size.

Definition at line 20 of file Matrix.h.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

Matrix::Matrix ( int  N)

Simple constructor of square matrix with initialization to identity matrix.

Definition at line 12 of file Matrix.cpp.

13 {
14  initialize (N, N);
15 }

◆ Matrix() [2/3]

Matrix::Matrix ( int  rows,
int  cols 
)

Simple constructor of rectangular matrix with initialization to zero matrix.

Definition at line 17 of file Matrix.cpp.

19 {
20  initialize (rows, cols);
21 }
int cols() const
Width of matrix.
Definition: Matrix.cpp:61
int rows() const
Height of matrix.
Definition: Matrix.cpp:423

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix other)

Copy constructor.

Definition at line 23 of file Matrix.cpp.

24 {
25  m_rows = other.rows();
26  m_cols = other.cols();
27  m_vector.resize (m_rows * m_cols);
28  for (int row = 0; row < m_rows; row++) {
29  for (int col = 0; col < m_cols; col++) {
30  set (row, col, other.get (row, col));
31  }
32  }
33 }
int cols() const
Width of matrix.
Definition: Matrix.cpp:61
int rows() const
Height of matrix.
Definition: Matrix.cpp:423
double get(int row, int col) const
Return (row, col) element.
Definition: Matrix.cpp:98

Member Function Documentation

◆ cols()

int Matrix::cols ( ) const

Width of matrix.

Definition at line 61 of file Matrix.cpp.

62 {
63  return m_cols;
64 }

◆ determinant()

double Matrix::determinant ( ) const

Return the determinant of this matrix.

Definition at line 66 of file Matrix.cpp.

67 {
68  ENGAUGE_ASSERT (m_rows == m_cols);
69 
70  double rtn;
71 
72  if (m_rows == 1) {
73 
74  rtn = m_vector [0];
75 
76  } else {
77 
78  const int COL = 0; // We arbitrarily iterate through the first column
79 
80  // This is a recursive algorithm
81  rtn = 0.0;
82  double multiplier = +1;
83  for (int row = 0; row < m_rows; row++) {
84  Matrix min = minorReduced (row, COL);
85  rtn += multiplier * get (row, COL) * min.determinant ();
86  multiplier *= -1.0;
87  }
88  }
89 
90  return rtn;
91 }
Matrix minorReduced(int rowOmit, int colOmit) const
Return minor matrix which is the original with the specified row and column omitted. The name &#39;minor&#39; is a reserved word.
Definition: Matrix.cpp:326
double determinant() const
Return the determinant of this matrix.
Definition: Matrix.cpp:66
Matrix class that supports arbitrary NxN size.
Definition: Matrix.h:20
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:20

◆ get()

double Matrix::get ( int  row,
int  col 
) const

Return (row, col) element.

Definition at line 98 of file Matrix.cpp.

99 {
100  int foldedIndex = fold2dIndexes (row, col);
101  return m_vector [foldedIndex];
102 }

◆ inverse()

Matrix Matrix::inverse ( int  significantDigits,
MatrixConsistent matrixConsistent 
) const

Return the inverse of this matrix.

Parameters
significantDigitsInput value for calculating epsilon threshold for determinants that are so close to zero that matrix should be considered inconsistent.
matrixConsistentOutput flag indicating success or failure

Definition at line 123 of file Matrix.cpp.

125 {
126  // Set epsilon threshold for valueFailsEpsilonTest
127  double maxValue = 0;
128  for (int row = 0; row < m_rows; row++) {
129  for (int col = 0; col < m_cols; col++) {
130  double value = qAbs (get (row, col));
131  if (value > maxValue) {
132  maxValue = value;
133  }
134  }
135  }
136 
137  // Available algorithms are inverseCramersRule and inverseGaussianElimination
138  matrixConsistent = MATRIX_CONSISTENT;
139  return inverseGaussianElimination (significantDigits,
140  matrixConsistent);
141 }

◆ minorReduced()

Matrix Matrix::minorReduced ( int  rowOmit,
int  colOmit 
) const

Return minor matrix which is the original with the specified row and column omitted. The name 'minor' is a reserved word.

Definition at line 326 of file Matrix.cpp.

327 {
328  ENGAUGE_ASSERT (m_rows == m_cols);
329 
330  Matrix outMinor (m_rows - 1);
331  int rowMinor = 0;
332  for (int row = 0; row < m_rows; row++) {
333 
334  if (row != rowOmit) {
335 
336  int colMinor = 0;
337  for (int col = 0; col < m_cols; col++) {
338 
339  if (col != colOmit) {
340 
341  outMinor.set (rowMinor, colMinor, get (row, col));
342  ++colMinor;
343  }
344  }
345  ++rowMinor;
346  }
347  }
348 
349  return outMinor;
350 }
Matrix class that supports arbitrary NxN size.
Definition: Matrix.h:20
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:20

◆ operator*() [1/2]

Matrix Matrix::operator* ( const Matrix other) const

Multiplication operator with a matrix.

Definition at line 386 of file Matrix.cpp.

387 {
388  ENGAUGE_ASSERT (m_cols == other.rows ());
389 
390  Matrix out (m_rows, other.cols ());
391 
392  for (int row = 0; row < m_rows; row++) {
393  for (int col = 0; col < other.cols (); col++) {
394  double sum = 0;
395  for (int index = 0; index < m_cols; index++) {
396  sum += get (row, index) * other.get (index, col);
397  }
398  out.set (row, col, sum);
399  }
400  }
401 
402  return out;
403 }
int cols() const
Width of matrix.
Definition: Matrix.cpp:61
Matrix class that supports arbitrary NxN size.
Definition: Matrix.h:20
int rows() const
Height of matrix.
Definition: Matrix.cpp:423
double get(int row, int col) const
Return (row, col) element.
Definition: Matrix.cpp:98
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:20

◆ operator*() [2/2]

QVector< double > Matrix::operator* ( const QVector< double >  other) const

Multiplication operator with a vector.

Definition at line 405 of file Matrix.cpp.

406 {
407  ENGAUGE_ASSERT (m_cols == other.size ());
408 
409  QVector<double> out;
410  out.resize (m_rows);
411  for (int row = 0; row < m_rows; row++) {
412  double sum = 0;
413  for (int col = 0; col < m_cols; col++) {
414  sum += get (row, col) * other [col];
415  }
416 
417  out [row] = sum;
418  }
419 
420  return out;
421 }
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS) define ENGAUGE...
Definition: EngaugeAssert.h:20

◆ operator=()

Matrix & Matrix::operator= ( const Matrix matrix)

Assignment operator.

Definition at line 35 of file Matrix.cpp.

36 {
37  m_rows = other.rows();
38  m_cols = other.cols();
39  m_vector.resize (m_rows * m_cols);
40  for (int row = 0; row < m_rows; row++) {
41  for (int col = 0; col < m_cols; col++) {
42  set (row, col, other.get (row, col));
43  }
44  }
45 
46  return *this;
47 }

◆ rows()

int Matrix::rows ( ) const

Height of matrix.

Definition at line 423 of file Matrix.cpp.

424 {
425  return m_rows;
426 }

◆ set()

void Matrix::set ( int  row,
int  col,
double  value 
)

Set (row, col) element.

Definition at line 428 of file Matrix.cpp.

429 {
430  m_vector [fold2dIndexes (row, col)] = value;
431 }

◆ toString()

QString Matrix::toString ( ) const

Dump matrix to a string.

Definition at line 445 of file Matrix.cpp.

446 {
447  QString out;
448  QTextStream str (&out);
449 
450  str << "(";
451  for (int row = 0; row < rows (); row++) {
452  if (row > 0) {
453  str << ", ";
454  }
455  str << "(";
456  for (int col = 0; col < cols (); col++) {
457  if (col > 0) {
458  str << ", ";
459  }
460  str << get (row, col);
461  }
462  str << ")";
463  }
464  str << ")";
465 
466  return out;
467 }
int cols() const
Width of matrix.
Definition: Matrix.cpp:61
int rows() const
Height of matrix.
Definition: Matrix.cpp:423

◆ transpose()

Matrix Matrix::transpose ( ) const

Return the transpose of the current matrix.

Definition at line 469 of file Matrix.cpp.

470 {
471  Matrix out (m_cols, m_rows);
472 
473  for (int row = 0; row < m_rows; row++) {
474  for (int col = 0; col < m_cols; col++) {
475  out.set (col, row, get (row, col));
476  }
477  }
478 
479  return out;
480 }
Matrix class that supports arbitrary NxN size.
Definition: Matrix.h:20

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