next up previous contents
Next: Field calculator Up: Field evaluator Previous: Field evaluator

Expressions

 

The NDA kernel has a simple parser, which can be used to evaluate expressions. The parser provides three services: parsing given expressions, checking data types and evaluating parsed expressions.

The parser uses a syntax including the following operators and operands:

Operands
numerical normal integer or floating point numbers
strings inside double quotes, for instance, "string"
boolean reserved words: TRUE/FALSE (1 / 0)
fields inside single quotes, for instance, 'stats.hits'
Operators
+ sum two numeric operands or, if operands are strings, concatenate them
- subtract the second operand from the first one
tex2html_wrap_inline7949 multiply two operands
/ divide the first operand by the second one (integer division, if both operands are integer)
% remainder of the integer division (first by second; both operands have to be integer)
tex2html_wrap_inline7951 raise the first operand a to the power of second operand b, i.e. tex2html_wrap_inline7953
=, >, >=, <, <=, != compare two operands ( tex2html_wrap_inline7955 , a > b, tex2html_wrap_inline7959 , a < b, tex2html_wrap_inline7963 and tex2html_wrap_inline7965 )
AND take logical and of two operands ( tex2html_wrap_inline7967 )
OR take logical or of two operands ( tex2html_wrap_inline7969 )
NOT take logical not of a boolean value ( tex2html_wrap_inline7971 )
Data type conversions
FLOAT convert the following operand to a float value (strings might result in 0.0, booleans 1.0/0.0)
INT convert the following operand to an integer value (strings might result in 0, booleans 1/0)
STRING corvert the following operand to a string (booleans will result in TRUE/FALSE)

Arithmetic functions
RAND return a pseudo-random floating point number between 0.0 and operand
IRAND return an integer pseudo-random number between 0 and operand - 1
ABS return the absolute value of operand
SQRT calculate tex2html_wrap_inline7973
EXP calculate tex2html_wrap_inline7975
LN calculate the natural logarithm of a positive operand
LOG calculate the 10-based logarithm of a positive operand
SIN calculate the sine of the operand (specified in radians)
COS calculate the cosine of the operand (specified in radians)
TAN calculate the tangent of the operand (specified in radians)
ARCSIN evaluate the angle, whose sine is operand (operand must be between -1.0 and 1.0)
ARCCOS evaluate the angle, whose cosine is operand (operand must be between -1.0 and 1.0)
ARCTAN evaluate the angle, whose tangent is operand
Parentheses
() the order of evaluation can be changed using parentheses. They may be nested
; an expression is always terminated with a semicolon (semicolons within double quotes, i.e. strings, are ignored)

Type checking is performed after an expression has been parsed to an abstract syntax tree. In addition, the lengths of data fields are checked. When data fields are referred to in an expression, they must be of equal length or the length must be 1, to make the evaluation possible. Namely, the expression is evaluated record by record (index by index), but data fields having a length of 1 are interpreted as scalar variables. Numerical constants can also be used to represent scalar values.

The evaluation result of the expression depends on the operation, which invokes the parser. If the parser is used to compute a new field (command expr), then the returned value of the evaluation can be of any type. Instead, when the parser is used to evaluate the truth-value of some expression, the parsing should produce boolean values. Then, the highest operator of the parsing tree should be a comparison or a logical operator.

Example (ex4.2): The following commands use the expression evaluator (see sections 4.3.2, 4.3.3, 4.3.4 and 7.3.6).

...
# Compute new fields
NDA> expr -dout boston -fout znPrice
     -expr 'boston.zn'/'boston.rate';
...
# Select indexes to a new class according to a given rule
NDA> selcl -cout c1/cld -clout bigZn 
     -expr ('boston.zn'>3.0) AND ('boston.rate'<25);
...
# Select a new data frame (containing values under the average)
# In this case 'rate.avg' is a used as a scalar variable
NDA> select avgfld -f boston.rate
NDA> fldstat -d avgfld -dout rate -avg
NDA> selrec -d boston -dout data
     -expr ('boston.rate' < 'rate.avg');
...
# Set neurons to be visible according to a given rule
NDA> nvsbl grp1 -expr 'sta.hits'> 0;


next up previous contents
Next: Field calculator Up: Field evaluator Previous: Field evaluator

Anssi Lensu
Thu May 17 15:00:44 EET DST 2001