Feel++ expressions

1. Using Mathematical expressions

Mathematical expressions are described as strings in C++, python or json files. An expression is composed of symbols and operations acting on them.

Available standard operations and functions are :

  • +, -, *, / addition, substraction, multiplication and division respectively

  • ^ given two real numbers x and y, x^y compute x` to the power y

  • log

  • exp

  • sin

  • cos

  • tan

  • asin

  • acos

  • atan

  • sinh

  • cosh

  • tanh

  • asinh

  • acosh

  • atanh

  • atan2

  • sqrt

  • mod: given integers x and y, compute mod(x,y)

  • min, max: given 2 real numbers x and y, min(x,y) and max(x,y) compute the minimum and maximym respectively.

  • lgamma: Evaluation of lgamma(x), the natural logarithm of the Gamma function.

  • tgamma: Evaluation of tgamma(x), the true Gamma function.

2. Additional Mathematical functions

2.1. ceil

Description

ceil(v) computes the smallest integer value not less than arg.

Keyword

ceil

Math

Given real number \(x\), integer \(m\) and the set of integers \(\mathbb{Z}\), ceil can be defined by the equation

\[\lceil x \rceil=\min \{m\in\mathbb{Z}\mid m\ge x\}.\]
C++ example
auto e = expr("ceil(u):u");
json example
"expr":"ceil(u):u";

2.2. floor

Description

floor(v) computes the largest integer value not greater than arg.

Keyword

floor

Math

Given real number \(x\), integer \(m\) and the set of integers \(\mathbb{Z}\), floor can be defined by the equation

\[\lfloor x \rfloor=\max \{m\in\mathbb{Z}\mid m\le x\},\]
C++ example
auto e = expr("floor(u):u");
json example
"expr":"floor(u):u";

2.3. fract

Description

fract(v) compute the fractional part of the argument. This is calculated as \(v - floor(v)\).

Keyword

fract

Math

The fractional part is the sawtooth function, denoted by \(\{x\}\) for real \(x\) and defined by the formula

\[\{x\} = x - \lfloor x \rfloor\]

For all \(x\), we have

\[0 \leq \{ x \} < 1\]
C++ example
auto e = expr("fract(u):u");
json example
"expr":"fract(u):u";

2.4. clamp

Description

clamp(v,lo,hi) If v compares less than lo, returns lo; otherwise if hi compares less than v, returns hi; otherwise returns v.

Keyword

clamp

C++ example
auto e = expr("clamp(u,0,1):u");
json example
"expr":"clamp(u,0,1):u";

2.5. Modulo

Description

mod(x,y) compute the modulo operation: it returns the remainder or signed remainder of the division \(x/y\).

Keyword

mod

auto e = expr("mod(u,v):u:v");
e.setParameterValues( { { "u", 2 }, { "v", 1 }} );
CHECK( std::abs( e.evaluate()(0,0) ) < 1e-12 ) << "Error in modulo";
e.setParameterValues( { { "u", 3 }, { "v", 6 }} );
CHECK( std::abs( e.evaluate()(0,0) - 3) < 1e-12 ) << "Error in modulo";
e.setParameterValues( { { "u", 6.1 }, { "v", 3 }} );
CHECK( std::abs( e.evaluate()(0,0) - 0.1) < 1e-12 ) << "Error in modulo";
}

2.6. sign

Description

sign(v) returns 1 if v positive, -1 if negative, 0 if v is zero. This is calculated as \((0 < v)- (v < 0)\).

Keyword

sign

auto e = expr("sign(u):u");

3. Mappings

3.1. mapabcd

mapabcd is the function \(f\) that allows to map \([a,b]\) to \([c,d]\), it is defines as follows

\[f(t) = c + \left(\frac{d-c}{b-a}\right)(t-a)\]

with \(f(a)=c\) and \(f(b)=d\).

auto e = expr("mapabcd(t,1,2,-1,1):t");
e.setParameterValues( { { "t", 1 } } );
CHECK( std::abs(e.evaluate()(0,0)- (-1)) < 1e-12 ) << "Invalid mapping";
e.setParameterValues( { { "t", 2 } } );
CHECK( std::abs( e.evaluate()(0,0)- 1 ) < 1e-12 ) << "Invalid mapping";
e.setParameterValues( { { "t", 1.5 } } );
CHECK( std::abs( e.evaluate()(0,0) ) < 1e-12 ) << "Invalid mapping";

4. Step functions

A function \(f\colon \mathbb{R} \rightarrow \mathbb{R}\) is called a step function if it can be written as

\[f(x) = \sum\limits_{i=0}^n \alpha_i \chi_{A_i}(x), \mbox{ for all real numbers } x.\]

where \(n\ge 0\), \(\alpha_i\) are real numbers, \(A_i\) are intervals, and \(\chi_A\) is the indicator function of \(A\):

\[\chi_A(x) = \begin{cases} 1 & \text{if } x \in A \\ 0 & \text{if } x \notin A \\ \end{cases}\]

4.1. step1

Description

step1(u,edge) return 0 is u is strictly less than edge, 1 otherwise.

Keyword

step1

C++ example
auto e = expr("step1(u,edge):u:edge");
json example
"expr":"step1(u,edge):u:edge";

4.2. smoothstep

Description

smoothstep(u,lo,hi) is calculated as follows:

\[\mbox{smoothstep}(x,lo,hi)=\left\{\begin{array}{ll}lo & x \leq lo \\ 3 x^{2}-2 x^{3} & lo \leq x \leq hi \\ hi & hi \leq x\end{array}\right.\]

smoothstep uses the clamp function

Keyword

smoothstep

C++ example
auto e = expr("smoothstep(u,lo,hi):u:lo:hi");
json example
"expr":"smoothstep(u,lo,hi):u:lo:hi"

4.3. triangle

4.4. rectangle

4.5. pulse

4.6. sinwave