diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 76ac6e18..0708eb65 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -400,14 +400,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// s - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex DoDotProduct(Vector other) { var denseVector = other as DenseVector; + return denseVector == null + ? base.DoDotProduct(other) + : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values); + } + + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex DoConjugateDotProduct(Vector 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; } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 454368c2..8c85c31e 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -506,16 +506,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex DoDotProduct(Vector 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; + } + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex DoConjugateDotProduct(Vector 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; } diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index c668c4bb..21cf2018 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -203,21 +203,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex DoDotProduct(Vector other) { var dot = Complex.Zero; - for (var i = 0; i < Count; i++) { dot += At(i) * other.At(i); } + return dot; + } + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex DoConjugateDotProduct(Vector other) + { + var dot = Complex.Zero; + for (var i = 0; i < Count; i++) + { + dot += At(i).Conjugate() * other.At(i); + } return dot; } diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 0dcc52b3..d23d3ae7 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -395,14 +395,33 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// s - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex32 DoDotProduct(Vector other) { var denseVector = other as DenseVector; + return denseVector == null + ? base.DoDotProduct(other) + : Control.LinearAlgebraProvider.DotProduct(_values, denseVector.Values); + } + + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex32 DoConjugateDotProduct(Vector 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; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 4d5f913d..8a79e8e3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -501,16 +501,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex32 DoDotProduct(Vector 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; + } + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex32 DoConjugateDotProduct(Vector 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; } diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index a4eb06a3..8b8c6039 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -198,21 +198,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override Complex32 DoDotProduct(Vector other) { var dot = Complex32.Zero; - for (var i = 0; i < Count; i++) { dot += At(i) * other.At(i); } + return dot; + } + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected override Complex32 DoConjugateDotProduct(Vector other) + { + var dot = Complex32.Zero; + for (var i = 0; i < Count; i++) + { + dot += At(i).Conjugate() * other.At(i); + } return dot; } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 8c0bdeb0..ac57bbce 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -405,14 +405,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// s - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override double DoDotProduct(Vector 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); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 17ff2785..86eb822e 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -462,16 +462,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override double DoDotProduct(Vector 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; } diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 2f56c5b8..e0b3d62f 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -199,21 +199,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override double DoDotProduct(Vector other) { var dot = 0.0; - for (var i = 0; i < Count; i++) { dot += At(i) * other.At(i); } - return dot; } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 814436a4..8ad16f12 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -394,14 +394,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// s - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override float DoDotProduct(Vector 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); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index caa7f492..f8ea1b20 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -463,16 +463,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override float DoDotProduct(Vector 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; } diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index b327ab72..e33f2fcd 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -199,21 +199,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// Computes the dot product between this vector and another vector. /// - /// - /// The other vector to add. - /// - /// s - /// The result of the addition. - /// + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected override float DoDotProduct(Vector other) { var dot = 0.0f; - for (var i = 0; i < Count; i++) { dot += At(i) * other.At(i); } - return dot; } diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index ed236886..ed42157e 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs @@ -96,10 +96,20 @@ namespace MathNet.Numerics.LinearAlgebra /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. protected abstract T DoDotProduct(Vector other); + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + protected virtual T DoConjugateDotProduct(Vector other) + { + return DoDotProduct(other); + } + /// /// Divides each element of the vector by a scalar and stores the result in the result vector. /// @@ -429,19 +439,30 @@ namespace MathNet.Numerics.LinearAlgebra /// /// Computes the dot product between this vector and another vector. /// - /// The other vector to add. - /// The result of the addition. + /// The other vector. + /// The sum of a[i]*b[i] for all i. /// If is not of the same size. public T DotProduct(Vector other) { - if (Count != other.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); - } + if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); return DoDotProduct(other); } + /// + /// Computes the dot product between the conjugate of this vector and another vector. + /// + /// The other vector. + /// The sum of conj(a[i])*b[i] for all i. + /// If is not of the same size. + /// If is . + public T ConjugateDotProduct(Vector other) + { + if (Count != other.Count) throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + + return DoConjugateDotProduct(other); + } + /// /// Divides each element of the vector by a scalar. ///