rrdgraph_rpn - About RPN Math in rrdtool graph
RPN expression:=vname|operator|value[,RPN expression]
If you have ever used a traditional HP calculator you already know RPN. The idea behind RPN is that you have a stack and push your data onto this stack. Whenever you execute an operation, it takes as many elements from the stack as needed. Pushing is done implicitly, so whenever you specify a number or a variable, it gets pushed onto the stack automatically.
At the end of the calculation there should be one and only one value left on the stack. This is the outcome of the function and this is what is put into the vname. For CDEF instructions, the stack is processed for each data point on the graph. VDEF instructions work on an entire data set in one run. Note, that currently VDEF instructions only support a limited list of functions.
Example: VDEF:maximum=mydata,MAXIMUM
This will set variable ``maximum'' which you now can use in the rest of your RRD script.
Example: CDEF:mydatabits=mydata,8,*
This means: push variable mydata, push the number 8, execute
the operator *. The operator needs two elements and uses those
to return one value. This value is then stored in mydatabits.
As you may have guessed, this instruction means nothing more than
mydatabits = mydata * 8. The real power of RPN lies in the
fact that it is always clear in which order to process the input.
For expressions like a = b + 3 * 5 you need to multiply 3 with
5 first before you add b to get a. However, with parentheses
you could change this order: a = (b + 3) * 5. In RPN, you
would do a = b, 3, +, 5, * without the need for parentheses.
Pop two elements from the stack, compare them for the selected condition and return 1 for true or 0 for false. Comparing an unknown or an infinite value will always result in 0 (false).
UN, ISINF
Pop one element from the stack, compare this to unknown respectively to positive or negative infinity. Returns 1 for true or 0 for false.
IF
Pops three elements from the stack. If the element popped last is 0 (false), the value popped first is pushed back onto the stack, otherwise the value popped second is pushed back. This does, indeed, mean that any value other than 0 is considered to be true.
Example: A,B,C,IF should be read as if (A) then (B) else (C)
Pops two elements from the stack and returns the smaller or larger, respectively. Note that infinite is larger than anything else. If one of the input numbers is unknown then the result of the operation will be unknown too.
LIMIT
Pops two elements from the stack and uses them to define a range. Then it pops another element and if it falls inside the range, it is pushed back. If not, an unknown is pushed.
The range defined includes the two boundaries (so: a number equal to one of the boundaries will be pushed back). If any of the three numbers involved is either unknown or infinite this function will always return an unknown
Example: CDEF:a=alpha,0,100,LIMIT will return unknown if
alpha is lower than 0 or if it is higher than 100.
Add, subtract, multiply, divide, modulo
SIN, COS, LOG, EXP, SQRT
Sine and cosine (input in radians), log and exp (natural logarithm), square root.
ATAN
Arctangent (output in radians).
ATAN2
Arctangent of y,x components (output in radians). This pops one element from the stack, the x (cosine) component, and then a second, which is the y (sine) component. It then pushes the arctangent of their ratio, resolving the ambiguity between quadrants.
Example: CDEF:angle=Y,X,ATAN2,RAD2DEG will convert X,Y
components into an angle in degrees.
FLOOR, CEIL
Round down or up to the nearest integer.
DEG2RAD, RAD2DEG
Convert angle in degrees to radians, or radians to degrees.
Pop one element from the stack. This is the count of items to be sorted (or reversed). The top count of the remaining elements are then sorted (or reversed) in place on the stack.
Example: CDEF:x=v1,v2,v3,v4,v5,v6,6,SORT,POP,5,REV,POP,+,+,+,4,/ will
compute the average of the values v1 to v6 after removing the smallest and
largest.
AVG
Pop one element (count) from the stack. Now pop count elements and build the average, ignoring all UNKNOWN values in the process.
Example: CDEF:x=a,b,c,d,4,AVG
TREND
Create a ``sliding window'' average of another data series.
Usage: CDEF:smoothed=x,1800,TREND
This will create a half-hour (1800 second) sliding window average of x. The average is essentially computed as shown here:
+---!---!---!---!---!---!---!---!--->
now
delay t0
<--------------->
delay t1
<--------------->
delay t2 del2,IF'' (``if 0 then 1 else 2) evaluates to 2.
Notice that none of the above examples really simulate the whole
``if X then Y else Z'' statement. This is because computer programmers
read this statement as ``if Some Condition then Y else Z''. So it's
important to be able to read IF operators along with the LT, LE,
GT, GE and EQ operators.
Some Examples
While compound expressions can look overly complex, they can be
considered elegantly simple. To quickly comprehend RPN expressions,
you must know the the algorithm for evaluating RPN expressions:
iterate searches from the left to the right looking for an operator.
When it's found, apply that operator by popping the operator and some
number of values (and by definition, not operators) off the stack.
For example, the stack ``1,2,3,+,+'' gets ``2,3,+'' evaluated (as ``2+3'')
during the first iteration and is replaced by 5. This results in
the stack ``1,5,+''. Finally, ``1,5,+'' is evaluated resulting in the
answer 6. For convenience, it's useful to write this set of
operations as:
1) 1,2,3,+,+ eval is 2,3,+ = 5 result is 1,5,+
2) 1,5,+ eval is 1,5,+ = 6 result is 6
3) 6
Let's use that notation to conveniently solve some complex RPN expressions
with multiple logic operators:
1) 20,10,GT,10,20,IF eval is 20,10,GT = 1 result is 1,10,20,IF
read the eval as pop ``20 is greater than 10'' so push 1
2) 1,10,20,IF eval is 1,10,20,IF = 10 result is 10
read pop ``if 1 then 10 else 20'' so push 10. Only 10 is left so
10 is the answer.
Let's read a complex RPN expression that also has the traditional
multiplication operator:
1) 128,8,*,7000,GT,7000,128,8,*,IF eval 128,8,* result is 1024
2) 1024,7000,GT,7000,128,8,*,IF eval 1024,7000,GT result is 0
3) 0,128,8,*,IF eval 128,8,* result is 1024
4) 0,7000,1024,IF result is 1024
Now let's go back to the first example of multiple logic operators,
but replace the value 20 with the variable ``input'':
1) input,10,GT,10,input,IF eval is input,10,GT ( lets call this A )
Read eval as ``if input > 10 then true'' and replace ``input,10,GT''
with ``A'':
2) A,10,input,IF eval is A,10,input,IF
read ``if A then 10 else input''. Now replace A with it's verbose
description againg and--voila!--you have a easily readable description
of the expression:
if input > 10 then 10 else input
Finally, let's go back to the first most complex example and replace
the value 128 with ``input'':
1) input,8,*,7000,GT,7000,input,8,*,IF eval input,8,* result is A
where A is ``input * 8''
2) A,7000,GT,7000,input,8,*,IF eval is A,7000,GT result is B
where B is ``if ((input * 8) > 7000) then true''
3) B,7000,input,8,*,IF eval is input,8,* result is C
where C is ``input * 8''
4) B,7000,C,IF
At last we have a readable decoding of the complex RPN expression with
a variable:
if ((input * 8) > 7000) then 7000 else (input * 8)
Exercises
Exercise 1:
Compute ``3,2,*,1,+ and ''3,2,1,+,*`` by hand. Rewrite them in
traditional notation. Explain why they have different answers.
Answer 1:
3*2+1 = 7 and 3*(2+1) = 9. These expressions have
different answers because the altering of the plus and
times operators alter the order of their evaluation.
Exercise 2:
One may be tempted to shorten the expression
input,8,*,56000,GT,56000,input,*,8,IF
by removing the redundant use of ``input,8,*'' like so:
input,56000,GT,56000,input,IF,8,*
Use traditional notation to show these expressions are not the same.
Write an expression that's equivalent to the first expression, but
uses the LE and DIV operators.
Answer 2:
if (input <= 56000/8 ) { input*8 } else { 56000 }
input,56000,8,DIV,LT,input,8,*,56000,IF
Exercise 3:
Briefly explain why traditional mathematic notation requires the
use of parentheses. Explain why RPN notation does not require
the use of parentheses.
Answer 3:
Traditional mathematic expressions are evaluated by
doing multiplication and division first, then addition and
subtraction. Parentheses are used to force the evaluation of
addition before multiplication (etc). RPN does not require
parentheses because the ordering of objects on the stack
can force the evaluation of addition before multiplication.
Exercise 4:
Explain why it was desirable for the RRDtool developers to implement
RPN notation instead of traditional mathematical notation.
Answer 4:
The algorithm that implements traditional mathematical
notation is more complex then algorithm used for RPN.
So implementing RPN allowed Tobias Oetiker to write less
code! (The code is also less complex and therefore less
likely to have bugs.)
AUTHOR
Steve Rader <rader@wiscnet.net>