Is there a way of testing a symbolic expression to see if it contains a given variable other than seeing if the derivative is zero. For example test (r*sin(th))^2
to see if it contains th
. Is there a way of looking at the expression tree to do this?
I don’t know if you found an answer for Symbolics specifically, but here’s how I’d do it for a plain Expr
:
julia> begin
check_var(expr::Expr, var::Symbol) = any(sub_expr -> check_var(sub_expr, var), expr.args)
check_var(candidate::Symbol, var::Symbol) = candidate === var
check_var(token, _) = false # ignore numbers, strings, etc
end
check_var (generic function with 3 methods)
julia> check_var(
:((r*sin(th))^2),
:th
)
true