ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// <copyright file="Permutation.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
 
namespace IStation.Numerics
{
    /// <summary>
    /// Class to represent a permutation for a subset of the natural numbers.
    /// </summary>
    [Serializable]
    public class Permutation
    {
        #region fields
 
        /// <summary>
        /// Entry _indices[i] represents the location to which i is permuted to.
        /// </summary>
        private readonly int[] _indices;
 
        #endregion fields
 
        #region Constructor
 
        /// <summary>
        /// Initializes a new instance of the Permutation class.
        /// </summary>
        /// <param name="indices">An array which represents where each integer is permuted too: indices[i] represents that integer i
        /// is permuted to location indices[i].</param>
        public Permutation(int[] indices)
        {
            if (!CheckForProperPermutation(indices))
            {
                throw new ArgumentException("The integer array does not represent a valid permutation.", nameof(indices));
            }
 
            _indices = (int[])indices.Clone();
        }
 
        #endregion
 
        /// <summary>
        /// Gets the number of elements this permutation is over.
        /// </summary>
        public int Dimension => _indices.Length;
 
        /// <summary>
        /// Computes where <paramref name="idx"/> permutes too.
        /// </summary>
        /// <param name="idx">The index to permute from.</param>
        /// <returns>The index which is permuted to.</returns>
        public int this[int idx] => _indices[idx];
 
        /// <summary>
        /// Computes the inverse of the permutation.
        /// </summary>
        /// <returns>The inverse of the permutation.</returns>
        public Permutation Inverse()
        {
            var invIdx = new int[Dimension];
            for (int i = 0; i < invIdx.Length; i++)
            {
                invIdx[_indices[i]] = i;
            }
 
            return new Permutation(invIdx);
        }
 
        /// <summary>
        /// Construct an array from a sequence of inversions.
        /// </summary>
        /// <example>
        /// From wikipedia: the permutation 12043 has the inversions (0,2), (1,2) and (3,4). This would be
        /// encoded using the array [22244].
        /// </example>
        /// <param name="inv">The set of inversions to construct the permutation from.</param>
        /// <returns>A permutation generated from a sequence of inversions.</returns>
        public static Permutation FromInversions(int[] inv)
        {
            var idx = new int[inv.Length];
            for (int i = 0; i < inv.Length; i++)
            {
                idx[i] = i;
            }
 
            for (int i = inv.Length - 1; i >= 0; i--)
            {
                if (idx[i] != inv[i])
                {
                    int t = idx[i];
                    idx[i] = idx[inv[i]];
                    idx[inv[i]] = t;
                }
            }
 
            return new Permutation(idx);
        }
 
        /// <summary>
        /// Construct a sequence of inversions from the permutation.
        /// </summary>
        /// <example>
        /// From wikipedia: the permutation 12043 has the inversions (0,2), (1,2) and (3,4). This would be
        /// encoded using the array [22244].
        /// </example>
        /// <returns>A sequence of inversions.</returns>
        public int[] ToInversions()
        {
            var idx = (int[])_indices.Clone();
 
            for (int i = 0; i < idx.Length; i++)
            {
                if (idx[i] != i)
                {
                    int q = Array.FindIndex(idx, i + 1, x => x == i);
                    var t = idx[i];
                    idx[i] = q;
                    idx[q] = t;
                }
            }
 
            return idx;
        }
 
        /// <summary>
        /// Checks whether the <paramref name="indices"/> array represents a proper permutation.
        /// </summary>
        /// <param name="indices">An array which represents where each integer is permuted too: indices[i] represents that integer i
        /// is permuted to location indices[i].</param>
        /// <returns>True if <paramref name="indices"/> represents a proper permutation, <c>false</c> otherwise.</returns>
        static bool CheckForProperPermutation(int[] indices)
        {
            var idxCheck = new bool[indices.Length];
 
            for (int i = 0; i < indices.Length; i++)
            {
                if (indices[i] >= indices.Length || indices[i] < 0)
                {
                    return false;
                }
 
                idxCheck[indices[i]] = true;
            }
 
            for (int i = 0; i < indices.Length; i++)
            {
                if (idxCheck[i] == false)
                {
                    return false;
                }
            }
 
            return true;
        }
    }
}