# Substitute powers and powers of fractions

**URL:** https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641
**Category:** General Usage
**Tags:** symbolics, symbolic-utils
**Created:** [May 6, 2022, 10:03pm UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641 "2022-05-06T22:03:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kjellqvist](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@kjellqvist](https://discourse.julialang.org/u/kjellqvist)
#### Post date: [May 6, 2022, 10:03pm UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641/1 "2022-05-06T22:03:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [May 7, 2022, 1:53am UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641/2 "2022-05-07T01:53:50Z")

</div>

That seems like a bug to me. Consider filing an issue at [`Symbolics.jl`](https://github.com/JuliaSymbolics/Symbolics.jl/issues). @ChrisRackauckas and @shashi may have more insight about this of course.

---

<div class="post-metadata">

### Author: ![kjellqvist](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@kjellqvist](https://discourse.julialang.org/u/kjellqvist)
#### Post date: [May 9, 2022, 8:13pm UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641/3 "2022-05-09T20:13:07Z")

</div>

Thanks, I will file an issue.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [May 9, 2022, 8:28pm UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641/4 "2022-05-09T20:28:47Z")

</div>

The MWE

```julia
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

```julia
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.

---

<div class="post-metadata">

### Author: ![shashi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shashi/32/1824_2.png) [@shashi](https://discourse.julialang.org/u/shashi)
#### Post date: [June 22, 2022, 2:36am UTC](https://discourse.julialang.org/t/substitute-powers-and-powers-of-fractions/80641/5 "2022-06-22T02:36:22Z")

</div>

Symbolics performs subtree matching to subsitute. That is why it does not work for (1/(x^2)) here. This type of general substitution can get be complicated to implement as you need to search all possible rewrites of an expression to figure out if any one of them will allow substitution, but maybe there are simple but effective things we can do to start with.
