How to use Reverse-Polish Mathematical Notation

Embed Size (px)

Citation preview

  • 8/7/2019 How to use Reverse-Polish Mathematical Notation

    1/4

    UNDERSTANDING REVERSE-POLISH NOTATION

    (c) James E. Fletcher 2008

    For mere mortals using or considering an HP scientific calculatoror computer, the question often arises, "What the heck is

    Reverse-Polish Notation?"

    The conventional mathematical notation we learned at school isknown as "Algebraic Notation." In this notation, the operatorsare placed either between or in front of the arguments.Operators designate the operation to be performed ("+", "*","sin", etc.) and arguments are the values or variables upon whichthey act ("1", "42.5", "pi", "x", etc.). A typical mathematicalexpression might be:

    sin[123 + 45 ln(27 - 6)]

    The operators in this expression, from left to right, are "sin","+", "*" (implied between "45" and "ln"), "ln", and "-". Bytheir natures, "sin" and "ln" require one argument each while"+", "*", and "-" require two each.

    The arguments are not so easily determined. At first glance theyappear to be "123", "45", "27", and "6", but this is patentlyfalse! The argument of "sin" is everything within the brackets,or "123 + 45 ln(27 - 6)", those of "+" are "123" and "45 ln(27 -6)", those of "*" are "45" and "ln(27 - 6)", that of "ln" is "27- 6", and those of "-" are "27" and "6".

    The whole expression may be thought of as a series of separate

    operations:

    sin[123 + 45 ln(27 - 6)] = sin(a) :: a = 123 + 45 ln(27 - 6)123 + 45 ln(27 - 6) = 123 + b :: b = 45 ln(27 - 6)45 ln(27 - 6) = 45 * c :: c = ln(27 - 6)ln(27 - 6) = ln(d) :: d = 27 - 627 - 6

    It is because of this that complex expressions are performed fromthe inside out:

    sin[123 + 45 ln(27 - 6)] = sin[123 + 45 ln(21)

    sin[123 + 45 ln(21)] = sin(123 + 45 * 3.04452243772)sin(123 + 45 * 3.04452243772) = sin(123 + 137.003509697)sin(123 + 137.003509697) = sin(260.003509697)sin(260.003539697) = 0.680672740775

    In this manner, it can easily be seen that each operation is afunction of its argument or arguments, and an argument may itselfbe a function of other arguments. Therefore, if we were to treatthe operations exactly as functions of arguments in thetraditional (x) and (y,x) formats, the expression "27 - 6"would become -(27,6), where "-" is the operator "", "27" is theargument "y", and "6" is the argument "x":

    27 - 6 > -(27,6)

    This is a two-argument function: in any two-argument function,

  • 8/7/2019 How to use Reverse-Polish Mathematical Notation

    2/4

    the second argument will always act against the first argument.In this case, the second argument "6" will be subtracted from thefirst argument "27".

    The concept of treating all operators as functions is not asstrange as it first appears when you consider that a considerablenumber of operators are already functions: ln(x) is (x) where

    "" is "ln".

    In the case of our example expression, the next function out isln(d), where "d" is itself the function -(27,6):

    27 - 6 > -(27,6)ln(27 - 6) > ln(-(27,6))45 ln(27 - 6) > *(45,ln(-(27,6)))123 + 45 ln(27 - 6) > +(123,*(45,ln(-(27,6))))sin[123 + 45 ln(27 - 6)] > sin(+(123,*(45,ln(-(27,6)))))

    The final function sin(+(123,*(45,ln(-(27,6))))) may at first

    seem confusing, especially with all the parentheses, until oneremembers that it is the one-argument function sin(x). Once thisis realized, then everything within the parenthesis following"sin" must be the one and only argument of sin(): the argumentof sin() must be +(123,*(45,ln(-(27,6)))).

    In a similar manner, +(123,*(45,ln(-(27,6)))) is the two-argumentfunction +(y,x), where "y" is "123" and "x" is *(45,ln(-(27,6))).

    A further clarification can be made if "+(y,x)" is thought of as"the sum of y and x" (just as "ln(x)" is "the natural logarithmof x"). This approach allows "sin(+(123,*(45,ln(-(27,6)))))" tobe read as "the sine of the sum of 123 and the product of 45 and

    the natural logarithm of the difference of 27 and 6." Thisphrase readily illustrates its clarity when empasized function byfunction:

    sin(+(123,*(45,ln(-(27,6)))))

    The sine of sin(...)the sum of sin(+(...))123 and the product of sin(+(123,*(...)))45 and the natural logarithm of sin(+(123,*(45,ln(...))))the difference of sin(+(123,*(45,ln(-(...)))))27 and 6. sin(+(123,*(45,ln(-(27,6)))))

    Awkward as that may seem, it is much cleaner and morestraightforward than "sin[123+45ln(27-6)]," which is "the sine ofthe quantity 123 plus 45 times the natural logarithm of thequantity 27 minus 6."

    What is even more important, it is unambiguous! If you heardsomeone say "the quantity 123 plus 45 times the natural logarithmof ...," does the speaker mean "(123+45)ln()" or"(123+(45ln()))". By defining all operations as functions thereis no ambiguity, ever!

    In algebra, a complex set of rules has been established as

    regards order and priority of operations, and if these rules arestrictly followed there will be no ambiguity. Unfortunately, fewof the "algebraic" calculators or software packages on the market

  • 8/7/2019 How to use Reverse-Polish Mathematical Notation

    3/4

    follow these rules properly, and there is a total lack ofconsistency in which rules are overlooked or changed.

    What is needed with a calculator or computer is a mechanistic andtotally unambiguous method of operation. Treating all argumentsas functions provides just such a method and is known as PolishNotation after its creator, the Polish logician Jan Lukasiewicz.

    The complex function sin(+(123,*(45,ln(-(27,6))))) has one andonly one possible meaning, whether written in mathematicalsymbology or spoken aloud.

    Further research by the logicians at Hewlett-Packard provided aslight variation on the theme. If instead of placing thefunction ahead of its argument(s), it is placed behind, we get:

    sin(+(123,*(45,ln(-(27,6)))))sin( ) > ( )sin

    +(123, ) > (123, )+

    *(45, ) > (45, )*ln( ) > ( )ln-(27,6) > (27,6)-

    ((123,(45,((27,6)-)ln)*)+)sin

    This method is known as Reverse Polish Notation, or RPN, in honorof the original method. RPN, like Polish Notation, isunambiguous: the order and only the order of arguments andoperators will determine the result. Since this is the case, theparentheses and commas are irrelevant, and we may express ourfunction as:

    ((123,(45,((27,6)-)ln)*)+)sin > 123 45 27 6 - ln * + sin

    This allows a simple and straightforward solution to calculatoror software mathematics: treat both the functions and theirarguments alike as "objects," and process them in a last-in,first-out (LIFO) storage structure, a "stack."

    When a function-object is entered onto the stack, it operatesupon the argument-object(s) already there to produce a result,which replaces the argument-object(s). The stack adjustsaccordingly. This can be seen as:

    object stack level x: y: z: t: 123 123 45 45 123 27 27 45 123 6 6 27 45 123 - -(27,6) 45 123 ln ln(-(27,6)) 45 123 * *(45,ln(-(27,6))) 123 + +(123,*(45,ln(-(27,6)))) sin sin(+(123,*(45,ln(-(27,6)))))

    The secret behind RPN's sophisticated yet simple power is in thestack. In computer terms, a stack is an area in which pieces ofinformation may be stored on a last-in, first-out (LIFO) basis.

  • 8/7/2019 How to use Reverse-Polish Mathematical Notation

    4/4

    Think of the stack as a stack of plates: the last plate placedon the stack will be the first plate used.

    Most simple RPN calculators use a four-level stack, with thelevels labeled "x", "y", "z", and "t". Some newer, moresophisticated calculators (such as the HP-28S and the HP-48SX)and most software programs use an "infinite" stack: the number

    of stack levels is limited only by available memory. The levelsin an infinite stack are usually numerical.

    Entering a value will cause a stack lift: existing values willbe pushed up a level.

    Entering a function will consume the arguments of the function.If this consumption causes a "hole," the values above the holewill be dropped to fill the hole.

    Following through our example on an HP-48SX calculator:

    123 1: 123 2: 123

    45 1: 45 3: 123 2: 45

    27 1: 27

    4: 123 3: 45 2: 27

    6 1: 6 3: 123 2: 45

    - 1: 21 -(27,6) 3: 123 2: 45

    ln 1: 3.04452243772 ln(-(27,6)) 2: 123

    * 1: 137.003509697 *(45,ln(-(27,6)))

    + 1: 260.003509697 +(123,*(45,ln(-(27,6))))

    sin 1: .680672740775 sin(+(123,*(45,ln(-(27,6)))))