# Test for constant expressions in Symbolics.jl

**URL:** <https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654>\
**Category:** New to Julia\
**Created:** [February 23, 2024, 4:31pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654 "2024-02-23T16:31:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 23, 2024, 4:31pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/1 "2024-02-23T16:31:17Z")

</div>

I am using Symbolics.jl and wish to test a symbolic expression to see if is a constant. The only way I can think of is to see if all the derivatives with respect to the independent symbolic variables are zero. I think there must be an simpler way. I have a two dimensional symbolic array and need to determine if all the entries are constants (not functions of a set of independent variables). I am taking the derivatives of the metric tensor if the tensor is not constant.

---

<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:** [February 23, 2024, 4:41pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/2 "2024-02-23T16:41:01Z")

</div>

Yeah there probably is a quicker way using `coeff`.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 23, 2024, 5:45pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/3 "2024-02-23T17:45:38Z")

</div>

I could probably convert the expression to a string and use regex to see if the string contains the names of any independent variables which I do know. For example if the string contains `x`, `y`, or `z` it is not constant even though it could contain `a` or `b` which are not independent variables or functions containing `a` or `b`.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 23, 2024, 8:29pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/4 "2024-02-23T20:29:30Z")

</div>

Is there anyway to use the expression tree of a symbolics expression to see if it contains any in a list of independent variables?

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 23, 2024, 8:46pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/5 "2024-02-23T20:46:02Z")

</div>

I tried the following code -

```julia
#!/snap/bin/julia

using Pkg
Pkg.activate("symbolics", shared = true)
using Symbolics
using SymbolicUtils

@variables x,y,z

expr = x*sin(y)*z^2

println(SymbolicUtils.istree(expr))

```

and the answer was `false`. Am I doing something wrong here because if `istree` is `true` I can use the `arguments` function to see if a particular arg is in the tree.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [February 23, 2024, 8:59pm UTC](https://discourse.julialang.org/t/test-for-constant-expressions-in-symbolics-jl/110654/6 "2024-02-23T20:59:52Z")

</div>

You have to remove the `Num` wrapper to get at the `BasicSymbolic` inside:

```julia
julia> typeof(expr)
Num

julia> typeof(Symbolics.unwrap(expr))
SymbolicUtils.BasicSymbolic{Real}

julia> SymbolicUtils.istree(Symbolics.unwrap(expr))
true

julia> SymbolicUtils.arguments(Symbolics.unwrap(expr))
3-element Vector{Any}:
 x
 z^2
 sin(y)

```

Then I think you have to keep recursively applying `arguments`, and collecting the results until nothing `istree`
