Browse Source

vector: fixed CopyTo bug and added IEnumerable test

la-knuth
Marcus Cuda 17 years ago
parent
commit
ca361c5d73
  1. 10
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  2. 4
      src/Numerics/LinearAlgebra/Double/Vector.cs
  3. 5
      src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrixTests.cs
  4. 4
      src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs
  5. 31
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

10
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -259,14 +259,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
if (ReferenceEquals(this, target))
{
return;
}
var otherVector = target as DenseVector;
if (otherVector == null)
{
for (var index = 0; index < Data.Length; index++)
{
target[index] = Data[index];
}
Parallel.For(0, Data.Length, index => target[index] = Data[index]);
}
else
{

4
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -775,8 +775,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, destination))
{
var tmpVector = destination.CreateVector(destination.Count);
CopyTo(tmpVector, offset, destinationOffset, count);
tmpVector.CopyTo(destination);
CopyTo(tmpVector);
Parallel.For(0, count, index => destination[destinationOffset + index] = this[offset + index]);
}
else
{

5
src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrixTests.cs

@ -1,7 +1,4 @@
using System.Collections.Generic;
using MbUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using LinearAlgebra.Double;

4
src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs

@ -1,9 +1,7 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using System;
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra.Double;
using LinearAlgebra.Double;
internal class UserDefinedVector : Vector
{

31
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -6,6 +6,8 @@ using MbUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using System.Collections;
public abstract partial class VectorTests
{
private readonly double[] _data = {1, 2, 3, 4, 5};
@ -65,6 +67,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(0.0, other[4]);
}
[Test]
[MultipleAsserts]
public void CanCopyPartialVectorToSelf()
{
var vector = CreateVector(_data);
vector.CopyTo(vector, 0, 2, 2);
Assert.AreEqual(1.0, vector[0]);
Assert.AreEqual(2.0, vector[1]);
Assert.AreEqual(1.0, vector[2]);
Assert.AreEqual(2.0, vector[3]);
Assert.AreEqual(5.0, vector[4]);
}
[Test]
[MultipleAsserts]
public void CanCopyVectorToAnother()
@ -108,6 +124,21 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
[Test]
[MultipleAsserts]
public void CanEnumerateOverVectorUsingIEnumerable()
{
var vector = CreateVector(_data);
var index = 0;
var enumerable = (IEnumerable)vector;
foreach (var element in enumerable)
{
Assert.AreEqual(index + 1, (double)element);
index++;
}
}
[Test]
[MultipleAsserts]
public void CanEquateVectors()

Loading…
Cancel
Save