コード例 #1
0
 public bool Equals(dtype other)
 {
     if (other == null)
     {
         return(false);
     }
     return(this.core == other.core || NpyCoreApi.EquivTypes(this, other));
 }
コード例 #2
0
ファイル: NpyArray.cs プロジェクト: lulzzz/numpy.net
        internal static ndarray FromScalar(ScalarGeneric scalar, dtype descr = null)
        {
            ndarray arr = scalar.ToArray();

            if (descr != null && !NpyCoreApi.EquivTypes((dtype)scalar.dtype, descr))
            {
                arr = NpyCoreApi.CastToType(arr, descr, arr.IsFortran);
            }
            return(arr);
        }
コード例 #3
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));
        }
コード例 #4
0
ファイル: NpyArray.cs プロジェクト: lulzzz/numpy.net
        /// <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));
        }