You can use Xcas’s control structures to define functions not given by a single simple formula. Notably, you can use the ifte command or ? : operator to define piecewise-defined functions.
The ifte command takes three arguments; the first argument is a condition, the second argument tells the command what to return when the condition is true, and the third argument tells the command what to return when the condition is false. For example, you could define your own absolute value function with
Afterwards, for example, entering
However, this will return an error if it can’t evaluate the conditional. For example, if you enter
you will get the error
The ?: construct behaves similarly to ifte but is structured differently. Here, the condition comes first, followed by ?, then what to return if the condition is true, followed by the :, and then what to return if the condition is false. You could define your absolute value function with
If you enter
you will again get
but now if the conditional can’t be evaluated, you won’t get an error.
The when and IFTE commands are synonyms for the ? : construct;
all represent the same expression.
If you want to define a function with several pieces, it may be simpler to use the piecewise function. The arguments to this function are alternately conditions and results to return if the condition is true, with the last argument being what to return if none of the conditions are true. For example, to define the function given by
you can enter