Substitute powers and powers of fractions

I’m a bit stuck on substitutions, for example:

@variables x y
eq = (1/x)^2
substitute(eq, Dict([1/x => y]))
julia> y^2

whereas

@variables x y
eq = 1/x^2
substitute(eq, Dict([1/x => y]))
julia> 1 / (x^2)

I was expecting the two to be equal. Further,

@variables x y z
eq = x/y
substitute(eq, Dict([1/y => z]))
julia> x/y

I was expecting x*z.
Substitution of monomials also doesn’t work the way I would expect:

eq = (x^2)^2
substitute(eq, Dict([x^2 => y]))
julia> x^4

I was expecting y^2. Is there other functionality that lets me do these substitutions? I’m trying to write some code that polynomializes nonlinear ODEs, and that relies on substitutions of this kind.

That seems like a bug to me. Consider filing an issue at Symbolics.jl. @ChrisRackauckas and @shashi may have more insight about this of course.

1 Like

Thanks, I will file an issue.

The MWE

using Symbolics
@variables x y
eq = (1/x)^2
@show substitute(eq, Dict([1/x => y]))
eq = 1/x^2
@show substitute(eq, Dict([1/x => y]))
eq = 1/x^2
@show substitute(eq, Dict([x => 1/y]))

yielding

substitute(eq, Dict([1 / x => y])) = y^2
substitute(eq, Dict([1 / x => y])) = 1 / (x^2)
substitute(eq, Dict([x => 1 / y])) = 1 / ((1 / y)^2)

indicates to me that this could be a matter of simplification vs. substitution. Are there certain rules in the CAS community which prescribe order of operations? Difficult, it seems (Knuth-Bendix, or so).

Edit: improving example.