def test_axis(self): axis = (0.37394394059075464, 0.49642290523592875, 0.7834093619893614) angle = flex.random_double() * math.pi rotmat = scitbx.matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis, angle ) ) missets = [ scitbx.matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( flex.random_double_point_on_sphere(), ( flex.random_double() - 0.5 )* self.MAX_ERROR, ) ) for i in range( self.SAMPLE_SIZE ) ] matrices = [ misset * rotmat for misset in missets ] aver_lie = scitbx.math.r3_rotation_average_rotation_via_lie_algebra( matrices = matrices, ) aver_quat = scitbx.matrix.sqr( scitbx.math.r3_rotation_average_rotation_matrix_from_matrices( *matrices ) ) diff = aver_quat.transpose() * aver_lie self.assertAlmostEqual( ( diff - TestElement.IDENTITY ).norm_sq(), 0, 7 )
def random_gonio(): # make a random rotation axis with a random setting matrix axis = matrix.col(flex.random_double_point_on_sphere()) fixed_rotation = matrix.sqr((1, 0, 0, 0, 1, 0, 0, 0, 1)) setting_rotation = matrix.sqr(flex.random_double_r3_rotation_matrix()) goniometer = Goniometer(axis, fixed_rotation, setting_rotation) return goniometer
def test_project_2d(): # The function project_2d should give the same results even if the # detector is rotated in the laboratory frame # Use a multipanel detector detector = create_multipanel_detector(offset=0) # Get 2D origin, fast and slow vectors for the detector o, f, s = project_2d(detector) # Now rotate the detector by 30 degrees around an arbitrary axis h = detector.hierarchy() fast = matrix.col(h.get_fast_axis()) slow = matrix.col(h.get_slow_axis()) origin = matrix.col(h.get_origin()) axis = matrix.col(flex.random_double_point_on_sphere()) rot = axis.axis_and_angle_as_r3_rotation_matrix(30, deg=True) for panel in detector: fast = matrix.col(panel.get_fast_axis()) slow = matrix.col(panel.get_slow_axis()) origin = matrix.col(panel.get_origin()) panel.set_frame(rot * fast, rot * slow, rot * origin) # Get 2D origin, fast and slow vectors for the rotated detector new_o, new_f, new_s = project_2d(detector) for o1, o2 in zip(o, new_o): assert o1 == pytest.approx(o2) for f1, f2 in zip(f, new_f): assert f1 == pytest.approx(f2) for s1, s2 in zip(s, new_s): assert s1 == pytest.approx(s2)
def test_random_matrices(self): for i in range( self.SAMPLE_SIZE ): axis = flex.random_double_point_on_sphere() angle = flex.random_double() * ( math.pi - self.SINGULARITY_GUARD ) rotmat = scitbx.matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis, angle ) ) self.run_tests_with( matrix = rotmat )
def test_angle(self): axis = scitbx.matrix.col( flex.random_double_point_on_sphere() ) centre = flex.random_double() * math.pi errors = [ ( flex.random_double() - 0.5 )* self.MAX_ERROR for i in range( self.SAMPLE_SIZE ) ] average = scitbx.math.r3_rotation_average_rotation_via_lie_algebra( matrices = [ scitbx.matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis, centre + e ) ) for e in errors ] ) angle = centre + sum( errors ) / len( errors ) aaf = scitbx.math.r3_rotation_axis_and_angle_from_matrix( average ) alignment = axis.dot( scitbx.matrix.col( aaf.axis ) ) self.assertAlmostEqual( abs( alignment ), 1.0, 7 ) self.assertAlmostEqual( aaf.angle() / alignment, angle, 7 )
def exercise1(n_trials): v = 1000 for i in range(n_trials): for scale in [0.0000001, 0.0001, 1.]: ri1 = random.randint(-v, v) * scale ri2 = random.randint(-v, v) * scale ri3 = random.randint(-v, v) * scale ri = flex.double([ri1, ri2, ri3]) x = flex.double(flex.random_double_point_on_sphere()) * ri a, b, c, d = abcd(x=x) # sol(x=x, a=a, b=b, c=c, d=d, check_roots=True) # sol(x=(x[0], x[0], x[0]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[1], x[1]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[2], x[2], x[2]), a=a, b=b, c=c, d=d, check_roots=False) # sol(x=(x[0], x[0], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[0], x[2], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[0], x[1]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[1], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[2], x[0], x[2]), a=a, b=b, c=c, d=d, check_roots=False)
def exercise1(n_trials): v = 1000 for i in xrange(n_trials): for scale in [0.0000001, 0.0001, 1.0]: ri1 = random.randint(-v, v) * scale ri2 = random.randint(-v, v) * scale ri3 = random.randint(-v, v) * scale ri = flex.double([ri1, ri2, ri3]) x = flex.double(flex.random_double_point_on_sphere()) * ri a, b, c, d = abcd(x=x) # sol(x=x, a=a, b=b, c=c, d=d, check_roots=True) # sol(x=(x[0], x[0], x[0]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[1], x[1]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[2], x[2], x[2]), a=a, b=b, c=c, d=d, check_roots=False) # sol(x=(x[0], x[0], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[0], x[2], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[0], x[1]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[1], x[1], x[2]), a=a, b=b, c=c, d=d, check_roots=False) sol(x=(x[2], x[0], x[2]), a=a, b=b, c=c, d=d, check_roots=False)
def exercise_vector_to_vector(angle_exponent_step=10, n_trials=10): principal_vectors = [ matrix.col(v) for v in ((1, 0, 0), (0, 1, 0), (0, 0, 1)) ] for g0 in principal_vectors: for t0 in principal_vectors: for g in [g0, -g0]: for t in [t0, -t0]: check_vector_to_vector(g=g, t=t) if (sys.platform.startswith("osf")): max_exp = 300 else: max_exp = 340 for ig, g0 in enumerate(principal_vectors): for it, t0 in enumerate(principal_vectors): if (ig == it): continue axis = g0.cross(t0) for e in range(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: r = matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=axis, angle=angle)) for g in [g0, -g0]: for t in [t0, -t0]: check_vector_to_vector(g, r * t) check_vector_to_vector(r * g, t) check_vector_to_vector(r * g, r * t) for i_trial in range(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_vector(g, g) check_vector_to_vector(g, -g) t = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_vector(g, t) for e in range(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rt = matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=g, angle=angle)) * t check_vector_to_vector(rt, t) check_vector_to_vector(rt, -t) # check_vector_to_001((0, 0, 1)) check_vector_to_001((0, 0, -1)) for e in range(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(1, 1, 0), angle=angle)) * matrix.col((0, 0, 1)) check_vector_to_001(rg) check_vector_to_001(-rg) for i_trial in range(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_001(g) # check_vector_to_010((0, 1, 0)) check_vector_to_010((0, -1, 0)) for e in range(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(1, 0, 1), angle=angle)) * matrix.col((0, 1, 0)) check_vector_to_010(rg) check_vector_to_010(-rg) for i_trial in range(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_010(g) # check_vector_to_100((1, 0, 0)) check_vector_to_100((-1, 0, 0)) for e in range(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr( scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(0, 1, 1), angle=angle)) * matrix.col((1, 0, 0)) check_vector_to_100(rg) check_vector_to_100(-rg) for i_trial in range(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_100(g) # rvv = vector_to_vector((0, 0, -1), (0, 0, 1)) rv1 = vector_to_001((0, 0, -1)) assert approx_equal(rv1, rvv) rvv = vector_to_vector((0, -1, 0), (0, 1, 0)) rv1 = vector_to_010((0, -1, 0)) assert approx_equal(rv1, rvv) rvv = vector_to_vector((-1, 0, 0), (1, 0, 0)) rv1 = vector_to_100((-1, 0, 0)) assert approx_equal(rv1, rvv)
def exercise_vector_to_vector(angle_exponent_step=10, n_trials=10): principal_vectors = [matrix.col(v) for v in (1,0,0), (0,1,0), (0,0,1)] for g0 in principal_vectors: for t0 in principal_vectors: for g in [g0, -g0]: for t in [t0, -t0]: check_vector_to_vector(g=g, t=t) if (sys.platform.startswith("osf")): max_exp = 300 else: max_exp = 340 for ig,g0 in enumerate(principal_vectors): for it,t0 in enumerate(principal_vectors): if (ig == it): continue axis = g0.cross(t0) for e in xrange(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: r = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=axis, angle=angle)) for g in [g0, -g0]: for t in [t0, -t0]: check_vector_to_vector(g, r*t) check_vector_to_vector(r*g, t) check_vector_to_vector(r*g, r*t) for i_trial in xrange(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_vector(g, g) check_vector_to_vector(g, -g) t = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_vector(g, t) for e in xrange(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rt = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=g, angle=angle)) * t check_vector_to_vector(rt, t) check_vector_to_vector(rt, -t) # check_vector_to_001((0,0,1)) check_vector_to_001((0,0,-1)) for e in xrange(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(1,1,0), angle=angle)) * matrix.col((0,0,1)) check_vector_to_001(rg) check_vector_to_001(-rg) for i_trial in xrange(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_001(g) # check_vector_to_010((0,1,0)) check_vector_to_010((0,-1,0)) for e in xrange(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(1,0,1), angle=angle)) * matrix.col((0,1,0)) check_vector_to_010(rg) check_vector_to_010(-rg) for i_trial in xrange(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_010(g) # check_vector_to_100((1,0,0)) check_vector_to_100((-1,0,0)) for e in xrange(0, max_exp, angle_exponent_step): angle = 10**(-e) for angle in [angle, -angle]: rg = matrix.sqr(scitbx.math.r3_rotation_axis_and_angle_as_matrix( axis=(0,1,1), angle=angle)) * matrix.col((1,0,0)) check_vector_to_100(rg) check_vector_to_100(-rg) for i_trial in xrange(n_trials): g = matrix.col(flex.random_double_point_on_sphere()) check_vector_to_100(g) # rvv = vector_to_vector((0,0,-1), (0,0,1)) rv1 = vector_to_001((0,0,-1)) assert approx_equal(rv1, rvv) rvv = vector_to_vector((0,-1,0), (0,1,0)) rv1 = vector_to_010((0,-1,0)) assert approx_equal(rv1, rvv) rvv = vector_to_vector((-1,0,0), (1,0,0)) rv1 = vector_to_100((-1,0,0)) assert approx_equal(rv1, rvv)