def integrate(integrand_coeffs): ''' Performs integration according to the given quadrature method by taking in the coefficients of the polynomial and the number of quadrature points. The number of quadrature points and the quadrature scheme are set in params.py module. Parameters ---------- integrand_coeffs : arrayfire.Array [M N 1 1] The coefficients of M number of polynomials of order N arranged in a 2D array. Returns ------- Integral : arrayfire.Array [M 1 1 1] The value of the definite integration performed using the specified quadrature method for M polynomials. ''' integrand = integrand_coeffs if (params.scheme == 'gauss_quadrature'): #print('gauss_quad') gaussian_nodes = params.gauss_points Gauss_weights = params.gauss_weights nodes_tile = af.transpose( af.tile(gaussian_nodes, 1, integrand.shape[1])) power = af.flip(af.range(integrand.shape[1])) nodes_power = af.broadcast(utils.power, nodes_tile, power) weights_tile = af.transpose( af.tile(Gauss_weights, 1, integrand.shape[1])) nodes_weight = nodes_power * weights_tile value_at_gauss_nodes = af.matmul(integrand, nodes_weight) integral = af.sum(value_at_gauss_nodes, 1) if (params.scheme == 'lobatto_quadrature'): #print('lob_quad') lobatto_nodes = params.lobatto_quadrature_nodes Lobatto_weights = params.lobatto_weights_quadrature nodes_tile = af.transpose(af.tile(lobatto_nodes, 1, integrand.shape[1])) power = af.flip(af.range(integrand.shape[1])) nodes_power = af.broadcast(utils.power, nodes_tile, power) weights_tile = af.transpose( af.tile(Lobatto_weights, 1, integrand.shape[1])) nodes_weight = nodes_power * weights_tile value_at_lobatto_nodes = af.matmul(integrand, nodes_weight) integral = af.sum(value_at_lobatto_nodes, 1) return integral
def RK4_timestepping(A_inverse, u, delta_t, gv): ''' Implementing the Runge-Kutta (RK4) method to evolve the wave. Parameters ---------- A_inverse : arrayfire.Array[N_LGL N_LGL 1 1] The inverse of the A matrix which was calculated using A_matrix() function. u : arrayfire.Array[N_LGL N_Elements 1 1] u at the mapped LGL points delta_t : float64 The time-step by which u is to be evolved. Returns ------- delta_u : arrayfire.Array [N_LGL N_Elements 1 1] The change in u at the mapped LGL points. ''' k1 = af.matmul(A_inverse, b_vector(u, gv)) k2 = af.matmul(A_inverse, b_vector(u + k1 * delta_t / 2, gv)) k3 = af.matmul(A_inverse, b_vector(u + k2 * delta_t / 2, gv)) k4 = af.matmul(A_inverse, b_vector(u + k3 * delta_t, gv)) delta_u = delta_t * (k1 + 2 * k2 + 2 * k3 + k4) / 6 return delta_u
def simple_blas(verbose=False): display_func = _util.display_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) display_func(af.matmul(a, b)) display_func(af.matmul(a, b, af.MATPROP.TRANS)) display_func(af.matmul(a, b, af.MATPROP.NONE, af.MATPROP.TRANS)) b = af.randu(5, 1) display_func(af.dot(b, b))
def simple_blas(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5,5) b = af.randu(5,5) display_func(af.matmul(a,b)) display_func(af.matmul(a,b,af.MATPROP.TRANS)) display_func(af.matmul(a,b,af.MATPROP.NONE, af.MATPROP.TRANS)) b = af.randu(5,1) display_func(af.dot(b,b))
def diffusion_step(u, v, Lambda, A_diffusion_inverse): ''' The diffusion step. Lambda = visc * delta_t / (delta_x ** 2) The matlab code uses -lambda ''' length = int(u.shape[0]**0.5) u[:length] -= Lambda u_new = af.matmul(A_diffusion_inverse, u) v_new = af.matmul(A_diffusion_inverse, v) return u_new, v_new
def calc_arrayfire(A, b, x0, maxiter=10): x = af.constant(0, b.dims()[0], dtype=af.Dtype.f32) r = b - af.matmul(A, x) p = r for i in range(maxiter): Ap = af.matmul(A, p) alpha_num = af.dot(r, r) alpha_den = af.dot(p, Ap) alpha = alpha_num / alpha_den r -= af.tile(alpha, Ap.dims()[0]) * Ap x += af.tile(alpha, Ap.dims()[0]) * p beta_num = af.dot(r, r) beta = beta_num / alpha_num p = r + af.tile(beta, p.dims()[0]) * p res = x0 - x return x, af.dot(res, res)
def simulateHestonModel( T, N, R, mu, kappa, vBar, sigmaV, rho, x0, v0 ) : deltaT = T / (float)(N - 1) x = [af.constant(x0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)] v = [af.constant(v0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)] sqrtDeltaT = math.sqrt(deltaT) sqrtOneMinusRhoSquare = math.sqrt(1-rho**2) m = af.constant(0, 2, dtype=af.Dtype.f32) m[0] = rho m[1] = sqrtOneMinusRhoSquare zeroArray = af.constant(0, R, 1, dtype=af.Dtype.f32) for t in range(1, N) : tPrevious = (t + 1) % 2 tCurrent = t % 2 dBt = af.randn(R, 2, dtype=af.Dtype.f32) * sqrtDeltaT vLag = af.maxof(v[tPrevious], zeroArray) sqrtVLag = af.sqrt(vLag) x[tCurrent] = x[tPrevious] + (mu - 0.5 * vLag) * deltaT + sqrtVLag * dBt[:, 0] v[tCurrent] = vLag + kappa * (vBar - vLag) * deltaT + sigmaV * (sqrtVLag * af.matmul(dBt, m)) return (x[tCurrent], af.maxof(v[tCurrent], zeroArray))
def gradient(nn, delta): nabla_b = [] nabla_w = [] # output dact = nn['nonlin'][-1][1] t = nn['zs'][-1] asdf = dact(2) #This is d_relu. It is a binary output dW = average_gradient(delta * af.sign(1e-5 - nn['zs'][-1]), nn['activations'][-2]) nabla_b.append(af.mean(delta)) nabla_w.append(dW) for i in range(len(nn['weights']) - 2, -1, -1): dact = delta * af.max(nn['zs'][i + 1]) trans = nn['weights'][i + 1].T delta = af.matmul(trans, dact) dW = average_gradient(delta, nn['activations'][i]) nabla_b.append(af.mean(delta * af.max(nn['zs'][i]))) nabla_w.append(dW) return nabla_w, nabla_b
def gradient(nn, delta): nabla_b = [] nabla_w = [] # output dact = nn['nonlin'][-1][1] t = nn['zs'][-1] asdf = dact(2) #This is d_relu. It is a binary output dW = average_gradient(delta*af.sign(1e-5 - nn['zs'][-1]), nn['activations'][-2]) nabla_b.append(af.mean(delta)) nabla_w.append(dW) for i in range(len(nn['weights']) - 2, -1, -1): dact = delta * af.max(nn['zs'][i+1]) trans = nn['weights'][i+1].T delta = af.matmul(trans, dact) dW = average_gradient(delta,nn['activations'][i]) nabla_b.append(af.mean(delta*af.max(nn['zs'][i]))) nabla_w.append(dW) return nabla_w, nabla_b
def polyval_1d(polynomials, xi): ''' Finds the value of the polynomials at the given :math:`\\xi` coordinates. Parameters ---------- polynomials : af.Array [number_of_polynomials N 1 1] ``number_of_polynomials`` :math:`2D` polynomials of degree :math:`N - 1` of the form .. math:: P(x) = a_0x^0 + a_1x^1 + ... \\ a_{N - 1}x^{N - 1} + a_Nx^N xi : af.Array [N 1 1 1] :math:`\\xi` coordinates at which the :math:`i^{th}` Lagrange basis polynomial is to be evaluated. Returns ------- af.Array [i.shape[0] xi.shape[0] 1 1] Evaluated polynomials at given :math:`\\xi` coordinates ''' N = int(polynomials.shape[1]) xi_ = af.tile(af.transpose(xi), d0=N) power = af.tile(af.flip(af.range(N), dim=0), d0=1, d1=xi.shape[0]) xi_power = xi_**power return af.matmul(polynomials, xi_power)
def matmul(a: ndarray, b: ndarray) \ -> ndarray: """ Matrix product of two arrays. """ return ndarray(af.matmul(a._af_array, b._af_array))
def calc_arrayfire(A, b, x0, maxiter=10): x = af.constant(0, b.dims()[0], dtype=af.Dtype.f32) r = b - af.matmul(A, x) p = r for i in range(maxiter): Ap = af.matmul(A, p) alpha_num = af.dot(r, r) alpha_den = af.dot(p, Ap) alpha = alpha_num/alpha_den r -= af.tile(alpha, Ap.dims()[0]) * Ap x += af.tile(alpha, Ap.dims()[0]) * p beta_num = af.dot(r, r) beta = beta_num/alpha_num p = r + af.tile(beta, p.dims()[0]) * p af.eval(x) res = x0 - x return x, af.dot(res, res)
def projection_step_poisson(u, v, A_projection_inv): ''' Solve the poisson equation to obtain q for the projection step. ''' A_inverse = A_projection_inv divergence_velocity = div_velocity(u, v) q = af.matmul(A_inverse, divergence_velocity) return q
def forward(nn_np, data): """ Given a dictionary representing a feed forward neural net and an input data matrix compute the network's output and store it within the dictionary :param nn: neural network dictionary :param data: a numpy n by m matrix where m in the number of input units in nn :return: the output layer activations """ nn = nn_np nn['activations'] = [] #prct = [] #prct.append(data[0][0]) #prct.append(data[1][0]) #src = data.astype(float) nsrc = data#af.Array(prct) nn['activations'].append(nsrc) #(af.Array(src.ctypes.data, src.shape, src.dtype.char)) nn['zs'] = [] for w, s, b in map(None, nn['weights'], nn['nonlin'], nn['biases']): #:wq #print af.transpose(nn['activations'][-1]) #print(type(nn['activations'][0])) #print(nn['activations'][0]) z = af.matmul(w, nn['activations'][-1]) #for i in range(w.shape[1]-1): # newB = af.join(1,newB,b) p = z.T + b nn['zs'].append(p.T) if len(p.shape) == 2: maxVal = af.constant(1e-5,p.shape[0],p.shape[1]).T else: maxVal = af.constant(1e-5,p.shape[0]).T q = af.maxof(p.T,1e-5)#p(i) = 1e-5 nn['activations'].append(q) return nn['activations'][-1]
def setup_input(n, sparsity=7): T = af.randu(n, n, dtype=af.Dtype.f32) A = af.floor(T*1000) A = A * ((A % sparsity) == 0) / 1000 A = A.T + A + n*af.identity(n, n, dtype=af.Dtype.f32) x0 = af.randu(n, dtype=af.Dtype.f32) b = af.matmul(A, x0) # printing # nnz = af.sum((A != 0)) # print "Sparsity of A: %2.2f %%" %(100*nnz/n**2,) return A, b, x0
def dot(a, b): # Arrayfire requires that the types match for dot and matmul res_dtype = numpy.result_type(a, b) a = a.astype(res_dtype, copy=False) b = b.astype(res_dtype, copy=False) if a.ndim == 1 and b.ndim == 1: s = arrayfire.dot(a.d_array, b.d_array) return afnumpy.ndarray((), dtype=a.dtype, af_array=s)[()] a_shape = a.shape b_shape = b.shape if a.ndim == 1: a = a.reshape((a.shape[0], 1)) if b.ndim == 1: b = b.reshape((b.shape[0], 1)) if a.ndim == 2 and b.ndim == 2: # Notice the order of the arguments to matmul. It's not a bug! s = arrayfire.matmul(b.d_array, a.d_array) return afnumpy.ndarray(pu.af_shape(s), dtype=pu.typemap(s.dtype()), af_array=s) # Multidimensional dot is done with loops # Calculate the shape of the result array a_shape = list(a_shape) a_shape.pop(-1) b_shape = list(b_shape) b_shape.pop(-2) res_shape = a_shape + b_shape # Make sure the arrays are at least 3D if a.ndim < 3: a = a.reshape((1, ) + a.shape) if b.ndim < 3: b = b.reshape((1, ) + b.shape) # We're going to flatten the regions over which we're going to loop over # to make our life easier and reduce the amount of indexing code a = a.reshape((-1, a.shape[-2], a.shape[-1])) b = b.reshape((-1, b.shape[-2], b.shape[-1])) # Initialize the output array. The shape matches the reshape a and b. res = afnumpy.empty((a.shape[0], a.shape[-2], b.shape[0], b.shape[-1]), dtype=a.dtype) # Loop through the flattened indices and calculate the matmuls for i in range(0, a.shape[0]): for j in range(0, b.shape[0]): res[i, :, j, :] = afnumpy.dot(a[i], b[j]) # Finally appropriately reshape the result array return res.reshape(res_shape)
def setup_input(n, sparsity=7): T = af.randu(n, n, dtype=af.Dtype.f32) A = af.floor(T * 1000) A = A * ((A % sparsity) == 0) / 1000 A = A.T + A + n * af.identity(n, n, dtype=af.Dtype.f32) x0 = af.randu(n, dtype=af.Dtype.f32) b = af.matmul(A, x0) # printing # nnz = af.sum((A != 0)) # print "Sparsity of A: %2.2f %%" %(100*nnz/n**2,) return A, b, x0
def dot(a, b): # Arrayfire requires that the types match for dot and matmul res_dtype = numpy.result_type(a,b) a = a.astype(res_dtype, copy=False) b = b.astype(res_dtype, copy=False) if a.ndim == 1 and b.ndim == 1: s = arrayfire.dot(a.d_array, b.d_array) return afnumpy.ndarray((), dtype=a.dtype, af_array=s)[()] a_shape = a.shape b_shape = b.shape if a.ndim == 1: a = a.reshape((a.shape[0],1)) if b.ndim == 1: b = b.reshape((b.shape[0],1)) if a.ndim == 2 and b.ndim == 2: # Notice the order of the arguments to matmul. It's not a bug! s = arrayfire.matmul(b.d_array, a.d_array) return afnumpy.ndarray(pu.af_shape(s), dtype=pu.typemap(s.dtype()), af_array=s) # Multidimensional dot is done with loops # Calculate the shape of the result array a_shape = list(a_shape) a_shape.pop(-1) b_shape = list(b_shape) b_shape.pop(-2) res_shape = a_shape + b_shape # Make sure the arrays are at least 3D if a.ndim < 3: a = a.reshape((1,)+a.shape) if b.ndim < 3: b = b.reshape((1,)+b.shape) # We're going to flatten the regions over which we're going to loop over # to make our life easier and reduce the amount of indexing code a = a.reshape((-1,a.shape[-2],a.shape[-1])) b = b.reshape((-1,b.shape[-2],b.shape[-1])) # Initialize the output array. The shape matches the reshape a and b. res = afnumpy.empty((a.shape[0], a.shape[-2], b.shape[0], b.shape[-1]), dtype=a.dtype) # Loop through the flattened indices and calculate the matmuls for i in range(0,a.shape[0]): for j in range(0,b.shape[0]): res[i,:,j,:] = afnumpy.dot(a[i],b[j]) # Finally appropriately reshape the result array return res.reshape(res_shape)
def conv(weights, biases, image, wx, wy, sx = 1, sy = 1, px = 0, py = 0, groups = 1): image = __pad(image, px, py) batch = util.num_input(image) n_filters = util.num_filters(weights) n_channel = util.num_channels(weights) w_i = image.dims()[0] h_i = image.dims()[1] w_o = (w_i - wx) / sx + 1 h_o = (h_i - wy) / sy + 1 tiles = af.unwrap(image, wx, wy, sx, sy) weights = af.moddims(weights, wx*wy, n_channel, n_filters) out = af.constant(0, batch, n_filters, w_o, h_o) if groups > 1: out = af.constant(0, w_o, h_o, n_filters, batch) split_in = util.num_channels(image) / groups s_i = split_in split_out = n_filters / groups s_o = split_out for i in xrange(groups): weights_slice = af.moddims(weights[:,:,i*s_o:(i+1)*s_o], wx, wy, n_channel, split_out) biases_slice = biases[i*s_o:(i+1)*s_o] image_slice = image[:,:,i*s_i:(i+1)*s_i] out[:,:,i*s_o:(i+1)*s_o] = conv(weights_slice, biases_slice, image_slice, wx, wy, sx, sy, 0, 0, 1) # out[:,i*s_o:(i+1)*s_o] = conv(weights_slice, # biases_slice, # image_slice, # wx, wy, sx, sy, 0, 0, 1) return out #TODO: Speedup this section for f in xrange(n_filters): for d in xrange(n_channel): tile_d = af.reorder(tiles[:,:,d],1,0) weight_d = weights[:,d,f] out[0,f] += af.moddims(af.matmul(tile_d, weight_d), 1,1, w_o, h_o) out[0,f] += biases[f].to_list()[0] return af.reorder(out, 2, 3, 1, 0)
def fc(weights, biases, image): #TODO: fix shape of fc weights #TODO: fix handling 1d vs 3d image #TODO: remove assumption of only 1 input s = image.dims()[0] if len(image.dims()) is 3: in0, in1, in2 = image.dims() image = af.reorder(image, 1, 0, 2) s = in0*in1*in2 image = af.moddims(image, 1, s) d0, d1, d2, d3 = weights.dims() weights = af.moddims(weights, d2, d3) out = af.moddims(af.matmul(image, weights), d3) + biases return out
def multivariable_poly_value(poly_2d, x, y): ''' ''' polynomial_coeffs = af.transpose(af.moddims(poly_2d, poly_2d.shape[0] ** 2, poly_2d.shape[2])) power_index = af.flip(af.np_to_af_array(np.arange(poly_2d.shape[0]))) x_power_indices = af.flat(af.transpose(af.tile(power_index, 1, poly_2d.shape[0]))) y_power_indices = af.tile(power_index, poly_2d.shape[0]) x_power = af.broadcast(power, af.transpose(x), x_power_indices) y_power = af.broadcast(power, af.transpose(y), y_power_indices) polynomial_value = af.matmul(polynomial_coeffs, x_power * y_power) return polynomial_value
def forward(nn_np, data): """ Given a dictionary representing a feed forward neural net and an input data matrix compute the network's output and store it within the dictionary :param nn: neural network dictionary :param data: a numpy n by m matrix where m in the number of input units in nn :return: the output layer activations """ nn = nn_np nn['activations'] = [] #prct = [] #prct.append(data[0][0]) #prct.append(data[1][0]) #src = data.astype(float) nsrc = data #af.Array(prct) nn['activations'].append(nsrc) #(af.Array(src.ctypes.data, src.shape, src.dtype.char)) nn['zs'] = [] for w, s, b in map(None, nn['weights'], nn['nonlin'], nn['biases']): #:wq #print af.transpose(nn['activations'][-1]) #print(type(nn['activations'][0])) #print(nn['activations'][0]) z = af.matmul(w, nn['activations'][-1]) #for i in range(w.shape[1]-1): # newB = af.join(1,newB,b) p = z.T + b nn['zs'].append(p.T) if len(p.shape) == 2: maxVal = af.constant(1e-5, p.shape[0], p.shape[1]).T else: maxVal = af.constant(1e-5, p.shape[0]).T q = af.maxof(p.T, 1e-5) #p(i) = 1e-5 nn['activations'].append(q) return nn['activations'][-1]
def af_gauss(data,kernels): d_kernels = [] for k in kernels: d_k = af.np_to_af_array(k) d_kernels.append(af.matmul(d_k,af.transpose(d_k))) ##d_k = af.matmul(af.transpose(d_k),d_k) out = [] for d in data: d_img = af.np_to_af_array(d) for d_k in d_kernels: #res = af.convolve2_separable(d_k, af.transpose(d_k), d_img) res = af.convolve2(d_img, d_k) # create numpy array out.append(res.__array__()) return out
def simulateHestonModel(T, N, R, mu, kappa, vBar, sigmaV, rho, x0, v0): deltaT = T / (float)(N - 1) x = [ af.constant(x0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32) ] v = [ af.constant(v0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32) ] sqrtDeltaT = math.sqrt(deltaT) sqrtOneMinusRhoSquare = math.sqrt(1 - rho**2) m = af.constant(0, 2, dtype=af.Dtype.f32) m[0] = rho m[1] = sqrtOneMinusRhoSquare zeroArray = af.constant(0, R, 1, dtype=af.Dtype.f32) for t in range(1, N): tPrevious = (t + 1) % 2 tCurrent = t % 2 dBt = af.randn(R, 2, dtype=af.Dtype.f32) * sqrtDeltaT vLag = af.maxof(v[tPrevious], zeroArray) sqrtVLag = af.sqrt(vLag) x[tCurrent] = x[tPrevious] + ( mu - 0.5 * vLag) * deltaT + sqrtVLag * dBt[:, 0] v[tCurrent] = vLag + kappa * (vBar - vLag) * deltaT + sigmaV * ( sqrtVLag * af.matmul(dBt, m)) return (x[tCurrent], af.maxof(v[tCurrent], zeroArray))
def simple_lapack(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) l, u, p = af.lu(a) display_func(l) display_func(u) display_func(p) p = af.lu_inplace(a, "full") display_func(a) display_func(p) a = af.randu(5, 3) q, r, t = af.qr(a) display_func(q) display_func(r) display_func(t) af.qr_inplace(a) display_func(a) a = af.randu(5, 5) a = af.matmulTN(a, a.copy()) + 10 * af.identity(5, 5) R, info = af.cholesky(a) display_func(R) print_func(info) af.cholesky_inplace(a) display_func(a) a = af.randu(5, 5) ai = af.inverse(a) display_func(a) display_func(ai) x0 = af.randu(5, 3) b = af.matmul(a, x0) x1 = af.solve(a, b) display_func(x0) display_func(x1) p = af.lu_inplace(a) x2 = af.solve_lu(a, p, b) display_func(x2) print_func(af.rank(a)) print_func(af.det(a)) print_func(af.norm(a, af.NORM.EUCLID)) print_func(af.norm(a, af.NORM.MATRIX_1)) print_func(af.norm(a, af.NORM.MATRIX_INF)) print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1)) a = af.randu(10, 10) display_func(a) u, s, vt = af.svd(a) display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt)) u, s, vt = af.svd_inplace(a) display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
def predict_proba(self, X): Z = af.matmul(X, self.__weights) return af.sigmoid(Z)
import sys from array import array def af_assert(left, right, eps=1E-6): if (af.max(af.abs(left -right)) > eps): raise ValueError("Arrays not within dictated precision") return if __name__ == "__main__": if (len(sys.argv) > 1): af.set_device(int(sys.argv[1])) af.info() h_dx = array('f', (1.0/12, -8.0/12, 0, 8.0/12, 1.0/12)) h_spread = array('f', (1.0/5, 1.0/5, 1.0/5, 1.0/5, 1.0/5)) img = af.randu(640, 480) dx = af.Array(h_dx, (5,1)) spread = af.Array(h_spread, (1, 5)) kernel = af.matmul(dx, spread) full_res = af.convolve2(img, kernel) sep_res = af.convolve2_separable(dx, spread, img) af_assert(full_res, sep_res) print("full 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2, img, kernel))) print("separable 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2_separable, dx, spread, img)))
def simple_lapack(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5,5) l,u,p = af.lu(a) display_func(l) display_func(u) display_func(p) p = af.lu_inplace(a, "full") display_func(a) display_func(p) a = af.randu(5,3) q,r,t = af.qr(a) display_func(q) display_func(r) display_func(t) af.qr_inplace(a) display_func(a) a = af.randu(5, 5) a = af.matmulTN(a, a) + 10 * af.identity(5,5) R,info = af.cholesky(a) display_func(R) print_func(info) af.cholesky_inplace(a) display_func(a) a = af.randu(5,5) ai = af.inverse(a) display_func(a) display_func(ai) x0 = af.randu(5, 3) b = af.matmul(a, x0) x1 = af.solve(a, b) display_func(x0) display_func(x1) p = af.lu_inplace(a) x2 = af.solve_lu(a, p, b) display_func(x2) print_func(af.rank(a)) print_func(af.det(a)) print_func(af.norm(a, af.NORM.EUCLID)) print_func(af.norm(a, af.NORM.MATRIX_1)) print_func(af.norm(a, af.NORM.MATRIX_INF)) print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1))
def bench(A, iters=100): start = time() for t in range(iters): B = af.matmul(A, A) af.sync() return (time() - start) / iters
R, info = af.cholesky(a) af.display(R) print(info) af.cholesky_inplace(a) af.display(a) a = af.randu(5, 5) ai = af.inverse(a) af.display(a) af.display(ai) x0 = af.randu(5, 3) b = af.matmul(a, x0) x1 = af.solve(a, b) af.display(x0) af.display(x1) p = af.lu_inplace(a) x2 = af.solve_lu(a, p, b) af.display(x2) print(af.rank(a)) print(af.det(a)) print(af.norm(a, af.AF_NORM_EUCLID)) print(af.norm(a, af.AF_NORM_MATRIX_1))
#!/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 a = af.randu(5, 5) b = af.randu(5, 5) af.display(af.matmul(a, b)) af.display(af.matmul(a, b, af.AF_MAT_TRANS)) af.display(af.matmul(a, b, af.AF_MAT_NONE, af.AF_MAT_TRANS)) b = af.randu(5, 1) af.display(af.dot(b, b))
def af_assert(left, right, eps=1E-6): if (af.max(af.abs(left - right)) > eps): raise ValueError("Arrays not within dictated precision") return if __name__ == "__main__": if (len(sys.argv) > 1): af.set_device(int(sys.argv[1])) af.info() h_dx = array('f', (1.0 / 12, -8.0 / 12, 0, 8.0 / 12, 1.0 / 12)) h_spread = array('f', (1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5, 1.0 / 5)) img = af.randu(3072, 3072) dx = af.Array(h_dx, (5, 1)) spread = af.Array(h_spread, (1, 5)) kernel = af.matmul(dx, spread) full_res = af.convolve2(img, kernel) sep_res = af.convolve2_separable(dx, spread, img) af_assert(full_res, sep_res) print("full 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2, img, kernel))) print("separable 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2_separable, dx, spread, img)))
def bench(A, iters = 100): start = time() for t in range(iters): B = af.matmul(A, A) af.sync() return (time() - start) / iters
def predict_prob(X, Weights): Z = af.matmul(X, Weights) return af.sigmoid(Z)
def run(iters): for t in range(iters): B = af.matmul(A, A) af.sync()
def volume_integral_flux(u_n): ''' Calculates the volume integral of flux in the wave equation. :math:`\\int_{-1}^1 f(u) \\frac{d L_p}{d\\xi} d\\xi` This will give N values of flux integral as p varies from 0 to N - 1. This integral is carried out using the analytical form of the integrand obtained as a linear combination of Lagrange basis polynomials. This integrand is the used in the integrate() function. Calculation of volume integral flux using N_LGL Lobatto quadrature points can be vectorized and is much faster. Parameters ---------- u : arrayfire.Array [N_LGL N_Elements M 1] Amplitude of the wave at the mapped LGL nodes of each element. This function can computer flux for :math:`M` :math:`u`. Returns ------- flux_integral : arrayfire.Array [N_LGL N_Elements M 1] Value of the volume integral flux. It contains the integral of all N_LGL * N_Element integrands. ''' shape_u_n = utils.shape(u_n) # The coefficients of dLp / d\xi diff_lag_coeff = params.dl_dxi_coeffs lobatto_nodes = params.lobatto_quadrature_nodes Lobatto_weights = params.lobatto_weights_quadrature nodes_tile = af.transpose( af.tile(lobatto_nodes, 1, diff_lag_coeff.shape[1])) power = af.flip(af.range(diff_lag_coeff.shape[1])) power_tile = af.tile(power, 1, params.N_quad) nodes_power = nodes_tile**power_tile weights_tile = af.transpose( af.tile(Lobatto_weights, 1, diff_lag_coeff.shape[1])) nodes_weight = nodes_power * weights_tile dLp_dxi = af.matmul(diff_lag_coeff, nodes_weight) # The first option to calculate the volume integral term, directly uses # the Lobatto quadrature instead of using the integrate() function by # passing the coefficients of the Lagrange interpolated polynomial. if(params.volume_integral_scheme == 'lobatto_quadrature' \ and params.N_quad == params.N_LGL): # Flux using u_n, reordered to 1 X N_LGL X N_Elements array. F_u_n = af.reorder(flux_x(u_n), 3, 0, 1, 2) F_u_n = af.tile(F_u_n, d0=params.N_LGL) # Multiplying with dLp / d\xi integral_expansion = af.broadcast(utils.multiply, dLp_dxi, F_u_n) # # Using the quadrature rule. flux_integral = af.sum(integral_expansion, 1) flux_integral = af.reorder(flux_integral, 0, 2, 3, 1) # Using the integrate() function to calculate the volume integral term # by passing the Lagrange interpolated polynomial. else: #print('option3') analytical_flux_coeffs = af.transpose(af.moddims(u_n, d0 = params.N_LGL, d1 = params.N_Elements \ * shape_u_n[2])) analytical_flux_coeffs = flux_x( lagrange.lagrange_interpolation(analytical_flux_coeffs)) analytical_flux_coeffs = af.transpose( af.moddims(af.transpose(analytical_flux_coeffs), d0=params.N_LGL, d1=params.N_Elements, d2=shape_u_n[2])) analytical_flux_coeffs = af.reorder(analytical_flux_coeffs, d0=3, d1=1, d2=0, d3=2) analytical_flux_coeffs = af.tile(analytical_flux_coeffs, d0=params.N_LGL) analytical_flux_coeffs = af.moddims( af.transpose(analytical_flux_coeffs), d0=params.N_LGL, d1=params.N_LGL * params.N_Elements, d2=1, d3=shape_u_n[2]) analytical_flux_coeffs = af.moddims(analytical_flux_coeffs, d0=params.N_LGL, d1=params.N_LGL * params.N_Elements * shape_u_n[2], d2=1, d3=1) analytical_flux_coeffs = af.transpose(analytical_flux_coeffs) dl_dxi_coefficients = af.tile(af.tile(params.dl_dxi_coeffs, d0 = params.N_Elements), \ d0 = shape_u_n[2]) volume_int_coeffs = utils.poly1d_product(dl_dxi_coefficients, analytical_flux_coeffs) flux_integral = lagrange.integrate(volume_int_coeffs) flux_integral = af.moddims(af.transpose(flux_integral), d0=params.N_LGL, d1=params.N_Elements, d2=shape_u_n[2]) return flux_integral
def __mul__(self, other): other = afnp.asarray(other).astype(self.dtype) s = arrayfire.matmul(self.d_array, other.d_array) a = afnp.ndarray(pu.af_shape(s), dtype=pu.typemap(s.dtype()), af_array=s) a._eval() return a
def _predict_proba(self, X: af.Array, Weights: af.Array) -> af.Array: Z = af.matmul(X, Weights) return af.sigmoid(Z)