def cb_op_as_rational(cb_op): num = cb_op.c().r().num() den = cb_op.c().r().den() rational_list = [] for ii in num: rational_list.append(rational.int(ii) / rational.int(den)) return matrix.sqr(rational_list).inverse()
def cb_op_as_rational(cb_op): num = cb_op.c().r().num() den = cb_op.c().r().den() rational_list = [] for ii in num: rational_list.append( rational.int(ii)/rational.int(den) ) return matrix.sqr( rational_list ).inverse()
def as_4x4_rational(self): r = self.r().as_rational().elems t = self.t().as_rational().elems zero = rational.int(0) one = rational.int(1) return matrix.rec((r[0], r[1], r[2], t[0], r[3], r[4], r[5], t[1], r[6], r[7], r[8], t[2], zero, zero, zero, one), (4, 4))
def as_4x4_rational(self): r = self.r().as_rational().elems t = self.t().as_rational().elems zero = rational.int(0) one = rational.int(1) return matrix.rec(( r[0], r[1], r[2], t[0], r[3], r[4], r[5], t[1], r[6], r[7], r[8], t[2], zero, zero, zero, one), (4, 4))
def generate_matrix(order): """ make all sub lattices generators of order order """ # first get all triples triples = find_triples(order) matrices = [] for triple in triples: # make a list of d and e values a = triple[0] b = triple[1] c = triple[2] d_and_e = [] f_list = [] if a % 2 == 0: # a is even tmp = -a // 2 + 1 while tmp <= a // 2: d_and_e.append(tmp) tmp += 1 if a % 2 != 0: # a is odd tmp = -(a - 1) // 2 while tmp <= (a - 1) // 2: d_and_e.append(tmp) tmp += 1 if b % 2 == 0: # b is even tmp = -b // 2 + 1 while tmp <= b // 2: f_list.append(tmp) tmp += 1 if b % 2 != 0: # b is odd tmp = -(b - 1) // 2 while tmp <= (b - 1) // 2: f_list.append(tmp) tmp += 1 for d in d_and_e: for e in d_and_e: for f in f_list: mat = [ rational.int(a), rational.int(d), rational.int(e), rational.int(0), rational.int(b), rational.int(f), rational.int(0), rational.int(0), rational.int(c), ] matrices.append(matrix.sqr(mat)) return matrices
def generate_matrix(order): """ make all sub lattices generators of order order """ # first get all triples triples = find_triples(order) matrices = [] for triple in triples: # make a list of d and e values a = triple[0] b = triple[1] c = triple[2] d_and_e = [] f_list = [] if a % 2 == 0: # a is even tmp = -a // 2 + 1 while tmp <= a // 2: d_and_e.append(tmp) tmp += 1 if a % 2 != 0: # a is odd tmp = -(a - 1) // 2 while tmp <= (a - 1) // 2: d_and_e.append(tmp) tmp += 1 if b % 2 == 0: # b is even tmp = -b // 2 + 1 while tmp <= b // 2: f_list.append(tmp) tmp += 1 if b % 2 != 0: # b is odd tmp = -(b - 1) // 2 while tmp <= (b - 1) // 2: f_list.append(tmp) tmp += 1 for d in d_and_e: for e in d_and_e: for f in f_list: mat = [ rational.int(a), rational.int(d), rational.int(e), rational.int(0), rational.int(b), rational.int(f), rational.int(0), rational.int(0), rational.int(c) ] matrices.append(matrix.sqr(mat)) return matrices
def get_point_in_plane(self): result = [0, 0, 0] for i in range(3): if (self.n[i] != 0): result[i] = -rational.int(self.c) / self.n[i] return result raise RuntimeError("cut_plane normal vector is the null vector.")
def rt_mx_as_rational(rot_mat): # make sure one provides an integer matrix! tmp_mat = rot_mat.r().as_double() rational_list = [] for ij in tmp_mat: rational_list.append(rational.int(ifloor(ij))) return matrix.sqr(rational_list)
def rt_mx_as_rational(rot_mat): # make sure one provides an integer matrix! tmp_mat = rot_mat.num() rational_list = [] for ij in tmp_mat: rational_list.append(rational.int(ifloor(ij))) return matrix.sqr(rational_list)
def get_point_in_plane(self): result = [0,0,0] for i in xrange(3): if (self.n[i] != 0): result[i] = -rational.int(self.c) / self.n[i] return result raise RuntimeError("cut_plane normal vector is the null vector.")
def __truediv__(self, other): assert isinstance(other, int) assert other != 0 assert self.c != 0 return cut(n=self.n, c=rational.int(1) * self.c / other, inclusive=self.inclusive, cut_expr=self.cut_expr)
def row_as_hkl( row, txt=['h','k','l']): result = "" part = 0 for n,j in zip(row,txt): nn="" if n != rational.int(0): part += 1 if n >rational.int(0): if part==1: if n==rational.int(1): nn = j else: nn = str(n)+j if part > 1: if n==rational.int(1): nn = "+"+j else: nn = "+"+str(n)+j if n < rational.int(0) : if part==1: if n==rational.int(-1): nn = "-"+j else: nn = str(n)+j else: if n == rational.int(-1): nn+="-"+j else: nn = str(n)+j result += nn return result
def __init__(self): self.unit = matrix.sqr( [rational.int(1), rational.int(0), rational.int(0), rational.int(0), rational.int(1), rational.int(0), rational.int(0), rational.int(0), rational.int(1) ] ) self.ops = [ self.unit ] self.max_ops = 100
def row_as_hkl(row, txt=['h', 'k', 'l']): result = "" part = 0 for n, j in zip(row, txt): nn = "" if n != rational.int(0): part += 1 if n > rational.int(0): if part == 1: if n == rational.int(1): nn = j else: nn = str(n) + j if part > 1: if n == rational.int(1): nn = "+" + j else: nn = "+" + str(n) + j if n < rational.int(0): if part == 1: if n == rational.int(-1): nn = "-" + j else: nn = str(n) + j else: if n == rational.int(-1): nn += "-" + j else: nn = str(n) + j result += nn return result
def special_op_simplifier(special_op): rt = special_op.as_rational() r = rt.r t = rt.t rows = [r[:3], r[3:6], r[6:]] terms = [None, None, None] r0 = rational.int(0) r1 = rational.int(1) n_done = 0 for i_row, row in enumerate(rows): if (row == (0, 0, 0)): terms[i_row] = special_op_simplified_term([], [], t[i_row]) n_done += 1 if (n_done == 3): return special_op_simplified(terms=terms) if (n_done == 0): m, v = [], [] for i in range(3): m.append([r[i + 0], r[i + 3]]) v.append(r[i + 6]) from scitbx.matrix import row_echelon free_vars = row_echelon.form_rational(m, v) if (len(free_vars) == 0): sol = row_echelon.back_substitution_rational( m, v, free_vars, [None] * 2) if (sol is not None and sol.count(0) == 0): for i_row in [0, 1]: terms[i_row] = special_op_simplified_term([i_row], [r1], r0) terms[2] = special_op_simplified_term( [0, 1], sol, t[2] - sol[0] * t[0] - sol[1] * t[1]) return special_op_simplified(terms=terms) for i_row in range(3): if (terms[i_row] is not None): continue terms[i_row] = special_op_simplified_term([i_row], [r1], r0) for j_row in range(i_row + 1, 3): if (terms[j_row] is not None): continue m = matrix.linearly_dependent_pair_scaling_factor( vector_1=rows[i_row], vector_2=rows[j_row]) if (m is None): continue assert m != 0 terms[j_row] = special_op_simplified_term([i_row], [m], t[j_row] - m * t[i_row]) return special_op_simplified(terms=terms)
def __init__(self): self.unit = matrix.sqr([ rational.int(1), rational.int(0), rational.int(0), rational.int(0), rational.int(1), rational.int(0), rational.int(0), rational.int(0), rational.int(1) ]) self.ops = [self.unit] self.max_ops = 100
def special_op_simplifier(special_op): rt = special_op.as_rational() r = rt.r t = rt.t rows = [r[:3], r[3:6], r[6:]] terms = [None, None, None] r0 = rational.int(0) r1 = rational.int(1) n_done = 0 for i_row,row in enumerate(rows): if (row == (0,0,0)): terms[i_row] = special_op_simplified_term([], [], t[i_row]) n_done += 1 if (n_done == 3): return special_op_simplified(terms=terms) if (n_done == 0): m, v = [], [] for i in xrange(3): m.append([r[i+0], r[i+3]]) v.append(r[i+6]) from scitbx.matrix import row_echelon free_vars = row_echelon.form_rational(m, v) if (len(free_vars) == 0): sol = row_echelon.back_substitution_rational(m, v, free_vars, [None]*2) if (sol is not None and sol.count(0) == 0): for i_row in [0,1]: terms[i_row] = special_op_simplified_term([i_row], [r1], r0) terms[2] = special_op_simplified_term( [0,1], sol, t[2] - sol[0]*t[0] - sol[1]*t[1]) return special_op_simplified(terms=terms) for i_row in xrange(3): if (terms[i_row] is not None): continue terms[i_row] = special_op_simplified_term([i_row], [r1], r0) for j_row in xrange(i_row+1,3): if (terms[j_row] is not None): continue m = matrix.linearly_dependent_pair_scaling_factor( vector_1=rows[i_row], vector_2=rows[j_row]) if (m is None): continue assert m != 0 terms[j_row] = special_op_simplified_term( [i_row], [m], t[j_row] - m*t[i_row]) return special_op_simplified(terms=terms)
def check_compatibility_with_sampling_grid(asu): print "Shape vertices:" n_outside_sampling_grid = 0 for vertex in asu.shape_vertices(): s = "" for v in vertex: if (v < -rational.int(1, 2) or v > 1): s = " outside sampling grid" n_outside_sampling_grid += 1 break print " %s%s" % (str(vertex), s) assert n_outside_sampling_grid == 0
def exercise_float_asu(space_group_info, n_grid=6): unit_cell = space_group_info.any_compatible_unit_cell(volume=1000) ref_asu = reference_table.get_asu(space_group_info.type().number()) exercise_cut_planes(ref_asu.cuts) inp_asu = space_group_info.direct_space_asu() assert sgtbx.space_group(inp_asu.hall_symbol) == space_group_info.group() exercise_cut_planes(inp_asu.cuts) exercise_shape_vertices(inp_asu, unit_cell) float_asu = inp_asu.add_buffer(unit_cell=unit_cell, thickness=0.001) cb_mx_ref_inp = space_group_info.type().cb_op().c_inv().as_rational() n = n_grid for ref_n in flex.nested_loop((-n // 2, -n // 2, -n // 2), (n, n, n), False): # check correctness of space_group_info.direct_space_asu() ref_r = matrix.col([rational.int(g, n) for g in ref_n]) inp_r = cb_mx_ref_inp * ref_r assert ref_asu.is_inside(ref_r.elems) == inp_asu.is_inside(inp_r.elems) # check correctness of cut_plane.add_buffer() inp_r = inp_r.elems inp_f = [float(r) for r in inp_r] for cut in inp_asu.cuts: r_cut = cut.strip() r_inside = r_cut.is_inside(inp_r) for buffer_thickness in [0.001, 1, -1][:1]: f_cut = cut.as_float_cut_plane().add_buffer( unit_cell=unit_cell, thickness=buffer_thickness) f_inside = f_cut.is_inside(inp_f) if (buffer_thickness < 0): if (r_inside != f_inside): assert r_inside elif (buffer_thickness < 0.01): assert r_inside == f_inside elif (r_inside != f_inside): assert f_inside # check correctness of float_asu.add_buffer() assert float_asu.is_inside(inp_f) == inp_asu.shape_only().is_inside( inp_r) asu_with_metric = inp_asu.define_metric(unit_cell) assert asu_with_metric.hall_symbol is inp_asu.hall_symbol assert len(asu_with_metric.cuts) == len(inp_asu.cuts) assert asu_with_metric.unit_cell is unit_cell asu_tight = asu_with_metric.as_float_asu() asu_buffer = asu_with_metric.add_buffer(thickness=2) asu_shrunk = asu_with_metric.add_buffer(relative_thickness=-1.e-5) vertices = facet_analysis.shape_vertices(inp_asu) for vertex in vertices: assert inp_asu.shape_only().is_inside(vertex) for vertex in vertices: assert asu_tight.is_inside(matrix.col(vertex).as_float().elems) for vertex in vertices: assert asu_buffer.is_inside(matrix.col(vertex).as_float().elems) for vertex in vertices: assert not asu_shrunk.is_inside(matrix.col(vertex).as_float().elems)
def exercise_float_asu(space_group_info, n_grid=6): unit_cell = space_group_info.any_compatible_unit_cell(volume=1000) ref_asu = reference_table.get_asu(space_group_info.type().number()) exercise_cut_planes(ref_asu.cuts) inp_asu = space_group_info.direct_space_asu() assert sgtbx.space_group(inp_asu.hall_symbol) == space_group_info.group() exercise_cut_planes(inp_asu.cuts) exercise_shape_vertices(inp_asu, unit_cell) float_asu = inp_asu.add_buffer(unit_cell=unit_cell, thickness=0.001) cb_mx_ref_inp = space_group_info.type().cb_op().c_inv().as_rational() n = n_grid for ref_n in flex.nested_loop((-n//2,-n//2,-n//2),(n,n,n),False): # check correctness of space_group_info.direct_space_asu() ref_r = matrix.col([rational.int(g,n) for g in ref_n]) inp_r = cb_mx_ref_inp * ref_r assert ref_asu.is_inside(ref_r.elems) == inp_asu.is_inside(inp_r.elems) # check correctness of cut_plane.add_buffer() inp_r = inp_r.elems inp_f = [float(r) for r in inp_r] for cut in inp_asu.cuts: r_cut = cut.strip() r_inside = r_cut.is_inside(inp_r) for buffer_thickness in [0.001, 1, -1][:1]: f_cut = cut.as_float_cut_plane().add_buffer( unit_cell=unit_cell, thickness=buffer_thickness) f_inside = f_cut.is_inside(inp_f) if (buffer_thickness < 0): if (r_inside != f_inside): assert r_inside elif (buffer_thickness < 0.01): assert r_inside == f_inside elif (r_inside != f_inside): assert f_inside # check correctness of float_asu.add_buffer() assert float_asu.is_inside(inp_f) == inp_asu.shape_only().is_inside(inp_r) asu_with_metric = inp_asu.define_metric(unit_cell) assert asu_with_metric.hall_symbol is inp_asu.hall_symbol assert len(asu_with_metric.cuts) == len(inp_asu.cuts) assert asu_with_metric.unit_cell is unit_cell asu_tight = asu_with_metric.as_float_asu() asu_buffer = asu_with_metric.add_buffer(thickness=2) asu_shrunk = asu_with_metric.add_buffer(relative_thickness=-1.e-5) vertices = facet_analysis.shape_vertices(inp_asu) for vertex in vertices: assert inp_asu.shape_only().is_inside(vertex) for vertex in vertices: assert asu_tight.is_inside(matrix.col(vertex).as_float().elems) for vertex in vertices: assert asu_buffer.is_inside(matrix.col(vertex).as_float().elems) for vertex in vertices: assert not asu_shrunk.is_inside(matrix.col(vertex).as_float().elems)
def vec3_rat_from_str(s): flds = s.split(",") assert len(flds) == 3 result = [] for fld in flds: slash_count = fld.count("/") assert slash_count < 2 if (slash_count == 0): n, d = int(fld), 1 else: n, d = [int(t) for t in fld.split("/")] result.append(rational.int(n, d)) return result
def __init__(self, space_group): self.space_group = space_group nc_dict = {} self.list = [] for i_smx in xrange(space_group.order_p()): s = space_group(i_smx) r_info = s.r().info() if (r_info.type() < 2): continue if (r_info.sense() < 0): continue n = r_info.ev() p = s.t().mod_positive() assert p.den() == space_group.t_den() c = -dot3(n, p.num()) if (not nc_dict.has_key((n,c))): nc_dict[(n,c)] = 0 self.list.append( plane_fractional(s=s, n=n, p=p, c=rational.int(c,p.den())))
def __init__(self, space_group): self.space_group = space_group nc_dict = {} self.list = [] for i_smx in xrange(space_group.order_p()): s = space_group(i_smx) r_info = s.r().info() if (r_info.type() < 2): continue if (r_info.sense() < 0): continue n = r_info.ev() p = s.t().mod_positive() assert p.den() == space_group.t_den() c = -dot3(n, p.num()) if (not nc_dict.has_key((n, c))): nc_dict[(n, c)] = 0 self.list.append( plane_fractional(s=s, n=n, p=p, c=rational.int(c, p.den())))
def intersection(cuts): assert len(cuts) == 3 m = flex.int() t = flex.int() denominator = 1 for cut in cuts: denominator = cut.lcm_of_denominators(start_lcm=denominator) for cut in cuts: for e in cut.n: m.append(int(e * denominator)) t.append(-int(cut.c * denominator)) m.reshape(flex.grid(3,3)) t.reshape(flex.grid(3,1)) r = scitbx.math.row_echelon_form_t(m, t) assert r in (2,3) if (r != 3): return None t.reshape(flex.grid(3)) sol = flex.int(3) d = scitbx.math.row_echelon_back_substitution_int(m, t, sol) assert d > 0 return tuple([rational.int(s,d) for s in sol])
def intersection(cuts): assert len(cuts) == 3 m = flex.int() t = flex.int() denominator = 1 for cut in cuts: denominator = cut.lcm_of_denominators(start_lcm=denominator) for cut in cuts: for e in cut.n: m.append(int(e * denominator)) t.append(-int(cut.c * denominator)) m.reshape(flex.grid(3, 3)) t.reshape(flex.grid(3, 1)) r = scitbx.math.row_echelon_form_t(m, t) assert r in (2, 3) if (r != 3): return None t.reshape(flex.grid(3)) sol = flex.int(3) d = scitbx.math.row_echelon_back_substitution_int(m, t, sol) assert d > 0 return tuple([rational.int(s, d) for s in sol])
def line_sample_point(a, b, f, gridding): return [a[i]+rational.int(f,gridding)*(b[i]-a[i]) for i in xrange(3)]
def __truediv__(self, other): assert isinstance(other, int) assert other != 0 assert self.c != 0 return cut(n=self.n, c=rational.int(1)*self.c/other, inclusive=self.inclusive, cut_expr=self.cut_expr)
def rr(): return rational.int(rng.randrange(-5,6), rng.randrange(1,10))
def rr(): return rational.int(rng.randrange(-5, 6), rng.randrange(1, 10))
def lcm_of_denominators(self, start_lcm=1): result = start_lcm for e in self.n: result = rational.lcm(result, rational.int(e).denominator()) result = rational.lcm(result, rational.int(self.c).denominator()) return result
def line_sample_point(a, b, f, gridding): return [ a[i] + rational.int(f, gridding) * (b[i] - a[i]) for i in xrange(3) ]
def exercise_int(): ri = rational.int r = ri() assert r.numerator() == 0 assert r.denominator() == 1 assert r.as_tuple() == (0, 1) assert int(r) == 0 assert float(r) == 0 assert rational.int(rational.int(3)).as_tuple() == (3, 1) assert rational.int(2).as_tuple() == (2, 1) assert rational.int(2, 3).as_tuple() == (2, 3) assert str(rational.int()) == "0" assert str(rational.int(2)) == "2" assert str(rational.int(-2, 3)) == "-2/3" assert (-rational.int(2, 3)).as_tuple() == (-2, 3) assert (rational.int(2, 3) + rational.int(3, 4)).as_tuple() == (17, 12) assert (rational.int(2, 3) - rational.int(3, 4)).as_tuple() == (-1, 12) assert (rational.int(2, 3) * rational.int(3, 4)).as_tuple() == (1, 2) assert (rational.int(2, 3) / rational.int(3, 4)).as_tuple() == (8, 9) assert (rational.int(2, 3) // rational.int(3, 4)) == 0 assert (rational.int(2, 3) % rational.int(1, 2)).as_tuple() == (1, 6) assert (rational.int(2, 3) + 4).as_tuple() == (14, 3) assert (rational.int(2, 3) - 4).as_tuple() == (-10, 3) assert (rational.int(2, 3) * 4).as_tuple() == (8, 3) assert (rational.int(2, 3) / 4).as_tuple() == (1, 6) assert (rational.int(2, 3) // 4) == 0 assert (rational.int(7, 3) % 2).as_tuple() == (1, 3) assert (5 + rational.int(2, 3)).as_tuple() == (17, 3) assert (5 - rational.int(2, 3)).as_tuple() == (13, 3) assert (5 * rational.int(2, 3)).as_tuple() == (10, 3) assert (5 / rational.int(2, 3)).as_tuple() == (15, 2) assert (5 // rational.int(2, 3)) == 7 assert (5 % rational.int(2, 3)).as_tuple() == (1, 3) assert rational.int(2, 3) == rational.int(2, 3) assert not rational.int(2, 3) == rational.int(2, 5) assert rational.int(2, 3) != rational.int(2, 5) assert not rational.int(2, 3) != rational.int(2, 3) assert rational.int(2, 3) < rational.int(3, 4) assert not rational.int(2, 3) < rational.int(2, 3) assert rational.int(2, 3) > rational.int(1, 2) assert not rational.int(2, 3) > rational.int(2, 3) assert rational.int(2, 3) <= rational.int(3, 4) assert not rational.int(2, 3) <= rational.int(1, 2) assert rational.int(2, 3) >= rational.int(1, 2) assert not rational.int(2, 3) >= rational.int(3, 4) assert rational.int(4, 2) == 2 assert not rational.int(4, 2) == 3 assert rational.int(4, 2) != 3 assert not rational.int(4, 2) != 2 assert rational.int(4, 2) < 3 assert not rational.int(4, 2) < 2 assert rational.int(4, 2) > 1 assert not rational.int(4, 2) > 2 assert rational.int(4, 2) <= 3 assert not rational.int(4, 2) <= 1 assert rational.int(4, 2) >= 1 assert not rational.int(4, 2) >= 3 assert 2 == rational.int(4, 2) assert not 3 == rational.int(4, 2) assert 3 != rational.int(4, 2) assert not 2 != rational.int(4, 2) assert 3 > rational.int(4, 2) assert not 2 > rational.int(4, 2) assert 1 < rational.int(4, 2) assert not 2 < rational.int(4, 2) assert 3 >= rational.int(4, 2) assert not 1 >= rational.int(4, 2) assert 1 <= rational.int(4, 2) assert not 3 <= rational.int(4, 2) r = rational.int(4, 3) r += 1 assert r.as_tuple() == (7, 3) assert approx_equal(float(r), 7. / 3) s = rational.int(4, 3) assert hash(s) == hash(rational.int(4, 3)) assert hash(s) != hash(r) for n in xrange(-100, 100): assert hash(n) == hash(rational.int(n)) for d in xrange(1, 8): assert hash(rational.int(n, d)) == hash(rational.int(n, d)) assert hash(rational.int(n, d)) == hash(rational.int(3 * n, 3 * d)) assert hash(rational.int(n, d)) == hash(rational.int(-3 * n, -3 * d)) try: int(r) except RuntimeError, e: assert str(e) == "boost.rational: as_int() conversion error:" \ " denominator is different from one."
from __future__ import absolute_import, division, print_function from cctbx.sgtbx.direct_space_asu.cut_plane import cut from boost import rational r1 = rational.int(1) x1 = cut((-1,0,0), 1) x0 = -x1*0 x2 = x1/2 x3 = x1/3 x4 = x1/4 x8 = x1/8 x34 = x1*3/4 y1 = cut((0,-1,0), 1) y0 = -y1*0 y2 = y1/2 y3 = y1/3 y4 = y1/4 y8 = y1/8 z1 = cut((0,0,-1), 1) z0 = -z1*0 z2 = z1/2 z3 = z1/3 z4 = z1/4 z6 = z1/6 z8 = z1/8 z12 = z1/12 p1 = cut((-1,1,0), 1) p0 = -p1*0 p2 = p1/2 p3 = p1/3
assert hash(s) == hash(rational.int(4,3)) assert hash(s) != hash(r) for n in xrange(-100,100): assert hash(n) == hash(rational.int(n)) for d in xrange(1,8): assert hash(rational.int(n,d)) == hash(rational.int(n,d)) assert hash(rational.int(n,d)) == hash(rational.int(3*n,3*d)) assert hash(rational.int(n,d)) == hash(rational.int(-3*n,-3*d)) try: int(r) except RuntimeError, e: assert str(e) == "boost.rational: as_int() conversion error:" \ " denominator is different from one." else: raise Exception_expected for n in xrange(-5,6): for d in xrange(1,10): r = rational.int(n, d) p = pickle.dumps(r) l = pickle.loads(p) assert l == r assert str(l) == str(r) # ee = "bad rational: zero denominator" lhs = ri(1) for rhs in [ri(0), 0]: try: lhs / rhs except RuntimeError, e: assert not show_diff(str(e), ee) else: raise Exception_expected try: lhs % rhs except RuntimeError, e: assert not show_diff(str(e), ee) else: raise Exception_expected #
assert hash(n) == hash(rational.int(n)) for d in xrange(1, 8): assert hash(rational.int(n, d)) == hash(rational.int(n, d)) assert hash(rational.int(n, d)) == hash(rational.int(3 * n, 3 * d)) assert hash(rational.int(n, d)) == hash(rational.int(-3 * n, -3 * d)) try: int(r) except RuntimeError, e: assert str(e) == "boost.rational: as_int() conversion error:" \ " denominator is different from one." else: raise Exception_expected for n in xrange(-5, 6): for d in xrange(1, 10): r = rational.int(n, d) p = pickle.dumps(r) l = pickle.loads(p) assert l == r assert str(l) == str(r) # ee = "bad rational: zero denominator" lhs = ri(1) for rhs in [ri(0), 0]: try: lhs / rhs except RuntimeError, e: assert not show_diff(str(e), ee) else: raise Exception_expected try:
def exercise_int(): ri = rational.int r = ri() assert r.numerator() == 0 assert r.denominator() == 1 assert r.as_tuple() == (0, 1) assert int(r) == 0 assert float(r) == 0 assert rational.int(rational.int(3)).as_tuple() == (3, 1) assert rational.int(2).as_tuple() == (2, 1) assert rational.int(2, 3).as_tuple() == (2, 3) assert str(rational.int()) == "0" assert str(rational.int(2)) == "2" assert str(rational.int(-2, 3)) == "-2/3" assert (-rational.int(2, 3)).as_tuple() == (-2, 3) assert (rational.int(2, 3) + rational.int(3, 4)).as_tuple() == (17, 12) assert (rational.int(2, 3) - rational.int(3, 4)).as_tuple() == (-1, 12) assert (rational.int(2, 3) * rational.int(3, 4)).as_tuple() == (1, 2) assert (rational.int(2, 3) / rational.int(3, 4)).as_tuple() == (8, 9) assert (rational.int(2, 3) // rational.int(3, 4)) == 0 assert (rational.int(2, 3) % rational.int(1, 2)).as_tuple() == (1, 6) assert (rational.int(2, 3) + 4).as_tuple() == (14, 3) assert (rational.int(2, 3) - 4).as_tuple() == (-10, 3) assert (rational.int(2, 3) * 4).as_tuple() == (8, 3) assert (rational.int(2, 3) / 4).as_tuple() == (1, 6) assert (rational.int(2, 3) // 4) == 0 assert (rational.int(7, 3) % 2).as_tuple() == (1, 3) assert (5 + rational.int(2, 3)).as_tuple() == (17, 3) assert (5 - rational.int(2, 3)).as_tuple() == (13, 3) assert (5 * rational.int(2, 3)).as_tuple() == (10, 3) assert (5 / rational.int(2, 3)).as_tuple() == (15, 2) assert (5 // rational.int(2, 3)) == 7 assert (5 % rational.int(2, 3)).as_tuple() == (1, 3) assert rational.int(2, 3) == rational.int(2, 3) assert not rational.int(2, 3) == rational.int(2, 5) assert rational.int(2, 3) != rational.int(2, 5) assert not rational.int(2, 3) != rational.int(2, 3) assert rational.int(2, 3) < rational.int(3, 4) assert not rational.int(2, 3) < rational.int(2, 3) assert rational.int(2, 3) > rational.int(1, 2) assert not rational.int(2, 3) > rational.int(2, 3) assert rational.int(2, 3) <= rational.int(3, 4) assert not rational.int(2, 3) <= rational.int(1, 2) assert rational.int(2, 3) >= rational.int(1, 2) assert not rational.int(2, 3) >= rational.int(3, 4) assert rational.int(4, 2) == 2 assert not rational.int(4, 2) == 3 assert rational.int(4, 2) != 3 assert not rational.int(4, 2) != 2 assert rational.int(4, 2) < 3 assert not rational.int(4, 2) < 2 assert rational.int(4, 2) > 1 assert not rational.int(4, 2) > 2 assert rational.int(4, 2) <= 3 assert not rational.int(4, 2) <= 1 assert rational.int(4, 2) >= 1 assert not rational.int(4, 2) >= 3 assert 2 == rational.int(4, 2) assert not 3 == rational.int(4, 2) assert 3 != rational.int(4, 2) assert not 2 != rational.int(4, 2) assert 3 > rational.int(4, 2) assert not 2 > rational.int(4, 2) assert 1 < rational.int(4, 2) assert not 2 < rational.int(4, 2) assert 3 >= rational.int(4, 2) assert not 1 >= rational.int(4, 2) assert 1 <= rational.int(4, 2) assert not 3 <= rational.int(4, 2) r = rational.int(4, 3) r += 1 assert r.as_tuple() == (7, 3) assert approx_equal(float(r), 7. / 3) s = rational.int(4, 3) assert hash(s) == hash(rational.int(4, 3)) assert hash(s) != hash(r) for n in range(-100, 100): assert hash(n) == hash(rational.int(n)) for d in range(1, 8): assert hash(rational.int(n, d)) == hash(rational.int(n, d)) assert hash(rational.int(n, d)) == hash(rational.int(3 * n, 3 * d)) assert hash(rational.int(n, d)) == hash(rational.int(-3 * n, -3 * d)) try: int(r) except RuntimeError as e: assert str(e) == "boost.rational: as_int() conversion error:" \ " denominator is different from one." else: raise Exception_expected for n in range(-5, 6): for d in range(1, 10): r = rational.int(n, d) p = pickle.dumps(r) l = pickle.loads(p) assert l == r assert str(l) == str(r) # ee = "bad rational: zero denominator" lhs = ri(1) for rhs in [ri(0), 0]: try: lhs / rhs except RuntimeError as e: assert not show_diff(str(e), ee) else: raise Exception_expected try: lhs % rhs except RuntimeError as e: assert not show_diff(str(e), ee) else: raise Exception_expected # try: import fractions except ImportError: fractions = None def check(nd1, nd2, expected=None): r1, r2 = ri(*nd1), ri(*nd2) rm = r1 % r2 assert (r1 // r2) * r2 + rm == r1 if (fractions is not None): ff = fractions.Fraction f1, f2 = ff(*nd1), ff(*nd2) fm = f1 % f2 assert (fm.numerator, fm.denominator) == rm.as_tuple() if (expected is not None): assert rm.as_tuple() == expected check((2, 3), (1, 2), (1, 6)) check((2, 3), (-1, 2), (-1, 3)) check((-2, 3), (1, 2), (1, 3)) check((-2, 3), (-1, 2), (-1, 6)) for ln in range(-7, 7 + 1): for rn in range(-9, 9 + 1): if (rn == 0): continue check((ln, 3), (rn, 4)) # ri = rational.int def check(r, e): assert isinstance(r, ri) assert r == e check(ri(3, 2) + ri(4, 5), ri(23, 10)) check(ri(3, 2) + 4, ri(11, 2)) check(2 + ri(4, 5), ri(14, 5)) check(ri(3, 2) - ri(4, 5), ri(7, 10)) check(ri(3, 2) - 4, ri(-5, 2)) check(2 - ri(4, 5), ri(6, 5)) check(ri(3, 2) * ri(4, 5), ri(6, 5)) check(ri(3, 2) * 4, ri(6, 1)) check(2 * ri(4, 5), ri(8, 5)) check(ri(3, 2) / ri(4, 5), ri(15, 8)) check(ri(3, 2) / 4, ri(3, 8)) check(2 / ri(4, 5), ri(5, 2)) # def check(r, e): assert isinstance(r, int) assert r == e check(ri(3, 2) // ri(4, 5), 1) check(ri(3, 2) // 4, 0) check(2 // ri(4, 5), 2) # def check(r, e): assert isinstance(r, float) assert approx_equal(r, e) check(ri(3, 2) + 4., 5.5) check(2. + ri(4, 5), 2.8) check(ri(3, 2) - 4., -2.5) check(2. - ri(4, 5), 1.2) check(ri(3, 2) * 4., 6.0) check(2. * ri(4, 5), 1.6) check(ri(3, 2) / 4., 0.375) check(2. / ri(4, 5), 2.5) # try: ri(3, 2) // 4. except TypeError as e: assert str(e).startswith("unsupported operand type(s)") else: raise Exception_expected try: 2. // ri(4, 5) except TypeError as e: assert str(e).startswith("unsupported operand type(s)") else: raise Exception_expected # try: ri(3, 2) % 4. except TypeError as e: assert str(e).startswith("unsupported operand type(s)") else: raise Exception_expected try: 2. % ri(4, 5) except TypeError as e: assert str(e).startswith("unsupported operand type(s)") else: raise Exception_expected # try: ri(1) / 0. except ZeroDivisionError as e: assert not show_diff(str(e), "float division by zero") else: raise Exception_expected try: 1. / ri(0) except ZeroDivisionError as e: assert not show_diff(str(e), "float division by zero") else: raise Exception_expected
from __future__ import division from cctbx.sgtbx.direct_space_asu.cut_plane import cut from boost import rational r1 = rational.int(1) x1 = cut((-1,0,0), 1) x0 = -x1*0 x2 = x1/2 x3 = x1/3 x4 = x1/4 x8 = x1/8 x34 = x1*3/4 y1 = cut((0,-1,0), 1) y0 = -y1*0 y2 = y1/2 y3 = y1/3 y4 = y1/4 y8 = y1/8 z1 = cut((0,0,-1), 1) z0 = -z1*0 z2 = z1/2 z3 = z1/3 z4 = z1/4 z6 = z1/6 z8 = z1/8 z12 = z1/12 p1 = cut((-1,1,0), 1) p0 = -p1*0 p2 = p1/2 p3 = p1/3
def exercise_int(): ri = rational.int r = ri() assert r.numerator() == 0 assert r.denominator() == 1 assert r.as_tuple() == (0,1) assert int(r) == 0 assert float(r) == 0 assert rational.int(rational.int(3)).as_tuple() == (3,1) assert rational.int(2).as_tuple() == (2,1) assert rational.int(2,3).as_tuple() == (2,3) assert str(rational.int()) == "0" assert str(rational.int(2)) == "2" assert str(rational.int(-2,3)) == "-2/3" assert (-rational.int(2,3)).as_tuple() == (-2,3) assert (rational.int(2,3) + rational.int(3,4)).as_tuple() == (17,12) assert (rational.int(2,3) - rational.int(3,4)).as_tuple() == (-1,12) assert (rational.int(2,3) * rational.int(3,4)).as_tuple() == (1,2) assert (rational.int(2,3) / rational.int(3,4)).as_tuple() == (8,9) assert (rational.int(2,3) // rational.int(3,4)) == 0 assert (rational.int(2,3) % rational.int(1,2)).as_tuple() == (1,6) assert (rational.int(2,3) + 4).as_tuple() == (14,3) assert (rational.int(2,3) - 4).as_tuple() == (-10,3) assert (rational.int(2,3) * 4).as_tuple() == (8,3) assert (rational.int(2,3) / 4).as_tuple() == (1,6) assert (rational.int(2,3) // 4) == 0 assert (rational.int(7,3) % 2).as_tuple() == (1,3) assert (5 + rational.int(2,3)).as_tuple() == (17,3) assert (5 - rational.int(2,3)).as_tuple() == (13,3) assert (5 * rational.int(2,3)).as_tuple() == (10,3) assert (5 / rational.int(2,3)).as_tuple() == (15,2) assert (5 // rational.int(2,3)) == 7 assert (5 % rational.int(2,3)).as_tuple() == (1,3) assert rational.int(2,3) == rational.int(2,3) assert not rational.int(2,3) == rational.int(2,5) assert rational.int(2,3) != rational.int(2,5) assert not rational.int(2,3) != rational.int(2,3) assert rational.int(2,3) < rational.int(3,4) assert not rational.int(2,3) < rational.int(2,3) assert rational.int(2,3) > rational.int(1,2) assert not rational.int(2,3) > rational.int(2,3) assert rational.int(2,3) <= rational.int(3,4) assert not rational.int(2,3) <= rational.int(1,2) assert rational.int(2,3) >= rational.int(1,2) assert not rational.int(2,3) >= rational.int(3,4) assert rational.int(4,2) == 2 assert not rational.int(4,2) == 3 assert rational.int(4,2) != 3 assert not rational.int(4,2) != 2 assert rational.int(4,2) < 3 assert not rational.int(4,2) < 2 assert rational.int(4,2) > 1 assert not rational.int(4,2) > 2 assert rational.int(4,2) <= 3 assert not rational.int(4,2) <= 1 assert rational.int(4,2) >= 1 assert not rational.int(4,2) >= 3 assert 2 == rational.int(4,2) assert not 3 == rational.int(4,2) assert 3 != rational.int(4,2) assert not 2 != rational.int(4,2) assert 3 > rational.int(4,2) assert not 2 > rational.int(4,2) assert 1 < rational.int(4,2) assert not 2 < rational.int(4,2) assert 3 >= rational.int(4,2) assert not 1 >= rational.int(4,2) assert 1 <= rational.int(4,2) assert not 3 <= rational.int(4,2) r = rational.int(4,3) r += 1 assert r.as_tuple() == (7,3) assert approx_equal(float(r), 7./3) s = rational.int(4,3) assert hash(s) == hash(rational.int(4,3)) assert hash(s) != hash(r) for n in xrange(-100,100): assert hash(n) == hash(rational.int(n)) for d in xrange(1,8): assert hash(rational.int(n,d)) == hash(rational.int(n,d)) assert hash(rational.int(n,d)) == hash(rational.int(3*n,3*d)) assert hash(rational.int(n,d)) == hash(rational.int(-3*n,-3*d)) try: int(r) except RuntimeError, e: assert str(e) == "boost.rational: as_int() conversion error:" \ " denominator is different from one."