Question: write a more efficient version of the function called poly _ opt that takes the same parameters in the same order and returns the same

write a more efficient version of the function called poly_opt that takes the same parameters in the same order and returns the same result. double poly_opt(double a[],double x, long degree)
{
} Practice Problem 5.5(solution page 611)
Suppose we wish to write a function to evaluate a polynomial, where a polynomial of degree \( n \) is defined to have a set of coefficients \( a_{0}, a_{1}, a_{2},\ldots, a_{n}\). For a value \( x \), we evaluate the polynomial by computing
\[
a_{0}+a_{1} x+a_{2} x^{2}+\cdots+a_{n} x^{n}
\]
This evaluation can be implemented by the following function, having as arguments an array of coefficients a, a value x , and the polynomial degree degree (the value \( n \) in Equation 5.2). In this function, we compute both the successive terms of the equation and the successive powers of \( x \) within a single loop:
```
double poly(double a[], double x, long degree)
{
long i;
double result = a[0];
double xpwr = x; /* Equals x^i at start of loop */
for (i =1; i = degree; i++){
result += a[i]* xpwr;
xpwr = x * xpwr;
}
return result;
}
```
A. For degree \( n \), how many additions and how many multiplications does this code perform?
B. On our reference machine, with arithmetic operations having the latencies shown in Figure 5.12, we measure the CPE for this function to be 5.00. Explain how this CPE arises based on the data dependencies formed between iterations due to the operations implementing lines \(7-8\) of the function. Practice Problem 5.6(solution page 611)
Let us continue exploring ways to evaluate polynomials, as described in Practice Problem 5.5. We can reduce the number of multiplications in evaluating a polynomial by applying Horner's method, named after British mathematician William G. Horner (1786-1837). The idea is to repeatedly factor out the powers of \( x \) to get the following evaluation:
\[
a_{0}+x\left(a_{1}+x\left(a_{2}+\cdots+x\left(a_{n-1}+x a_{n}\right)\cdots\right)\right)
\]
Using Horner's method, we can implement polynomial evaluation using the following code:
```
/* Apply Horner's method */
double polyh(double a[], double x, long degree)
{
```
```
long i;
double result = a[degree];
for (i = degree-1; i >=0; i--)
result = a[i]+ x*result;
return result;
}
```
A. For degree \( n \), how many additions and how many multiplications does this code perform?
B. On our reference machine, with the arithmetic operations having the latencies shown in Figure 5.12, we measure the CPE for this function to be 8.00. Explain how this CPE arises based on the data dependencies formed between iterations due to the operations implementing line 7 of the function.
C. Explain how the function shown in Practice Problem 5.5 can run faster, even though it requires more operations. Practice Problem 5.5(solution page 611)
Suppose we wish to write a function to evaluate a polynomial, where a polynomial of degree \( n \) is defined to have a set of coefficients \( a_{0}, a_{1}, a_{2},\ldots, a_{n}\). For a value \( x \), we evaluate the polynomial by computing
\[
a_{0}+a_{1} x+a_{2} x^{2}+\cdots+a_{n} x^{n}
\]
This evaluation can be implemented by the following function, having as arguments an array of coefficients a, a value x , and the polynomial degree degree (the value \( n \) in Equation 5.2). In this function, we compute both the successive terms of the equation and the successive powers of \( x \) within a single loop:
```
double poly(double a[], double x, long degree)
{
long i;
double result = a[0];
double xpwr = x; /* Equals x^i at start of loop */
for (i =1; i = degree; i++){
result += a[i]* xpwr;
xpwr = x * xpwr;
}
return result;
}
```
A. For degree \( n \), how many additions and how many multiplications does this code perform?
B. On our reference machine, with arithmetic operations having the latencies shown in Figure 5.12, we measure the CPE for this function to be 5.00. Explain how this CPE arises based on the data dependencies formed between iterations due to the operations implementing lines \(7-8\) of the function. Practice Problem 5.6(solution page 611)
Let us continue exploring ways to evaluate polynomials, as described in Practice Problem 5.5. We can reduce the number of multiplications in evaluating a polynomial by applying Horner's method, named after British mathematician William G. Horner (1786-1837). The idea is to repeatedly factor out the powers of \( x \) to get the following evaluation:
\[
a_{0}+x\left(a_{1}+x\left(a_{2}+\cdots+x\left(a_{n-1}+x a_{n}\right)\cdots\right)\right)
\]
Using Horner's method, we can implement polynomial evaluation using the following code:
```
/* Apply Horner's method */
double polyh(double a[], double x, long degree)
{
```
```
long i;
double result = a[degree];
for (i = degree-1; i >=0; i--)
result = a[i]+ x*result;
return result;
}
```
A. For degree \( n \), how many additions and how many multiplications does this code perform?
B. On our reference machine, with the arithmetic operations having the lat
write a more efficient version of the function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!