SymEngine.jl icon indicating copy to clipboard operation
SymEngine.jl copied to clipboard

Problem with subs and powers

Open NormannR opened this issue 5 years ago • 5 comments

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,

NormannR avatar Feb 04 '20 17:02 NormannR

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)))

isuruf avatar Feb 04 '20 17:02 isuruf

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 ?

NormannR avatar Feb 04 '20 17:02 NormannR

Ah, thanks. That looks like a bug. I would expect, 3 + z^2 + z.

isuruf avatar Feb 04 '20 17:02 isuruf

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

mforets avatar Feb 04 '20 17:02 mforets

It is a nice idea to go through Julia AST ! I had not thought about it. Thank you very much !

NormannR avatar Feb 04 '20 18:02 NormannR