I have a problem to solve the following excercise:
*Be given the special polynomial: and the input: coefficients a[n], a[n-1], ..., a[0], argument x
Create an algorithm in C# or Pseudocode which will use Horner's method to solve the special polynomial for x.*
I created an algorithm to solve default polynomial functions with Horner's method, but it doesn't work for the special function, because the exponents are squared. I don't know how to modify the algorithm to respect the squared exponents, because as far as I know, Horner's method doesn't use exponents. This is my code:
int[] a = new int[] { 0, 3, 2, 1};//a[0] - a[n]
int n = 3;
int x = 2;
double r = a[n];
for (int i = n - 1; i >= 0; i--)
{
r = r * x + a[i];
}
Console.WriteLine(r);
I'm thankful for any help!
4*4 = 1 + 3 + 5 + 7
x^(4*4) = x^1 * x^3 * x^5 * x^7
a(4)*x^(4*4) + a(3)*x^(3*3) + a(2)*x^(2*2) + a(1)*x + a(0) = (((a(4)*x^7 + a(3)) * x^5 + a(2) ) * x^3 + a(1) ) * x^1 + a(0)
You can keep track of the odd powers of x by multiplying the previous odd power by x^2 on each iteration
Let me revisit the blunt way and Horner's method to evaluate a polynomial aₙxⁿ+…+a₂x²+a₁x+a₀ in a single variable x at a given value x₀ :
Horner's method saves work because the powers x₀ has to be raised to are (much) lower and (, for this very reason,) fewer (down to only needing/using x₀ ).
How do you save work evaluating a polynomial with square exponents ?
re-use lesser powers in the computation of greater ones.
just as neighbouring squares differ by and odd number two greater that the next lower pair, neighbouring square powers are an odd power apart
the next odd power is the current one multiplied by the square
evaluate from "highest coefficient" to lowest
have power evaluation memoized or keep powers (used) explicitly.
It may be easiest to set them up upfront.
Is the combination of tricks (still) Horner's method ?
You decide. (And your tutor/teacher/interviewer as applicable.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.