Browse Source

vector: fixed outer product parallel bug

la-knuth
Marcus Cuda 17 years ago
parent
commit
116d4c0a13
  1. 20
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  2. 20
      src/Numerics/LinearAlgebra/Double/Vector.cs
  3. 12
      src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs
  4. 12
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

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

@ -1083,14 +1083,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Dyadic product of two vectors /// Outer product of two vectors
/// </summary> /// </summary>
/// <param name="u">First vector</param> /// <param name="u">First vector</param>
/// <param name="v">Second vector</param> /// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns> /// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
public static DenseMatrix DyadicProduct(DenseVector u, DenseVector v) public static DenseMatrix OuterProduct(DenseVector u, DenseVector v)
{ {
if (u == null) if (u == null)
{ {
@ -1104,9 +1104,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var matrix = new DenseMatrix(u.Count, v.Count); var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For( CommonParallel.For(
0, 0,
u.Count, u.Count,
i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u.Data[i] * v.Data[j]))); i =>
{
for (int j = 0; j < v.Count; j++)
{
matrix.At(i, j, u.Data[i] * v.Data[j]);
}
});
return matrix; return matrix;
} }
@ -1169,10 +1175,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns> /// <returns>
/// Matrix M[i,j] = this[i] * v[j]. /// Matrix M[i,j] = this[i] * v[j].
/// </returns> /// </returns>
/// <seealso cref="DyadicProduct"/> /// <seealso cref="OuterProduct"/>
public Matrix TensorMultiply(DenseVector v) public Matrix TensorMultiply(DenseVector v)
{ {
return DyadicProduct(this, v); return OuterProduct(this, v);
} }
#region Vector Norms #region Vector Norms

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

@ -682,14 +682,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Dyadic product of two vectors /// Outer product of two vectors
/// </summary> /// </summary>
/// <param name="u">First vector</param> /// <param name="u">First vector</param>
/// <param name="v">Second vector</param> /// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns> /// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
public static DenseMatrix DyadicProduct(Vector u, Vector v) public static DenseMatrix OuterProduct(Vector u, Vector v)
{ {
if (u == null) if (u == null)
{ {
@ -703,9 +703,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var matrix = new DenseMatrix(u.Count, v.Count); var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For( CommonParallel.For(
0, 0,
u.Count, u.Count,
i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u[i] * v[j]))); i =>
{
for (int j = 0; j < v.Count; j++)
{
matrix.At(i, j, u[i] * v[j]);
}
});
return matrix; return matrix;
} }
@ -768,10 +774,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns> /// <returns>
/// Matrix M[i,j] = this[i] * v[j]. /// Matrix M[i,j] = this[i] * v[j].
/// </returns> /// </returns>
/// <seealso cref="DyadicProduct"/> /// <seealso cref="OuterProduct"/>
public Matrix TensorMultiply(Vector v) public Matrix TensorMultiply(Vector v)
{ {
return DyadicProduct(this, v); return OuterProduct(this, v);
} }
/// <summary> /// <summary>

12
src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs

@ -252,11 +252,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
} }
[Test] [Test]
public void CanCalculateDyadicProductForDenseVector() public void CanCalculateOuterProductForDenseVector()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = this.CreateVector(this._data);
var vector2 = 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 i = 0; i < vector1.Count; i++)
{ {
for (var j = 0; j < vector2.Count; j++) for (var j = 0; j < vector2.Count; j++)
@ -268,20 +268,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void DyadicProducForDenseVectortWithFirstParameterNullShouldThrowException() public void OuterProducForDenseVectortWithFirstParameterNullShouldThrowException()
{ {
DenseVector vector1 = null; DenseVector vector1 = null;
var vector2 = this.CreateVector(this._data); var vector2 = this.CreateVector(this._data);
Vector.DyadicProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
[Test] [Test]
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void DyadicProductForDenseVectorWithSecondParameterNullShouldThrowException() public void OuterProductForDenseVectorWithSecondParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = this.CreateVector(this._data);
DenseVector vector2 = null; DenseVector vector2 = null;
Vector.DyadicProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
} }
} }

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

@ -834,11 +834,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
} }
[Test] [Test]
public void CanCalculateDyadicProduct() public void CanCalculateOuterProduct()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = this.CreateVector(this._data);
var vector2 = 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 i = 0; i < vector1.Count; i++)
{ {
for (var j = 0; j < vector2.Count; j++) for (var j = 0; j < vector2.Count; j++)
@ -850,20 +850,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void DyadicProductWithFirstParameterNullShouldThrowException() public void OuterProductWithFirstParameterNullShouldThrowException()
{ {
Vector vector1 = null; Vector vector1 = null;
var vector2 = this.CreateVector(this._data); var vector2 = this.CreateVector(this._data);
Vector.DyadicProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
[Test] [Test]
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void DyadicProductWithSecondParameterNullShouldThrowException() public void OutercProductWithSecondParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = this.CreateVector(this._data);
Vector vector2 = null; Vector vector2 = null;
Vector.DyadicProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
[Test] [Test]

Loading…
Cancel
Save