예제 #1
0
        /// <summary>
        /// Permute the elements of an array given a permutation.
        /// </summary>
        public static void Permute <T>(Narray <T> data, Narray <int> permutation)
        {
            if (!data.SameDims(permutation))
            {
                throw new Exception("CHECK_ARG: data.SameDims(permutation)");
            }
            Narray <bool> finished = new Narray <bool>(data.Length());

            finished.Fill(false);
            for (int start = 0; start < finished.Length(); start++)
            {
                if (finished[start])
                {
                    continue;
                }
                int index = start;
                T   value = data[index];
                for ( ; ;)
                {
                    int next = permutation[index];
                    if (next == start)
                    {
                        break;
                    }
                    data[index] = data[next];
                    index       = next;
                    //CHECK_ARG(!finished[index] && "not a permutation");
                    if (finished[index])
                    {
                        throw new Exception("CHECK_ARG: !finished[index]");
                    }
                    finished[index] = true;
                    index           = next;
                }
                data[index]     = value;
                finished[index] = true;
            }
        }
예제 #2
0
 public static bool SameDims <S>(Narray <T> a, Narray <S> b)
 {
     return(a.SameDims(b));
 }