English 中文(简体)
Julia symbolic differentiation
原标题:

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?

问题回答

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/

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
}




相关问题
Julia symbolic differentiation

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 ...

Haskell library like SymPy? [closed]

I need to manipulate expressions like 1 + sqrt(3) and do basic arithmetic like addition, subtraction, and division. I d like the result to be in some sort of canonical form so that it can be used as a ...

Simple? Message Passing in Mathematica 7

Uu[z_,x_,t_] := A1[z]*F[t*a*x] Wu[z_,x_,t_] := B1[z]*F[t*a*x] Pu[z_,x_,t_] := C1[z]*F[t*a*x] eq1 = D[Uu[z,x,t],t]==-R*D[Pu[z,x,t],x]; C1z = DSolve[eq1,C1[z],z]; eq2 = D[Wu[z,x,t],t]==-R*D[Pu[z,x,t],z]/...

Non-sequential substitution in SymPy

I m trying to use [SymPy][1] to substitute multiple terms in an expression at the same time. I tried the [subs function][2] with a dictionary as parameter, but found out that it substitutes ...

Symbolic Math in MATLAB, solving simple integration

I have a problem with solving a simple integration through MATLAB. I want to solve this symbolic and don t have any problems doing this through other programs. Well I have this equation: syms k x ...

热门标签