## `in` -- membership

#### Introduction

`x` in set is the ``element of'' relation. Further, the keyword `in` may also be used in combination with the keywords `for` and `$`, where it means ``iterate over all operands.''

#### Call(s)

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

#### 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

### Overloadable:

`x`, `set`

#### Returns

`x in set` returns an expression of type `"`_or", or `"`_and", or `"`_equal", or `"_in"`.

#### Related Functions

`_seqin`, `bool`, `contains`, `for`, `has`, `is`

#### Details

- `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 5.

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

- For sets of type `DOM_SET`, set unions, differences and intersections, `x in set` is evaluated 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`, `in` returns a Boolean condition that is equivalent to `x` being a solution. Cf. example 2.
- If `set` is a `RootOf` expression, `in` returns a Boolean condition that is equivalent to `x` being a root of the corresponding equation. Cf. example 3.
- 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 4 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`.

#### Example 1

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

    >> x in {1, 2, 3}

                              x = 1 or x = 2 or x = 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`:

    >> 1 in {1, 2, 3}, bool(1 in {1, 2, 3}), is(1 in {1, 2, 3})

                        1 = 1 or 1 = 2 or 1 = 3, TRUE, TRUE

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

    >> x in {1, 2, 3} union A

                         x in A or x = 1 or x = 2 or x = 3

#### 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(x^2 = 2^x, x); 2 in %, bool(2 in %)

                                      2    x
                               solve(x  - 2  = 0, x)

                                    0 = 0, TRUE

#### 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);
       1 in r, bool(1 in r), 2 in r, bool(2 in r)

                                         2
                                 RootOf(x  - 1, x)

                             0 = 0, TRUE, 3 = 0, FALSE

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

                                   2          2
                            2 y - y  + (y - 1)  - 1 = 0

    >> expand(%)

                                       0 = 0

    >> delete r:

#### Example 4

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_)

                                    TRUE, TRUE

    >> assume(p, Type::Prime): is(p in Z_), is(p in Type::NonNegative)

                                    TRUE, TRUE

    >> unassume(p):

#### Example 5

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:

                                         1

                                         2

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

                              2       2  2       4
                             a  + 1, b  c  + 1, d  + 1

    >> delete y:

#### Changes

- new function
- [_in]

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000