Ejemplo n.º 1
0
        /// <summary>
        /// Create an enumerator suitable for constructing images (i.e. travels along the first dimension,
        /// then the second). If not 2D, first element is null.
        /// </summary>
        /// <param name="enumerable"></param>
        /// <returns></returns>
        public static IEnumerable <double> ToImageEnumerator(IEnumerable <object> enumerable, out int xLength, out int yLength)
        {
            int[] dimensions = GeneralArray.GetDimensions(enumerable);
            xLength = dimensions[0];
            yLength = 0;
            if (dimensions.Length > 2)
            {
                return(null);
            }

            // If this is a list of lists, then create enumerator, otherwise convert to a rectangular array first.
            // This gives better performance if not indexable or if this is a Numpy array.

            Type arrayType = enumerable.GetType();

            if (dimensions.Length == 2 && arrayType.Name != "ndarray" && IsIListOfILists(enumerable))
            {
                List <int> dimensionList = new List <int>();
                IList      parent = enumerable as IList;
                int        minLength = int.MaxValue; int maxLength = int.MinValue;
                int        count = parent.FastCount();

                for (int i = 0; i < count; ++i)
                {
                    IList child = parent[i] as IList;
                    if (child == null)
                    {
                        minLength = 0;
                    }
                    else
                    {
                        minLength = Math.Min(minLength, child.Count);
                        maxLength = Math.Max(maxLength, child.Count);
                    }
                }
                yLength = maxLength;
                if (minLength == 0)
                {
                    return(null);
                }
                else
                {
                    return(Jagged2DImageEnumerable(parent, minLength, maxLength));
                }
            }
            else if (dimensions.Length == 1)
            {
                return(Enumerable1D(enumerable));
            }
            else
            {
                double[,] array = (double[, ])GeneralArray.ToDoubleArray(enumerable);
                yLength         = dimensions[1];
                return(array.ArrayEnumerator(EnumerationOrder2D.ColumnMajor));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts object to a
        /// </summary>
        /// <param name="generalArray"></param>
        /// <returns></returns>
        public unsafe static Array ToDoubleArray(object generalArray)
        {
            Type  arrayType    = generalArray.GetType();
            Array managedArray = null;

            #region ILArray
            if (arrayType.Name == "ILArray`1")
            {
                dynamic dynamicArray = generalArray;

                if ((dynamicArray.Dimensions.NumberOfDimensions == 1) ||
                    ((dynamicArray.Dimensions.NumberOfDimensions == 2) && (dynamicArray.Dimensions[1] == 1)))
                {
                    int n = dynamicArray.Dimensions[0];
                    managedArray = new double[n];
                    double[] managedArrayCast = managedArray as double[];
                    for (int i = 0; i < n; ++i)
                    {
                        managedArrayCast[i] = dynamicArray.GetValue(i);
                    }
                    return(managedArray);
                }
                else if (dynamicArray.Dimensions.NumberOfDimensions == 2)
                {
                    int n1 = dynamicArray.Dimensions[0];
                    int n2 = dynamicArray.Dimensions[1];
                    managedArray = new double[n1, n2];
                    double[,] managedArrayCast = managedArray as double[, ];
                    for (int i = 0; i < n1; ++i)
                    {
                        for (int j = 0; j < n2; ++j)
                        {
                            managedArrayCast[i, j] = dynamicArray.GetValue(i, j);
                        }
                    }
                    return(managedArray);
                }
                throw new Exception("Array must be one or two dimensionsal.");
            }
            #endregion
            else if (arrayType.Name == "ndarray")
            {
                managedArray = ManagedArrayFromNumpyArray(generalArray);
            }
            else
            {
                // not Array, ILArray or Numpy array
                int[] dimensions = GeneralArray.GetDimensions(generalArray);
                if (dimensions.Length > 2)
                {
                    throw new Exception("More than two dimensions found.");
                }
                if (dimensions.Length == 1)
                {
                    var      enumerable = generalArray as IEnumerable <object>;
                    double[] tempArray  = new double[enumerable.Count()];
                    int      index      = 0;
                    foreach (object item in enumerable)
                    {
                        tempArray[index] = Convert.ToDouble(item);
                        index++;
                    }
                    managedArray = tempArray;
                }
                else
                {
                    var parent = generalArray as IEnumerable <object>;
                    int minLength = int.MaxValue; int maxLength = int.MinValue;
                    int count = parent.FastCount();
                    foreach (IEnumerable <object> child in parent)
                    {
                        if (child == null)
                        {
                            minLength = 0;
                        }
                        else
                        {
                            minLength = Math.Min(minLength, child.FastCount());
                            maxLength = Math.Max(maxLength, child.FastCount());
                        }
                    }
                    int i = 0;
                    double[,] tempArray = new double[count, maxLength];
                    foreach (IEnumerable <object> child in parent)
                    {
                        int j = 0;
                        foreach (object element in child)
                        {
                            tempArray[i, j] = Convert.ToDouble(element);
                            j++;
                        }
                        while (j < maxLength)
                        {
                            tempArray[i, j] = Double.NaN; ++j;
                        }
                        i++;
                    }
                    managedArray = tempArray;
                }
            }
            return(managedArray);
        }