def exercise_matrix_x_vector(): from scitbx.random import variate, uniform_distribution for m,n in [(5,5), (3,5), (5,3)]: random_vectors = variate( sparse.vector_distribution( n, density=0.4, elements=uniform_distribution(min=-2, max=2))) random_matrices = variate( sparse.matrix_distribution( m, n, density=0.3, elements=uniform_distribution(min=-2, max=2))) for n_test in xrange(50): a = random_matrices.next() x = random_vectors.next() y = a*x aa = a.as_dense_matrix() xx = x.as_dense_vector() yy1 = y.as_dense_vector() yy2 = aa.matrix_multiply(xx) assert approx_equal(yy1,yy2) for m,n in [(5,5), (3,5), (5,3)]: random_matrices = variate( sparse.matrix_distribution( m, n, density=0.4, elements=uniform_distribution(min=-2, max=2))) for n_test in xrange(50): a = random_matrices.next() x = flex.random_double(n) y = a*x aa = a.as_dense_matrix() yy = aa.matrix_multiply(x) assert approx_equal(y, yy)
def exercise_random(): from scitbx.random import variate, uniform_distribution g = random_matrices = variate( sparse.matrix_distribution(5, 3, density=0.4, elements=uniform_distribution(min=-1, max=0.5))) for a in itertools.islice(g, 10): assert a.n_rows == 5 and a.n_cols == 3 assert approx_equal(a.non_zeroes, a.n_rows * a.n_cols * 0.4, eps=1) for j in range(a.n_cols): for i, x in a.col(j): assert -1 <= x < 0.5, (i, j, x) g = random_vectors = variate( sparse.vector_distribution(6, density=0.3, elements=uniform_distribution(min=-2, max=2))) for v in itertools.islice(g, 10): assert v.size == 6 assert approx_equal(v.non_zeroes, v.size * 0.3, eps=1) for i, x in v: assert -2 <= x < 2, (i, j, x)
def exercise_matrix_x_matrix(): from scitbx.random import variate, uniform_distribution mat = lambda m, n: variate( sparse.matrix_distribution( m, n, density=0.4, elements=uniform_distribution(min=-10, max=10)) )() a, b = mat(3, 4), mat(4, 2) c = a * b aa, bb, cc = [m.as_dense_matrix() for m in (a, b, c)] cc1 = aa.matrix_multiply(bb) assert approx_equal(cc, cc1)
def exercise_matrix_x_matrix(): from scitbx.random import variate, uniform_distribution mat = lambda m,n: variate( sparse.matrix_distribution( m, n, density=0.4, elements=uniform_distribution(min=-10, max=10)))() a,b = mat(3,4), mat(4,2) c = a*b aa, bb, cc = [ m.as_dense_matrix() for m in (a,b,c) ] cc1 = aa.matrix_multiply(bb) assert approx_equal(cc, cc1)
def exercise_a_b_a_tr(): from scitbx.random import variate, uniform_distribution for m,n in [(5,5), (3,5), (5,3)]: random_matrices = variate( sparse.matrix_distribution( m, n, density=0.6, elements=uniform_distribution(min=-3, max=10))) for n_test in xrange(50): b = flex.random_double(n*(n+1)//2) a = random_matrices.next() c = a.self_times_symmetric_times_self_transpose(b) aa = a.as_dense_matrix() bb = b.matrix_packed_u_as_symmetric() cc = c.matrix_packed_u_as_symmetric() assert approx_equal( cc, aa.matrix_multiply(bb.matrix_multiply(aa.matrix_transpose())))
def exercise_random(): from scitbx.random import variate, uniform_distribution g = random_matrices = variate( sparse.matrix_distribution( 5, 3, density=0.4, elements=uniform_distribution(min=-1, max=0.5))) for a in itertools.islice(g, 10): assert a.n_rows== 5 and a.n_cols == 3 assert approx_equal(a.non_zeroes, a.n_rows*a.n_cols*0.4, eps=1) for j in xrange(a.n_cols): for i,x in a.col(j): assert -1 <= x < 0.5, (i,j, x) g = random_vectors = variate( sparse.vector_distribution( 6, density=0.3, elements=uniform_distribution(min=-2, max=2))) for v in itertools.islice(g, 10): assert v.size == 6 assert approx_equal(v.non_zeroes, v.size*0.3, eps=1) for i,x in v: assert -2 <= x < 2, (i,j, x)
def exercise_a_tr_diag_a_and_a_diag_a_tr(): from scitbx.random import variate, uniform_distribution for m, n in [(5, 5), (3, 5), (5, 3)]: random_matrices = variate( sparse.matrix_distribution(m, n, density=0.6, elements=uniform_distribution(min=-3, max=10))) w = flex.double_range(0, m) ww = flex.double(m * m) ww.resize(flex.grid(m, m)) ww.matrix_diagonal_set_in_place(diagonal=w) for n_test in range(50): a = next(random_matrices) b = a.self_transpose_times_diagonal_times_self(w) aa = a.as_dense_matrix() bb = b.as_dense_matrix() assert approx_equal( bb, aa.matrix_transpose().matrix_multiply(ww).matrix_multiply(aa)) c = a.transpose().self_times_diagonal_times_self_transpose(w) cc = c.as_dense_matrix() assert approx_equal(cc, bb)