3.1 Numeric expressions

Numeric expression is a rule for computing a single numeric value represented in the form of floating-point number.

The primary numeric expression may be a numeric literal, dummy index, unsubscripted parameter, subscripted parameter, built-in function reference, iterated numeric expression, conditional numeric expression, or another numeric expression enclosed in parentheses.

Examples

1.23(numeric literal)
j(dummy index)
time(unsubscripted parameter)
a['May 2003',j+1](subscripted parameter)
abs(b[i,j])(function reference)
sum{i in S diff T} alpha[i] * b[i,j](iterated expression)
if i in I then 2 * p else q[i+1](conditional expression)
(b[i,j] + .5 * c)(parenthesized expression)

More general numeric expressions containing two or more primary numeric expressions may be constructed by using certain arithmetic operators.

Examples

j+1
2 * a[i-1,j+1] - b[i,j]
sum{j in J} a[i,j] * x[j] + sum{k in K} b[i,k] * x[k]
(if i in I then 2 * p else q[i+1]) / (a[i,j] + 1.5)

Numeric literals

If the primary numeric expression is a numeric literal, the resultant value is obvious.

Dummy indices

If the primary numeric expression is a dummy index, the resultant value is current value assigned to the dummy index.

Unsubscripted parameters

If the primary numeric expression is an unsubscripted parameter (which must be 0-dimensional), the resultant value is the value of the parameter.

Subscripted parameters

The primary numeric expression, which refers to a subscripted parameter, has the following syntactic form:

$name[i_1,i_2,\dots,i_n],$

where $name$ is the symbolic name of the parameter, $i_1$, $i_2, \dots, i_n$ are subscripts.

Each subscript must be a numeric or symbolic expression. The number of subscripts in the subscript list must be the same as the dimension of the parameter with which the subscript list is associated.

Actual values of subscript expressions are used to identify a particular member of the parameter that determines the resultant value of the primary expression.

Function references

In MathProg there are the following built-in functions which may be used in numeric expressions:

abs(x)absolute value
atan(x)trigonometric arctangent arctan x (in radians)
atan(yx)trigonometric arctangent arctan y/x (in radians)
card(x)cardinality (the number of elements) of set x
ceil(x)smallest integer not less than x (“ceiling of x”)
cos(x)trigonometric cosine cos x (in radians)
exp(x)base-e exponential $e^x$
floor(x)largest integer not greater than x (“floor of x”)
gmtime()the number of seconds elapsed since 00:00:00 Jan 1, 1970,
Coordinated Universal Time2
length(x)length of character string x
log(x)natural logarithm log x
log10(x)common (decimal) logarithm $\log_{10}x$
max$(x_1,x_2,\dots,x_n)$the largest of values $x_1, x_2, \dots, x_n$
min$(x_1,x_2,\dots,x_n)$the smallest of values $x_1, x_2, \dots, x_n$
round(x)rounding x to nearest integer
round(xn)rounding x to n fractional decimal digits
sin(x)trigonometric sine sin x (in radians)
sqrt(x)square root $\sqrt{x}$
str2time(sf)converting character string s to calendar time3
trunc(x)truncating x to nearest integer
trunc(xn)truncating x to n fractional decimal digits
Irand224()pseudo-random integer uniformly distributed in $[0,2^{24})$
Uniform01()pseudo-random number uniformly distributed in $[0,1)$
Uniform(ab)pseudo-random number uniformly distributed in [ab)
Normal01$()$Gaussian pseudo-random variate with $\mu=0$ and $\sigma=1$
Normal$(\mu,\sigma)$Gaussian pseudo-random variate with given $\mu$ and $\sigma$

Arguments of all built-in functions, except card, length, and str2time, must be numeric expressions. The argument of card must be a set expression. The argument of length and both arguments of str2time must be symbolic expressions.

The resultant value of the numeric expression, which is a function reference, is the result of applying the function to its argument(s).

Note that each pseudo-random generator function have a latent argument (i.e. some internal state), which is changed whenever the function has been applied. Thus, if the function is applied repeatedly even to identical arguments, due to the side effect different resultant values are always produced.

Iterated expressions

Iterated numeric expression is a primary numeric expression, which has the following syntactic form:

iterated-operator indexing-expression integrand

where iterated-operator is the symbolic name of the iterated operator to be performed (see below), indexing expression is an indexing expression which introduces dummy indices and controls iterating, integrand is a numeric expression that participates in the operation.

In MathProg there are four iterated operators, which may be used in numeric expressions:

sumsummation$\displaystyle\sum_{(i_1,\dots,i_n)\in\Delta}x(i_1,\dots,i_n)$
prodproduction$\displaystyle\prod_{(i_1,\dots,i_n)\in\Delta}x(i_1,\dots,i_n)$
minminimum$\displaystyle\min_{(i_1,\dots,i_n)\in\Delta}x(i_1,\dots,i_n)$
maxmaximum$\displaystyle\max_{(i_1,\dots,i_n)\in\Delta}x(i_1,\dots,i_n)$

where $i_1, \dots, i_n$ are dummy indices introduced in the indexing expression, $\Delta$ is the domain, a set of $n$-tuples specified by the indexing expression which defines particular values assigned to the dummy indices on performing the iterated operation, $x(i_1,\dots,i_n)$ is the integrand, a numeric expression whose resultant value depends on the dummy indices.

The resultant value of an iterated numeric expression is the result of applying of the iterated operator to its integrand over all n-tuples contained in the domain.

Conditional expressions

Conditional numeric expression is a primary numeric expression, which has one of the following two syntactic forms:

if b then x else y

if b then x

where b is an logical expression, x and y are numeric expressions.

The resultant value of the conditional expression depends on the value of the logical expression that follows the keyword if. If it takes on the value true, the value of the conditional expression is the value of the expression that follows the keyword then. Otherwise, if the logical expression takes on the value false, the value of the conditional expression is the value of the expression that follows the keyword else. If the reduced form of the conditional expression is used and the logical expression takes on the value false, the resultant value of the conditional expression is zero.

Parenthesized expressions

Any numeric expression may be enclosed in parentheses that syntactically makes it primary numeric expression.

Parentheses may be used in numeric expressions, as in algebra, to specify the desired order in which operations are to be performed. Where parentheses are used, the expression within the parentheses is evaluated before the resultant value is used.

The resultant value of the parenthesized expression is the same as the value of the expression enclosed within parentheses.

Arithmetic operators

In MathProg there are the following arithmetic operators, which may be used in numeric expressions:

+ xunary plus
- xunary minus
x + yaddition
x - ysubtraction
x less ypositive difference (if x < y then 0 else x − y)
x * ymultiplication
x / ydivision
x div yquotient of exact division
x mod yremainder of exact division
x ** y, x ^ yexponentiation (raise to power)

where x and y are numeric expressions.

If the expression includes more than one arithmetic operator, all operators are performed from left to right according to the hierarchy of operations (see below) with the only exception that the exponentiaion operators are performed from right to left.

The resultant value of the expression, which contains arithmetic operators, is the result of applying the operators to their operands.

Hierarchy of operations

The following list shows the hierarchy of operations in numeric expressions:

OperationHierarchy
Evaluation of functions (abs, ceil, etc.)1st
Exponentiation (**, ^)2nd
Unary plus and minus (+, -)3rd
Multiplication and division (*, /, div, mod)4th
Iterated operations (sum, prod, min, max)5th
Addition and subtraction (+, -, less)6th
Conditional evaluation (if $\dots$ then $\dots$ else)7th

This hierarchy is used to determine which of two consecutive operations is performed first. If the first operator is higher than or equal to the second, the first operation is performed. If it is not, the second operator is compared to the third, etc. When the end of the expression is reached, all of the remaining operations are performed in the reverse order.


Footnotes

(2)

For details See Obtaining current calendar time.

(3)

For details See Converting character string to calendar time.