From f7b06e2955492e590ff47decace92fed93b2a527 Mon Sep 17 00:00:00 2001 From: Daniel Sword Date: Mon, 19 Nov 2018 19:59:37 -0800 Subject: [PATCH] Combinatorics: add a method to invert a permutation. --- src/Numerics/Combinatorics.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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.