# Substitute symbols in expression

**URL:** <https://discourse.julialang.org/t/substitute-symbols-in-expression/23099>\
**Category:** General Usage\
**Created:** [April 12, 2019, 11:21pm UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099 "2019-04-12T23:21:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [April 12, 2019, 11:21pm UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/1 "2019-04-12T23:21:42Z")

</div>

Is there any straight way or a function to substitute symbols in an expression?  
For example:

```julia
ex = :(x + x^2 + x^3)
substitute(ex, :x => :y) # yields :(y + y^2 + y^3)
substitute(ex, :x => 1.5) # yields :(1.5 + 1.5^2 + 1.5^3)

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 12, 2019, 11:35pm UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/2 "2019-04-12T23:35:43Z")

</div>

```julia
julia> function rep!(e, old, new)
           for (i,a) in enumerate(e.args)
               if a==old
                   e.args[i] = new
               elseif a isa Expr
                   rep!(a, old, new)
               end
               ## otherwise do nothing
           end
           e
       end
rep! (generic function with 1 method)

julia> rep!(:(x + x^2 + x^3), :x, :y)
:(y + y ^ 2 + y ^ 3)

julia> rep!(:(x + x^2 + x^3), :x, 1.5)
:(1.5 + 1.5 ^ 2 + 1.5 ^ 3)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 12, 2019, 11:46pm UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/3 "2019-04-12T23:46:39Z")

</div>

With the [MacroTools package](http://mikeinnes.github.io/MacroTools.jl) you can do

```julia
using MacroTools
ex = :(x + x^2 + x^3)
MacroTools.postwalk(ex -> ex == :x ? :y : ex, ex)
MacroTools.postwalk(ex -> ex == :x ? 1.5 : ex, ex)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 12, 2019, 11:54pm UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/4 "2019-04-12T23:54:24Z")

</div>

This is how you could do it with my [Reduce.jl](https://github.com/chakravala/Reduce.jl) package

```nohighlight
julia> using Reduce
Reduce (Free CSL version, revision 4590), 11-May-18 ...

julia> ex = :(x+x^2+x^3)
:(x + x ^ 2 + x ^ 3)

julia> rcall(:(x=y)); rcall(ex)
:((y ^ 2 + y + 1) * y)

julia> rcall(:(x=1.5)); rcall(ex)
:(57 / 8)

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 13, 2019, 12:11am UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/5 "2019-04-13T00:11:24Z")

</div>

Alternatively, you can also use the `Reduce.Algebra.sub` method,

```nohighlight
julia> using Reduce
Reduce (Free CSL version, revision 4590), 11-May-18 ...

julia> ex = :(x+x^2+x^3)
:(x + x ^ 2 + x ^ 3)

julia> Algebra.sub(:x=>:y,ex)
:((y ^ 2 + y + 1) * y)

julia> Algebra.sub(:x=>1.5,ex)
:(57 / 8)

```

which is closer to your original proposal and is probably better than my original example

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [April 13, 2019, 1:11am UTC](https://discourse.julialang.org/t/substitute-symbols-in-expression/23099/6 "2019-04-13T01:11:04Z")

</div>

Thanks all for the nice suggestions. I also found a native function:

```julia
Base.Cartesian.lreplace(:(x + x^2 + x^3), :x, 1)

```

but it does not work with float numbers.
