# Can Symbolics.jl simplify subexpressions?

**URL:** <https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [August 20, 2021, 1:17pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731 "2021-08-20T13:17:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhardy](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@nhardy](https://discourse.julialang.org/u/nhardy)\
**Post date:** [August 20, 2021, 1:17pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/1 "2021-08-20T13:17:12Z")

</div>

I have been trying out Symbolics.jl for a few projects recently. Initially I was excited but have run into a few issues that are road blocks. Some are probably asking too much this early in development - `(A::SparseMatrixCSC{Num, Int64})^(1/2)` doesn’t work (I get the" non-boolean (Num) used in a boolean context" error referenced in the docs) - but more surprising to me is I couldn’t get `simplify` to work below the first level of an expression. I’m coming from Mathematica for symbolic math, and this was really disappointing (and I am hoping user error). As an MWE,

```julia
using Symbolics

@variables y
t1 = sqrt(y^2)
t2 = abs(sqrt(y^2))
sqrtrule = @rule sqrt((~x)^2) => ~x

s1 = simplify(t1, sqrtrule)
s2 = simplify(t2, sqrtrule)

```

In this example, (a common simplification for a real positive variable) I get ` s1=y`, but `s2=abs(sqrt(y^2))`.  
Symbolics is (rightfully) giving me some gigantic expressions during my computations, and this is one example of the kind of pattern embedded in the result that I would like to simplify before generating a function… how can I do this?

Thanks.

---

<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:** [August 20, 2021, 1:54pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/2 "2021-08-20T13:54:40Z")

</div>

> [@nhardy](#):
>
> Some are probably asking too much this early in development - `(A::SparseMatrixCSC{Num, Int64})^(1/2)` doesn’t work (I get the" non-boolean (Num) used in a boolean context" error referenced in the docs)

Square root of a sparse matrix is hard to do numerically. Did you mean `.^`?

> [@nhardy](#):
>
> In this example, (a common simplification for a real positive variable) I get ` s1=y` , but `s2=abs(sqrt(y^2))` .

You need to add a `Postwalk` to that I think?

> [@nhardy](#):
>
> Symbolics is (rightfully) giving me some gigantic expressions during my computations, and this is one example of the kind of pattern embedded in the result that I would like to simplify before generating a function… how can I do this?

Did you run `simplify` or were you using your own rule set?

---

<div class="post-metadata">

**Author:** ![nhardy](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@nhardy](https://discourse.julialang.org/u/nhardy)\
**Post date:** [August 20, 2021, 2:07pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/3 "2021-08-20T14:07:24Z")

</div>

Thanks for the reply. Point by point…

1. Nope, I meant the square root of the matrix. I guessed that would be hard / unsupported, and can probably cut of the symbolic part before that (and do it numerically later).
2. I also tried making the rule a post walk, but maybe I didn’t do it right? Doing

```julia
simplify(t2, SymbolicUtils.Postwalk(sqrtrule))

```

also does nothing.  
3. I tried plain simplify first, but it didn’t do anything to my expression. Coming from Mathematica for symbolics (Julia for numerics for a while now), I had hoped to specify some assumptions about variable domains, but that doesn’t appear to be supported yet. Baring that I tried passing my own simplification for this simple case, but even that didn’t do anything (and hence my post).

Thoughts?

---

<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:** [August 20, 2021, 2:16pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/4 "2021-08-20T14:16:58Z")

</div>

> [@nhardy](#):
>
> Nope, I meant the square root of the matrix. I guessed that would be hard / unsupported, and can probably cut of the symbolic part before that (and do it numerically later).

I think you need the matrix log to define that. Yeah, I don’t think you’ll get a good general symbolic solution there?

> [@nhardy](#):
>
> I also tried making the rule a post walk, but maybe I didn’t do it right? Doing

Yeah, open an issue. This is a question for @shashi

> [@nhardy](#):
>
> 1. I tried plain simplify first, but it didn’t do anything to my expression. Coming from Mathematica for symbolics (Julia for numerics for a while now), I had hoped to specify some assumptions about variable domains, but that doesn’t appear to be supported yet. Baring that I tried passing my own simplification for this simple case, but even that didn’t do anything (and hence my post).

Variable domains are not implemented yet, though we’re getting there: [Added the ability to pass known SymPy symbols to symbolics\_to\_sympy function by owiecc · Pull Request #351 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/pull/351)

---

<div class="post-metadata">

**Author:** ![nhardy](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@nhardy](https://discourse.julialang.org/u/nhardy)\
**Post date:** [August 22, 2021, 9:00pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/5 "2021-08-22T21:00:21Z")

</div>

Ok, thanks for the reply. I opened an [issue](https://github.com/JuliaSymbolics/Symbolics.jl/issues/356) at Symbolics.jl (maybe it should be at SymbolicUtils?).

---

<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:** [August 22, 2021, 10:13pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/6 "2021-08-22T22:13:26Z")

</div>

That’s good, thanks.

---

<div class="post-metadata">

**Author:** ![nhardy](https://avatars.discourse-cdn.com/v4/letter/n/9fc348/32.png) [@nhardy](https://discourse.julialang.org/u/nhardy)\
**Post date:** [August 31, 2021, 12:41pm UTC](https://discourse.julialang.org/t/can-symbolics-jl-simplify-subexpressions/66731/7 "2021-08-31T12:41:29Z")

</div>

For anyone that comes across this, I thought I would share the answer I got from shashi to the [issue](https://github.com/JuliaSymbolics/Symbolics.jl/issues/356). In short, if you want a rewrite rule that operates below the top level of an expression it does need to be a Prewalk/Postwalk, BUT those require a rewriter with passthrough behavior (by default rewriters return `nothing` if there is no change). If combining multiple rules, `Chain` has this behavior, and for just one rule you can wrap it in `PassThrough`. So, for the example above,

```julia
simplify(t2, Rewriters.Prewalk(Rewriters.PassThrough(sqrtrule)))

```

gives the desired behavior.
