Browse Source

LA Storage: dense matrix get/set submatrix (perf, dedupl)

la-knuth
Christoph Ruegg 14 years ago
parent
commit
da88833216
  1. 53
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 36
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  3. 28
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 53
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  5. 36
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  6. 28
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  7. 53
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  8. 36
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  9. 28
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  10. 60
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  11. 53
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  12. 36
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  13. 28
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  14. 81
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

53
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.CopyTo(target);
}
/// <summary>
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
var storage = new DenseColumnMajorMatrixStorage<Complex>(rowCount, columnCount);
_storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount);
return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data);
}
/// <summary>
/// Copies the values of a given matrix into a region in this matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying to.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying to.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <param name="subMatrix">The sub-matrix to copy from.</param>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentNullException">If <paramref name="subMatrix"/> is <see langword="null" /></exception>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowCount"/> x <paramref name="columnCount"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix<Complex> subMatrix)
{
var denseSubMatrix = subMatrix as DenseMatrix;
if (denseSubMatrix != null)
{
denseSubMatrix._storage.CopySubMatrixTo(_storage, 0, rowIndex, rowCount, 0, columnIndex, columnCount);
return;
}
base.SetSubMatrix(rowIndex, rowCount, columnIndex, columnCount, subMatrix);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

36
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -1143,9 +1143,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -1153,9 +1153,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<Complex> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -1167,48 +1167,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = new SparseMatrix(rowLength, columnLength);
var result = new SparseMatrix(rowCount, columnCount);
if (rowIndex > columnIndex && columnIndex + columnLength > rowIndex)
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i];
}
}
else if (rowIndex < columnIndex && rowIndex + rowLength > columnIndex)
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i];
}
}
else
{
for (var i = 0; i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; i < Math.Min(columnCount, rowCount); i++)
{
result[i, i] = Data[rowIndex + i];
}

28
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -377,9 +377,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -387,9 +387,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<Complex> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -401,30 +401,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = (SparseMatrix)CreateMatrix(rowLength, columnLength);
var result = (SparseMatrix)CreateMatrix(rowCount, columnCount);
for (int i = rowIndex, row = 0; i < rowMax; i++, row++)
{
@ -433,7 +433,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
for (int j = startIndex; j < endIndex; j++)
{
// check if the column index is in the range
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnLength))
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnCount))
{
var column = _columnIndices[j] - columnIndex;
result.SetValueAt(row, column, _nonZeroValues[j]);

53
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.CopyTo(target);
}
/// <summary>
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex32> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
var storage = new DenseColumnMajorMatrixStorage<Complex32>(rowCount, columnCount);
_storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount);
return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data);
}
/// <summary>
/// Copies the values of a given matrix into a region in this matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying to.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying to.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <param name="subMatrix">The sub-matrix to copy from.</param>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentNullException">If <paramref name="subMatrix"/> is <see langword="null" /></exception>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowCount"/> x <paramref name="columnCount"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix<Complex32> subMatrix)
{
var denseSubMatrix = subMatrix as DenseMatrix;
if (denseSubMatrix != null)
{
denseSubMatrix._storage.CopySubMatrixTo(_storage, 0, rowIndex, rowCount, 0, columnIndex, columnCount);
return;
}
base.SetSubMatrix(rowIndex, rowCount, columnIndex, columnCount, subMatrix);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

36
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -1148,9 +1148,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -1158,9 +1158,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex32> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<Complex32> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -1172,48 +1172,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = new SparseMatrix(rowLength, columnLength);
var result = new SparseMatrix(rowCount, columnCount);
if (rowIndex > columnIndex && columnIndex + columnLength > rowIndex)
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i];
}
}
else if (rowIndex < columnIndex && rowIndex + rowLength > columnIndex)
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i];
}
}
else
{
for (var i = 0; i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; i < Math.Min(columnCount, rowCount); i++)
{
result[i, i] = Data[rowIndex + i];
}

28
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -377,9 +377,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -387,9 +387,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<Complex32> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<Complex32> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -401,30 +401,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = (SparseMatrix)CreateMatrix(rowLength, columnLength);
var result = (SparseMatrix)CreateMatrix(rowCount, columnCount);
for (int i = rowIndex, row = 0; i < rowMax; i++, row++)
{
@ -434,7 +434,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
for (int j = startIndex; j < endIndex; j++)
{
// check if the column index is in the range
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnLength))
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnCount))
{
var column = _columnIndices[j] - columnIndex;
result.SetValueAt(row, column, _nonZeroValues[j]);

53
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Double
base.CopyTo(target);
}
/// <summary>
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<double> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
var storage = new DenseColumnMajorMatrixStorage<double>(rowCount, columnCount);
_storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount);
return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data);
}
/// <summary>
/// Copies the values of a given matrix into a region in this matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying to.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying to.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <param name="subMatrix">The sub-matrix to copy from.</param>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentNullException">If <paramref name="subMatrix"/> is <see langword="null" /></exception>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowCount"/> x <paramref name="columnCount"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix<double> subMatrix)
{
var denseSubMatrix = subMatrix as DenseMatrix;
if (denseSubMatrix != null)
{
denseSubMatrix._storage.CopySubMatrixTo(_storage, 0, rowIndex, rowCount, 0, columnIndex, columnCount);
return;
}
base.SetSubMatrix(rowIndex, rowCount, columnIndex, columnCount, subMatrix);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

36
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -1137,9 +1137,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -1147,9 +1147,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<double> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<double> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -1161,48 +1161,48 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = new SparseMatrix(rowLength, columnLength);
var result = new SparseMatrix(rowCount, columnCount);
if (rowIndex > columnIndex && columnIndex + columnLength > rowIndex)
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i];
}
}
else if (rowIndex < columnIndex && rowIndex + rowLength > columnIndex)
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i];
}
}
else
{
for (var i = 0; i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; i < Math.Min(columnCount, rowCount); i++)
{
result[i, i] = Data[rowIndex + i];
}

28
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -370,9 +370,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -380,9 +380,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<double> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<double> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -394,30 +394,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = (SparseMatrix)CreateMatrix(rowLength, columnLength);
var result = (SparseMatrix)CreateMatrix(rowCount, columnCount);
for (int i = rowIndex, row = 0; i < rowMax; i++, row++)
{
@ -427,7 +427,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
for (int j = startIndex; j < endIndex; j++)
{
// check if the column index is in the range
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnLength))
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnCount))
{
var column = _columnIndices[j] - columnIndex;
result.SetValueAt(row, column, _nonZeroValues[j]);

60
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -638,9 +638,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -648,9 +648,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public virtual Matrix<T> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public virtual Matrix<T> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -662,30 +662,30 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = CreateMatrix(rowLength, columnLength);
var result = CreateMatrix(rowCount, columnCount);
for (var j = columnIndex; j < colMax; j++)
{
@ -1134,9 +1134,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// Copies the values of a given matrix into a region in this matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying to.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying to.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <param name="subMatrix">The sub-matrix to copy from.</param>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -1145,10 +1145,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentNullException">If <paramref name="subMatrix"/> is <see langword="null" /></exception>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowLength"/> x <paramref name="columnLength"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowCount"/> x <paramref name="columnCount"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public virtual void SetSubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength, Matrix<T> subMatrix)
public virtual void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix<T> subMatrix)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -1160,14 +1160,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
if (subMatrix == null)
@ -1175,27 +1175,27 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentNullException("subMatrix");
}
if (columnLength > subMatrix.ColumnCount)
if (columnCount > subMatrix.ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength", @"columnLength can be at most the number of columns in subMatrix.");
throw new ArgumentOutOfRangeException("columnCount", @"columnLength can be at most the number of columns in subMatrix.");
}
if (rowLength > subMatrix.RowCount)
if (rowCount > subMatrix.RowCount)
{
throw new ArgumentOutOfRangeException("rowLength", @"rowLength can be at most the number of rows in subMatrix.");
throw new ArgumentOutOfRangeException("rowCount", @"rowLength can be at most the number of rows in subMatrix.");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
for (var j = columnIndex; j < colMax; j++)

53
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Single
base.CopyTo(target);
}
/// <summary>
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<float> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
var storage = new DenseColumnMajorMatrixStorage<float>(rowCount, columnCount);
_storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount);
return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data);
}
/// <summary>
/// Copies the values of a given matrix into a region in this matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying to.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying to.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <param name="subMatrix">The sub-matrix to copy from.</param>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
/// <item><paramref name="columnIndex"/> is negative, or greater than or equal to the number
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentNullException">If <paramref name="subMatrix"/> is <see langword="null" /></exception>
/// <item>the size of <paramref name="subMatrix"/> is not at least <paramref name="rowCount"/> x <paramref name="columnCount"/>.</item>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix<float> subMatrix)
{
var denseSubMatrix = subMatrix as DenseMatrix;
if (denseSubMatrix != null)
{
denseSubMatrix._storage.CopySubMatrixTo(_storage, 0, rowIndex, rowCount, 0, columnIndex, columnCount);
return;
}
base.SetSubMatrix(rowIndex, rowCount, columnIndex, columnCount, subMatrix);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

36
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -1137,9 +1137,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -1147,9 +1147,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<float> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<float> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -1161,48 +1161,48 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = new SparseMatrix(rowLength, columnLength);
var result = new SparseMatrix(rowCount, columnCount);
if (rowIndex > columnIndex && columnIndex + columnLength > rowIndex)
if (rowIndex > columnIndex && columnIndex + columnCount > rowIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[i, rowIndex - columnIndex + i] = Data[rowIndex + i];
}
}
else if (rowIndex < columnIndex && rowIndex + rowLength > columnIndex)
else if (rowIndex < columnIndex && rowIndex + rowCount > columnIndex)
{
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; rowIndex - columnIndex + i < Math.Min(columnCount, rowCount); i++)
{
result[columnIndex - rowIndex + i, i] = Data[columnIndex + i];
}
}
else
{
for (var i = 0; i < Math.Min(columnLength, rowLength); i++)
for (var i = 0; i < Math.Min(columnCount, rowCount); i++)
{
result[i, i] = Data[rowIndex + i];
}

28
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -371,9 +371,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Creates a matrix that contains the values from the requested sub-matrix.
/// </summary>
/// <param name="rowIndex">The row to start copying from.</param>
/// <param name="rowLength">The number of rows to copy. Must be positive.</param>
/// <param name="rowCount">The number of rows to copy. Must be positive.</param>
/// <param name="columnIndex">The column to start copying from.</param>
/// <param name="columnLength">The number of columns to copy. Must be positive.</param>
/// <param name="columnCount">The number of columns to copy. Must be positive.</param>
/// <returns>The requested sub-matrix.</returns>
/// <exception cref="ArgumentOutOfRangeException">If: <list><item><paramref name="rowIndex"/> is
/// negative, or greater than or equal to the number of rows.</item>
@ -381,9 +381,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// of columns.</item>
/// <item><c>(columnIndex + columnLength) &gt;= Columns</c></item>
/// <item><c>(rowIndex + rowLength) &gt;= Rows</c></item></list></exception>
/// <exception cref="ArgumentException">If <paramref name="rowLength"/> or <paramref name="columnLength"/>
/// <exception cref="ArgumentException">If <paramref name="rowCount"/> or <paramref name="columnCount"/>
/// is not positive.</exception>
public override Matrix<float> SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength)
public override Matrix<float> SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount)
{
if (rowIndex >= RowCount || rowIndex < 0)
{
@ -395,30 +395,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentOutOfRangeException("columnIndex");
}
if (rowLength < 1)
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnLength < 1)
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnLength");
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
var colMax = columnIndex + columnLength;
var rowMax = rowIndex + rowLength;
var colMax = columnIndex + columnCount;
var rowMax = rowIndex + rowCount;
if (rowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowLength");
throw new ArgumentOutOfRangeException("rowCount");
}
if (colMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnLength");
throw new ArgumentOutOfRangeException("columnCount");
}
var result = (SparseMatrix)CreateMatrix(rowLength, columnLength);
var result = (SparseMatrix)CreateMatrix(rowCount, columnCount);
for (int i = rowIndex, row = 0; i < rowMax; i++, row++)
{
@ -428,7 +428,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
for (int j = startIndex; j < endIndex; j++)
{
// check if the column index is in the range
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnLength))
if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnCount))
{
var column = _columnIndices[j] - columnIndex;
result.SetValueAt(row, column, _nonZeroValues[j]);

81
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -45,5 +45,86 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
//Buffer.BlockCopy(Data, 0, target.Data, 0, Data.Length * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));
Array.Copy(Data, 0, target.Data, 0, Data.Length);
}
public void CopySubMatrixTo(DenseColumnMajorMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount)
{
if (rowCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "rowCount");
}
if (columnCount < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "columnCount");
}
// Verify Source
if (sourceRowIndex >= RowCount || sourceRowIndex < 0)
{
throw new ArgumentOutOfRangeException("sourceRowIndex");
}
if (sourceColumnIndex >= ColumnCount || sourceColumnIndex < 0)
{
throw new ArgumentOutOfRangeException("sourceColumnIndex");
}
var sourceRowMax = sourceRowIndex + rowCount;
var sourceColumnMax = sourceColumnIndex + columnCount;
if (sourceRowMax > RowCount)
{
throw new ArgumentOutOfRangeException("rowCount");
}
if (sourceColumnMax > ColumnCount)
{
throw new ArgumentOutOfRangeException("columnCount");
}
// Verify Target
if (target == null)
{
throw new ArgumentNullException("target");
}
if (ReferenceEquals(this, target))
{
throw new NotSupportedException();
}
if (targetRowIndex >= target.RowCount || targetRowIndex < 0)
{
throw new ArgumentOutOfRangeException("targetRowIndex");
}
if (targetColumnIndex >= target.ColumnCount || targetColumnIndex < 0)
{
throw new ArgumentOutOfRangeException("targetColumnIndex");
}
var targetRowMax = targetRowIndex + rowCount;
var targetColumnMax = targetColumnIndex + columnCount;
if (targetRowMax > target.RowCount)
{
throw new ArgumentOutOfRangeException("rowCount");
}
if (targetColumnMax > target.ColumnCount)
{
throw new ArgumentOutOfRangeException("columnCount");
}
for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnMax; j++, jj++)
{
//Buffer.BlockCopy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));
Array.Copy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount);
}
}
}
}

Loading…
Cancel
Save