From 116d4c0a1349ebe2bfd1a96b77445b9970e79f07 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Mon, 7 Jun 2010 19:37:33 +0300 Subject: [PATCH] vector: fixed outer product parallel bug --- .../LinearAlgebra/Double/DenseVector.cs | 20 ++++++++++++------- src/Numerics/LinearAlgebra/Double/Vector.cs | 20 ++++++++++++------- .../Double/DenseVectorTests.cs | 12 +++++------ .../Double/VectorTests.Arithmetic.cs | 12 +++++------ 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 530684f7..e6ca6b6d 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -1083,14 +1083,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Dyadic product of two vectors + /// Outer product of two vectors /// /// First vector /// Second vector /// Matrix M[i,j] = u[i]*v[j] /// If the u vector is . /// If the v vector is . - public static DenseMatrix DyadicProduct(DenseVector u, DenseVector v) + public static DenseMatrix OuterProduct(DenseVector u, DenseVector v) { if (u == null) { @@ -1104,9 +1104,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, - i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u.Data[i] * v.Data[j]))); + 0, + u.Count, + i => + { + for (int j = 0; j < v.Count; j++) + { + matrix.At(i, j, u.Data[i] * v.Data[j]); + } + }); return matrix; } @@ -1169,10 +1175,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Matrix M[i,j] = this[i] * v[j]. /// - /// + /// public Matrix TensorMultiply(DenseVector v) { - return DyadicProduct(this, v); + return OuterProduct(this, v); } #region Vector Norms diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 91a354f9..531c09eb 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -682,14 +682,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Dyadic product of two vectors + /// Outer product of two vectors /// /// First vector /// Second vector /// Matrix M[i,j] = u[i]*v[j] /// If the u vector is . /// If the v vector is . - public static DenseMatrix DyadicProduct(Vector u, Vector v) + public static DenseMatrix OuterProduct(Vector u, Vector v) { if (u == null) { @@ -703,9 +703,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, - i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u[i] * v[j]))); + 0, + u.Count, + i => + { + for (int j = 0; j < v.Count; j++) + { + matrix.At(i, j, u[i] * v[j]); + } + }); return matrix; } @@ -768,10 +774,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Matrix M[i,j] = this[i] * v[j]. /// - /// + /// public Matrix TensorMultiply(Vector v) { - return DyadicProduct(this, v); + return OuterProduct(this, v); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs index 981bbfa9..10cada79 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs @@ -252,11 +252,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } [Test] - public void CanCalculateDyadicProductForDenseVector() + public void CanCalculateOuterProductForDenseVector() { var vector1 = this.CreateVector(this._data); var vector2 = this.CreateVector(this._data); - Matrix m = Vector.DyadicProduct(vector1, vector2); + Matrix m = Vector.OuterProduct(vector1, vector2); for (var i = 0; i < vector1.Count; i++) { for (var j = 0; j < vector2.Count; j++) @@ -268,20 +268,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] [ExpectedArgumentNullException] - public void DyadicProducForDenseVectortWithFirstParameterNullShouldThrowException() + public void OuterProducForDenseVectortWithFirstParameterNullShouldThrowException() { DenseVector vector1 = null; var vector2 = this.CreateVector(this._data); - Vector.DyadicProduct(vector1, vector2); + Vector.OuterProduct(vector1, vector2); } [Test] [ExpectedArgumentNullException] - public void DyadicProductForDenseVectorWithSecondParameterNullShouldThrowException() + public void OuterProductForDenseVectorWithSecondParameterNullShouldThrowException() { var vector1 = this.CreateVector(this._data); DenseVector vector2 = null; - Vector.DyadicProduct(vector1, vector2); + Vector.OuterProduct(vector1, vector2); } } } \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs index dae3835e..a28b56f8 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs @@ -834,11 +834,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } [Test] - public void CanCalculateDyadicProduct() + public void CanCalculateOuterProduct() { var vector1 = this.CreateVector(this._data); var vector2 = this.CreateVector(this._data); - Matrix m = Vector.DyadicProduct(vector1, vector2); + Matrix m = Vector.OuterProduct(vector1, vector2); for (var i = 0; i < vector1.Count; i++) { for (var j = 0; j < vector2.Count; j++) @@ -850,20 +850,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] [ExpectedArgumentNullException] - public void DyadicProductWithFirstParameterNullShouldThrowException() + public void OuterProductWithFirstParameterNullShouldThrowException() { Vector vector1 = null; var vector2 = this.CreateVector(this._data); - Vector.DyadicProduct(vector1, vector2); + Vector.OuterProduct(vector1, vector2); } [Test] [ExpectedArgumentNullException] - public void DyadicProductWithSecondParameterNullShouldThrowException() + public void OutercProductWithSecondParameterNullShouldThrowException() { var vector1 = this.CreateVector(this._data); Vector vector2 = null; - Vector.DyadicProduct(vector1, vector2); + Vector.OuterProduct(vector1, vector2); } [Test]