Symbolics.jl convert string to symbolic equation

Consider that I have a string S = "r^2*sin(th)^2" that I want to convert to an Symbolics expression, Sexpr, so that -

Derivative(r)*Sexpr = 2*r*sin(th)^2
Derivative(th)*Sexpr = 2r^2*sin(th)*cos(th)

Basically I wish to input symbolic expressions via strings (actually a two dimensional array of strings).

You can use eval(Meta.parse("r^2*sin(th)^2")) to convert a string to a Julia code which can be evaluated in the Main scope. For this to work, you need to define the symbols r and th before via @variables r th.

Perhaps, use Meta.parse like Steffen suggested, but roll-out your own specialized Expr processing to build an expression fit for your system:

julia> Meta.parse("r^2*sin(th)^2")
:(r ^ 2 * sin(th) ^ 2)

julia> dump(Meta.parse("r^2*sin(th)^2"))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol *
    2: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol ^
        2: Symbol r
        3: Int64 2
    3: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol ^
        2: Expr
          head: Symbol call
          args: Array{Any}((2,))
            1: Symbol sin
            2: Symbol th
        3: Int64 2

Some macro processing tools could help in Expr analysis.

Thank you both. Steffen’s suggestion works for what I have to do.