## GNU Astronomy Utilities

Next: , Previous: , Up: Arithmetic   [Contents][Index]

#### 6.2.1 Reverse polish notation

The most common notation for arithmetic operations is the infix notation where the operator goes between the two operands, for example $$4+5$$. While the infix notation is the preferred way in most programming languages, currently the Gnuastro’s program (in particular Arithmetic and Table, when doing column arithmetic) do not use it. This is because it will require parenthesis which can complicate the implementation of the code. In the near future we do plan to also allow this notation96, but for the time being (due to time constraints on the developers), arithmetic operations can only be done in the post-fix notation (also known as reverse polish notation). The Wikipedia article provides some excellent explanation on this notation but here we will give a short summary here for self-sufficiency.

In the post-fix notation, the operator is placed after the operands, as we will see below this removes the need to define parenthesis for most ordinary operators. For example, instead of writing 5+6, we write 5 6 +. To easily understand how this notation works, you can think of each operand as a node in a “last-in-first-out” stack. Every time an operator is confronted, the operator pops the number of operands it needs from the top of the stack (so they don’t exist in the stack any more), does its operation and pushes the result back on top of the stack. So if you want the average of 5 and 6, you would write: 5 6 + 2 /. The operations that are done are:

1. 5 is an operand, so it is pushed to the top of the stack (which is initially empty).
2. 6 is an operand, so it is pushed to the top of the stack.
3. + is a binary operator, so it will pop the top two elements of the stack out of it, and perform addition on them (the order is $$5+6$$ in the example above). The result is 11 which is pushed to the top of the stack.
4. 2 is an operand so push it onto the top of the stack.
5. / is a binary operator, so pull out the top two elements of the stack (top-most is 2, then 11) and divide the second one by the first.

In the Arithmetic program, the operands can be FITS images or numbers (see Invoking Arithmetic). In Table’s column arithmetic, they can be any column or a number (see Column arithmetic).

With this notation, very complicated procedures can be created without the need for parenthesis or worrying about precedence. Even functions which take an arbitrary number of arguments can be defined in this notation. This is a very powerful notation and is used in languages like Postscript 97 which produces PDF files when compiled.