Browse Source

LA: static CreateRandom for dense matrix and vector (replacing complicated templating approach)

la-knuth
Christoph Ruegg 14 years ago
parent
commit
211da24362
  1. 14
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 14
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  3. 33
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  4. 52
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  5. 14
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  6. 65
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  7. 33
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  8. 52
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  9. 14
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  10. 14
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  11. 36
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  12. 52
      src/Numerics/LinearAlgebra/Double/Vector.cs
  13. 70
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  14. 24
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  15. 14
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  16. 14
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  17. 33
      src/Numerics/LinearAlgebra/Single/Matrix.cs
  18. 52
      src/Numerics/LinearAlgebra/Single/Vector.cs
  19. 4
      src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs
  20. 4
      src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs
  21. 4
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs
  22. 4
      src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs
  23. 4
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs
  24. 4
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs
  25. 4
      src/UnitTests/LinearAlgebraTests/Single/MatrixTests.Arithmetic.cs
  26. 4
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

14
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -29,6 +29,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
using System; using System;
using System.Numerics; using System.Numerics;
using Algorithms.LinearAlgebra; using Algorithms.LinearAlgebra;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage; using Storage;
@ -152,6 +153,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyTo(Storage, skipClearing: true);
} }
/// <summary>
/// Create a new dense matrix with values sampled from the provided random distribution.
/// </summary>
public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution)
{
var storage = new DenseColumnMajorMatrixStorage<Complex>(rows, columns);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = new Complex(distribution.Sample(), distribution.Sample());
}
return new DenseMatrix(storage);
}
/// <summary> /// <summary>
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>

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

@ -30,6 +30,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using Distributions;
using Generic; using Generic;
using NumberTheory; using NumberTheory;
using Properties; using Properties;
@ -119,6 +120,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
} }
/// <summary>
/// Create a new dense vector with values sampled from the provided random distribution.
/// </summary>
public static DenseVector CreateRandom(int size, IContinuousDistribution distribution)
{
var storage = new DenseVectorStorage<Complex>(size);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = new Complex(distribution.Sample(), distribution.Sample());
}
return new DenseVector(storage);
}
/// <summary> /// <summary>
/// Gets the vector's data. /// Gets the vector's data.
/// </summary> /// </summary>

33
src/Numerics/LinearAlgebra/Complex/Matrix.cs

@ -28,7 +28,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{ {
using System; using System;
using System.Numerics; using System.Numerics;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage; using Storage;
@ -370,37 +369,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return sum; return sum;
} }
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IContinuousDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex> matrix, IDiscreteDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
} }
} }

52
src/Numerics/LinearAlgebra/Complex/Vector.cs

@ -357,58 +357,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return result; return result;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is non-positive.</exception>
public override Vector<Complex> Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var vector = CreateVector(length);
for (var index = 0; index < length; index++)
{
vector.At(index, new Complex(randomDistribution.Sample(), randomDistribution.Sample()));
}
return vector;
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is not positive.</exception>
public override Vector<Complex> Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var vector = CreateVector(length);
for (var index = 0; index < length; index++)
{
vector.At(index, new Complex(randomDistribution.Sample(), randomDistribution.Sample()));
}
return vector;
}
/// <summary> /// <summary>
/// Returns the index of the absolute maximum element. /// Returns the index of the absolute maximum element.
/// </summary> /// </summary>

14
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -28,6 +28,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
using System; using System;
using Algorithms.LinearAlgebra; using Algorithms.LinearAlgebra;
using Distributions;
using Generic; using Generic;
using Numerics; using Numerics;
using Properties; using Properties;
@ -152,6 +153,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyTo(Storage, skipClearing: true);
} }
/// <summary>
/// Create a new dense matrix with values sampled from the provided random distribution.
/// </summary>
public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution)
{
var storage = new DenseColumnMajorMatrixStorage<Complex32>(rows, columns);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = new Complex32((float)distribution.Sample(), (float)distribution.Sample());
}
return new DenseMatrix(storage);
}
/// <summary> /// <summary>
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>

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

@ -120,6 +120,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
} }
/// <summary>
/// Create a new dense vector with values sampled from the provided random distribution.
/// </summary>
public static DenseVector CreateRandom(int size, IContinuousDistribution distribution)
{
var storage = new DenseVectorStorage<Complex32>(size);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = new Complex32((float)distribution.Sample(), (float)distribution.Sample());
}
return new DenseVector(storage);
}
/// <summary> /// <summary>
/// Gets the vector's data. /// Gets the vector's data.
/// </summary> /// </summary>
@ -753,58 +766,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return matrix; return matrix;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<Complex32> Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v._values.Length; index++)
{
v._values[index] = new Complex32((float)randomDistribution.Sample(), (float)randomDistribution.Sample());
}
return v;
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<Complex32> Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v._values.Length; index++)
{
v._values[index] = new Complex32(randomDistribution.Sample(), randomDistribution.Sample());
}
return v;
}
/// <summary> /// <summary>
/// Outer product of this and another vector. /// Outer product of this and another vector.
/// </summary> /// </summary>

33
src/Numerics/LinearAlgebra/Complex32/Matrix.cs

@ -27,7 +27,6 @@
namespace MathNet.Numerics.LinearAlgebra.Complex32 namespace MathNet.Numerics.LinearAlgebra.Complex32
{ {
using System; using System;
using Distributions;
using Generic; using Generic;
using Numerics; using Numerics;
using Properties; using Properties;
@ -364,37 +363,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return sum; return sum;
} }
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IContinuousDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
}
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<Complex32> matrix, IDiscreteDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
} }
} }

52
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -402,57 +402,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return clone; return clone;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is non-positive.</exception>
public override Vector<Complex32> Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var vector = CreateVector(length);
for (var index = 0; index < length; index++)
{
vector.At(index, new Complex32(Convert.ToSingle(randomDistribution.Sample()), Convert.ToSingle(randomDistribution.Sample())));
}
return vector;
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is not positive.</exception>
public override Vector<Complex32> Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var vector = CreateVector(length);
for (var index = 0; index < length; index++)
{
vector.At(index, new Complex32(Convert.ToSingle(randomDistribution.Sample()), Convert.ToSingle(randomDistribution.Sample())));
}
return vector;
}
} }
} }

14
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -28,6 +28,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
using System; using System;
using Algorithms.LinearAlgebra; using Algorithms.LinearAlgebra;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage; using Storage;
@ -152,6 +153,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyTo(Storage, skipClearing: true);
} }
/// <summary>
/// Create a new dense matrix with values sampled from the provided random distribution.
/// </summary>
public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution)
{
var storage = new DenseColumnMajorMatrixStorage<double>(rows, columns);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = distribution.Sample();
}
return new DenseMatrix(storage);
}
/// <summary> /// <summary>
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>

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

@ -30,6 +30,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Distributions;
using Generic; using Generic;
using NumberTheory; using NumberTheory;
using Properties; using Properties;
@ -119,6 +120,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
} }
/// <summary>
/// Create a new dense vector with values sampled from the provided random distribution.
/// </summary>
public static DenseVector CreateRandom(int size, IContinuousDistribution distribution)
{
var storage = new DenseVectorStorage<double>(size);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = distribution.Sample();
}
return new DenseVector(storage);
}
/// <summary> /// <summary>
/// Gets the vector's data. /// Gets the vector's data.
/// </summary> /// </summary>

36
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -24,14 +24,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.LinearAlgebra.Storage;
namespace MathNet.Numerics.LinearAlgebra.Double namespace MathNet.Numerics.LinearAlgebra.Double
{ {
using System; using System;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage;
/// <summary> /// <summary>
/// <c>double</c> version of the <see cref="Matrix{T}"/> class. /// <c>double</c> version of the <see cref="Matrix{T}"/> class.
@ -361,37 +359,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return sum; return sum;
} }
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IContinuousDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<double> matrix, IDiscreteDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
} }
} }

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

@ -430,57 +430,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return clone; return clone;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<double> Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = CreateVector(length);
for (var index = 0; index < Count; index++)
{
v.At(index, randomDistribution.Sample());
}
return v;
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<double> Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = CreateVector(length);
for (var index = 0; index < Count; index++)
{
At(index, randomDistribution.Sample());
}
return v;
}
} }
} }

70
src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs

@ -1102,76 +1102,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.Modulus(rightSide); return leftSide.Modulus(rightSide);
} }
/// <summary>
/// Generates a matrix with random elements.
/// </summary>
/// <param name="numberOfRows">Number of rows.</param>
/// <param name="numberOfColumns">Number of columns.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
/// <returns>
/// An <c>numberOfRows</c>-by-<c>numberOfColumns</c> matrix with elements distributed according to the provided distribution.
/// </returns>
/// <exception cref="ArgumentException">If the parameter <paramref name="numberOfRows"/> is not positive.</exception>
/// <exception cref="ArgumentException">If the parameter <paramref name="numberOfColumns"/> is not positive.</exception>
public virtual Matrix<T> Random(int numberOfRows, int numberOfColumns, IContinuousDistribution distribution)
{
if (numberOfRows < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfRows");
}
if (numberOfColumns < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns");
}
var matrix = CreateMatrix(numberOfRows, numberOfColumns);
DoRandom(matrix, distribution);
return matrix;
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected abstract void DoRandom(Matrix<T> matrix, IContinuousDistribution distribution);
/// <summary>
/// Generates a matrix with random elements.
/// </summary>
/// <param name="numberOfRows">Number of rows.</param>
/// <param name="numberOfColumns">Number of columns.</param>
/// <param name="distribution">Continuous Random Distribution or Source</param>
/// <returns>
/// An <c>numberOfRows</c>-by-<c>numberOfColumns</c> matrix with elements distributed according to the provided distribution.
/// </returns>
/// <exception cref="ArgumentException">If the parameter <paramref name="numberOfRows"/> is not positive.</exception>
/// <exception cref="ArgumentException">If the parameter <paramref name="numberOfColumns"/> is not positive.</exception>
public virtual Matrix<T> Random(int numberOfRows, int numberOfColumns, IDiscreteDistribution distribution)
{
if (numberOfRows < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfRows");
}
if (numberOfColumns < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns");
}
var matrix = CreateMatrix(numberOfRows, numberOfColumns);
DoRandom(matrix, distribution);
return matrix;
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected abstract void DoRandom(Matrix<T> matrix, IDiscreteDistribution distribution);
/// <summary> /// <summary>
/// Computes the trace of this matrix. /// Computes the trace of this matrix.
/// </summary> /// </summary>

24
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -862,30 +862,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return matrix; return matrix;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is non-positive.</exception>
public abstract Vector<T> Random(int length, IContinuousDistribution randomDistribution);
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentException">If the n vector is not positive.</exception>
public abstract Vector<T> Random(int length, IDiscreteDistribution randomDistribution);
/// <summary> /// <summary>
/// Outer product of this and another vector. /// Outer product of this and another vector.
/// </summary> /// </summary>

14
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -28,6 +28,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
using System; using System;
using Algorithms.LinearAlgebra; using Algorithms.LinearAlgebra;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage; using Storage;
@ -152,6 +153,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
matrix.Storage.CopyTo(Storage, skipClearing: true); matrix.Storage.CopyTo(Storage, skipClearing: true);
} }
/// <summary>
/// Create a new dense matrix with values sampled from the provided random distribution.
/// </summary>
public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution)
{
var storage = new DenseColumnMajorMatrixStorage<float>(rows, columns);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = (float)distribution.Sample();
}
return new DenseMatrix(storage);
}
/// <summary> /// <summary>
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>

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

@ -30,6 +30,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Distributions;
using Generic; using Generic;
using NumberTheory; using NumberTheory;
using Properties; using Properties;
@ -119,6 +120,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{ {
} }
/// <summary>
/// Create a new dense vector with values sampled from the provided random distribution.
/// </summary>
public static DenseVector CreateRandom(int size, IContinuousDistribution distribution)
{
var storage = new DenseVectorStorage<float>(size);
for (var i = 0; i < storage.Data.Length; i++)
{
storage.Data[i] = (float)distribution.Sample();
}
return new DenseVector(storage);
}
/// <summary> /// <summary>
/// Gets the vector's data. /// Gets the vector's data.
/// </summary> /// </summary>

33
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -27,7 +27,6 @@
namespace MathNet.Numerics.LinearAlgebra.Single namespace MathNet.Numerics.LinearAlgebra.Single
{ {
using System; using System;
using Distributions;
using Generic; using Generic;
using Properties; using Properties;
using Storage; using Storage;
@ -360,37 +359,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return sum; return sum;
} }
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IContinuousDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, Convert.ToSingle(distribution.Sample()));
}
}
}
/// <summary>
/// Populates a matrix with random elements.
/// </summary>
/// <param name="matrix">The matrix to populate.</param>
/// <param name="distribution">Continuous Random Distribution to generate elements from.</param>
protected override void DoRandom(Matrix<float> matrix, IDiscreteDistribution distribution)
{
for (var i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
{
matrix.At(i, j, distribution.Sample());
}
}
}
} }
} }

52
src/Numerics/LinearAlgebra/Single/Vector.cs

@ -430,57 +430,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return clone; return clone;
} }
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<float> Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = CreateVector(length);
for (var index = 0; index < Count; index++)
{
v.At(index, Convert.ToSingle(randomDistribution.Sample()));
}
return v;
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// A vector with n-random elements distributed according
/// to the specified random distribution.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector<float> Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
var v = CreateVector(length);
for (var index = 0; index < Count; index++)
{
v.At(index, Convert.ToSingle(randomDistribution.Sample()));
}
return v;
}
} }
} }

4
src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs

@ -1212,9 +1212,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[TestCase(-2)] [TestCase(-2)]
public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows) public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows)
{ {
var matrix = CreateMatrix(2, 3); Assert.Throws<ArgumentOutOfRangeException>(() => DenseMatrix.CreateRandom(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new DiscreteUniform(0, 2)));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs

@ -33,6 +33,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using Distributions; using Distributions;
using LinearAlgebra.Complex;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using NUnit.Framework; using NUnit.Framework;
@ -475,8 +476,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
[Test] [Test]
public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException() public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException()
{ {
var vector = CreateVector(4); Assert.Throws<ArgumentOutOfRangeException>(() => DenseVector.CreateRandom(-2, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => vector.Random(-2, new ContinuousUniform()));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs

@ -1212,9 +1212,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[TestCase(-2)] [TestCase(-2)]
public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows) public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows)
{ {
var matrix = CreateMatrix(2, 3); Assert.Throws<ArgumentOutOfRangeException>(() => DenseMatrix.CreateRandom(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new DiscreteUniform(0, 2)));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs

@ -32,6 +32,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Distributions; using Distributions;
using LinearAlgebra.Complex32;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using NUnit.Framework; using NUnit.Framework;
using Complex32 = Numerics.Complex32; using Complex32 = Numerics.Complex32;
@ -475,8 +476,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
[Test] [Test]
public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException() public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException()
{ {
var vector = CreateVector(4); Assert.Throws<ArgumentOutOfRangeException>(() => DenseVector.CreateRandom(-2, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => vector.Random(-2, new ContinuousUniform()));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs

@ -1207,9 +1207,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestCase(-2)] [TestCase(-2)]
public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows) public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows)
{ {
var matrix = CreateMatrix(2, 3); Assert.Throws<ArgumentOutOfRangeException>(() => DenseMatrix.CreateRandom(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new DiscreteUniform(0, 2)));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -31,6 +31,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using Distributions; using Distributions;
using LinearAlgebra.Double;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using NUnit.Framework; using NUnit.Framework;
@ -489,8 +490,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException() public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException()
{ {
var vector = CreateVector(4); Assert.Throws<ArgumentOutOfRangeException>(() => DenseVector.CreateRandom(-2, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => vector.Random(-2, new ContinuousUniform()));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Single/MatrixTests.Arithmetic.cs

@ -1201,9 +1201,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[TestCase(-2)] [TestCase(-2)]
public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows) public void RandomWithNonPositiveNumberOfRowsThrowsArgumentException(int numberOfRows)
{ {
var matrix = CreateMatrix(2, 3); Assert.Throws<ArgumentOutOfRangeException>(() => DenseMatrix.CreateRandom(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => matrix.Random(numberOfRows, 4, new DiscreteUniform(0, 2)));
} }
/// <summary> /// <summary>

4
src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

@ -32,6 +32,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
using System.Globalization; using System.Globalization;
using Distributions; using Distributions;
using LinearAlgebra.Generic; using LinearAlgebra.Generic;
using LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
/// <summary> /// <summary>
@ -489,8 +490,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
[Test] [Test]
public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException() public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException()
{ {
var vector = CreateVector(4); Assert.Throws<ArgumentOutOfRangeException>(() => DenseVector.CreateRandom(-2, new ContinuousUniform()));
Assert.Throws<ArgumentException>(() => vector.Random(-2, new ContinuousUniform()));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save