Home > Back-end >  How do I pass an expression, as a literal variable, to a function in Julia
How do I pass an expression, as a literal variable, to a function in Julia

Time:10-31

I'm building a homecooked-function to return the symbolic derivative expression and a callable derivative function.

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

# RESULTS: : derivative

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

# RESULTS: (error)

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

Calling interactively what I would like that the function does (expected behavior):

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

# RESULTS: : 0.1cos(x) 4cos(x)*sin(x) - 2x

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

# RESULTS: : dϕ

dϕ(1)

# RESULTS: : -0.12737491576182247

CodePudding user response:

You may turn the exp into a anonymous function that receives x as a variable, like the following:

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)

You can also use the do syntax if you like, which is equivalent to passing 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)
  • Related