# How to get subtree from symbolic expression? in ModelingToolkit.jl

**URL:** https://discourse.julialang.org/t/how-to-get-subtree-from-symbolic-expression-in-modelingtoolkit-jl/117070
**Category:** Modelling & Simulations
**Created:** [July 15, 2024, 8:04pm UTC](https://discourse.julialang.org/t/how-to-get-subtree-from-symbolic-expression-in-modelingtoolkit-jl/117070 "2024-07-15T20:04:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jogama](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jogama/32/49842_2.png) [@jogama](https://discourse.julialang.org/u/jogama)
#### Post date: [July 15, 2024, 8:04pm UTC](https://discourse.julialang.org/t/how-to-get-subtree-from-symbolic-expression-in-modelingtoolkit-jl/117070/1 "2024-07-15T20:04:56Z")

</div>

My apologies if this has already been addressed.

Let’s say we have

```julia
using ModelingToolkit
@variables t x(t) y(t) z(t)
ex = x * y + z

```

How should I extract the sub-expression `x * y` from `ex`? It’s not clear to me how I’d traverse the expression tree in the documentation, nor did I see any way to do this via filtering.

I don’t seek to build a function. I’m fine with the output being an expression or string `x * y`. I want to extract products to populate nodes in a graph later on. For now, all the expressions would just have sums and products, and I’d want to get all the products.

I could do `split(string(ex), ['+', '-'])`, and use this result, and am currently working on this route. I still wonder if I’m not seeing a better way.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [July 15, 2024, 11:34pm UTC](https://discourse.julialang.org/t/how-to-get-subtree-from-symbolic-expression-in-modelingtoolkit-jl/117070/2 "2024-07-15T23:34:47Z")

</div>

ModelingToolkit is built on Symbolics and SymbolicUtils, that is the place to look for low-level expression manipulation like this. Here is an example (not complete and minimally tested).

```julia
using Symbolics

function products(expr)
    ex = Symbolics.unwrap(expr)
    terms = arguments(ex)
    products = filter(terms) do t
        istree(t) && ((operation(t) == *) || (operation(t) == ^))
    end
end

```

```julia
julia> ex = x * y + z + z*x + y*y
z(t) + x(t)*y(t) + x(t)*z(t) + y(t)^2

julia> products(ex)
3-element Vector{Any}:
 x(t)*y(t)
 x(t)*z(t)
 y(t)^2

```

---

<div class="post-metadata">

### Author: ![jogama](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jogama/32/49842_2.png) [@jogama](https://discourse.julialang.org/u/jogama)
#### Post date: [July 16, 2024, 9:42pm UTC](https://discourse.julialang.org/t/how-to-get-subtree-from-symbolic-expression-in-modelingtoolkit-jl/117070/3 "2024-07-16T21:42:07Z")

</div>

Thank you; this is helpful. I hadn’t seen `Symbolics.unwrap` in the symbolics documentation, and I would have never thought of using `filter` with `operation` that way!
