Beispiel #1
0
        public idx <T> transpose(int[] p)
        {
            idx <T> r = new idx <T>(srgptr, spec.getoffset());

            spec.transpose_into(ref r.spec, p);
            return(r);
        }
Beispiel #2
0
        public idx <T> unfold(int d, intg k, intg s)
        {
            idx <T> r = new idx <T>(srgptr, spec.getoffset());

            spec.unfold_into(ref r.spec, d, k, s);
            return(r);
        }
Beispiel #3
0
        public idx <T> narrow(int d, intg s, intg o)
        {
            idx <T> r = new idx <T>(srgptr, spec.getoffset());

            spec.narrow_into(ref r.spec, d, s, o);
            return(r);
        }
Beispiel #4
0
        public static void idx_m2squdotm2acc(idx <T> i1, idx <T> i2, idx <T> o)
        {
            loops.idx_checkorder3(i1, 2, i2, 2, o, 0);
#warning   idx_checkdim2(i1, 0, i2.dim(0), i1, 1, i2.dim(1));

            intg     imax = i1.dim(0), jmax = i2.dim(1);
            intg     c1_m0 = i1.mod(0), c2_m0 = i2.mod(0);
            intg     c1_m1 = i1.mod(1), c2_m1 = i2.mod(1);
            eptr <T> c1_0, c2_0, d1, c1, c2;

            c1_0 = i1.idx_ptr();
            c2_0 = i2.idx_ptr();
            d1   = o.idx_ptr();

            c1 = c1_0;
            c2 = c2_0;
            T f = d1.item;
            for (int i = 0; i < imax; ++i)
            {
                c1.index = c1_0.index;
                c2.index = c2_0.index;
                for (int j = 0; j < jmax; ++j)
                {
                    T ff = Dot(Dot(c1.item, c1.item), c2.item);
                    f         = Add <T>(ff, f);
                    c1.index += c1_m1;
                    c2.index += c2_m1;
                }
                c1_0.index += c1_m0;
                c2_0.index += c2_m0;
            }
            d1.item = f;
        }
Beispiel #5
0
 public static void idx_compatibility_error3 <T, T2, T3>(idx <T> idx1, idx <T2> idx2, idx <T3> idx3, string errmsg)
     where T : struct
     where T2 : struct
     where T3 : struct
 {
     Global.eblerror(idx1 + " , " + idx2 + " and " + idx3 + " are incompatible: " + errmsg);
 }
Beispiel #6
0
        public static bool save_matrix <T>(idx <T> m, FILE fp) where T : struct
        {
            int v;

            v = get_magic <T>();
            if (fp.fwrite(v) != 1)
            {
                return(false);
            }
            v = m.order();
            if (fp.fwrite(v) != 1)
            {
                return(false);
            }
            for (int i = 0; (i < m.order()) || (i < 3); ++i)
            {
                if (i < m.order())
                {
                    v = m.dim(i);
                }
                else
                {
                    v = 1;
                }
                if (fp.fwrite(v) != 1)
                {
                    return(false);
                }
            }
            loops.idx_aloop1(m, (i) => fp.fwrite(i.item));
            return(true);
        }
Beispiel #7
0
 public static void idx_checkorder1 <T>(idx <T> src0, int o0) where T : struct
 {
     if ((src0).order() != o0)
     {
         Global.eblerror(src0 + " does not have order " + o0);
     }
 }
Beispiel #8
0
        public static void idx_m1extm1(idx <T> i1, idx <T> i2, idx <T> o1)
        {
            eptr <T> c2, d1, c1, c2_0, d1_0;
            intg     c2_m0 = i2.mod(0), d1_m1 = o1.mod(1);
            intg     c1_m0 = i1.mod(0), d1_m0 = o1.mod(0);
            intg     j, jmax = o1.dim(1);
            intg     i, imax = o1.dim(0);

            c1   = i1.idx_ptr();
            c2_0 = i2.idx_ptr();
            d1_0 = o1.idx_ptr();
            for (i = 0; i < imax; i++)
            {
                d1 = d1_0;
                c2 = c2_0;
                for (j = 0; j < jmax; j++)
                {
                    d1.item   = Dot(c1.item, c2.item);
                    d1.index += d1_m1;
                    c2.index += c2_m0;
                }
                d1_0.index += d1_m0;
                c1.index   += c1_m0;
            }
        }
Beispiel #9
0
        public idx <T> select(int d, intg i)
        {
            idx <T> r = new idx <T>(srgptr, spec.getoffset());

            spec.select_into(ref r.spec, d, i);
            return(r);
        }
Beispiel #10
0
        public static T idx_sumacc(idx <T> m, idx <T> acc)
        {
            var sum = Add <T>(acc.get(), idx_sum(m));

            acc.set((T)Global.B(sum));
            return(sum);
        }
Beispiel #11
0
        public static idx <T> idx_copy(idx <T> m)
        {
            idx <T> dst = new idx <T>(m.get_idxdim());

            idx_copy <T>(m, dst);
            return(dst);
        }
Beispiel #12
0
        public static T idx_sum(idx <T> m)
        {
            T V = (T)Global.B(0.0);

            i.idx_aloop1(m, (m1) => V = Add <T>(V, m1.item));
            return(V);
        }
Beispiel #13
0
 public static void CHECK_CONTIGUOUS1 <T>(idx <T> m1) where T : struct
 {
     if (!(m1).contiguousp())
     {
         Global.eblerror("expected contiguous tensor " + m1);
     }
 }
Beispiel #14
0
        static public void read_cast_matrix <T, T2>(ref FILE fp, ref idx <T2> Out) where T  : struct where T2 : struct
        {
            idx <T> m = new idx <T>(Out.get_idxdim());

            read_matrix_body(ref fp, ref m);
            idxops <T> .idx_copy <T2>(m, Out);
        }
Beispiel #15
0
 public static void idx_aloop1_on <T>(idxiter <T> itr0, idx <T> src0, Action <eptr <T> > act) //Action<eptr<T>, idx<T>> act
     where T : struct
 {
     for (itr0.init(src0); itr0.notdone(); itr0.next())
     {
         act.Invoke(itr0.data);
     }
 }
Beispiel #16
0
 public static void idx_copy <T2>(idx <T> m, idx <T2> dst) where T2 : struct
 {
     if (m.order() == 0 && dst.order() == 0)
     {
         dst.set((T2)Global.B(m.get())); return;
     }
     i.idx_aloop2(m, dst, (m1, d1) => d1.item = (T2)Global.B(m1.item));
 }
Beispiel #17
0
 public static void idx_copy(idx <T> m, idx <T> dst)
 {
     if (m.order() == 0 && dst.order() == 0)
     {
         dst.set(m.get()); return;
     }
     i.idx_aloop2(m, dst, (m1, d1) => d1.item = m1.item);
 }
Beispiel #18
0
        public static void idx_m4dotm2(idx <T> i1, idx <T> i2, idx <T> o1)
        {
            loops.idx_checkorder3(i1, 4, i2, 2, o1, 2); // check for compatible orders
            if ((i1.dim(0) != o1.dim(0)) || (i1.dim(1) != o1.dim(1)) ||
                (i1.dim(2) != i2.dim(0)) || (i1.dim(3) != i2.dim(1)))
            {
                loops.idx_compatibility_error3(i1, i2, o1, "incompatible dimensions");
            }
            eptr <T> c1, c1_2;
            eptr <T> c2, c2_0;
            eptr <T> c1_0, c1_1;
            eptr <T> ker;
            intg     c1_m2 = (i1).mod(2), c2_m0 = (i2).mod(0);
            intg     c1_m3 = (i1).mod(3), c2_m1 = (i2).mod(1);
            intg     k, l, kmax = (i2).dim(0), lmax = (i2).dim(1);
            eptr <T> d1_0, d1;
            T        f;
            intg     c1_m0 = (i1).mod(0), d1_m0 = (o1).mod(0);
            intg     c1_m1 = (i1).mod(1), d1_m1 = (o1).mod(1);
            intg     i, j, imax = (o1).dim(0), jmax = (o1).dim(1);

            c1_0 = i1.idx_ptr();
            ker  = i2.idx_ptr();
            d1_0 = o1.idx_ptr();
            c1_1 = c1_0;
            d1   = d1_0;
            c1_2 = c1_1;
            c2_0 = ker;
            for (i = 0; i < imax; i++)
            {
                c1_1.index = c1_0.index;
                d1.index   = d1_0.index;
                for (j = 0; j < jmax; j++)
                {
                    f          = default(T);
                    c1_2.index = c1_1.index;
                    c2_0.index = ker.index;
                    for (k = 0; k < kmax; k++)
                    {
                        c1 = c1_2;
                        c2 = c2_0;
                        for (l = 0; l < lmax; l++)
                        {
                            f         = Add(Dot(c1.item, c2.item), f);
                            c1.index += c1_m3;
                            c2.index += c2_m1;
                        }
                        c1_2.index += c1_m2;
                        c2_0.index += c2_m0;
                    }
                    d1.item     = f;
                    d1.index   += d1_m1;
                    c1_1.index += c1_m1;
                }
                d1_0.index += d1_m0;
                c1_0.index += c1_m0;
            }
        }
Beispiel #19
0
 public static void idx_checknelems2_all <T, T2>(idx <T> src0, idx <T2> src1)
     where T : struct
     where T2 : struct
 {
     if ((src0).nelements() != (src1).nelements())
     {
         Global.eblerror(src0 + " and " + src1 +
                         " should have the same number of elements");
     }
 }
Beispiel #20
0
        public static void idx_eloop1 <T>(idx <T> src0, Action <idxlooper <T>, idx <T> > act)
            where T : struct
        {
            idxlooper <T> dst0 = new idxlooper <T>(src0, src0.order() - 1);

            for (; dst0.notdone(); dst0.next())
            {
                act.Invoke(dst0, src0);
            }
        }
Beispiel #21
0
 public idx <T> view_as_order(int n)
 {
     if (n < 0)
     {
         throw new Exception("view_as_order: input dimension must be positive");
         return(this);
     }
     if (n == spec.ndim)
     {
         return(this);
     }
     else
     {
         if ((n == 1) && (spec.ndim == 1))
         {
             // the order is already 1, do nothing and return current idx.
             return(new idx <T>(this));
         }
         else if (n == 1)
         {
             // the order is not 1, check that data is contiguous and return
             // a 1D idx.
             loops.CHECK_CONTIGUOUS1(this);
             idx <T> r = new idx <T>(getstorage(), 0, spec.nelements());
             return(r);
         }
         else if (n > spec.ndim)
         {
             intg[] ldim = new intg[n];
             intg[] lmod = new intg[n];
             ldim = spec.dim;
             lmod = spec.mod;
             for (int i = spec.ndim; i < n; ++i)
             {
                 ldim[i] = 1;
                 lmod[i] = 1;
             }
             idx <T> r = new idx <T>(getstorage(), spec.getoffset(), n, ldim, lmod);
             if (!(ldim == null))
             {
                 ldim = null;
             }
             if ((lmod == null))
             {
                 lmod = null;
             }
             return(r);
         }
         else
         {
             throw new Exception("view_as_order is not defined when n < current order");
             return(new idx <T>(this));
         }
     }
 }
Beispiel #22
0
        public static void TestReader()
        {
            //idxdim d = idxIO.get_matrix_dims(@"F:\Datasets\DSCompile\net00000.mat");
            //Global.Print(d.ToString());


            FILE         fp   = new FILE(@"F:\Datasets\DSCompile\net00000.mat");
            idx <double> dubs = idxIO.load_matrix <double>(ref fp, null);

            Global.Print(dubs.ToString());
        }
Beispiel #23
0
        public static void idx_aloop2 <T, T2>(idx <T> src0, idx <T2> src1, Action <eptr <T>, eptr <T2> > act)
            where T : struct
            where T2 : struct
        {
            var itr0 = new idxiter <T>();
            var itr1 = new idxiter <T2>();

            for (itr0.init(src0), itr1.init(src1); itr0.notdone(); itr0.next(), itr1.next())
            {
                act.Invoke(itr0.data, itr1.data);
            }
        }
Beispiel #24
0
        public static void idx_eloop2 <T, T2>(idx <T> src0, idx <T2> src1, Action <idxlooper <T>, idx <T>, idxlooper <T2>, idx <T2> > act)
            where T : struct
            where T2 : struct
        {
            idxlooper <T>  dst0 = new idxlooper <T>(src0, src0.order() - 1);
            idxlooper <T2> dst1 = new idxlooper <T2>(src1, src1.order() - 1);

            for (; dst0.notdone(); dst0.next(), dst1.next())
            {
                act.Invoke(dst0, src0, dst1, src1);
            }
        }
Beispiel #25
0
 public static bool CheckEqual(idx <T> a, idx <T> b)
 {
     //if (a.spec.dim != b.spec.dim) { return false; }
     if (a.spec.ndim != b.spec.ndim)
     {
         return(false);
     }
     if (a.spec.mod != b.spec.mod)
     {
         return(false);
     }
     return(true);
 }
Beispiel #26
0
 public static void idx_checkorder2 <T, T2>(idx <T> src0, int o0, idx <T2> src1, int o1)
     where T : struct
     where T2 : struct
 {
     if ((src0).order() != o0)
     {
         Global.eblerror(src0 + " does not have order " + o0);
     }
     if ((src1).order() != o1)
     {
         Global.eblerror(src1 + " does not have order " + o1);
     }
 }
Beispiel #27
0
 public idxlooper(idx <T> m, int ld)
 {
     if (m.order() == 0)
     {
         Global.eblerror("cannot loop on idx with order 0. idx is: " + m);
     }
     i    = 0;
     dimd = m.spec.dim[ld];
     modd = m.spec.mod[ld];
     m.spec.select_into(ref spec, ld, i);
     this.srgptr = m.srgptr.Addreference();
     item        = new eptr <T>(srgptr, spec.getoffset());
 }
Beispiel #28
0
 public static void CHECK_CONTIGUOUS2 <T, T2>(idx <T> m1, idx <T2> m2)
     where T : struct
     where T2 : struct
 {
     if (!(m1).contiguousp())
     {
         Global.eblerror("expected contiguous tensor " + m1);
     }
     if (!(m2).contiguousp())
     {
         Global.eblerror("expected contiguous tensor " + m2);
     }
 }
Beispiel #29
0
        public static void Testm2dotm1()
        {
            idx <T> d2 = new idx <T>(2, 5);
            idx <T> d1 = new idx <T>(5);
            idx <T> o  = new idx <T>(2);

            idxops <T> .idx_fill(d2, 2.0);

            idxops <T> .idx_fill(d1, 1.0);

            idxops <T> .idx_m2dotm1(d2, d1, o);

            loops.idx_bloop1(o, (lop, o1) => Global.Print(o.get()));
        }
Beispiel #30
0
        public static void idx_cloop3 <T, T2, T3>(idx <T> src0, idx <T2> src1, idx <T3> src2, Action <idxlooper <T>, idxlooper <T2>, idxlooper <T3> > act)
            where T : struct
            where T2 : struct
            where T3 : struct
        {
            idxlooper <T>  dst0 = new idxlooper <T>(src0, 0);
            idxlooper <T2> dst1 = new idxlooper <T2>(src1, 0);
            idxlooper <T3> dst2 = new idxlooper <T3>(src2, 0);

            for (; dst0.notdone(); dst0.next(), dst1.next(), dst2.next())
            {
                act.Invoke(dst0, dst1, dst2);
            }
        }