Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with subs and powers #194

Open
NormannR opened this issue Feb 4, 2020 · 5 comments
Open

Problem with subs and powers #194

NormannR opened this issue Feb 4, 2020 · 5 comments

Comments

@NormannR
Copy link

NormannR commented Feb 4, 2020

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,

@isuruf
Copy link
Member

isuruf commented Feb 4, 2020

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

@NormannR
Copy link
Author

NormannR commented Feb 4, 2020

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 ?

@isuruf
Copy link
Member

isuruf commented Feb 4, 2020

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

@mforets
Copy link
Contributor

mforets commented Feb 4, 2020

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

@NormannR
Copy link
Author

NormannR commented Feb 4, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants