diff --git a/src/Numerics/LinearAlgebra/Storage/Indexers/Static/PackedStorageIndexerUpper.cs b/src/Numerics/LinearAlgebra/Storage/Indexers/Static/PackedStorageIndexerUpper.cs new file mode 100644 index 00000000..4fc29f00 --- /dev/null +++ b/src/Numerics/LinearAlgebra/Storage/Indexers/Static/PackedStorageIndexerUpper.cs @@ -0,0 +1,88 @@ +namespace MathNet.Numerics.LinearAlgebra.Storage.Indexers.Static +{ + using System; + + /// + /// A class for managing indexing when using Packed Storage scheme, which is a column-Wise packing scheme for Symmetric, Hermitian or Triangular square matrices. + /// This variation provides indexes for storing the upper triangle of a matrix (row less than or equal to column). + /// + public class PackedStorageIndexerUpper : PackedStorageIndexer + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The order of the matrix. + /// + internal PackedStorageIndexerUpper(int order) + : base(order) + { + } + + /// + /// Gets the index of the given element. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// + /// This method is parameter checked. and to get values without parameter checking. + /// + public override int this[int row, int column] + { + get + { + if (row < 0 || row >= Order) + { + throw new ArgumentOutOfRangeException("row"); + } + + if (column < 0 || column >= Order) + { + throw new ArgumentOutOfRangeException("column"); + } + + if (row > column) + { + throw new ArgumentException("Row must be less than or equal to column"); + } + + return IndexOf(row, column); + } + } + + /// + /// Retrieves the index of the requested element without parameter checking. Row must be less than or equal to column. + /// + /// + /// The row of the element. + /// + /// + /// The column of the element. + /// + /// + /// The requested index. + /// + public override int IndexOf(int row, int column) + { + return row + ((column * (column + 1)) / 2); + } + + /// + /// Retrieves the index of the requested diagonal element without parameter checking. + /// + /// + /// The row=column of the diagonal element. + /// + /// + /// The requested index. + /// + public override int IndexOfDiagonal(int row) + { + return (row * (row + 3)) / 2; + } + } +} diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 9d2198d4..ce386ecb 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -143,6 +143,7 @@ +