def sparse_matrix(n, Lambda, projection='false'): if projection == 'false': em1 = af.constant(-1, n**2) em1[n - 1:n**2:n] = 0 ep1 = af.constant(-1, n**2) ep1[n:n**2:n] = 0 sp_matrix = af.identity(n**2, n**2, dtype=af.Dtype.f64) * (1 - 4 * Lambda) sp_matrix[n:, :-n] += Lambda * af.identity(n**2 - n, n**2 - n) sp_matrix[:-n, n:] += Lambda * af.identity(n**2 - n, n**2 - n) for i in range(n**2): for j in range(n**2): if (i == j + 1): sp_matrix[i, j] -= Lambda * em1[j] if (i == j - 1): sp_matrix[i, j] -= Lambda * ep1[j] if projection == 'true': sp_matrix = af.constant(0, n**2, n**2, dtype=af.Dtype.f64) * (1 - 4 * Lambda) ep1 = af.constant(1, n**2, 1, dtype=af.Dtype.f64) ep1[n:n**2:n] = 0 ep1[1:n**2:n] = 2 epn = af.constant(1, n**2, 1, dtype=af.Dtype.f64) epn[0:2 * n] = 2 em1 = af.constant(1, n**2, 1, dtype=af.Dtype.f64) em1[n - 1:n**2:n] = 0 em1[n - 2:n**2:n] = 2 emn = af.constant(1, n**2, 1, dtype=af.Dtype.f64) emn[n**2 - 2 * n:n**2] = 2 d = af.constant(-4, n**2, 1, dtype=af.Dtype.f64) for i in range(n**2): for j in range(n**2): if (i == j): sp_matrix[i, j] = d[i] if (i == j + 1): sp_matrix[i, j] = em1[j] if (i == j + n): sp_matrix[i, j] = emn[j] if (i == j - 1): sp_matrix[i, j] = ep1[i + 1] if (i == j - n): sp_matrix[i, j] = epn[i + n] sp_matrix *= Lambda sp_matrix[-1, :] = 0 sp_matrix[-1, -1] = 1 return sp_matrix
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 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 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 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))
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) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) af.display(a) af.display(b) af.display(c) af.display(af.diag(b, extract = False)) af.display(af.diag(c, 1, extract = False)) af.display(af.tile(a, 2, 2))
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))
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) af.print_array(c) af.print_array(af.diag(b, extract = False)) af.print_array(af.diag(c, 1, extract = False)) af.print_array(af.tile(a, 2, 2))
af.display(p) a = af.randu(5, 3) q, r, t = af.qr(a) af.display(q) af.display(r) af.display(t) af.qr_inplace(a) af.display(a) a = af.randu(5, 5) a = af.matmulTN(a, a) + 10 * af.identity(5, 5) 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)