Browse Source

Vector: Added ConjugateDotProduct

v2
Christoph Ruegg 13 years ago
parent
commit
33051dcc0c
  1. 27
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  2. 33
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  3. 23
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  4. 27
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  5. 33
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  6. 23
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  7. 10
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  8. 12
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  9. 10
      src/Numerics/LinearAlgebra/Double/Vector.cs
  10. 44
      src/Numerics/LinearAlgebra/Generic/Vector.Arithmetic.cs
  11. 10
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  12. 12
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  13. 10
      src/Numerics/LinearAlgebra/Single/Vector.cs

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

@ -434,14 +434,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>s
/// The result of the addition.</returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex DoDotProduct(Vector<Complex> other)
{
var denseVector = other as DenseVector;
return denseVector == null
? base.DoDotProduct(other)
: Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex DoConjugateDotProduct(Vector<Complex> other)
{
var denseVector = other as DenseVector;
if (denseVector == null) return base.DoConjugateDotProduct(other);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
// TODO: provide native zdotc routine
var dot = Complex.Zero;
for (var i = 0; i < _values.Length; i++)
{
dot += _values[i].Conjugate()*denseVector._values[i];
}
return dot;
}
/// <summary>

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

@ -540,16 +540,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex DoDotProduct(Vector<Complex> other)
{
var result = Complex.Zero;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
@ -564,7 +559,31 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex DoConjugateDotProduct(Vector<Complex> other)
{
var result = Complex.Zero;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Conjugate() * _storage.Values[i];
}
}
else
{
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Conjugate() * other.At(_storage.Indices[i]);
}
}
return result;
}

23
src/Numerics/LinearAlgebra/Complex/Vector.cs

@ -203,21 +203,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <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>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex DoDotProduct(Vector<Complex> other)
{
var dot = Complex.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex DoConjugateDotProduct(Vector<Complex> other)
{
var dot = Complex.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i).Conjugate() * other.At(i);
}
return dot;
}

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

@ -429,14 +429,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>s
/// The result of the addition.</returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex32 DoDotProduct(Vector<Complex32> other)
{
var denseVector = other as DenseVector;
return denseVector == null
? base.DoDotProduct(other)
: Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex32 DoConjugateDotProduct(Vector<Complex32> other)
{
var denseVector = other as DenseVector;
if (denseVector == null) return base.DoConjugateDotProduct(other);
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
// TODO: provide native cdotc routine
var dot = Complex32.Zero;
for (var i = 0; i < _values.Length; i++)
{
dot += _values[i].Conjugate() * denseVector._values[i];
}
return dot;
}
/// <summary>

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

@ -535,16 +535,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex32 DoDotProduct(Vector<Complex32> other)
{
var result = Complex32.Zero;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
@ -559,7 +554,31 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex32 DoConjugateDotProduct(Vector<Complex32> other)
{
var result = Complex32.Zero;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Conjugate() * _storage.Values[i];
}
}
else
{
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Conjugate() * other.At(_storage.Indices[i]);
}
}
return result;
}

23
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -198,21 +198,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override Complex32 DoDotProduct(Vector<Complex32> other)
{
var dot = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected override Complex32 DoConjugateDotProduct(Vector<Complex32> other)
{
var dot = Complex32.Zero;
for (var i = 0; i < Count; i++)
{
dot += At(i).Conjugate() * other.At(i);
}
return dot;
}

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

@ -440,14 +440,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>s
/// The result of the addition.</returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override double DoDotProduct(Vector<double> other)
{
var denseVector = other as DenseVector;
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
return denseVector == null
? base.DoDotProduct(other)
: Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
}
/// <summary>

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

@ -497,16 +497,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override double DoDotProduct(Vector<double> other)
{
var result = 0.0;
var result = 0d;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
@ -521,7 +516,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}

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

@ -200,21 +200,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override double DoDotProduct(Vector<double> other)
{
var dot = 0.0;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}

44
src/Numerics/LinearAlgebra/Generic/Vector.Arithmetic.cs

@ -96,10 +96,20 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <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>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected abstract T DoDotProduct(Vector<T> other);
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected virtual T DoConjugateDotProduct(Vector<T> other)
{
return DoDotProduct(other);
}
/// <summary>
/// Divides each element of the vector by a scalar and stores the result in the result vector.
/// </summary>
@ -502,25 +512,33 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <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>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</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 T DotProduct(Vector<T> other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (other == null) throw new ArgumentNullException("other");
if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
return DoDotProduct(other);
}
/// <summary>
/// Computes the dot product between the conjugate of this vector and another vector.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns>The sum of conj(a[i])*b[i] for all i.</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 T ConjugateDotProduct(Vector<T> other)
{
if (other == null) throw new ArgumentNullException("other");
if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
return DoConjugateDotProduct(other);
}
/// <summary>
/// Divides each element of the vector by a scalar.
/// </summary>

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

@ -429,14 +429,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">The other vector to add.</param>
/// <returns>s
/// The result of the addition.</returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override float DoDotProduct(Vector<float> other)
{
var denseVector = other as DenseVector;
return denseVector == null ? base.DoDotProduct(other) : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
return denseVector == null
? base.DoDotProduct(other)
: Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values);
}
/// <summary>

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

@ -498,16 +498,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override float DoDotProduct(Vector<float> other)
{
var result = 0.0f;
var result = 0f;
if (ReferenceEquals(this, other))
{
for (var i = 0; i < _storage.ValueCount; i++)
@ -522,7 +517,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}

10
src/Numerics/LinearAlgebra/Single/Vector.cs

@ -200,21 +200,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Computes the dot product between this vector and another vector.
/// </summary>
/// <param name="other">
/// The other vector to add.
/// </param>
/// <returns>s
/// The result of the addition.
/// </returns>
/// <param name="other">The other vector.</param>
/// <returns>The sum of a[i]*b[i] for all i.</returns>
protected override float DoDotProduct(Vector<float> other)
{
var dot = 0.0f;
for (var i = 0; i < Count; i++)
{
dot += At(i) * other.At(i);
}
return dot;
}

Loading…
Cancel
Save