# Retrieve all variables appearing in a symbolic expression

**URL:** https://discourse.julialang.org/t/retrieve-all-variables-appearing-in-a-symbolic-expression/123846
**Category:** General Usage
**Created:** [December 14, 2024, 10:20pm UTC](https://discourse.julialang.org/t/retrieve-all-variables-appearing-in-a-symbolic-expression/123846 "2024-12-14T22:20:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)
#### Post date: [December 14, 2024, 10:20pm UTC](https://discourse.julialang.org/t/retrieve-all-variables-appearing-in-a-symbolic-expression/123846/1 "2024-12-14T22:20:35Z")

</div>

I would like to get all the variables involved in an expression, even the implicit ones. For example, if you do

```julia
using Symbolics
@variables x y u(x, y)
println(Symbolics.get_variables(u))

```

You get `1-element Vector{SymbolicUtils.BasicSymbolic}: u(x, y)`. However, i would like to get as a result a 3-element Vector with `u, x` and `y`.

Is there a way to retreive all variables involved in u? (namely, also x and y)

Thanks!

---

<div class="post-metadata">

### Author: ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)
#### Post date: [December 15, 2024, 5:10pm UTC](https://discourse.julialang.org/t/retrieve-all-variables-appearing-in-a-symbolic-expression/123846/2 "2024-12-15T17:10:37Z")

</div>

> [@elvispy](#):
>
> Is there a way to retreive all variables involved in u? (namely, also x and y)

To extract all variables from a symbolic expression, including implicit ones, you may want to use the recursive approach:

1. Start by using `get_variables` to get the top-level variables (e.g., `u(x, y)`).
2. Extract the “ops” (e.g., `u`) with `operation` and its “args” (e.g., `x` and `y`) with `arguments`.
3. Recursively apply this process to both the ops and its args to collect all the components.

```julia
using Symbolics

using Symbolics: get_variables, isterm, operation, arguments

function all_vars(expr)
    vars = get_variables(expr)
    res = Set(vars)
    for v in vars
        if isterm(v)
            res = union(res, all_vars(operation(v)))
            for arg in arguments(v)
                res = union(res, all_vars(arg))
            end
        end
    end
    return res
end

```

```julia
julia> @variables x, y, u(x, y);
julia> all_vars(u)
Set{Any} with 4 elements:
  u
  u(x, y)
  y
  x

```

---

<div class="post-metadata">

### Author: ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)
#### Post date: [December 15, 2024, 5:35pm UTC](https://discourse.julialang.org/t/retrieve-all-variables-appearing-in-a-symbolic-expression/123846/3 "2024-12-15T17:35:52Z")

</div>

Nice, thanks!
