# How to simplify a symbolic expression

**URL:** <https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381>\
**Category:** General Usage\
**Tags:** question, symbolics\
**Created:** [September 29, 2023, 8:57am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381 "2023-09-29T08:57:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 29, 2023, 8:57am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/1 "2023-09-29T08:57:02Z")

</div>

I want to simplify an expression by replacing a sub-expression with a new variable, called `Q`.

The following code doesn’t work:

```julia
# simplify an expression
using Symbolics

@variables t
@syms ω(t) Pg(t) Pge(t) Pgc(t)
@variables A R a b Γ ρ U J Q

expr = (0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t)) - J*((0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U) - Pgc(t) - Pge(t)) / ((J^2)*(ω(t)^2)))
# Q = 0.5A*R*a*Γ*ρ

# how to substitute 0.5A*R*a*Γ*ρ by Q in expr?
expr2 = substitute(expr, Dict([0.5A*R*a*Γ*ρ => Q]))

```

Q does not appear in expr2, the substitute command has no effect.

Why?

EDIT:  
Is it possible to solve this task with Symbolics, or should I try other packages like [GitHub - chakravala/Reduce.jl: Symbolic parser for Julia language term rewriting using REDUCE algebra](https://github.com/chakravala/Reduce.jl) ?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 29, 2023, 9:23am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/2 "2023-09-29T09:23:50Z")

</div>

> [@ufechner7](#):
>
> Q does not appear in expr2, the substitute command has no effect.

This needs to be done with a rule application, not substitute.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 29, 2023, 9:36am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/3 "2023-09-29T09:36:00Z")

</div>

Good suggestion, but not yet working.

```julia
using Symbolics

@variables t
@syms ω(t) Pg(t) Pge(t) Pgc(t)
@variables A R a b Γ ρ U J Q

expr = (0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t)) - J*((0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U) - Pgc(t) - Pge(t)) / ((J^2)*(ω(t)^2)))

r1 = @rule 0.5A*R*a*Γ*ρ => Q
# Q = 0.5A*R*a*Γ*ρ

# how to substitute 0.5A*R*a*Γ*ρ by Q in expr?
expr2 = r1(expr)

```

The output is empty.

Any suggestion how to do this correctly?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 29, 2023, 12:44pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/4 "2023-09-29T12:44:33Z")

</div>

Perhaps I should ask different questions. For example:

- is `expr` above actually an expression, because it is of type `Num`?
- how can I iterate over the elements of such an expression?
- what is the difference between `@syms` and `@variables` ?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 29, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/5 "2023-09-29T19:37:21Z")

</div>

Maybe you should use the package [SymbolicUtils](https://symbolicutils.juliasymbolics.org/rewrite/#rule-based_rewriting)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 29, 2023, 8:18pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/6 "2023-09-29T20:18:19Z")

</div>

Well, I think Symbolics re-exports SymbolicUtils, so that makes no difference…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 29, 2023, 8:22pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/7 "2023-09-29T20:22:10Z")

</div>

> [@ufechner7](#):
>
> ```julia
> expr = (0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t)) - J*((0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U) - Pgc(t) - Pge(t)) / ((J^2)*(ω(t)^2)))
> # Q = 0.5A*R*a*Γ*ρ
> 
> # how to substitute 0.5A*R*a*Γ*ρ by Q in expr?
> 
> ```

On paper this is trivial, why is it so hard with Julia?

---

<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:** [September 30, 2023, 12:34am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/8 "2023-09-30T00:34:36Z")

</div>

`substitute` currently only allows you to substitute variables for other expressions, not the other way around unfortunately…

To walk the expression, `tree = Symbolics.unwrap(expr)` which removes the `Num <: Real` wrapper.

Then you can use the expression walking interface: [https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/page/interface.md](https://github.com/JuliaSymbolics/SymbolicUtils.jl/blob/master/page/interface.md)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2023, 9:53am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/9 "2023-10-02T09:53:54Z")

</div>

> [@shashi](#):
>
> To walk the expression, `tree = Symbolics.unwrap(expr)` which removes the `Num <: Real` wrapper.

If I run this script:

```julia
using Symbolics

@variables t
@syms ω(t) Pg(t) Pge(t) Pgc(t) A
@variables R a b Γ ρ U J Q

expr = (0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t)) - J*((0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U) - Pgc(t) - Pge(t)) / ((J^2)*(ω(t)^2)))

tree = Symbolics.unwrap(expr)

function walk(tree, level=0)
    for arg in arguments(tree)
        indent = " "^level
        println(indent, arg)
        if istree(arg)
            level += 1
            walk(arg, level)
            level -= 1
        end
    end
end

walk(tree)

```

the output is:

```julia
(Pgc(t) + Pge(t) - 0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U)) / (J*(ω(t)^2))
 Pgc(t) + Pge(t) - 0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U)
  -0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U)
   -0.5
   A
   Γ
   ρ
   U^3
    U
    3
   b + (R*a*ω(t)) / U
    b
    (R*a*ω(t)) / U
     R*a*ω(t)
      R
      a
      ω(t)
       t
     U
  Pgc(t)
   t
  Pge(t)
   t
 J*(ω(t)^2)
  J
  ω(t)^2
   ω(t)
    t
   2
(0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t))
 0.5A*R*a*Γ*ρ*(U^2)
  0.5
  A
  R
  a
  Γ
  ρ
  U^2
   U
   2
 J*ω(t)
  J
  ω(t)
   t

```

So I can walk the tree. 🙂

Next questions are how to identify the expression I want to replace, how to replace it, and then how to re-assemble it.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 2, 2023, 1:13pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/10 "2023-10-02T13:13:25Z")

</div>

Next question: How can I check if two expressions are equal?

```julia
Q=0.5A*R*a*Γ*ρ*(U^2)
# now check if another expression is equal to Q
Q==0.5A*R*a*Γ*ρ*(U^2)
# the output is another expression, and not true or false
(0.5A*R*a*Γ*ρ*(U^2)) == (0.5A*R*a*Γ*ρ*(U^2))

```

EDIT:  
I am using now:

```julia
repr(subexpr) == "0.5A*R*a*Γ*ρ*(U^2)"

```

Definitly not a robust solution, but works for now…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 6, 2023, 10:29am UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/11 "2023-10-06T10:29:51Z")

</div>

My current solution:

```julia
using Symbolics

@variables t
@syms ω(t) Pg(t) Pge(t) Pgc(t)
@variables R a b Γ ρ U J Q A

expr = (0.5A*R*a*Γ*ρ*(U^2)) / (J*ω(t)) - J*((0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U) - Pgc(t) - Pge(t)) / ((J^2)*(ω(t)^2)))

Qs = "0.5A*R*a*Γ*ρ"

# how to substitute 0.5A*R*a*Γ*ρ by Q in expr?

function subst(expr, from="", to="")
    sexpr = repr(expr)
    sres = replace(sexpr, from => to)
    eval(Meta.parse(sres))
end

simple = subst(expr, Qs, "Q")

```

Output:

```julia
(Pgc(t) + Pge(t) - 0.5A*Γ*ρ*(U^3)*(b + (R*a*ω(t)) / U)) / (J*(ω(t)^2)) + (Q*(U^2)) / (J*ω(t))

```

Not a good solution because it uses eval and requires to the symbolic variables to be defined globally.

I will create two issues on this topic:

- [Attach properties to symbols · Issue #988 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/988)
- [substitute should be able of replacing an expression with a variable · Issue #989 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/989)

---

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [November 22, 2024, 3:46pm UTC](https://discourse.julialang.org/t/how-to-simplify-a-symbolic-expression/104381/12 "2024-11-22T15:46:16Z")

</div>

> [@ufechner7](#):
>
> Next question: How can I check if two expressions are equal?

isequal(x, y) should work
