def compute_restraints_functional_gradients_and_curvatures(self): '''use the restraints_parameterisation object, if present, to calculate the least squares restraints objective plus gradients and approximate curvatures''' if not self._restraints_parameterisation: return None residuals, jacobian, weights = \ self._restraints_parameterisation.get_residuals_gradients_and_weights() w_resid = weights * residuals residuals2 = residuals * residuals # calculate target function L = 0.5 * flex.sum(weights * residuals2) # calculate gradients using the scalar product of sparse vector col with # dense vector w_resid dL_dp = [col * w_resid for col in jacobian.cols()] # calculate lsq approximation to curvatures using weighted dot product curvs = [ sparse.weighted_dot(col, weights, col) for col in jacobian.cols() ] return (L, dL_dp, curvs)
def compute_restraints_functional_gradients_and_curvatures(self): '''use the restraints_parameterisation object, if present, to calculate the least squares restraints objective plus gradients and approximate curvatures''' if not self._restraints_parameterisation: return None residuals, jacobian, weights = \ self._restraints_parameterisation.get_residuals_gradients_and_weights() w_resid = weights * residuals residuals2 = residuals * residuals # calculate target function L = 0.5 * flex.sum(weights * residuals2) # calculate gradients using the scalar product of sparse vector col with # dense vector w_resid dL_dp = [col * w_resid for col in jacobian.cols()] # calculate lsq approximation to curvatures using weighted dot product curvs = [sparse.weighted_dot(col, weights, col) for col in jacobian.cols()] return (L, dL_dp, curvs)
def exercise_vector(): v = sparse.vector(5) assert v.size == 5 assert v.is_structurally_zero() v[1] = 2 v[2] = 0 v[3] = 6 assert list(v) == [(1, 2.), (2, 0.), (3, 6.)] assert list(v.compact()) == [(1, 2.), (2, 0.), (3, 6.)] assert [v[i] for i in xrange(5)] == [0, 2, 0, 6, 0] p = flex.size_t([1, 2, 3, 4, 0]) assert list(v.permute(p)) == [(2, 2.), (3, 0.), (4, 6.)] assert v.non_zeroes == 3 v = sparse.vector(10) v[7] = -5 v[1] = -1 v[4] = 0 v[1] = 2 v[9] = 9. v[7] = 6 v[4] = 1 v[1] = 3 v[4] = 0 assert list(v.compact()) == [(1, 3.), (4, 0.), (7, 6.), (9, 9.)] assert ([v.is_structural_zero(i) for i in xrange(10)] == [ True, False, True, True, False, True, True, False, True, False ]) v = sparse.vector(10) v[4] += 1 v[5] += 2 v[4] += 2 v[5] = 1 v[3] = 2 v[5] += 3 assert list(v.compact()) == [(3, 2.), (4, 3.), (5, 4.)] assert v.non_zeroes == 3 v = sparse.vector(6) v[3] = 1 v[2] = 1 v[5] = 1 assert v.size == 6 v[7] = 1 assert v[7] == 0 assert v.size == 6 u = flex.double((1, -1, 2, 0, -2)) v = sparse.vector(5) v[0] = 10 v[3] = 4 v[4] = 5 assert u * v == 0 u = sparse.vector(10, {1: 1, 3: 3, 7: 7}) v = sparse.vector(10, {0: -1, 1: 2, 7: -1, 8: 2}) assert u * v == -5 assert sparse.weighted_dot(u, flex.double_range(10), v) == -47 a = flex.double() for i in xrange(10): for j in xrange(i, 10): a.append(1 / (i + j + 1)) assert approx_equal(sparse.quadratic_form(u, a, v), 4003 / 1980, eps=1e-15) assert approx_equal(sparse.quadratic_form(a, u), sparse.quadratic_form(u, a, u), eps=1e-15) sparse_approx_equal = sparse.approx_equal(tolerance=0.1) u = sparse.vector(4) u[0] = 1.01 v = sparse.vector(4) v[0] = 1.02 v[3] = 0.001 assert sparse_approx_equal(u, v) u = sparse.vector(4) v = sparse.vector(4) v[3] = 0.001 assert sparse_approx_equal(u, v) u = sparse.vector(5, {3: 0.3, 1: 0.1}) assert list(u.as_dense_vector()) == [0, 0.1, 0, 0.3, 0] try: sparse.vector(4, [1, 2, 3, 4]) raise Exception_expected except Exception, e: assert e.__class__.__module__ == 'Boost.Python' assert e.__class__.__name__ == 'ArgumentError'
def exercise_vector(): v = sparse.vector(5) assert v.size == 5 assert v.is_structurally_zero() v[1] = 2 v[2] = 0 v[3] = 6 assert list(v) == [(1, 2.), (2, 0.), (3, 6.)] assert list(v.compact()) == [(1, 2.), (2, 0.), (3, 6.)] assert [v[i] for i in range(5)] == [0, 2, 0, 6, 0] p = flex.size_t([1, 2, 3, 4, 0]) assert list(v.permute(p)) == [(2, 2.), (3, 0.), (4, 6.)] assert v.non_zeroes == 3 v = sparse.vector(10) v[7] = -5 v[1] = -1 v[4] = 0 v[1] = 2 v[9] = 9. v[7] = 6 v[4] = 1 v[1] = 3 v[4] = 0 assert list(v.compact()) == [(1, 3.), (4, 0.), (7, 6.), (9, 9.)] assert ([v.is_structural_zero(i) for i in range(10)] == [ True, False, True, True, False, True, True, False, True, False ]) v = sparse.vector(10) v[4] += 1 v[5] += 2 v[4] += 2 v[5] = 1 v[3] = 2 v[5] += 3 assert list(v.compact()) == [(3, 2.), (4, 3.), (5, 4.)] assert v.non_zeroes == 3 v = sparse.vector(6) v[3] = 1 v[2] = 1 v[5] = 1 assert v.size == 6 v[7] = 1 assert v[7] == 0 assert v.size == 6 u = flex.double((1, -1, 2, 0, -2)) v = sparse.vector(5) v[0] = 10 v[3] = 4 v[4] = 5 assert u * v == 0 u = sparse.vector(10, {1: 1, 3: 3, 7: 7}) v = sparse.vector(10, {0: -1, 1: 2, 7: -1, 8: 2}) assert u * v == -5 assert sparse.weighted_dot(u, flex.double_range(10), v) == -47 a = flex.double() for i in range(10): for j in range(i, 10): a.append(1 / (i + j + 1)) assert approx_equal(sparse.quadratic_form(u, a, v), 4003 / 1980, eps=1e-15) assert approx_equal(sparse.quadratic_form(a, u), sparse.quadratic_form(u, a, u), eps=1e-15) sparse_approx_equal = sparse.approx_equal(tolerance=0.1) u = sparse.vector(4) u[0] = 1.01 v = sparse.vector(4) v[0] = 1.02 v[3] = 0.001 assert sparse_approx_equal(u, v) u = sparse.vector(4) v = sparse.vector(4) v[3] = 0.001 assert sparse_approx_equal(u, v) u = sparse.vector(5, {3: 0.3, 1: 0.1}) assert list(u.as_dense_vector()) == [0, 0.1, 0, 0.3, 0] try: sparse.vector(4, [1, 2, 3, 4]) raise Exception_expected except Exception as e: assert e.__class__.__module__ == 'Boost.Python' assert e.__class__.__name__ == 'ArgumentError' u = sparse.vector(4, {1: 1, 3: 3}) v = flex.double([1, 2, 3, 4]) assert u * v == 14 u = sparse.vector(5) s = flex.bool((True, False, False, True, True)) v = flex.double((1, 2, 3, 4, 5)) u.set_selected(s, v) assert u == sparse.vector(5, {0: 1, 3: 4, 4: 5}) u = sparse.vector(7) i = flex.size_t((2, 4, 5)) v = flex.double((-2.0, -4.0, -5.0)) u.set_selected(i, v) assert u == sparse.vector(7, {2: -2.0, 4: -4.0, 5: -5.0}) sparse_approx_equal = sparse.approx_equal(tolerance=1e-15) def linear_combination_trial_vectors(): u = sparse.vector(8, {1: 1.1, 3: 1.3}) v = sparse.vector(8, {0: 2.0, 2: 2.2, 3: 2.3, 4: 2.4}) w = list(-2 * u.as_dense_vector() + 3 * v.as_dense_vector()) yield u, v, w random_vectors = scitbx.random.variate( sparse.vector_distribution( 8, density=0.4, elements=scitbx.random.uniform_distribution(min=-2, max=2))) u = next(random_vectors) v = next(random_vectors) w = list(-2 * u.as_dense_vector() + 3 * v.as_dense_vector()) yield u, v, w for u, v, w in itertools.islice(linear_combination_trial_vectors(), 50): w1 = -2 * u + 3 * v w2 = 3 * v - 2 * u assert sparse_approx_equal(w1, w2) assert approx_equal(list(w1.as_dense_vector()), w, eps=1e-15) w1 += 2 * u w1 /= 3 assert sparse_approx_equal(w1, v) w2 -= 3 * v w2 /= -2 assert sparse_approx_equal(w2, u) u = sparse.vector(3, {1: 2}) v = u / 2 assert v == sparse.vector(3, {1: 1})
def exercise_vector(): v = sparse.vector(5) assert v.size == 5 assert v.is_structurally_zero() v[1] = 2 v[2] = 0 v[3] = 6 assert list(v) == [(1,2.), (2,0.), (3,6.)] assert list(v.compact()) == [(1,2.), (2,0.), (3,6.)] assert [ v[i] for i in xrange(5) ] == [0, 2, 0, 6, 0] p = flex.size_t([1,2,3,4,0]) assert list(v.permute(p)) == [(2,2.), (3,0.), (4,6.)] assert v.non_zeroes == 3 v = sparse.vector(10) v[7] = -5 v[1] = -1 v[4] = 0 v[1] = 2 v[9] = 9. v[7] = 6 v[4] = 1 v[1] = 3 v[4] = 0 assert list(v.compact()) == [(1,3.), (4,0.), (7,6.), (9,9.)] assert ([ v.is_structural_zero(i) for i in xrange(10) ] == [ True, False, True, True, False, True, True, False, True, False ]) v = sparse.vector(10) v[4] += 1 v[5] += 2 v[4] += 2 v[5] = 1 v[3] = 2 v[5] += 3 assert list(v.compact()) == [ (3,2.), (4,3.), (5,4.) ] assert v.non_zeroes == 3 v = sparse.vector(6) v[3] = 1 v[2] = 1 v[5] = 1 assert v.size == 6 v[7] = 1 assert v[7] == 0 assert v.size == 6 u = flex.double((1, -1, 2, 0, -2)) v = sparse.vector(5) v[0] = 10 v[3] = 4 v[4] = 5 assert u*v == 0 u = sparse.vector(10, {1:1, 3:3, 7:7}) v = sparse.vector(10, {0:-1, 1:2, 7:-1, 8:2}) assert u*v == -5 assert sparse.weighted_dot(u, flex.double_range(10), v) == -47 a = flex.double() for i in xrange(10): for j in xrange(i,10): a.append(1/(i+j+1)) assert approx_equal(sparse.quadratic_form(u, a, v), 4003/1980, eps=1e-15) assert approx_equal(sparse.quadratic_form(a, u), sparse.quadratic_form(u, a, u), eps=1e-15) sparse_approx_equal = sparse.approx_equal(tolerance=0.1) u = sparse.vector(4) u[0] = 1.01 v = sparse.vector(4) v[0] = 1.02 v[3] = 0.001 assert sparse_approx_equal(u,v) u = sparse.vector(4) v = sparse.vector(4) v[3] = 0.001 assert sparse_approx_equal(u,v) u = sparse.vector(5, {3: 0.3, 1: 0.1}) assert list(u.as_dense_vector()) == [ 0, 0.1, 0, 0.3, 0 ] try: sparse.vector(4, [1, 2, 3, 4]) raise Exception_expected except Exception, e: assert e.__class__.__module__ == 'Boost.Python' assert e.__class__.__name__ == 'ArgumentError'