DiemazzMichel López NúñezTennis statistics Egyptian chronology Template:Brownwood Radio Jacques Revaux flash player problems such firefox Portal:Bible Fujishiro Station Category:Central owned enterprise of the People's Republic of China Herbert Blumer Blaze Template:Dated adoptme 741 datasheet Hong Kong films of 1983 Category:Supermarket tabloids panasonic dvd video recorder Neva River Capital Cities Communications Maisons en Champagne Garpieni online think tank while traveling north Vágar Category:Excelsior Rotterdam players Nick Lander Matsubase, Kumamoto NengÅ Category:760 births Affirmative defense Thai Airways International Adria Airways Monsters (TV series) Salem Township, Meigs County, Ohio Central Park Cierp Gaud List of tallest buildings in Des Moines, Iowa Tambon Pierre Carmouche Naha (train) Jonan ku, Fukuoka oklahoma real estate moein Independence Movement (Lebanon) Asahi Shimbun List of Royal Navy aircraft wings Pognamadéni Exaile National University of Singapore 1558 in music Communes of the Isère department Peter Lai WIAA |
Reverse Polish notation (or just RPN) by analogy with the related Polish notation, a prefix notation introduced in 1920 by the Polish mathematician Jan Łukasiewicz, is a mathematical notation wherein every operator follows all of its operands. It is also known as Postfix notation and is parenthesis-free. The Reverse Polish scheme was proposed by F. L. Bauer and E. W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to evaluate expressions. The notation and algorithms for this scheme were enriched by Australian philosopher and computer scientist Charles Hamblin in the mid-1960s.[1][2] During the 1960s and 1970s, RPN had some currency even among the general public, as it was widely used in desktop calculators of the time. Most of what follows is about binary operators. A unary operator for which the Reverse Polish notation is the general convention is the factorial.
ExplanationIn Reverse Polish notation the operators follow their operands; for instance, to add three and four, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand; so the expression written "3 − 4 + 5" in conventional infix notation would be written "3 4 − 5 +" in RPN: first subtract 4 from 3, then add 5 to that. An advantage of RPN is that it obviates the need for parentheses that are required by infix. While "3 − 4 * 5" can also be written "3 − (4 * 5)", that means something quite different from "(3 − 4) * 5". In postfix, the former would be written "3 4 5 * −", which unambiguously means "3 (4 5 *) −". Interpreters of Reverse Polish notation are often stack-based; that is, operands are pushed onto a stack, and when an operation is performed, its operands are popped from a stack and its result pushed back on. Stacks, and therefore RPN, have the advantage of being easy to implement and very fast. Note that, despite the name, reverse Polish notation is not exactly the reverse of Polish notation, as the operands of non-commutative operations are still written in the conventional order (e.g. "/ 6 3" in Polish notation corresponds to "6 3 /" in reverse Polish, both evaluating to 2, whereas "3 6 /" would evaluate to 0.5). Numbers are also written with the digits in the conventional order. Practical implications
The postfix algorithmThe algorithm for evaluating any postfix expression is fairly straightforward:
ExampleThe infix expression "5 + ((1 + 2) * 4) − 3" can be written down like this in RPN:
The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the list of values the algorithm is "keeping track of" after the Operation given in the middle column has taken place):
When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14. The above example could be rewritten by following the "chain calculation" method described by HP for their series of RPN calculators:
Converting from infix notationEdsger Dijkstra invented the "shunting yard" algorithm to convert infix expressions to postfix (RPN), so named because its operation resembles that of a railroad shunting yard. There are other ways of producing postfix expressions from infix notation. Most Operator-precedence parsers can be modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the corresponding postfix expression is given by a simple post-order traversal of that tree. ImplementationsThe first computers to implement architectures enabling RPN were the English Electric Company's KDF9 machine, which was announced in 1960 and delivered (i.e. made available commercially) in 1963, and the American Burroughs B5000, announced in 1961 and also delivered in 1963. One of the designers of the B5000, Robert S. Barton, later wrote that he developed RPN independently of Hamblin, sometime in 1958 while reading a textbook on symbolic logic, and before he was aware of Hamblin's work. Friden introduced RPN to the desktop calculator market with the EC-130 in June 1963. Hewlett-Packard (HP) engineers designed the 9100A Desktop Calculator in 1968 with RPN. This calculator popularized RPN among the scientific and engineering communities, even though early advertisements for the 9100A failed to mention RPN. The HP-35, the world's first handheld scientific calculator, used RPN in 1972. HP used RPN on every handheld calculator it sold, whether scientific, financial, or programmable, until it introduced an adding machine-style calculator, the HP-10A. HP introduced an LCD display line of calculators in the early 1980s that used RPN, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the famous financial calculator, the HP-12C. When Hewlett-Packard introduced a later business calculator, the HP-19B, without RPN, feedback from financiers and others used to the 12-C compelled them to release the HP-19BII, which gave users the option of using algebraic notation or RPN. Existing implementations using Reverse Polish notation include:
A Postfix evaluator implemented in Python# Valid elements of input are binary operators, integers, # and floating point numbers, separated by spaces. # e.g. 100 2 / 2 ** 3.14 * stack = [] for i in raw_input('Input: ').split(): if i[-1].isdigit(): stack.append(float(i)) else: if len(stack) < 2: quit('Error: Too few input values') else: num2 = stack.pop() num1 = stack.pop() stack.append(eval(str(num1) + i + str(num2))) if len(stack) > 1: quit('Error: Too many input values') else: print 'Answer: %d' % stack[0] Notes
See alsoExternal links
|
Site Map: RSS 2.0
Recent Searches:
Reverse Polish notation
Related Pages:
|