Problem with subs and powers
Hello,
I currently encounter a problem using the subs function. When I run the following code,
@vars z
subs(z^3+z^2+z,z^3,3)
I get 3 + z + 3^(2/3) instead of either 3 + 3^(2/3) + 3^(1/3) or 3 + z^2 + z. Is there any trick to get the latter as output ?
Thanks in advance,
subs looks at terms and does not use a solver inside.
You'll have to do the following,
@vars z
subs(z^3+z^2+z,z,cbrt(Basic(3)))
Thank you very much for your quick reply ! This yields 3 + 3^(2/3) + 3^(1/3). How could I get 3 + z^2 + z as output instead ? Is there any way to isolate specific terms, meaning that
subs(z^3+z^2+z,z^4,3)
would return z^3+z^2+z, for example ?
Ah, thanks. That looks like a bug. I would expect, 3 + z^2 + z.
If you don't bother to use some tooling such as https://github.com/dfdx/Espresso.jl,
julia> using SymEngine, Espresso
julia> @vars z
(z,)
julia> poly = z^3+z^2+z
z + z^2 + z^3
julia> Espresso.subs(convert(Expr, poly), Dict(:(z^3)=>3))
:(z + z ^ 2 + 3)
julia> eval(ans)
3 + z + z^2
julia> typeof(ans)
Basic
It is a nice idea to go through Julia AST ! I had not thought about it. Thank you very much !