Esempio 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);
        }
Esempio n. 2
0
        internal static NpyArray NpyArray_Correlate(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode)
        {
            int unused = 0;

            return(_npyarray_correlate(ap1, ap2, typenum, mode, ref unused));
        }
Esempio n. 3
0
        internal static NpyArray NpyArray_Correlate2(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode)
        {
            NpyArray ret      = null;
            NpyArray cap2     = null;
            int      inverted = 0;
            int      status;

            if (NpyArray_ISCOMPLEX(ap2))
            {
                /* FIXME: PyArray_Conjugate need to be replaced by NpyArray_Conjugate,
                 *        once NpyArray_Conjugate is created, which can be done once
                 *        the ufunc stuff is in the core.
                 */
                ap2 = NpyArray_Conjugate(ap2, null);
                if (null == ap2)
                {
                    return(null);
                }
                cap2 = ap2;
            }

            ret = _npyarray_correlate(ap1, ap2, typenum, mode, ref inverted);
            if (ret == null)
            {
                goto done;
            }

            /* If we inverted input orders, we need to reverse the output array (i.e.
             * ret = ret[::-1]) */
            if (inverted != 0)
            {
                status = _npyarray_revert(ret);
                if (status != 0)
                {
                    Npy_DECREF(ret);
                    ret = null;
                    goto done;
                }
            }
done:
            Npy_XDECREF(cap2);
            return(ret);
        }
Esempio n. 4
0
 internal static NpyArray NpyArray_Correlate(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode)
 {
     return(numpyinternal.NpyArray_Correlate(ap1, ap2, typenum, mode));
 }
Esempio n. 5
0
        static NpyArray _npyarray_correlate(NpyArray ap1, NpyArray ap2, NPY_TYPES typenum, NPY_CONVOLE_MODE mode, ref int inverted)
        {
            NpyArray ret;

            npy_intp[]       length = new npy_intp[1];
            npy_intp         i, n1, n2, n, n_left, n_right, is1, is2, os;
            VoidPtr          ip1;
            VoidPtr          ip2;
            VoidPtr          op;
            NpyArray_DotFunc dot;

            n1 = NpyArray_DIM(ap1, 0);
            n2 = NpyArray_DIM(ap2, 0);
            if (n1 < n2)
            {
                ret      = ap1;
                ap1      = ap2;
                ap2      = ret;
                ret      = null;
                i        = n1;
                n1       = n2;
                n2       = i;
                inverted = 1;
            }
            else
            {
                inverted = 0;
            }

            length[0] = n1;
            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.
             */
            ret = new_array_for_sum(ap1, ap2, 1, length, typenum);
            if (ret == null)
            {
                return(null);
            }
            dot = NpyArray_DESCR(ret).f.dotfunc;
            if (dot == null)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError,
                                 "function not available for this data type");
                goto clean_ret;
            }

            is1              = NpyArray_STRIDE(ap1, 0);
            is2              = NpyArray_STRIDE(ap2, 0);
            op               = new VoidPtr(ret);
            os               = NpyArray_ITEMSIZE(ret);
            ip1              = new VoidPtr(ap1);
            ip2              = new VoidPtr(ap2);
            ip2.data_offset += n_left * is2;
            n -= n_left;

            for (i = 0; i < n_left; i++)
            {
                dot(ip1, is1, ip2, is2, op, n, ret);
                n++;
                ip2.data_offset -= is2;
                op.data_offset  += os;
            }

            for (i = 0; i < (n1 - n2 + 1); i++)
            {
                dot(ip1, is1, ip2, is2, op, n, ret);
                ip1.data_offset += is1;
                op.data_offset  += os;
            }
            for (i = 0; i < n_right; i++)
            {
                n--;
                dot(ip1, is1, ip2, is2, op, n, ret);
                ip1.data_offset += is1;
                op.data_offset  += os;
            }

            if (NpyErr_Occurred())
            {
                goto clean_ret;
            }

            return(ret);

clean_ret:
            Npy_DECREF(ret);
            return(null);
        }