# Mathematical expressions in Reflex

When a boundary condition accepts a mathematical expression such as sqrt(x^2+y^2)+z instead of just a constant value, the Reflex mathematical parser and evaluator is employed. A mathematical expression is a string that contains operators, variables and functions that work as described below.

## Operators

The available operators, in decreasing order of precedence, are

Operator

Example

Expression

Result

Exponentiation ^

$$2^3$$

2^3

8

Multiplication * and division /

$$4 \cdot 3/2$$

4*3/2

6

Addition + and subtraction -

$$4 + 3 - 2$$

4+3-2

5

Smaller than < and greater than >

$$2>1$$

2>1

1

Equal to = and different from !

$$2 \neq 1$$

2!1

1

Logical AND & and logical OR |

$$2>1 \wedge 3>2$$

2>1 & 3>2

1

• Spaces are ignored by the parser. They can be used (or not) to make the string clearer.

• All logical operators evaluate to zero (false) or one (true).

• All operations (even the logical ones) are made in double-precision floating-point algebra.

• There is no need to add an explicit decimal dot such as writing 2.0 instead of 2. All numbers are converted to floating point values.

• Scientific notation such as 1e3 can be used for numerical constants.

Parenthesis can be used to change the order of precedence in the usual way. They can be used to handle addition or subtraction of negative numbers as well. In general, Reflex’ mathematical parser behaves as any other parser one may encounter in any programming langauge:

Example

Expression

Result

$$1+2 \cdot 3$$

1+2*3

7

$$(1+2)\cdot 3$$

(1+2)*3

9

$$-2^3$$

-2^3

-8

$$-1-(-1)$$

-1-(-1)

0

$$-1+(-1)$$

-1+(-1)

-2

$$-1+(+1)$$

-1+(+1)

0

$$-1-(+1)$$

-1-(+1)

-2

$$\frac{1}{3}$$

1/3

0.333333

$$\left(1+\frac{1}{10^6}\right)^{10^6}$$

(1+1/1e6)^1e6

2.71828

$$\frac{1}{(1+\frac{1}{10^6})^{10^6}}$$

1/((1+1/1e6)^1e6)

0.36788

$$\left[\left(1+\frac{1}{10^6}\right)^{10^6}\right]^{-1}$$

((1+1/1e6)^1e6)^(-1)

0.36788

$$2^{0.5}$$

2^0.5

1.41421

$$2^{\frac{1}{2}}$$

2^(1/2)

1.41421

## Variables

Besides numerical constants, expression strings can contain references to variables which hold scalar values.

Variable name

Description

t

Current time of a transient or quasistatic simulation

x

Global spatial coordinate $$x$$

y

Global spatial coordinate $$y$$

z

Global spatial coordinate $$z$$

T

Temperature at the current spatial location $$(x,y,z)$$

pi

The $$\pi$$ constant

Also, for each coordinate system defined in the simulation, three extra variables are defined that hold the local coordinates as converted from the global spatial location $$(x,y,z)$$. For instance, if the coordinate system is named coord1 then the following extra variables are available, depending on the type of coord1:

Type

Local coordinate 1

Local coordinate 2

Local coordinate 3

Cartesian

x_coord1

y_coord1

z_coord1

Cylindrical

r_coord1

theta_coord1

z_coord1

Spherical

r_coord1

theta_coord1

phi_coord1

## Built-in functions

The built-in mathematical functions described below are available. Functions take one or more arguments between brackets separated by commas. Arguments can be sub-expressions as well:

Example

Expression

Result

$$\sqrt{2}$$

sqrt(2)

1.41421

$$\sqrt{3}$$

sqrt(3)

1.73205

$$\sqrt{\pi}$$

sqrt(pi)

1.77245

$$\sqrt{\pi^2}$$

sqrt(pi^2)

3.14159

$$\sqrt{\pi}^2$$

sqrt(pi)^2

3.14159

$$\sin \pi$$

sin(pi)

1.22465 $$\cdot 10^{-16}$$

$$\cos \pi$$

cos(pi)

-1

$$\sin \frac{\pi}{2}$$

sin(pi/2)

1

$$\cos \frac{\pi}{2}$$

cos(pi/2)

6.12323$$\cdot 10^{-17}$$

$$\sin 1$$

sin(1)

0.841471

$$\sqrt{1-\cos(1)^2}$$

sqrt(1-cos(1)^2)

0.841471

$$\sin(1)^2 + \cos(1)^2$$

sin(1)^2+cos(1)^2

1

$$\arctan \left[ \exp\left(-\frac{1}{2}\right), \log\left( \sqrt{2} \right) \right]$$

atan(exp(-1/2),log(sqrt(2)))

1.05167

$$\sinh(1+x^2) \mod \pi$$

mod(sinh(1+x^2), pi)

0.485268

$$\max \left[ 1, \sqrt{2}, \lfloor 1.9 \rfloor \right]$$

max(1,sqrt(2),floor(1.9))

1.41421

$$\begin{cases}10 & \text{if \sqrt{10}>\pi}\\ \pi^2 & \text{otherwise}\end{cases}$$

if(sqrt(10)>pi, 10, pi^2)

10

### abs

Returns the absolute value of the argument $$x$$.

abs(x) $$= \displaystyle |x|$$ ### acos

Computes the arc in radians whose cosine is equal to the argument $$x$$.

acos(x) $$= \displaystyle \arccos(x)$$ ### asin

Computes the arc in radians whose sine is equal to the argument $$x$$.

asin(x) $$= \displaystyle \arcsin(x)$$ ### atan

Computes, in radians, the arc tangent of the argument $$x$$.

atan(x) $$= \displaystyle \arctan(x)$$ ### atan2

Computes, in radians, the arc tangent of quotient $$y/x$$, using the signs of the two arguments to determine the quadrant of the result, which is in the range $$[-\pi,\pi]$$.

atan2(y,x) $$= \displaystyle \arctan(y/x)$$

### ceil

Returns the smallest integral value not less than the argument $$x$$.

ceil(x) $$= \displaystyle \lceil x \rceil$$ ### cos

Computes the cosine of the argument $$x$$, where $$x$$ is in radians. A cosine wave can be generated by passing as the argument $$x$$ a linear function of time such as $$\omega t+\phi$$, where $$\omega$$ controls the frequency of the wave and $$\phi$$ controls its phase.

cos(x) $$= \displaystyle \cos(x)$$ ### cosh

Computes the hyperbolic cosine of the argument $$x$$, where $$x$$ is in radians.

cosh(x) $$= \displaystyle \cosh(x)$$ ### equal

Checks if the two first expressions $$a$$ and $$b$$ are equal, up to the tolerance given by the third optional argument $$\epsilon$$. Default value for $$\epsilon = 10^{-9}$$.

equal(a, b, [eps]) $$= \displaystyle \begin{cases} 1 & \text{if |a-b|<\epsilon} \\ 0 & \text{otherwise} \end{cases}$$

### exp

Computes the exponential function the argument $$x$$, i.e. the base of the natural logarithm $$e$$ raised to the $$x$$-th power.

exp(x) $$= \displaystyle e^x$$ ### floor

Returns the largest integral value not greater than the argument $$x$$.

floor(x) $$= \displaystyle \lfloor x \rfloor$$ ### heaviside

Computes the zero-centered Heaviside step function of the argument $$x$$. If the optional second argument $$\delta$$ is provided, the discontinuous step at $$x=0$$ is replaced by a ramp starting at $$x=0$$ and finishing at $$x=\delta$$.

heaviside(x, [delta]) $$= \displaystyle \begin{cases} 0 & \text{if x < 0} \\ x / \delta & \text{if 0 < x < \delta} \\ 1 & \text{if x > \delta} \end{cases}$$ ### if

Performs a conditional testing of the first argument $$a$$, and returns either the second optional argument $$b$$ if $$a$$ is different from zero or the third optional argument $$c$$ if $$a$$ evaluates to zero. The comparison of the condition $$a$$ with zero is performed within the precision given by the optional fourth argument $$\epsilon$$. If the second argument $$c$$ is not given and $$a$$ is not zero, the function returns one. If the third argument $$c$$ is not given and $$a$$ is zero, the function returns zero. The default precision is $$\epsilon = 10^{-9}$$. Even though if is a logical operation, all the arguments and the returned value are double-precision floating point numbers.

if(a, [b], [c], [eps]) $$= \displaystyle \begin{cases} b & \text{if |a|<\epsilon} \\ c & \text{otherwise} \end{cases}$$

### limit

Limits the first argument $$x$$ to the interval $$[a,b]$$. The second argument $$a$$ should be less than the third argument $$b$$.

limit(x, a, b) $$= \displaystyle \begin{cases} a & \text{if x < a} \\ x & \text{if a \leq x \leq b} \\ b & \text{if x > b} \end{cases}$$

### log

Computes the natural logarithm of the argument $$x$$.

log(x) $$= \displaystyle \ln(x)$$ ### max

Returns the maximum of the arguments $$x_i$$ provided. Currently only maximum of ten arguments can be given.

max(x1, x2, [...], [x10]) $$= \displaystyle \max \Big (x_1, x_2, \dots, x_{10} \Big)$$

### min

Returns the minimum of the arguments $$x_i$$ provided. Currently only maximum of ten arguments can be given.

min(x1, x2, [...], [x10]) $$= \displaystyle \min \Big (x_1, x_2, \dots, x_{10} \Big)$$

### mod

Returns the remainder of the division between the first argument $$a$$ and the second one $$b$$. Both arguments may be non-integral.

mod(a, b) $$= \displaystyle a - \left\lfloor \frac{a}{b} \right\rfloor \cdot b$$

### not

Returns one if the first argument $$x$$ is zero and zero otherwise. The second optional argument $$\epsilon$$ gives the precision of the “zero” evaluation. If not given, default is $$\epsilon = 10^{-9}$$.

not(x, [eps]) $$= \displaystyle \begin{cases}1 &\text{if |x| < \epsilon} \\ 0 &\text{otherwise} \end{cases}$$

### random

Returns a random real number uniformly distributed between the first real argument $$x_1$$ and the second one $$x_2$$. If the third integer argument $$s$$ is given, it is used as the seed and thus repetitive sequences can be obtained. If no seed is provided, the current time is used.

random(x1, x2, [s]) $$= \displaystyle x_1 + r \cdot (x_2-x_1) \quad \quad 0 \leq r < 1$$

### round

Rounds the argument $$x$$ to the nearest integer. Halfway cases are rounded away from zero.

round(x) $$= \displaystyle \begin{cases} \lceil x \rceil & \text{if \lceil x \rceil - x < 0.5} \\ \lceil x \rceil & \text{if \lceil x \rceil - x = 0.5 \wedge x > 0} \\ \lfloor x \rfloor & \text{if x-\lfloor x \rfloor < 0.5} \\ \lfloor x \rfloor & \text{if x-\lfloor x \rfloor = 0.5 \wedge x < 0} \end{cases}$$ ### sawtooth_wave

Computes a sawtooth wave between zero and one with a period equal to one. As with the sine wave, a sawtooh wave can be generated by passing as the argument $$x$$ a linear function of time such as $$\omega t+\phi$$, where $$\omega$$ controls the frequency of the wave and $$\phi$$ controls its phase.

sawtooth_wave(x) $$= \displaystyle x - \lfloor x \rfloor$$ ### sgn

Returns minus one, zero or plus one depending on the sign of the first argument $$x$$. The second optional argument $$\epsilon$$ gives the precision of the “zero” evaluation. If not given, default is $$\epsilon = 10^{-9}$$.

sgn(x, [eps]) $$= \displaystyle \begin{cases}-1 &\text{if x \le -\epsilon} \\ 0 &\text{if |x| < \epsilon} \\ +1 &\text{if x \ge +\epsilon} \end{cases}$$ ### sin

Computes the sine of the argument $$x$$, where $$x$$ is in radians. A sine wave can be generated by passing as the argument $$x$$ a linear function of time such as $$\omega t+\phi$$, where $$\omega$$ controls the frequency of the wave and $$\phi$$ controls its phase.

sin(x) $$= \displaystyle \sin(x)$$ ### sinh

Computes the hyperbolic sine of the argument $$x$$, where $$x$$ is in radians.

sinh(x) $$= \displaystyle \sinh(x)$$ ### sqrt

Computes the positive square root of the argument $$x$$.

sqrt(x) $$= \displaystyle +\sqrt{x}$$ ### square_wave

Computes a square function between zero and one with a period equal to one. The output is one for $$0 < x < 1/2$$ and zero for $$1/2 \leq x < 1$$. As with the sine wave, a square wave can be generated by passing as the argument $$x$$ a linear function of time such as $$\omega t+\phi$$, where $$\omega$$ controls the frequency of the wave and $$\phi$$ controls its phase.

square_wave(x) $$= \displaystyle \begin{cases} 1 & \text{if x - \lfloor x \rfloor < 0.5} \\ 0 & \text{otherwise} \end{cases}$$ ### tan

Computes the tangent of the argument $$x$$, where $$x$$ is in radians.

tan(x) $$= \displaystyle \tan(x)$$ ### tanh

Computes the hyperbolic tangent of the argument $$x$$, where $$x$$ is in radians.

tanh(x) $$= \displaystyle \tanh(x)$$ ### triangular_wave

Computes a triangular wave between zero and one with a period equal to one. As with the sine wave, a triangular wave can be generated by passing as the argument $$x$$ a linear function of time such as $$\omega t+\phi$$, where $$\omega$$ controls the frequency of the wave and $$\phi$$ controls its phase.

triangular_wave(x) $$= \displaystyle \begin{cases} 2 \cdot (x - \lfloor x \rfloor) & \text{if x - \lfloor x \rfloor < 0.5} \\ 2 \cdot [1-(x - \lfloor x \rfloor)] & \text{otherwise} \end{cases}$$ ## User-provided functions

If the Reflex input also contains user-defined functions, such as

• $$f(\xi)$$ defined by an expression

{
"function_name": "f",
"function_type": "Expression",
"function": {
"arguments": [ "xi" ],
"expression": "heaviside(xi-0.25)*(xi-0.25)^2"
}
}

• $$g(\xi)$$ as a linear interpolation of one-dimensional point-wise data

{
"function_name": "g",
"function_type": "Interpolation",
"function": {
"domain_values": [
{
"values": [0, 0.1,  0.2, 0.3,  0.4, 0.6, 0.8, 10 ]
}
],
"range_values":   [0, 0.0, 0.25, 0.5, 0.75,   1, 0.5, 0 ],
"interpolation_type": "LinearInterpolation",
"interpolation": {}
}
}

• $$h(x,y,z)$$ with multi-dimensional data from a CSV interpolated using a Shepard-based scheme

{
"function_name": "h",
"function_type": "Interpolation",
"domain_dimension": 3,
"function": {
"file_name": "TempFunction.csv",
"interpolation_type": "ShepardInterpolation",
"interpolation": {
"power": 4
}
}
}


In these cases, the functions above might be referred to as f(t/100), g((T-20)/100) or h(x_coord1,y_coord1,z_coord1). The number of arguments ought to match the function definition (there can be no optional arguments) or otherwise the parser will issue a syntax error and Reflex will not run. But apart from that, the arguments can be any other valid mathematical expression, which will we evaluated before calling the actual user-defined function. These user-provided functions can be then used as a part of a more general expression like 1+f(t/100) or sqrt(1-g((T-20)/100)^2) or (h(x_coord1,y_coord1,z_coord1)-32)*5/9.