def exercise_prime_factors_of(): from libtbx.math_utils import prime_factors_of assert prime_factors_of(n=1) == [] prime_set = set() for n in xrange(2, 100): primes = prime_factors_of(n) pp = 1 for p in primes: pp *= p assert pp == n prime_set.update(primes) if (n == 30): assert prime_set == set([2,3,5,7,11,13,17,19,23,29]) for n in prime_set: assert prime_factors_of(n) == [n] assert len(prime_set) == 25
def compare_fftpack_with_hermft_1d(): mt = flex.mersenne_twister(seed=0) for n_cmpl in xrange(1, 101): primes = prime_factors_of(n_cmpl) if (n_cmpl != 1 and max(primes) > 19): continue n_real = n_cmpl * 2 m_real = n_real + 2 z = (mt.random_double(size=m_real)*2-1).as_float() # The imaginary parts of the first and last complex values should # be zero, but z has random values in these places, to prove that # they don't change the result. hermft_xy = z.deep_copy() hermft_xy[1] = hermft_xy[-2] # real part of last complex value stored in imaginary part of first # (see ccp4/doc/libfft.doc) hermft_xy[-2] = 253 # random value, for consistency check below d = flex.int((m_real,2,m_real,m_real,m_real)) ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d) # consistency check first assert hermft_xy[-2] == 253 assert hermft_xy[-1] == z[-1] # reset to zero for comparision with fftpack result further down hermft_xy[-2] = 0 hermft_xy[-1] = 0 fft = scitbx.fftpack.real_to_complex(n_real) assert fft.m_real() == m_real fftpack_xy = z.as_double() for i in xrange(n_cmpl): fftpack_xy[i*2+1] *= -1 # conjugate fft.backward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(hermft_xy-fftpack_xy) > 1e-5): assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
def exercise_prime_factors_of(): from libtbx.math_utils import prime_factors_of assert prime_factors_of(n=1) == [] prime_set = set() for n in xrange(2, 100): primes = prime_factors_of(n) pp = 1 for p in primes: pp *= p assert pp == n prime_set.update(primes) if (n == 30): assert prime_set == set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29]) for n in prime_set: assert prime_factors_of(n) == [n] assert len(prime_set) == 25
def compare_fftpack_with_hermft_1d(): mt = flex.mersenne_twister(seed=0) for n_cmpl in xrange(1, 101): primes = prime_factors_of(n_cmpl) if (n_cmpl != 1 and max(primes) > 19): continue n_real = n_cmpl * 2 m_real = n_real + 2 z = (mt.random_double(size=m_real) * 2 - 1).as_float() # The imaginary parts of the first and last complex values should # be zero, but z has random values in these places, to prove that # they don't change the result. hermft_xy = z.deep_copy() hermft_xy[1] = hermft_xy[-2] # real part of last complex value stored in imaginary part of first # (see ccp4/doc/libfft.doc) hermft_xy[-2] = 253 # random value, for consistency check below d = flex.int((m_real, 2, m_real, m_real, m_real)) ccp4io_dev_ext.fftlib_hermft(xy=hermft_xy, n=n_cmpl, d=d) # consistency check first assert hermft_xy[-2] == 253 assert hermft_xy[-1] == z[-1] # reset to zero for comparision with fftpack result further down hermft_xy[-2] = 0 hermft_xy[-1] = 0 fft = scitbx.fftpack.real_to_complex(n_real) assert fft.m_real() == m_real fftpack_xy = z.as_double() for i in xrange(n_cmpl): fftpack_xy[i * 2 + 1] *= -1 # conjugate fft.backward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(hermft_xy - fftpack_xy) > 1e-5): assert approx_equal(hermft_xy, fftpack_xy, eps=1e-5)
def compare_fftpack_with_cmplft_1d(): mt = flex.mersenne_twister(seed=0) for n in xrange(1, 101): primes = prime_factors_of(n) if (n != 1 and max(primes) > 19): continue z = (mt.random_double(size=n*2)*2-1).as_float() cmplft_xy = z.deep_copy() d = flex.int((2*n,2,2*n,2*n,2*n)) ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=n, d=d) fft = scitbx.fftpack.complex_to_complex(n) fftpack_xy = z.as_double() fft.forward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(cmplft_xy-fftpack_xy) > 1e-5): assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-5)
def compare_fftpack_with_cmplft_1d(): mt = flex.mersenne_twister(seed=0) for n in xrange(1, 101): primes = prime_factors_of(n) if (n != 1 and max(primes) > 19): continue z = (mt.random_double(size=n * 2) * 2 - 1).as_float() cmplft_xy = z.deep_copy() d = flex.int((2 * n, 2, 2 * n, 2 * n, 2 * n)) ccp4io_dev_ext.fftlib_cmplft(xy=cmplft_xy, n=n, d=d) fft = scitbx.fftpack.complex_to_complex(n) fftpack_xy = z.as_double() fft.forward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(cmplft_xy - fftpack_xy) > 1e-5): assert approx_equal(cmplft_xy, fftpack_xy, eps=1e-5)
def compare_fftpack_with_realft_1d(): mt = flex.mersenne_twister(seed=0) for n_cmpl in xrange(1, 101): primes = prime_factors_of(n_cmpl) if (n_cmpl != 1 and max(primes) > 19): continue n_real = n_cmpl * 2 m_real = n_real + 2 z = (mt.random_double(size=m_real)*2-1).as_float() realft_xy = z.deep_copy() d = flex.int((m_real,2,m_real,m_real,m_real)) ccp4io_dev_ext.fftlib_realft(xy=realft_xy, n=n_cmpl, d=d) fft = scitbx.fftpack.real_to_complex(n_real) assert fft.m_real() == m_real fftpack_xy = z.as_double() fft.forward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(realft_xy-fftpack_xy) > 1e-5): assert approx_equal(realft_xy, fftpack_xy, eps=1e-5)
def compare_fftpack_with_realft_1d(): mt = flex.mersenne_twister(seed=0) for n_cmpl in xrange(1, 101): primes = prime_factors_of(n_cmpl) if (n_cmpl != 1 and max(primes) > 19): continue n_real = n_cmpl * 2 m_real = n_real + 2 z = (mt.random_double(size=m_real) * 2 - 1).as_float() realft_xy = z.deep_copy() d = flex.int((m_real, 2, m_real, m_real, m_real)) ccp4io_dev_ext.fftlib_realft(xy=realft_xy, n=n_cmpl, d=d) fft = scitbx.fftpack.real_to_complex(n_real) assert fft.m_real() == m_real fftpack_xy = z.as_double() fft.forward(fftpack_xy) fftpack_xy = fftpack_xy.as_float() if (flex.max_absolute(realft_xy - fftpack_xy) > 1e-5): assert approx_equal(realft_xy, fftpack_xy, eps=1e-5)
def run(args): for arg in args: n = int(arg) assert n > 0 print "prime factors of %d:" % n, prime_factors_of(n)