Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public static ndarray reshape(this ndarray a, object oshape, NPY_ORDER order = NPY_ORDER.NPY_CORDER)
        {
            shape newshape = ConvertTupleToShape(oshape);

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

            return(np.reshape(a, newshape, order));
        }
Exemplo n.º 5
0
        /// <summary>
        /// returns true of the shapes are equivalent
        /// </summary>
        /// <param name="o2"></param>
        /// <returns></returns>
        public override bool Equals(object o2)
        {
            shape s2 = o2 as shape;

            if (this.iDims.Length == s2.iDims.Length)
            {
                for (int i = 0; i < this.iDims.Length; i++)
                {
                    if (this.iDims[i] != s2.iDims[i])
                    {
                        return(false);
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
 /// <summary>
 /// create a new shape, copied from existing shape
 /// </summary>
 /// <param name="s"></param>
 public shape(shape s)
 {
     iDims = new npy_intp[s.iDims.Length];
     Array.Copy(s.iDims, iDims, s.iDims.Length);
 }
Exemplo n.º 8
0
 public static ndarray reshape(this ndarray a, shape newshape, NPY_ORDER order = NPY_ORDER.NPY_CORDER)
 {
     return(np.reshape(a, newshape, order));
 }