Example #1
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);
        }