has

Tests whether a symbolic expression contains a given subexpression.

Category: Symbolic Computations in MATLAB > Symbolic Variables, Expressions, Functions, and Preferences

Syntax

tf = has(expr, sub)

Returns true when sub occurs inside expr.

Description

has searches the whole expression tree, so has(x^2 + y, y) is true even though y is only one term of a sum. It matches structure, not value, so has(2*x, x) is true while has(x^2, 4) is false.

A typical use is deciding whether an expression still depends on a variable before differentiating or solving for it.

Input arguments

expr
Scalar symbolic expression to search.
sub
Variable or subexpression to look for.

Output arguments

tf
Logical scalar.

Example

Does the expression depend on a variable

syms x y
has(x^2 + y, y)
has(sin(x)*2, y)
has(sin(x)*2, sin(x))
Output
ans = logical
   1
ans = logical
   0
ans = logical
   1

Notes

In MatLivre sub must be a scalar symbolic expression; passing a symbolic array as sub raises an error. expr may be an array.

See also

hasSymType children symvar isequal findSymType