def exercise_matrix(): a = sparse.matrix(10, 7) assert a.n_rows == 10 and a.n_cols == 7 for c in a.cols(): assert c.is_structurally_zero() a[0, 1] = 1. a[9, 5] = 2. assert a.non_zeroes == 2 for i in xrange(10): for j in xrange(7): if (i, j) == (0, 1): assert a[i, j] == 1. elif (i, j) == (9, 5): assert a[i, j] == 2. else: assert a[i, j] == 0, (i, j, a[i, j]) a = sparse.matrix(6, 3) assert a.n_rows == 6 a[1, 1] = 1. a[3, 2] = 2. a[5, 1] = 2. a[4, 0] = 1. assert a.non_zeroes == 4 assert a.n_rows == 6 a[7, 0] = 1. assert a[7, 0] == 0 assert a.n_rows == 6 a = sparse.matrix(4, 3) a[0, 1] = 1.01 b = sparse.matrix(4, 3) b[0, 1] = 1.02 b[3, 2] = 0.001 approx_equal = sparse.approx_equal(tolerance=0.1) assert approx_equal(a, b) m = 10 a = sparse.matrix(m, 2) columns = (sparse.matrix_column(m, { 1: 0.1, 2: 0.2 }), sparse.matrix_column(m, { 4: 0.4, 8: 0.8 })) a[:, 0] = columns[0] a[:, 1] = columns[1] assert a[:, 0], a[:, 1] == columns try: a[1, :] = sparse.vector(2, {1: 1}) raise Exception_expected except RuntimeError, e: assert str(e)
def exercise_matrix(): a = sparse.matrix(10,7) assert a.n_rows == 10 and a.n_cols == 7 for c in a.cols(): assert c.is_structurally_zero() a[0,1] = 1. a[9,5] = 2. assert a.non_zeroes == 2 for i in xrange(10): for j in xrange(7): if (i,j) == (0,1): assert a[i,j] == 1. elif (i,j) == (9,5): assert a[i,j] == 2. else: assert a[i,j] == 0, (i, j, a[i,j]) a = sparse.matrix(6, 3) assert a.n_rows == 6 a[1,1] = 1. a[3,2] = 2. a[5,1] = 2. a[4,0] = 1. assert a.non_zeroes == 4 assert a.n_rows == 6 a[7,0] = 1. assert a[7,0] == 0 assert a.n_rows == 6 a = sparse.matrix(4,3) a[0,1] = 1.01 b = sparse.matrix(4,3) b[0,1] = 1.02 b[3,2] = 0.001 approx_equal = sparse.approx_equal(tolerance=0.1) assert approx_equal(a,b) m = 10 a = sparse.matrix(m, 2) columns = ( sparse.matrix_column(m, {1:0.1, 2:0.2}), sparse.matrix_column(m, {4:0.4, 8:0.8}) ) a[:,0] = columns[0] a[:,1] = columns[1] assert a[:,0], a[:,1] == columns try: a[1,:] = sparse.vector(2, {1:1}) raise Exception_expected except RuntimeError, e: assert str(e)
def _extend_gradient_vectors(results, m, n, keys=("dX_dp", "dY_dp", "dZ_dp")): """Extend results list by n empty results. These will each be a dictionary indexed by the given keys. The value for each key will be an empty vector of size m, to store the derivatives of n parameters, for m reflections. This is the sparse vector version.""" new_results = [{key: sparse.matrix_column(m) for key in keys} for _ in range(n)] results.extend(new_results) return results
def _extend_gradient_vectors(results, m, n, keys=("dX_dp", "dY_dp", "dZ_dp")): """Extend results list by n empty results. These will each be a dictionary indexed by the given keys. The value for each key will be an empty vector of size m, to store the derivatives of n parameters, for m reflections. This is the sparse vector version.""" from scitbx import sparse new_results = [] for i in range(n): result = {} for key in keys: result[key] = sparse.matrix_column(m) new_results.append(result) results.extend(new_results) return results