Browse Source

Added vector Dot product.

la-knuth
Jurgen Van Gael 17 years ago
parent
commit
a8b66050c0
  1. 60
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  2. 56
      src/Numerics/LinearAlgebra/Double/Vector.cs
  3. 68
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

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

@ -620,6 +620,38 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public override double DotProduct(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
return base.DotProduct(other);
}
else
{
return Control.LinearAlgebraProvider.DotProduct(this.Data, denseVector.Data);
}
}
/// <summary>
/// Multiplies a vector with a scalar.
/// </summary>
@ -658,6 +690,34 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ret;
}
/// <summary>
/// Computes the dot product between two <strong>Vectors</strong>.
/// </summary>
/// <param name="leftSide">The left row vector.</param>
/// <param name="rightSide">The right column vector.</param>
/// <returns>The dot product between the two vectors.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static double operator *(DenseVector leftSide, DenseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return Control.LinearAlgebraProvider.DotProduct(leftSide.Data, rightSide.Data);
}
/// <summary>
/// Divides a vector with a scalar.
/// </summary>

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

@ -370,6 +370,34 @@ namespace MathNet.Numerics.LinearAlgebra.Double
result.Multiply(scalar);
}
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="other"/> is not of the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="other"/> is <see langword="null" />.</exception>
public virtual double DotProduct(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
double dot = 0.0;
for (int i = 0; i < Count; i++)
{
dot += this[i] * other[i];
}
return dot;
}
/// <summary>
/// Divides each element of the vector by a scalar.
/// </summary>
@ -544,6 +572,34 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ret;
}
/// <summary>
/// Computes the dot product between two <strong>Vectors</strong>.
/// </summary>
/// <param name="leftSide">The left row vector.</param>
/// <param name="rightSide">The right column vector.</param>
/// <returns>The dot product between the two vectors.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static double operator *(Vector leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
return leftSide.DotProduct(rightSide);
}
/// <summary>
/// Divides a vector with a scalar.
/// </summary>

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

@ -628,5 +628,73 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Vector vector = null;
Assert.Throws<ArgumentNullException>(() => vector = vector / 2.0);
}
[Test]
public void CanDotProduct()
{
Vector dataA = CreateVector(_data);
Vector dataB = CreateVector(_data);
Assert.AreEqual(55.0, dataA.DotProduct(dataB));
}
[Test]
[ExpectedArgumentNullException]
public void DotProductThrowsExceptionWhenArgumentIsNull()
{
Vector dataA = CreateVector(_data);
Vector dataB = null;
dataA.DotProduct(dataB);
}
[Test]
[ExpectedArgumentException]
public void DotProductThrowsExceptionWhenArgumentHasDifferentSize()
{
Vector dataA = CreateVector(_data);
Vector dataB = CreateVector(new double[] {1, 2, 3, 4, 5, 6});
dataA.DotProduct(dataB);
}
[Test]
public void CanDotProductUsingOperator()
{
Vector dataA = CreateVector(_data);
Vector dataB = CreateVector(_data);
Assert.AreEqual(55.0, dataA * dataB);
}
[Test]
[ExpectedArgumentNullException]
public void OperatorDotProductThrowsExceptionWhenLeftArgumentIsNull()
{
Vector dataA = CreateVector(_data);
Vector dataB = null;
double d = dataA * dataB;
}
[Test]
[ExpectedArgumentNullException]
public void OperatorDotProductThrowsExceptionWhenRightArgumentIsNull()
{
Vector dataA = null;
Vector dataB = CreateVector(_data);
double d = dataA * dataB;
}
[Test]
[ExpectedArgumentException]
public void OperatorDotProductThrowsExceptionWhenArgumentHasDifferentSize()
{
Vector dataA = CreateVector(_data);
Vector dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 });
double d = dataA * dataB;
}
}
}
Loading…
Cancel
Save