Operations in Reverse Polish Notation

1. Oct 24, 2009

The Javaman

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?

2. Oct 24, 2009

zgozvrm

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. Oct 24, 2009

rasmhop

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
$$\lim_{x \to a} f(x)$$
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
$$\lim_{x \to 0} \frac{\sin(x)}{x}$$
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. Oct 24, 2009

rasmhop

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. Oct 24, 2009

The Javaman

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. Oct 24, 2009

zgozvrm

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. Oct 24, 2009

The Javaman

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: Oct 24, 2009
8. Oct 24, 2009

zgozvrm

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. Oct 25, 2009

Staff: Mentor

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. Oct 25, 2009

rasmhop

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 $y^x$
would evaluate $5^3$, 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
$$\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$$
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. Oct 25, 2009

zgozvrm

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. Oct 25, 2009

rasmhop

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.

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 ($\int_a^b f(x)dx$) 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.