Skip to content

foresight_expression ​

foresight_expression, arithmetic expressions of gnuplot using fields and plotted functions.

An expression such as ($2*1e3) or ($3 > 0 ? log10($3) : 1/0) is compiled once into stack code and evaluated on each data row; compiled with a dummy variable, sin(x)/x is a function evaluated at sample abscissae (value_at), where columns are errors. The semantics are gnuplot's:

  • operators, loosest first: ?:, ||, &&, == !=, < <= > >=, + -, * / %, unary - + !, ** (right associative, so -2**2 is -4 and 2**3**2 is 512); &&, || and ?: evaluate only what they need;
  • operands: numbers, $N and column(N) (column N of the row, 0 is the point number), column("name") (the column of that header, see resolve), pi, and the functions abs acos asin atan atan2 ceil cos cosh exp floor int log log10 sgn sin sinh sqrt tan tanh;
  • integer constants are integers: 1/2 is 0, -5/2 is -2, 7/2. is 3.5; an integer overflow gives a real; columns are real; floor, ceil, int, sgn, comparisons and logical operators give integers;
  • a missing cell, a division by zero, a domain error (sqrt(-1), log(0)) or an overflow make the whole value undefined: NaN, a gap in the plot, as gnuplot's 1/0;
  • compiled with degrees (gnuplot set angles degrees), sin, cos, tan take degrees and asin, acos, atan, atan2 return them.

Accepted beyond gnuplot: % and the logical operators on reals (gnuplot rejects them), and overflows give a gap where gnuplot stops the plot. Every floating point operation is checked beforehand, so evaluation never raises an IEEE invalid, division by zero or overflow exception.

Source: src/lib/foresight_expression.F90

Dependencies

Contents ​

Variables ​

NameTypeAttributesDescription
OP_CONSTANTinteger(kind=I4P)parameterPush the constant.
OP_COLUMNinteger(kind=I4P)parameterPush column arg.
OP_COLUMN_OFinteger(kind=I4P)parameterReplace the top with the column it numbers.
OP_NEGATEinteger(kind=I4P)parameterUnary minus.
OP_NOTinteger(kind=I4P)parameterLogical not.
OP_TRUTHinteger(kind=I4P)parameterReplace the top with 0 or 1.
OP_ADDinteger(kind=I4P)parameter+; binary operators run from here to OP_NE.
OP_SUBTRACTinteger(kind=I4P)parameter-.
OP_MULTIPLYinteger(kind=I4P)parameter*.
OP_DIVIDEinteger(kind=I4P)parameter/.
OP_MODULOinteger(kind=I4P)parameter%.
OP_POWERinteger(kind=I4P)parameter**.
OP_ATAN2integer(kind=I4P)parameteratan2(y, x).
OP_LTinteger(kind=I4P)parameter<.
OP_LEinteger(kind=I4P)parameter<=.
OP_GTinteger(kind=I4P)parameter>.
OP_GEinteger(kind=I4P)parameter>=.
OP_EQinteger(kind=I4P)parameter==.
OP_NEinteger(kind=I4P)parameter!=.
OP_FUNCTIONinteger(kind=I4P)parameterFunction arg of the top.
OP_JUMPinteger(kind=I4P)parameterJump to arg.
OP_JUMP_UNLESSinteger(kind=I4P)parameterPop; jump to arg if false.
OP_AND_JUMPinteger(kind=I4P)parameterIf the top is false: make it 0, jump to arg; else pop.
OP_OR_JUMPinteger(kind=I4P)parameterIf the top is true: make it 1, jump to arg; else pop.
OP_COLUMN_NAMEDinteger(kind=I4P)parameterPush the column of header names(arg): undefined until resolved.
FUNCTIONScharacter(len=*)parameter
LEVELScharacter(len=*)parameter
MAX_NESTINGinteger(kind=I4P)parameterDeepest parentheses and unary chains.
MOSTinteger(kind=I8P)parameterLargest integer.
LEASTinteger(kind=I8P)parameterSmallest integer.
HUGE_REALreal(kind=R8P)parameterLargest real.
LOG_HUGEreal(kind=R8P)parameterLargest safe exp argument.
LOG_TINYreal(kind=R8P)parameterexp of less is 0.
INT_LIMITreal(kind=R8P)parameterReals from here on are not I8P integers.
PIreal(kind=R8P)parameterpi.

Derived Types ​

value_object ​

Stack value: an integer or a real, as in gnuplot.

Components ​

NameTypeAttributesDescription
rreal(kind=R8P)Real value.
iinteger(kind=I8P)Integer value.
is_intlogicalInteger.

instruction_object ​

Stack code instruction.

Components ​

NameTypeAttributesDescription
opinteger(kind=I4P)Operation code.
arginteger(kind=I4P)Column, function or jump target.
vtype(value_object)Constant.

name_object ​

Column header name.

Components ​

NameTypeAttributesDescription
textcharacter(len=:)allocatableName.

expression_object ​

Compiled expression.

Components ​

NameTypeAttributesDescription
textcharacter(len=:)allocatableSource text.
codetype(instruction_object)allocatableStack code.
namestype(name_object)allocatableColumn header names, column("name").
depthinteger(kind=I4P)Stack size needed.
degreeslogicalAngles of the trigonometric functions in degrees

Type-Bound Procedures ​

NameAttributesDescription
compilepass(self)Compile an expression.
evaluatepass(self)Value on a data row.
first_columnpass(self)First data column read.
name_countpass(self)Number of column header names.
name_ofpass(self)A column header name.
resolvepass(self)Header names to column numbers.
set_columnpass(self)Plain column.
set_namepass(self)Plain column of a header name.
value_atpass(self)Value of a function at its variable value.

Subroutines ​

compile ​

Compile text; on error iostat is not 0 and iomsg names the problem and its position.

With variable (gnuplot's dummy x) the expression is a function of it, evaluated by value_at; columns are then errors. The variable is held as the only cell of the row evaluate receives. With degrees true the trigonometric functions work in degrees.

fortran
subroutine compile(self, text, iostat, iomsg, variable, degrees)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inoutExpression.
textcharacter(len=*)inSource text.
iostatinteger(kind=I4P)out0 on success.
iomsgcharacter(len=:)outallocatableError message.
variablecharacter(len=*)inoptionalDummy variable name, for a function.
degreeslogicalinoptionalAngles in degrees.

Call graph

set_column ​

Make the expression the plain column c (0 is the point number).

Attributes: pure

fortran
subroutine set_column(self, c)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inoutExpression.
cinteger(kind=I4P)inColumn.

Call graph

set_name ​

Make the expression the plain column of header name, as gnuplot using 1:"name".

Attributes: pure

fortran
subroutine set_name(self, name)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inoutExpression.
namecharacter(len=*)inColumn header name.

Call graph

binary ​

c = a op b: an integer when both are and the result fits, else a real; ok false if undefined.

Attributes: pure

fortran
subroutine binary(op, a, b, c, ok)

Arguments

NameTypeIntentAttributesDescription
opinteger(kind=I4P)inOperation code.
atype(value_object)inLeft operand.
btype(value_object)inRight operand.
ctype(value_object)outResult.
oklogicalinoutDefined.

Call graph

integer_binary ​

c = i op j on integers; done false when the result is not an integer (overflow, negative power, atan2).

Attributes: pure

fortran
subroutine integer_binary(op, i, j, c, done, ok)

Arguments

NameTypeIntentAttributesDescription
opinteger(kind=I4P)inOperation code.
iinteger(kind=I8P)inLeft operand.
jinteger(kind=I8P)inRight operand.
ctype(value_object)inoutResult.
donelogicaloutInteger result computed.
oklogicalinoutDefined.

Call graph

unary_degrees ​

c = FUNCTIONS(f)(a) with the angles of the trigonometric functions in degrees; ok false if undefined.

Attributes: pure

fortran
subroutine unary_degrees(f, a, c, ok)

Arguments

NameTypeIntentAttributesDescription
finteger(kind=I4P)inFunction index.
atype(value_object)inArgument.
ctype(value_object)outResult.
oklogicalinoutDefined.

Call graph

unary ​

c = FUNCTIONS(f)(a); ok false if undefined.

Attributes: pure

fortran
subroutine unary(f, a, c, ok)

Arguments

NameTypeIntentAttributesDescription
finteger(kind=I4P)inFunction index.
atype(value_object)inArgument.
ctype(value_object)outResult.
oklogicalinoutDefined.

Call graph

safe_add ​

z = x + y, undefined on overflow.

Attributes: pure

fortran
subroutine safe_add(x, y, z, ok)

Arguments

NameTypeIntentAttributesDescription
xreal(kind=R8P)inAddend.
yreal(kind=R8P)inAddend.
zreal(kind=R8P)outSum.
oklogicalinoutDefined.

Call graph

safe_power ​

z = x ** y on reals; undefined when complex (negative base, fractional exponent), infinite or overflowing.

Attributes: pure

fortran
subroutine safe_power(x, y, z, ok)

Arguments

NameTypeIntentAttributesDescription
xreal(kind=R8P)inBase.
yreal(kind=R8P)inExponent.
zreal(kind=R8P)outPower.
oklogicalinoutDefined.

Call graph

Functions ​

evaluate ​

Value on a data row: row holds its cells (NaN if missing), point is its point number (column 0).

NaN if undefined. Every floating point operation is checked beforehand, so no IEEE exception is raised.

Attributes: pure

Returns: real(kind=R8P)

fortran
function evaluate(self, row, point) result(v)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.
rowreal(kind=R8P)inRow cells.
pointinteger(kind=I4P)inPoint number.

Call graph

first_column ​

First data column (from 1) the expression reads, 0 if none (an unresolved name is none): the column whose header titles the item, as gnuplot title columnhead.

Attributes: pure

Returns: integer(kind=I4P)

fortran
function first_column(self) result(c)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.

name_count ​

Number of column header names used.

Attributes: elemental

Returns: integer(kind=I4P)

fortran
function name_count(self) result(n)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.

Call graph

name_of ​

The k-th column header name.

Attributes: pure

Returns: character(len=:)

fortran
function name_of(self, k) result(name)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.
kinteger(kind=I4P)inName index.

Call graph

resolve ​

The expression with its column header names replaced by their column numbers in header (the names of columns 1, 2, ...; trailing blanks ignored); a name not in it reads no column: undefined.

Attributes: pure

Returns: type(expression_object)

fortran
function resolve(self, header) result(resolved)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.
headercharacter(len=*)inColumn names.

value_at ​

Value of an expression compiled with a dummy variable at the variable value x; NaN if undefined.

Attributes: pure

Returns: real(kind=R8P)

fortran
function value_at(self, x) result(v)

Arguments

NameTypeIntentAttributesDescription
selfclass(expression_object)inExpression.
xreal(kind=R8P)inVariable value.

Call graph

in_level ​

Whether operator is a binary operator of grammar level level.

Attributes: pure

Returns: logical

fortran
function in_level(operator, level) result(yes)

Arguments

NameTypeIntentAttributesDescription
operatorcharacter(len=*)inOperator.
levelinteger(kind=I4P)inLevel in LEVELS.

binary_code ​

Operation code of a binary operator.

Attributes: pure

Returns: integer(kind=I4P)

fortran
function binary_code(operator) result(op)

Arguments

NameTypeIntentAttributesDescription
operatorcharacter(len=*)inOperator.

real_of ​

Real value of a.

Attributes: pure

Returns: real(kind=R8P)

fortran
function real_of(a) result(r)

Arguments

NameTypeIntentAttributesDescription
atype(value_object)inValue.

Call graph

truth ​

Whether a is not zero.

Attributes: pure

Returns: logical

fortran
function truth(a) result(yes)

Arguments

NameTypeIntentAttributesDescription
atype(value_object)inValue.

Call graph

logical_value ​

Integer 1 or 0.

Attributes: pure

Returns: type(value_object)

fortran
function logical_value(yes) result(a)

Arguments

NameTypeIntentAttributesDescription
yeslogicalinTruth.

Call graph

int_value ​

Integer value.

Attributes: pure

Returns: type(value_object)

fortran
function int_value(i) result(a)

Arguments

NameTypeIntentAttributesDescription
iinteger(kind=I8P)inInteger.

Call graph

fits_product ​

Whether i*j fits an I8P integer.

Attributes: pure

Returns: logical

fortran
function fits_product(i, j) result(yes)

Arguments

NameTypeIntentAttributesDescription
iinteger(kind=I8P)inFactor.
jinteger(kind=I8P)inFactor.

Call graph

divisible ​

Whether x / y is finite.

Attributes: pure

Returns: logical

fortran
function divisible(x, y) result(ok)

Arguments

NameTypeIntentAttributesDescription
xreal(kind=R8P)inDividend.
yreal(kind=R8P)inDivisor.

Call graph