예제 #1
0
        //TODO! implement mgrid that takes (string lhs, string rhs) and (Slice lhs, Slice rhs) fallbacking to each other.

        /// <summary>
        ///     nd_grid instance which returns a dense multi-dimensional “meshgrid”.
        ///     An instance of numpy.lib.index_tricks.nd_grid which returns an dense (or fleshed out) mesh-grid when indexed, so that each returned argument has the same shape.
        ///     The dimensions and number of the output arrays are equal to the number of indexing dimensions.If the step length is not a complex number, then the stop is not inclusive.
        /// </summary>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns>mesh-grid `ndarrays` all of the same dimensions</returns>
        /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html</remarks>
        public static (NDArray, NDArray) mgrid(NDArray lhs, NDArray rhs)
        {
            if (!(lhs.ndim == 1 || rhs.ndim == 1))
            {
                throw new IncorrectShapeException();
            }

            IArraySlice nd1Data = lhs.Storage.GetData();
            IArraySlice nd2Data = rhs.Storage.GetData();

            int[] resultDims = new int[] { lhs.Storage.Shape.Dimensions[0], rhs.Storage.Shape.Dimensions[0] };

            NDArray res1 = new NDArray(lhs.dtype, resultDims);
            NDArray res2 = new NDArray(lhs.dtype, resultDims);

            IArraySlice res1Arr = res1.Storage.GetData();
            IArraySlice res2Arr = res2.Storage.GetData();

            int counter = 0;

            for (int row = 0; row < nd1Data.Count; row++)
            {
                for (int col = 0; col < nd2Data.Count; col++)
                {
                    res1Arr[counter] = nd1Data[row];
                    res2Arr[counter] = nd2Data[col];
                    counter++;
                }
            }


            return(res1, res2);
        }
예제 #2
0
        /// <summary>
        ///     Convert an array of size 1 to its scalar equivalent.
        /// </summary>
        /// <param name="arr">Input array of size 1.</param>
        /// <returns></returns>
        /// <remarks>https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.asscalar.html</remarks>
        public static T asscalar <T>(IArraySlice arr) where T : unmanaged
        {
            if (arr.Count != 1)
            {
                throw new IncorrectSizeException("Unable to convert NDArray to scalar because size is not 1.");
            }

            return((T)Convert.ChangeType(arr[0], typeof(T)));
        }
예제 #3
0
        /// <summary>
        /// Constructor which takes .NET array
        /// dtype and shape is determined from array
        /// </summary>
        /// <param name="values"></param>
        /// <param name="shape"></param>
        /// <param name="order"></param>
        /// <returns>Array with values</returns>
        /// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
        public NDArray(IArraySlice values, Shape shape = default, char order = 'C') : base(values, shape, order)
        {
            var underlying = values.GetType().GenericTypeArguments[0];

            if (underlying != typeof(T))
            {
                throw new ArgumentException($"Array type must be the same as T. {underlying.Name} != {typeof(T).Name}", nameof(values));
            }
        }
예제 #4
0
        /* sort all type LMS suffixes */
        private static void LMSsort(IArraySlice T, int[] SA, IArraySlice C, IArraySlice B, int n, int k)
        {
            int b, i, j;
            int c0, c1;

            /* compute SAl */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, false); /* find starts of buckets */
            j  = n - 1;
            c1 = T[j];
            b  = B[c1];
            --j;
            SA[b++] = (T[j] < c1) ? ~j : j;
            for (i = 0; i < n; ++i)
            {
                if (0 < (j = SA[i]))
                {
                    c0 = T[j];
                    if (c0 != c1)
                    {
                        B[c1] = b;
                        c1    = c0;
                        b     = B[c1];
                    }
                    --j;
                    SA[b++] = (T[j] < c1) ? ~j : j;
                    SA[i]   = 0;
                }
                else if (j < 0)
                {
                    SA[i] = ~j;
                }
            }
            /* compute SAs */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, true); /* find ends of buckets */
            c1 = 0;
            for (i = n - 1, b = B[c1]; 0 <= i; --i)
            {
                if (0 < (j = SA[i]))
                {
                    if ((c0 = T[j]) != c1)
                    {
                        B[c1] = b; b = B[c1 = c0];
                    }
                    --j;
                    SA[--b] = (T[j] > c1) ? ~(j + 1) : j;
                    SA[i]   = 0;
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Constructor which takes .NET array
        /// dtype and shape is determined from array
        /// </summary>
        /// <param name="values"></param>
        /// <param name="shape"></param>
        /// <param name="order"></param>
        /// <returns>Array with values</returns>
        /// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
        public NDArray(IArraySlice values, Shape shape = default, char order = 'C') : this(values.TypeCode)
        {
            if (order != 'C')
                shape.ChangeTensorLayout(order);

            if (shape.IsEmpty)
                shape = Shape.Vector((int) values.Count); //TODO! when long index, remove cast int

            Storage.Allocate(values, shape);
        }
예제 #6
0
파일: NDArray.cs 프로젝트: pvk2727/NumSharp
        /// <summary>
        /// Constructor which takes .NET array
        /// dtype and shape is determined from array
        /// </summary>
        /// <param name="values"></param>
        /// <param name="shape"></param>
        /// <param name="order"></param>
        /// <returns>Array with values</returns>
        /// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
        public NDArray(IArraySlice values, Shape shape = default, char order = 'C') : this(values.TypeCode)
        {
            if (order != 'C')
                shape.ChangeTensorLayout(order);

            if (shape.IsEmpty)
                shape = Shape.Vector(values.Count);

            Storage.Allocate(values, shape);
        }
예제 #7
0
        /* compute SA and BWT */
        private static void InduceSA(IArraySlice T, int[] SA, IArraySlice C, IArraySlice B, int n, int k)
        {
            int b, i, j;
            int c0, c1;

            /* compute SAl */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, false); /* find starts of buckets */
            j       = n - 1;
            b       = B[c1 = T[j]];
            SA[b++] = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
            for (i = 0; i < n; ++i)
            {
                j     = SA[i];
                SA[i] = ~j;
                if (0 < j)
                {
                    if ((c0 = T[--j]) != c1)
                    {
                        B[c1] = b;
                        c1    = c0;
                        b     = B[c1];
                    }
                    SA[b++] = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
                }
            }
            /* compute SAs */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, true); /* find ends of buckets */
            for (i = n - 1, b = B[c1 = 0]; 0 <= i; --i)
            {
                if (0 < (j = SA[i]))
                {
                    if ((c0 = T[--j]) != c1)
                    {
                        B[c1] = b;
                        c1    = c0;
                        b     = B[c1];
                    }
                    SA[--b] = ((j == 0) || (T[j - 1] > c1)) ? ~j : j;
                }
                else
                {
                    SA[i] = ~j;
                }
            }
        }
예제 #8
0
        private static void GetCounts(IArraySlice T, IArraySlice C, int n, int k)
        {
            for (int i = 0; i < k; ++i)
            {
                C[i] = 0;
            }

            for (int i = 0; i < n; ++i)
            {
                var ti = T[i];
                C[ti] = C[ti] + 1;
            }
        }
예제 #9
0
        private static void GetBuckets(IArraySlice C, IArraySlice B, int k, bool end)
        {
            int i, sum = 0;

            if (end)
            {
                for (i = 0; i < k; ++i)
                {
                    sum += C[i];
                    B[i] = sum;
                }
            }
            else
            {
                for (i = 0; i < k; ++i)
                {
                    sum += C[i];
                    B[i] = sum - C[i];
                }
            }
        }
예제 #10
0
 public NDIterator(IArraySlice slice, Shape shape, Shape?broadcastedShape, bool autoReset = false) : this((IMemoryBlock)slice, shape, broadcastedShape, autoReset)
 {
 }
예제 #11
0
 private NDArray(IArraySlice array, Shape shape) : this(array.TypeCode)
 {
     Storage.Allocate(array, shape);
 }
예제 #12
0
        /* find the suffix array SA of T[0..n-1] in {0..k-1}^n
         * use a working space (excluding T and SA) of at most 2n+O(1) for a constant alphabet */
        private static int SaisMain(IArraySlice T, int[] SA, int fs, int n, int k, bool isbwt)
        {
            IArraySlice C, B, RA;
            int         i, j, b, m, p, q, name, pidx = 0, newfs;
            int         c0, c1;
            uint        flags = 0;

            if (k <= MINBUCKETSIZE)
            {
                C = new IntArraySlice(new int[k], 0);
                if (k <= fs)
                {
                    B     = new IntArraySlice(SA, n + fs - k);
                    flags = 1;
                }
                else
                {
                    B     = new IntArraySlice(new int[k], 0);
                    flags = 3;
                }
            }
            else if (k <= fs)
            {
                C = new IntArraySlice(SA, n + fs - k);
                if (k <= (fs - k))
                {
                    B     = new IntArraySlice(SA, n + fs - k * 2);
                    flags = 0;
                }
                else if (k <= (MINBUCKETSIZE * 4))
                {
                    B     = new IntArraySlice(new int[k], 0);
                    flags = 2;
                }
                else
                {
                    B     = C;
                    flags = 8;
                }
            }
            else
            {
                C     = B = new IntArraySlice(new int[k], 0);
                flags = 4 | 8;
            }

            /* stage 1: reduce the problem by at least 1/2
             * sort all the LMS-substrings */
            GetCounts(T, C, n, k); GetBuckets(C, B, k, true); /* find ends of buckets */
            for (i = 0; i < n; ++i)
            {
                SA[i] = 0;
            }
            b  = -1;
            i  = n - 1;
            j  = n;
            m  = 0;
            c0 = T[n - 1];
            do
            {
                c1 = c0;
            } while ((0 <= --i) && ((c0 = T[i]) >= c1));
            for (; 0 <= i;)
            {
                do
                {
                    c1 = c0;
                } while ((0 <= --i) && ((c0 = T[i]) <= c1));
                if (0 <= i)
                {
                    if (0 <= b)
                    {
                        SA[b] = j;
                    }
                    b = --B[c1];
                    j = i;
                    ++m;
                    do
                    {
                        c1 = c0;
                    } while ((0 <= --i) && ((c0 = T[i]) >= c1));
                }
            }
            if (1 < m)
            {
                LMSsort(T, SA, C, B, n, k);
                name = LMSpostproc(T, SA, n, m);
            }
            else if (m == 1)
            {
                SA[b] = j + 1;
                name  = 1;
            }
            else
            {
                name = 0;
            }

            /* stage 2: solve the reduced problem
             * recurse if names are not yet unique */
            if (name < m)
            {
                if ((flags & 4) != 0)
                {
                    C = null;
                    B = null;
                }
                if ((flags & 2) != 0)
                {
                    B = null;
                }
                newfs = (n + fs) - (m * 2);
                if ((flags & (1 | 4 | 8)) == 0)
                {
                    if ((k + name) <= newfs)
                    {
                        newfs -= k;
                    }
                    else
                    {
                        flags |= 8;
                    }
                }
                for (i = m + (n >> 1) - 1, j = m * 2 + newfs - 1; m <= i; --i)
                {
                    if (SA[i] != 0)
                    {
                        SA[j--] = SA[i] - 1;
                    }
                }
                RA = new IntArraySlice(SA, m + newfs);
                SaisMain(RA, SA, newfs, m, name, false);
                RA = null;

                i = n - 1; j = m * 2 - 1; c0 = T[n - 1];
                do
                {
                    c1 = c0;
                } while ((0 <= --i) && ((c0 = T[i]) >= c1));
                for (; 0 <= i;)
                {
                    do
                    {
                        c1 = c0;
                    } while ((0 <= --i) && ((c0 = T[i]) <= c1));
                    if (0 <= i)
                    {
                        SA[j--] = i + 1;
                        do
                        {
                            c1 = c0;
                        } while ((0 <= --i) && ((c0 = T[i]) >= c1));
                    }
                }

                for (i = 0; i < m; ++i)
                {
                    SA[i] = SA[m + SA[i]];
                }
                if ((flags & 4) != 0)
                {
                    C = B = new IntArraySlice(new int[k], 0);
                }
                if ((flags & 2) != 0)
                {
                    B = new IntArraySlice(new int[k], 0);
                }
            }

            /* stage 3: induce the result for the original problem */
            if ((flags & 8) != 0)
            {
                GetCounts(T, C, n, k);
            }
            /* put all left-most S characters into their buckets */
            if (1 < m)
            {
                GetBuckets(C, B, k, true); /* find ends of buckets */
                i = m - 1; j = n; p = SA[m - 1]; c1 = T[p];
                do
                {
                    q = B[c0 = c1];
                    while (q < j)
                    {
                        SA[--j] = 0;
                    }
                    do
                    {
                        SA[--j] = p;
                        if (--i < 0)
                        {
                            break;
                        }
                        p = SA[i];
                    } while ((c1 = T[p]) == c0);
                } while (0 <= i);
                while (0 < j)
                {
                    SA[--j] = 0;
                }
            }
            if (!isbwt)
            {
                InduceSA(T, SA, C, B, n, k);
            }
            else
            {
                pidx = ComputeBwt(T, SA, C, B, n, k);
            }
            C = null;
            B = null;
            return(pidx);
        }
예제 #13
0
        private static int ComputeBwt(IArraySlice T, int[] SA, IArraySlice C, IArraySlice B, int n, int k)
        {
            int b, i, j, pidx = -1;
            int c0, c1;

            /* compute SAl */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, false); /* find starts of buckets */
            j       = n - 1;
            c1      = T[j];
            b       = B[c1];
            SA[b++] = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
            for (i = 0; i < n; ++i)
            {
                if (0 < (j = SA[i]))
                {
                    SA[i] = ~(c0 = T[--j]);
                    if (c0 != c1)
                    {
                        B[c1] = b;
                        b     = B[c1 = c0];
                    }
                    SA[b++] = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
                }
                else if (j != 0)
                {
                    SA[i] = ~j;
                }
            }

            /* compute SAs */
            if (C == B)
            {
                GetCounts(T, C, n, k);
            }
            GetBuckets(C, B, k, true); /* find ends of buckets */
            for (i = n - 1, b = B[c1 = 0]; 0 <= i; --i)
            {
                if (0 < (j = SA[i]))
                {
                    SA[i] = (c0 = T[--j]);
                    if (c0 != c1)
                    {
                        B[c1] = b;
                        c1    = c0;
                        b     = B[c1];
                    }
                    SA[--b] = ((0 < j) && (T[j - 1] > c1)) ? ~((int)T[j - 1]) : j;
                }
                else if (j != 0)
                {
                    SA[i] = ~j;
                }
                else
                {
                    pidx = i;
                }
            }
            return(pidx);
        }
예제 #14
0
        private static int LMSpostproc(IArraySlice T, int[] SA, int n, int m)
        {
            int  i, j, p, q, plen, qlen, name;
            int  c0, c1;
            bool diff;

            /* compact all the sorted substrings into the first m items of SA
             *  2*m must be not larger than n (proveable) */
            for (i = 0; (p = SA[i]) < 0; ++i)
            {
                SA[i] = ~p;
            }
            if (i < m)
            {
                for (j = i, ++i; ; ++i)
                {
                    if ((p = SA[i]) < 0)
                    {
                        SA[j++] = ~p; SA[i] = 0;
                        if (j == m)
                        {
                            break;
                        }
                    }
                }
            }

            /* store the length of all substrings */
            i = n - 1; j = n - 1; c0 = T[n - 1];
            do
            {
                c1 = c0;
            } while ((0 <= --i) && ((c0 = T[i]) >= c1));

            for (; 0 <= i;)
            {
                do
                {
                    c1 = c0;
                } while ((0 <= --i) && ((c0 = T[i]) <= c1));
                if (0 <= i)
                {
                    SA[m + ((i + 1) >> 1)] = j - i;
                    j = i + 1;
                    do
                    {
                        c1 = c0;
                    } while ((0 <= --i) && ((c0 = T[i]) >= c1));
                }
            }

            /* find the lexicographic names of all substrings */
            for (i = 0, name = 0, q = n, qlen = 0; i < m; ++i)
            {
                p    = SA[i];
                plen = SA[m + (p >> 1)];
                diff = true;
                if ((plen == qlen) && ((q + plen) < n))
                {
                    for (j = 0; (j < plen) && (T[p + j] == T[q + j]); ++j)
                    {
                    }
                    if (j == plen)
                    {
                        diff = false;
                    }
                }
                if (diff != false)
                {
                    ++name;
                    q    = p;
                    qlen = plen;
                }
                SA[m + (p >> 1)] = name;
            }

            return(name);
        }