diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 02318a69..7dc0947f 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Complex base.CopyTo(target); } + /// + /// Creates a matrix that contains the values from the requested sub-matrix. + /// + /// The row to start copying from. + /// The number of rows to copy. Must be positive. + /// The column to start copying from. + /// The number of columns to copy. Must be positive. + /// The requested sub-matrix. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If or + /// is not positive. + public override Matrix SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount) + { + var storage = new DenseColumnMajorMatrixStorage(rowCount, columnCount); + _storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount); + return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data); + } + + /// + /// Copies the values of a given matrix into a region in this matrix. + /// + /// The row to start copying to. + /// The number of rows to copy. Must be positive. + /// The column to start copying to. + /// The number of columns to copy. Must be positive. + /// The sub-matrix to copy from. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If is + /// the size of is not at least x . + /// If or + /// is not positive. + public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix 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); + } + /// /// Gets or sets the value at the given row and column. /// diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index 24e957b6..aa52f271 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -1153,9 +1153,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]; } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 0717522e..a810bd2f 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -387,9 +387,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]); diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 3f644992..ea2111d6 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 base.CopyTo(target); } + /// + /// Creates a matrix that contains the values from the requested sub-matrix. + /// + /// The row to start copying from. + /// The number of rows to copy. Must be positive. + /// The column to start copying from. + /// The number of columns to copy. Must be positive. + /// The requested sub-matrix. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If or + /// is not positive. + public override Matrix SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount) + { + var storage = new DenseColumnMajorMatrixStorage(rowCount, columnCount); + _storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount); + return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data); + } + + /// + /// Copies the values of a given matrix into a region in this matrix. + /// + /// The row to start copying to. + /// The number of rows to copy. Must be positive. + /// The column to start copying to. + /// The number of columns to copy. Must be positive. + /// The sub-matrix to copy from. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If is + /// the size of is not at least x . + /// If or + /// is not positive. + public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix 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); + } + /// /// Gets or sets the value at the given row and column. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 335fe87a..fa1ee64b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -1158,9 +1158,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]; } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 8fd77e6f..a668ed54 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -387,9 +387,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]); diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index d120ed15..1a713dd0 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Double base.CopyTo(target); } + /// + /// Creates a matrix that contains the values from the requested sub-matrix. + /// + /// The row to start copying from. + /// The number of rows to copy. Must be positive. + /// The column to start copying from. + /// The number of columns to copy. Must be positive. + /// The requested sub-matrix. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If or + /// is not positive. + public override Matrix SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount) + { + var storage = new DenseColumnMajorMatrixStorage(rowCount, columnCount); + _storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount); + return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data); + } + + /// + /// Copies the values of a given matrix into a region in this matrix. + /// + /// The row to start copying to. + /// The number of rows to copy. Must be positive. + /// The column to start copying to. + /// The number of columns to copy. Must be positive. + /// The sub-matrix to copy from. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If is + /// the size of is not at least x . + /// If or + /// is not positive. + public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix 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); + } + /// /// Gets or sets the value at the given row and column. /// diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 1c8527b2..a3bccb01 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -1147,9 +1147,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]; } diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index a2bae092..89d26f2a 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -380,9 +380,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]); diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 8865bd22..514d6f94 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -648,9 +648,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public virtual Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public virtual Matrix 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. /// /// The row to start copying to. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying to. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The sub-matrix to copy from. /// If: is /// negative, or greater than or equal to the number of rows. @@ -1145,10 +1145,10 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows /// If is - /// the size of is not at least x . - /// If or + /// the size of is not at least x . + /// If or /// is not positive. - public virtual void SetSubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength, Matrix subMatrix) + public virtual void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix 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++) diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 227844f1..73032e81 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -224,6 +224,59 @@ namespace MathNet.Numerics.LinearAlgebra.Single base.CopyTo(target); } + /// + /// Creates a matrix that contains the values from the requested sub-matrix. + /// + /// The row to start copying from. + /// The number of rows to copy. Must be positive. + /// The column to start copying from. + /// The number of columns to copy. Must be positive. + /// The requested sub-matrix. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If or + /// is not positive. + public override Matrix SubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount) + { + var storage = new DenseColumnMajorMatrixStorage(rowCount, columnCount); + _storage.CopySubMatrixTo(storage, rowIndex, 0, rowCount, columnIndex, 0, columnCount); + return new DenseMatrix(storage.RowCount, storage.ColumnCount, storage.Data); + } + + /// + /// Copies the values of a given matrix into a region in this matrix. + /// + /// The row to start copying to. + /// The number of rows to copy. Must be positive. + /// The column to start copying to. + /// The number of columns to copy. Must be positive. + /// The sub-matrix to copy from. + /// If: is + /// negative, or greater than or equal to the number of rows. + /// is negative, or greater than or equal to the number + /// of columns. + /// (columnIndex + columnLength) >= Columns + /// (rowIndex + rowLength) >= Rows + /// If is + /// the size of is not at least x . + /// If or + /// is not positive. + public override void SetSubMatrix(int rowIndex, int rowCount, int columnIndex, int columnCount, Matrix 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); + } + /// /// Gets or sets the value at the given row and column. /// diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 946817bb..bd5f28fd 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -1147,9 +1147,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]; } diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index c1466146..c88589a2 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/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. /// /// The row to start copying from. - /// The number of rows to copy. Must be positive. + /// The number of rows to copy. Must be positive. /// The column to start copying from. - /// The number of columns to copy. Must be positive. + /// The number of columns to copy. Must be positive. /// The requested sub-matrix. /// If: is /// negative, or greater than or equal to the number of rows. @@ -381,9 +381,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// of columns. /// (columnIndex + columnLength) >= Columns /// (rowIndex + rowLength) >= Rows - /// If or + /// If or /// is not positive. - public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) + public override Matrix 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]); diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index b211dfce..c566d410 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/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 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); + } + } } }