My professor includes a \"test script\" for us to write our code in and test it,
ID: 3601896 • Letter: M
Question
My professor includes a "test script" for us to write our code in and test it, I have included it below
'''implement three ways to evaluate a degree m polynomial
pm(x) = c[0] + c[1]*x + ... c[m]*x**m .
at x, and reurn the polynomial value pm(x).
the m should be determined from the length of the input vector c.
note 1: for each input c, the polynomial is assumed to be c[0] + c[1]*x + ... c[m]*x**m,
not the other form c[0]*x**m + c[1]*x**(m-1) + ... + c[m] which is also often used.
note 2: if x is a vector, then the returned value should be a vector of same length as x.
'''
import numpy as np
def polym(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it computes x**i at the i-th iteration (the slowest of all of the three methods)
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)
def polym2(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by summing term by term,
# it should not compute x**i directly at each iteration, instead it should update
# x**i as x**(i-1)*x, reusing the x**(i-1) that is already computed at the previous iteration
#
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)
#(also, be careful when copying an np array, if not done properly, it would be hard to debug)
def horner(c, x):
# this function evaluates c[0] + c[1]*x + ... c[m]*x**m by the Horner's method.
#make sure x is an np array so that it can be scaled (i.e., x*x will be x**2 elementwisely)
def compare_efficiency():
import time
print(' ==> Now compare the efficiency of the methods: ')
mmin=4000; mmax=20000; step=2000
#initialize lists to store the time for each method
time_poly=[]; time_poly2=[]; time_horner=[]
for m in range(mmin, mmax, step):
c = np.random.randn(m)
x = np.random.randn(round(m/4));
x /= max(abs(x))
#using the above generated x and c,
#add code below to call the three functions above, find the excution time for each,
#and store the time in the corresponding lists
#add code to print out the timing info using the format specified in the project PDF file
x= range(mmin, mmax, step)
#add code below to plot the figure using the format specified in the project PDF file
#add code save the plot into a file naed 'poly_eval_compare.png' for submission
#==========================================================================
#do not modify code below this line
def accuracy_check(c, x, tol=1e-8):
failcount=0
p1 = polym(c, x)
p2 = polym2(c, x)
p3 = horner(c, x)
print(' by polym: {}, by polym2:{}, by horner:{}'.format(p1, p2, p3))
if max(abs((p1 - p3)/p3)) > tol:
print(' **** error in either polym() or horner() *** '); failcount+=1
elif max(abs((p2 - p3)/p3)) > tol:
print(' **** error in either polym2() or horner() *** '); failcount+=1
elif max(abs((p1 - p2)/p1)) > tol:
print(' **** error in polym() or polym2() *** '); failcount+=1
else:
print(' Well done, polynomial values by all methods are the same ! ')
return failcount
if __name__=='__main__':
import matplotlib.pyplot as plt
fcount = 0
pf = lambda x: np.pi + x**6 - x**9 + np.e*x**10
c = [np.pi, 0, 0, 1, 0, -1, 0, 0, 0, 0, np.e]; x= [-0.8, 0.9]
print(' Test 1:'); fcount += accuracy_check(c, x)
c = np.random.randint(-100, 100, size=(61))/1.; x = np.random.rand(10)
print(' Test 2:'); fcount += accuracy_check(c, x)
k = 3
for m in range(2, 350, 20):
c = np.random.randn(m); x = np.random.randn(12)
print(' Test {} on degree {} polynomial'.format(k, m))
fcount += accuracy_check(c, x)
k+=1
if fcount == 0:
print(' Congratulations for passing all tests. ')
else:
print(" Total number of failed tests={}, need more debugging".format(fcount))
#compare the time efficiency (evaluting higher degree polynomials at many points)
#(you may want to comment out the flowing line whe debuging to pass the above tests)
compare_efficiency()
Explanation / Answer
#do not modify code below this line
def accuracy_check(c, x, tol=1e-8):
failcount=0
p1 = polym(c, x)
p2 = polym2(c, x)
p3 = horner(c, x)
print(' by polym: {}, by polym2:{}, by horner:{}'.format(p1, p2, p3))
if max(abs((p1 - p3)/p3)) > tol:
print(' **** error in either polym() or horner() *** '); failcount+=1
elif max(abs((p2 - p3)/p3)) > tol:
print(' **** error in either polym2() or horner() *** '); failcount+=1
elif max(abs((p1 - p2)/p1)) > tol:
print(' **** error in polym() or polym2() *** '); failcount+=1
else:
print(' Well done, polynomial values by all methods are the same ! ')
return failcount
if __name__=='__main__':
import matplotlib.pyplot as plt
fcount = 0
pf = lambda x: np.pi + x**6 - x**9 + np.e*x**10
c = [np.pi, 0, 0, 1, 0, -1, 0, 0, 0, 0, np.e]; x= [-0.8, 0.9]
print(' Test 1:'); fcount += accuracy_check(c, x)
c = np.random.randint(-100, 100, size=(61))/1.; x = np.random.rand(10)
print(' Test 2:'); fcount += accuracy_check(c, x)
k = 3
for m in range(2, 350, 20):
c = np.random.randn(m); x = np.random.randn(12)
print(' Test {} on degree {} polynomial'.format(k, m))
fcount += accuracy_check(c, x)
k+=1
if fcount == 0:
print(' Congratulations for passing all tests. ')
else:
print(" Total number of failed tests={}, need more debugging".format(fcount))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.