예제 #1
0
        internal static ndarray FromPythonScalar(object src, dtype descr)
        {
            int       itemsize = descr.ElementSize;
            NPY_TYPES type     = descr.TypeNum;

            if (itemsize == 0 && NpyDefs.IsExtended(type))
            {
                int n = PythonOps.Length(src);
                if (type == NPY_TYPES.NPY_UNICODE)
                {
                    n *= 4;
                }
                descr             = new dtype(descr);
                descr.ElementSize = n;
            }

            ndarray result = NpyCoreApi.AllocArray(descr, 0, null, false);

            if (result.ndim > 0)
            {
                throw new ArgumentException("shape-mismatch on array construction");
            }

            result.Dtype.f.setitem(0, src, result.Array);
            return(result);
        }
예제 #2
0
        private static ndarray FromScalar(object src, dtype descr, object context)
        {
            npy_intp[] dims = new npy_intp[1] {
                1
            };
            ndarray result = NpyCoreApi.AllocArray(descr, 1, dims, false);

            if (result.ndim != 1)
            {
                throw new ArgumentException("shape-mismatch on array construction");
            }

            result.Dtype.f.setitem(0, src, result.Array);
            return(result);
        }
예제 #3
0
        internal static ndarray FromNestedList(object src, dtype descr, bool fortran)
        {
            npy_intp[] dims = new npy_intp[NpyDefs.NPY_MAXDIMS];


            int nd = ObjectDepthAndDimension(src, dims, 0, NpyDefs.NPY_MAXDIMS);

            if (nd == 0)
            {
                return(FromPythonScalar(src, descr));
            }
            ndarray result = NpyCoreApi.AllocArray(descr, nd, dims, fortran);

            AssignToArray(src, result);
            return(result);
        }
예제 #4
0
        /// <summary>
        /// Builds an array from a sequence of objects.  The elements of the sequence
        /// can also be sequences in which case this function recursively walks the
        /// nested sequences and builds an n dimentional array.
        ///
        /// IronPython tuples and lists work as sequences.
        /// </summary>
        /// <param name="src">Input sequence</param>
        /// <param name="descr">Desired array element type or null to determine automatically</param>
        /// <param name="fortran">True if array should be Fortran layout, false for C</param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <returns>New array instance</returns>
        internal static ndarray FromIEnumerable(IEnumerable <Object> src, dtype descr,
                                                bool fortran, int minDepth, int maxDepth)
        {
            ndarray result = null;


            if (descr == null)
            {
                descr = FindArrayType(src, null, NpyDefs.NPY_MAXDIMS);
            }

            int itemsize = descr.ElementSize;

            NPY_TYPES type         = descr.TypeNum;
            bool      checkIt      = true;
            bool      stopAtString =
                type != NPY_TYPES.NPY_STRING ||
                descr.Type == (char)NPY_TYPECHAR.NPY_STRINGLTR;
            bool stopAtTuple = false;

            int numDim = DiscoverDepth(src, NpyDefs.NPY_MAXDIMS + 1, stopAtString, stopAtTuple);

            if (numDim == 0)
            {
                return(FromPythonScalar(src, descr));
            }
            else
            {
                if (maxDepth > 0 && type == NPY_TYPES.NPY_OBJECT &&
                    numDim > maxDepth)
                {
                    numDim = maxDepth;
                }
                if (maxDepth > 0 && numDim > maxDepth ||
                    minDepth > 0 && numDim < minDepth)
                {
                    throw new ArgumentException("Invalid number of dimensions.");
                }

                npy_intp[] dims = new npy_intp[numDim];
                DiscoverDimensions(src, numDim, dims, 0, checkIt);

                result = NpyCoreApi.AllocArray(descr, numDim, dims, fortran);
                AssignToArray(src, result);
            }
            return(result);
        }
예제 #5
0
        private static ndarray Concatenate(IEnumerable <ndarray> arrays, int?axis = 0)
        {
            int i;

            bool MustFlatten = false;

            if (axis == null)
            {
                MustFlatten = true;
                axis        = 0;
            }

            try
            {
                arrays.First();
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException("concatenation of zero-length sequence is impossible");
            }

            ndarray[] mps = NpyUtil_ArgProcessing.ConvertToCommonType(arrays);
            int       n   = mps.Length;

            if (MustFlatten)
            {
                // Flatten the arrays
                for (i = 0; i < n; i++)
                {
                    mps[i] = mps[i].Ravel(NPY_ORDER.NPY_CORDER);
                }
                axis = 0;
            }
            else if (axis != 0)
            {
                // Swap to make the axis 0
                for (i = 0; i < n; i++)
                {
                    mps[i] = NpyCoreApi.FromArray(mps[i].SwapAxes(axis.Value, 0), null, NPYARRAYFLAGS.NPY_C_CONTIGUOUS);
                }
            }


            npy_intp[] dims = mps[0].dims;
            if (dims.Length == 0)
            {
                throw new ArgumentException("0-d arrays can't be concatenated");
            }

            npy_intp new_dim = dims[0];

            for (i = 1; i < n; i++)
            {
                npy_intp[] dims2 = mps[i].dims;
                if (dims.Length != dims2.Length)
                {
                    throw new ArgumentException("arrays must have same number of dimensions");
                }
                bool eq = Enumerable.Zip(dims.Skip(1), dims2.Skip(1), (a1, b) => (a1 == b)).All(x => x);
                if (!eq)
                {
                    throw new ArgumentException("array dimensions do not agree");
                }
                new_dim += dims2[0];
            }
            dims[0] = new_dim;


            ndarray result = NpyCoreApi.AllocArray(mps[0].Dtype, dims.Length, dims, false);

            if (NpyCoreApi.CombineInto(result, mps) < 0)
            {
                throw new ArgumentException("unable to concatenate these arrays");
            }

            if (0 < axis && axis < NpyDefs.NPY_MAXDIMS || axis < 0)
            {
                result = result.SwapAxes(axis.Value, 0);
            }


            return(result);
        }
예제 #6
0
        /// <summary>
        /// Builds an array from a sequence of objects.  The elements of the sequence
        /// can also be sequences in which case this function recursively walks the
        /// nested sequences and builds an n dimentional array.
        ///
        /// IronPython tuples and lists work as sequences.
        /// </summary>
        /// <param name="src">Input sequence</param>
        /// <param name="descr">Desired array element type or null to determine automatically</param>
        /// <param name="fortran">True if array should be Fortran layout, false for C</param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <returns>New array instance</returns>
        internal static ndarray FromIEnumerable(IEnumerable <Object> src, dtype descr,
                                                bool fortran, int minDepth, int maxDepth)
        {
            ndarray result = null;


            if (descr == null)
            {
                descr = FindArrayType(src, null, NpyDefs.NPY_MAXDIMS);
            }

            int itemsize = descr.ElementSize;

            NPY_TYPES type         = descr.TypeNum;
            bool      checkIt      = (descr.Type != NPY_TYPECHAR.NPY_CHARLTR);
            bool      stopAtString =
                type != NPY_TYPES.NPY_STRING ||
                descr.Type == NPY_TYPECHAR.NPY_STRINGLTR;
            bool stopAtTuple =
                type == NPY_TYPES.NPY_VOID &&
                (descr.HasNames || descr.HasSubarray);

            int numDim = DiscoverDepth(src, NpyDefs.NPY_MAXDIMS + 1, stopAtString, stopAtTuple);

            if (numDim == 0)
            {
                return(FromPythonScalar(src, descr));
            }
            else
            {
                if (maxDepth > 0 && type == NPY_TYPES.NPY_OBJECT &&
                    numDim > maxDepth)
                {
                    numDim = maxDepth;
                }
                if (maxDepth > 0 && numDim > maxDepth ||
                    minDepth > 0 && numDim < minDepth)
                {
                    throw new ArgumentException("Invalid number of dimensions.");
                }

                npy_intp[] dims = new npy_intp[numDim];
                DiscoverDimensions(src, numDim, dims, 0, checkIt);
                if (descr.Type == NPY_TYPECHAR.NPY_CHARLTR &&
                    numDim > 0 && dims[numDim - 1] == 1)
                {
                    numDim--;
                }

                if (itemsize == 0 && NpyDefs.IsExtended(descr.TypeNum))
                {
                    itemsize = DiscoverItemsize(src, numDim, 0);
                    if (descr.TypeNum == NPY_TYPES.NPY_UNICODE)
                    {
                        itemsize *= 4;
                    }
                    descr             = new dtype(descr);
                    descr.ElementSize = itemsize;
                }

                result = NpyCoreApi.AllocArray(descr, numDim, dims, fortran);
                AssignToArray(src, result);
            }
            return(result);
        }