Browse Source

vector: fixed outer product parallel bug

pull/36/head
Marcus Cuda 16 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>
/// Dyadic product of two vectors
/// Outer product of two vectors
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <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 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)
{
@ -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
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="DyadicProduct"/>
/// <seealso cref="OuterProduct"/>
public Matrix TensorMultiply(DenseVector v)
{
return DyadicProduct(this, v);
return OuterProduct(this, v);
}
#region Vector Norms

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

@ -682,14 +682,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Dyadic product of two vectors
/// Outer product of two vectors
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <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 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)
{
@ -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
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="DyadicProduct"/>
/// <seealso cref="OuterProduct"/>
public Matrix TensorMultiply(Vector v)
{
return DyadicProduct(this, v);
return OuterProduct(this, v);
}
/// <summary>

12
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);
}
}
}

12
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]

Loading…
Cancel
Save