# Symbolics substitute and simplify in a polynomial

**URL:** <https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 6, 2023, 7:47pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160 "2023-02-06T19:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [February 6, 2023, 7:47pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160/1 "2023-02-06T19:47:27Z")

</div>

This is my first time using Symbolics.

I have a polynomial that I want to substitute a variable and then simplify the result as shown.

```julia
julia> dcs_P1 = 229.50752 - 321.64369 * r - 121.01008 * r^2 + 53.15226 * r^3
229.50752 + 53.15226(r^3) - 121.01008(r^2) - 321.64369r

julia> dcs_P1ₙ = simplify.(substitute.(dcs_P1, (Dict(r => rₙ - 0.0475),)))
(244.785595275 + 53.15226((rₙ - 0.0475)^3) - 121.01008((rₙ - 0.0475)^2) - 321.64369rₙ,)

```

The simplification works for the linear part, but not the squared or cubic part. If I use Horner’s method, it does not help.

```julia
julia> dcs_P1 = 229.50752 - r*(321.64369 - r*(121.01008 + r*53.15226))
229.50752 - r*(321.64369 - r*(121.01008 + 53.15226r))

julia> dcs_P1ₙ = simplify.(substitute.(dcs_P1, (Dict(r => rₙ - 0.0475),)))
(229.50752 - (rₙ - 0.0475)*(321.64369 - (rₙ - 0.0475)*(118.48534765000001 + 53.15226rₙ)),)

```

Is there something I am missing or is this functionality yet to be developed?

I have MathCad 14 which uses Maple for its Symbolic engine and it solves the problem.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 6, 2023, 8:12pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160/2 "2023-02-06T20:12:05Z")

</div>

What exactly is the desired output? I guess “simplifying” is not uniquely defined. If you want to have a polynomial in `rₙ` in the “usual” form, `simplify` has a few keyword arguments.

> **Docstring**
>
> ```julia
> help?> simplify
> search: simplify simplify_fractions
> 
> simplify(x; expand=false,
> threaded=false,
> thread_subtree_cutoff=100,
> rewriter=nothing)
> 
> Simplify an expression (x) by applying rewriter until there are no changes. expand=true applies expand (/api/#expand) in the beginning
> of each fixpoint iteration.
> 
> By default, simplify will assume denominators are not zero and allow cancellation in fractions. Pass simplify_fractions=false to
> prevent this.
> 
> ```

By using `expand=true` in your example, we can get the (I assume) desired output

```julia
julia> dcs_P1ₙ = simplify.(substitute.(dcs_P1, (Dict(r => rₙ - 0.0475),)); expand=true)
(244.5068698546353 + 53.15226(rₙ^3) - 309.787958040125rₙ - 128.58427705(rₙ^2),)

```

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [February 6, 2023, 8:32pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160/3 "2023-02-06T20:32:18Z")

</div>

Your assumptions were correct, good reminder to look at the docstring as well as the tutorial. I can cut and paste to put the result into a function.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 6, 2023, 8:53pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160/4 "2023-02-06T20:53:05Z")

</div>

Happy I could help 🙂

Absolutely, it took me a while to realize and get used to just hitting `?` and search the built-in docs. As a bonus, it really makes one appreciate the effort many package authors put into their docstrings.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 6, 2023, 8:59pm UTC](https://discourse.julialang.org/t/symbolics-substitute-and-simplify-in-a-polynomial/94160/5 "2023-02-06T20:59:39Z")

</div>

Ah, one more think I just remembered: There is also a tutorial specifically about defining your own “rewriters” – you can put them as a kwarg into `simplify`. If you have a very specific final form of the symbolic expression in mind, it might be useful to define such custom simplification rules:

[https://symbolicutils.juliasymbolics.org/rewrite/](https://symbolicutils.juliasymbolics.org/rewrite/)
