def exercise(flags, space_group_info, n_sampled):
    symbol = space_group_info.type().hall_symbol()
    print symbol,
    if flags.fix_seed:
        random.seed(0)
    if not flags.include_high_symmetry:
        if space_group_info.group().order_p() > 8:
            if len(symbol) > 15: print
            print "  [ Omitted, rerun with --include_high_symmetry to override ]"
            return
    print
    n = int(flags.repeats)
    if n == 0: n = 1
    progress = progress_displayed_as_fraction(n)
    for i in xrange(n):
        xs = random_structure.xray_structure(space_group_info=space_group_info,
                                             elements=['C'] * 5 + ['O'] * 2 +
                                             ['N'],
                                             use_u_iso=True,
                                             random_u_iso=True,
                                             random_u_iso_scale=0.04,
                                             use_u_aniso=False)
        f_c = miller.build_set(xs, anomalous_flag=False,
                               d_min=0.8).structure_factors_from_scatterers(
                                   xs, algorithm='direct').f_calc()
        f_o = f_c.as_amplitude_array()
        f_c_in_p1 = f_c.expand_to_p1()
        exercise_value(f_c_in_p1, f_o, flags, n_sampled)
        exercise_gradient(f_c_in_p1, f_o, flags, n_sampled)
        progress.advance()
    progress.done()
def exercise(flags, space_group_info, n_sampled):
  symbol = space_group_info.type().hall_symbol()
  print symbol,
  if flags.fix_seed:
    random.seed(0)
  if not flags.include_high_symmetry:
    if space_group_info.group().order_p() > 8:
      if len(symbol) > 15: print
      print "  [ Omitted, rerun with --include_high_symmetry to override ]"
      return
  print
  n = int(flags.repeats)
  if n == 0: n = 1
  progress = progress_displayed_as_fraction(n)
  for i in xrange(n):
    xs = random_structure.xray_structure(
      space_group_info=space_group_info,
      elements=['C']*5 + ['O']*2 + ['N'],
      use_u_iso=True,
      random_u_iso=True,
      random_u_iso_scale=0.04,
      use_u_aniso=False)
    f_c = miller.build_set(
      xs, anomalous_flag=False, d_min=0.8
      ).structure_factors_from_scatterers(
        xs, algorithm='direct').f_calc()
    f_o = f_c.as_amplitude_array()
    f_c_in_p1 = f_c.expand_to_p1()
    exercise_value(f_c_in_p1, f_o, flags, n_sampled)
    exercise_gradient(f_c_in_p1, f_o, flags, n_sampled)
    progress.advance()
  progress.done()
def compare_times(comprehensive=False,
                  svd_impl_name="dgesvd",
                  use_fortran=False):
    import scitbx.linalg.svd
    from scitbx.array_family import flex
    import time
    from libtbx.utils import progress_displayed_as_fraction
    mt = flex.mersenne_twister(seed=0)
    samples = []
    if not comprehensive:
        dims = (100, 200)
        for m in dims:
            for n in dims:
                samples.append((m, n))
    else:
        if comprehensive == "timing-1":
            dims = range(100, 600, 100)
            for m in dims:
                for n in dims:
                    samples.append((m, n))
        elif comprehensive == "timing-2":
            for k in (1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 3, 4,
                      5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100):
                for n in (10, 20, 30, 40, 50, 60, 70, 80, 90, 100):
                    samples.append((int(k * n), n))
        else:
            raise RuntimeError(comprehensive)
    if not comprehensive:
        header = " m   n  real %s %s" % (svd_impl_name, ["fem", "for"
                                                         ][int(use_fortran)])
    else:
        handwritten_wrt_lapack = []
        progress = progress_displayed_as_fraction(len(samples))
    lapack_svd_impl = getattr(scitbx.linalg, "lapack_%s" % svd_impl_name)
    try:
        for m, n in samples:
            if comprehensive: progress.advance()
            a = mt.random_double(size=m * n) * 4 - 2
            a.reshape(flex.grid(m, n))
            ac = a.deep_copy()
            t0 = time.time()
            svd_real = scitbx.linalg.svd.real(ac,
                                              accumulate_u=True,
                                              accumulate_v=True)
            time_svd_real = time.time() - t0
            t0 = time.time()
            at = a.matrix_transpose()
            svd_lapack = lapack_svd_impl(a=at, use_fortran=use_fortran)
            if (svd_lapack is None):
                return
            svd_lapack.u.matrix_transpose()
            svd_lapack.vt.matrix_transpose()
            time_dgesvd = time.time() - t0
            if not comprehensive:
                if (header is not None):
                    print header
                    header = None
                print "%3d %3d %4.2f %4.2f" % (m, n, time_svd_real,
                                               time_dgesvd)
            else:
                handwritten_wrt_lapack.append(
                    (m, n, time_svd_real / time_dgesvd))
    finally:
        if comprehensive:
            print "handwrittenwrtlapack%s%s={" % (svd_impl_name,
                                                  ['', 'fortran'][use_fortran])
            print ",".join([
                "{%3d, %3d, %4.2f}" % (m, n, t)
                for (m, n, t) in handwritten_wrt_lapack
            ])
            print "}"
def compare_times(comprehensive=False, svd_impl_name="dgesvd", use_fortran=False):
    import scitbx.linalg.svd
    from scitbx.array_family import flex
    import time
    from libtbx.utils import progress_displayed_as_fraction

    mt = flex.mersenne_twister(seed=0)
    samples = []
    if not comprehensive:
        dims = (100, 200)
        for m in dims:
            for n in dims:
                samples.append((m, n))
    else:
        if comprehensive == "timing-1":
            dims = range(100, 600, 100)
            for m in dims:
                for n in dims:
                    samples.append((m, n))
        elif comprehensive == "timing-2":
            for k in (
                1,
                1.1,
                1.2,
                1.3,
                1.4,
                1.5,
                1.6,
                1.7,
                1.8,
                1.9,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10,
                20,
                30,
                40,
                50,
                60,
                70,
                80,
                90,
                100,
            ):
                for n in (10, 20, 30, 40, 50, 60, 70, 80, 90, 100):
                    samples.append((int(k * n), n))
        else:
            raise RuntimeError(comprehensive)
    if not comprehensive:
        header = " m   n  real %s %s" % (svd_impl_name, ["fem", "for"][int(use_fortran)])
    else:
        handwritten_wrt_lapack = []
        progress = progress_displayed_as_fraction(len(samples))
    lapack_svd_impl = getattr(scitbx.linalg, "lapack_%s" % svd_impl_name)
    try:
        for m, n in samples:
            if comprehensive:
                progress.advance()
            a = mt.random_double(size=m * n) * 4 - 2
            a.reshape(flex.grid(m, n))
            ac = a.deep_copy()
            t0 = time.time()
            svd_real = scitbx.linalg.svd.real(ac, accumulate_u=True, accumulate_v=True)
            time_svd_real = time.time() - t0
            t0 = time.time()
            at = a.matrix_transpose()
            svd_lapack = lapack_svd_impl(a=at, use_fortran=use_fortran)
            if svd_lapack is None:
                return
            svd_lapack.u.matrix_transpose()
            svd_lapack.vt.matrix_transpose()
            time_dgesvd = time.time() - t0
            if not comprehensive:
                if header is not None:
                    print header
                    header = None
                print "%3d %3d %4.2f %4.2f" % (m, n, time_svd_real, time_dgesvd)
            else:
                handwritten_wrt_lapack.append((m, n, time_svd_real / time_dgesvd))
    finally:
        if comprehensive:
            print "handwrittenwrtlapack%s%s={" % (svd_impl_name, ["", "fortran"][use_fortran])
            print ",".join(["{%3d, %3d, %4.2f}" % (m, n, t) for (m, n, t) in handwritten_wrt_lapack])
            print "}"