Advertisement
homer512

cexp

Dec 15th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. import math
  5. import cmath
  6. import numpy
  7.  
  8.  
  9. DEFAULT_P0 = -0.07969529411773515
  10. DEFAULT_PSTEP = -73.63139197777613
  11. DEFAULT_TOTAL = 16384
  12.  
  13. def double_exp(p0=DEFAULT_P0,
  14.                pstep=DEFAULT_PSTEP,
  15.                total=DEFAULT_TOTAL):
  16.     exp = cmath.exp
  17.     for count in range(total):
  18.         yield exp(1j * (p0 + count * pstep))
  19.  
  20.  
  21. def double_mult(p0=DEFAULT_P0,
  22.                pstep=DEFAULT_PSTEP,
  23.                total=DEFAULT_TOTAL):
  24.     exp = cmath.exp
  25.     ep0, estep = exp(1j * p0), exp(1j * pstep)
  26.     for _ in range(total):
  27.         yield ep0
  28.         ep0 *= estep
  29.  
  30.  
  31. def float_exp(p0=DEFAULT_P0,
  32.               pstep=DEFAULT_PSTEP,
  33.               total=DEFAULT_TOTAL):
  34.     exp = numpy.exp
  35.     c64 = numpy.complex64
  36.     f32 = numpy.float32
  37.     p0, pstep = c64(1j * p0), c64(1j * pstep)
  38.     for count in range(total):
  39.         count = f32(count)
  40.         yield exp(p0 + count * pstep)
  41.  
  42.  
  43. def float_mult(p0=DEFAULT_P0,
  44.                pstep=DEFAULT_PSTEP,
  45.                total=DEFAULT_TOTAL):
  46.     exp = cmath.exp
  47.     c64 = numpy.complex64
  48.     ep0, epstep = c64(exp(1j * p0)), c64(exp(1j * pstep))
  49.     for _ in range(total):
  50.         yield ep0
  51.         ep0 *= estep
  52.  
  53.  
  54. def float_mod(p0=DEFAULT_P0,
  55.               pstep=DEFAULT_PSTEP,
  56.               total=DEFAULT_TOTAL):
  57.     exp = numpy.exp
  58.     c64 = numpy.complex64
  59.     f32 = numpy.float32
  60.     fmod = math.fmod
  61.     twopi = 2. * math.pi    
  62.     p0, pstep = fmod(p0, twopi), fmod(pstep, twopi)
  63.     p0, pstep = c64(1j * p0), c64(1j * pstep)
  64.     for count in range(total):
  65.         count = f32(count)
  66.         yield exp(p0 + count * pstep)
  67.  
  68.  
  69. def main():
  70.     generators = (double_exp(), double_mult(), float_exp(), float_mult(),
  71.                   float_mod())
  72.     arrays = [numpy.fromiter(generator, dtype=numpy.complex64)
  73.               for generator in generators]
  74.     combined = numpy.vstack(arrays)
  75.     rel_errors = abs(combined - combined[0]) / abs(combined[0])
  76.     print(numpy.amax(rel_errors, 1))
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement