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

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 ?

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?

1 Like

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?

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

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

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]