namespace XmlDocumentation;
///
/// Dense implementation using a 2D array.
///
///
public class ArrayMatrix : Matrix
{
private readonly 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 ArrayMatrix(int rows, int columns)
{
Rows = rows;
Columns = columns;
_data = new double[rows, columns];
}
///
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.");
return _data[row, column];
}
set
{
if (row < 0 || row >= Rows || column < 0 || column >= Columns)
throw new ArgumentOutOfRangeException(nameof(row), "Row or column is out of bounds.");
_data[row, column] = value;
}
}
///
public override Matrix Transpose()
{
var result = new ArrayMatrix(Columns, Rows);
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
result[j, i] = this[i, j];
}
}
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 ArrayMatrix(Rows, Columns);
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Columns; j++)
{
result[i, j] = this[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 ArrayMatrix(Rows, other.Columns);
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < other.Columns; j++)
{
double sum = 0;
for (int k = 0; k < Columns; k++)
{
sum += this[i, k] * other[k, j];
}
result[i, j] = sum;
}
}
return result;
}
}