Browse Source

Perf: override CopyTo for all linear algebra types

pull/47/head
Christoph Ruegg 14 years ago
parent
commit
16414ce904
  1. 34
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 43
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  3. 33
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 2
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  5. 34
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  6. 43
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  7. 33
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  8. 2
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  9. 34
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  10. 35
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  11. 7
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  12. 23
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  13. 34
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  14. 35
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  15. 7
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

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

@ -180,6 +180,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseVector(size);
}
/// <summary>
/// Copies the elements of this matrix to the given matrix.
/// </summary>
/// <param name="target">
/// The matrix to copy values into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If target is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If this and the target matrix do not have the same dimensions..
/// </exception>
public override void CopyTo(Matrix<Complex> target)
{
var denseTarget = target as DenseMatrix;
if (denseTarget == null)
{
base.CopyTo(target);
return;
}
if (ReferenceEquals(this, target))
{
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
Array.Copy(Data, 0, denseTarget.Data, 0, Data.Length);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

43
src/Numerics/LinearAlgebra/Complex/DenseVector.cs

@ -244,6 +244,49 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseVector(size);
}
/// <summary>
/// Copies the values of this vector into the target vector.
/// </summary>
/// <param name="target">
/// The vector to copy elements into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="target"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="target"/> is not the same size as this vector.
/// </exception>
public override void CopyTo(Vector<Complex> target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target))
{
return;
}
var otherVector = target as DenseVector;
if (otherVector == null)
{
CommonParallel.For(
0,
Data.Length,
index => target[index] = Data[index]);
}
else
{
Array.Copy(Data, 0, otherVector.Data, 0, Data.Length);
}
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>

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

@ -803,31 +803,32 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override void CopyTo(Matrix<Complex> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
{
base.CopyTo(target);
return;
}
else
if (ReferenceEquals(this, target))
{
if (ReferenceEquals(this, target))
{
return;
}
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new Complex[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new Complex[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
if (NonZerosCount != 0)
{
Array.Copy(_nonZeroValues, sparseTarget._nonZeroValues, NonZerosCount);
Array.Copy(_columnIndices, sparseTarget._columnIndices, NonZerosCount);
Array.Copy(_rowIndex, sparseTarget._rowIndex, RowCount);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount * Constants.SizeOfInt);
}
}

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

@ -304,7 +304,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (NonZerosCount != 0)
{
CommonParallel.For(0, NonZerosCount, index => otherVector._nonZeroValues[index] = _nonZeroValues[index]);
Array.Copy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
}
}

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

@ -180,6 +180,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseVector(size);
}
/// <summary>
/// Copies the elements of this matrix to the given matrix.
/// </summary>
/// <param name="target">
/// The matrix to copy values into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If target is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If this and the target matrix do not have the same dimensions..
/// </exception>
public override void CopyTo(Matrix<Complex32> target)
{
var denseTarget = target as DenseMatrix;
if (denseTarget == null)
{
base.CopyTo(target);
return;
}
if (ReferenceEquals(this, target))
{
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
Array.Copy(Data, 0, denseTarget.Data, 0, Data.Length);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

43
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -245,6 +245,49 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseVector(size);
}
/// <summary>
/// Copies the values of this vector into the target vector.
/// </summary>
/// <param name="target">
/// The vector to copy elements into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="target"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="target"/> is not the same size as this vector.
/// </exception>
public override void CopyTo(Vector<Complex32> target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target))
{
return;
}
var otherVector = target as DenseVector;
if (otherVector == null)
{
CommonParallel.For(
0,
Data.Length,
index => target[index] = Data[index]);
}
else
{
Array.Copy(Data, 0, otherVector.Data, 0, Data.Length);
}
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>

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

@ -804,31 +804,32 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override void CopyTo(Matrix<Complex32> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
{
base.CopyTo(target);
return;
}
else
if (ReferenceEquals(this, target))
{
if (ReferenceEquals(this, target))
{
return;
}
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new Complex32[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new Complex32[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
if (NonZerosCount != 0)
{
Array.Copy(_nonZeroValues, sparseTarget._nonZeroValues, NonZerosCount);
Array.Copy(_columnIndices, sparseTarget._columnIndices, NonZerosCount);
Array.Copy(_rowIndex, sparseTarget._rowIndex, RowCount);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount * Constants.SizeOfInt);
}
}

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

@ -334,7 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (NonZerosCount != 0)
{
CommonParallel.For(0, NonZerosCount, index => otherVector._nonZeroValues[index] = _nonZeroValues[index]);
Array.Copy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
}
}

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

@ -181,6 +181,40 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseVector(size);
}
/// <summary>
/// Copies the elements of this matrix to the given matrix.
/// </summary>
/// <param name="target">
/// The matrix to copy values into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If target is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If this and the target matrix do not have the same dimensions..
/// </exception>
public override void CopyTo(Matrix<double> target)
{
var denseTarget = target as DenseMatrix;
if (denseTarget == null)
{
base.CopyTo(target);
return;
}
if (ReferenceEquals(this, target))
{
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
Buffer.BlockCopy(Data, 0, denseTarget.Data, 0, Data.Length * Constants.SizeOfDouble);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

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

@ -815,31 +815,32 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override void CopyTo(Matrix<double> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
{
base.CopyTo(target);
return;
}
else
if (ReferenceEquals(this, target))
{
if (ReferenceEquals(this, target))
{
return;
}
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new double[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new double[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(_nonZeroValues, 0, sparseTarget._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount * Constants.SizeOfInt);
if (NonZerosCount != 0)
{
Buffer.BlockCopy(_nonZeroValues, 0, sparseTarget._nonZeroValues, 0, NonZerosCount*Constants.SizeOfDouble);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount*Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount*Constants.SizeOfInt);
}
}

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

@ -296,8 +296,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
otherVector._nonZeroIndices = new int[NonZerosCount];
otherVector.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
if (NonZerosCount != 0)
{
Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount*Constants.SizeOfDouble);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount*Constants.SizeOfInt);
}
}
}

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

@ -273,23 +273,26 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentNullException("target");
}
if (ReferenceEquals(this, target))
{
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target);
}
for (var i = 0; i < RowCount; i++)
if (ReferenceEquals(this, target))
{
for (var j = 0; j < ColumnCount; j++)
{
target.At(i, j, At(i, j));
}
return;
}
CommonParallel.For(
0,
RowCount,
row =>
{
for (var j = 0; j < ColumnCount; j++)
{
target.At(row, j, At(row, j));
}
});
}
/// <summary>

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

@ -180,6 +180,40 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseVector(size);
}
/// <summary>
/// Copies the elements of this matrix to the given matrix.
/// </summary>
/// <param name="target">
/// The matrix to copy values into.
/// </param>
/// <exception cref="ArgumentNullException">
/// If target is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// If this and the target matrix do not have the same dimensions..
/// </exception>
public override void CopyTo(Matrix<float> target)
{
var denseTarget = target as DenseMatrix;
if (denseTarget == null)
{
base.CopyTo(target);
return;
}
if (ReferenceEquals(this, target))
{
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
Buffer.BlockCopy(Data, 0, denseTarget.Data, 0, Data.Length * Constants.SizeOfFloat);
}
/// <summary>
/// Gets or sets the value at the given row and column.
/// </summary>

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

@ -798,31 +798,32 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override void CopyTo(Matrix<float> target)
{
var sparseTarget = target as SparseMatrix;
if (sparseTarget == null)
{
base.CopyTo(target);
return;
}
else
if (ReferenceEquals(this, target))
{
if (ReferenceEquals(this, target))
{
return;
}
return;
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
if (RowCount != target.RowCount || ColumnCount != target.ColumnCount)
{
throw DimensionsDontMatch<ArgumentException>(this, target, "target");
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new float[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
sparseTarget._nonZeroValues = new float[NonZerosCount];
sparseTarget._columnIndices = new int[NonZerosCount];
sparseTarget.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(_nonZeroValues, 0, sparseTarget._nonZeroValues, 0, NonZerosCount * Constants.SizeOfFloat);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount * Constants.SizeOfInt);
if (NonZerosCount != 0)
{
Buffer.BlockCopy(_nonZeroValues, 0, sparseTarget._nonZeroValues, 0, NonZerosCount*Constants.SizeOfFloat);
Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount*Constants.SizeOfInt);
Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount*Constants.SizeOfInt);
}
}

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

@ -326,8 +326,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single
otherVector._nonZeroIndices = new int[NonZerosCount];
otherVector.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount * Constants.SizeOfFloat);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
if (NonZerosCount != 0)
{
Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount*Constants.SizeOfFloat);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount*Constants.SizeOfInt);
}
}
}

Loading…
Cancel
Save