namespace XmlDocumentation;
///
/// Sparse implementation using a dictionary to store non-zero values.
///
///
public class SparseMatrix : Matrix
{
private readonly Dictionary<(int row, int column), double> _data;
///
public override int Rows { get; }
///
public override int Columns { get; }
///
/// Initializes a new instance of the class.
///
/// The number of rows in the matrix.
/// The number of columns in the matrix.
public SparseMatrix(int rows, int columns)
{
Rows = rows;
Columns = columns;
_data = new Dictionary<(int row, int column), double>();
}
///
public override double this[int row, int column]
{
get
{
if (row < 0 || row >= Rows || column < 0 || column >= Columns)
throw new ArgumentOutOfRangeException(nameof(row), "Row or column is out of bounds.");
if (_data.TryGetValue((row, column), out var value))
return value;
return 0.0;
}
set
{
if (row < 0 || row >= Rows || column < 0 || column >= Columns)
throw new ArgumentOutOfRangeException(nameof(row), "Row or column is out of bounds.");
if (value == 0.0)
_data.Remove((row, column));
else
_data[(row, column)] = value;
}
}
///
public override Matrix Transpose()
{
var result = new SparseMatrix(Columns, Rows);
foreach (var ((row, column), value) in _data)
{
result[column, row] = value;
}
return result;
}
///
public override Matrix Add(Matrix other)
{
if (Rows != other.Rows || Columns != other.Columns)
throw new ArgumentException("Matrices must have the same dimensions to be added.");
var result = new SparseMatrix(Rows, Columns);
foreach (var ((row, column), value) in _data)
{
result[row, column] = value + other[row, column];
}
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
if (!_data.ContainsKey((i, j)))
{
result[i, j] = other[i, j];
}
}
}
return result;
}
///
public override Matrix Multiply(Matrix other)
{
if (Columns != other.Rows)
throw new ArgumentException("The number of columns in the first matrix must match the number of rows in the second matrix.");
var result = new SparseMatrix(Rows, other.Columns);
foreach (var ((row, column), value) in _data)
{
for (int j = 0; j < other.Columns; j++)
{
result[row, j] += value * other[column, j];
}
}
return result;
}
}