Exemple #1
0
        internal static ndarray CheckFromAny(Object src, dtype descr, int minDepth,
                                             int maxDepth, NPYARRAYFLAGS requires, Object context)
        {
            if ((requires & NPYARRAYFLAGS.NPY_NOTSWAPPED) != 0)
            {
                if (descr == null && src is ndarray &&
                    !((ndarray)src).Dtype.IsNativeByteOrder)
                {
                    descr = new dtype(((ndarray)src).Dtype);
                }
                else if (descr != null && !descr.IsNativeByteOrder)
                {
                    // Descr replace
                }
                if (descr != null)
                {
                    descr.ByteOrder = '=';
                }
            }

            ndarray arr = np.FromAny(src, descr, minDepth, maxDepth, requires, context);

            if (arr != null && (requires & NPYARRAYFLAGS.NPY_ELEMENTSTRIDES) != 0 &&
                arr.ElementStrides == 0)
            {
                arr = arr.NewCopy(NPY_ORDER.NPY_ANYORDER);
            }
            return(arr);
        }
Exemple #2
0
        /*
         * Resize (reallocate data).  Only works if nothing else is referencing this
         * array and it is contiguous.  If refcheck is 0, then the reference count is
         * not checked and assumed to be 1.  You still must own this data and have no
         * weak-references and no base object.
         */
        internal static int NpyArray_Resize(NpyArray self, NpyArray_Dims newshape, bool refcheck, NPY_ORDER fortran)
        {
            npy_intp oldsize, newsize;
            int      new_nd = newshape.len, k, elsize;
            int      refcnt;

            npy_intp  [] new_dimensions = newshape.ptr;
            npy_intp []  new_strides    = new npy_intp[npy_defs.NPY_MAXDIMS];
            size_t       sd;

            npy_intp[] dimptr;
            npy_intp[] strptr;
            npy_intp   largest;

            if (!NpyArray_ISONESEGMENT(self))
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "resize only works on single-segment arrays");
                return(-1);
            }

            if (self.descr.elsize == 0)
            {
                NpyErr_SetString(npyexc_type.NpyExc_ValueError, "Bad data-type size.");
                return(-1);
            }
            newsize = 1;
            largest = npy_defs.NPY_MAX_INTP / self.descr.elsize;
            for (k = 0; k < new_nd; k++)
            {
                if (new_dimensions[k] == 0)
                {
                    break;
                }
                if (new_dimensions[k] < 0)
                {
                    NpyErr_SetString(npyexc_type.NpyExc_ValueError, "negative dimensions not allowed");
                    return(-1);
                }
                newsize *= new_dimensions[k];
                if (newsize <= 0 || newsize > largest)
                {
                    NpyErr_MEMORY();
                    return(-1);
                }
            }
            oldsize = NpyArray_SIZE(self);

            if (oldsize != newsize)
            {
                if (!((self.flags & NPYARRAYFLAGS.NPY_OWNDATA) != 0))
                {
                    NpyErr_SetString(npyexc_type.NpyExc_ValueError, "cannot resize this array: it does not own its data");
                    return(-1);
                }

                /* TODO: This isn't right for usage from C.  I think we
                 * need to revisit the refcounts so we don't have counts
                 * of 0. */
                if (refcheck)
                {
                    refcnt = (int)self.nob_refcnt;
                }
                else
                {
                    refcnt = 0;
                }
                if ((refcnt > 0) ||
                    (self.base_arr != null) || (null != self.base_obj))
                {
                    NpyErr_SetString(npyexc_type.NpyExc_ValueError, "cannot resize an array references or is referenced\nby another array in this way.  Use the resize function");
                    return(-1);
                }

                if (newsize == 0)
                {
                    sd = (size_t)self.descr.elsize;
                }
                else
                {
                    sd = (size_t)(newsize * self.descr.elsize);
                }
                /* Reallocate space if needed */
                VoidPtr new_data = NpyDataMem_RENEW(self.data, sd);
                if (new_data == null)
                {
                    NpyErr_MEMORY();
                    return(-1);
                }
                self.data = new_data;
            }

            if ((newsize > oldsize) && NpyArray_ISWRITEABLE(self))
            {
                /* Fill new memory with zeros */
                elsize = self.descr.elsize;
                memset(self.data + oldsize * elsize, 0, (newsize - oldsize) * elsize);
            }

            if (self.nd != new_nd)
            {
                /* Different number of dimensions. */
                self.nd = new_nd;
                /* Need new dimensions and strides arrays */
                dimptr = NpyDimMem_NEW(new_nd);
                strptr = NpyDimMem_NEW(new_nd);
                if (dimptr == null || strptr == null)
                {
                    NpyErr_MEMORY();
                    return(-1);
                }

                memcpy(dimptr, self.dimensions, self.nd * sizeof(npy_intp));
                memcpy(strptr, self.strides, self.nd * sizeof(npy_intp));
                self.dimensions = dimptr;
                self.strides    = strptr;
            }

            /* make new_strides variable */
            sd = (size_t)self.descr.elsize;

            NPYARRAYFLAGS flags = 0;

            sd = (size_t)npy_array_fill_strides(new_strides, new_dimensions, new_nd, sd, self.flags, ref flags); self.flags = flags;
            Array.Copy(new_dimensions, self.dimensions, new_nd);
            Array.Copy(new_strides, self.strides, new_nd);
            return(0);
        }
Exemple #3
0
        /*
         * Update Several Flags at once.
         */
        internal static void NpyArray_UpdateFlags(NpyArray ret, NPYARRAYFLAGS flagmask)
        {
            if ((flagmask & NPYARRAYFLAGS.NPY_FORTRAN) > 0)
            {
                if (_IsFortranContiguous(ret))
                {
                    ret.flags |= NPYARRAYFLAGS.NPY_FORTRAN;
                    if (ret.nd > 1)
                    {
                        ret.flags &= ~NPYARRAYFLAGS.NPY_CONTIGUOUS;
                    }
                }
                else
                {
                    ret.flags &= ~NPYARRAYFLAGS.NPY_FORTRAN;
                }
            }
            if ((flagmask & NPYARRAYFLAGS.NPY_CONTIGUOUS) > 0)
            {
                if (_IsContiguous(ret))
                {
                    ret.flags |= NPYARRAYFLAGS.NPY_CONTIGUOUS;
                    if (ret.nd > 1)
                    {
                        ret.flags &= ~NPYARRAYFLAGS.NPY_FORTRAN;
                    }
                }
                else
                {
                    ret.flags &= ~NPYARRAYFLAGS.NPY_CONTIGUOUS;
                }
            }
            if ((flagmask & NPYARRAYFLAGS.NPY_ALIGNED) > 0)
            {
                if (Npy_IsAligned(ret))
                {
                    ret.flags |= NPYARRAYFLAGS.NPY_ALIGNED;
                }
                else
                {
                    ret.flags &= ~NPYARRAYFLAGS.NPY_ALIGNED;
                }
            }

            /*
             * This is not checked by default WRITEABLE is not
             * part of UPDATE_ALL
             */
            if ((flagmask & NPYARRAYFLAGS.NPY_WRITEABLE) > 0)
            {
                if (Npy_IsWriteable(ret))
                {
                    ret.flags |= NPYARRAYFLAGS.NPY_WRITEABLE;
                }
                else
                {
                    ret.flags &= ~NPYARRAYFLAGS.NPY_WRITEABLE;
                }
            }
            return;
        }
 internal static bool NpyArray_FLAGSWAP(NpyArray arr, NPYARRAYFLAGS flags)
 {
     return(NpyArray_CHKFLAGS(arr, flags) && NpyArray_ISNOTSWAPPED(arr));
 }
 internal static NPYARRAYFLAGS NpyArray_FLAGS_OR(NpyArray arr, NPYARRAYFLAGS flag)
 {
     return(arr.flags |= flag);
 }
 internal static bool NpyArray_CHKFLAGS(NpyArray arr, NPYARRAYFLAGS FLAGS)
 {
     return((arr.flags & FLAGS) == FLAGS);
 }
Exemple #7
0
 private bool ChkFlags(NPYARRAYFLAGS check)
 {
     return((flags & check) == check);
 }
Exemple #8
0
        /// <summary>
        /// Constructs a new array from multiple input types, like lists, arrays, etc.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="descr"></param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <param name="requires"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        internal static ndarray FromAny(Object src, dtype descr = null, int minDepth     = 0,
                                        int maxDepth            = 0, NPYARRAYFLAGS flags = 0, Object context = null)
        {
            ndarray result = null;

            if (src == null)
            {
                return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT)));
            }

            Type t = src.GetType();

            if (t != typeof(PythonTuple))
            {
                if (src is ndarray)
                {
                    result = NpyCoreApi.FromArray((ndarray)src, descr, flags);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                if (t.IsArray)
                {
                    result = asanyarray(src);
                }

                dtype newtype = (descr ?? FindScalarType(src));
                if (descr == null && newtype != null)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromPythonScalar(src, newtype);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                //result = FromScalar(src, descr, context);
                if (result != null)
                {
                    if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0)
                    {
                        result = NpyCoreApi.FromArray(result, descr, flags);
                        return(FromAnyReturn(result, minDepth, maxDepth));
                    }
                }
            }

            bool is_object = false;

            if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
            {
                throw UpdateIfCopyError();
            }
            if (descr == null)
            {
                descr = FindArrayType(src, null);
            }
            else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT)
            {
                is_object = true;
            }

            if (result == null)
            {
                bool seq = false;
                if (src is IEnumerable <object> )
                {
                    try
                    {
                        result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth);
                        seq    = true;
                    }
                    catch (InsufficientMemoryException)
                    {
                        throw;
                    }
                    catch
                    {
                        if (is_object)
                        {
                            result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0);
                            seq    = true;
                        }
                    }
                }
                if (!seq)
                {
                    result = FromScalar(src, descr, null);
                }
            }
            return(FromAnyReturn(result, minDepth, maxDepth));
        }
Exemple #9
0
 internal static NpyArray NpyArray_FromArray(NpyArray arr, NpyArray_Descr newtype, NPYARRAYFLAGS flags)
 {
     return(numpyinternal.NpyArray_FromArray(arr, newtype, flags));
 }
Exemple #10
0
 internal static void NpyArray_UpdateFlags(NpyArray ret, NPYARRAYFLAGS flagmask)
 {
     numpyinternal.NpyArray_UpdateFlags(ret, flagmask);
 }
Exemple #11
0
 internal static NpyArray NpyArray_New(object subtype, int nd, npy_intp[] dims, NPY_TYPES type_num, npy_intp[] strides, VoidPtr data, int itemsize, NPYARRAYFLAGS flags, object obj)
 {
     return(numpyinternal.NpyArray_New(subtype, nd, dims, type_num, strides, data, itemsize, flags, obj));
 }
Exemple #12
0
 internal static NpyArray NpyArray_NewFromDescr(NpyArray_Descr descr, int nd, npy_intp[] dims, npy_intp[] strides, VoidPtr data,
                                                NPYARRAYFLAGS flags, bool ensureArray, object subtype, object interfaceData)
 {
     return(numpyinternal.NpyArray_NewFromDescr(descr, nd, dims, strides, data, flags, ensureArray, subtype, interfaceData));
 }
Exemple #13
0
 internal static NpyArray NpyArray_CheckAxis(NpyArray arr, ref int axis, NPYARRAYFLAGS flags)
 {
     return(numpyinternal.NpyArray_CheckAxis(arr, ref axis, flags));
 }
Exemple #14
0
 internal static NpyArray NpyArray_CheckFromArray(NpyArray arr, NpyArray_Descr descr, NPYARRAYFLAGS requires)
 {
     return(numpyinternal.NpyArray_CheckFromArray(arr, descr, requires));
 }
Exemple #15
0
        /// <summary>
        /// Constructs a new array from multiple input types, like lists, arrays, etc.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="descr"></param>
        /// <param name="minDepth"></param>
        /// <param name="maxDepth"></param>
        /// <param name="requires"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ndarray FromAny(Object src, dtype descr = null, int minDepth     = 0,
                                      int maxDepth            = 0, NPYARRAYFLAGS flags = 0, Object context = null)
        {
            ndarray result = null;

            if (src == null)
            {
                return(np.empty(new shape(0), NpyCoreApi.DescrFromType(NPY_TYPES.NPY_OBJECT)));
            }

            Type t = src.GetType();

            if (t != typeof(PythonTuple))
            {
                if (src is ndarray)
                {
                    result = NpyCoreApi.FromArray((ndarray)src, descr, flags);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                if (t.IsArray)
                {
                    result = asanyarray(src);
                }

                if (src is ScalarGeneric)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromScalar((ScalarGeneric)src, descr);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                dtype newtype = (descr ?? FindScalarType(src));
                if (descr == null && newtype != null)
                {
                    if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
                    {
                        throw UpdateIfCopyError();
                    }
                    result = FromPythonScalar(src, newtype);
                    return(FromAnyReturn(result, minDepth, maxDepth));
                }

                //result = FromScalar(src, descr, context);
                if (result != null)
                {
                    if (descr != null && !NpyCoreApi.EquivTypes(descr, result.Dtype) || flags != 0)
                    {
                        result = NpyCoreApi.FromArray(result, descr, flags);
                        return(FromAnyReturn(result, minDepth, maxDepth));
                    }
                }
            }

            bool is_object = false;

            if ((flags & NPYARRAYFLAGS.NPY_UPDATEIFCOPY) != 0)
            {
                throw UpdateIfCopyError();
            }
            if (descr == null)
            {
                descr = FindArrayType(src, null);
            }
            else if (descr.TypeNum == NPY_TYPES.NPY_OBJECT)
            {
                is_object = true;
            }

            if (result == null)
            {
                // Hack required because in C# strings are enumerations of chars, not objects.
                // However, we want to keep src as a string if we are building a string or object array.
                if (!is_object &&
                    (descr.TypeNum != NPY_TYPES.NPY_STRING || descr.Type == NPY_TYPECHAR.NPY_CHARLTR) &&
                    descr.TypeNum != NPY_TYPES.NPY_UNICODE && src is string && ((string)src).Length > 1)
                {
                    src = ((string)src).Cast <object>();
                }

                bool seq = false;
                if (src is IEnumerable <object> )
                {
                    try
                    {
                        result = FromIEnumerable((IEnumerable <object>)src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0, minDepth, maxDepth);
                        seq    = true;
                    }
                    catch (InsufficientMemoryException)
                    {
                        throw;
                    }
                    catch
                    {
                        if (is_object)
                        {
                            result = FromNestedList(src, descr, (flags & NPYARRAYFLAGS.NPY_FORTRAN) != 0);
                            seq    = true;
                        }
                    }
                }
                if (!seq)
                {
                    result = FromScalar(src, descr, null);
                }
            }
            return(FromAnyReturn(result, minDepth, maxDepth));
        }