예제 #1
0
        public static ndarray setxor1d(ndarray ar1, ndarray ar2, bool assume_unique = false)
        {
            /*
             * Find the set exclusive-or of two arrays.
             *
             * Return the sorted, unique values that are in only one (not both) of the
             * input arrays.
             *
             * Parameters
             * ----------
             * ar1, ar2 : array_like
             *  Input arrays.
             * assume_unique : bool
             *  If True, the input arrays are both assumed to be unique, which
             *  can speed up the calculation.  Default is False.
             *
             * Returns
             * -------
             * setxor1d : ndarray
             *  Sorted 1D array of unique values that are in only one of the input
             *  arrays.
             *
             * Examples
             * --------
             * >>> a = np.array([1, 2, 3, 2, 4])
             * >>> b = np.array([2, 3, 5, 7, 5])
             * >>> np.setxor1d(a,b)
             * array([1, 4, 5, 7])
             */

            if (!assume_unique)
            {
                ar1 = unique(ar1).data;
                ar2 = unique(ar2).data;
            }

            ndarray aux = np.concatenate(new ndarray[] { ar1, ar2 });

            if (aux.size == 0)
            {
                return(aux);
            }

            aux = aux.Sort();

            ndarray True1 = np.array(new bool[] { true });
            ndarray True2 = np.array(new bool[] { true });

            ndarray flag = np.concatenate(new ndarray[] { True1, (aux.A("1:")).NotEquals(aux.A(":-1")), True2 });

            ndarray mask = flag.A("1:") & flag.A(":-1");

            return(aux.A(mask));
        }
예제 #2
0
        public static ndarray intersect1d(ndarray ar1, ndarray ar2, bool assume_unique = false)
        {
            /*
             * Find the intersection of two arrays.
             *
             * Return the sorted, unique values that are in both of the input arrays.
             *
             * Parameters
             * ----------
             * ar1, ar2 : array_like
             *  Input arrays.
             * assume_unique : bool
             *  If True, the input arrays are both assumed to be unique, which
             *  can speed up the calculation.  Default is False.
             *
             * Returns
             * -------
             * intersect1d : ndarray
             *  Sorted 1D array of common and unique elements.
             *
             * See Also
             * --------
             * numpy.lib.arraysetops : Module with a number of other functions for
             *                      performing set operations on arrays.
             *
             * Examples
             * --------
             * >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
             * array([1, 3])
             *
             * To intersect more than two arrays, use functools.reduce:
             *
             * >>> from functools import reduce
             * >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2]))
             * array([3])
             */

            if (!assume_unique)
            {
                //Might be faster than unique( intersect1d( ar1, ar2 ) )?
                ar1 = unique(ar1).data;
                ar2 = unique(ar2).data;
            }

            ndarray aux = np.concatenate(new ndarray[] { ar1, ar2 });

            aux = aux.Sort();

            ndarray mask = (aux.A("1:")).Equals(aux.A(":-1"));

            return(aux.A(":-1", mask));
        }