英文:
Julia symbolic differentiation
问题
尽管我查阅了几页关于Julia中各种微分工具的文档,但我仍然没有找到以下简单功能。我想定义一个可以手动微分的函数,例如,f(x) = 2x.^3
,然后调用一个符号计算函数来获得fprime = derivative(f)
。这段代码应该产生与fprime(x) = 6x.^2
相同的结果。是否有一个类似于上面虚构的derivative
函数的库函数?
英文:
Though I've looked through several pages documenting various differentiation tools in Julia, I have yet to find the following simple functionality. I want to define a function which is differentiable by hand, e.g, f(x) = 2x.^3
, then call a symbolic computation function to obtain fprime = derivative(f)
. This code should produce the same results as fprime(x) = 6x.^2
. Is there a library function which acts like the made-up function derivative
above?
答案1
得分: 2
Symbolics.jl
包能够实现你想要的功能,尽管你需要明确声明你的变量。
In [2]: using Symbolics
In [3]: @variables x
Out[3]: 1-element Vector{Num}:
x
In [4]: D = Differential(x)
Out[4]: (::Differential) (generic function with 2 methods)
In [5]: f(t) = 2*t^3
Out[5]: f (generic function with 1 method)
In [6]: expand_derivatives(D(f(x)))
Out[6]: 6(x^2)
你可以在这里了解更多信息:https://symbolics.juliasymbolics.org/dev/manual/derivatives/
英文:
The Symbolics.jl
package is capable of what you're looking for, although you need to explicitly declare what your variables are
In [2]: using Symbolics
In [3]: @variables x
Out[3]: 1-element Vector{Num}:
x
In [4]: D = Differential(x)
Out[4]: (::Differential) (generic function with 2 methods)
In [5]: f(t) = 2*t^3
Out[5]: f (generic function with 1 method)
In [6]: expand_derivatives(D(f(x)))
Out[6]: 6(x^2)
You can read more about it here: https://symbolics.juliasymbolics.org/dev/manual/derivatives/
答案2
得分: 1
If you work numerically with symbolic derivatives you can also use code-to-code symbolic differentiation by using Zygote
Assume you have f(x) = 2x^3
than having Zygote loaded you can do
julia> f'(5)
150.0
to understand what just happened peek into the compilation process to see that the function f
has been actually symbolically differentiated.
julia> @code_llvm f'(5)
define double @"julia_#79_991"(i64 signext %0) #0 {
top:
%1 = mul i64 %0, 3
%2 = mul i64 %1, %0
%3 = sitofp i64 %2 to double
%4 = fmul double %3, 2.000000e+00
ret double %4
}
英文:
If you work numerically with symbolic derivatives you can also use code-to-code symbolic differentiation by using Zygote
Assume you have f(x) = 2x^3
than having Zygote loaded you can do
julia> f'(5)
150.0
to understand what just happened peek into the compilation process to see that the function f
has been actually symbolically differentiated.
julia> @code_llvm f'(5)
define double @"julia_#79_991"(i64 signext %0) #0 {
top:
%1 = mul i64 %0, 3
%2 = mul i64 %1, %0
%3 = sitofp i64 %2 to double
%4 = fmul double %3, 2.000000e+00
ret double %4
}
答案3
得分: 1
以下是翻译好的部分:
在Julia中,有各种用于自动微分(AD)的包。对于像您描述的单变量函数,最简单且最快的可能是ForwardDiff包,它允许您执行以下操作:
using ForwardDiff
fprime(x) = ForwardDiff.derivative(f, x)
ForwardDiff和其他AD包确实计算精确的解析导数,相当于您可能手动编写的6x^2
。(然而,算法与您可能想象的“形成大型符号表达式然后进行微分”的方法有些不同。)
英文:
There are various packages for automatic differentiation (AD) in Julia. For a single-variable function like the one you describe, the simplest and fastest is probably the ForwardDiff package, which lets you do:
using ForwardDiff
fprime(x) = ForwardDiff.derivative(f, x)
ForwardDiff and other AD packages indeed compute the exact analytical derivative, equivalent to the 6x^2
you might write by hand. (However, the algorithms are a bit different from the "form a big symbolic expression then differentiate" that you might imagine.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论