Chris’ answer implies that it should work.
But… I am not in any way an expert on this but Symbolics always fascinates me, so I tried to find a way to do this and I thought it should work somehow recursively. This is the solution I created. It’s not complete and generic enough to work on all expressions, but it can easily be expanded (so I hope).
And maybe someone is motivated to show us a more Symbolics-Way of doing it.
using Symbolics
sr = @rule ( +(~~x) ) => ~~x
mr = @rule ( *(~~x) ) => ~~x
pr = @rule ( ^(~x,~y) ) => [~x,~y]
function rationalize!(a)
for index in eachindex(a)
if typeof(a[index]) <: AbstractFloat || typeof(a[index]) <: ComplexF64
a[index] = Base.rationalize(a[index])
end
end
a
end
function decompose_compose(expr)
if typeof(expr) <: SymbolicUtils.Sym || typeof(expr) <: Number
return expr
end
if ! isnothing(sr(expr))
subexpr = sr(expr)
compose=sum
elseif ! isnothing(mr(expr))
subexpr = mr(expr)
compose=prod
elseif ! isnothing(pr(expr))
subexpr = pr(expr)
compose=^
else
display(expr)
error("no rule defined")
end
subexpr = rationalize!(subexpr)
subexpr = decompose_compose.(subexpr)
(compose == ^) ? compose(subexpr...) : compose(subexpr)
end
@syms x
expr=(0.014378665475956662 - 0.0645731363698366im)*((72 + 132x + 2262(x^4) + 6743(x^5) - 754(x^2) - 234(x^6) - 1641(x^3) - 9180(x^7) - 5400(x^8))^-1) + (-0.0995313498923154 + 0.07922575111068406im)*x*((72 + 132x + 2262(x^4) + 6743(x^5) - 754(x^2) - 234(x^6) - 1641(x^3) - 9180(x^7) - 5400(x^8))^-1)
decompose_compose(expr)