Browse Source

LA: proper vector outer product api, faster implementation

provider
Christoph Ruegg 13 years ago
parent
commit
34888f8413
  1. 47
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  2. 47
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  3. 47
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  4. 47
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  5. 47
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  6. 44
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  7. 47
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  8. 47
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  9. 54
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

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

@ -667,53 +667,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// 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 OuterProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(0, u.Count, (a, b) =>
{
for (int i = a; i < b; i++)
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u._values[i]*v._values[j]);
}
}
});
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct(DenseVector, DenseVector)"/>
public Matrix<Complex> OuterProduct(DenseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

47
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -794,53 +794,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// 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 Matrix<Complex> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new SparseMatrix(u.Count, v.Count);
for (var i = 0; i < u._storage.ValueCount; i++)
{
for (var j = 0; j < v._storage.ValueCount; j++)
{
if (u._storage.Indices[i] == v._storage.Indices[j])
{
matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
}
}
}
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
public Matrix<Complex> OuterProduct(SparseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

47
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -662,53 +662,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// 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 OuterProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(0, u.Count, (a, b) =>
{
for (int i = a; i < b; i++)
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u._values[i]*v._values[j]);
}
}
});
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct(DenseVector, DenseVector)"/>
public Matrix<Complex32> OuterProduct(DenseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

47
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -789,53 +789,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// 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 Matrix<Complex32> /*SparseMatrix*/ OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new SparseMatrix(u.Count, v.Count);
for (var i = 0; i < u._storage.ValueCount; i++)
{
for (var j = 0; j < v._storage.ValueCount; j++)
{
if (u._storage.Indices[i] == v._storage.Indices[j])
{
matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
}
}
}
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
public Matrix<Complex32> OuterProduct(SparseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

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

@ -744,53 +744,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// 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 OuterProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(0, u.Count, (a, b) =>
{
for (int i = a; i < b; i++)
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u._values[i]*v._values[j]);
}
}
});
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct(DenseVector, DenseVector)"/>
public Matrix<double> OuterProduct(DenseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

44
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -836,50 +836,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// 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 Matrix<double> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new SparseMatrix(u.Count, v.Count);
for (var i = 0; i < u._storage.ValueCount; i++)
{
for (var j = 0; j < v._storage.ValueCount; j++)
{
matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
}
}
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
public Matrix<double> OuterProduct(SparseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

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

@ -734,53 +734,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// 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 OuterProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(0, u.Count, (a, b) =>
{
for (int i = a; i < b; i++)
{
for (var j = 0; j < v.Count; j++)
{
matrix.At(i, j, u._values[i]*v._values[j]);
}
}
});
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct(DenseVector, DenseVector)"/>
public Matrix<float> OuterProduct(DenseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

47
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -837,53 +837,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// 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 Matrix<float> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
var matrix = new SparseMatrix(u.Count, v.Count);
for (var i = 0; i < u._storage.ValueCount; i++)
{
for (var j = 0; j < v._storage.ValueCount; j++)
{
if (u._storage.Indices[i] == v._storage.Indices[j])
{
matrix.At(i, j, u._storage.Values[i] * v._storage.Values[j]);
}
}
}
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
public Matrix<float> OuterProduct(SparseVector v)
{
return OuterProduct(this, v);
}
#region Parse Functions
/// <summary>

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

@ -117,6 +117,21 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The sum of conj(a[i])*b[i] for all i.</returns>
protected abstract T DoConjugateDotProduct(Vector<T> other);
/// <summary>
/// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector and stores the result in the result matrix.
/// </summary>
/// <param name="other">The other vector</param>
/// <param name="result">The matrix to store the result of the product.</param>
protected void DoOuterProduct(Vector<T> other, Matrix<T> result)
{
var work = Build.Dense(Count);
for (var i = 0; i < Count; i++)
{
DoMultiply(other.At(i), work);
result.SetColumn(i, work);
}
}
/// <summary>
/// Divides each element of the vector by a scalar and stores the result in the result vector.
/// </summary>
@ -940,33 +955,34 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// Outer product of two vectors
/// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector.
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
public static Matrix<T> OuterProduct(Vector<T> u, Vector<T> v)
/// <param name="other">The other vector</param>
public Matrix<T> OuterProduct(Vector<T> other)
{
var matrix = Matrix<T>.Build.SameAs(u, u.Count, v.Count);
for (var i = 0; i < u.Count; i++)
{
matrix.SetRow(i, v.Multiply(u.At(i)));
}
var matrix = Matrix<T>.Build.SameAs(this, Count, other.Count);
DoOuterProduct(other, matrix);
return matrix;
}
/// <summary>
/// Outer product of this and another vector.
/// Computes the outer product M[i,j] = u[i]*v[j] of this and another vector and stores the result in the result matrix.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="OuterProduct(Vector{T}, Vector{T})"/>
public Matrix<T> OuterProduct(Vector<T> v)
/// <param name="other">The other vector</param>
/// <param name="result">The matrix to store the result of the product.</param>
public void OuterProduct(Vector<T> other, Matrix<T> result)
{
if (Count != result.RowCount || other.Count != result.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
DoOuterProduct(other, result);
}
public static Matrix<T> OuterProduct(Vector<T> u, Vector<T> v)
{
return OuterProduct(this, v);
return u.OuterProduct(v);
}
/// <summary>

Loading…
Cancel
Save