如何将表达式作为文字变量传递给 Julia 中的函数

How do I pass an expression, as a literal variable, to a function in Julia

我正在为 return 构建一个自制函数 符号导数表达式 和一个可调用的 微分函数.

Pkg.add("Symbolics")
using Symbolics
function derivative(exp,x)
    @variables x
    Dx=Differential(x)
    ϕe = exp 
    return (expand_derivatives(Dx(ϕe))), first(substitute.(expand_derivatives(Dx(ϕe)), (Dict(x => ξ),)))
end

#+结果: : 导数

derivative(-x^2 + 0.1*sin(x) + 2*sin(x)^2,x)

#+结果:(错误)

ERROR: MethodError: no method matching ^(::StepRangeLen{Float64, Base.TwicePrecis
ion{Float64}, Base.TwicePrecision{Float64}}, ::Int64)
Closest candidates are:
  ^(::Union{AbstractChar, AbstractString}, ::Integer) at strings/basic.jl:718
  ^(::LinearAlgebra.Symmetric{var"#s814", S} where {var"#s814"<:Real, S<:(Abstrac
tMatrix{var"#s814"} where var"#s814"<:var"#s814")}, ::Integer) at /build/julia/sr
c/julia-1.6.3/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/symmetric.jl:868
  ^(::LinearAlgebra.Symmetric{var"#s814", S} where {var"#s814"<:Complex, S<:(Abst
ractMatrix{var"#s814"} where var"#s814"<:var"#s814")}, ::Integer) at /build/julia
/src/julia-1.6.3/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/symmetric.jl:869
  ...
Stacktrace:
 [1] macro expansion
   @ ./none:0 [inlined]
 [2] literal_pow(f::typeof(^), x::StepRangeLen{Float64, Base.TwicePrecision{Float
64}, Base.TwicePrecision{Float64}}, #unused#::Val{2})
   @ Base ./none:0
 [3] top-level scope
   @ REPL[191]:1

交互调用我希望函数执行的操作(预期行为):

@variables x
Dx=Differential(x)
ϕe = -x^2 + 0.1*sin(x) + 2*sin(x)^2 
(expand_derivatives(Dx(ϕe))) 

#+结果: : 0.1cos(x) + 4cos(x)*sin(x) - 2x

dϕ(ξ) = first(substitute.(expand_derivatives(Dx(ϕe)), (Dict(x => ξ),)))

#+结果: : dϕ

dϕ(1)

#+结果: : -0.12737491576182247

您可以将 exp 变成一个接收 x 作为变量的匿名函数,如下所示:

julia> function derivative(exp, ξ)
           @variables x
           Dx=Differential(x)
           ϕe = exp(x) # <-- change here
           return (expand_derivatives(Dx(ϕe))), first(substitute.(expand_derivatives(Dx(ϕe)), (Dict(x => ξ),)))
       end

julia> derivative(x->-x^2 + 0.1*sin(x) + 2*sin(x)^2, 1)
(0.1cos(x) + 4cos(x)*sin(x) - (2x), -0.12737491576182247)

你也可以用do syntax,相当于传x -> ...

julia> derivative(1) do x
         -x^2 + 0.1*sin(x) + 2*sin(x)^2
       end
(0.1cos(x) + 4cos(x)*sin(x) - (2x), -0.12737491576182247)