Documentation

# `in`, `_in`

Membership

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.

## Syntax

```x in set
_in(`x`, `set`)
for y in object do body end_for
f(y) \$ y in object
```

## Description

`x in set` is the MuPAD® notation for the statement “x is a member of `set`.”

In conjunction with one of the keywords `for` or `\$`, the meaning changes to “iterate over all operands of the object”. See `for` and `\$` for details. Cf. Example 6.

Apart from the usage with `for` and `\$`, the statement ```x in object``` is equivalent to the function call ```_in(x, object)```.

`x in set` is just evaluated to itself. ```expand(x in set)``` tries to return an equivalent expression without using the operator `in`, as described in the following paragraphs.

For sets of type `DOM_SET`, set unions, differences and intersections, ```x in set``` is expanded to an equivalent Boolean expression of equations and expressions involving `in`. Cf. Example 1.

If `set` is a solution set of a single equation in one unknown, given by a symbolic call to `solve`, expanding `in` returns a Boolean condition that is equivalent to `x` being a solution. Cf. Example 2.

If `set` is a `RootOf` expression, expanding `in` returns a Boolean condition that is equivalent to `x` being a root of the corresponding equation. Cf. Example 3.

The function `bool` and every function that uses boolean evaluation can also handle many logical expressions involving `in`. Cf. Example 4.

The function `is` handles various logical statements involving `in`, including a variety of types for the parameter `set` which are not handled by `in` itself. Cf. Example 5 for a few typical cases.

Apart from the usual overloading mechanism by the first argument of an `in` call, `in` can be overloaded by its second argument, too. This argument must define the slot`"set2expr"` for this purpose. The slot will be called with the arguments ```set, x```.

## Examples

### Example 1

`x in {1, 2, 3}` is expanded into an equivalent statement involving `=` and `or`:

`expand(x in {1, 2, 3})`

The same happens if you replace `x` by a number, because Boolean expressions are only evaluated inside certain functions such as `bool` or `is`:

`expand(1 in {1, 2, 3}), bool(1 in {1, 2, 3}), is(1 in {1, 2, 3})`

If only some part of the expression can be simplified this way, the returned expression can contain unevaluated calls to `in`:

`expand(x in {1, 2, 3} union A)`

### Example 2

For symbolic calls to `solve` representing the solution set of a single equation in one unknown, `in` can be used to check whether a particular value lies in the solution set:

```solve(cos(x) + x = cos(2) + 2, x); expand(2 in %), bool(2 in %)```

### Example 3

`in` can be used to check whether a value is a member of the solution set represented by a `RootOf` expression:

```r := RootOf(x^2 - 1, x); expand(1 in r), bool(1 in r), expand(2 in r), bool(2 in r)```

`expand((y - 1) in RootOf(x^2 - 1 - y^2 + 2*y, x))`

`expand(%)`

`delete r:`

### Example 4

Expressions with operator `in` are boolean expressions: they can be used like equations or inequalities.

`if 2 in {2, 3, 5} then "ok" end`

### Example 5

The MuPAD function `is` can investigate membership of objects in infinite sets. It respects properties of identifiers:

`is(123 in Q_), is(2/3 in Q_)`

### Example 6

In conjunction with `for` and `\$`, ```y in object``` iterates y over all operands of the object:

```for y in [1, 2] do print(y) end_for:```

`y^2 + 1 \$ y in a + b*c + d^2`

`delete y:`

## Parameters

 `x` An arbitrary MuPAD object `set` A set or an object of set-like type `y` An identifier or a local variable (`DOM_VAR`) of a procedure `object`, `f(y)` Arbitrary MuPAD objects

## Return Values

`x in set` just returns the input.

` set`, `x`