[Tutor] A little math and Python: Horner's rule

Guillermo Fernandez guillermo.fernandez@epfl.ch
Wed, 21 Aug 2002 11:43:36 +0930


> Just when I thought I had strange hobbies =8)...
> 
> > ...     return((((-27 * x - 7) * x - 39) * x - 40) * x - 12) * x - 35
> 
> At the risk of asking an amazingly stupid question, this looks like some of
> the constructs that I used to create in high school to feed my HP 15C
> calculator that uses an entry format called "reverse Polish notation"
> (RPN) which you are probably familiar with: You don't type "2 + 3 =" but
> rather "2 ENTER 4 +" - sounds strange, but after you get used to it, it is
> faster than anything else. Is this Horner's rule somehow related to RPN,
> or does it just look that way?

Actually it only looks that way. What it do is simply expand the first
polynom in order to try to reduce the number of times you multiply x by
itself. If you express the same expression without parentesis, you find
the polynom -27*x^5-7*x^4-39*x^3-40*x^2-12*x-35 again. But if you have a
look to the expression, it still simply an expression that you could
express in polish notation. it would be something like:
((((((((((-27 x *) 7 -) x *) 39 -) x *) 40 -) x *) 12 -) x *) 35 -)

Parentesis are added for clarity only as one of the good things with
polish notations (that I also lve, by the way) is that you don't need
parentesis. This expression is equivalent to 
-27 x * 7 - x * 39 - x * 40 - x * 12 - x * 35 -

But if you look at the expression
((((-27 * x - 7) * x - 39) * x - 40) * x - 12) * x - 35
you will notice that the parentesis are needed!

Well... I hope its not too unclear...

By the way, have you try lisp? If you like recursion and polish
notation, it should interest you :-)

> [Nostalgic rant: A brief visit to the HP website shows me even their
> scientific calculators don't have RPN anymore (except in the HP 48 class),
> which is a damn shame. Fortunately, the old HP made fantastically robust
> products, so my HP 15C is still going strong even after being dropped,
> stepped on, and generally abused physically for almost 20 years now. The
> new stuff just looks flimsy - more like Texas Instruments clones than real
> HP calculators...ah, the days before shareholder value mania...]
I learned to program in the HP48, in a way my first machine :-)
Great calculators once you know how to program and use them!

Guille