Пример #1
0
        public static dtype result_type(NPY_TYPES type_num)
        {
            var   return_type  = DefaultArrayHandlers.GetArrayHandler(type_num).MathOpFloatingType(UFuncOperation.divide);
            dtype result_dtype = NpyCoreApi.DescrFromType(return_type);

            return(result_dtype);
        }
Пример #2
0
        public void NpyArray_FillWithScalar_TestDouble()
        {
            int DataSize = 0;


            NPY_TYPES num_type = NPY_TYPES.NPY_DOUBLE;

            var SimpleArray = Common.GetSimpleArray(num_type, ref DataSize);

            Assert.AreEqual(num_type, SimpleArray.ItemType);

            Common.ArrayDataAdjust = 5;
            var ScalarArray = Common.GetSimpleArray(num_type, ref DataSize);

            Assert.AreEqual(num_type, ScalarArray.ItemType);

            var result = numpyAPI.NpyArray_FillWithScalar(SimpleArray, ScalarArray);

            Assert.AreEqual(0, result);


            double[] simpleData = (double[])SimpleArray.data.datap;
            double[] scalarData = (double[])ScalarArray.data.datap;

            Assert.IsTrue(Common.CompareArrays(simpleData, scalarData));
        }
Пример #3
0
        public void NpyArray_CheckFromArray_Test()
        {
            var NPY_TYPES_Values = Enum.GetValues(typeof(NPY_TYPES));

            NPY_TYPES targetType = NPY_TYPES.NPY_INT32;

            foreach (NPY_TYPES srcType in NPY_TYPES_Values)
            {
                int ExpectedSize = Common.GetDefaultItemSize(srcType);
                if (ExpectedSize < 0)
                {
                    continue;
                }



                int DataSize  = 0;
                var DataArray = Common.GetSimpleArray(srcType, ref DataSize, 0, true, false);


                var srcDescr = numpyAPI.NpyArray_DescrNewFromType(srcType);

                var CheckArray = numpyAPI.NpyArray_CheckFromArray(DataArray, srcDescr, NPYARRAYFLAGS.NPY_CARRAY | NPYARRAYFLAGS.NPY_ENSUREARRAY | NPYARRAYFLAGS.NPY_ENSURECOPY);

                Assert.IsNotNull(CheckArray);

                Assert.AreEqual(srcType, CheckArray.ItemType);

                if (srcType != NPY_TYPES.NPY_BOOL)
                {
                    Assert.IsTrue(Common.CompareArrays(CheckArray, DataArray));
                }
            }
        }
Пример #4
0
 internal static int NpyArray_RegisterCastFunc(NpyArray_Descr descr, NPY_TYPES totype, NpyArray_VectorUnaryFunc castfunc)
 {
     if (totype < NPY_TYPES.NPY_NTYPES)
     {
         descr.f.cast[(int)totype] = castfunc;
         return(0);
     }
     if (!NpyTypeNum_ISUSERDEF(totype))
     {
         NpyErr_SetString(npyexc_type.NpyExc_TypeError, "invalid type number.");
         return(-1);
     }
     if (descr.f.castfuncs == null)
     {
         descr.f.castfuncs = new List <NpyArray_CastFuncsItem>();
         if (descr.f.castfuncs == null)
         {
             return(-1);
         }
     }
     descr.f.castfuncs.Add(new NpyArray_CastFuncsItem()
     {
         castfunc = castfunc, totype = totype
     });
     return(0);
 }
Пример #5
0
        private void ConvertToDesiredArrayType_Test(NPY_TYPES targetType)
        {
            NpyArray SimpleArray = null;

            var NPY_TYPES_Values = Enum.GetValues(typeof(NPY_TYPES));

            foreach (NPY_TYPES desiredType in NPY_TYPES_Values)
            {
                int ExpectedSize = Common.GetDefaultItemSize(desiredType);
                if (ExpectedSize < 0)
                {
                    continue;
                }

                int DataSize = 0;
                SimpleArray = Common.GetSimpleArray(targetType, ref DataSize);

                DataSize *= Common.GetDefaultItemSize(targetType);

                VoidPtr nvp1 = numpyAPI.ConvertToDesiredArrayType(SimpleArray.data, 0, DataSize, desiredType);
                VoidPtr nvp2 = numpyAPI.ConvertToDesiredArrayType(nvp1, 0, DataSize, targetType);

                if (desiredType != NPY_TYPES.NPY_BOOL && desiredType != NPY_TYPES.NPY_DECIMAL && targetType != NPY_TYPES.NPY_DECIMAL)
                {
                    Assert.IsTrue(Common.CompareArrays(SimpleArray.data, nvp2));
                }
            }

            return;
        }
Пример #6
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);
        }
Пример #7
0
        internal static bool NpyArray_EquivTypes(NpyArray_Descr typ1, NpyArray_Descr typ2)
        {
            NPY_TYPES typenum1 = typ1.type_num;
            NPY_TYPES typenum2 = typ2.type_num;
            int       size1    = typ1.elsize;
            int       size2    = typ2.elsize;

            if (size1 != size2)
            {
                return(false);
            }
            if (NpyArray_ISNBO(typ1) != NpyArray_ISNBO(typ2))
            {
                return(false);
            }
            if (null != typ1.subarray || null != typ2.subarray)
            {
                return(typenum1 == typenum2 && _equivalent_subarrays(typ1.subarray, typ2.subarray));
            }

            if (typenum1 == NPY_TYPES.NPY_VOID || typenum2 == NPY_TYPES.NPY_VOID)
            {
                return((typenum1 == typenum2) &&
                       _equivalent_fields(typ1.fields, typ2.fields));
            }
            if (typenum1 == NPY_TYPES.NPY_DATETIME ||
                typenum1 == NPY_TYPES.NPY_DATETIME ||
                typenum2 == NPY_TYPES.NPY_TIMEDELTA ||
                typenum2 == NPY_TYPES.NPY_TIMEDELTA)
            {
                return((typenum1 == typenum2) &&
                       _equivalent_units(typ1.dtinfo, typ2.dtinfo));
            }
            return(typ1.kind == typ2.kind);
        }
Пример #8
0
 internal static int NpyArray_RegisterCanCast(NpyArray_Descr descr, NPY_TYPES totype, NPY_SCALARKIND scalar)
 {
     if (scalar == NPY_SCALARKIND.NPY_NOSCALAR)
     {
         /*
          * register with cancastto
          * These lists won't be freed once created
          * -- they become part of the data-type
          */
         if (descr.f.cancastto == null)
         {
             descr.f.cancastto = new List <NPY_TYPES>();
             descr.f.cancastto.Add(NPY_TYPES.NPY_NOTYPE);
         }
         descr.f.cancastto.Add(totype);
     }
     else
     {
         /* register with cancastscalarkindto */
         if (descr.f.cancastscalarkindto == null)
         {
             descr.f.cancastscalarkindto = new Dictionary <NPY_SCALARKIND, object>();
             for (int i = 0; i < npy_defs.NPY_NSCALARKINDS; i++)
             {
                 descr.f.cancastscalarkindto.Add((NPY_SCALARKIND)i, null);
             }
         }
         descr.f.cancastscalarkindto[scalar] = totype;
     }
     return(0);
 }
Пример #9
0
 /// <summary>
 /// Returns the array of functions appropriate to a given type.  The actual
 /// functions in the array will vary with the type sizes in the native code.
 /// </summary>
 /// <param name="t">Native array type</param>
 /// <returns>Functions matching that type</returns>
 internal static ArrFuncs FuncsForType(NPY_TYPES t)
 {
     if (ArrFuncs == null)
     {
         InitArrFuncs();
     }
     return(ArrFuncs[(int)t]);
 }
Пример #10
0
        internal static bool NpyTypeNum_ISCOMPLEX(NPY_TYPES type)
        {
            if (type == NPY_TYPES.NPY_COMPLEX)
            {
                return(true);
            }

            return(false);
        }
Пример #11
0
        internal static bool NpyTypeNum_ISFLEXIBLE(NPY_TYPES type)
        {
            if ((type >= NPY_TYPES.NPY_STRING) && (type <= NPY_TYPES.NPY_VOID))
            {
                return(true);
            }

            return(false);
        }
Пример #12
0
        internal static bool NpyTypeNum_ISCOMPLEX(NPY_TYPES type)
        {
            if ((type >= NPY_TYPES.NPY_CFLOAT) && (type <= NPY_TYPES.NPY_CLONGDOUBLE))
            {
                return(true);
            }

            return(false);
        }
Пример #13
0
        internal static bool NpyTypeNum_ISUSERDEF(NPY_TYPES type)
        {
            if ((type >= NPY_TYPES.NPY_USERDEF) && (type < NPY_TYPES.NPY_USERDEF + numpyinternal.NpyArray_GetNumusertypes()))
            {
                return(true);
            }

            return(false);
        }
Пример #14
0
        internal static bool NpyTypeNum_ISINTEGER(NPY_TYPES type)
        {
            if ((type >= NPY_TYPES.NPY_BYTE) && (type <= NPY_TYPES.NPY_ULONGLONG))
            {
                return(true);
            }

            return(false);
        }
Пример #15
0
        internal static bool NpyTypeNum_ISNUMBER(NPY_TYPES type)
        {
            if (type <= NPY_TYPES.NPY_CLONGDOUBLE)
            {
                return(true);
            }

            return(false);
        }
Пример #16
0
        public void Npy_IsWriteable_Test1()
        {
            NPY_TYPES type = NPY_TYPES.NPY_INT32;

            int DataSize = 0;

            bool b1 = numpyAPI.Npy_IsWriteable(Common.GetOneSegmentArray(type, ref DataSize));
            bool b2 = numpyAPI.Npy_IsWriteable(Common.GetSimpleArray(type, ref DataSize));
        }
Пример #17
0
        internal static bool NpyArray_CanCastTo(NpyArray_Descr from, NpyArray_Descr to)
        {
            NPY_TYPES fromtype = from.type_num;
            NPY_TYPES totype   = to.type_num;
            bool      ret;

            ret = NpyArray_CanCastSafely(fromtype, totype);
            return(ret);
        }
Пример #18
0
        internal static bool NpyTypeNum_ISDATETIME(NPY_TYPES type)
        {
            if ((type >= NPY_TYPES.NPY_DATETIME) && (type <= NPY_TYPES.NPY_TIMEDELTA))
            {
                return(true);
            }

            return(false);
        }
Пример #19
0
        public NpyArray_Descr(NPY_TYPES type_num)
        {
            f             = numpyinternal.GetArrFuncs(type_num);
            byteorder     = numpyinternal.NPY_NATIVE;
            this.type_num = type_num;

            this.elsize = DefaultArrayHandlers.GetArrayHandler(type_num).ItemSize;

            this.f.nonzero = DefaultArrayHandlers.GetArrayHandler(type_num).NonZero;
        }
Пример #20
0
        internal static NpyArray_Descr _get_builtin_descrs(NPY_TYPES type)
        {
            var ret = _builtin_descrs[(int)type];

            if (ret == null)
            {
                throw new Exception(string.Format("the type '{0}' is not registered as a built in descriptor", type.ToString()));
            }
            return(ret);
        }
Пример #21
0
        private static ndarray ndArrayFromMD(Array ssrc, NPY_TYPES type_num, int ndim)
        {
            npy_intp [] newshape = new npy_intp[ndim];
            for (int i = 0; i < ndim; i++)
            {
                newshape[i] = ssrc.GetLength(i);
            }

            return(np.array(new VoidPtr(ArrayFromMD(ssrc, type_num), type_num), null).reshape(newshape));
        }
Пример #22
0
        /// <summary>
        /// Returns the descriptor for a given native type or null if src is
        /// not a scalar type
        /// </summary>
        /// <param name="src">Object to type</param>
        /// <returns>Descriptor for type of 'src' or null if not scalar</returns>
        internal static dtype FindScalarType(Object src)
        {
            NPY_TYPES type = DefaultArrayHandlers.GetArrayType(src);

            if (type != NPY_TYPES.NPY_NOTSET)
            {
                return(NpyCoreApi.DescrFromType(type));
            }
            return(null);
        }
Пример #23
0
        internal static NpyArray_Descr NpyArray_DescrNewFromType(NPY_TYPES type_num)
        {
            NpyArray_Descr old;
            NpyArray_Descr _new;

            old  = NpyArray_DescrFromType(type_num);
            _new = NpyArray_DescrNew(old);
            Npy_DECREF(old);
            return(_new);
        }
Пример #24
0
        public static bool IsDecimal(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_DECIMAL:
                return(true);

            default:
                return(false);
            }
        }
Пример #25
0
        public static bool IsFlexible(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_STRING:
                return(true);

            default:
                return(false);
            }
        }
Пример #26
0
        public static bool IsBigInt(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_BIGINT:
                return(true);

            default:
                return(false);
            }
        }
Пример #27
0
        public static bool IsComplex(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_COMPLEX:
                return(true);

            default:
                return(false);
            }
        }
Пример #28
0
        internal static bool NpyTypeNum_ISFLEXIBLE(NPY_TYPES type)
        {
            switch (type)
            {
            //case NPY_TYPES.NPY_STRING:
            //    return true;

            default:
                return(false);
            }
        }
Пример #29
0
        internal static bool NpyTypeNum_ISOBJECT(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_OBJECT:
                return(true);

            default:
                return(false);
            }
        }
Пример #30
0
        internal static bool IsString(NPY_TYPES type)
        {
            switch (type)
            {
            case NPY_TYPES.NPY_STRING:
                return(true);

            default:
                return(false);
            }
        }