diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 8b001712..93dfcb87 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index ffb81b0e..46f9c0ce 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -151,7 +151,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index c5764779..63c5e87f 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -180,7 +180,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index b5cf03fe..e95c21f2 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 28eb1a36..f95ceda3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -151,7 +151,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 5ec92abe..2e672c06 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -180,7 +180,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index e8126208..46176dc2 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 32737edb..6b916612 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 335847ec..42eca740 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -179,7 +179,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index c152d219..4305f742 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -286,7 +286,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix Clone() { var result = CreateMatrix(RowCount, ColumnCount); - Storage.CopyTo(result.Storage, skipClearing: true); + Storage.CopyToUnchecked(result.Storage, skipClearing: true); return result; } @@ -309,17 +309,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentNullException("target"); } - if (ReferenceEquals(this, target) || ReferenceEquals(Storage, target.Storage)) - { - return; - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - Storage.CopyTo(target.Storage); } @@ -1485,8 +1474,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } var result = CreateMatrix(RowCount, ColumnCount + right.ColumnCount, fullyMutable: true); - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); - right.Storage.CopySubMatrixTo(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount, skipClearing: true); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); + right.Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount, skipClearing: true); return result; } @@ -1517,8 +1506,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); - right.Storage.CopySubMatrixTo(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); + right.Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount); } /// @@ -1541,8 +1530,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount, fullyMutable: true); - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); - lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount, skipClearing: true); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); + lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount, skipClearing: true); return result; } @@ -1575,8 +1564,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw DimensionsDontMatch(this, result, "result"); } - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); - lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); + lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount); } /// @@ -1595,8 +1584,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic } var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount + lower.ColumnCount, fullyMutable: true); - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); - lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); + lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); return result; } @@ -1625,8 +1614,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw DimensionsDontMatch(this, result, "result"); } - Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); - lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); + Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); + lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); } /// Calculates the L1 norm. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index cde0f437..ac2a119a 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public DenseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 41fdc738..9fb8ca13 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public DiagonalMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 52b3596d..b9039618 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -179,7 +179,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public SparseMatrix(Matrix matrix) : this(matrix.RowCount, matrix.ColumnCount) { - matrix.Storage.CopyTo(Storage, skipClearing: true); + matrix.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index 1a654550..4bd4f658 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -68,13 +68,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - /// Parameters assumed to be validated already. - public override void CopyTo(MatrixStorage target, bool skipClearing = false) + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { - CopyTo(denseTarget); + CopyToUnchecked(denseTarget); return; } @@ -89,29 +88,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(DenseColumnMajorMatrixStorage target) + void CopyToUnchecked(DenseColumnMajorMatrixStorage target) { - if (ReferenceEquals(this, target)) - { - return; - } - - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - //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 override void CopySubMatrixTo(MatrixStorage target, + internal override void CopySubMatrixToUnchecked(MatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) @@ -119,33 +102,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { - CopySubMatrixTo(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); + CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); return; } // FALL BACK - base.CopySubMatrixTo(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); + base.CopySubMatrixToUnchecked(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); } - void CopySubMatrixTo(DenseColumnMajorMatrixStorage target, + void CopySubMatrixToUnchecked(DenseColumnMajorMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (ReferenceEquals(this, target)) - { - throw new NotSupportedException(); - } - - ValidateSubMatrixRange(target, - sourceRowIndex, targetRowIndex, rowCount, - sourceColumnIndex, targetColumnIndex, columnCount); - for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { //Buffer.BlockCopy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T))); diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs index ab01ad4a..78a19d23 100644 --- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs @@ -143,27 +143,26 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } - /// Parameters assumed to be validated already. - public override void CopyTo(MatrixStorage target, bool skipClearing = false) + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var diagonalTarget = target as DiagonalMatrixStorage; if (diagonalTarget != null) { - CopyTo(diagonalTarget); + CopyToUnchecked(diagonalTarget); return; } var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { - CopyTo(denseTarget, skipClearing); + CopyToUnchecked(denseTarget, skipClearing); return; } var sparseTarget = target as SparseCompressedRowMatrixStorage; if (sparseTarget != null) { - CopyTo(sparseTarget, skipClearing); + CopyToUnchecked(sparseTarget, skipClearing); return; } @@ -180,41 +179,14 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(DiagonalMatrixStorage target) + void CopyToUnchecked(DiagonalMatrixStorage target) { - if (ReferenceEquals(this, target)) - { - return; - } - - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - //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); } - void CopyTo(SparseCompressedRowMatrixStorage target, bool skipClearing) + void CopyToUnchecked(SparseCompressedRowMatrixStorage target, bool skipClearing) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - if (!skipClearing) { target.Clear(); @@ -226,19 +198,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(DenseColumnMajorMatrixStorage target, bool skipClearing) + void CopyToUnchecked(DenseColumnMajorMatrixStorage target, bool skipClearing) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - if (!skipClearing) { target.Clear(); @@ -250,7 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - public override void CopySubMatrixTo(MatrixStorage target, + internal override void CopySubMatrixToUnchecked(MatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) @@ -258,43 +219,33 @@ namespace MathNet.Numerics.LinearAlgebra.Storage var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { - CopySubMatrixTo(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); + CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); return; } var diagonalTarget = target as DiagonalMatrixStorage; if (diagonalTarget != null) { - CopySubMatrixTo(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); + CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); return; } var sparseTarget = target as SparseCompressedRowMatrixStorage; if (sparseTarget != null) { - CopySubMatrixTo(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); + CopySubMatrixToUnchecked(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); return; } // FALL BACK - base.CopySubMatrixTo(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); + base.CopySubMatrixToUnchecked(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); } - void CopySubMatrixTo(DiagonalMatrixStorage target, + void CopySubMatrixToUnchecked(DiagonalMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - if (ReferenceEquals(this, target)) - { - throw new NotSupportedException(); - } - if (sourceRowIndex - sourceColumnIndex != targetRowIndex - targetColumnIndex) { if (Data.Any(x => !_zero.Equals(x))) @@ -306,10 +257,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return; } - ValidateSubMatrixRange(target, - sourceRowIndex, targetRowIndex, rowCount, - sourceColumnIndex, targetColumnIndex, columnCount); - var beginInclusive = Math.Max(sourceRowIndex, sourceColumnIndex); var endExclusive = Math.Min(sourceRowIndex + rowCount, sourceColumnIndex + columnCount); if (endExclusive > beginInclusive) @@ -319,20 +266,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopySubMatrixTo(DenseColumnMajorMatrixStorage target, + void CopySubMatrixToUnchecked(DenseColumnMajorMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - ValidateSubMatrixRange(target, - sourceRowIndex, targetRowIndex, rowCount, - sourceColumnIndex, targetColumnIndex, columnCount); - if (!skipClearing) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); @@ -379,20 +317,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopySubMatrixTo(SparseCompressedRowMatrixStorage target, + void CopySubMatrixToUnchecked(SparseCompressedRowMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing) { - if (target == null) - { - throw new ArgumentNullException("target"); - } - - ValidateSubMatrixRange(target, - sourceRowIndex, targetRowIndex, rowCount, - sourceColumnIndex, targetColumnIndex, columnCount); - if (!skipClearing) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs index f9f40e93..b5ce7f2d 100644 --- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs @@ -195,8 +195,28 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } - /// Parameters assumed to be validated already. - public virtual void CopyTo(MatrixStorage target, bool skipClearing = false) + public void CopyTo(MatrixStorage target, bool skipClearing = false) + { + if (target == null) + { + throw new ArgumentNullException("target"); + } + + if (ReferenceEquals(this, target)) + { + return; + } + + if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) + { + var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); + throw new ArgumentException(message, "target"); + } + + CopyToUnchecked(target, skipClearing); + } + + internal virtual void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { for (int j = 0; j < ColumnCount; j++) { @@ -207,7 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - public virtual void CopySubMatrixTo(MatrixStorage target, + public void CopySubMatrixTo(MatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) @@ -226,6 +246,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); + CopySubMatrixToUnchecked(target, sourceRowIndex, targetRowIndex, rowCount, + sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); + } + + internal virtual void CopySubMatrixToUnchecked(MatrixStorage target, + int sourceRowIndex, int targetRowIndex, int rowCount, + int sourceColumnIndex, int targetColumnIndex, int columnCount, + bool skipClearing = false) + { for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 50b77f7b..d0fa9599 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -338,20 +338,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } - /// Parameters assumed to be validated already. - public override void CopyTo(MatrixStorage target, bool skipClearing = false) + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var sparseTarget = target as SparseCompressedRowMatrixStorage; if (sparseTarget != null) { - CopyTo(sparseTarget); + CopyToUnchecked(sparseTarget); return; } var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { - CopyTo(denseTarget, skipClearing); + CopyToUnchecked(denseTarget, skipClearing); return; } @@ -376,19 +375,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(SparseCompressedRowMatrixStorage target) + void CopyToUnchecked(SparseCompressedRowMatrixStorage target) { - if (ReferenceEquals(this, target)) - { - return; - } - - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - target.ValueCount = ValueCount; target.Values = new T[ValueCount]; target.ColumnIndices = new int[ValueCount]; @@ -401,14 +389,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(DenseColumnMajorMatrixStorage target, bool skipClearing) + void CopyToUnchecked(DenseColumnMajorMatrixStorage target, bool skipClearing) { - if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) - { - var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); - throw new ArgumentException(message, "target"); - } - if (!skipClearing) { target.Clear(); @@ -428,7 +410,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - public override void CopySubMatrixTo(MatrixStorage target, + internal override void CopySubMatrixToUnchecked(MatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) @@ -441,7 +423,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage var sparseTarget = target as SparseCompressedRowMatrixStorage; if (sparseTarget != null) { - CopySubMatrixTo(sparseTarget, + CopySubMatrixToUnchecked(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); @@ -481,7 +463,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopySubMatrixTo(SparseCompressedRowMatrixStorage target, + void CopySubMatrixToUnchecked(SparseCompressedRowMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing)