#Ephraim Rothschild
#June 10th 2014
import math
import timeit
import sys
import os
from functools import partial
#Global Variabls
pi2 = 6.28318530718
piover2 = 1.57079632679
#Global Lists
factorialList = [1.0,
-6.0,
120.0,
-5040.0,
362880.0,
-39916800.0,
6227020800.0,
-1307674368000.0,
355687428096000.0,
-121645100408832000.0,
51090942171709440000.0,
-25852016738884976640000.0,
15511210043330985984000000.0,
-10888869450418352160768000000.0,
8841761993739701954543616000000.0]
#simplifies angles and converts them to radians
def torad(x):
rev = float(x)/360.0
if (rev>1) or (rev<0):
return (rev - math.floor(rev))*pi2
return rev*pi2
def sinyield(x):
squared = x*x
for n in factorialList:
yield x/n
x*=squared
def tanfastdivide(sin, cos):
if (cos == 0):
return "infinity"
return sin/cos
#start calculating sin cos and tan
def calcyield(outList):
for angle in outList[0]:
angle = torad(angle)
sin = round(math.fsum(sinyield(angle)), 7)
cos=math.copysign(math.sqrt(1-(sin*sin)),(piover2-angle))
yield sin
yield cos
yield tanfastdivide(sin, cos) #tan
def calculations(IOList):
calcyieldgen = calcyield(IOList)
for angle in IOList[0]:
IOList[1].append(next(calcyieldgen))
IOList[2].append(next(calcyieldgen))
IOList[3].append(next(calcyieldgen))
return IOList
#Begin input from file
def ImportFile():
try:
infile = open("trig.in", "r")
except:
infile = sys.stdin
inList = [[], [], [], []]
#take input from file
for line in infile:
inList[0].extend([float(line)])
return inList
#Begin output to file
def OutputResults(outList):
try:
outfile = open("trig.out", "w")
PrintOutput(outList, outfile)
except:
print 'Failed to write to file. Printing to stdout instead...'
finally:
PrintOutput(outList, sys.stdout)
def PrintOutput(outList, outfile):
#outList[0][i] holds original angles
#outList[1][i] holds sin values
#outList[2][i] holds cos values
#outList[3][i] holds tan values
outfile.write('-----------------------------------------------------\n')
outfile.write(' TRIG RESULTS \n')
outfile.write('-----------------------------------------------------\n')
for i in range(len(outList[0])):
if (i):
outfile.write('\n')
outfile.write("For angle: ")
outfile.write(str(outList[0][i]))
outfile.write('\n ')
outfile.write("Sin: ")
outfile.write(str('%.7E' % float(outList[1][i])))
outfile.write('\n ')
outfile.write("Cos: ")
outfile.write(str('%.7E' % float(outList[2][i])))
outfile.write('\n ')
outfile.write("Tan: ")
outfile.write(str('%.7E' % float(outList[3][i])))
#Run the Program first
inList = ImportFile()
OutputResults(calculations(inList))
#Begin Runtime estimation
def timeTest(IOList):
for output in calcyield(IOList):
pass
def baselined(inList):
for x in inList:
pass
totime = timeit.Timer(partial(timeTest, inList))
baseline = timeit.Timer(partial(baselined, inList))
print '\n-----------------------------------------------------'
print ' TIME RESULTS: '
print '-----------------------------------------------------'
OverheadwithCalcTime = min(totime.repeat(repeat=10, number=10000))
Overhead = min(baseline.repeat(repeat=1, number=10000))
estimatedCalcTime = (OverheadwithCalcTime - Overhead)
estimatedTimePerAngle = estimatedCalcTime/len(inList)
estimatedTimePerCalc = estimatedTimePerAngle/3
print ' Estimated CalcTime+Overhead:.....', '%.10f' % (OverheadwithCalcTime*100), 'µsec'
print ' Estimated Overhead Time:..........', '%.10f' % (Overhead*100), 'µsec'
print ''
print ' Estimated CalcTime/Run:..........', '%.10f' % (estimatedCalcTime*100), 'µsec'
print ' Estimated CalcTime/Angle:.........', '%.10f' % (estimatedTimePerAngle*100), 'µsec'
print ' Estimated CalcTime/Cacluation:....', '%.10f' % (estimatedTimePerCalc*100), 'µsec'
print '-----------------------------------------------------'
print " COOL, IT WORKED! "
print '-----------------------------------------------------'