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.
Yeah there probably is a quicker way using coeff
.
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
.
Is there anyway to use the expression tree of a symbolics expression to see if it contains any in a list of independent variables?
I tried the following code -
#!/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.
You have to remove the Num
wrapper to get at the BasicSymbolic
inside:
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