# Working with Symbolics.jl... how to extract terms from a sum etc?

**URL:** <https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [March 10, 2023, 8:05pm UTC](https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886 "2023-03-10T20:05:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [March 10, 2023, 8:05pm UTC](https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886/1 "2023-03-10T20:05:44Z")

</div>

I have a simple example I’m trying to construct in which I define an expression in h that has a variable f, substitute f for a linear function of h and then expand that to get a polynomial in h. So far, so good… Now what I’d like to do is take this sum and extract each term and look at the size of each term given some numerical values for the variables.

How can I split a polynomial into individual terms?

Also how can I tell symbolics to collect all the powers of h ?

```julia
using Symbolics
@variables w f h k

wexpr = 1.8 * f * h^3 + 1.0 *(1-f)*h^3
fexpr = 0.15 + k * (h-.6)
wexpr = substitute(wexpr,Dict([f => fexpr]))
wexpr = simplify(wexpr,expand=true)

```

I want to show that the h^4 term is small relative to the h^3 terms, and that the derivative of that term is small relative to the derivatives of the h^3 terms.

In something like Maxima I’d do this no problem and I know how to accomplish it. Is there a simple way to split this up and evaluate each term separately in Julia?

---

<div class="post-metadata">

**Author:** ![seismobassoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/seismobassoon/32/211478_2.png) [@seismobassoon](https://discourse.julialang.org/u/seismobassoon)\
**Post date:** [September 3, 2024, 10:13am UTC](https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886/2 "2024-09-03T10:13:04Z")

</div>

No reply here? I’m having a hard time here working with simplify. Even with expand=true, I cannot get an expression with individual terms, e.g. I get this kind of non-sense : (1, 2, (-(12//1)_μ₁ˏ₁ˏ₁ˏ₁ + (5//1)_(Δx₀^2)_ρ₁ˏ₁ˏ₁ˏ₁_(ω₁ˏ₁ˏ₁ˏ₁^2)) / (6Δx₀))

Anyone who can help us?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 3, 2024, 10:27am UTC](https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886/3 "2024-09-03T10:27:10Z")

</div>

It’s a bit unclear what you’re aiming for, but from the original post I assume this could help:

```julia
julia> wexpr = rationalize(1.8) * f * h^3 + 1 *(1-f)*h^3
(9//5)*f*(h^3) + (1 - f)*(h^3)

julia> fexpr = rationalize(0.15) + k * (h-rationalize(0.6))
(3//20) + (-(3//5) + h)*k

julia> wexpr = substitute(wexpr,Dict([f => fexpr]))
(9//5)*((3//20) + (-(3//5) + h)*k)*(h^3) + ((17//20) - (-(3//5) + h)*k)*(h^3)

julia> p = expand(wexpr)
(28//25)*(h^3) - (12//25)*(h^3)*k + (4//5)*(h^4)*k

julia> ch3=Symbolics.coeff(p,h^3)
(28//25) - (12//25)*k

julia> ch4=Symbolics.coeff(p,h^4)
(4//5)*k

julia> ch3/ch4 |> simplify
((28//25) - (12//25)*k) / ((4//5)*k)

julia> Symbolics.derivative(ch3,k)/Symbolics.derivative(ch4,k)
-3//5

```

---

<div class="post-metadata">

**Author:** ![Yassin\_ElBedwihy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yassin_elbedwihy/32/208506_2.png) [@Yassin\_ElBedwihy](https://discourse.julialang.org/u/Yassin_ElBedwihy)\
**Post date:** [September 4, 2024, 7:40am UTC](https://discourse.julialang.org/t/working-with-symbolics-jl-how-to-extract-terms-from-a-sum-etc/95886/4 "2024-09-04T07:40:43Z")

</div>

you can use Symbolics.arguments which for a given expression if the oper is sum it will give you all the summed arguments.

Say x + 1 returns [x, 1]
