Defining functions with boolean tests: ifte ?: when

### 5.1.3 Defining functions with boolean tests: ifte ?: when

You can use boolean tests to define functions not given by a single simple formula. Notably, you can use the ifte command or ?: operator to define piecewise-defined functions.

- ifte takes three arguments:
  - condition, a boolean condition.
  - true-result, the result to return if condition is true.
  - false-result, the result to return if condition is false.
- ifte(condition, true-result, false-result) returns true-result if condition is true and returns false-result if condition if false.

#### Example

You can define your own absolute value function with:
Input:

myabs(x):= ifte(x >= 0, x, -1*x)

Afterwards, entering:
Input:

myabs(-4)

will return:

4

However, myabs will return an error if it can’t evaluate the condition.
Input:

myabs(x)

Output:

Ifte: Unable to check test Error: Bad Argument Value

The ?: construct behaves similarly to ifte, but is structured differently and doesn’t return an error if the condition can’t be evaluated.

- The ?: construct takes three arguments:
  - condition, a boolean condition.
  - true-result, the result to return if condition is true.
  - false-result, the result to return if condition is false.
- condition? true-result: false-result returns true-result if condition is true and returns false-result if condition if false.

#### Example

You can define your absolute value function with

myabs(x):= (x >= 0)? x: -1*x

If you enter

myabs(-4)

you will again get

4

but now if the conditional can’t be evaluated, you won’t get an error.
Input:

myabs(x)

Output:

((x >= 0)? x: -x)

The when and IFTE commands are prefixed synonyms for the ?: construct.

- when (and IFTE) take three arguments:
  - condition, a boolean condition.
  - true-result, the result to return if condition is true.
  - false-result, the result to return if condition is false.
- when(condition, true-result, false-result) (and IFTE(condition, true-result, false-result)) return true-result if condition is true and returns false-result if condition if false.

(condition)? true-result: false-result
when(condition, true-result, false-result)

and

IFTE(condition, true-result, false-result)

all represent the same expression.

If you want to define a function with several pieces, it may be simpler to use the piecewise function.

- piecewise takes an unspecified (odd) number of arguments:
  - cond1, return1, cond2, return2, …, condn, returnn, an arbitrary number of pairs of conditions and corresponding return values.
  - default, a result to return if none of the conditions are true.
- piecewise(cond1, return1, …, condn, returnn, default) returns returnk if condk is the first true condition, or default if none of the conditions are true.

#### Example

To define

f(x) = |
⎧ ⎪ ⎨ ⎪ ⎩ |
−2 | if x < −2
3x+4 | if −2 ≤ x < −1
1 | if −1 ≤ x < 0
x + 1 | if x ≥ 0

you can enter:
Input:

f(x):= piecewise(x < -2, -2, x < -1, 3*x+4, x < 0, 1, x + 1)