Ejemplo n.º 1
0
        public static ndarray ediff1d(ndarray ary, ndarray to_end = null, ndarray to_begin = null)
        {
            /*
             * The differences between consecutive elements of an array.
             *
             * Parameters
             * ----------
             * ary : array_like
             *  If necessary, will be flattened before the differences are taken.
             * to_end : array_like, optional
             *  Number(s) to append at the end of the returned differences.
             * to_begin : array_like, optional
             *  Number(s) to prepend at the beginning of the returned differences.
             *
             * Returns
             * -------
             * ediff1d : ndarray
             *  The differences. Loosely, this is ``ary.flat[1:] - ary.flat[:-1]``.
             *
             * See Also
             * --------
             * diff, gradient
             *
             * Notes
             * -----
             * When applied to masked arrays, this function drops the mask information
             * if the `to_begin` and/or `to_end` parameters are used.
             *
             * Examples
             * --------
             * >>> x = np.array([1, 2, 4, 7, 0])
             * >>> np.ediff1d(x)
             * array([ 1,  2,  3, -7])
             *
             * >>> np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
             * array([-99,   1,   2,   3,  -7,  88,  99])
             *
             * The returned array is always 1D.
             *
             * >>> y = [[1, 2, 4], [1, 6, 24]]
             * >>> np.ediff1d(y)
             * array([ 1,  2, -3,  5, 18])
             */

            // force a 1d array
            ary = np.asanyarray(ary).ravel();

            // fast track default case
            if (to_begin is null && to_end is null)
            {
                return(ary.A("1:") - ary.A(":-1"));
            }

            int l_begin = 0;
            int l_end   = 0;

            if (to_begin is null)
            {
                l_begin = 0;
            }
            else
            {
                to_begin = np.asanyarray(to_begin).ravel();
                l_begin  = len(to_begin);
            }

            if (to_end == null)
            {
                l_end = 0;
            }
            else
            {
                to_end = np.asanyarray(to_end).ravel();
                l_end  = len(to_end);
            }

            // do the calculation in place and copy to_begin and to_end
            int     l_diff = Math.Max(len(ary) - 1, 0);
            ndarray result = np.empty(new shape(l_diff + l_begin + l_end), dtype: ary.Dtype);

            result = ary.__array_wrap__(result);
            if (l_begin > 0)
            {
                result[":" + l_begin.ToString()] = to_begin;
            }

            if (l_end > 0)
            {
                result[(l_begin + l_diff).ToString() + ":"] = to_end;
            }

            ndarray _out = result.A(l_begin.ToString() + ":" + (l_begin + l_diff).ToString());

            _out = np.subtract(ary.A("1:"), ary.A(":-1"));
            result[l_begin.ToString() + ":" + (l_begin + l_diff).ToString()] = _out;
            return(result);
        }