Browse Source

combinatorics: permutations

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
la-knuth
Christoph Ruegg 17 years ago
parent
commit
2fe6a9efcb
  1. 18
      src/Numerics/Combinatorics.cs
  2. 14
      src/UnitTests/CombinatoricsTests/CombinatoricsCountingTest.cs

18
src/Numerics/Combinatorics.cs

@ -36,7 +36,7 @@ namespace MathNet.Numerics
public static class Combinatorics public static class Combinatorics
{ {
/// <summary> /// <summary>
/// Computes the number of variations without repetition. /// Counts the number of possible variations without repetition.
/// The order matters and each object can be chosen only once. /// The order matters and each object can be chosen only once.
/// </summary> /// </summary>
/// <param name="n">Number of elements in the set.</param> /// <param name="n">Number of elements in the set.</param>
@ -56,7 +56,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// Computes the number of variations with repetition. /// Counts the number of possible variations with repetition.
/// The order matters and each object can be chosen more than once. /// The order matters and each object can be chosen more than once.
/// </summary> /// </summary>
/// <param name="n">Number of elements in the set.</param> /// <param name="n">Number of elements in the set.</param>
@ -73,7 +73,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// Computes the number of combinations without repetition. /// Counts the number of possible combinations without repetition.
/// The order does not matter and each object can be chosen only once. /// The order does not matter and each object can be chosen only once.
/// </summary> /// </summary>
/// <param name="n">Number of elements in the set.</param> /// <param name="n">Number of elements in the set.</param>
@ -85,7 +85,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// Computes the number of combinations with repetition. /// Counts the number of possible combinations with repetition.
/// The order does not matter and an object can be chosen more than once. /// The order does not matter and an object can be chosen more than once.
/// </summary> /// </summary>
/// <param name="n">Number of elements in the set.</param> /// <param name="n">Number of elements in the set.</param>
@ -109,5 +109,15 @@ namespace MathNet.Numerics
- SpecialFunctions.FactorialLn(k) - SpecialFunctions.FactorialLn(k)
- SpecialFunctions.FactorialLn(n - 1))); - SpecialFunctions.FactorialLn(n - 1)));
} }
/// <summary>
/// Counts the number of possible permutations (without repetition).
/// </summary>
/// <param name="n">Number of (distinguishable) elements in the set.</param>
/// <returns>Maximum number of permutations without repetition.</returns>
public static double Permutations(int n)
{
return SpecialFunctions.Factorial(n);
}
} }
} }

14
src/UnitTests/CombinatoricsTests/CombinatoricsCountingTest.cs

@ -160,5 +160,19 @@ namespace MathNet.Numerics.UnitTests.CombinatoricsTests
Combinatorics.CombinationsWithRepetition(n, k), Combinatorics.CombinationsWithRepetition(n, k),
"The number of combinations with repetition but out of the range must be 0."); "The number of combinations with repetition but out of the range must be 0.");
} }
[Test]
[Row(0, 1)]
[Row(1, 1)]
[Row(2, 2)]
[Row(8, 40320)]
[Row(15, 1307674368000)]
public void CanCountPermutations(int n, long expected)
{
Assert.AreEqual(
expected,
Combinatorics.Permutations(n),
"Count the number of permutations");
}
} }
} }
Loading…
Cancel
Save