Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about the anonymous function in julia

I have this code:

Using ForwardDiff:gradient
derivative(f,x) = gradient(x->f(x[1]),[x])

I know gradient in the ForwardDiff take a vector as argument, but what does this mean: (x->f(x[1])), I am confused by x[1].

like image 236
Francis Avatar asked Dec 13 '25 10:12

Francis


1 Answers

I am confused by x[1]

It means to take the first element of the vector x.

This code doesn't really make much sense with the [x] as the last argument. You should probably just use e.g.:

julia> ForwardDiff.derivative(sin, 1.0)
0.5403023058681398
like image 162
Kristoffer Carlsson Avatar answered Dec 16 '25 12:12

Kristoffer Carlsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!