Documentation

# has

Check if expression contains particular subexpression

## Syntax

``has(expr,subexpr)``

## Description

example

````has(expr,subexpr)` returns logical `1` (true) if `expr` contains `subexpr`. Otherwise, it returns logical `0` (false).If `expr` is an array, `has(expr,subexpr)` returns an array of the same size as `expr`. The returned array contains logical `1s` (true) where the elements of `expr` contain `subexpr`, and logical `0s` (false) where they do not.If `subexpr` is an array, `has(expr,subexpr)` checks if `expr` contains any element of `subexpr`.```

## Examples

### Check If Expression Contains Particular Subexpression

Use the `has` function to check if an expression contains a particular variable or subexpression.

Check if these expressions contain variable `z`.

```syms x y z has(x + y + z, z)```
```ans = logical 1```
`has(x + y, z)`
```ans = logical 0```

Check if `x + y + z` contains the following subexpressions. Note that `has` finds the subexpression ```x + z``` even though the terms `x` and `z` do not appear next to each other in the expression.

```has(x + y + z, x + y) has(x + y + z, y + z) has(x + y + z, x + z)```
```ans = logical 1 ans = logical 1 ans = logical 1```

Check if the expression `(x + 1)^2` contains `x^2`. Although `(x + 1)^2` is mathematically equivalent to the expression `x^2 + 2*x + 1`, the result is a logical `0` because `has` typically does not transform expressions to different forms when testing for subexpressions.

`has((x + 1)^2, x^2)`
```ans = logical 0```

Expand the expression and then call `has` to check if the result contains `x^2`. Because ```expand((x + 1)^2)``` transforms the original expression to ```x^2 + 2*x + 1```, the `has` function finds the subexpression `x^2` and returns logical `1`.

`has(expand((x + 1)^2), x^2)`
```ans = logical 1```

### Check If Expression Contains Any of Specified Subexpressions

Check if a symbolic expression contains any of subexpressions specified as elements of a vector.

If an expression contains one or more of the specified subexpressions, `has` returns logical `1`.

```syms x has(sin(x) + cos(x) + x^2, [tan(x), cot(x), sin(x), exp(x)])```
```ans = logical 1```

If an expression does not contain any of the specified subexpressions, `has` returns logical `0`.

```syms x has(sin(x) + cos(x) + x^2, [tan(x), cot(x), exp(x)])```
```ans = logical 0```

### Find Matrix Elements Containing Particular Subexpression

Using `has`, find those elements of a symbolic matrix that contain a particular subexpression.

First, create a matrix.

```syms x y M = [sin(x)*sin(y), cos(x*y) + 1; cos(x)*tan(x), 2*sin(x)^2]```
```M = [ sin(x)*sin(y), cos(x*y) + 1] [ cos(x)*tan(x), 2*sin(x)^2]```

Use `has` to check which elements of `M` contain `sin(x)`. The result is a matrix of the same size as `M`, with `1s` and `0s` as its elements. For the elements of `M` containing the specified expression, `has` returns logical `1s`. For the elements that do not contain that subexpression, `has` returns logical `0s`.

`T = has(M, sin(x))`
```T = 2×2 logical array 1 0 0 1```

Return only the elements that contain `sin(x)` and replace all other elements with `0` by multiplying `M` by `T` elementwise.

`M.*T`
```ans = [ sin(x)*sin(y), 0] [ 0, 2*sin(x)^2]```

To check if any of matrix elements contain a particular subexpression, use `any`.

`any(has(M(:), sin(x)))`
```ans = logical 1```
`any(has(M(:), cos(y)))`
```ans = logical 0```

### Find Vector Elements Containing Any of Specified Subexpressions

Using `has`, find those elements of a symbolic vector that contain any of the specified subexpressions.

```syms x y z T = has([x + 1, cos(y) + 1, y + z, 2*x*cos(y)], [x, cos(y)])```
```T = 1×4 logical array 1 1 0 1```

Return only the elements of the original vector that contain `x` or `cos(y)` or both, and replace all other elements with `0` by multiplying the original vector by `T` elementwise.

`[x + 1, cos(y) + 1, y + z, 2*x*cos(y)].*T`
```ans = [ x + 1, cos(y) + 1, 0, 2*x*cos(y)]```

### Use `has` for Symbolic Functions

If `expr` or `subexpr` is a symbolic function, `has` uses `formula(expr)` or `formula(subexpr)`. This approach lets the `has` function check if an expression defining the symbolic function `expr` contains an expression defining the symbolic function `subexpr`.

Create a symbolic function.

```syms x f(x) = sin(x) + cos(x);```

Here, `sin(x) + cos(x)` is an expression defining the symbolic function `f`.

`formula(f)`
```ans = cos(x) + sin(x)```

Check if `f` and `f(x)` contain `sin(x)`. In both cases `has` checks if the expression ```sin(x) + cos(x)``` contains `sin(x)`.

```has(f, sin(x)) has(f(x), sin(x))```
```ans = logical 1 ans = logical 1```

Check if `f(x^2)` contains `f`. For these arguments, `has` returns logical `0` (false) because it does not check if the expression `f(x^2)` contains the letter `f`. This call is equivalent to ```has(f(x^2), formula(f))```, which, in turn, resolves to ```has(cos(x^2) + sin(x^2), cos(x) + sin(x))```.

`has(f(x^2), f)`
```ans = logical 0```

### Check for Calls to Particular Function

Check for calls to a particular function by specifying the function name as the second argument. Check for calls to any one of multiple functions by specifying the multiple functions as a cell array of character vectors.

Integrate `tan(x^7)`. Determine if the integration is successful by checking the result for calls to `int`. Because `has` finds the `int` function and returns logical `1` (`true`), the integration is not successful.

```syms x f = int(tan(x^7), x); has(f, 'int')```
```ans = logical 1```

Check if the solution to a differential equation contains calls to either `sin` or `cos` by specifying the second argument as `{'sin','cos'}`. The `has` function returns logical `0` (`false`), which means the solution does not contain calls to either `sin` or `cos`.

```syms y(x) a sol = dsolve(diff(y,x) == a*y); has(sol, {'sin' 'cos'})```
```ans = logical 0```

## Input Arguments

collapse all

Expression to test, specified as a symbolic expression, function, equation, or inequality. Also it can be a vector, matrix, or array of symbolic expressions, functions, equations, and inequalities.

Subexpression to test for, specified as a symbolic variable, expression, function, equation, or inequality, or a character vector, or a cell array of character vectors. `subexpr` can also be a vector, matrix, or array of symbolic variables, expressions, functions, equations, and inequalities.

## Tips

• `has` does not transform or simplify expressions. This is why it does not find subexpressions like `x^2` in expressions like `(x + 1)^2`. However, in some cases `has` might find that an expression or subexpression can be represented in a form other than its original form. For example, `has` finds that the expression `-x - 1` can be represented as ```-(x + 1)```. Thus, the call `has(-x - 1, x + 1)` returns `1`.

• If `expr` is an empty symbolic array, `has` returns an empty logical array of the same size as `expr`.