private static void Npy_XDECREF(NpyArrayMultiIterObject multi)
 {
     if (multi == null)
     {
         return;
     }
     Npy_DECREF(multi);
 }
示例#2
0
        internal static void NpyArray_MultiIter_NEXT(NpyArrayMultiIterObject multi)
        {
            Debug.Assert(Validate(multi));
            multi.index++;

            for (int i = 0; i < multi.numiter; i++)
            {
                NpyArray_ITER_NEXT(multi.iters[i]);
            }
        }
示例#3
0
        internal static NpyArrayMultiIterObject NpyArray_vMultiIterFromArrays(NpyArray[] mps, int n, int nadd, params object[] va)
        {
            NpyArrayMultiIterObject multi;
            NpyArray current;
            int      i, ntot;
            bool     err = false;

            ntot = n + nadd;
            if (ntot < 2 || ntot > npy_defs.NPY_MAXARGS)
            {
                var msg = string.Format("Need between 2 and({0}) array objects(inclusive).", npy_defs.NPY_MAXARGS);
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, msg);
                return(null);
            }
            multi = new NpyArrayMultiIterObject();
            if (multi == null)
            {
                NpyErr_MEMORY();
                return(null);
            }
            NpyObject_Init(multi, NpyArrayMultiIter_Type);

            for (i = 0; i < ntot; i++)
            {
                multi.iters[i] = null;
            }
            multi.numiter = ntot;
            multi.index   = 0;

            for (i = 0; i < ntot; i++)
            {
                if (i < n)
                {
                    current = mps[i];
                }
                else
                {
                    current = (NpyArray)va[i - n];
                }
                multi.iters[i] = NpyArray_IterNew(current);
            }

            if (!err && NpyArray_Broadcast(multi) < 0)
            {
                err = true;
            }
            if (err)
            {
                Npy_DECREF(multi);
                return(null);
            }
            NpyArray_MultiIter_RESET(multi);
            /* Defer creation of the wrapper - will be handled by Npy_INTERFACE. */
            return(multi);
        }
示例#4
0
        static void arraymultiter_dealloc(object obj)
        {
            NpyArrayMultiIterObject multi = obj as NpyArrayMultiIterObject;

            Debug.Assert(0 == multi.nob_refcnt);

            for (int i = 0; i < multi.numiter; i++)
            {
                Npy_XDECREF(multi.iters[i]);
            }
            multi.nob_magic_number = npy_defs.NPY_INVALID_MAGIC;
            NpyArray_free(multi);
        }
示例#5
0
        internal static NpyArrayMultiIterObject NpyArray_MultiIterNew()
        {
            NpyArrayMultiIterObject ret;

            ret = new NpyArrayMultiIterObject();
            if (null == ret)
            {
                NpyErr_MEMORY();
                return(null);
            }
            NpyObject_Init(ret, NpyArrayMultiIter_Type);
            /* Defer creation of the wrapper - will be handled by Npy_INTERFACE. */
            return(ret);
        }
示例#6
0
        internal static int NpyArray_RemoveSmallest(NpyArrayMultiIterObject multi)
        {
            NpyArrayIterObject it;
            int      i, j;
            int      axis;
            npy_intp smallest;

            npy_intp [] sumstrides = new npy_intp[npy_defs.NPY_MAXDIMS];

            if (multi.nd == 0)
            {
                return(-1);
            }
            for (i = 0; i < multi.nd; i++)
            {
                sumstrides[i] = 0;
                for (j = 0; j < multi.numiter; j++)
                {
                    sumstrides[i] += multi.iters[j].strides[i];
                }
            }
            axis     = 0;
            smallest = sumstrides[0];
            /* Find longest dimension */
            for (i = 1; i < multi.nd; i++)
            {
                if (sumstrides[i] < smallest)
                {
                    axis     = i;
                    smallest = sumstrides[i];
                }
            }
            for (i = 0; i < multi.numiter; i++)
            {
                it            = multi.iters[i];
                it.contiguous = false;
                if (it.size != 0)
                {
                    it.size /= (it.dims_m1[axis] + 1);
                }
                it.dims_m1[axis]     = 0;
                it.backstrides[axis] = 0;
            }
            multi.size = multi.iters[0].size;
            return(axis);
        }
示例#7
0
 internal static int NpyArray_Broadcast(NpyArrayMultiIterObject mit)
 {
     return(numpyinternal.NpyArray_Broadcast(mit));
 }
示例#8
0
 internal static VoidPtr NpyArray_MultiIter_DATA(NpyArrayMultiIterObject multi, int index)
 {
     return(numpyinternal.NpyArray_MultiIter_DATA(multi, index));
 }
示例#9
0
 internal static bool NpyArray_NotDone(NpyArrayMultiIterObject multi)
 {
     return(numpyinternal.NpyArray_MultiIter_NOTDONE(multi));
 }
示例#10
0
 internal static void NpyArray_Next(NpyArrayMultiIterObject multi)
 {
     numpyinternal.NpyArray_MultiIter_NEXT(multi);
 }
示例#11
0
 internal static void NpyArray_Reset(NpyArrayMultiIterObject multi)
 {
     numpyinternal.NpyArray_MultiIter_RESET(multi);
 }
示例#12
0
 internal static int NpyArray_RemoveSmallest(NpyArrayMultiIterObject multi)
 {
     return(numpyinternal.NpyArray_RemoveSmallest(multi));
 }
示例#13
0
 internal static bool NpyArray_MultiIter_NOTDONE(NpyArrayMultiIterObject multi)
 {
     return(multi.index < multi.size);
 }
示例#14
0
        internal static int NpyArray_Broadcast(NpyArrayMultiIterObject mit)
        {
            int                i, nd, k, j;
            npy_intp           tmp;
            NpyArrayIterObject it;

            /* Discover the broadcast number of dimensions */
            for (i = 0, nd = 0; i < mit.numiter; i++)
            {
                nd = Math.Max(nd, mit.iters[i].ao.nd);
            }
            mit.nd = nd;

            /* Discover the broadcast shape in each dimension */
            for (i = 0; i < nd; i++)
            {
                mit.dimensions[i] = 1;
                for (j = 0; j < mit.numiter; j++)
                {
                    it = mit.iters[j];
                    /* This prepends 1 to shapes not already equal to nd */
                    k = i + it.ao.nd - nd;
                    if (k >= 0)
                    {
                        tmp = it.ao.dimensions[k];
                        if (tmp == 1)
                        {
                            continue;
                        }
                        if (mit.dimensions[i] == 1)
                        {
                            mit.dimensions[i] = tmp;
                        }
                        else if (mit.dimensions[i] != tmp)
                        {
                            NpyErr_SetString(npyexc_type.NpyExc_ValueError, "shape mismatch: objects cannot be broadcast to a single shape");
                            return(-1);
                        }
                    }
                }
            }

            /*
             * Reset the iterator dimensions and strides of each iterator
             * object -- using 0 valued strides for broadcasting
             * Need to check for overflow
             */
            tmp = NpyArray_OverflowMultiplyList(mit.dimensions, mit.nd);
            if (tmp == npy_intp.MaxValue)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "broadcast dimensions too large.");
                return(-1);
            }
            mit.size = tmp;
            for (i = 0; i < mit.numiter; i++)
            {
                it       = mit.iters[i];
                it.nd_m1 = mit.nd - 1;
                it.size  = tmp;
                nd       = it.ao.nd;

                if (mit.nd != 0)
                {
                    it.factors[mit.nd - 1] = 1;
                }
                for (j = 0; j < mit.nd; j++)
                {
                    it.dims_m1[j] = mit.dimensions[j] - 1;
                    k             = j + nd - mit.nd;

                    /*
                     * If this dimension was added or shape of
                     * underlying array was 1
                     */
                    if ((k < 0) ||
                        it.ao.dimensions[k] != mit.dimensions[j])
                    {
                        it.contiguous = false;
                        it.strides[j] = 0;
                    }
                    else
                    {
                        it.strides[j] = it.ao.strides[k];
                    }
                    it.backstrides[j] = it.strides[j] * it.dims_m1[j];
                    if (j > 0)
                    {
                        it.factors[mit.nd - j - 1] =
                            it.factors[mit.nd - j] * mit.dimensions[mit.nd - j];
                    }
                }
                NpyArray_ITER_RESET(it);
            }
            return(0);
        }
示例#15
0
 internal static void NpyArray_free(NpyArrayMultiIterObject iterobj)
 {
     return;
 }
示例#16
0
 internal static bool Validate(NpyArrayMultiIterObject multi)
 {
     return(null != multi && npy_defs.NPY_VALID_MAGIC == multi.nob_magic_number);
 }
示例#17
0
 internal static VoidPtr NpyArray_MultiIter_DATA(NpyArrayMultiIterObject multi, npy_intp index)
 {
     return(multi.iters[index].dataptr);
 }