def _sort_internal(a: ndarray, axis: int = -1, ascending: bool = True) \ -> af.Array: if axis is None: a = a.flatten() axis = 0 elif axis == -1: axis = a.ndim - 1 elif axis >= a.ndim: raise ValueError(f"Parameter axis must be between -1 and {a.ndim - 1}") return af.sort(a._af_array, dim=axis, is_ascending=ascending)
def sort(self, axis=-1, kind='quicksort', order=None): if kind != 'quicksort': print("sort 'kind' argument ignored") if order is not None: raise ValueError('order argument is not supported') if (axis is None): input = self.flatten() axis = 0 else: input = self if (axis < 0): axis = self.ndim + axis s = arrayfire.sort(input.d_array, pu.c2f(input.shape, axis)) self.d_array = s
def sort(self, axis=-1, kind='quicksort', order=None): if kind != 'quicksort': print( "sort 'kind' argument ignored" ) if order is not None: raise ValueError('order argument is not supported') if(axis is None): input = self.flatten() axis = 0 else: input = self if(axis < 0): axis = self.ndim+axis s = arrayfire.sort(input.d_array, pu.c2f(input.shape, axis)) self.d_array = s
def sort(a, axis=-1, kind='quicksort', order=None): try: if kind != 'quicksort': print( "sort 'kind' argument ignored" ) if order is not None: raise ValueError('order argument is not supported') if(axis is None): input = a.flatten() axis = 0 else: input = a if(axis < 0): axis = a.ndim+axis s = arrayfire.sort(input.d_array, pu.c2f(input.shape, axis)) return afnumpy.ndarray(input.shape, dtype=pu.typemap(s.dtype()), af_array=s) except AttributeError: return numpy.argsort(a, axis, kind, order)
def unique(ar: ndarray, return_index: bool = False, return_inverse: bool = False, return_counts: bool = False) -> ndarray: if return_index: raise ValueError("return_index=True is not supported") if return_inverse: raise ValueError("return_inverse=True is not supported") if return_counts: raise ValueError("return_counts=True is not supported") unsorted_unique_set_af_array = af.set_unique(ar._af_array, is_sorted=False) sorted_unique_set_af_array = af.sort(unsorted_unique_set_af_array, dim=0, is_ascending=True) return ndarray(sorted_unique_set_af_array)
def simple_algorithm(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) display_func(af.sum(a, 0)) display_func(af.sum(a, 1)) display_func(af.product(a, 0)) display_func(af.product(a, 1)) display_func(af.min(a, 0)) display_func(af.min(a, 1)) display_func(af.max(a, 0)) display_func(af.max(a, 1)) display_func(af.count(a, 0)) display_func(af.count(a, 1)) display_func(af.any_true(a, 0)) display_func(af.any_true(a, 1)) display_func(af.all_true(a, 0)) display_func(af.all_true(a, 1)) display_func(af.accum(a, 0)) display_func(af.accum(a, 1)) display_func(af.sort(a, is_ascending=True)) display_func(af.sort(a, is_ascending=False)) b = (a > 0.1) * a c = (a > 0.4) * a d = b / c print_func(af.sum(d)); print_func(af.sum(d, nan_val=0.0)); display_func(af.sum(d, dim=0, nan_val=0.0)); val,idx = af.sort_index(a, is_ascending=True) display_func(val) display_func(idx) val,idx = af.sort_index(a, is_ascending=False) display_func(val) display_func(idx) b = af.randu(3,3) keys,vals = af.sort_by_key(a, b, is_ascending=True) display_func(keys) display_func(vals) keys,vals = af.sort_by_key(a, b, is_ascending=False) display_func(keys) display_func(vals) c = af.randu(5,1) d = af.randu(5,1) cc = af.set_unique(c, is_sorted=False) dd = af.set_unique(af.sort(d), is_sorted=True) display_func(cc) display_func(dd) display_func(af.set_union(cc, dd, is_unique=True)) display_func(af.set_union(cc, dd, is_unique=False)) display_func(af.set_intersect(cc, cc, is_unique=True)) display_func(af.set_intersect(cc, cc, is_unique=False))
af.display(af.max(a, 0)) af.display(af.max(a, 1)) af.display(af.count(a, 0)) af.display(af.count(a, 1)) af.display(af.any_true(a, 0)) af.display(af.any_true(a, 1)) af.display(af.all_true(a, 0)) af.display(af.all_true(a, 1)) af.display(af.accum(a, 0)) af.display(af.accum(a, 1)) af.display(af.sort(a, is_ascending=True)) af.display(af.sort(a, is_ascending=False)) val, idx = af.sort_index(a, is_ascending=True) af.display(val) af.display(idx) val, idx = af.sort_index(a, is_ascending=False) af.display(val) af.display(idx) b = af.randu(3, 3) keys, vals = af.sort_by_key(a, b, is_ascending=True) af.display(keys) af.display(vals) keys, vals = af.sort_by_key(a, b, is_ascending=False) af.display(keys)
af.print_array(af.max(a, 0)) af.print_array(af.max(a, 1)) af.print_array(af.count(a, 0)) af.print_array(af.count(a, 1)) af.print_array(af.any_true(a, 0)) af.print_array(af.any_true(a, 1)) af.print_array(af.all_true(a, 0)) af.print_array(af.all_true(a, 1)) af.print_array(af.accum(a, 0)) af.print_array(af.accum(a, 1)) af.print_array(af.sort(a, is_ascending=True)) af.print_array(af.sort(a, is_ascending=False)) val,idx = af.sort_index(a, is_ascending=True) af.print_array(val) af.print_array(idx) val,idx = af.sort_index(a, is_ascending=False) af.print_array(val) af.print_array(idx) b = af.randu(3,3) keys,vals = af.sort_by_key(a, b, is_ascending=True) af.print_array(keys) af.print_array(vals) keys,vals = af.sort_by_key(a, b, is_ascending=False) af.print_array(keys)
af.display(af.max(a, 0)) af.display(af.max(a, 1)) af.display(af.count(a, 0)) af.display(af.count(a, 1)) af.display(af.any_true(a, 0)) af.display(af.any_true(a, 1)) af.display(af.all_true(a, 0)) af.display(af.all_true(a, 1)) af.display(af.accum(a, 0)) af.display(af.accum(a, 1)) af.display(af.sort(a, is_ascending=True)) af.display(af.sort(a, is_ascending=False)) val,idx = af.sort_index(a, is_ascending=True) af.display(val) af.display(idx) val,idx = af.sort_index(a, is_ascending=False) af.display(val) af.display(idx) b = af.randu(3,3) keys,vals = af.sort_by_key(a, b, is_ascending=True) af.display(keys) af.display(vals) keys,vals = af.sort_by_key(a, b, is_ascending=False) af.display(keys)
def simple_algorithm(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) k = af.constant(1, 3, 3, dtype=af.Dtype.u32) af.eval(k) print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) display_func(af.sum(a, 0)) display_func(af.sum(a, 1)) rk = af.constant(1, 3, dtype=af.Dtype.u32) rk[2] = 0 af.eval(rk) display_func(af.sumByKey(rk, a, dim=0)) display_func(af.sumByKey(rk, a, dim=1)) display_func(af.productByKey(rk, a, dim=0)) display_func(af.productByKey(rk, a, dim=1)) display_func(af.minByKey(rk, a, dim=0)) display_func(af.minByKey(rk, a, dim=1)) display_func(af.maxByKey(rk, a, dim=0)) display_func(af.maxByKey(rk, a, dim=1)) display_func(af.anyTrueByKey(rk, a, dim=0)) display_func(af.anyTrueByKey(rk, a, dim=1)) display_func(af.allTrueByKey(rk, a, dim=0)) display_func(af.allTrueByKey(rk, a, dim=1)) display_func(af.countByKey(rk, a, dim=0)) display_func(af.countByKey(rk, a, dim=1)) display_func(af.product(a, 0)) display_func(af.product(a, 1)) display_func(af.min(a, 0)) display_func(af.min(a, 1)) display_func(af.max(a, 0)) display_func(af.max(a, 1)) display_func(af.count(a, 0)) display_func(af.count(a, 1)) display_func(af.any_true(a, 0)) display_func(af.any_true(a, 1)) display_func(af.all_true(a, 0)) display_func(af.all_true(a, 1)) display_func(af.accum(a, 0)) display_func(af.accum(a, 1)) display_func(af.scan(a, 0, af.BINARYOP.ADD)) display_func(af.scan(a, 1, af.BINARYOP.MAX)) display_func(af.scan_by_key(k, a, 0, af.BINARYOP.ADD)) display_func(af.scan_by_key(k, a, 1, af.BINARYOP.MAX)) display_func(af.sort(a, is_ascending=True)) display_func(af.sort(a, is_ascending=False)) b = (a > 0.1) * a c = (a > 0.4) * a d = b / c print_func(af.sum(d)) print_func(af.sum(d, nan_val=0.0)) display_func(af.sum(d, dim=0, nan_val=0.0)) val, idx = af.sort_index(a, is_ascending=True) display_func(val) display_func(idx) val, idx = af.sort_index(a, is_ascending=False) display_func(val) display_func(idx) b = af.randu(3, 3) keys, vals = af.sort_by_key(a, b, is_ascending=True) display_func(keys) display_func(vals) keys, vals = af.sort_by_key(a, b, is_ascending=False) display_func(keys) display_func(vals) c = af.randu(5, 1) d = af.randu(5, 1) cc = af.set_unique(c, is_sorted=False) dd = af.set_unique(af.sort(d), is_sorted=True) display_func(cc) display_func(dd) display_func(af.set_union(cc, dd, is_unique=True)) display_func(af.set_union(cc, dd, is_unique=False)) display_func(af.set_intersect(cc, cc, is_unique=True)) display_func(af.set_intersect(cc, cc, is_unique=False))