def center_of_mass(inarr): arr = af.abs(inarr) normalizer = af.sum(arr) t_dims = list(arr.dims()) mod_dims = [None, None, None, None] for i in range(len(t_dims)): mod_dims[i] = 1 com = [] for dim in range(len(t_dims)): # swap mod_dims[dim] = t_dims[dim] t_dims[dim] = 1 grid = af.iota(mod_dims[0], mod_dims[1], mod_dims[2], mod_dims[3], tile_dims=t_dims) # print(grid) com.append(af.sum(grid * arr) / normalizer) # swap back t_dims[dim] = mod_dims[dim] mod_dims[dim] = 1 return com
def simple_data(verbose=False): display_func = _util.display_func(verbose) display_func(af.constant(100, 3, 3, dtype=af.Dtype.f32)) display_func(af.constant(25, 3, 3, dtype=af.Dtype.c32)) display_func(af.constant(2**50, 3, 3, dtype=af.Dtype.s64)) display_func(af.constant(2+3j, 3, 3)) display_func(af.constant(3+5j, 3, 3, dtype=af.Dtype.c32)) display_func(af.range(3, 3)) display_func(af.iota(3, 3, tile_dims=(2, 2))) display_func(af.identity(3, 3, 1, 2, af.Dtype.b8)) display_func(af.identity(3, 3, dtype=af.Dtype.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) display_func(a) display_func(b) display_func(c) display_func(af.diag(b, extract=False)) display_func(af.diag(c, 1, extract=False)) display_func(af.join(0, a, a)) display_func(af.join(1, a, a, a)) display_func(af.tile(a, 2, 2)) display_func(af.reorder(a, 1, 0)) display_func(af.shift(a, -1, 1)) display_func(af.moddims(a, 6, 2)) display_func(af.flat(a)) display_func(af.flip(a, 0)) display_func(af.flip(a, 1)) display_func(af.lower(a, False)) display_func(af.lower(a, True)) display_func(af.upper(a, False)) display_func(af.upper(a, True)) a = af.randu(5, 5) display_func(af.transpose(a)) af.transpose_inplace(a) display_func(a) display_func(af.select(a > 0.3, a, -0.3)) af.replace(a, a > 0.3, -0.3) display_func(a) display_func(af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0)))
def simple_data(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) display_func(af.constant(100, 3,3, dtype=af.Dtype.f32)) display_func(af.constant(25, 3,3, dtype=af.Dtype.c32)) display_func(af.constant(2**50, 3,3, dtype=af.Dtype.s64)) display_func(af.constant(2+3j, 3,3)) display_func(af.constant(3+5j, 3,3, dtype=af.Dtype.c32)) display_func(af.range(3, 3)) display_func(af.iota(3, 3, tile_dims=(2,2))) display_func(af.identity(3, 3, 1, 2, af.Dtype.b8)) display_func(af.identity(3, 3, dtype=af.Dtype.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) display_func(a) display_func(b) display_func(c) display_func(af.diag(b, extract = False)) display_func(af.diag(c, 1, extract = False)) display_func(af.join(0, a, a)) display_func(af.join(1, a, a, a)) display_func(af.tile(a, 2, 2)) display_func(af.reorder(a, 1, 0)) display_func(af.shift(a, -1, 1)) display_func(af.moddims(a, 6, 2)) display_func(af.flat(a)) display_func(af.flip(a, 0)) display_func(af.flip(a, 1)) display_func(af.lower(a, False)) display_func(af.lower(a, True)) display_func(af.upper(a, False)) display_func(af.upper(a, True)) a = af.randu(5,5) display_func(af.transpose(a)) af.transpose_inplace(a) display_func(a) display_func(af.select(a > 0.3, a, -0.3)) af.replace(a, a > 0.3, -0.3) display_func(a)
def simple_statistics(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) display_func(af.mean(a, dim=0)) display_func(af.mean(a, weights=w, dim=0)) print_func(af.mean(a)) print_func(af.mean(a, weights=w)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) display_func(af.var(a, weights=w, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) print_func(af.var(a, weights=w)) mean, var = af.meanvar(a, dim=0) display_func(mean) display_func(var) mean, var = af.meanvar(a, weights=w, bias=af.VARIANCE.SAMPLE, dim=0) display_func(mean) display_func(var) display_func(af.stdev(a, dim=0)) print_func(af.stdev(a)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) display_func(af.median(a, dim=0)) print_func(af.median(w)) print_func(af.corrcoef(a, b)) data = af.iota(5, 3) k = 3 dim = 0 order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX assert (dim == 0) # topk currently supports first dim only values, indices = af.topk(data, k, dim, order) display_func(values) display_func(indices)
def simple_statistics(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) display_func(af.mean(a, dim=0)) display_func(af.mean(a, weights=w, dim=0)) print_func(af.mean(a)) print_func(af.mean(a, weights=w)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) display_func(af.var(a, weights=w, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) print_func(af.var(a, weights=w)) display_func(af.stdev(a, dim=0)) print_func(af.stdev(a)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) display_func(af.median(a, dim=0)) print_func(af.median(w)) print_func(af.corrcoef(a, b)) data = af.iota(5, 3) k = 3 dim = 0 order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX assert(dim == 0) # topk currently supports first dim only values,indices = af.topk(data, k, dim, order) display_func(values) display_func(indices)
def complex_grid(w, h, zoom, center): x = (af.iota(d0 = 1, d1 = h, tile_dims = (w, 1)) - h/2) / zoom + center[0] y = (af.iota(d0 = w, d1 = 1, tile_dims = (1, h)) - w/2) / zoom + center[1] return af.cplx(x, y)
#load input on device arr = af.np_to_af_array(input.T) print(center_of_mass(arr), ndimage.measurements.center_of_mass(input)) normalizer = af.sum(arr) t_dims = list(arr.dims()) mod_dims = [1] * len(t_dims) for dim in range(len(t_dims)): # swap mod_dims[dim] = t_dims[dim] t_dims[dim] = 1 print(mod_dims, t_dims) grid = af.iota(mod_dims[0], mod_dims[1], mod_dims[2], tile_dims=t_dims) af.display(grid) af.display(af.iota(mod_dims[0], mod_dims[1], mod_dims[2])) # results = [np.sum(input * grids[dir].astype(float)) / normalizer # for dir in range(input.ndim)] # # if numpy.isscalar(results[0]): # return tuple(results) # # return [tuple(v) for v in numpy.array(results).T] # d_type # multiplier = - 0.5 * alpha / pow(sgma[0], 2); # af::array
# # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af af.display(af.constant(100, 3,3, dtype=af.f32)) af.display(af.constant(25, 3,3, dtype=af.c32)) af.display(af.constant(2**50, 3,3, dtype=af.s64)) af.display(af.constant(2+3j, 3,3)) af.display(af.constant(3+5j, 3,3, dtype=af.c32)) af.display(af.range(3, 3)) af.display(af.iota(3, 3, tile_dims=(2,2))) af.display(af.randu(3, 3, 1, 2)) af.display(af.randu(3, 3, 1, 2, af.b8)) af.display(af.randu(3, 3, dtype=af.c32)) af.display(af.randn(3, 3, 1, 2)) af.display(af.randn(3, 3, dtype=af.c32)) af.set_seed(1024) assert(af.get_seed() == 1024) af.display(af.identity(3, 3, 1, 2, af.b8)) af.display(af.identity(3, 3, dtype=af.c32)) a = af.randu(3, 4)
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af af.info() POINTS = 30 N = 2 * POINTS x = (af.iota(d0=N, d1=1, tile_dims=(1, N)) - POINTS) / POINTS y = (af.iota(d0=1, d1=N, tile_dims=(N, 1)) - POINTS) / POINTS win = af.Window(800, 800, "3D Surface example using ArrayFire") t = 0 while not win.close(): t = t + 0.07 z = 10 * x * -af.abs(y) * af.cos(x * x * (y + t)) + af.sin(y * (x + t)) - 1.5 win.surface(x, y, z)
#!/usr/bin/python import arrayfire as af af.print_array(af.constant(100, 3,3, dtype=af.f32)) af.print_array(af.constant(25, 3,3, dtype=af.c32)) af.print_array(af.constant(2**50, 3,3, dtype=af.s64)) af.print_array(af.constant(2+3j, 3,3)) af.print_array(af.constant(3+5j, 3,3, dtype=af.c32)) af.print_array(af.range(3, 3)) af.print_array(af.iota(3, 3, tile_dims=(2,2))) af.print_array(af.randu(3, 3, 1, 2)) af.print_array(af.randu(3, 3, 1, 2, af.b8)) af.print_array(af.randu(3, 3, dtype=af.c32)) af.print_array(af.randn(3, 3, 1, 2)) af.print_array(af.randn(3, 3, dtype=af.c32)) af.set_seed(1024) assert(af.get_seed() == 1024) af.print_array(af.identity(3, 3, 1, 2, af.b8)) af.print_array(af.identity(3, 3, dtype=af.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) af.print_array(a) af.print_array(b)
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af af.info() POINTS = 30 N = 2 * POINTS x = (af.iota(d0 = N, d1 = 1, tile_dims = (1, N)) - POINTS) / POINTS y = (af.iota(d0 = 1, d1 = N, tile_dims = (N, 1)) - POINTS) / POINTS win = af.Window(800, 800, "3D Surface example using ArrayFire") t = 0 while not win.close(): t = t + 0.07 z = 10*x*-af.abs(y) * af.cos(x*x*(y+t))+af.sin(y*(x+t))-1.5; win.surface(x, y, z)
def complex_grid(w, h, zoom, center): x = (af.iota(d0=1, d1=h, tile_dims=(w, 1)) - h / 2) / zoom + center[0] y = (af.iota(d0=w, d1=1, tile_dims=(1, h)) - w / 2) / zoom + center[1] return af.cplx(x, y)