27 std::vector<T> diagonalElements;
28 for (
const auto& element : matrix) {
29 if (element.first.first == element.first.second) {
30 diagonalElements.push_back(element.first.first);
33 return diagonalElements;
46 for (
const T& index : indices) {
47 identity[{index, index}] = 1.0;
63 auto it = matrix.find({row, col});
64 if (it != matrix.end()) {
82 matrix[{row, col}] = value;
84 matrix.erase({row, col});
102 for (
size_t i = 0; i < indices.size(); ++i) {
103 T pivot = indices[i];
105 double pivotValue =
getElement(augmentedMatrix, pivot, pivot);
106 if (pivotValue == 0.0) {
107 throw std::runtime_error(
"Matrix is singular and cannot be inverted.");
110 for (
size_t k = 0; k < indices.size(); ++k) {
116 for (
size_t j = 0; j < indices.size(); ++j) {
117 if (i == j)
continue;
119 double factor =
getElement(augmentedMatrix, row, pivot);
120 for (
size_t k = 0; k < indices.size(); ++k) {
143 std::map<T, int> rowCount;
144 std::map<T, int> colCount;
147 for (
const auto& [coord, value] : matrix) {
150 T col = coord.second;
157 std::vector<T> cleanedIndices;
158 for (
const T& idx : indices) {
159 if (rowCount[idx] > 0 || colCount[idx] > 0) {
160 cleanedIndices.push_back(idx);
166 for (
const auto& [coord, value] : matrix) {
169 T col = coord.second;
170 if (std::find(cleanedIndices.begin(), cleanedIndices.end(), row) != cleanedIndices.end() &&
171 std::find(cleanedIndices.begin(), cleanedIndices.end(), col) != cleanedIndices.end()) {
172 cleanedMatrix[coord] = value;
177 return {cleanedMatrix, cleanedIndices};
190 for (
const auto& i : indices) {
191 for (
const auto& j : indices) {
194 std::cout << std::endl;
208 size_t maxLabelWidth = 0;
209 size_t maxValueWidth = 0;
211 std::ostringstream oss;
212 for (
const T& index : indices) {
216 maxLabelWidth = std::max(maxLabelWidth, oss.str().length());
219 for (
const auto& [coord, value] : matrix) {
223 maxValueWidth = std::max(maxValueWidth, oss.str().length());
226 size_t cellWidth = std::max(maxLabelWidth, maxValueWidth) + 2;
229 std::cout << std::setw(cellWidth) <<
" ";
232 for (
const auto& col : indices) {
233 std::cout << std::setw(cellWidth) << col;
238 std::cout << std::setw(cellWidth) <<
" ";
239 for (
size_t i = 0; i < indices.size(); ++i) {
240 std::cout << std::setw(cellWidth) << std::string(cellWidth - 1,
'-');
245 for (
const auto& row : indices) {
246 std::cout << std::setw(cellWidth) << row;
247 for (
const auto& col : indices) {
249 std::cout << std::setw(cellWidth) << val;
263 return ::getElement(
matrix, row, col);
271 return ::getDiagonalElements(
matrix);
274 void print(
const std::vector<T>& indices)
const {
void customPrintMatrix(const SparseMatrix< T > &matrix, const std::vector< T > &indices)
Prints the matrix in a formatted grid with row and column headers.
void setElement(SparseMatrix< T > &matrix, const T &row, const T &col, double value)
Sets or updates an element in the sparse matrix.
std::map< std::pair< T, T >, double > SparseMatrix
Alias for a sparse matrix using a map of coordinate pairs.
void printMatrix(const SparseMatrix< T > &matrix, const std::vector< T > &indices)
Prints the matrix in dense form using a list of indices.
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).
SparseMatrix< T > invertMatrix(const SparseMatrix< T > &matrix, const std::vector< T > &indices)
Inverts a square sparse matrix using Gauss-Jordan elimination.
double getElement(const SparseMatrix< T > &matrix, const T &row, const T &col)
Retrieves an element from the sparse matrix.
std::vector< T > getDiagonalElements(const SparseMatrix< T > &matrix)
Extracts the diagonal elements from a sparse matrix.
SparseMatrix< T > createIdentityMatrix(const std::vector< T > &indices)
Creates an identity matrix from a list of indices.
double getElement(T row, T col) const
std::vector< T > getDiagonalElements() const
void print(const std::vector< T > &indices) const
void setElement(T row, T col, double value)
SparseMatrixWrapper< T > invert(const std::vector< T > &indices) const
SparseMatrixWrapper()=default
double T(double x)
Wilson coefficient T(x).