fork(2) download
  1. #Ephraim Rothschild
  2. #June 10th 2014
  3.  
  4. import math
  5. import timeit
  6. import sys
  7. import os
  8. from functools import partial
  9.  
  10. #Global Variabls
  11. pi2 = 6.28318530718
  12. piover2 = 1.57079632679
  13.  
  14. #Global Lists
  15. factorialList = [1.0,
  16. -6.0,
  17. 120.0,
  18. -5040.0,
  19. 362880.0,
  20. -39916800.0,
  21. 6227020800.0,
  22. -1307674368000.0,
  23. 355687428096000.0,
  24. -121645100408832000.0,
  25. 51090942171709440000.0,
  26. -25852016738884976640000.0,
  27. 15511210043330985984000000.0,
  28. -10888869450418352160768000000.0,
  29. 8841761993739701954543616000000.0]
  30.  
  31. #simplifies angles and converts them to radians
  32. def torad(x):
  33. rev = float(x)/360.0
  34. if (rev>1) or (rev<0):
  35. return (rev - math.floor(rev))*pi2
  36. return rev*pi2
  37.  
  38. def sinyield(x):
  39. squared = x*x
  40. for n in factorialList:
  41. yield x/n
  42. x*=squared
  43.  
  44. def tanfastdivide(sin, cos):
  45. if (cos == 0):
  46. return "infinity"
  47. return sin/cos
  48.  
  49. #start calculating sin cos and tan
  50. def calcyield(outList):
  51. for angle in outList[0]:
  52. angle = torad(angle)
  53. sin = round(math.fsum(sinyield(angle)), 7)
  54. cos=math.copysign(math.sqrt(1-(sin*sin)),(piover2-angle))
  55. yield sin
  56. yield cos
  57. yield tanfastdivide(sin, cos) #tan
  58.  
  59. def calculations(IOList):
  60. calcyieldgen = calcyield(IOList)
  61. for angle in IOList[0]:
  62. IOList[1].append(next(calcyieldgen))
  63. IOList[2].append(next(calcyieldgen))
  64. IOList[3].append(next(calcyieldgen))
  65. return IOList
  66.  
  67. #Begin input from file
  68. def ImportFile():
  69. try:
  70. infile = open("trig.in", "r")
  71. except:
  72. infile = sys.stdin
  73. inList = [[], [], [], []]
  74.  
  75. #take input from file
  76. for line in infile:
  77. inList[0].extend([float(line)])
  78. return inList
  79.  
  80. #Begin output to file
  81. def OutputResults(outList):
  82. try:
  83. outfile = open("trig.out", "w")
  84. PrintOutput(outList, outfile)
  85. except:
  86. print 'Failed to write to file. Printing to stdout instead...'
  87. finally:
  88. PrintOutput(outList, sys.stdout)
  89.  
  90. def PrintOutput(outList, outfile):
  91. #outList[0][i] holds original angles
  92. #outList[1][i] holds sin values
  93. #outList[2][i] holds cos values
  94. #outList[3][i] holds tan values
  95. outfile.write('-----------------------------------------------------\n')
  96. outfile.write(' TRIG RESULTS \n')
  97. outfile.write('-----------------------------------------------------\n')
  98. for i in range(len(outList[0])):
  99. if (i):
  100. outfile.write('\n')
  101. outfile.write("For angle: ")
  102. outfile.write(str(outList[0][i]))
  103. outfile.write('\n ')
  104. outfile.write("Sin: ")
  105. outfile.write(str('%.7E' % float(outList[1][i])))
  106. outfile.write('\n ')
  107. outfile.write("Cos: ")
  108. outfile.write(str('%.7E' % float(outList[2][i])))
  109. outfile.write('\n ')
  110. outfile.write("Tan: ")
  111. outfile.write(str('%.7E' % float(outList[3][i])))
  112.  
  113.  
  114. #Run the Program first
  115. inList = ImportFile()
  116. OutputResults(calculations(inList))
  117.  
  118. #Begin Runtime estimation
  119. def timeTest(IOList):
  120. for output in calcyield(IOList):
  121. pass
  122. def baselined(inList):
  123. for x in inList:
  124. pass
  125.  
  126. totime = timeit.Timer(partial(timeTest, inList))
  127. baseline = timeit.Timer(partial(baselined, inList))
  128. print '\n-----------------------------------------------------'
  129. print ' TIME RESULTS: '
  130. print '-----------------------------------------------------'
  131. OverheadwithCalcTime = min(totime.repeat(repeat=10, number=10000))
  132. Overhead = min(baseline.repeat(repeat=1, number=10000))
  133. estimatedCalcTime = (OverheadwithCalcTime - Overhead)
  134. estimatedTimePerAngle = estimatedCalcTime/len(inList)
  135. estimatedTimePerCalc = estimatedTimePerAngle/3
  136. print ' Estimated CalcTime+Overhead:.....', '%.10f' % (OverheadwithCalcTime*100), 'µsec'
  137. print ' Estimated Overhead Time:..........', '%.10f' % (Overhead*100), 'µsec'
  138. print ''
  139. print ' Estimated CalcTime/Run:..........', '%.10f' % (estimatedCalcTime*100), 'µsec'
  140. print ' Estimated CalcTime/Angle:.........', '%.10f' % (estimatedTimePerAngle*100), 'µsec'
  141. print ' Estimated CalcTime/Cacluation:....', '%.10f' % (estimatedTimePerCalc*100), 'µsec'
  142. print '-----------------------------------------------------'
  143. print " COOL, IT WORKED! "
  144. print '-----------------------------------------------------'
  145.  
Success #stdin #stdout 3.62s 8128KB
stdin
90.00000000
74.54390000
175.5000000
3600000.000
stdout
Failed to write to file. Printing to stdout instead...
-----------------------------------------------------
                    TRIG RESULTS                     
-----------------------------------------------------
For angle: 90.0
    Sin: 1.0000000E+00
    Cos: -0.0000000E+00
    Tan: INF
For angle: 74.5439
    Sin: 9.6383490E-01
    Cos: 2.6650007E-01
    Tan: 3.6166404E+00
For angle: 175.5
    Sin: 7.8459100E-02
    Cos: -9.9691733E-01
    Tan: -7.8701711E-02
For angle: 3600000.0
    Sin: 0.0000000E+00
    Cos: 1.0000000E+00
    Tan: 0.0000000E+00
-----------------------------------------------------
                    TIME RESULTS:                    
-----------------------------------------------------
 Estimated CalcTime+Overhead:..... 35.8361005783 µsec
 Estimated Overhead Time:.......... 0.4333019257 µsec

 Estimated CalcTime/Run:.......... 35.4027986526 µsec
 Estimated CalcTime/Angle:......... 8.8506996632 µsec
 Estimated CalcTime/Cacluation:.... 2.9502332211 µsec
-----------------------------------------------------
                   COOL, IT WORKED!                  
-----------------------------------------------------