def polyval_2d(poly_2d, xi, eta): ''' ''' poly_2d_shape = poly_2d.shape poly_xy = af.tile(poly_2d, d0 = 1, d1 = 1, d2 = 1, d3 = xi.shape[0]) poly_xy_shape = poly_xy.shape # print(poly_xy) xi_power = af.flip(af.range(poly_xy_shape[1], dtype = af.Dtype.u32)) xi_power = af.tile(af.transpose(xi_power), d0 = poly_xy_shape[0]) xi_power = af.tile(xi_power, d0 = 1, d1 = 1, d2 = xi.shape[0]) eta_power = af.flip(af.range(poly_xy_shape[0], dtype = af.Dtype.u32)) eta_power = af.tile(eta_power, d0 = 1, d1 = poly_xy_shape[1]) eta_power = af.tile(eta_power, d0 = 1, d1 = 1, d2 = eta.shape[0]) Xi = af.reorder(xi, d0 = 2, d1 = 1, d2 = 0) Xi = af.tile(Xi, d0 = poly_xy_shape[0], d1 = poly_xy_shape[1]) Xi = af.pow(Xi, xi_power) Xi = af.reorder(Xi, d0 = 0, d1 = 1, d2 = 3, d3 = 2) # print(Xi) Eta = af.reorder(eta, d0 = 2, d1 = 1, d2 = 0) Eta = af.tile(Eta, d0 = poly_xy_shape[0], d1 = poly_xy_shape[1]) Eta = af.pow(Eta, eta_power) Eta = af.reorder(Eta, d0 = 0, d1 = 1, d2 = 3, d3 = 2) # print(Eta) Xi_Eta = Xi * Eta poly_val = af.broadcast(multiply, poly_xy, Xi_Eta) poly_val = af.sum(af.sum(poly_val, dim = 1), dim = 0) poly_val = af.reorder(poly_val, d0 = 2, d1 = 3, d2 = 0, d3 = 1) return poly_val
def __rpow__(self, other): if(isinstance(other, numbers.Number) and numpy.issubdtype(type(other), numpy.float) and numpy.issubdtype(self.dtype, numpy.integer)): # AF does not automatically upconvert A**0.5 to float for integer arrays s = arrayfire.pow(pu.raw(other), self.astype(type(other)).d_array) else: s = arrayfire.pow(pu.raw(other), self.d_array) return ndarray(self.shape, dtype=pu.typemap(s.dtype()), af_array=s)
def __rpow__(self, other): if (isinstance(other, numbers.Number) and numpy.issubdtype(type(other), numpy.float) and numpy.issubdtype(self.dtype, numpy.integer)): # AF does not automatically upconvert A**0.5 to float for integer arrays s = arrayfire.pow(pu.raw(other), self.astype(type(other)).d_array) else: s = arrayfire.pow(pu.raw(other), self.d_array) a = ndarray(self.shape, dtype=pu.typemap(s.dtype()), af_array=s) a._eval() return a
def gaussian(dims, sigmas, **kwargs): alpha = 1.0 grid = af.constant(1.0, dims[0]) multiplier = -0.5 * alpha / pow(sigmas[0], 2) exponent = af.pow( (af.range(dims[0], dim=0) - (dims[0] - 1) / 2.0), 2) * multiplier grid = grid * af.arith.exp(exponent) grid_tot = af.sum(grid, dim=0) grid_total = af.tile(grid_tot, dims[0]) grid = grid / grid_total return grid
def lrn(image, size, alpha, beta): #TODO: Remove assumption of image.dims()[3] == 1 d0, d1, d2 = image.dims() #TODO: remove reorder and moddims call image = af.moddims(af.reorder(image, 2, 0, 1), 1, d2, d0, d1) d0, d1, d2, d3 = image.dims() pad = (size - 1) / 2 padded_size = d1+2*pad out = af.constant(0,d1, d2, d3) image = af.moddims(image, d1, d2, d3) padded = af.moddims(af.unwrap(image, padded_size, d2, padded_size, d2, pad,0), padded_size, d2, d2) padded = padded*padded for i in xrange(size): out += padded[i:i+d1] out = 1 + (float(alpha)/size) * out out = af.pow(out,beta) #TODO: remove reorder call return af.reorder(af.moddims(image/out, d0,d1,d2,d3), 2, 3, 1, 0)
def simple_arith(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3,3,dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a ** b d = a d **= b display_func(c ** 2.0) display_func(3.0 ** d) display_func(a ** 2) display_func(3 ** a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a,b)) display_func(af.minof(a,b)) display_func(af.rem(a,b)) a = af.randu(3,3) - 0.5 b = af.randu(3,3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a,b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a/b)) display_func(af.isnan(a/a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x,y: x+y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, b))
def simple_arith(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) b = af.constant(4, 3, 3) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a**b d = a d **= b display_func(c**2.0) display_func(3.0**d) display_func(a**2) display_func(3**a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) a = af.randu(3, 3, dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a, b)) display_func(af.minof(a, b)) display_func(af.rem(a, b)) a = af.randu(3, 3) - 0.5 b = af.randu(3, 3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a, b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.sigmoid(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3, 3) - 1) b = af.round(5 * af.randu(3, 3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a / b)) display_func(af.isnan(a / a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x, y: x + y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, b))
af.display(af.real(d)) af.display(af.imag(d)) af.display(af.conjg(d)) af.display(af.sinh(a)) af.display(af.cosh(a)) af.display(af.tanh(a)) af.display(af.asinh(a)) af.display(af.acosh(a)) af.display(af.atanh(a)) a = af.abs(a) b = af.abs(b) af.display(af.root(a, b)) af.display(af.pow(a, b)) af.display(af.pow2(a)) af.display(af.exp(a)) af.display(af.expm1(a)) af.display(af.erf(a)) af.display(af.erfc(a)) af.display(af.log(a)) af.display(af.log1p(a)) af.display(af.log10(a)) af.display(af.log2(a)) af.display(af.sqrt(a)) af.display(af.cbrt(a)) a = af.round(5 * af.randu(3, 3) - 1) b = af.round(5 * af.randu(3, 3) - 1)
def square(arr): return af.pow(arr, 2)
af.display(af.real(d)) af.display(af.imag(d)) af.display(af.conjg(d)) af.display(af.sinh(a)) af.display(af.cosh(a)) af.display(af.tanh(a)) af.display(af.asinh(a)) af.display(af.acosh(a)) af.display(af.atanh(a)) a = af.abs(a) b = af.abs(b) af.display(af.root(a, b)) af.display(af.pow(a, b)) af.display(af.pow2(a)) af.display(af.exp(a)) af.display(af.expm1(a)) af.display(af.erf(a)) af.display(af.erfc(a)) af.display(af.log(a)) af.display(af.log1p(a)) af.display(af.log10(a)) af.display(af.log2(a)) af.display(af.sqrt(a)) af.display(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1)