Browse Source

LA Storage: CopyTo

la-knuth
Christoph Ruegg 14 years ago
parent
commit
34d0d4d4f8
  1. 7
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 27
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  3. 7
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  4. 28
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  5. 7
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  6. 27
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  7. 7
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  8. 27
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  9. 66
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
  10. 28
      src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs

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

@ -823,6 +823,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var sparseTarget = target as SparseMatrix;
if (sparseTarget != null)
{
_storage.CopyTo(sparseTarget.Storage);
return;
}
base.CopyTo(target);
}

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

@ -692,35 +692,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override void CopyTo(Matrix<Complex> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
if (sparseTarget != null)
{
base.CopyTo(target);
_storage.CopyTo(sparseTarget.Storage);
return;
}
if (ReferenceEquals(this, target))
var denseTarget = target as DenseMatrix;
if (denseTarget != null)
{
_storage.CopyTo(denseTarget.Storage);
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
var targetStorage = sparseTarget.Storage;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetStorage.ValueCount = _storage.ValueCount;
targetStorage.Values = new Complex[_storage.ValueCount];
targetStorage.ColumnIndices = new int[_storage.ValueCount];
if (_storage.ValueCount != 0)
{
Array.Copy(_storage.Values, targetStorage.Values, _storage.ValueCount);
Buffer.BlockCopy(_storage.ColumnIndices, 0, targetStorage.ColumnIndices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.RowPointers, 0, targetStorage.RowPointers, 0, RowCount * Constants.SizeOfInt);
}
base.CopyTo(target);
}
/// <summary>

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

@ -828,6 +828,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var sparseTarget = target as SparseMatrix;
if (sparseTarget != null)
{
_storage.CopyTo(sparseTarget.Storage);
return;
}
base.CopyTo(target);
}

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

@ -692,36 +692,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override void CopyTo(Matrix<Complex32> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
if (sparseTarget != null)
{
base.CopyTo(target);
_storage.CopyTo(sparseTarget.Storage);
return;
}
if (ReferenceEquals(this, target))
var denseTarget = target as DenseMatrix;
if (denseTarget != null)
{
_storage.CopyTo(denseTarget.Storage);
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
var targetStorage = sparseTarget.Storage;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetStorage.ValueCount = _storage.ValueCount;
targetStorage.Values = new Complex32[_storage.ValueCount];
targetStorage.ColumnIndices = new int[_storage.ValueCount];
if (_storage.ValueCount != 0)
{
Array.Copy(_storage.Values, targetStorage.Values, _storage.ValueCount);
Buffer.BlockCopy(_storage.ColumnIndices, 0, targetStorage.ColumnIndices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.RowPointers, 0, targetStorage.RowPointers, 0, RowCount * Constants.SizeOfInt);
}
base.CopyTo(target);
}
/// <summary>

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

@ -817,6 +817,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var sparseTarget = target as SparseMatrix;
if (sparseTarget != null)
{
_storage.CopyTo(sparseTarget.Storage);
return;
}
base.CopyTo(target);
}

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

@ -691,35 +691,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override void CopyTo(Matrix<double> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
if (sparseTarget != null)
{
base.CopyTo(target);
_storage.CopyTo(sparseTarget.Storage);
return;
}
if (ReferenceEquals(this, target))
var denseTarget = target as DenseMatrix;
if (denseTarget != null)
{
_storage.CopyTo(denseTarget.Storage);
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
var targetStorage = sparseTarget.Storage;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetStorage.ValueCount = _storage.ValueCount;
targetStorage.Values = new double[_storage.ValueCount];
targetStorage.ColumnIndices = new int[_storage.ValueCount];
if (_storage.ValueCount != 0)
{
Buffer.BlockCopy(_storage.Values, 0, targetStorage.Values, 0, _storage.ValueCount * Constants.SizeOfDouble);
Buffer.BlockCopy(_storage.ColumnIndices, 0, targetStorage.ColumnIndices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.RowPointers, 0, targetStorage.RowPointers, 0, RowCount * Constants.SizeOfInt);
}
base.CopyTo(target);
}
/// <summary>

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

@ -821,6 +821,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
_storage.CopyTo(denseTarget.Storage);
return;
}
var sparseTarget = target as SparseMatrix;
if (sparseTarget != null)
{
_storage.CopyTo(sparseTarget.Storage);
return;
}
base.CopyTo(target);
}

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

@ -691,35 +691,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override void CopyTo(Matrix<float> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
if (sparseTarget != null)
{
base.CopyTo(target);
_storage.CopyTo(sparseTarget.Storage);
return;
}
if (ReferenceEquals(this, target))
var denseTarget = target as DenseMatrix;
if (denseTarget != null)
{
_storage.CopyTo(denseTarget.Storage);
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
var targetStorage = sparseTarget.Storage;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetStorage.ValueCount = _storage.ValueCount;
targetStorage.Values = new float[_storage.ValueCount];
targetStorage.ColumnIndices = new int[_storage.ValueCount];
if (_storage.ValueCount != 0)
{
Buffer.BlockCopy(_storage.Values, 0, targetStorage.Values, 0, _storage.ValueCount * Constants.SizeOfFloat);
Buffer.BlockCopy(_storage.ColumnIndices, 0, targetStorage.ColumnIndices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.RowPointers, 0, targetStorage.RowPointers, 0, RowCount * Constants.SizeOfInt);
}
base.CopyTo(target);
}
/// <summary>

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

@ -197,7 +197,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <param name="itemIndex">Index of value in nonZeroValues array</param>
/// <param name="row">Row number of matrix</param>
/// <remarks>WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks</remarks>
private void DeleteItemByIndex(int itemIndex, int row)
void DeleteItemByIndex(int itemIndex, int row)
{
// Move all values (with an position larger than index) in the value array to the previous position
// move all values (with an position larger than index) in the columIndices array to the previous position
@ -244,7 +244,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// increased in size.
/// </summary>
/// <returns>The amount grown.</returns>
private int GrowthSize()
int GrowthSize()
{
int delta;
if (Values.Length > 1024)
@ -265,5 +265,67 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return delta;
}
public void CopyTo(SparseCompressedRowMatrixStorage<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");
}
target.ValueCount = ValueCount;
target.Values = new T[ValueCount];
target.ColumnIndices = new int[ValueCount];
if (ValueCount != 0)
{
Array.Copy(Values, target.Values, ValueCount);
Buffer.BlockCopy(ColumnIndices, 0, target.ColumnIndices, 0, ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(RowPointers, 0, target.RowPointers, 0, RowCount * Constants.SizeOfInt);
}
}
public void CopyTo(DenseColumnMajorMatrixStorage<T> target, bool skipClearing = false)
{
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();
}
if (ValueCount != 0)
{
for (int row = 0; row < RowCount; row++)
{
var startIndex = RowPointers[row];
var endIndex = row < RowPointers.Length - 1 ? RowPointers[row + 1] : ValueCount;
for (var j = startIndex; j < endIndex; j++)
{
target.At(row, ColumnIndices[j], Values[j]);
}
}
}
}
}
}

28
src/Numerics/LinearAlgebra/Storage/SparseDiagonalMatrixStorage.cs

@ -127,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
Array.Copy(Data, 0, target.Data, 0, Data.Length);
}
public void CopyTo(DenseColumnMajorMatrixStorage<T> target, bool targetKnownClear = false)
public void CopyTo(SparseCompressedRowMatrixStorage<T> target, bool skipClearing = false)
{
if (target == null)
{
@ -140,7 +140,31 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
throw new ArgumentException(message, "target");
}
if (!targetKnownClear)
if (!skipClearing)
{
target.Clear();
}
for (int i = 0; i < Data.Length; i++)
{
target.At(i, i, Data[i]);
}
}
public void CopyTo(DenseColumnMajorMatrixStorage<T> target, bool skipClearing = false)
{
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();
}

Loading…
Cancel
Save