Some of my Avisynth filters take RPN strings as inputs to perform calculations. For a general view of Reverse Polish Notation, see the Wikipedia page.
Documentation for my specific RPN implementation:
Arithmetic operations | ||||
---|---|---|---|---|
Operation | Op | Example | Description | Result |
Add | + | x y + | Add the top two operands on the stack | x + y |
Subtract | - | x y - | Subtract y from x | x - y |
Multiply | * | x y * | Multiply x by y | x * y |
Divide | * | x y / | Divide x by y | x / y |
Reverse divide | * | x y \ | Divide y by x | y / x |
Power | ^ | x y ^ | Raise x to the power of y. This is a slow operation; for raising to an integer power, use a combination of dup and * | x^{y} |
Square root | sqrt | x sqrt | Square root of x | √x |
Modulo | % | x y % | Remainder after dividing x by y | x mod y |
Absolute | abs | x abs | Absolute value of x | fabs(x) |
Negate | neg | x neg | Negative value of x | -x |
Min/max | ||||
Operation | Op | Example | Description | Result |
Minimum | min | x y min | Returns the lowest of x or y | min(x,y) |
Maximmum | max | x y min | Returns the highest of x or y | max(x,y) |
Zero-max | zmax | x y zmax | Constrains x between 0 and y | max(0,min(x,y)) |
Random | ||||
Operation | Op | Example | Description | Result |
Floating-point random | rand | rand | Returns a random value between 0 and 1 (exclusive) | (0,1) |
Integer random | irand | x irand | Returns an integer random value between 0 (inclusive) and x (exclusive) | [0,x) |
Trigonometrical operations | ||||
Operation | Op | Example | Description | Result |
Sine | sin | x sin | Sine of x | sin(x) |
Cosine | cos | x cos | Cosine of x | cos(x) |
Tan | tan | x tan | Tangent of x | tan(x) |
Sine/cosine | sincos | x sincos | This operations places both the sine and cosine of x onto the stack. It is faster than performing the operations separately. | sin(x), cos(x) |
Arcsine | asin | x asin | Inverse sine of x | asin(x) |
Arccosine | acos | x acos | Inverse cosine of x | acos(x) |
Atan | atan | x atan | Inverse tangent of x | atan(x) |
Atan2 | atan2 | x y atan2 | Inverse tangent of y/x (taking into account the signs of y and x) | atan2(y,x)(x) |
Comparison operations | ||||
Operation | Op | Example | Description | Result |
Less than | < | x y < | True if x is less than y | x < y |
Greater than | > | x y > | True if x is greater than y | x > y |
Less than or equal | <= | x y <= | True if x is less than or equal to y | x <= y |
Greater than or equal | >= | x y >= | True if x is greater than or equal toy | x >= y |
Equal | == | x y == | True if x is equal to y | x == y |
Not equal | != | x y != | True if x is not equal to y | x != y |
Stack operations | ||||
Operation | Op | Example | Description | Result |
Duplicate | dup | x dup | Duplicates the top item on the stack. This operation can be used in combination with * to raise to integer powers. | x, x |
Swap | swap | x y swap | Swaps the top two items on the stack. Can be used, for example, in combination with dup to apply multiple operations to the same input. For example x dup asin swap acos will leave both the arcsine and arccosine of x on the stack. | y, x |
The comparison operators above can be used with the conditional operator, ?
, to return different values based on the result of the comparison (or other operation). The format is [result_if_true] [result_if_false] [condition] ?
For example: 10 20 x y < ?
will return a value of 10 if x is less than y, otherwise it will return 20.
A condition is considered true if the value is not zero, otherwise it is considered false.
There are no boolean operations, but these can be simulated using arithmetic operations.
Note that all calculations are performed even if the value is ultimately bypassed by a conditional operation. For example: x y ^ 0 x y < ?
- the calcuation of x^y will occur regardless of the outcome of the comparison.
As an extension to the general RPN concept, the implementation allows for saving and loading of user-defined and filter-defined variables. Variables are referenced by a single case-sensitive alphabetical character.
To store the current top value of the stack to a variable: @q
To store the current top value and pop it from the top of the stack: @q^
To recall a variable, simply use the character: q
Example of adding two variables: a b +
Some filters use variables to store specific values. These always use lower-case letters, so upper-case letters are always free to use.
Using an uninitialised value will return an uncertain value.