Beispiel #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;
            }
        }
Beispiel #2
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 #3
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 #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
        /// <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 #6
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 #7
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 #8
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 #9
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 #10
0
 public int Dim(int d)
 {
     if (d != 0)
     {
         throw new Exception("ObjList: rank must be 0");
     }
     return(data.Dim(d));
 }
Beispiel #11
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 #12
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 #13
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 #14
0
        /// <summary>
        /// Make the first array have the same dimensions as the second array.
        /// </summary>
        public Narray <T> MakeLike <S>(Narray <S> b)
        {
            if (SameDims(b))
            {
                return(this);
            }
            Narray <T> a = this;
            int        r = b.Rank();

            switch (r)
            {
            case 0:
                a.Dealloc();
                break;

            case 1:
                a.Resize(b.Dim(0));
                break;

            case 2:
                a.Resize(b.Dim(0), b.Dim(1));
                break;

            case 3:
                a.Resize(b.Dim(0), b.Dim(1), b.Dim(2));
                break;

            case 4:
                a.Resize(b.Dim(0), b.Dim(1), b.Dim(2), b.Dim(3));
                break;

            default:
                throw new Exception("bad rank");
            }
            return(this);
        }
Beispiel #15
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 #16
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);
        }
Beispiel #17
0
 /// <summary>
 /// Array subscripting with extending boundary conditions.
 /// </summary>
 public static T Ext <T>(Narray <T> a, int i, int j)
 {
     i = Math.Max(0, Math.Min(i, a.Dim(0) - 1));
     j = Math.Max(0, Math.Min(j, a.Dim(1) - 1));
     return(a.UnsafeAt(i, j));
 }
Beispiel #18
0
 /// <summary>
 /// Array subscripting with extending boundary conditions.
 /// </summary>
 public static void ExtPut <T>(Narray <T> a, int i, int j, T value)
 {
     i = Math.Max(0, Math.Min(i, a.Dim(0) - 1));
     j = Math.Max(0, Math.Min(j, a.Dim(1) - 1));
     a.UnsafePut(i, j, value);
 }