Ejemplo n.º 1
0
        static NpyArray _npyarray_correlate(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode, ref int inverted)
        {
            npy_intp n1 = NpyArray_DIM(ap1, 0);
            npy_intp n2 = NpyArray_DIM(ap2, 0);

            if (n1 < n2)
            {
                var temp = ap1;
                ap1  = ap2;
                ap2  = temp;
                temp = null;
                var t = n1;
                n1       = n2;
                n2       = t;
                inverted = 1;
            }
            else
            {
                inverted = 0;
            }

            npy_intp n_left, n_right;

            npy_intp[] length = new npy_intp[] { n1 };

            npy_intp n = n2;

            switch (mode)
            {
            case NPY_CONVOLE_MODE.NPY_CONVOLVE_VALID:
                length[0] = length[0] - n + 1;
                n_left    = n_right = 0;
                break;

            case NPY_CONVOLE_MODE.NPY_CONVOLVE_SAME:
                n_left  = (npy_intp)(n / 2);
                n_right = n - n_left - 1;
                break;

            case NPY_CONVOLE_MODE.NPY_CONVOLVE_FULL:
                n_right   = n - 1;
                n_left    = n - 1;
                length[0] = length[0] + n - 1;
                break;

            default:
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "mode must be 0, 1, or 2");
                return(null);
            }

            /*
             * Need to choose an output array that can hold a sum
             * -- use priority to determine which subtype.
             */
            NpyArray ret = new_array_for_sum(ap1, ap2, 1, length, typenum);

            if (ret == null)
            {
                return(null);
            }

            npy_intp is1 = NpyArray_STRIDE(ap1, 0);
            npy_intp is2 = NpyArray_STRIDE(ap2, 0);
            VoidPtr  op  = new VoidPtr(ret);
            npy_intp os  = NpyArray_ITEMSIZE(ret);
            VoidPtr  ip1 = new VoidPtr(ap1);
            VoidPtr  ip2 = new VoidPtr(ap2);

            ip2.data_offset += n_left * is2;
            n -= n_left;

            var helper = MemCopy.GetMemcopyHelper(ip1);

            helper.correlate(ip1, ip2, op, is1, is2, os, n, n1, n2, n_left, n_right);


            if (NpyErr_Occurred())
            {
                goto clean_ret;
            }

            return(ret);

clean_ret:
            Npy_DECREF(ret);
            return(null);
        }
Ejemplo n.º 2
0
        internal static NpyArray NpyArray_MatrixProduct(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum)
        {
            NpyArray           ret = null;
            NpyArrayIterObject it1, it2;
            npy_intp           i, j, l, is1, is2, os;

            npy_intp [] dimensions = new  npy_intp[npy_defs.NPY_MAXDIMS];
            int         nd, axis, matchDim;
            VoidPtr     op;

            if (ap2.nd > 1)
            {
                matchDim = ap2.nd - 2;
            }
            else
            {
                matchDim = 0;
            }
            l = ap1.dimensions[ap1.nd - 1];
            if (ap2.dimensions[matchDim] != l)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "objects are not aligned");
                return(null);
            }
            nd = ap1.nd + ap2.nd - 2;
            if (nd > npy_defs.NPY_MAXDIMS)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "dot: too many dimensions in result");
                return(null);
            }
            j = 0;
            for (i = 0; i < ap1.nd - 1; i++)
            {
                dimensions[j++] = ap1.dimensions[i];
            }
            for (i = 0; i < ap2.nd - 2; i++)
            {
                dimensions[j++] = ap2.dimensions[i];
            }
            if (ap2.nd > 1)
            {
                dimensions[j++] = ap2.dimensions[ap2.nd - 1];
            }
            is1 = ap1.strides[ap1.nd - 1];
            is2 = ap2.strides[matchDim];
            /* Choose which subtype to return */
            ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum);
            if (ret == null)
            {
                return(null);
            }
            /* Ensure that multiarray.dot(<Nx0>,<0xM>) . zeros((N,M)) */
            if (NpyArray_SIZE(ap1) == 0 && NpyArray_SIZE(ap2) == 0)
            {
                memset(NpyArray_DATA(ret), 0, (int)NpyArray_NBYTES(ret));
            }
            else
            {
                /* Ensure that multiarray.dot([],[]) . 0 */
                memset(NpyArray_DATA(ret), 0, (int)NpyArray_ITEMSIZE(ret));
            }


            op   = new VoidPtr(ret);
            os   = NpyArray_ITEMSIZE(ret);
            axis = ap1.nd - 1;
            it1  = NpyArray_IterAllButAxis(ap1, ref axis);
            it2  = NpyArray_IterAllButAxis(ap2, ref matchDim);

            var helper = MemCopy.GetMemcopyHelper(it1.dataptr);

            helper.MatrixProduct(it1, it2, op, is1, is2, os, l);

            Npy_DECREF(it1);
            Npy_DECREF(it2);
            if (NpyErr_Occurred())
            {
                goto fail;
            }
            return(ret);

fail:
            Npy_XDECREF(ret);
            return(null);
        }
Ejemplo n.º 3
0
        internal static NpyArray NpyArray_InnerProduct(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum)
        {
            npy_intp [] dimensions = new npy_intp[npy_defs.NPY_MAXDIMS];

            npy_intp l = ap1.dimensions[ap1.nd - 1];

            if (ap2.dimensions[ap2.nd - 1] != l)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "matrices are not aligned");
                return(null);
            }

            int nd = ap1.nd + ap2.nd - 2;
            int j  = 0;

            for (int i = 0; i < ap1.nd - 1; i++)
            {
                dimensions[j++] = ap1.dimensions[i];
            }
            for (int i = 0; i < ap2.nd - 1; i++)
            {
                dimensions[j++] = ap2.dimensions[i];
            }

            /*
             * Need to choose an output array that can hold a sum
             * -- use priority to determine which subtype.
             */
            NpyArray ret = new_array_for_sum(ap1, ap2, nd, dimensions, typenum);

            if (ret == null)
            {
                return(null);
            }

            npy_intp           is1  = ap1.strides[ap1.nd - 1];
            npy_intp           is2  = ap2.strides[ap2.nd - 1];
            VoidPtr            op   = new VoidPtr(ret);
            npy_intp           os   = ret.descr.elsize;
            int                axis = ap1.nd - 1;
            NpyArrayIterObject it1  = NpyArray_IterAllButAxis(ap1, ref axis);

            axis = ap2.nd - 1;
            NpyArrayIterObject it2 = NpyArray_IterAllButAxis(ap2, ref axis);

            var helper = MemCopy.GetMemcopyHelper(it1.dataptr);

            helper.InnerProduct(it1, it2, op, is1, is2, os, l);

            Npy_DECREF(it1);
            Npy_DECREF(it2);
            if (NpyErr_Occurred())
            {
                goto fail;
            }
            return(ret);

fail:
            Npy_DECREF(ret);
            return(null);
        }
Ejemplo n.º 4
0
        internal static NpyArray NpyArray_CopyAndTranspose(NpyArray arr)
        {
            NpyArray ret, tmp;
            int      nd, eltsize;
            npy_intp stride2;

            npy_intp[] dims = new npy_intp[2];
            npy_intp   i, j;
            VoidPtr    iptr; VoidPtr optr;

            /* make sure it is well-behaved */
            tmp = NpyArray_ContiguousFromArray(arr, NpyArray_TYPE(arr));
            if (tmp == null)
            {
                return(null);
            }
            arr = tmp;

            nd = NpyArray_NDIM(arr);
            if (nd == 1)
            {
                /* we will give in to old behavior */
                Npy_DECREF(tmp);
                return(arr);
            }
            else if (nd != 2)
            {
                Npy_DECREF(tmp);
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "only 2-d arrays are allowed");
                return(null);
            }

            /* Now construct output array */
            dims[0] = NpyArray_DIM(arr, 1);
            dims[1] = NpyArray_DIM(arr, 0);
            eltsize = NpyArray_ITEMSIZE(arr);
            Npy_INCREF(arr.descr);
            ret = NpyArray_Alloc(arr.descr, 2, dims, false, null);
            if (ret == null)
            {
                Npy_DECREF(tmp);
                return(null);
            }

            /* do 2-d loop */
            optr    = new VoidPtr(ret);
            stride2 = eltsize * dims[0];
            for (i = 0; i < dims[0]; i++)
            {
                iptr              = new VoidPtr(arr);
                iptr.data_offset += i * eltsize;

                var helper = MemCopy.GetMemcopyHelper(optr);
                helper.memmove_init(optr, iptr);
                for (j = 0; j < dims[1]; j++)
                {
                    /* optr[i,j] = iptr[j,i] */
                    helper.memcpy(optr.data_offset, iptr.data_offset, eltsize);
                    optr.data_offset += eltsize;
                    iptr.data_offset += stride2;
                }
            }

            Npy_DECREF(tmp);
            return(ret);
        }
Ejemplo n.º 5
0
        internal static NpyArrayMapIterObject NpyArray_MapIterNew(NpyIndex [] indexes, int n)
        {
            NpyArrayMapIterObject mit;
            int i, j;

            /* Allocates the Python object wrapper around the map iterator. */
            mit = new NpyArrayMapIterObject();

            NpyObject_Init(mit, NpyArrayMapIter_Type);
            if (mit == null)
            {
                return(null);
            }
            for (i = 0; i < npy_defs.NPY_MAXDIMS; i++)
            {
                mit.iters[i] = null;
            }
            mit.index         = 0;
            mit.ait           = null;
            mit.subspace      = null;
            mit.numiter       = 0;
            mit.consec        = 1;
            mit.n_indexes     = 0;
            mit.nob_interface = null;

            /* Expand the boolean arrays in indexes. */
            mit.n_indexes = NpyArray_IndexExpandBool(indexes, n, mit.indexes);
            if (mit.n_indexes < 0)
            {
                Npy_DECREF(mit);
                return(null);
            }

            /* Make iterators from any intp arrays and intp in the index. */
            j = 0;
            for (i = 0; i < mit.n_indexes; i++)
            {
                NpyIndex index = mit.indexes[i];

                if (index.type == NpyIndexType.NPY_INDEX_INTP_ARRAY)
                {
                    mit.iters[j] = NpyArray_IterNew(index.intp_array);
                    if (mit.iters[j] == null)
                    {
                        mit.numiter = j - 1;
                        Npy_DECREF(mit);
                        return(null);
                    }
                    j++;
                }
                else if (index.type == NpyIndexType.NPY_INDEX_INTP)
                {
                    NpyArray_Descr indtype;
                    NpyArray       indarray;

                    /* Make a 0-d array for the index. */
                    indtype  = NpyArray_DescrFromType(NPY_TYPES.NPY_INTP);
                    indarray = NpyArray_Alloc(indtype, 0, null, false, null);
                    if (indarray == null)
                    {
                        mit.numiter = j - 1;
                        Npy_DECREF(mit);
                        return(null);
                    }

                    byte[] src = BitConverter.GetBytes(index.intp);

                    var srcvp  = new VoidPtr(src);
                    var helper = MemCopy.GetMemcopyHelper(indarray.data);
                    helper.memmove_init(indarray.data, srcvp);
                    helper.memcpy(indarray.data.data_offset, srcvp.data_offset, sizeof(npy_intp));
                    mit.iters[j] = NpyArray_IterNew(indarray);
                    Npy_DECREF(indarray);
                    if (mit.iters[j] == null)
                    {
                        mit.numiter = j - 1;
                        Npy_DECREF(mit);
                        return(null);
                    }
                    j++;
                }
            }
            mit.numiter = j;

            /* Broadcast the index iterators. */
            if (NpyArray_Broadcast(mit) < 0)
            {
                Npy_DECREF(mit);
                return(null);
            }

            return(mit);
        }
Ejemplo n.º 6
0
        internal static void memclr(VoidPtr dest, npy_intp len)
        {
            var helper = MemCopy.GetMemcopyHelper(dest);

            helper.memclr(dest, dest.data_offset, len);
        }