Method on @sym: has (expr, subexpr)
Method on @sym: has (M, subexpr)

Naively test if an expression contains a subexpression.

Example:

syms x
has(x^2 + 3*x + 2, x^2)
  ⇒ ans =  1
has((x+1)*(x+2), x^2)
  ⇒ ans =  0

(Note has does not try to do any mathematics: it just checks whether expr as written contains subexpr.)

If the first argument is a matrix M, check if each element of the matrix contains subexpr:

M = [sym(1) sym(pi)/2; 2*sym(pi) 4];
has(M, sym(pi))
  ⇒ ans =
      0   1
      1   0

Caution: has does not do mathematics; it is just searching for subexpr. This can lead to confusing results, for example, has should not be used to check for for membership in a set:

A = finiteset(1, 2, -sym(pi));
has(A, -1)
  ⇒ ans =  1

Instead, see ‘@sym/ismember’.

See also: @sym/ismember.

Package: symbolic