Browse Source

LA: Matrix storage: clarify checked vs unchecked

v2
Christoph Ruegg 14 years ago
parent
commit
2cf9d889e9
  1. 2
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 2
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  3. 2
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 2
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  5. 2
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  6. 2
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  7. 2
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  8. 2
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  9. 2
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  10. 37
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  11. 2
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  12. 2
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  13. 2
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  14. 45
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  15. 101
      src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
  16. 35
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  17. 34
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DenseMatrix(Matrix<Complex> matrix) public DenseMatrix(Matrix<Complex> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -151,7 +151,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public DiagonalMatrix(Matrix<Complex> matrix) public DiagonalMatrix(Matrix<Complex> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -180,7 +180,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public SparseMatrix(Matrix<Complex> matrix) public SparseMatrix(Matrix<Complex> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DenseMatrix(Matrix<Complex32> matrix) public DenseMatrix(Matrix<Complex32> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -151,7 +151,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public DiagonalMatrix(Matrix<Complex32> matrix) public DiagonalMatrix(Matrix<Complex32> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -180,7 +180,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public SparseMatrix(Matrix<Complex32> matrix) public SparseMatrix(Matrix<Complex32> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(Matrix<double> matrix) public DenseMatrix(Matrix<double> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DiagonalMatrix(Matrix<double> matrix) public DiagonalMatrix(Matrix<double> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -179,7 +179,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public SparseMatrix(Matrix<double> matrix) public SparseMatrix(Matrix<double> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -286,7 +286,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> Clone() public virtual Matrix<T> Clone()
{ {
var result = CreateMatrix(RowCount, ColumnCount); var result = CreateMatrix(RowCount, ColumnCount);
Storage.CopyTo(result.Storage, skipClearing: true); Storage.CopyToUnchecked(result.Storage, skipClearing: true);
return result; return result;
} }
@ -309,17 +309,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentNullException("target"); 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); Storage.CopyTo(target.Storage);
} }
@ -1485,8 +1474,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
var result = CreateMatrix(RowCount, ColumnCount + right.ColumnCount, fullyMutable: true); var result = CreateMatrix(RowCount, ColumnCount + right.ColumnCount, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); Storage.CopySubMatrixToUnchecked(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); right.Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount, skipClearing: true);
return result; return result;
} }
@ -1517,8 +1506,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount);
right.Storage.CopySubMatrixTo(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount); right.Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, right.RowCount, 0, ColumnCount, right.ColumnCount);
} }
/// <summary> /// <summary>
@ -1541,8 +1530,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount, fullyMutable: true); var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount, skipClearing: true); Storage.CopySubMatrixToUnchecked(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); lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount, skipClearing: true);
return result; return result;
} }
@ -1575,8 +1564,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw DimensionsDontMatch<ArgumentException>(this, result, "result"); throw DimensionsDontMatch<ArgumentException>(this, result, "result");
} }
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount);
lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount); lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, 0, lower.ColumnCount);
} }
/// <summary> /// <summary>
@ -1595,8 +1584,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
} }
var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount + lower.ColumnCount, fullyMutable: true); var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount + lower.ColumnCount, fullyMutable: true);
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount);
lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount);
return result; return result;
} }
@ -1625,8 +1614,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw DimensionsDontMatch<ArgumentException>(this, result, "result"); throw DimensionsDontMatch<ArgumentException>(this, result, "result");
} }
Storage.CopySubMatrixTo(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount); Storage.CopySubMatrixToUnchecked(result.Storage, 0, 0, RowCount, 0, 0, ColumnCount);
lower.Storage.CopySubMatrixTo(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount); lower.Storage.CopySubMatrixToUnchecked(result.Storage, 0, RowCount, lower.RowCount, 0, ColumnCount, lower.ColumnCount);
} }
/// <summary>Calculates the L1 norm.</summary> /// <summary>Calculates the L1 norm.</summary>

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DenseMatrix(Matrix<float> matrix) public DenseMatrix(Matrix<float> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public DiagonalMatrix(Matrix<float> matrix) public DiagonalMatrix(Matrix<float> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -179,7 +179,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public SparseMatrix(Matrix<float> matrix) public SparseMatrix(Matrix<float> matrix)
: this(matrix.RowCount, matrix.ColumnCount) : this(matrix.RowCount, matrix.ColumnCount)
{ {
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyToUnchecked(Storage, skipClearing: true);
} }
/// <summary> /// <summary>

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

@ -68,13 +68,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
/// <remarks>Parameters assumed to be validated already.</remarks> internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)
public override void CopyTo(MatrixStorage<T> target, bool skipClearing = false)
{ {
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null) if (denseTarget != null)
{ {
CopyTo(denseTarget); CopyToUnchecked(denseTarget);
return; return;
} }
@ -89,29 +88,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopyTo(DenseColumnMajorMatrixStorage<T> target) void CopyToUnchecked(DenseColumnMajorMatrixStorage<T> 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))); //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); Array.Copy(Data, 0, target.Data, 0, Data.Length);
} }
public override void CopySubMatrixTo(MatrixStorage<T> target, internal override void CopySubMatrixToUnchecked(MatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing = false) bool skipClearing = false)
@ -119,33 +102,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null) if (denseTarget != null)
{ {
CopySubMatrixTo(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount);
return; return;
} }
// FALL BACK // 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<T> target, void CopySubMatrixToUnchecked(DenseColumnMajorMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount) 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++) 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))); //Buffer.BlockCopy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T)));

101
src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs

@ -143,27 +143,26 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return hash; return hash;
} }
/// <remarks>Parameters assumed to be validated already.</remarks> internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)
public override void CopyTo(MatrixStorage<T> target, bool skipClearing = false)
{ {
var diagonalTarget = target as DiagonalMatrixStorage<T>; var diagonalTarget = target as DiagonalMatrixStorage<T>;
if (diagonalTarget != null) if (diagonalTarget != null)
{ {
CopyTo(diagonalTarget); CopyToUnchecked(diagonalTarget);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null) if (denseTarget != null)
{ {
CopyTo(denseTarget, skipClearing); CopyToUnchecked(denseTarget, skipClearing);
return; return;
} }
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null) if (sparseTarget != null)
{ {
CopyTo(sparseTarget, skipClearing); CopyToUnchecked(sparseTarget, skipClearing);
return; return;
} }
@ -180,41 +179,14 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopyTo(DiagonalMatrixStorage<T> target) void CopyToUnchecked(DiagonalMatrixStorage<T> 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))); //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); Array.Copy(Data, 0, target.Data, 0, Data.Length);
} }
void CopyTo(SparseCompressedRowMatrixStorage<T> target, bool skipClearing) void CopyToUnchecked(SparseCompressedRowMatrixStorage<T> 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) if (!skipClearing)
{ {
target.Clear(); target.Clear();
@ -226,19 +198,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopyTo(DenseColumnMajorMatrixStorage<T> target, bool skipClearing) void CopyToUnchecked(DenseColumnMajorMatrixStorage<T> 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) if (!skipClearing)
{ {
target.Clear(); target.Clear();
@ -250,7 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
public override void CopySubMatrixTo(MatrixStorage<T> target, internal override void CopySubMatrixToUnchecked(MatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing = false) bool skipClearing = false)
@ -258,43 +219,33 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null) if (denseTarget != null)
{ {
CopySubMatrixTo(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing);
return; return;
} }
var diagonalTarget = target as DiagonalMatrixStorage<T>; var diagonalTarget = target as DiagonalMatrixStorage<T>;
if (diagonalTarget != null) if (diagonalTarget != null)
{ {
CopySubMatrixTo(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount);
return; return;
} }
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null) if (sparseTarget != null)
{ {
CopySubMatrixTo(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); CopySubMatrixToUnchecked(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing);
return; return;
} }
// FALL BACK // 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<T> target, void CopySubMatrixToUnchecked(DiagonalMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount) 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 (sourceRowIndex - sourceColumnIndex != targetRowIndex - targetColumnIndex)
{ {
if (Data.Any(x => !_zero.Equals(x))) if (Data.Any(x => !_zero.Equals(x)))
@ -306,10 +257,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return; return;
} }
ValidateSubMatrixRange(target,
sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount);
var beginInclusive = Math.Max(sourceRowIndex, sourceColumnIndex); var beginInclusive = Math.Max(sourceRowIndex, sourceColumnIndex);
var endExclusive = Math.Min(sourceRowIndex + rowCount, sourceColumnIndex + columnCount); var endExclusive = Math.Min(sourceRowIndex + rowCount, sourceColumnIndex + columnCount);
if (endExclusive > beginInclusive) if (endExclusive > beginInclusive)
@ -319,20 +266,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopySubMatrixTo(DenseColumnMajorMatrixStorage<T> target, void CopySubMatrixToUnchecked(DenseColumnMajorMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing) bool skipClearing)
{ {
if (target == null)
{
throw new ArgumentNullException("target");
}
ValidateSubMatrixRange(target,
sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount);
if (!skipClearing) if (!skipClearing)
{ {
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount);
@ -379,20 +317,11 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopySubMatrixTo(SparseCompressedRowMatrixStorage<T> target, void CopySubMatrixToUnchecked(SparseCompressedRowMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing) bool skipClearing)
{ {
if (target == null)
{
throw new ArgumentNullException("target");
}
ValidateSubMatrixRange(target,
sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount);
if (!skipClearing) if (!skipClearing)
{ {
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount);

35
src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs

@ -195,8 +195,28 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return hash; return hash;
} }
/// <remarks>Parameters assumed to be validated already.</remarks> public void CopyTo(MatrixStorage<T> target, bool skipClearing = false)
public virtual void CopyTo(MatrixStorage<T> 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<T> target, bool skipClearing = false)
{ {
for (int j = 0; j < ColumnCount; j++) for (int j = 0; j < ColumnCount; j++)
{ {
@ -207,7 +227,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
public virtual void CopySubMatrixTo(MatrixStorage<T> target, public void CopySubMatrixTo(MatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing = false) bool skipClearing = false)
@ -226,6 +246,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
sourceRowIndex, targetRowIndex, rowCount, sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount); sourceColumnIndex, targetColumnIndex, columnCount);
CopySubMatrixToUnchecked(target, sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount, skipClearing);
}
internal virtual void CopySubMatrixToUnchecked(MatrixStorage<T> 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 j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++)
{ {
for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++)

34
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -338,20 +338,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return hash; return hash;
} }
/// <remarks>Parameters assumed to be validated already.</remarks> internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)
public override void CopyTo(MatrixStorage<T> target, bool skipClearing = false)
{ {
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null) if (sparseTarget != null)
{ {
CopyTo(sparseTarget); CopyToUnchecked(sparseTarget);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null) if (denseTarget != null)
{ {
CopyTo(denseTarget, skipClearing); CopyToUnchecked(denseTarget, skipClearing);
return; return;
} }
@ -376,19 +375,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopyTo(SparseCompressedRowMatrixStorage<T> target) void CopyToUnchecked(SparseCompressedRowMatrixStorage<T> 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.ValueCount = ValueCount;
target.Values = new T[ValueCount]; target.Values = new T[ValueCount];
target.ColumnIndices = new int[ValueCount]; target.ColumnIndices = new int[ValueCount];
@ -401,14 +389,8 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopyTo(DenseColumnMajorMatrixStorage<T> target, bool skipClearing) void CopyToUnchecked(DenseColumnMajorMatrixStorage<T> 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) if (!skipClearing)
{ {
target.Clear(); target.Clear();
@ -428,7 +410,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
public override void CopySubMatrixTo(MatrixStorage<T> target, internal override void CopySubMatrixToUnchecked(MatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing = false) bool skipClearing = false)
@ -441,7 +423,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null) if (sparseTarget != null)
{ {
CopySubMatrixTo(sparseTarget, CopySubMatrixToUnchecked(sparseTarget,
sourceRowIndex, targetRowIndex, rowCount, sourceRowIndex, targetRowIndex, rowCount,
sourceColumnIndex, targetColumnIndex, columnCount, sourceColumnIndex, targetColumnIndex, columnCount,
skipClearing); skipClearing);
@ -481,7 +463,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
void CopySubMatrixTo(SparseCompressedRowMatrixStorage<T> target, void CopySubMatrixToUnchecked(SparseCompressedRowMatrixStorage<T> target,
int sourceRowIndex, int targetRowIndex, int rowCount, int sourceRowIndex, int targetRowIndex, int rowCount,
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
bool skipClearing) bool skipClearing)

Loading…
Cancel
Save