Manuals list | Manuals list & contents | Table of contents |
The Expression Evaluator of the
Optimisation Shell INVERSE
Version 0
Igor Gresovnik
Ljubljana, the 30 th of November 1998
Table of contents:
1. Expression Evaluator *
1.2 Expression Evaluator's Operators *
1.2.2 Expression Evaluator's Unary Operators (functions) *
1.3 Pre-defined Functions of the Expression Evaluator
*
1.3.2 If [ cond, exp ] *
1.3.3 Else [ cond, exp1, exp2 ] *
1.3.4 Case [ ] *
1.3.5 Min [ arg1, arg2, arg3, ... ] *
1.3.6 Max [ arg1, arg2, arg3, ... ] *
1.3.7 Sum [ arg1, arg2, arg3, ... ] *
1.3.8 Prod [ arg1, arg2, arg3, ... ] *
1.3.9 Trapint [ func[ 0.0 ], left, right, numsteps ] *
1.4 File Interpreter's Functions which Affect the
Expression Evaluator *
1.4.2 $ { var : expr }, $ { func[ arg1, arg2, ... ] : expr } *
1.4.3 definefunction { funcname [ defblock ] }*
1.4.4 return { expr } *
1.4.5 argument [ num ] *
1.4.6 numargs [ ] *
Manuals list | Manuals list & contents | Table of contents |
Manuals list | Manuals list & contents | Table of contents |
The expression evaluator, also referred to as calculator, is a system which evaluate mathematical expressions. These expressions consist of numbers, operators (unary and binary), parentheses, and symbols. Spaces are allowed between these expression parts, but they have no meaning in expressions. Symbols can be variable or function names. There are some pre-defined functions of the expression evaluator, but its functions can be also defined anew, either by the file interpreter or the expression evaluator itself. There are no pre-defined variables. The expression evaluator's variables differ from the user-defined variables of the shell.
Expressions can be formed in conventional way. When there are several binary operator in the expression, operators with lower priority number take effect first. When there are several binary operators with the same priority, the operators which appear before take effect first. The order of operations can be changed by round parentheses which group sub-expressions.
Variable and function names consist of letters and numbers. The first character of the name must be a letter. Difference is made between capital and small letters.
The expression evaluator's functions can take one or more arguments. They must be listed in square brackets which follow the function name, and must be separated by commas. Arguments can be expressions which can be evaluated in the expression evaluator. Of course, these expressions can consist of a single variable or number.
Some expression evaluator's functions can also take string arguments.
This feature was actually added to support functions which access the shell's
user defined variables. The use of strings as arguments is limited due
to the fact that any mathematical expression evaluated in the expression
evaluator can evaluate only to a real number, not to a string. Therefore,
the expression evaluator's functions defined by the definefunction
command can not take string arguments.
Manuals list | Manuals list & contents | Table of contents |
|
|
|
+ | addition | 4 |
- | substraction | 4 |
* | multiplication | 3 |
/ | division | 3 |
^ | power | 2 |
P | power | 2 |
CP | power with integer exponent | 2 |
LOG | logarithm with arbitrary basis | 2 |
MIN | lesser of both arguments | 2 |
MAX | greater of both arguments | 2 |
< | is lesser than | 5 |
> | is greater than | 5 |
!= | is not equal | 5 |
<= | is lesser or equal | 5 |
>= | is greater or qqual | 5 |
== | is equal | 5 |
&& | and | 6 |
|| | or | 6 |
: | definition | 10 |
, | enumeration | 9 |
= | assignment | 10 |
|
|
EQ | equality |
- | negative value |
NEGV | negative value |
exp | exponential function |
ln | natural logarithm |
sqr | square |
sqrt | square root |
abs | absolute value |
sin | sine |
cos | cosine |
tg | tangent |
ctg | cotangent |
arcsin | inverse sine |
arccos | inverse cosine |
arctg | inverse tangent |
arcctg | inverse cotangent |
sh | hyperbolic sine |
ch | hyperbolic cosine |
th | hyperbolic tangent |
cth | hyperbolic cotangent |
arsh | inverse hyperbolic sine |
arch | inverse hyperbolic cosine |
arth | inverse hyperbolic tangent |
arcth | inverse hyperbolic cotangent |
st | converts radians to degrees |
deg | converts radians to degrees |
rad | converts degrees to radians |
round | rounds its argument to the nearest integer |
trunc | truncates its argument to the nearest integer below |
floor | truncates its argument to the nearest integer below |
int | truncates its argument to the nearest integer below |
frac | returns the fractional part of the argument |
Manuals list | Manuals list & contents | Table of contents |
Expression evaluator functions can have more than one argument. Their
arguments must be in square brackets and separated by commas. The expression
evaluator has some basic pre/defined functions:
Example of function use:
= { b : Trapint[ sin[0.0 ], 0, 1, 100 }
write { "Integral of the sine function between
0 and 1 is " $b ".\n\n" }
Manuals list | Manuals list & contents | Table of contents |
The $ function can also be used for the definition of new expression evaluator's functions. In this case, func is the name of the newly defined function, arg1, arg2, etc. are the names of function arguments which must be listed in square brackets and separated by commas, and expr is the expression which defines how the newly defined function will be evaluated. The expression exp usually contains objects named as formal arguments listed in the square brackets. At the function evaluation, these objects are replaced by the actual arguments with which function is called.
The expression expr can also contain other variables and functions of the expression evaluator. If the definitions of these variables or functions are changed later, the definition of the variable or function defined by the $ function changes accordingly.
Example:
$ { powsum[x,y,z]:x^[y*z] }
defines a new function of the expression evaluator named powsum,
which takes three arguments and returns the first argument raised to the
power of the sum of the second and the third argument.
Calculator's functions defined by the definefunction command can be called with arbitrary number of arguments. These can be accessed by the expression evaluator's function argument. This function returns the value of a specific argument which was passed to the expression evaluator's function defined by the definefunction command. Therefore, the argument function can be evaluated only within the definition block of an expression evaluator function defined by the file interpreter's command definefunction. The only argument of the argument function is the sequential number of argument the value of which should be returned. Since expression evaluator's functions can only return (i.e. evaluate to) real numbers, the expression evaluator's functions defined by the definefunction command can not take string arguments. Such arguments could not be accessed in the defblock block because the only way of accessing arguments in this block is through the calculator's function argument. Arguments must therefore be expressions which evaluate to real numbers.
The number of arguments which have been passed to the function defined by the definefunction command can be accessed by the expression evaluator's function numargs. This functin takes no arguments and and can also be evaluated only within the definition block of the definefunction command.
The value which is returned by a function defined by the definefunction command must be specified by the file interpreter's function return. The only argument of this function must be a mathematical expression which can be evaluated in the expression evaluator. The value of this expression is what the function defined by the definefunction command returns.
Example:
The following portion of code defines an expression evaluator's function Summation which takes an arbitrary number of arguments and returns their sum:
definefunction { ( Summation )
[
={retsum:0}
={indsum:0}
while { (isum<=numargs[ ])
[
={indsum:indsum+1}
return{retsum}
] }
After the function is defined, it can be used in mathematical expressions. For example, the expression "Summation[3,2*4,5]" will evaluate to 16 (=3+2*4+5).
Warning:
At the definition of new expression evaluator's functions we must be
careful at choosing names for auxiliary variables used as counters or for
carrying intermediate results. A concept of local variables is not implemented
in the file interpreter, therefore all variables are global. The variables
used locally in the definition block of the definefunction command
can therefore interfere with global variables if we accidentally chose
the same name for them.