Programming Gri
1: Introduction 2: Simple example 3: Fancy example 4: Running Gri 5: Programming Gri 6: General Issues 7: X-Y Plots 8: Contour Plots 9: Image Plots 10: Examples 11: Handling Data 12: Gri Commands 13: Gri Extras 14: Evolution of Gri 15: Installing Gri 16: Gri Bugs 17: System Tools 18: Acknowledgments 19: License 20: Newsgroup 21: Concept Index |
5.8: Built-in rpn CalculatorGri can do simple mathematics on numbers. The syntax is reverse-polish notation (`rpn '), which is used in some calculators. Most users
can learn rpn in a few minutes, so don't worry if you don't know RPN
yet.
syntax rpn expressions can be used anywhere Gri expects a number.
RPN expressions start with a opening curly brace (`{ ') which is
immediately followed by the word `rpn '. rpn expressions end with a
closing curly brace (`} '). Instead of `set x size 10 ' you
could write `set x size {rpn 20 2 /} ', where the expression
`{rpn 20 2 /} ' tells Gri to insert the number 20 onto a stack,
then insert the number 2 above it on the stack, and then divide the top
two items on the stack. The following are equivalent:
5.8.1: Stack OperatorsStack operators manipulate or display the stack. `pop ' removes the
top item from the stack, `dup ' duplicates the top item on the
stack, `exch ' reorders the top two items on the stack,
`pstack ' prints the items on the stack (without changing the
stack), `roll_right ' rolls the items to the right and
`roll_left ' rolls the items to the left, e.g.
5.8.2: Rpn function Operators`rpnfunction ' operators are user-defined operators. The parser
replaces any such operator with the user-defined rpn expression. The
`rpnfunction ' operators are both general and powerful. An
`rpnfunction ' may be composed of any legal primitive rpn constructs
or even other legal `rpnfunction ' constructs. For details,
see Rpnfunction.
5.8.3: rpn constantsGri knows the values `pi ' = 3.141...,
`e ' = 2.718.... These are defined near the end of the startup
file `gri.cmd' as `rpnfunction ' commands see Rpnfunction.
To add more builtin constants, follow the same `rpnfunction '
procedure.
5.8.4: Testing for Existence of Files, Variables, and SynonymThe `defined ' operator checks whether synonyms
or variables are defined, e.g.
5.8.5: Binary OperatorsBinary operators act on the top two items on the stack. With most binary operators, the result is to replace these two items with a single new item. For example, `{rpn 1 2 /} ' yields 0.5.
Some binary operators replace the top two items with two new items.
Examples are the pairwise conversion operators `xyusertocm ' and
`xycmtouser '.
List of binary operators:
`+ ',
`- ',
`* ',
`/ ',
`< ' (e.g., `1 2 < ' yields 0 since 2 is not less than 1),
`<= ',
`> ', (e.g., `0 10 > ' yields 1 since 10 is greater than 0),
`>= ',
`== ',
`& ',
`| ',
`power ' (e.g., `2 3 power ' yields 8),
`exch ' (exchange two items on top of stack),
`strcat ' (add top string to end of string before it, then delete top string),
`= ' (assign string value to synonym or numerical value to variable; see example below),
`sup ' picks the larger of two values on stack,
`inf ' picks the smaller of two values on the stack,
`remainder ' yields the remainder from division of two values on the
stack, using the C-language subroutine `fmod ', which is similar to
`mod ' in fortran. See the manuals on these functions, and the
examples below, for details on sign of the result compared to the two
input numbers.
Notes:
5.8.6: Unary OperatorsFirst, an operation that does not require even a single item on the stack: random number generation. Random numbers are given by `rand '.
Unary operators replace the last item on the stack with another item.
For example, the `sin ' operator takes the sine of the number on the
top of the stack; e.g., `{rpn 45 sin} ' yields sin of 45 degrees.
List of unary operators:
`ismissing ' (=1 if value is current missing value, 0 otherwise),
`abs ',
`atof ' (converts string to number),
`! ',
`acosh ',
`asinh ',
`atanh ',
`acos ',
`asin ',
`atan ',
`sin ',
`cos ',
`tan ',
`cosh ',
`sinh ',
`tanh ',
`sqrt ',
`system ' (replaces string with output when that string is fed to the operating system),
`log ',
`ln ',
`exp ',
`exp10 ',
`ceil ',
`floor ',
`cmtopt ',
`pttocm ',
`xusertocm ',
`yusertocm ',
`xcmtouser ',
`ycmtouser ',
`width ' (e.g., `"hi" width ' yields the width of this string in cm),
`ascent ' (e.g., `"hi" ascent ' yields the ascent above the baseline in cm),
`descent ' (e.g.,m `"hi" descent ' yields the descent below the baseline in cm),
`pop ' (removes top item from stack),
`dup ' (duplicates top item on stack).
5.8.7: Manipulation of Columns etc5.8.7.1: ColumnsIndividual data in the `x ', `y ', `z ', `u ', `v '
and `weight ' columns can be accessed with the `@ ' operator.
The first point has index 0. Examples:
mean ' operator (e.g.,
`.xmean. = {rpn x mean } ', while the standard deviation is given
by `stddev ' and the minimal and maximal values are given by
`min ' and `max '.
The area under the curve y=y(x) is found by `{rpn y x area } ',
defined by
`0.5 * sum ( (y[i] + y[i-1]) * (x[i] - x[i-1]) ) '
for `i ' ranging from 1 to `..num_col_data.. '-1.
5.8.7.2: GridGrid data can be accessed with e.g. `{rpn grid min } ',
`{rpn grid max } ', and `{rpn grid mean } '.
The value of the grid at a given `(.x.,.y.) ' coordinate may be
found by by e.g. `{rpn grid .x. .y. interpolate} '. The
interpolation scheme is the same as that used in converting grids to
images.
5.8.8: rpn ExamplesHere are some reverse-polish expressions and the corresponding algebraic interpretations:
|