|
|
|
@ -135,7 +135,7 @@ namespace MathNet.Numerics |
|
|
|
/// <param name="randomSource">The random number generator to use. Optional; the default random source will be used if null.</param>
|
|
|
|
public static int[] GeneratePermutation(int n, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
int[] indices = new int[n]; |
|
|
|
for (int i = 0; i < indices.Length; i++) |
|
|
|
@ -195,7 +195,7 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>Boolean mask array of length <c>N</c>, for each item true if it is selected.</returns>
|
|
|
|
public static bool[] GenerateCombination(int n, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
|
|
|
|
@ -217,9 +217,9 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>Boolean mask array of length <c>N</c>, for each item true if it is selected.</returns>
|
|
|
|
public static bool[] GenerateCombination(int n, int k, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", k, Resources.ArgumentNotNegative); |
|
|
|
if (k > n) throw new ArgumentOutOfRangeException("k", k, string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "k", "n")); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", Resources.ArgumentNotNegative); |
|
|
|
if (k > n) throw new ArgumentOutOfRangeException("k", string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "k", "n")); |
|
|
|
|
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
|
|
|
|
@ -262,8 +262,8 @@ namespace MathNet.Numerics |
|
|
|
{ |
|
|
|
T[] array = data as T[] ?? data.ToArray(); |
|
|
|
|
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose > array.Length) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "elementsToChoose", "data.Count")); |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose > array.Length) throw new ArgumentOutOfRangeException("elementsToChoose", string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "elementsToChoose", "data.Count")); |
|
|
|
|
|
|
|
bool[] mask = GenerateCombination(array.Length, elementsToChoose, randomSource); |
|
|
|
|
|
|
|
@ -285,8 +285,8 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>Integer mask array of length <c>N</c>, for each item the number of times it was selected.</returns>
|
|
|
|
public static int[] GenerateCombinationWithRepetition(int n, int k, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", k, Resources.ArgumentNotNegative); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
|
|
|
|
@ -308,7 +308,7 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>The chosen combination with repetition, in the original order.</returns>
|
|
|
|
public static IEnumerable<T> SelectCombinationWithRepetition<T>(this IEnumerable<T> data, int elementsToChoose, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
T[] array = data as T[] ?? data.ToArray(); |
|
|
|
int[] mask = GenerateCombinationWithRepetition(array.Length, elementsToChoose, randomSource); |
|
|
|
@ -332,9 +332,9 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>An array of length <c>K</c> that contains the indices of the selections as integers of the interval <c>[0, N)</c>.</returns>
|
|
|
|
public static int[] GenerateVariation(int n, int k, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", k, Resources.ArgumentNotNegative); |
|
|
|
if (k > n) throw new ArgumentOutOfRangeException("k", k, string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "k", "n")); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", Resources.ArgumentNotNegative); |
|
|
|
if (k > n) throw new ArgumentOutOfRangeException("k", string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "k", "n")); |
|
|
|
|
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
|
|
|
|
@ -369,8 +369,8 @@ namespace MathNet.Numerics |
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
T[] array = data.ToArray(); |
|
|
|
|
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose > array.Length) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "elementsToChoose", "data.Count")); |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose > array.Length) throw new ArgumentOutOfRangeException("elementsToChoose", string.Format(Resources.ArgumentOutOfRangeSmallerEqual, "elementsToChoose", "data.Count")); |
|
|
|
|
|
|
|
// Partial Fisher-Yates Shuffling
|
|
|
|
for (int i = array.Length - 1; i >= array.Length - elementsToChoose; i--) |
|
|
|
@ -390,8 +390,8 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>An array of length <c>K</c> that contains the indices of the selections as integers of the interval <c>[0, N)</c>.</returns>
|
|
|
|
public static int[] GenerateVariationWithRepetition(int n, int k, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", n, Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", k, Resources.ArgumentNotNegative); |
|
|
|
if (n < 0) throw new ArgumentOutOfRangeException("n", Resources.ArgumentNotNegative); |
|
|
|
if (k < 0) throw new ArgumentOutOfRangeException("k", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
var random = randomSource ?? SystemRandomSource.Default; |
|
|
|
|
|
|
|
@ -409,7 +409,7 @@ namespace MathNet.Numerics |
|
|
|
/// <returns>The chosen variation with repetition, in random order.</returns>
|
|
|
|
public static IEnumerable<T> SelectVariationWithRepetition<T>(this IEnumerable<T> data, int elementsToChoose, System.Random randomSource = null) |
|
|
|
{ |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", elementsToChoose, Resources.ArgumentNotNegative); |
|
|
|
if (elementsToChoose < 0) throw new ArgumentOutOfRangeException("elementsToChoose", Resources.ArgumentNotNegative); |
|
|
|
|
|
|
|
T[] array = data as T[] ?? data.ToArray(); |
|
|
|
int[] indices = GenerateVariationWithRepetition(array.Length, elementsToChoose, randomSource); |
|
|
|
|