Example #1
0
        public static void RowPermute <T>(Narray <T> data, Narray <int> permutation)
        {
            CHECK_ARG(data.Dim(0) == permutation.Length(), "data.Dim(0) == permutation.Length()");
            Narray <bool> finished = new Narray <bool>(data.Dim(0));

            finished.Fill(false);
            for (int start = 0; start < finished.Length(); start++)
            {
                if (finished[start])
                {
                    continue;
                }
                int        index = start;
                Narray <T> value = new Narray <T>();
                RowCopy(value, data, index);
                for ( ; ;)
                {
                    int next = permutation[index];
                    if (next == start)
                    {
                        break;
                    }
                    RowCopy(data, index, next);
                    index = next;
                    CHECK_ARG(!finished[index], "!finished[index]");
                    finished[index] = true;
                    index           = next;
                }
                RowCopy(data, index, value);
                finished[index] = true;
            }
        }
Example #2
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;
            }
        }
Example #3
0
 public static void Fill <S>(Narray <T> a, S value)
 {
     a.Fill(value);
 }