Пример #1
0
        private static void AssignFromSeq(IEnumerable <Object> seq, ndarray result,
                                          int dim, long offset)
        {
            if (dim >= result.ndim)
            {
                throw new RuntimeException(String.Format("Source dimensions ({0}) exceeded target array dimensions ({1}).", dim, result.ndim));
            }

            if (seq is ndarray && seq.GetType() != typeof(ndarray))
            {
                // Convert to an array to ensure the dimensionality reduction
                // assumption works.
                ndarray array = NpyCoreApi.FromArray((ndarray)seq, null, NPYARRAYFLAGS.NPY_ENSUREARRAY);
                seq = (IEnumerable <object>)array;
            }

            if (seq.Count() != result.Dim(dim))
            {
                throw new RuntimeException("AssignFromSeq: sequence/array shape mismatch.");
            }

            long stride = result.Stride(dim);

            if (dim < result.ndim - 1)
            {
                // Sequence elements should be additional sequences
                seq.Iteri((o, i) =>
                          AssignFromSeq((IEnumerable <Object>)o, result, dim + 1, offset + stride * i));
            }
            else
            {
                seq.Iteri((o, i) => result.Dtype.f.setitem(offset + i * stride, o, result.Array));
            }
        }
Пример #2
0
        internal static void SetField(ndarray dest, NpyArray_Descr descr, int offset, object src)
        {
            // For char arrays pad the input string.
            if (dest.Dtype.Type == NPY_TYPECHAR.NPY_CHARLTR &&
                dest.ndim > 0 && src is String)
            {
                int ndimNew = (int)dest.Dim(dest.ndim - 1);
                int ndimOld = ((String)src).Length;

                if (ndimNew > ndimOld)
                {
                    src = ((String)src).PadRight(ndimNew, ' ');
                }
            }
            ndarray srcArray;

            if (src is ndarray)
            {
                srcArray = (ndarray)src;
            }
            else if (false)
            {
                // TODO: Not handling scalars.  See arrayobject.c:111
            }
            else
            {
                dtype src_dtype = new dtype(descr);
                srcArray = np.FromAny(src, src_dtype, 0, dest.ndim, NPYARRAYFLAGS.NPY_CARRAY, null);
            }
            NpyCoreApi.Incref(descr);
            if (numpyAPI.NpyArray_SetField(dest.core, descr, offset, srcArray.core) < 0)
            {
                NpyCoreApi.CheckError();
            }
        }
Пример #3
0
        /// <summary>
        /// Copies the source object into the destination array.  src can be
        /// any type so long as the number of elements matches dest.  In the
        /// case of strings, they will be padded with spaces if needed but
        /// can not be longer than the number of elements in dest.
        /// </summary>
        /// <param name="dest">Destination array</param>
        /// <param name="src">Source object</param>
        public static void CopyObject(ndarray dest, Object src)
        {
            // For char arrays pad the input string.
            if (dest.Dtype.Type == NPY_TYPECHAR.NPY_CHARLTR &&
                dest.ndim > 0 && src is String)
            {
                int ndimNew = (int)dest.Dim(dest.ndim - 1);
                int ndimOld = ((String)src).Length;

                if (ndimNew > ndimOld)
                {
                    src = ((String)src).PadRight(ndimNew, ' ');
                }
            }

            ndarray srcArray;

            if (src is ndarray)
            {
                srcArray = (ndarray)src;
            }
            else if (false)
            {
                // TODO: Not handling scalars.  See arrayobject.c:111
            }
            else
            {
                srcArray = np.FromAny(src, dest.Dtype, 0, dest.ndim, 0, null);
            }
            NpyCoreApi.MoveInto(dest, srcArray);
        }
Пример #4
0
        /// <summary>
        /// Return the indices to access the main diagonal of an n - dimensional array.
        /// </summary>
        /// <param name="arr">array, at least 2 - D</param>
        /// <returns></returns>
        public static ndarray[] diag_indices_from(ndarray arr)
        {
            //Return the indices to access the main diagonal of an n - dimensional array.

            //See `diag_indices` for full details.


            //Parameters
            //----------

            //arr : array, at least 2 - D


            //See Also
            //--------

            //diag_indices

            if (arr.ndim < 2)
            {
                throw new ValueError("input array must be at least 2-d");
            }

            // For more than d=2, the strided formula is only valid for arrays with
            // all dimensions equal, so we check first.

            var FirstDim = arr.Dim(0);

            for (int i = 1; i < arr.ndim; i++)
            {
                if (arr.Dim(i) != FirstDim)
                {
                    throw new ValueError("All dimensions of input must be of equal length");
                }
            }

            return(diag_indices((int)arr.Dim(0), arr.ndim));
        }
Пример #5
0
        internal static ndarray PrependOnes(ndarray arr, int nd, int ndmin)
        {
            npy_intp[] newdims    = new npy_intp[ndmin];
            npy_intp[] newstrides = new npy_intp[ndmin];
            int        num        = ndmin - nd;

            // Set the first num dims and strides for the 1's
            for (int i = 0; i < num; i++)
            {
                newdims[i]    = (npy_intp)1;
                newstrides[i] = (npy_intp)arr.ItemSize;
            }
            // Copy in the rest of dims and strides
            for (int i = num; i < ndmin; i++)
            {
                int k = i - num;
                newdims[i]    = (npy_intp)arr.Dim(k);
                newstrides[i] = (npy_intp)arr.Stride(k);
            }

            return(NpyCoreApi.NewView(arr.Dtype, ndmin, newdims, newstrides, arr, 0, false));
        }
Пример #6
0
        private Slice ParseIndexString(ndarray arr, int index, object s, bool UseLiteralRanges)
        {
            npy_intp startingIndex = 0;
            npy_intp endingIndex   = 0;
            npy_intp step          = 0;

            //https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
            if (s is int)
            {
                int?i = s as int?;

                if (!i.HasValue)
                {
                    return(null);
                }
                startingIndex = i.Value;
                if (UseLiteralRanges)
                {
                    endingIndex = startingIndex + 1;
                }
                else
                {
                    endingIndex = arr.Dim(index);
                }
                step = 1;
            }
            else
            if (s is string)
            {
                string   ss = s as String;
                npy_intp i  = 0;
                npy_intp j  = arr.Dim(index);
                npy_intp k  = 1;

                // check if this is a CSharpTuple
                if (ss.StartsWith("[") && ss.EndsWith("]"))
                {
                    return(null);
                }

                string[] Parts = ss.Split(':');
                if (Parts.Length > 3)
                {
                    return(null);
                }

                if (Parts.Length == 3)
                {
                    int kt;
                    if (int.TryParse(Parts[2], out kt))
                    {
                        k = kt;
                        if (k < 0)
                        {
                            i = arr.Dim(index) - 1;
                            j = -arr.Dim(index) - 1;
                        }
                        else
                        {
                            i = 0;
                            j = arr.Dim(index);
                        }
                    }
                }
                if (Parts.Length >= 2)
                {
                    int jt;
                    if (int.TryParse(Parts[1], out jt))
                    {
                        if (jt < 0)
                        {
                            j = arr.Dim(index) + jt;
                        }
                        else
                        {
                            j = jt;
                        }
                    }
                }
                if (Parts.Length >= 1)
                {
                    int it;
                    if (int.TryParse(Parts[0], out it))
                    {
                        if (it < 0)
                        {
                            i = arr.Dim(index) + it;
                        }
                        else
                        {
                            i = it;
                        }
                        if (Parts.Length == 1 && UseLiteralRanges)
                        {
                            j = i + 1;
                        }
                    }
                }

                if (ss == ":" || ss == "::")
                {
                    i = 0;
                    j = arr.Dim(index);
                    k = 1;
                }

                startingIndex = i;
                endingIndex   = j;
                step          = k;
            }

            return(new Slice(startingIndex, endingIndex, step));
        }