diff --git a/src/Numerics/Combinatorics.cs b/src/Numerics/Combinatorics.cs index 63e66c9d..fe0503d8 100644 --- a/src/Numerics/Combinatorics.cs +++ b/src/Numerics/Combinatorics.cs @@ -146,6 +146,20 @@ namespace MathNet.Numerics return indices; } + /// + /// Inverts a permutation, returning a new permutation. + /// No checks are performed to validate the input (apart from null checks). + /// + /// Permutation to invert. + /// The inverted permutation. + public static int[] InvertPermutation(int[] permutation) + { + if (permutation == null) throw new ArgumentNullException(nameof(permutation)); + var result = Enumerable.Range(0, permutation.Length).ToArray(); + Sorting.Sort(permutation, result); + return result; + } + /// /// Select a random permutation, without repetition, from a data array by reordering the provided array in-place. /// Implemented using Fisher-Yates Shuffling. The provided data array will be modified.