Operations in Reverse Polish Notation

In summary, the conversation explores the use of Reverse Polish Notation (RPN) for complex mathematical functions such as limits and integrals. While RPN is typically used for unary and binary functions, it can also be extended to n-ary functions by specifying an order for entering the values. The lack of a universal standard for RPN can make it difficult to implement more complex functions, but systems like HP's RPL programming language have attempted to standardize the notation.
  • #1
The Javaman
3
0
Recently I was doing various equations in RPN to teach myself the notation. Everything went fine until I wanted to do a limit. How would one go about expressing limits (and any other operation that isn't addition, subtraction, division, or multiplication) in RPN?
 
Mathematics news on Phys.org
  • #2
Limits and integrals are complex functions that require several values and/or variables to be entered at one time. They don't really have a specific, defined "order of operations" (i.e., how would you enter a limit in a calculator; standard or RPN?)

RPN only really lends itself to 1- or 2-value functions:
X [enter]
Y [enter]
[function]

or

X [enter]
[function]

where [function] might be addition, subtraction, multiplication, x^y, e^x, 10^x, SIN, COS, TAN, 1/x, etc.
 
  • #3
In the usual way. We write addition "a b +" where a and b are numbers because + is an operation that takes two arguments. If you have a function f(x) and you want to evaluate
[tex]\lim_{x \to a} f(x)[/tex]
then you can consider a limit as an operation that takes two arguments: first a function, then a number that specifies where you want to evaluate the limit so it might look something like,
f(x) a lim
If we call the operation lim. Or for a more concrete example where x is a built-in variable and sin is a one-argument operation returning the sine.
x sin x / 0 lim
which would evaluate
[tex]\lim_{x \to 0} \frac{\sin(x)}{x}[/tex]
Of course if you have some specific RPN system in mind, then there might be a specific way to evaluate limits, but RPN in general is just a way to write evaluation of functions so there are many options as to how it's designed.
 
  • #4
zgozvrm said:
RPN only really lends itself to 1- or 2-value functions:
X [enter]
Y [enter]
[function]

This is false. RPN lends itself perfectly fine to n-ary operations. For instance for a 3-ary function:
X [enter]
Y [enter]
Z [enter]
[function]
For a 4-ary function:
X [enter]
Y [enter]
Z [enter]
W [enter]
[function]
It's the ordinary infix notation that doesn't work well with more than 2 arguments.
 
  • #5
Interesting. I never thought about using multiple arguments, but it does make a great deal of sense. I actually have no system in mind, I was just curious.
 
  • #6
It DOES make some sense, but I haven't seen it done.

When I think about it, I'm not really sure you can still call it RPN; any definition I've seen refers to unary and binary functions.

I think the problem with ternary (and larger functions) is that there is no standard defined order of entering the values. Clearly, the order would be important though.
 
  • #7
So there is no universal order of operations in RPN? That's disappointing, but I suppose one could define their own system. It is safe to say that any operation in RPN can be represented as a stack of it's arguments followed by the symbol itself though, isn't it?
 
Last edited:
  • #8
It's not that there's no "universal order" in RPN, it's more that complicated functions with many variables, don't have a universal order in which the values should be entered. Therefore, it would be difficult to implement in RPN (or any other system) with universal agreement.

You are correct in that it can be done though.
 
  • #9
The Javaman said:
So there is no universal order of operations in RPN?

Even some binary functions require specific order of arguments - so some basic ordering of arguments already is there. But it is not generalized to functions with more variables.
 
  • #10
zgozvrm said:
Therefore, it would be difficult to implement in RPN (or any other system) with universal agreement.
Luckily you don't need universal agreement, just a specification. For instance on HP's calculator's they usually use x to represent the top-most argument on the stack, and y the next so.
5 3 [itex]y^x[/itex]
would evaluate [itex]5^3[/itex], but there is nothing wrong in extending this notation to include z for the next argument. Actually while most users doing simple computation won't need functions taking 3 arguments they do exist on modern HP calculators. For instance there is a command called IFTE which is short for "If-then-else". The first argument is a truth-value (1 or 0), the second is what to return if the first argument is 1 and the last is what to return if the first argument is 0. Using this we could for instance evaluate the sign of the top-most value on the stack as follows :
DUP 0 == 0 DUP ABS / IFTE
which basically works as follows:
if(x == 0) then: 0
else: x/|x|
There is nothing confusing about this as long as we standardize the order of the arguments just as we have with -. In normal infix notation there is no danger of confusing x-y and y-x or
[tex]\int_a^b f(x) \,\textrm{d}x \quad \int_b^a f(x) \,\textrm{d}x \quad \int_a^{f(x)} b \,\textrm{d}x[/tex]
They are clearly different. If you standardize INTEGRATE, then there is no way you would confuse
A B F INTEGRATE
with
A F B INTEGRATE
 
  • #11
Agreed, but to my knowledge there hasn't been any standardization for such expressions.

Besides, you could just as easily say "take the integral of function F from a to b" as "take the integral from a to b of function F"
To us, they mean the same thing, but a calculator, needs to have strict rules. Once there is standardization in the order in which the variables are entered, then no, there is no confusion, but unless and until that happens, it is not necessarily clear.

In addition, your example shows a very simple function F, what if it was more complicated?
 
  • #12
zgozvrm said:
Agreed, but to my knowledge there hasn't been any standardization for such expressions.
HP developed the RPL (Reverse-Polish LISP) programming language for their calculators which standardize the functions I mentioned earlier. It also includes syntax for actually working with complex functions and even defining your own. You're right that there is no universal standard though, just as 5 3 ^ could mean both 5^3 or 3^5 since there is no universally agreed upon way of ordering it.

Besides, you could just as easily say "take the integral of function F from a to b" as "take the integral from a to b of function F"
To us, they mean the same thing, but a calculator, needs to have strict rules. Once there is standardization in the order in which the variables are entered, then no, there is no confusion, but unless and until that happens, it is not necessarily clear.
I completely agree, but keep in mind that many calculator producers have taken the trouble to standardize the order. Due to the fact that when writing definite integrals ([itex]\int_a^b f(x)dx[/itex]) we write the arguments in the order a b f so the natural candidate is A B F INTEGRATE, but F A B INTEGRATE is also imaginable even though I have never seen that order. Keep in mind however that this problem is in no way exclusive to RPN calculators/languages. Most calculators and programming languages don't have an integral sign as such either so you end up writing INTEGRATE(a,b,f) which also relies on some ordering.
 

What is Reverse Polish Notation (RPN)?

Reverse Polish Notation (RPN) is a mathematical notation in which operators are placed after the operands rather than between them. This allows for a more efficient way of evaluating mathematical expressions compared to traditional infix notation.

What are the advantages of using RPN for operations?

One of the main advantages of RPN is that it eliminates the need for parentheses and operator precedence rules, making expressions easier to read and evaluate. RPN also requires less memory and processing power, which can be beneficial for complex calculations.

How do you perform operations in RPN?

To perform operations in RPN, you first enter the operands followed by the operator. For example, to add 2 and 3, you would enter "2 3 +" in RPN. The result will be 5. If there are multiple operations, they are performed in the order they are entered.

What happens if there are too many or too few operands in RPN?

If there are too many operands, the calculator will display an error message. If there are too few operands, the calculator will also display an error message and may prompt for more input. It is important to enter the correct number of operands for each operation.

Are there any mathematical operations that cannot be performed in RPN?

RPN can perform all of the same mathematical operations as traditional infix notation, including addition, subtraction, multiplication, division, and exponentiation. However, some complex mathematical operations may be more difficult to perform in RPN and may require multiple steps.

Similar threads

Replies
1
Views
865
Replies
2
Views
951
Replies
3
Views
239
  • General Math
Replies
7
Views
2K
Replies
7
Views
2K
  • General Math
Replies
2
Views
950
Replies
3
Views
1K
Replies
5
Views
2K
Back
Top