Browse Source

Updated unit tests to reflect new Vector API.

pull/36/head
Jurgen Van Gael 16 years ago
parent
commit
48454e6094
  1. 39
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  2. 46
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  3. 64
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

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

@ -341,6 +341,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector Add(Vector other)
@ -359,11 +360,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.Add(other);
return base.Add(other);
}
else
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data);
var copy = (DenseVector)this.Clone();
Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, 1.0, denseVector.Data);
return copy;
}
}
@ -457,17 +460,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Subtracts a scalar from each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the scalar.</returns>
public override Vector Subtract(double scalar)
{
if (scalar == 0.0)
{
return;
return this.Clone();
}
var copy = (DenseVector)this.Clone();
CommonParallel.For(
0,
Data.Length,
index => Data[index] -= scalar);
index => copy.Data[index] -= scalar);
return copy;
}
/// <summary>
@ -497,6 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector Subtract(Vector other)
@ -515,11 +522,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.Subtract(other);
return base.Subtract(other);
}
else
{
Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data);
var copy = (DenseVector)this.Clone();
Control.LinearAlgebraProvider.AddVectorToScaledVector(copy.Data, -1.0, denseVector.Data);
return copy;
}
}
@ -628,14 +637,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector Multiply(double scalar)
{
if (scalar == 1.0)
{
return;
return this.Clone();
}
Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
var copy = (DenseVector)this.Clone();
Control.LinearAlgebraProvider.ScaleArray(scalar, copy.Data);
return copy;
}
/// <summary>
@ -966,7 +978,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.PointwiseMultiply(other);
return base.PointwiseMultiply(other);
}
else
{
@ -1027,6 +1039,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector PointwiseDivide(Vector other)
@ -1045,14 +1058,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseVector == null)
{
base.PointwiseMultiply(other);
return base.PointwiseMultiply(other);
}
else
{
var copy = (DenseVector)this.Clone();
CommonParallel.For(
0,
Count,
index => this[index] /= other[index]);
Count,
index => copy[index] /= other[index]);
return copy;
}
}

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

@ -389,6 +389,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <returns>A new vector containing the sum of both vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector Add(Vector other)
@ -407,11 +408,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (sparseVector == null)
{
base.Add(other);
return base.Add(other);
}
else
{
AddScaledSparceVector(1.0, sparseVector);
var copy = (SparseVector)this.Clone();
copy.AddScaledSparceVector(1.0, sparseVector);
return copy;
}
}
@ -574,17 +577,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Subtracts a scalar from each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <returns>A new vector containing the subtraction of this vector and the scalar.</returns>
public override Vector Subtract(double scalar)
{
if (scalar == 0.0)
{
return;
return this.Clone();
}
var copy = (SparseVector)this.Clone();
for (var i = 0; i < Count; i++)
{
this[i] -= scalar;
copy[i] -= scalar;
}
return copy;
}
/// <summary>
@ -614,6 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Subtracts another vector from this vector.
/// </summary>
/// <param name="other">The vector to subtract from this one.</param>
/// <returns>A new vector containing the subtraction of the the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector Subtract(Vector other)
@ -632,11 +639,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (sparseVector == null)
{
base.Subtract(other);
return base.Subtract(other);
}
else
{
AddScaledSparceVector(-1.0, sparseVector);
var copy = (SparseVector)this.Clone();
copy.AddScaledSparceVector(-1.0, sparseVector);
return copy;
}
}
@ -753,20 +762,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Multiplies a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to multiply.</param>
/// <returns>A new vector that is the multiplication of the vector and the scalar.</returns>
public override Vector Multiply(double scalar)
{
if (scalar == 1.0)
{
return;
return this.Clone();
}
if (scalar == 0)
else if (scalar == 0)
{
Clear(); // Set array empty
return;
var copy = this.Clone();
copy.Clear(); // Set array empty
return copy;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues);
var copy = (SparseVector)this.Clone();
Control.LinearAlgebraProvider.ScaleArray(scalar, copy._nonZeroValues);
return copy;
}
}
/// <summary>
@ -1128,6 +1143,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <returns>A new vector which is the pointwise division of the two vectors.</returns>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override Vector PointwiseDivide(Vector other)
@ -1142,11 +1158,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
// base implementation iterates though all elements, but we need only take non-zeros
// base implementation iterates though all elements, but we need only take non-zeros
var copy = (SparseVector)this.Clone();
for (var i = 0; i < NonZerosCount; i++)
{
this[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]];
copy[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]];
}
return copy;
}
/// <summary>

64
src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

@ -60,8 +60,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanAddScalarToVector()
{
var vector = this.CreateVector(this._data);
vector.Add(2.0);
var copy = this.CreateVector(this._data);
var vector = copy.Add(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@ -166,9 +166,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanAddTwoVectors()
{
var vector = this.CreateVector(this._data);
var copy = this.CreateVector(this._data);
var other = this.CreateVector(this._data);
vector.Add(other);
var vector = copy.Add(other);
for (var i = 0; i < this._data.Length; i++)
{
@ -212,8 +212,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanAddVectorToItself()
{
var vector = this.CreateVector(this._data);
vector.Add(vector);
var copy = this.CreateVector(this._data);
var vector = copy.Add(copy);
for (var i = 0; i < this._data.Length; i++)
{
@ -285,8 +285,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanSubtractScalarFromVector()
{
var vector = this.CreateVector(this._data);
vector.Subtract(2.0);
var copy = this.CreateVector(this._data);
var vector = copy.Subtract(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@ -391,9 +391,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanSubtractTwoVectors()
{
var vector = this.CreateVector(this._data);
var copy = this.CreateVector(this._data);
var other = this.CreateVector(this._data);
vector.Subtract(other);
var vector = copy.Subtract(other);
for (var i = 0; i < this._data.Length; i++)
{
@ -437,8 +437,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void CanSubtractVectorFromItself()
{
var vector = this.CreateVector(this._data);
vector.Subtract(vector);
var copy = this.CreateVector(this._data);
var vector = copy.Subtract(copy);
for (var i = 0; i < this._data.Length; i++)
{
@ -480,8 +480,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanDivideVectorByScalar()
{
var vector = this.CreateVector(this._data);
vector.Divide(2.0);
var copy = this.CreateVector(this._data);
var vector = copy.Divide(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@ -520,8 +520,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts]
public void CanMultiplyVectorByScalar()
{
var vector = this.CreateVector(this._data);
vector.Multiply(2.0);
var copy = this.CreateVector(this._data);
var vector = copy.Multiply(2.0);
for (var i = 0; i < this._data.Length; i++)
{
@ -725,6 +725,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void PointwiseMultiply()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
var result = vector1.PointwiseMultiply(vector2);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] * this._data[i], result[i]);
}
}
[Test]
public void PointwiseMultiplyUsingResultVector()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
@ -767,19 +779,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
public void PointWiseMultiplyWithResult()
public void PointWiseDivide()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
vector1.PointwiseMultiply(vector2);
var result = vector1.PointwiseDivide(vector2);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] * this._data[i], vector1[i]);
Assert.AreEqual(this._data[i] / this._data[i], result[i]);
}
}
[Test]
public void PointWiseDivide()
public void PointWiseDivideUsingResultVector()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
@ -821,18 +833,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
vector1.PointwiseDivide(vector2, result);
}
[Test]
public void PointwiseDivideWithResult()
{
var vector1 = this.CreateVector(this._data);
var vector2 = vector1.Clone();
vector1.PointwiseDivide(vector2);
for (var i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(this._data[i] / this._data[i], vector1[i]);
}
}
[Test]
public void CanCalculateOuterProduct()
{

Loading…
Cancel
Save