Skip to main content

Functions defined piecewise

· One min read
Pantelis Sopasakis
Lecturer, QUB

Gradgen v0.5.2 brings functions defined piecewise using if_else. Moreover, gradgen allows the differentiation of these functions.

The user can define symbolic expressions of the following form

$$f = \begin{cases} f_1, & \text{ if } (\text{condition}) \\ f_2, & \text{ otherwise} \end{cases}$$

For example, the Heaviside functions with parameters $p$, that is $f(x) = 0$ if $x < p$ and $f(x) = 1$ if $x \geq p$ is defined as

x = SX.sym("x")
p = SX.sym("p")
f = if_else(0, 1, x < p)