Exemplo n.º 1
0
        private static void ConvertSingleIndex(ndarray arr, Object arg, NpyIndexes indexes, int index)
        {
            if (arg == null)
            {
                indexes.AddNewAxis();
            }
            else if (arg is string && (string)arg == "...")
            {
                indexes.AddEllipsis();
            }
            else if (arg is Ellipsis)
            {
                indexes.AddEllipsis();
            }
            else if (arg is CSharpTuple)
            {
                indexes.AddCSharpTuple((CSharpTuple)arg);
            }
            else if (arg is bool)
            {
                indexes.AddIndex((bool)arg);
            }
            else if (arg is int)
            {
                indexes.AddIndex((int)arg);
            }
            else if (arg is BigInteger)
            {
                BigInteger bi   = (BigInteger)arg;
                npy_intp   lval = (npy_intp)bi;
                indexes.AddIndex(lval);
            }
            else if (arg is ISlice)
            {
                indexes.AddIndex((ISlice)arg);
            }
            else if (arg is string)
            {
                indexes.AddIndex(arr, (string)arg, index);
            }
            else
            {
                ndarray array_arg = null;

                if (arg.GetType().IsArray)
                {
                    dynamic arr1 = arg;
                    if (arr1[0] is ndarray)
                    {
                    }
                    else
                    {
                        array_arg = np.array(new VoidPtr(arr1), null);
                    }
                }
                else
                {
                    array_arg = arg as ndarray;
                }


                if (array_arg == null && arg is IEnumerable <object> )
                {
                    array_arg = np.FromIEnumerable((IEnumerable <object>)arg, null, false, 0, 0);
                }
                if (array_arg == null && arg is IEnumerable <npy_intp> )
                {
                    var arr1 = arg as IEnumerable <npy_intp>;
                    array_arg = np.array(arr1.ToArray(), null);
                }

                // Boolean scalars
                if (array_arg != null &&
                    array_arg.ndim == 0 &&
                    NpyDefs.IsBool(array_arg.TypeNum))
                {
                    indexes.AddIndex(Convert.ToBoolean(array_arg[0]));
                }
                // Integer scalars
                else if (array_arg != null &&
                         array_arg.ndim == 0 &&
                         NpyDefs.IsInteger(array_arg.TypeNum))
                {
                    try
                    {
                        indexes.AddIndex((npy_intp)Convert.ToInt64(array_arg[0]));
                    }
                    catch (Exception e)
                    {
                        throw new IndexOutOfRangeException(e.Message);
                    }
                }
                else if (array_arg != null)
                {
                    // Arrays must be either boolean or integer.
                    if (NpyDefs.IsInteger(array_arg.TypeNum))
                    {
                        indexes.AddIntpArray(array_arg);
                    }
                    else if (NpyDefs.IsBool(array_arg.TypeNum))
                    {
                        indexes.AddBoolArray(array_arg);
                    }
                    else
                    {
                        throw new IndexOutOfRangeException("arrays used as indices must be of integer (or boolean) type.");
                    }
                }
                else if (arg is IEnumerable <Object> )
                {
                    // Other sequences we convert to an intp array
                    indexes.AddIntpArray(arg);
                }
                else if (arg is IConvertible)
                {
#if NPY_INTP_64
                    indexes.AddIndex((npy_intp)Convert.ToInt64(arg));
#else
                    indexes.AddIndex((npy_intp)Convert.ToInt32(arg));
#endif
                }
                else
                {
                    throw new ArgumentException(String.Format("Argument '{0}' is not a valid index.", arg));
                }
            }
        }