From ca361c5d73c35058d33570625358419ffacecf7b Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Mon, 4 Jan 2010 19:18:18 +0800 Subject: [PATCH] vector: fixed CopyTo bug and added IEnumerable test --- .../LinearAlgebra/Double/DenseVector.cs | 10 +++--- src/Numerics/LinearAlgebra/Double/Vector.cs | 4 +-- .../Double/UserDefinedMatrixTests.cs | 5 +-- .../Double/UserDefinedVectorTests.cs | 4 +-- .../LinearAlgebraTests/Double/VectorTests.cs | 31 +++++++++++++++++++ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index e28f45f7..b820241f 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/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 { diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 412bd298..81dc647c 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/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 { diff --git a/src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrixTests.cs index 14a6054e..a9893e0c 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/UserDefinedMatrixTests.cs +++ b/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; diff --git a/src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs index 320a7d4f..19926be7 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs +++ b/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 { diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index 7dced736..3662ab0f 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/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()