Example #1
0
        /// <summary>
        /// sort data in A along dimension 'dim'
        /// </summary>
        /// <param name="A">input array: empty, scalar, vector or matrix</param>
        /// <param name="descending">Specifies the direction of sorting</param>
        /// <param name="dim">dimension to sort along</param>
        /// <param name="Indices">output parameter: returns permutation matrix also</param>
        /// <returns>sorted array of the same size as A</returns>
        /// <remarks><para>The data in A will be sorted using the quick sort algorithm. Data
        /// along the dimension <paramref name="dim"/> will therefore get sorted independently from data
        /// in the next row/column.</para>
        /// <para>This overload also returns an array 'Indices' which will hold the indices into the original
        /// elements <b>after sorting</b>. Elements of 'Indices' are of type double.</para>
        ///</remarks>
        public static ILArray <string> sort(ILArray <string> A, out ILArray <double> Indices, int dim, bool descending)
        {
            if (object.Equals(A, null))
            {
                throw new Exception("sort: parameter A must not be null");
            }
            if (A.Dimensions.NumberOfDimensions > 2)
            {
                throw new ILArgumentException("sort: for element type string only matrices are supported!");
            }
            if (dim < 0 || dim >= A.Dimensions.NumberOfDimensions)
            {
                throw new ILArgumentException("sort: invalid dimension argument");
            }
            // early exit: scalar/ empty
            if (A.IsEmpty || A.IsScalar)
            {
                Indices = 0.0;
                return(A.C);
            }
            ILArray <string> ret = new ILArray <string>(new string[A.Dimensions.NumberOfElements], A.Dimensions);
            int dim1             = dim % A.Dimensions.NumberOfDimensions;
            int dim2             = (dim1 + 1) % A.Dimensions.NumberOfDimensions;
            int maxRuns          = A.Dimensions[dim2];
            ILQueueList <string, double> ql;

            ILArray <int>[]  ind  = new ILArray <int> [2];
            int []           indI = new int[2];
            ILASCIIKeyMapper km   = new ILASCIIKeyMapper();

            Indices = ILMath.counter(0.0, 1.0, A.Dimensions.ToIntArray());
            for (int c = 0; c < maxRuns; c++)
            {
                ind[dim2]  = c;
                ind[dim1]  = null;
                ql         = ILBucketSort.BucketSort <string, char, double>(A[ind].Values, Indices[ind].Values, km, ILBucketSort.SortMethod.ConstantLength);
                indI[dim2] = c;
                if (descending)
                {
                    for (int i = ql.Count; i-- > 0;)
                    {
                        indI[dim1] = i;
                        ILListItem <string, double> item = ql.Dequeue();
                        ret.SetValue(item.Data, indI);
                        Indices.SetValue(item.m_index, indI);
                    }
                }
                else
                {
                    for (int i = 0; ql.Count > 0; i++)
                    {
                        indI[dim1] = i;
                        ILListItem <string, double> item = ql.Dequeue();
                        ret.SetValue(item.Data, indI);
                        Indices.SetValue(item.m_index, indI);
                    }
                }
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// sort data in A along dimension 'dim'
        /// </summary>
        /// <param name="A">input array, n-dimensional</param>
        /// <param name="descending">Specifies the direction of sorting</param>
        /// <param name="dim">dimension to sort along</param>
        /// <param name="Indices">output parameter: returns permutation matrix also</param>
        /// <returns>sorted array of the same size as A</returns>
        /// <remarks><para>The data in A will be sorted using the quick sort algorithm. Data
        /// along the dimension <paramref name="dim"/> will therefore get sorted independently from data
        /// in the next row/column.</para>
        /// <para>This overload also returns an array 'Indices' which will hold the indices into the original
        /// elements <b>after sorting</b>. Elements of 'Indices' are of type double.</para>
        /// <example><code>ILArray&lt;double&gt; A = ILMath.rand(1,5);
        /// //A:
        /// // 0.4324  0.9231  0.1231  0.1423  0.2991
        /// ILArray&lt;double&gt; I;
        /// ILArray&lt;double&gt; R = ILMath.sort(A, out I, 1, false);
        /// //R:
        /// // 0.1231  0.1423  0.2991  0.4324  0.9231
        /// //I:
        /// // 2  3  4  0  1
        /// </code>
        /// </example></remarks>
        public static ILArray <byte> sort(ILArray <byte> A, out ILArray <double> Indices, int dim, bool descending)
        {
            if (object.Equals(A, null))
            {
                throw new Exception("sort: parameter A must not be null");
            }
            if (dim < 0 || dim >= A.Dimensions.NumberOfDimensions)
            {
                throw new ILArgumentException("sort: invalid dimension argument");
            }
            // early exit: scalar/ empty
            Indices = ILMath.counter(0.0, 1.0, A.Dimensions.ToIntArray());
            if (A.IsEmpty || A.IsScalar)
            {
                return(A.C);
            }
            ILArray <byte> ret      = A.C;
            int            inc      = ret.Dimensions.SequentialIndexDistance(dim);
            int            dimLen   = A.Dimensions[dim];
            int            maxRuns  = A.Dimensions.NumberOfElements / dimLen;
            int            posInArr = 0;

            if (descending)
            {
                for (int c = 0; c < maxRuns; c++)
                {
                    if (posInArr >= A.Dimensions.NumberOfElements)
                    {
                        posInArr -= (A.Dimensions.NumberOfElements - 1);
                    }
                    ILQuickSort.QuickSortDescSolidIDX(ret.m_data, Indices.m_data, posInArr, posInArr + (dimLen - 1) * inc, inc);
                    posInArr += dimLen * inc;
                }
            }
            else
            {
                // ascending
                for (int c = 0; c < maxRuns; c++)
                {
                    if (posInArr >= A.Dimensions.NumberOfElements)
                    {
                        posInArr -= (A.Dimensions.NumberOfElements - 1);
                    }
                    ILQuickSort.QuickSortAscSolidIDX(ret.m_data, Indices.m_data, posInArr, posInArr + (dimLen - 1) * inc, inc);
                    posInArr += dimLen * inc;
                }
            }
            return(ret);
        }
Example #3
0
 public IronPython.Runtime.List this[IronPython.Runtime.Slice xslice, IronPython.Runtime.Slice yslice]
 {
     get
     {
         if ((xslice.start == null) || (xslice.stop == null) || (yslice.start == null) || (yslice.stop == null))
         {
             throw new ILArgumentException("mgrid: start and stop of slices must be provided");
         }
         double xStep = 1, yStep = 1, xStart = 0, yStart = 0, xStop = 1, yStop = 1;
         //
         if (xslice.start is int)
         {
             xStart = (double)(int)(xslice.start);
         }
         if (xslice.start is double)
         {
             xStart = (double)(xslice.start);
         }
         if (xslice.stop is int)
         {
             xStop = (double)(int)(xslice.stop);
         }
         if (xslice.stop is double)
         {
             xStop = (double)(xslice.stop);
         }
         //
         if (yslice.start is int)
         {
             yStart = (double)(int)(yslice.start);
         }
         if (yslice.start is double)
         {
             yStart = (double)(yslice.start);
         }
         if (yslice.stop is int)
         {
             yStop = (double)(int)(yslice.stop);
         }
         if (yslice.stop is double)
         {
             yStop = (double)(yslice.stop);
         }
         //
         if (xslice.step == null)
         {
             if (xStop >= xStart)
             {
                 xStep = 1;
             }
             else
             {
                 xStep = -1;
             }
         }
         else
         {
             if (xslice.step is int)
             {
                 xStep = (double)(int)(xslice.step);
             }
             if (xslice.step is double)
             {
                 xStep = (double)(xslice.step);
             }
         }
         if (yslice.step == null)
         {
             if (yStop >= yStart)
             {
                 yStep = 1;
             }
             else
             {
                 yStep = -1;
             }
         }
         else
         {
             if (yslice.step is int)
             {
                 yStep = (double)(int)(yslice.step);
             }
             if (yslice.step is double)
             {
                 yStep = (double)(yslice.step);
             }
         }
         //
         IronPython.Runtime.List list = new IronPython.Runtime.List();
         int nx = (int)Math.Floor((xStop - xStart) / xStep) + 1;
         int ny = (int)Math.Floor((yStop - yStart) / yStep) + 1;
         ILArray <double>         x        = ILMath.counter(xStart, xStep, nx);
         ILArray <double>         y        = ILMath.counter(yStart, yStep, ny);
         List <ILArray <double> > meshgrid = ILMath.meshgrid(x, y);
         list.Add(meshgrid[0]); list.Add(meshgrid[1]);
         return(list);
     }
 }