EVAL
Contents
Introduction
The EVAL
command can be used to evaluate numerical expressions. These expressions may be built up from numerical constants, from scalar, vector, and matrix variables, and from a large number of functions and operators.
The EVAL
command was added to the S_TOOLS-STx language in version 3.7. It replaces and extends the old EVALUATE
command.
See also: EVALCHECK, INT, INTCHECK,NUM and NUMCHECK
Syntax
An EVAL
command uses the following general syntax:
result := eval expression
or
result := evalcheck expression
- result
- This is the target to be assigned with the result of the evaluation of the numerical expression. The result can be a shell variable or a numerical object
- expression
- The numerical expression to be evaluated. The expression consists of numerical objects, functions and operators.
Examples:
result := eval (5 * 10) % 3
result := eval init(10,1,1)
result := eval 5+max(fill(6,1,1))
The following list shows the syntax of EVAL
expression using the EBNF notation. The expressions are listed in the reverse order of priority (lowest first). The operators with lower priority are evaluated after that with higher priority.
assignment | Value = Or [ "?" Or ":" Or ] |
---|---|
logical or | Or = And { "||" And } |
logical and | And = Comparison { "&&" Comparison } |
comparison: | Comparison = AddSub { "<" | "<=" | "==" | "!=" | ">=" | ">" AddSub } |
add, substract | AddSub := MulDiv { "+" | "-" MulDiv } |
multiply, divide | MulDiv := Pwr [ "*" | "/" | "%" Pwr ]* |
power | Pwr := NegInv [ "^" NegInv ] |
negate | NegInv := [ "-" | "!" ] Atom |
brackets, functions, numerical objects |
Atom := "(" expression ")" | "|" expression "|" | functionName "(" [ expression { "," expression } ] ")" | numericalObject |
If the expression is syntactically ill-formed an error (EVAL
) or warning (EVALCHECK
) is reported and the assignment is not performed (content of result is not changed). See the example script expression_check.sts
for details. You can also use the BScript console to try out the EVAL
command.
Numerical objects
The following numerical objects are known to the EVAL
command. The fields of the table item table are all numeric (extended table or parameter table). The value item value can contain numbers, vectors or matrices. The wave item wave is any wave item.
Syntax | Description | Data type |
---|---|---|
constant | a scalar constant. Any number (17, 4.5, 2.5e-6, ...) or one of the following strings: pi (3.14159...), e (2.71828...), true (1) or false (0) |
scalar |
table | the content of the whole table | vector, matrix |
table[i,*] or table[i,] | the i-th row of the table | scalar, vector |
table[*,j] or table[,j] | the j-th column of the table | scalar, vector |
table[i,j] | the value of the i-th row and j-th column of the table | scalar |
value | the content of the value item | scalar, vector, matrix |
value[i,*] or value[i,] | the i-th row of the value item | scalar, vector |
value[*,j] or value[,j] | the j-th column of the value item | scalar, vector |
value[i,j] | the value of the i-th row and j-th column of the value item | scalar |
wave[!signal,*] or wave[!signal,] | the signal from all channels | vector, matrix |
wave[!signal,ch] | the signal from channel ch (=1,2,...) | vector |
wave[!signal,*,b,l] | the signal from all channels from sample b (0 <= b < wave[!length]) to sample b+l-1 (l > 0) If b is lower than 0 or b+l is greater than wave[!length] zero padding is applied to the result |
vector, matrix |
wave[!signal,ch,b,l] | the signal from channel ch from sample b to sample b+l-1 | vector |
row- and column-vectors
In STx it is not possible to differentiate between row-vectors and column-vectors. Normally a vector is a row-vector, but it depends on the context (the operator or function used) which type of vector is supposed.
complex numbers
The current version of STx do not implement the datatype complex, but a lot of functions take complex arguments and/or return complex results. Therefore the following convention for numerical objects holding complex numbers is used:
- complex number: A vector with two elements. If the number is stored in the cartesian format, the first element is the real part and the second the imaginary part. If it is stored in polar format, the first element is the magnitude (or length) and the second the phase.
- complex vector: A vector of N complex numbers consists of 2N elements. The element 2i is the real part (or magnitude) and the element 2i+1 is the imaginary part (or phase) of the i-th complex number (i = 0..N-1).
- complex matrix: A matrix of N x M complex numbers consists of 2N rows and M. Each row is formatted like a complex vector described above.
polygons
The EVAL
command implements a set of functions to manipulate 2-dimensional polygons. These functions are using special data types (or formats) to store informations about one or more polygon.
- closed point-list (pg_cplist)
- Is a list of N points Pi in a 2-dimensional plane. Each pair (Pi , Pi+1) builds one edge of a polygon. The list is called closed, because PN-1 is connected to P0. A pg_cplist is stored in a matrix PL with N rows and 2 columns. The coordinates of point Pi are stored in PL[i,0] (xi) and PL[i,1] (yi).
polygon-stream (pg_stream)
- simple-polygon (pg_simple)
- Is a closed point-list (pg_cplist) defining a polygon without crossing edges.
- polygon-stream (pg_stream)
- Is a packed format to store the point-list and some additional information for one or more simple-polygons. This format is created by the initialisation function initialisation function and used as argument and/or result by the most other polynom processing functions. A pg_stream is stored in a matrix with 2 columns and 8M + N0 + .. + NM-1 rows. Where M means the number of polynoms stored in the stream and Ni the number of points of the i-th polynom.
row
indexvalue of
column 0value of
column 1description -oi+0 reserved
set to 0Ni: number of points -oi+1 IXi IYi IXi: the index of the leftmost/lowest point (used for computations y=f(x))
IYi: the index of the lowest/leftmost point (used for computations x=f(y))
X[o+2] = X1, Y[o+2] = Y1
X[o+3] = X2, Y[o+3] = Y2
X[o+4] = Xc, Y[o+4] = Yc
X[o+5] = A, Y[o+5] = L
X[o+6] = cinside{0|1}, Y[o+6] = convex{0|1}
x[o+7] = reserved1{0}, Y[o+7] = reserved2{0}
X[o+8+i] = x[i], Y[o+8+i] = y[i]
with:
o - starting index of polygon in pg_stream buffer (offset)
N - number of points
Ix - index of left/lower corner (used for computations of y=f(x))
Iy - index of lower/left corner (used for computations of x=f(y))
X1,Y1 - lower left corner of bounding rectangle
X2,Y2 - upper right corner of bounding rectangle
A - area
L - circumference
Xc,Yc - center of gravity
cinside - 1 if center of gravity is inside the polygon, 0 otherwise
convex - 1 if polygon is convex, 0 otherwise
x[i],y[i] - coordinates of point i (i = 0..N-1)
See the example macro polygon_stream
in the example script polygon_examples.sts
.
Operators
Numerical operators
Syntax | Description | Datatype of Result |
---|---|---|
-x | Negate all elements of x. | same as x |
y+xS or xS+y | Add the scalar xS to all elements of y. | same as y |
x+y | Add elements of x to the elements of y. Both operands must be of the same type. | same as x and y |
y-xS | Subtract the scalar xS from each element in y. | same as y |
xS–y | Subtract all elements of y from the scalar xS. | same as y |
x-y | Subtract elements of x from the elements of y. Both operands must be of the same type. | same as x and y |
y*xS or xS*y | Multiply all elements of y with the scalar xS. | same as y |
xV*yV | The inner (or dot) product of the vectors xV and yV. The length of both vectors must be the same. | scalar |
xM*yV | The product of the matrix xM and the vector yV. The length of the vector yV must be the same as the number of columns in the matrix xM. | vector with nrow(xM) elements |
xM*yM | The product of the matrix xM and yM. The number of rows in yM must be equal to the number of columns in xM. | matrix with nrow(xM) rows and ncol(yM)) columns |
y/xS | Divide all elements of y through the scalar xS. The value of xM may not be 0. | same as y |
xS/yM | Multiply all elements of the inverse matrix of yM with the scalar xS. The matrix yMmust be a square matrix.The special case 1/yM returns the inverse matrix. Note that the function inv(y) can also be used to invert scalars and matrices. | same as yM |
y%xS | The rest of the division of every element in y through the scalar xS (modulo) | same as y |
y^xS | Raise every element of y to the power of the scalar xS. Special cases: y^-1 is calculated like 1/y and y^2 is calculated like this y*y
|
same as y |
If one of the numerical operands has the prefix ? then the operation is carried out element per element. Example:
#dotprod := eval $#x * $#y // dot product of vectors #x and #y #winsig := eval $#x ?* $#y // multiply elements of #x and #y (e.g. to apply signal windowing)
Comparison operators
Syntax | Description |
---|---|
x<y | x less than y |
x>y | x greater than y |
x<=y | x less than or equal' to y |
x>=y | x greater than or equal to y |
x==y | x equal to y. |
x!=y | x not equal to y. |
The result of a comparison of two numerical expressions is (logical) true (numerical 1), if the condition is fulfilled, otherwise it is (logical) false (numerical 0). Normally it make only sense to compare scalars or, more generally, numerical objects with equal dimensions. The following rules applies to a comparison:
- The numerical objects x and y' are equal (x==y is true), if (and only if) the dimensions of x and y are the same and all pairs of elements (xi,j,yi,j) are numerically equal.
- The numerical objects x and y are not equal (x!=y is true), if the dimensions of x and y are different or at least one element pair (xi,j,yi,j) is numerically not equal.
- For all other comparisons the dimensions of x and y must be the same, otherwise the comparison failes. The result is true, if the condition is fulfilled for all pairs of elements (xi,j,yi,j).
Logical operators
Syntax | Description |
---|---|
x||y | logical or |
x&&y | logical and |
!x | unary not |
The result of a logical operation is true (numerical 1) or false (numerical 0). The arguments x and y can be any numerical type. It is also possible to use different arguments with different types. The following rules are used to convert logical (boolean) to numerical values and vice versa:
The numerical result of a logical operation is the value ...
- 1 if the logical expression is true
- 0 if the logical expression is false
The logical value of a numerical object (or expression) is ...
- true if one of its elements is not equal to 0.
- false if all of its elements are equal to 0.
Selection operator
log_expr ? true_expr : false_expr
- log_expr
- A logical expression which is used to select the expression to be evaluated.
- true_expr
- This expression is evaluated and returned as result of the selection operator, if log_expr is true.
- true_expr
- This expression is evaluated and returned as result of the selection operator, if log_expr is false.
If the selection operator is nested, it must be surrounded by brackets, e.g.:
result := eval 1 > 2 ? (5 == 5 ? 5 : 0) : (4 == 5 ? 3 : 4) // result is 4
Functions=
Examples
For an extensive list of examples, see the script eval_examples.sts
:
Programmer Guide/Command Reference/EVAL Examples/EVAL Examples