#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stdexcept>
Go to the source code of this file.
|
| template<typename T > |
| using | SparseMatrix = std::map< std::pair< T, T >, double > |
| | Alias for a sparse matrix using a map of coordinate pairs.
|
| |
|
| template<typename T > |
| std::vector< T > | getDiagonalElements (const SparseMatrix< T > &matrix) |
| | Extracts the diagonal elements from a sparse matrix.
|
| |
| template<typename T > |
| SparseMatrix< T > | createIdentityMatrix (const std::vector< T > &indices) |
| | Creates an identity matrix from a list of indices.
|
| |
| template<typename T > |
| double | getElement (const SparseMatrix< T > &matrix, const T &row, const T &col) |
| | Retrieves an element from the sparse matrix.
|
| |
| template<typename T > |
| void | setElement (SparseMatrix< T > &matrix, const T &row, const T &col, double value) |
| | Sets or updates an element in the sparse matrix.
|
| |
| template<typename T > |
| SparseMatrix< T > | invertMatrix (const SparseMatrix< T > &matrix, const std::vector< T > &indices) |
| | Inverts a square sparse matrix using Gauss-Jordan elimination.
|
| |
| template<typename T > |
| std::pair< SparseMatrix< T >, std::vector< T > > | removeEmptyRowsAndCols (const SparseMatrix< T > &matrix, const std::vector< T > &indices) |
| | Removes rows and columns that are entirely zero (i.e., not present in the sparse matrix).
|
| |
| template<typename T > |
| void | printMatrix (const SparseMatrix< T > &matrix, const std::vector< T > &indices) |
| | Prints the matrix in dense form using a list of indices.
|
| |
| template<typename T > |
| void | customPrintMatrix (const SparseMatrix< T > &matrix, const std::vector< T > &indices) |
| | Prints the matrix in a formatted grid with row and column headers.
|
| |
◆ SparseMatrix
Alias for a sparse matrix using a map of coordinate pairs.
- Template Parameters
-
| T | Type of the row and column indices. |
Definition at line 16 of file SparseMatrix.h.
◆ createIdentityMatrix()
| SparseMatrix< T > createIdentityMatrix |
( |
const std::vector< T > & |
indices | ) |
|
Creates an identity matrix from a list of indices.
- Template Parameters
-
- Parameters
-
| indices | List of row/column indices. |
- Returns
- Sparse identity matrix.
Definition at line 44 of file SparseMatrix.h.
◆ customPrintMatrix()
| void customPrintMatrix |
( |
const SparseMatrix< T > & |
matrix, |
|
|
const std::vector< T > & |
indices |
|
) |
| |
Prints the matrix in a formatted grid with row and column headers.
- Template Parameters
-
| T | Index type (must support std::ostream << operator). |
- Parameters
-
| matrix | Sparse matrix. |
| indices | List of row/column indices. |
Definition at line 206 of file SparseMatrix.h.
◆ getDiagonalElements()
| std::vector< T > getDiagonalElements |
( |
const SparseMatrix< T > & |
matrix | ) |
|
Extracts the diagonal elements from a sparse matrix.
- Template Parameters
-
- Parameters
-
- Returns
- Vector of diagonal indices.
Definition at line 26 of file SparseMatrix.h.
◆ getElement()
| double getElement |
( |
const SparseMatrix< T > & |
matrix, |
|
|
const T & |
row, |
|
|
const T & |
col |
|
) |
| |
Retrieves an element from the sparse matrix.
- Template Parameters
-
- Parameters
-
| matrix | The sparse matrix. |
| row | Row index. |
| col | Column index. |
- Returns
- Value of the element (0.0 if not present).
Definition at line 62 of file SparseMatrix.h.
◆ invertMatrix()
Inverts a square sparse matrix using Gauss-Jordan elimination.
- Template Parameters
-
- Parameters
-
| matrix | Input square sparse matrix. |
| indices | List of row/column indices (must match matrix size). |
- Returns
- Inverted matrix.
- Exceptions
-
| std::runtime_error | if the matrix is singular. |
Definition at line 98 of file SparseMatrix.h.
◆ printMatrix()
| void printMatrix |
( |
const SparseMatrix< T > & |
matrix, |
|
|
const std::vector< T > & |
indices |
|
) |
| |
Prints the matrix in dense form using a list of indices.
- Template Parameters
-
- Parameters
-
| matrix | Sparse matrix. |
| indices | List of indices to determine order. |
Definition at line 189 of file SparseMatrix.h.
◆ removeEmptyRowsAndCols()
| std::pair< SparseMatrix< T >, std::vector< T > > removeEmptyRowsAndCols |
( |
const SparseMatrix< T > & |
matrix, |
|
|
const std::vector< T > & |
indices |
|
) |
| |
Removes rows and columns that are entirely zero (i.e., not present in the sparse matrix).
- Template Parameters
-
- Parameters
-
| matrix | The input sparse matrix. |
| indices | The input list of row/column indices. |
- Returns
- A pair containing:
- The cleaned sparse matrix.
- The updated list of indices.
Definition at line 142 of file SparseMatrix.h.
◆ setElement()
| void setElement |
( |
SparseMatrix< T > & |
matrix, |
|
|
const T & |
row, |
|
|
const T & |
col, |
|
|
double |
value |
|
) |
| |
Sets or updates an element in the sparse matrix.
- Template Parameters
-
- Parameters
-
| matrix | Sparse matrix to modify. |
| row | Row index. |
| col | Column index. |
| value | Value to assign. |
Definition at line 80 of file SparseMatrix.h.