3

I have a problem to solve the following excercise:

*Be given the special polynomial: enter image description here 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!

  • Actually I tested it with some values and calculated it by myself with polynomial division and the results were the same as from the algorithm above... –  Nov 21 '17 at 17:54
  • 1
    @GiladGreen *where do you put `x` to the power of the current `n`?* nowhere: [Horner Scheme](https://en.wikipedia.org/wiki/Horner%27s_method#Description_of_the_algorithm) – greybeard Nov 21 '17 at 17:59
  • @greybeard: I think there is double exponentiation in the last two terms as well - but removed for simplification (x to power 1 squared is equal to x & x to power of 0 squared = 1) - so it could be made generic – PaulF Nov 21 '17 at 18:00
  • 1
    (This looks an *excellent* assignment: please nobody spoil it by giving an answer without "spoiler" (`>! `).) – greybeard Nov 21 '17 at 18:19
  • (Much to my dismay, I seem to be able to do/code this with two multiplications per term (which I think minimal), but it looks anything but Horner.) – greybeard Nov 21 '17 at 18:51

2 Answers2

0

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

Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
  • (That's two multiplications per term: how is it *Horner*?) – greybeard Nov 21 '17 at 20:48
  • I guess it depends on what you consider the essence of the Horner method. To my mind, the essence is the nested factorisation, and so this method is in that way similar to Horner. But I totally agree this is subjective and opinions may vary. Can you see an alternative that would be even closer to the Horner method? – Peter de Rivaz Nov 21 '17 at 22:20
0

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.)

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • I have no earthly way of knowing whether 2. is included/implied in what [Peter de Rivaz has been getting at](https://stackoverflow.com/a/47421205/3789665) (short of asking/discussing). – greybeard Nov 23 '17 at 09:06