MANUALS:
  • Short Guide to INVERSE
  • Flow Control
  • Expression evaluator
  • Basic Properties of the Expression Evaluator
  • Expression Evaluator's Operators
  • Pre-defined Functions of the Expression Evaluator
  • File Interpreter's Functions which Affect the Expression Evaluator
  • User defined variables
  • General file interface
  • Syntax checker & debugger
  • Interfaces with simulation programmes
  • 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.1 Basic Properties of the Expression Evaluator *

    1.2 Expression Evaluator's Operators *

     
    1.2.1 Expression Evaluator?'s Binary Operators *

    1.2.2 Expression Evaluator's Unary Operators (functions) *


    1.3 Pre-defined Functions of the Expression Evaluator *

     
    1.3.1 random [ ] *

    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.1 = { var : expr } *

    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 [ ] *
     
     


     
     
    1. Expression Evaluator

    2.  
      Manuals list Manuals list & contents Table of contents 

       
      1. Basic Properties of the Expression Evaluator

      2.  
        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.
         
         

      3. Expression Evaluator's Operators

      4.  
        Manuals list Manuals list & contents Table of contents 

         
        1. Expression Evaluator's Binary Operators

        2.  
          Operator
          Meaning
          Priority number
          + 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

           

        3. Expression Evaluator's Unary Operators (functions)

         
            All unary operators of the expression evaluator have the priority 1.
           
          Operator name
          Meaning
          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

           
           
      5. Pre-defined Functions of the Expression Evaluator

      6.  
        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:
         

        1. random [ ]

        2. Returns a random number between 0 and 1.
           
           
        3. If [ cond, exp ]

        4. Returns the value of exp if the value of cond is not 0, else it returns 0.
           
           
        5. Else [ cond, exp1, exp2 ]

        6. If the value of cond is not 0, it returns the value of exp1, else it returns the value of exp2.
           
           
        7. Case [ ]

        8.  

           
           
           
           
           

        9. Min [ arg1, arg2, arg3, ... ]

        10. Returns the value of the least of its arguments arg1, arg2, etc. It requires at least one argument and can take unlimited number of arguments.
           
           
        11. Max [ arg1, arg2, arg3, ... ]

        12. Returns the value of the least of its arguments arg1, arg2, etc. It requires at least one argument and can take unlimited number of arguments.
           
           
        13. Sum [ arg1, arg2, arg3, ... ]

        14. Returns the sum of its arguments arg1, arg2, etc. It requires at least one argument and can take unlimited number of arguments.
           
           
        15. Prod [ arg1, arg2, arg3, ... ]

        16. Returns the product of its arguments arg1, arg2, etc. It requires at least one argument and can take unlimited number of arguments.
           
           
        17. Trapint [ func[ 0.0 ], left, right, numsteps ]

        18. Returns the integral of function func between left and right calculated by the trapezoidal rule with numsteps steps. The first argument must be a function call with an argument which is a real number. Such arrangement speeds up the calculation. func can be a pre-defined function of one argument, a function defined with the expression evaluator, or a function defined by the definefunction command.

          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" }
           
           

        19. File Interpreter's Functions which Affect the Expression Evaluator

        20.  
          Manuals list Manuals list & contents Table of contents 

           
          1. = { var : expr }

          2. The = function assigns the value of the expression expr to the expression evaluator's variable var. If a variable named var does not yet exist, it is created.
             
             
             
          3. $ { var : expr }, $ { func[ arg1, arg2, ... ] : expr }

          4. The $ function assigns the expression expr to the expression evaluator's variable var. After the execution of this function, the value of variable named var changes if the values of variables or definitions of functions which are included in the expression expr change. If a variable named var does not yet exist, it is created.

            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.
             
             
             

          5. definefunction { funcname [ defblock ] }

          6. The file interpreter's function definefunction defines a new expression evaluator's function. funcname is the name of the function and the defblock is the definition block of the function. At every evaluation of the function after this definition, this block is interpreted.

            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[ ])

            [
             

              ={retsum:retsum+argument[indsum]}

              ={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.
             
             
             

          7. return { expr }

          8. The file interpreter's function return is used for setting the value which is returned by user defined functions of the expression evaluator which are defined by the definefunction command. expr is a mathematical expression which defines the value which will be returned by such function. The return function can be used only in the definition block of the definefunction command.
             
             
          9. argument [ num ]

          10. The expression evaluator-s function argument returns the value of num-th argument passed to the expression evaluator's function which is currently being evaluated. Therefore, this function can be evaluated only within the definition block of the definefunction command.
             
             
             
          11. numargs [ ]

          12. The expression evaluator-s function numargs returns the number of arguments that were passed to the expression evaluator-s function which is currently being evaluated. This function can therefore be evaluated only within the definition block of the definefunction function.