Browse Source

Vector: Added ConjugateDotProduct

optimization-1
Christoph Ruegg 13 years ago
parent
commit
0d1999998e
  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. 10
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  11. 12
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  12. 10
      src/Numerics/LinearAlgebra/Single/Vector.cs
  13. 37
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

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

@ -400,14 +400,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

@ -506,16 +506,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++)
@ -530,7 +525,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

@ -395,14 +395,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

@ -501,16 +501,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++)
@ -525,7 +520,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

@ -405,14 +405,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

@ -462,16 +462,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++)
@ -486,7 +481,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}

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

@ -199,21 +199,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;
}

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

@ -394,14 +394,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

@ -463,16 +463,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++)
@ -487,7 +482,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
result += _storage.Values[i] * other.At(_storage.Indices[i]);
}
}
return result;
}

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

@ -199,21 +199,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;
}

37
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -96,10 +96,20 @@ namespace MathNet.Numerics.LinearAlgebra
/// <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>
@ -429,19 +439,30 @@ namespace MathNet.Numerics.LinearAlgebra
/// <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>
public T DotProduct(Vector<T> other)
{
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "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 (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
return DoConjugateDotProduct(other);
}
/// <summary>
/// Divides each element of the vector by a scalar.
/// </summary>

Loading…
Cancel
Save