示例#1
0
        public static ndarray as_strided(ndarray x, object oshape = null, object ostrides = null, bool subok = false, bool writeable = false)
        {
            npy_intp[] shape   = null;
            npy_intp[] strides = null;

            if (oshape != null)
            {
                shape newshape = NumpyExtensions.ConvertTupleToShape(oshape);
                if (newshape == null)
                {
                    throw new Exception("Unable to convert shape object");
                }
                shape = newshape.iDims;
            }

            if (ostrides != null)
            {
                shape newstrides = NumpyExtensions.ConvertTupleToShape(ostrides);
                if (newstrides == null)
                {
                    throw new Exception("Unable to convert strides object");
                }
                strides = newstrides.iDims;
            }

            return(as_strided(x, shape, strides, subok, writeable));
        }
示例#2
0
        private static ndarray _broadcast_to(object oarray, object oshape, bool subok, bool _readonly)
        {
            shape newshape = NumpyExtensions.ConvertTupleToShape(oshape);

            if (newshape == null)
            {
                throw new Exception("Unable to convert shape object");
            }

            if (newshape.iDims == null || newshape.iDims.Length == 0)
            {
                newshape = new shape(asanyarray(oarray).shape);
            }

            ndarray array = np.array(asanyarray(oarray), copy: false, subok: subok);

            if (array.dims == null)
            {
                throw new ValueError("cannot broadcast a non-scalar to a scalar array");
            }

            if (np.anyb(np.array(newshape.iDims) < 0))
            {
                throw new ValueError("all elements of broadcast shape must be non-negative");
            }

            var toshape = NpyCoreApi.BroadcastToShape(array, newshape.iDims, newshape.iDims.Length);

            var newStrides = new npy_intp[toshape.nd_m1 + 1];

            Array.Copy(toshape.strides, 0, newStrides, 0, newStrides.Length);
            var result = np.as_strided(array, shape: newshape.iDims, strides: newStrides);

            return(result);
        }
示例#3
0
        /// <summary>
        /// An N-dimensional iterator object to index arrays.
        /// </summary>
        public ndindex(object oshape)
        {
            shape newshape = NumpyExtensions.ConvertTupleToShape(oshape);

            var x = np.as_strided(np.zeros(1), shape: newshape.iDims, strides: np.zeros_like(newshape.iDims, dtype: np.intp).ToArray <npy_intp>());

            core = new nditer(x);
        }
示例#4
0
        public static ndarray upscale_to(ndarray a, object oshape)
        {
            shape newshape = NumpyExtensions.ConvertTupleToShape(oshape);

            if (newshape == null)
            {
                throw new Exception("Unable to convert shape object");
            }

            if (!broadcastable(a, newshape.iDims, newshape.iDims.Length))
            {
                throw new Exception(string.Format("operands could not be broadcast together with shapes ({0}),({1})", a.shape.ToString(), newshape.ToString()));
            }

            ndarray ret = NpyCoreApi.NpyArray_UpscaleSourceArray(a, newshape);

            return(ret.reshape(newshape));
        }