Sponsored Links
-->

Kamis, 17 Mei 2018

Reverse Polish Notation (RPN) - YouTube
src: i.ytimg.com

Reverse Polish notation (RPN), also known as Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to Polish notation (PN), in which operators precede their operands. It does not need any parentheses as long as each operator has a fixed number of operands. The description "Polish" refers to the nationality of logician Jan ?ukasiewicz, who invented Polish notation in 1924.

The reverse Polish scheme was proposed in 1954 by Arthur Burks, Don Warren, and Jesse Wright and was independently reinvented by Friedrich L. Bauer and Edsger W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to evaluate expressions. The algorithms and notation for this scheme were extended by Australian philosopher and computer scientist Charles L. Hamblin in the mid-1950s.

During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators. In computer science, reverse Polish notation is used in stack-oriented programming languages such as Forth and PostScript.

Most of what follows is about binary operators. An example of a unary operator whose standard notation may be interpreted as reverse Polish notation is the factorial, "n!".


Video Reverse Polish notation



Explanation

In reverse Polish notation, the operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4. If there are multiple operations, operators are given immediately after their second operands; so the expression written 3 - 4 + 5 in conventional notation would be written 3 4 - 5 + in reverse Polish notation: 4 is first subtracted from 3, then 5 is added to it. An advantage of reverse Polish notation is that it removes the need for parentheses that are required by infix notation. While 3 - 4 × 5 can also be written 3 - (4 × 5), that means something quite different from (3 - 4) × 5. In reverse Polish notation, the former could be written 3 4 5 × -, which unambiguously means 3 (4 5 ×) - which reduces to 3 20 -; the latter could be written 3 4 - 5 × (or 5 3 4 - ×, if keeping similar formatting), which unambiguously means (3 4 -) 5 ×.


Maps Reverse Polish notation



Practical implications

In comparison testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to faster calculations, for two reasons. Because reverse Polish calculators do not need expressions to be parenthesized, fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators made fewer mistakes than for other types of calculator. Later research clarified that the increased speed from reverse Polish notation may be attributed to the smaller number of keystrokes needed to enter this notation, rather than to a smaller cognitive load on its users. However, anecdotal evidence suggests that reverse Polish notation is more difficult for users to learn than algebraic notation.


File:Reverse Polish Notation Stack Example.jpg - Wikimedia Commons
src: upload.wikimedia.org


Postfix evaluation algorithm

  • This algorithm evaluates postfix expressions using a stack, with the expression processed from left to right:
for each token in the postfix expression:    if token is an operator:      operand_2 <- pop from the stack      operand_1 <- pop from the stack      result <- evaluate token with operand_1 and operand_2      push result back onto the stack    else if token is an operand:      push token onto the stack  result <- pop from the stack  

This algorithm does the same but the expression is processed from right to left:

for each token in the reversed postfix expression:    if token is an operator:      push token onto the operator stack      pending_operand <- False    else if token is an operand:      operand <- token      if pending_operand is True:        while the operand stack is not empty:          operand_1 <- pop from the operand stack          operator <- pop from the operator stack          operand <- evaluate operator with operand_1 and operand      push operand onto the operand stack      pending_operand <- True  result <- pop from the operand stack  

Example

The infix expression ((15 ÷ (7 - (1 + 1))) × 3) - (2 + (1 + 1)) can be written like this in reverse Polish notation:

15 7 1 1 + - ÷ 3 × 2 1 1 + + -
  • Evaluating this postfix expression with the above left-to-right algorithm yields (red items are the stack contents, bold is the current token):
15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7     2 - ÷ 3 × 2 1 1 + + - =  15         5 ÷ 3 × 2 1 1 + + - =               3 3 × 2 1 1 + + - =               3 3 × 2 1 1 + + - =                   9 2 1 1 + + - =                   9 2 1 1 + + - =                   9 2 1 1 + + - =                   9 2 1 1 + + - =                   9 2     2 + - =                   9         4 - =                               5 =                               5  
  • Evaluating this postfix expression with the above right-to-left algorithm yields:
15 7 1 1 + - ÷ 3 × 2 1 1 + + - =  15 7 1 1 + - ÷ 3 × 2     2 + - =  15 7 1 1 + - ÷ 3 ×         4 - =  15 7     2 - ÷ 3 ×         4 - =  15         5 ÷ 3 ×         4 - =               3 3 ×         4 - =                   9         4 - =                               5  

The following table shows the state of the operand stack at each stage of the above left-to-right algorithm:

The above example could be rewritten by following the "chain calculation" method described by HP for their series of reverse Polish notation calculators:

As was demonstrated in the Algebraic mode, it is usually easier (fewer keystrokes) in working a problem like this to begin with the arithmetic operations inside the parentheses first.

1 2 + 4 × 5 + 3 -

Tree Traversal - Prefix (Polish) and Postfix (Reverse Polish ...
src: i.ytimg.com


Converting from infix notation

Edsger Dijkstra invented the shunting-yard algorithm to convert infix expressions to postfix expressions (reverse Polish notation), so named because its operation resembles that of a railroad shunting yard.

There are other ways of producing postfix expressions from infix expressions. 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.


Patrick Goh (@PatrickGohBS) | Twitter
src: pbs.twimg.com


Implementations

History of implementations

The first computers to implement architectures enabling reverse Polish notation 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 reverse Polish notation independently of Hamblin sometime in 1958 after reading a 1954 textbook on symbolic logic by Irving Copi, where he found a reference to Polish notation, which made him read the works of Jan ?ukasiewicz as well, and before he was aware of Hamblin's work. Designed by Robert "Bob" Appleby Ragen, Friden introduced reverse Polish notation to the desktop calculator market with the EC-130 supporting a four-level stack in June 1963. The successor EC-132 added a square root function in April 1965. Around 1966, the Monroe Epic calculator supported an unnamed input scheme resembling RPN as well.

Hewlett-Packard

Hewlett-Packard engineers designed the 9100A Desktop Calculator in 1968 with reverse Polish notation with only three stack levels, a reverse Polish notation variant later referred to as three-level RPN. This calculator popularized reverse Polish notation among the scientific and engineering communities. The HP-35, the world's first handheld scientific calculator, introduced the classical four-level RPN in 1972. HP used reverse Polish notation on every handheld calculator it sold, whether scientific, financial, or programmable, until it introduced the HP-10 adding machine calculator in 1977. By this time, HP was the leading manufacturer of calculators for professionals, including engineers and accountants.

Later LCD-based calculators in the early 1980s such as the HP-10C, HP-11C, HP-15C, HP-16C, and the financial calculator, the HP-12C also used reverse Polish notation. In 1988, Hewlett-Packard introduced a business calculator, the HP-19B, without reverse Polish notation, but its 1990 successor, the HP-19BII, gave users the option of using algebraic notation or reverse Polish notation.

Around 1987, HP introduced RPL, an object-oriented successor to reverse Polish notation. It deviates from classical reverse Polish notation by utilizing a stack only limited by the amount of available memory (instead of three or four fixed levels) and which can hold all kinds of data objects (including symbols, strings, lists, matrices, graphics, programs, etc.) instead of just numbers. It also changed the behaviour of the stack to no longer duplicate the top register on drops (since in an unlimited stack there is no longer a top register) and the behaviour of the Enter key so that it no longer duplicates values into Y under certain conditions, both part of the specific ruleset of the so-called automatic memory stack or operational (memory) stack in classical reverse Polish notation in order to ease some calculations and to save keystrokes, but which had shown to also sometimes cause confusion among users not familiar with these properties. From 1990 to 2003 HP manufactured the HP-48 series of graphing RPL calculators and in 2006 introduced the HP 50g.

As of 2011, Hewlett-Packard was offering the calculator models 12C, 12C Platinum, 17bII+, 20b, 30b, 33s, 35s, 48gII (RPL) and 50g (RPL) which support reverse Polish notation. While calculators emulating classical models continue to support classical reverse Polish notation, new reverse Polish notation models feature a variant of reverse Polish notation, where the Enter key behaves as in RPL. This latter variant is sometimes known as entry RPN. In 2013, the HP Prime introduced a 128-level form of entry RPN called advanced RPN. By late 2017, only the 12C, 12C Platinum, 17bii+, 35s and Prime remain active HP models supporting reverse Polish notation.

WP 31S and WP 34S

The community-developed calculators WP 31S and WP 34S, which are based on the HP 20b/HP 30b hardware platform, support Hewlett-Packard-style classical reverse Polish notation with either a four- or an eight-level stack. A seven-level stack had been implemented in the MITS 7400C scientific desktop calculator in 1972 and an eight-level stack was already suggested by John A. Ball in 1978.

Sinclair Radionics

In Britain, Clive Sinclair's Sinclair Scientific and Scientific Programmable models used reverse Polish notation.

Commodore

In 1974 Commodore produced the Minuteman *6 (MM6) without Enter key and the Minuteman *6X (MM6X) with Enter key, both implementing a form of two-level RPN. The SR4921 RPN came with a variant of four-level RPN with stack levels named X, Y, Z, and W (rather than T). In contrast to Hewlett-Packard's reverse Polish notation implementation, W filled with 0 instead of its contents being duplicated on stack drops.

Prinztronic

Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores retail chain, which was later rebranded as Currys Digital stores, and became part of DSG International. A variety of calculator models was sold in the 1970s under the Prinztronic brand, all made for them by other companies.

Among these was the PROGRAM Programmable Scientific Calculator which featured reverse Polish notation.

Heathkit

The Aircraft Navigation Computer Heathkit OC-1401/OCW-1401 used five-level RPN in 1978.

Soviet Union

Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21 models) used reverse Polish notation for both automatic mode and programming. Modern Russian calculators MK-161 and MK-152, designed and manufactured in Novosibirsk since 2007 and offered by Semico, are backward compatible with them. Their extended architecture is also based on reverse Polish notation.

Other implementations

Existing implementations using reverse Polish notation include:

  • Any stack-oriented programming language, such as:
    • Forth
    • Factor
    • PostScript page description language
    • BibTeX
    • Befunge
    • Joy
    • IPTSCRAE
    • Lotus 1-2-3 and Lotus Symphony formulas
    • RPL (aka Reverse Polish Language), a programming language for the Commodore PET around 1979/1981
    • RPL (aka Reverse Polish Lisp), a programming language for Hewlett-Packard calculators between 1984 and 2015
    • RPNL (Reverse Polish Notation Language)
  • Hardware calculators:
    • Some Hewlett-Packard science/engineering and business/finance calculators
    • Semico calculators
    • SwissMicros calculators
  • Software calculators:
    • Mac OS X Calculator
    • Several Apple iPhone applications e.g. "reverse polish notation calculator"
    • Several Android applications e.g. "RealCalc"
    • Several Windows 10 Mobile applications e.g. "RPN9"
    • Unix system calculator program dc
    • Emacs lisp library package calc
    • Xorg calculator (xcalc)
    • grpn scientific/engineering calculator using the GIMP Toolkit (GTK+)
    • F-Correlatives in MultiValue dictionary items
    • RRDtool, a widely used tabulating and graphing software
    • grdmath, a program for algebraic operations on NetCDF grids, part of Generic Mapping Tools (GMT) suite
    • galculator, a GTK desktop calculator
    • Mouseless Stack-Calculator scientific/engineering calculator including complex numbers.

How to use Reverse Polish Notation (RPN) - YouTube
src: i.ytimg.com


See also

  • Calculator input methods
  • Factor (programming language)
  • FOCAL keystroke programming
  • Formula calculator
  • Forth (programming language)
  • Joy (programming language)
  • LIFO (computing)
  • Object-subject-verb
  • Polish notation
  • PostScript
  • Reverse Polish Lisp (RPL)
  • Stack machine
  • Subject-object-verb (SOV)

How to use a Reverse Polish Notation Calculator - YouTube
src: i.ytimg.com


References


Befunge Reverse Polish Notation Calculator - YouTube
src: i.ytimg.com


Further reading

  • Wirth, Niklaus (2005-06-15) [2005-02-02]. "Good Ideas, Through the Looking Glass" (PDF). Zürich, Switzerland. Archived (PDF) from the original on 2017-06-24. Retrieved 2015-09-12. 
  • "Everything you've always wanted to know about RPN but were afraid to pursue - Comprehensive manual for scientific calculators - Corvus 500 - APF Mark 55 - OMRON 12-SR and others" (PDF). T. K. Enterprises. 1976. Archived (PDF) from the original on 2017-06-24. Retrieved 2017-06-24.  (NB. The book's cover title contains a typographical error reading "APS Mark 55" instead of the correct "APF Mark 55".)

I love reverse polish notation
src: ih0.redbubble.net


External links

  • Brown, Bob (2015-06-05) [2001]. "Postfix Notation Mini-Lecture". Information Technology Department, College of Computing and Software Engineering, Kennesaw State University. Archived from the original on 2017-06-24. Retrieved 2015-09-12. 
  • Redin, James (2005-02-12) [1997]. "RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic". Archived from the original on 2017-06-24. Retrieved 2015-09-12. 
  • Hicks, David G. (2013) [1995]. "What is RPN?". The Museum of HP Calculators (MoHPC). Archived from the original on 2017-06-24. Retrieved 2015-09-12. 
  • Klaver, Hans (2014). "RPN Tutorial, incl. some things HP did not tell". Archived from the original on 2017-06-24. Retrieved 2015-09-12. 
  • Rosettacode.org providing many implementations in several programming languages.
  • http://rpn.codeplex.com/ Implementation of RPN with custom functions support and flexible list of operators.
  • https://xrjunque.nom.es/ConvertAlg2RPN_RPL.aspx Free online Algebraic expression to RPN Converter

Source of article : Wikipedia