Exemple #1
0
        /// <summary>
        /// Attempts to convert a text representation into a permutation.
        /// </summary>
        /// <param name="text">A text representation of the permutation.</param>
        /// <param name="output">The corresponding permutation.</param>
        /// <returns>True if the conversion succeeded, otherwise false.</returns>
        /// <remarks>
        /// <para>For information on supported text representations, see <see cref="Parse"/>.</para>
        /// </remarks>
        public static bool TryParse(string text, out Permutation output)
        {
            output = null;

            if (text == null)
            {
                return(false);
            }

            bool success = false;

            if ((text.Length > 0) && (text[0] == '['))
            {
                // try to parse as ordering
                PermutationAsMap map;
                success = PermutationAsMap.TryParse(text, out map);
                if (success)
                {
                    output = new Permutation(map);
                }
            }
            else
            {
                // try to parse as cycles
                PermutationAsCycles cycles;
                success = PermutationAsCycles.TryParse(text, out cycles);
                if (success)
                {
                    output = new Permutation(cycles);
                }
            }

            return(success);
        }
Exemple #2
0
 public static bool TryParse(string text, out PermutationAsMap result)
 {
     result = null;
     if (text.Length < 2)
     {
         return(false);
     }
     if ((text[0] != '[') || (text[text.Length - 1] != ']'))
     {
         return(false);
     }
     string[] mapText = text.Substring(1, text.Length - 2).Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
     int[]    map     = new int[mapText.Length];
     bool[]   flags   = new bool[mapText.Length];
     for (int i = 0; i < map.Length; i++)
     {
         int value;
         if (!Int32.TryParse(mapText[i], out value))
         {
             return(false);
         }
         if ((value < 0) || (value > (map.Length - 1)))
         {
             return(false);
         }
         if (flags[value])
         {
             return(false);
         }
         map[i]       = value;
         flags[value] = true;
     }
     result = new PermutationAsMap(map);
     return(true);
 }
Exemple #3
0
 /// <summary>
 /// Returns the identity permutation of the given dimension.
 /// </summary>
 /// <param name="dimension">The number of elements on which the permutation acts.</param>
 /// <returns>The identity permutation of the requested dimension.</returns>
 public static Permutation Identity(int dimension)
 {
     if (dimension < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(dimension));
     }
     return(new Permutation(PermutationAsMap.Identity(dimension)));
 }
Exemple #4
0
 /// <summary>
 /// Get a random permutation.
 /// </summary>
 /// <param name="dimension">The number of elements on which the permutation acts.</param>
 /// <param name="rng">A random number generator.</param>
 /// <returns>A random permutation of the specified dimension. All permutations of the specified dimension are equally likely.</returns>
 public static Permutation GetRandomPermutation(int dimension, Random rng)
 {
     if (dimension < 1)
     {
         throw new ArgumentOutOfRangeException(nameof(dimension));
     }
     if (rng == null)
     {
         throw new ArgumentNullException(nameof(rng));
     }
     return(new Permutation(PermutationAsMap.GetRandomPermutation(dimension, rng)));
 }
Exemple #5
0
 private void ComputeMap()
 {
     Debug.Assert(cycles != null);
     map = new PermutationAsMap(FromCyclesToMap(cycles.cycles));
 }
Exemple #6
0
 internal Permutation(PermutationAsMap map)
 {
     this.map = map;
 }