Beispiel #1
0
 public static void RowCopy <T>(Narray <T> values, int i, int j)
 {
     for (int k = 0; k < values.Dim(1); k++)
     {
         values[i, k] = values[j, k];
     }
 }
Beispiel #2
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;
            }
        }
Beispiel #3
0
        public static int RowArgMin <T>(Narray <T> values, int i)
        {
            if (values.Dim(1) < 1)
            {
                return(-1);
            }
            int mj = 0;
            T   mv = values[i, 0];

            for (int j = 1; j < values.Dim(1); j++)
            {
                T value = values[i, j];
                if (value.Equals(mv))
                {
                    continue;
                }
                if (Convert.ToDouble(value) > Convert.ToDouble(mv))
                {
                    continue;
                }
                mv = value;
                mj = j;
            }
            return(mj);
        }
Beispiel #4
0
 public static void RowCopy <T>(Narray <T> a, Narray <T> b, int i)
 {
     a.Resize(b.Dim(1));
     for (int k = 0; k < b.Dim(1); k++)
     {
         a[k] = b[i, k];
     }
 }
Beispiel #5
0
 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];
     }
 }
Beispiel #6
0
 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));
     }
 }
Beispiel #7
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]);
     }
 }
Beispiel #8
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());
 }
Beispiel #9
0
        /// <summary>
        /// Copy the elements of the source array into the destination array,
        /// resizing if necessary.
        /// </summary>
        public void Copy <S>(Narray <S> src)
        {
            this.Resize(src.Dim(0), src.Dim(1), src.Dim(2), src.Dim(3));
            index_t n = this.Length1d();

            for (index_t i = 0; i < n; i++)
            {
                this.UnsafePut1d(i, src.UnsafeAt1d(i));
            }
        }
Beispiel #10
0
 /// <summary>
 /// Swap the contents of the two arrays.
 /// </summary>
 public void Swap(Narray <T> src)
 {
     swap_ <T[]>(ref this.data, ref src.data);
     for (int i = 0; i < 5; i++)
     {
         swap_(ref this.dims[i], ref src.dims[i]);
     }
     swap_(ref this.total, ref src.total);
     swap_(ref this.allocated, ref src.allocated);
 }
Beispiel #11
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);
 }
Beispiel #12
0
 public static bool contains_only <T, U, V>(Narray <T> a, U value1, V value2)
 {
     for (int i = 0; i < a.Length1d(); i++)
     {
         if (!a.At1d(i).Equals(value1) && !a.At1d(i).Equals(value2))
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #13
0
        public static double Sum <T>(Narray <T> data)
        {
            double result = 0.0;
            int    n      = data.Length1d();

            for (int i = 0; i < n; i++)
            {
                result += Convert.ToDouble(data.At1d(i));
            }
            return(result);
        }
Beispiel #14
0
 public static void RowPut <T, S>(Narray <T> data, int row, Narray <S> v)
 {
     if (!(v.Length() == data.Dim(1)))
     {
         throw new Exception("CHECK: v.Length() == data.Dim(1)");
     }
     for (int i = 0; i < v.Length(); i++)
     {
         data[row, i] = (T)Convert.ChangeType(v[i], typeof(T));
     }
 }
Beispiel #15
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);
 }
Beispiel #16
0
 /// <summary>
 /// Take the data held by the src array and put it into the dest array.
 /// The src array is made empty in the proceess.  This is an O(1) operation.
 /// </summary>
 public void Move(Narray <T> src)
 {
     this.data = src.data;
     for (int i = 0; i < 5; i++)
     {
         this.dims[i] = src.dims[i];
     }
     this.total     = src.total;
     this.allocated = src.allocated;
     src.Dealloc();
 }
Beispiel #17
0
 public void Append <S>(Narray <S> src)
 {
     check_rank1();
     Reserve(src.Length());
     for (index_t i = 0; i < src.Length(); i++)
     {
         var value = src.UnsafeAt1d(i);
         data[dims[0]] = (T)Convert.ChangeType(value, typeof(T));
         dims[0]      += 1;
         total         = dims[0];
     }
 }
Beispiel #18
0
 /// <summary>
 /// Copy the elements of the source array into the destination array,
 /// resizing if necessary.
 /// </summary>
 public void Append(Narray <T> src)
 {
     check_rank1();
     Reserve(src.Length());
     for (index_t i = 0; i < src.Length(); i++)
     {
         //Push(src.unsafe_at1d(i));
         data[dims[0]] = src.UnsafeAt1d(i);
         dims[0]      += 1;
         total         = dims[0];
     }
 }
Beispiel #19
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);
        }
Beispiel #20
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;
            }
        }
Beispiel #21
0
        public static float Fractile(Narray <float> a, double f)
        {
            Floatarray temp = new Floatarray();

            if (!(f >= 0 && f <= 1))
            {
                throw new Exception("CHECK: f >= 0 && f <= 1");
            }
            temp.Copy(a);
            temp.Reshape(temp.Length1d());
            Quicksort(temp);
            return(temp[(int)(f * temp.Length())]);
        }
Beispiel #22
0
        public StdInput(Narray <float> floatarray)
        {
            alloc_(floatarray.Dim(1), floatarray.Dim(0));
            int yput;

            for (int y = 0; y < Height; y++)
            {
                yput = Height - y - 1;
                for (int x = 0; x < Width; x++)
                {
                    Put(yput, x, Convert.ToByte(floatarray[x, y] * 255));
                }
            }
        }
Beispiel #23
0
        public StdInput(Narray <byte> bytearray)
        {
            alloc_(bytearray.Dim(1), bytearray.Dim(0));
            int yput;

            for (int y = 0; y < Height; y++)
            {
                yput = Height - y - 1;
                for (int x = 0; x < Width; x++)
                {
                    Put(yput, x, bytearray[x, y]);
                }
            }
        }
Beispiel #24
0
 public static T Bat <T>(Narray <T> a, int i, int j, T value)
 {
     unchecked
     {
         if ((uint)(i) >= (uint)(a.Dim(0)))
         {
             return(value);
         }
         if ((uint)(j) >= (uint)(a.Dim(1)))
         {
             return(value);
         }
     }
     return(a.UnsafeAt(i, j));
 }
Beispiel #25
0
 /// <summary>
 /// Check whether two narrays have the same rank and sizes.
 /// </summary>
 public bool SameDims <S>(Narray <S> b)
 {
     if (this.Rank() != b.Rank())
     {
         return(false);
     }
     for (int i = 0; i < this.Rank(); i++)
     {
         if (this.Dim(i) != b.Dim(i))
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #26
0
        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);
            }
        }
Beispiel #27
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);
        }
Beispiel #28
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);
            }
        }
Beispiel #29
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;
            }
        }
Beispiel #30
0
        /// <summary>
        /// Check whether two narrays are equal (mostly for testing).
        /// </summary>
        public bool Equal(Narray <T> b)
        {
            if (this.Rank() != b.Rank())
            {
                return(false);
            }
            for (int i = 0; i < this.Rank(); i++)
            {
                if (this.Dim(i) != b.Dim(i))
                {
                    return(false);
                }
            }
            index_t n = this.Length1d();

            for (index_t i = 0; i < n; i++)
            {
                if (!this.UnsafeAt1d(i).Equals(b.UnsafeAt1d(i)))
                {
                    return(false);
                }
            }
            return(true);
        }