コード例 #1
0
ファイル: NarrayRowUtil.cs プロジェクト: liaoheping/OCRonet
        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;
            }
        }
コード例 #2
0
ファイル: NarrayRowUtil.cs プロジェクト: liaoheping/OCRonet
 public static void RowGet <T, S>(Narray <T> outv, Narray <S> data, int row)
 {
     outv.Resize(data.Dim(1));
     for (int i = 0; i < outv.Length(); i++)
     {
         outv[i] = (T)Convert.ChangeType(data[row, i], typeof(T));
     }
 }
コード例 #3
0
 /// <summary>
 /// Reverse an array
 /// </summary>
 public static void Reverse <T>(Narray <T> outa, Narray <T> ina)
 {
     outa.Clear();
     for (int i = ina.Length() - 1; i >= 0; i--)
     {
         outa.Push(ina[i]);
     }
 }
コード例 #4
0
ファイル: NarrayRowUtil.cs プロジェクト: liaoheping/OCRonet
 public static void RowCopy <T>(Narray <T> a, int i, Narray <T> b)
 {
     CHECK_ARG(a.Dim(1) == b.Length(), "a.Dim(1) == b.Length()");
     for (int k = 0; k < a.Dim(1); k++)
     {
         a[i, k] = b[k];
     }
 }
コード例 #5
0
ファイル: NarrayRowUtil.cs プロジェクト: liaoheping/OCRonet
        public static void RowPush <T>(Narray <T> table, Narray <T> data)
        {
            if (table.Length1d() == 0)
            {
                table.Copy(data);
                table.Reshape(1, table.Length());
                return;
            }
            CHECK_ARG(table.Dim(1) == data.Length(), "table.Dim(1) == data.Length()");
            table.Reserve(table.Length1d() + data.Length());
            table.SetDims(table.Dim(0) + 1, table.Dim(1), 0, 0);
            int irow = table.Dim(0) - 1;

            for (int k = 0; k < table.Dim(1); k++)
            {
                table[irow, k] = data.UnsafeAt1d(k);
            }
        }
コード例 #6
0
 /// <summary>
 /// Quicksort an array, generating a permutation of the indexes.
 /// </summary>
 public static void Quicksort <T>(Narray <int> outindex, Narray <T> values)
 {
     outindex.Resize(values.Length());
     for (int i = 0; i < values.Length(); i++)
     {
         outindex[i] = i;
     }
     Quicksort(outindex, values, 0, outindex.Length());
 }
コード例 #7
0
        /// <summary>
        /// Find unique elements.
        /// </summary>
        public static void Uniq <T>(Narray <T> values)
        {
            if (values.Length() == 0)
            {
                return;
            }
            Quicksort(values);
            int j = 1;

            for (int i = 1; i < values.Length(); i++)
            {
                if (values[i].Equals(values[j - 1]))
                {
                    continue;
                }
                values[j++] = values[i];
            }
            values.Truncate(j);
        }
コード例 #8
0
 public static int first_index_of(Narray <int> a, int target)
 {
     for (int i = 0; i < a.Length(); i++)
     {
         if (a[i] == target)
         {
             return(i);
         }
     }
     return(-1);
 }
コード例 #9
0
 public static int first_index_of <T>(Narray <T> a, T target)
 {
     for (int i = 0; i < a.Length(); i++)
     {
         if (a[i].Equals(target))
         {
             return(i);
         }
     }
     return(-1);
 }
コード例 #10
0
        /// <summary>
        /// Original name: randomly_permute
        /// </summary>
        public static void RandomlyPermute <T>(Narray <T> v)
        {
            int n = v.Length();

            for (int i = 0; i < n - 1; i++)
            {
                int target = DRandomizer.Default.nrand() % (n - i) + i;
                T   temp   = v[target];
                v[target] = v[i];
                v[i]      = temp;
            }
        }
コード例 #11
0
        public static void Shuffle <T>(Narray <T> values)
        {
            Floatarray temp  = new Floatarray(values.Length());
            Intarray   index = new Intarray();

            for (int i = 0; i < temp.Length(); i++)
            {
                temp.UnsafePut1d(i, DRandomizer.Default.drand());
            }
            Quicksort(index, temp);
            Permute(values, index);
        }
コード例 #12
0
        public static float Median(Narray <int> a)
        {
            Narray <int> s = new Narray <int>();

            s.Copy(a);
            s.Reshape(s.Length1d());
            Quicksort(s);
            int n = s.Length();

            if (n == 0)
            {
                return(0);
            }
            if ((n % 2) > 0)
            {
                return(s[n / 2]);
            }
            else
            {
                return((s[n / 2 - 1] + s[n / 2]) / 2.0f);
            }
        }
コード例 #13
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;
            }
        }
コード例 #14
0
ファイル: ObjList.cs プロジェクト: liaoheping/OCRonet
 public int Length()
 {
     return(data.Length());
 }
コード例 #15
0
 public static void Quicksort <T>(Narray <T> values)
 {
     Quicksort(values, 0, values.Length());
 }