简体   繁体   中英

Simplify algorithm with Horner Scheme

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!

HINT 1

4*4 = 1 + 3 + 5 + 7

HINT 2

x^(4*4) = x^1 * x^3 * x^5 * x^7

HINT 3

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)

HINT 5

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₀ :

  • just raise x₀ to the appropriate power k for each non-zero coefficient aₖ ( k > 1),
    multiply and accumulate these terms
  • start with the "highest" coefficient; while there is a "lower" coefficient, multiply with x₀ raised to the difference in exponents between the previous coefficient and the current and add the latter

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 ?

  1. re-use lesser powers in the computation of greater ones.

    • square exponents are special

      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

  2. evaluate from "highest coefficient" to lowest

    • to combine this with 1.,

      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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM