コード例 #1
0
def exercise_a_tr_diag_a():
  a = sparse.matrix(9, 7)
  for i in xrange(a.n_rows):
    for j in xrange(a.n_cols):
      if (2*i + j) % 3 == 1: a[i,j] = 1
  w = flex.double([ (-1)**i*i for i in xrange(a.n_rows) ])
  b = a.self_transpose_times_diagonal_times_self(w)
  b0 = sparse.matrix(7, 7)
  b0[0, 0] = 5.
  b0[0, 3] = 5.
  b0[0, 6] = 5.
  b0[1, 1] = 3.
  b0[1, 4] = 3.
  b0[2, 2] = -4.
  b0[2, 5] = -4.
  b0[3, 0] = 5.
  b0[3, 3] = 5.
  b0[3, 6] = 5.
  b0[4, 1] = 3.
  b0[4, 4] = 3.
  b0[5, 2] = -4.
  b0[5, 5] = -4.
  b0[6, 0] = 5.
  b0[6, 3] = 5.
  b0[6, 6] = 5.
  assert sparse.approx_equal(tolerance=1e-12)(b, b0)
コード例 #2
0
    def run(self):
        self.reparam.linearise()
        self.reparam.store()
        uc = self.cs.unit_cell()
        _ = mat.col
        xh, xo, x1, x2 = [
            uc.orthogonalize(sc.site)
            for sc in (self.h, self.o, self.c1, self.c2)
        ]
        u_12 = _(x2) - _(x1)
        u_o1 = _(x1) - _(xo)
        u_oh = _(xh) - _(xo)
        assert approx_equal(u_12.cross(u_o1).dot(u_oh), 0, self.eps)
        assert approx_equal(
            u_12.cross(u_o1).angle(u_oh, deg=True), 90, self.eps)
        assert approx_equal(abs(u_oh), self.bond_length, self.eps)
        assert approx_equal(u_o1.angle(u_oh, deg=True), 109.47, 0.01)

        jt0 = sparse.matrix(3, 14)
        for i in xrange(3):
            jt0[self.xo + i, self.xo + i] = 1.
            jt0[self.xo + i, self.xh + i] = 1.
        jt = self.reparam.jacobian_transpose
        assert sparse.approx_equal(self.eps)(jt, jt0)

        if self.verbose:
            # finite difference derivatives to compare with
            # the crude riding approximation used for analytical ones
            def differentiate(sc):
                eta = 1.e-4
                jac = []
                for i in xrange(3):
                    x0 = tuple(sc.site)
                    x = list(x0)
                    x[i] += eta
                    sc.site = tuple(x)
                    self.reparam.linearise()
                    self.reparam.store()
                    xp = _(self.h.site)
                    x[i] -= 2 * eta
                    sc.site = tuple(x)
                    self.reparam.linearise()
                    self.reparam.store()
                    xm = _(self.h.site)
                    sc.site = tuple(x0)
                    jac.extend((xp - xm) / (2 * eta))
                return mat.sqr(jac)

            jac_o = differentiate(self.o)
            jac_1 = differentiate(self.c1)
            jac_2 = differentiate(self.c2)
            print("staggered: %s" % self.staggered)
            print("J_o:")
            print(jac_o.mathematica_form())
            print("J_1:")
            print(jac_1.mathematica_form())
            print("J_2:")
            print(jac_2.mathematica_form())
コード例 #3
0
  def run(self):
    self.reparam.linearise()
    self.reparam.store()
    uc = self.cs.unit_cell()
    _ = mat.col
    xh, xo, x1, x2 = [ uc.orthogonalize(sc.site)
                       for sc in (self.h, self.o, self.c1, self.c2) ]
    u_12 = _(x2) - _(x1)
    u_o1 = _(x1) - _(xo)
    u_oh = _(xh) - _(xo)
    assert approx_equal(u_12.cross(u_o1).dot(u_oh), 0, self.eps)
    assert approx_equal(u_12.cross(u_o1).angle(u_oh, deg=True), 90, self.eps)
    assert approx_equal(abs(u_oh), self.bond_length, self.eps)
    assert approx_equal(u_o1.angle(u_oh, deg=True), 109.47, 0.01)

    jt0 = sparse.matrix(3, 14)
    for i in xrange(3):
      jt0[self.xo + i, self.xo + i] = 1.
      jt0[self.xo + i, self.xh + i] = 1.
    jt = self.reparam.jacobian_transpose
    assert sparse.approx_equal(self.eps)(jt, jt0)

    if self.verbose:
      # finite difference derivatives to compare with
      # the crude riding approximation used for analytical ones
      def differentiate(sc):
        eta = 1.e-4
        jac = []
        for i in xrange(3):
          x0 = tuple(sc.site)
          x = list(x0)
          x[i] += eta
          sc.site = tuple(x)
          self.reparam.linearise()
          self.reparam.store()
          xp = _(self.h.site)
          x[i] -= 2*eta
          sc.site = tuple(x)
          self.reparam.linearise()
          self.reparam.store()
          xm = _(self.h.site)
          sc.site = tuple(x0)
          jac.extend( (xp - xm)/(2*eta) )
        return mat.sqr(jac)

      jac_o = differentiate(self.o)
      jac_1 = differentiate(self.c1)
      jac_2 = differentiate(self.c2)
      print "staggered: %s" % self.staggered
      print "J_o:"
      print jac_o.mathematica_form()
      print "J_1:"
      print jac_1.mathematica_form()
      print "J_2:"
      print jac_2.mathematica_form()
コード例 #4
0
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)
コード例 #5
0
ファイル: tst_sparse.py プロジェクト: cctbx/cctbx-playground
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)
コード例 #6
0
 def run(self):
   self.reparam.linearise()
   self.reparam.store()
   assert approx_equal(self.sc.u_star, (19/6, 19/6, 17/2,
                                        11/6, 9/2, 9/2), self.eps)
   jt0 = sparse.matrix(2, 8)
   jt0[0, 0] = 1
   jt0[1, 1] = 1
   jac_u_star_trans = self.site_symm.adp_constraints().gradient_sum_matrix()
   jac_u_star_trans.reshape(flex.grid(
     self.site_symm.adp_constraints().n_independent_params(), 6))
   (m,n) = jac_u_star_trans.focus()
   for i in xrange(m):
     for j in xrange(n):
       jt0[i, j + 2] = jac_u_star_trans[i, j]
   jt = self.reparam.jacobian_transpose
   assert sparse.approx_equal(self.eps)(jt, jt0)
コード例 #7
0
    def run(self):
        self.reparam.linearise()
        self.reparam.store()
        x_c0, x_c1, x_h = [
            mat.col(sc.site) for sc in (self.c0, self.c1, self.h)
        ]
        if self.with_special_position_pivot:
            assert approx_equal(x_c0, self.site_symm.exact_site(), self.eps)
        assert approx_equal(self.uc.angle(x_c1, x_c0, x_h), 180, self.eps)
        assert approx_equal(self.uc.distance(x_c0, x_h), self.bond_length,
                            self.eps)

        if self.with_special_position_pivot:
            jt0 = sparse.matrix(
                1 + 3,  # y0, x1
                1 + 3 + 3 + 1 + 3)  # y0, x0, x1, l, x_h
        else:
            jt0 = sparse.matrix(
                3 + 3,  # x0, x1
                3 + 3 + +1 + 3)  # x0, x1, l, x_h

        # Identity for independent parameters
        if self.with_special_position_pivot:
            jt0[self.y0, self.y0] = 1.
        for i in xrange(3):
            jt0[self.x0 + i, self.x0 + i] = 1.
        for i in xrange(3):
            jt0[self.x1 + i, self.x1 + i] = 1.

        # special position x0
        if self.with_special_position_pivot:
            jt0[self.y0, self.x0] = 0
            jt0[self.y0, self.x0 + 1] = 0
            jt0[self.y0, self.x0 + 2] = 1.

        # riding
        if self.with_special_position_pivot:
            jt0[self.y0, self.x_h + 2] = 1.
        else:
            for i in xrange(3):
                jt0[self.x0 + i, self.x_h + i] = 1.

        jt = self.reparam.jacobian_transpose
        assert sparse.approx_equal(self.eps)(jt, jt0)
コード例 #8
0
def exercise_u_iso_proportional_to_pivot_u_eq():
    xs = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=(), space_group_symbol='hall: P 2x 2y'),
                        scatterers=flex.xray_scatterer((
                            xray.scatterer('C0', u=(1, 1, 1, 0, 0, 0)),
                            xray.scatterer('C1'),
                            xray.scatterer('C2',
                                           site=(0.1, 0.2, 0.3),
                                           u=(1, 2, 3, 0, 0, 0)),
                            xray.scatterer('C3'),
                        )))
    r = constraints.ext.reparametrisation(xs.unit_cell())
    sc = xs.scatterers()
    sc[0].flags.set_grad_u_aniso(True)
    sc[2].flags.set_grad_u_aniso(True)

    u_0 = r.add(constraints.special_position_u_star_parameter,
                site_symmetry=xs.site_symmetry_table().get(0),
                scatterer=sc[0])
    u_iso_1 = r.add(constraints.u_iso_proportional_to_pivot_u_eq,
                    pivot_u=u_0,
                    multiplier=3,
                    scatterer=sc[1])
    u_2 = r.add(constraints.independent_u_star_parameter, sc[2])
    u_iso_3 = r.add(constraints.u_iso_proportional_to_pivot_u_eq,
                    pivot_u=u_2,
                    multiplier=2,
                    scatterer=sc[3])
    r.finalise()
    m = 3 + 6
    n = m + 6 + 1 + 1
    r.linearise()
    assert approx_equal(u_iso_1.value, 3, eps=1e-15)
    assert approx_equal(u_iso_3.value, 4, eps=1e-15)
    jt0 = sparse.matrix(m, n)
    for i in xrange(m):
        jt0[i, i] = 1
    p, q = u_0.argument(0).index, u_0.index
    jt0[p, q] = jt0[p + 1, q + 1] = jt0[p + 2, q + 2] = 1
    q = u_iso_1.index
    jt0[p, q] = jt0[p + 1, q] = jt0[p + 2, q] = 1
    p, q = u_2.index, u_iso_3.index
    jt0[p, q] = jt0[p + 1, q] = jt0[p + 2, q] = 2 / 3
    assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)
コード例 #9
0
def exercise_u_iso_proportional_to_pivot_u_eq():
  xs = xray.structure(
    crystal_symmetry=crystal.symmetry(
      unit_cell=(),
      space_group_symbol='hall: P 2x 2y'),
    scatterers=flex.xray_scatterer((
      xray.scatterer('C0', u=(1, 1, 1, 0, 0, 0)),
      xray.scatterer('C1'),
      xray.scatterer('C2', site=(0.1, 0.2, 0.3), u=(1, 2, 3, 0, 0, 0)),
      xray.scatterer('C3'),
      )))
  r = constraints.ext.reparametrisation(xs.unit_cell())
  sc = xs.scatterers()
  sc[0].flags.set_grad_u_aniso(True)
  sc[2].flags.set_grad_u_aniso(True)

  u_0 = r.add(constraints.special_position_u_star_parameter,
              site_symmetry=xs.site_symmetry_table().get(0),
              scatterer=sc[0])
  u_iso_1 = r.add(constraints.u_iso_proportional_to_pivot_u_eq,
                pivot_u=u_0,
                multiplier=3,
                scatterer=sc[1])
  u_2 = r.add(constraints.independent_u_star_parameter, sc[2])
  u_iso_3 = r.add(constraints.u_iso_proportional_to_pivot_u_eq,
                pivot_u=u_2,
                multiplier=2,
                scatterer=sc[3])
  r.finalise()
  m = 3 + 6
  n = m + 6 + 1 + 1
  r.linearise()
  assert approx_equal(u_iso_1.value, 3, eps=1e-15)
  assert approx_equal(u_iso_3.value, 4, eps=1e-15)
  jt0 = sparse.matrix(m, n)
  for i in xrange(m): jt0[i, i] = 1
  p, q = u_0.argument(0).index, u_0.index
  jt0[p, q] = jt0[p+1, q+1] = jt0[p+2, q+2] = 1
  q = u_iso_1.index
  jt0[p, q] = jt0[p+1, q] = jt0[p+2, q] = 1
  p, q = u_2.index, u_iso_3.index
  jt0[p, q] = jt0[p+1, q] = jt0[p+2, q] = 2/3
  assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)
コード例 #10
0
 def run(self):
     self.reparam.linearise()
     self.reparam.store()
     assert approx_equal(self.sc.u_star,
                         (19 / 6, 19 / 6, 17 / 2, 11 / 6, 9 / 2, 9 / 2),
                         self.eps)
     jt0 = sparse.matrix(2, 8)
     jt0[0, 0] = 1
     jt0[1, 1] = 1
     jac_u_star_trans = self.site_symm.adp_constraints(
     ).gradient_sum_matrix()
     jac_u_star_trans.reshape(
         flex.grid(self.site_symm.adp_constraints().n_independent_params(),
                   6))
     (m, n) = jac_u_star_trans.focus()
     for i in xrange(m):
         for j in xrange(n):
             jt0[i, j + 2] = jac_u_star_trans[i, j]
     jt = self.reparam.jacobian_transpose
     assert sparse.approx_equal(self.eps)(jt, jt0)
コード例 #11
0
  def run(self):
    self.reparam.linearise()
    self.reparam.store()
    x_c0, x_c1, x_h = [ mat.col(sc.site)
                        for sc in (self.c0, self.c1, self.h) ]
    if self.with_special_position_pivot:
      assert approx_equal(x_c0, self.site_symm.exact_site(), self.eps)
    assert approx_equal(self.uc.angle(x_c1, x_c0, x_h), 180, self.eps)
    assert approx_equal(
      self.uc.distance(x_c0, x_h), self.bond_length, self.eps)

    if self.with_special_position_pivot:
      jt0 = sparse.matrix(1 + 3, # y0, x1
                          1 + 3 + 3 + 1 + 3) # y0, x0, x1, l, x_h
    else:
      jt0 = sparse.matrix(3 + 3, # x0, x1
                          3 + 3 + + 1 + 3) # x0, x1, l, x_h

    # Identity for independent parameters
    if self.with_special_position_pivot:
      jt0[self.y0, self.y0] = 1.
    for i in xrange(3): jt0[self.x0 + i, self.x0 + i] = 1.
    for i in xrange(3): jt0[self.x1 + i, self.x1 + i] = 1.

    # special position x0
    if self.with_special_position_pivot:
      jt0[self.y0, self.x0    ] = 0
      jt0[self.y0, self.x0 + 1] = 0
      jt0[self.y0, self.x0 + 2] = 1.

    # riding
    if self.with_special_position_pivot:
      jt0[self.y0, self.x_h + 2] = 1.
    else:
      for i in xrange(3): jt0[self.x0 + i, self.x_h + i] = 1.

    jt = self.reparam.jacobian_transpose
    assert sparse.approx_equal(self.eps)(jt, jt0)
コード例 #12
0
    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 = random_vectors.next()
        v = random_vectors.next()
        w = list(-2 * u.as_dense_vector() + 3 * v.as_dense_vector())
        yield u, v, w
コード例 #13
0
def exercise_affine_occupancy_parameter():
    xs = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=(), space_group_symbol='hall: P 1'),
                        scatterers=flex.xray_scatterer((
                            xray.scatterer('C0', occupancy=1),
                            xray.scatterer('C1', occupancy=1),
                            xray.scatterer('C2', occupancy=1),
                            xray.scatterer('C3', occupancy=1),
                        )))
    sc = xs.scatterers()
    sc.flags_set_grad_occupancy(flex.size_t_range(4))

    # Two occupancies adding up to 1 (most common case of disorder)
    r = constraints.ext.reparametrisation(xs.unit_cell())
    occ_1 = r.add(constraints.independent_occupancy_parameter, sc[1])
    occ_3 = r.add(constraints.affine_asu_occupancy_parameter,
                  dependee=occ_1,
                  a=-1,
                  b=1,
                  scatterer=sc[3])
    r.finalise()
    r.linearise()
    assert approx_equal(occ_1.value, 1)
    assert approx_equal(occ_3.value, 0)
    jt0 = sparse.matrix(
        1,
        2,
        [
            {
                0: 1
            },  # 1st col = derivatives of occ_1
            {
                0: -1
            },  # 2nd col = derivatives of occ_3
        ])
    assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)

    # Example illustrating the instruction SUMP in SHELX 97 manual (p. 7-26)
    # We disregard the issue of the special position which is orthogonal to the
    # point we want to test here.
    xs = xray.structure(crystal_symmetry=crystal.symmetry(
        unit_cell=(), space_group_symbol='hall: P 1'),
                        scatterers=flex.xray_scatterer((
                            xray.scatterer('Na+', occupancy=1),
                            xray.scatterer('Ca2+', occupancy=1),
                            xray.scatterer('Al3+', occupancy=0.35),
                            xray.scatterer('K+', occupancy=0.15),
                        )))
    sc = xs.scatterers()
    sc.flags_set_grad_occupancy(flex.size_t_range(4))

    # The constraints are:
    # fully occupied: occ(Na+) + occ(Ca2+) + occ(Al3+) + occ(K+) = 1
    # average charge +2: occ(Na+) + 2 occ(Ca2+) + 3 occ(Al3+) + occ(K+) = +2
    # This can be solved as:
    # occ(Na+)  = occ(Al3+) - occ(K+)
    # occ(Ca2+) = 1 - 2 occ(Al3+)
    r = constraints.ext.reparametrisation(xs.unit_cell())
    occ_Al = r.add(constraints.independent_occupancy_parameter, sc[2])
    occ_K = r.add(constraints.independent_occupancy_parameter, sc[3])
    occ_Na = r.add(constraints.affine_asu_occupancy_parameter,
                   occ_Al,
                   1,
                   occ_K,
                   -1,
                   0,
                   scatterer=sc[0])
    occ_Ca = r.add(constraints.affine_asu_occupancy_parameter,
                   occ_Al,
                   -2,
                   1,
                   scatterer=sc[1])
    r.finalise()
    r.linearise()
    assert approx_equal(occ_Na.value, 0.2)
    assert approx_equal(occ_Ca.value, 0.3)
    assert approx_equal(occ_Al.value, 0.35)
    assert approx_equal(occ_K.value, 0.15)
    jt0 = sparse.matrix(
        2,
        4,
        [
            {
                0: 1
            },  # diff occ(Al3+)
            {
                1: 1
            },  # diff occ(K+)
            {
                0: 1,
                1: -1
            },  # diff occ(Na+)
            {
                0: -2
            },  # diff occ(Ca2+)
        ])
    assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)
コード例 #14
0
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})
コード例 #15
0
def exercise_affine_occupancy_parameter():
  xs = xray.structure(
    crystal_symmetry=crystal.symmetry(unit_cell=(), space_group_symbol='hall: P 1'),
    scatterers=flex.xray_scatterer((
      xray.scatterer('C0', occupancy=1),
      xray.scatterer('C1', occupancy=1),
      xray.scatterer('C2', occupancy=1),
      xray.scatterer('C3', occupancy=1),
    )))
  sc = xs.scatterers()
  sc.flags_set_grad_occupancy(flex.size_t_range(4))

  # Two occupancies adding up to 1 (most common case of disorder)
  r = constraints.ext.reparametrisation(xs.unit_cell())
  occ_1 = r.add(constraints.independent_occupancy_parameter, sc[1])
  occ_3 = r.add(constraints.affine_asu_occupancy_parameter,
                dependee=occ_1, a=-1, b=1, scatterer=sc[3])
  r.finalise()
  r.linearise()
  assert approx_equal(occ_1.value, 1)
  assert approx_equal(occ_3.value, 0)
  jt0 = sparse.matrix(1, 2,
                     [ {0:1},   # 1st col = derivatives of occ_1
                       {0:-1},   # 2nd col = derivatives of occ_3
                     ])
  assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)

  # Example illustrating the instruction SUMP in SHELX 97 manual (p. 7-26)
  # We disregard the issue of the special position which is orthogonal to the
  # point we want to test here.
  xs = xray.structure(
    crystal_symmetry=crystal.symmetry(unit_cell=(), space_group_symbol='hall: P 1'),
    scatterers=flex.xray_scatterer((
      xray.scatterer('Na+', occupancy=1),
      xray.scatterer('Ca2+', occupancy=1),
      xray.scatterer('Al3+', occupancy=0.35),
      xray.scatterer('K+', occupancy=0.15),
    )))
  sc = xs.scatterers()
  sc.flags_set_grad_occupancy(flex.size_t_range(4))

  # The constraints are:
  # fully occupied: occ(Na+) + occ(Ca2+) + occ(Al3+) + occ(K+) = 1
  # average charge +2: occ(Na+) + 2 occ(Ca2+) + 3 occ(Al3+) + occ(K+) = +2
  # This can be solved as:
  # occ(Na+)  = occ(Al3+) - occ(K+)
  # occ(Ca2+) = 1 - 2 occ(Al3+)
  r = constraints.ext.reparametrisation(xs.unit_cell())
  occ_Al = r.add(constraints.independent_occupancy_parameter, sc[2])
  occ_K  = r.add(constraints.independent_occupancy_parameter, sc[3])
  occ_Na = r.add(constraints.affine_asu_occupancy_parameter,
                 occ_Al, 1, occ_K, -1, 0, scatterer=sc[0])
  occ_Ca = r.add(constraints.affine_asu_occupancy_parameter,
                 occ_Al, -2, 1, scatterer=sc[1])
  r.finalise()
  r.linearise()
  assert approx_equal(occ_Na.value, 0.2)
  assert approx_equal(occ_Ca.value, 0.3)
  assert approx_equal(occ_Al.value, 0.35)
  assert approx_equal(occ_K.value, 0.15)
  jt0 = sparse.matrix(2, 4,
                     [
                       {0:1},         # diff occ(Al3+)
                       {1:1} ,        # diff occ(K+)
                       {0:1, 1:-1},   # diff occ(Na+)
                       {0:-2},        # diff occ(Ca2+)
                     ])
  assert sparse.approx_equal(tolerance=1e-15)(r.jacobian_transpose, jt0)
コード例 #16
0
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'
コード例 #17
0
ファイル: tst_sparse.py プロジェクト: cctbx/cctbx-playground
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'
コード例 #18
0
ファイル: tst_sparse.py プロジェクト: cctbx/cctbx-playground
  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 = random_vectors.next()
    v = random_vectors.next()
    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):