Ejemplo n.º 1
0
def scatterer_as_g_alpha(scatterer, hkl, d_star_sq):
    return g_exp_i_alpha_derivatives.parameters(
        g=scatterer.occupancy *
        math.exp(-2 * math.pi**2 * scatterer.u_iso * d_star_sq),
        ffp=1 + scatterer.fp,
        fdp=scatterer.fdp,
        alpha=2 * math.pi * matrix.col(scatterer.site).dot(matrix.col(hkl)))
 def as_g_alpha(self, hkl, d_star_sq):
     return g_exp_i_alpha_derivatives.parameters(
         g=self.w * math.exp(-2 * math.pi ** 2 * self.u * d_star_sq),
         ffp=1 + self.fp,
         fdp=self.fdp,
         alpha=2 * math.pi * matrix.col(self.xyz).dot(matrix.col(hkl)),
     )
def exercise(args):
  verbose =  "--verbose" in args
  if (not verbose):
    out = StringIO()
  else:
    out = sys.stdout
  for n_params in xrange(2,5):
    for i_trial in xrange(5):
      params = []
      for i in xrange(n_params):
        params.append(parameters(
          g=(random.random()-0.5)*2,
          ffp=(random.random()-0.5)*2,
          fdp=(random.random()-0.5)*2,
          alpha=2*math.pi*random.random()))
      exp_sum = g_exp_i_alpha_sum(params=params)
      obs = abs(exp_sum.f())
      compare_analytical_and_finite(
        obs=obs,
        params=params,
        out=out)
      compare_analytical_and_finite(
        obs=obs*(random.random()+0.5),
        params=params,
        out=out)
  print "OK"
def scatterer_as_g_alpha(scatterer, hkl, d_star_sq):
  return g_exp_i_alpha_derivatives.parameters(
    g = scatterer.occupancy
        * math.exp(-2 * math.pi**2 * scatterer.u_iso * d_star_sq),
    ffp = 1 + scatterer.fp,
    fdp = scatterer.fdp,
    alpha = 2 * math.pi * matrix.col(scatterer.site).dot(matrix.col(hkl)))
def d2_target_d_params_finite(obs, params, eps=1.e-8):
  result = []
  params_eps = copy.deepcopy(params)
  for i_param in xrange(len(params)):
    for ix in xrange(4):
      gs = []
      for signed_eps in [eps, -eps]:
        pi_eps = params[i_param].as_list()
        pi_eps[ix] += signed_eps
        params_eps[i_param] = parameters(*pi_eps)
        exp_sum = g_exp_i_alpha_sum(params=params_eps)
        target = least_squares(obs=obs, calc=exp_sum.f())
        dp = exp_sum.d_target_d_params(target=target)
        gs.append(pack_gradients(dp))
      result.append([(gp-gm)/(2*eps) for gp,gm in zip(gs[0],gs[1])])
    params_eps[i_param] = params[i_param]
  return result
Ejemplo n.º 6
0
def d2_target_d_params_finite(obs, params, eps=1.e-8):
    result = []
    params_eps = copy.deepcopy(params)
    for i_param in range(len(params)):
        for ix in range(4):
            gs = []
            for signed_eps in [eps, -eps]:
                pi_eps = params[i_param].as_list()
                pi_eps[ix] += signed_eps
                params_eps[i_param] = parameters(*pi_eps)
                exp_sum = g_exp_i_alpha_sum(params=params_eps)
                target = least_squares(obs=obs, calc=exp_sum.f())
                dp = exp_sum.d_target_d_params(target=target)
                gs.append(pack_gradients(dp))
            result.append([(gp - gm) / (2 * eps)
                           for gp, gm in zip(gs[0], gs[1])])
        params_eps[i_param] = params[i_param]
    return result
Ejemplo n.º 7
0
def d_target_d_params_finite(obs, params, eps=1.e-8):
    result = []
    params_eps = copy.deepcopy(params)
    for i_param in range(len(params)):
        dx = []
        for ix in range(4):
            ts = []
            for signed_eps in [eps, -eps]:
                pi_eps = params[i_param].as_list()
                pi_eps[ix] += signed_eps
                params_eps[i_param] = parameters(*pi_eps)
                exp_sum = g_exp_i_alpha_sum(params=params_eps)
                target = least_squares(obs=obs, calc=exp_sum.f())
                ts.append(target.f())
            dx.append((ts[0] - ts[1]) / (2 * eps))
        result.append(gradients(*dx))
        params_eps[i_param] = params[i_param]
    return result
def d_target_d_params_finite(obs, params, eps=1.e-8):
  result = []
  params_eps = copy.deepcopy(params)
  for i_param in xrange(len(params)):
    dx = []
    for ix in xrange(4):
      ts = []
      for signed_eps in [eps, -eps]:
        pi_eps = params[i_param].as_list()
        pi_eps[ix] += signed_eps
        params_eps[i_param] = parameters(*pi_eps)
        exp_sum = g_exp_i_alpha_sum(params=params_eps)
        target = least_squares(obs=obs, calc=exp_sum.f())
        ts.append(target.f())
      dx.append((ts[0]-ts[1])/(2*eps))
    result.append(gradients(*dx))
    params_eps[i_param] = params[i_param]
  return result
Ejemplo n.º 9
0
def exercise(args):
    verbose = "--verbose" in args
    if (not verbose):
        out = StringIO()
    else:
        out = sys.stdout
    for n_params in range(2, 5):
        for i_trial in range(5):
            params = []
            for i in range(n_params):
                params.append(
                    parameters(g=(random.random() - 0.5) * 2,
                               ffp=(random.random() - 0.5) * 2,
                               fdp=(random.random() - 0.5) * 2,
                               alpha=2 * math.pi * random.random()))
            exp_sum = g_exp_i_alpha_sum(params=params)
            obs = abs(exp_sum.f())
            compare_analytical_and_finite(obs=obs, params=params, out=out)
            compare_analytical_and_finite(obs=obs * (random.random() + 0.5),
                                          params=params,
                                          out=out)
    print("OK")
 def as_g_alpha(self, hkl, d_star_sq):
     return g_exp_i_alpha_derivatives.parameters(
         g=self.w * math.exp(-2 * math.pi**2 * self.u * d_star_sq),
         ffp=1 + self.fp,
         fdp=self.fdp,
         alpha=2 * math.pi * matrix.col(self.xyz).dot(matrix.col(hkl)))