Documentation

# `bool`

Boolean evaluation

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

For booleans in MATLAB®, see `logical`.

## Syntax

```bool(`b`)
```

## Description

`bool(b)` evaluates the Boolean expression `b`.

The function `bool` serves for reducing Boolean expressions to one of the Boolean constants `TRUE`, `FALSE`, or `UNKNOWN`.

Boolean expressions are expressions that are composed of equalities, inequalities, elementhood relations, and these constants, combined via the logical operators `and`, `or`, `not`.

The function `bool` evaluates all equalities and inequalities inside a Boolean expression to either `TRUE` or `FALSE`. The resulting logical combination of the Boolean constants is reduced according to the rules of the MuPAD® three state logic (see `and`, `or`, `not`).

### Note

Equations `x = y` and inequalities ```x <> y``` are evaluated syntactically by `bool`. It does not test equality in any mathematical sense.

### Note

Inequalities `x < y`, `x <= y` etc. can be evaluated by `bool` if and only if `x` and `y` are real numbers of type `Type::Real`. Otherwise, an error occurs.

`bool` evaluates all subexpressions of a Boolean expression before simplifying the result. The functions `_lazy_and`, `_lazy_or` provide an alternative: “lazy Boolean evaluation”.

There is no need to use `bool` in the conditional part of `if`, `repeat`, and `while` statements. Internally, these statements enforce Boolean evaluation by `_lazy_and` and `_lazy_or`. Cf. Example 5.

Use `simplify` with the option `logic` to simplify expressions involving symbolic Boolean subexpressions. Cf. Example 7.

`bool` is overloadable not only for domains, but also for function environments. This means that, if `f` evaluates to a function environment, then `bool(f(x1, …, xn))` returns ```f::bool( x1, …, xn )```, or an error if no slot `f::bool` exists.

The call `bool(x ~= y)` serves for comparing numerical values `x` and `y`. If both `x` and `y` can be converted to non-zero real or complex floating-point numbers, it is checked whether `float((x - y)/x)| < 10^(-DIGITS)` is satisfied. Thus, `TRUE` is returned if `x` and `y` coincide within the relative numerical precision set by `DIGITS`. For ```x = 0```, the criterion is `|float(y)| < 10^(-DIGITS)`. For `y = 0`, the criterion is ```|float(x)| < 10^(-DIGITS)```. If either `x` or `y` contains a symbolic object that cannot be converted to a real or complex floating point number, the function `bool` returns the value `UNKNOWN`.

## Examples

### Example 1

MuPAD realizes that 1 is less than 2:

`1 < 2 = bool(1 < 2)`
` `

Note that `bool` can fail to compare real numbers expressed symbolically:

`bool(sqrt(14) <= sqrt(2)*sqrt(7))`
```Error: Unable to evaluate to Boolean. [_leequal] ```

You can compare floating-point approximations. Alternatively, you can use `is`:

```bool(float(sqrt(14)) <= float(sqrt(2)*sqrt(7))), is(sqrt(14) <= sqrt(2)*sqrt(7))```
` `

### Example 2

The Boolean operators `and`, `or`, `not` do not evaluate equations and inequalities logically, and return a symbolic Boolean expression. Boolean evaluation and simplification is enforced by `bool`:

`a = a and 3 < 4`
` `
`bool(a = a and 3 < 4)`
` `

### Example 3

`bool` handles the special Boolean constant `UNKNOWN`:

```bool(UNKNOWN and 1 < 2), bool(UNKNOWN or 1 < 2), bool(UNKNOWN and 1 > 2), bool(UNKNOWN or 1 > 2)```
` `

### Example 4

`bool` must be able to reduce all parts of a composite Boolean expression to one of the Boolean constants. No symbolic Boolean subexpressions may be involved:

`b := b1 and b2 or b3: bool(b)`
```Error: Unable to evaluate to Boolean. [bool] ```
`b1 := 1 < 2: b2 := x = x: b3 := FALSE: bool(b)`
` `
`delete b, b1, b2, b3:`

### Example 5

There is no need to use `bool` explicitly in the conditional parts of `if`, `repeat`, and `while` statements. Note, however, that these structures internally use “lazy evaluation” via `_lazy_and` and `_lazy_or` rather than “complete Boolean evaluation” via `bool`:

`x := 0: if x <> 0 and sin(1/x) = 0 then 1 else 2 end`
` `

In contrast to “lazy evaluation”, `bool` evaluates all conditions. Consequently, a division by zero occurs in the evaluation of ```sin(1/x) = 0```:

`bool(x <> 0 and sin(1/x) = 0)`
```Error: Division by zero. [_invert] ```
`delete x:`

### Example 6

Note that `bool` does not operate recursively. The following calls are completely different, the first one comparing the expression `TRUE = TRUE` and the constant `TRUE` (syntactically), the second one comparing the result of another `bool`-call with `TRUE`:

```bool((TRUE = TRUE) = TRUE); bool(bool(TRUE = TRUE) = TRUE)```
` `
` `

Since `if`, `while` and similar constructs use the same Boolean evaluation internally, this also effects conditions in such clauses:

```if (is(a < b) = TRUE) or (3 = 3) then YES else NO end; if (is(a < b) or (3 = 3)) = TRUE then YES else NO end```
` `
` `

### Example 7

Expressions involving symbolic Boolean subexpressions cannot be processed by `bool`. However, `simplify` with the option `logic` can be used for simplification:

`(b1 and b2) or (b1 and (not b2)) and (1 < 2)`
` `
`simplify(%, logic)`
` `

## Parameters

 `b` A Boolean expression

## Return Values

`TRUE`, `FALSE`, or `UNKNOWN`.

`b`