JuliaDiff / ForwardDiff.jl

Forward Mode Automatic Differentiation for Julia
Other
892 stars 145 forks source link

Derivative of a function of derivatives #673

Open tlorance opened 1 year ago

tlorance commented 1 year ago

First, for context: I am new to both Julia and ForwardDiff, but I am experienced at coding. For my project, I need to compute the first derivative of a 2x2 determinant where the elements of the determinant are second-order partial derivatives; specifically, I need CodeCogsEqn (1) and CodeCogsEqn (2) where CodeCogsEqn I am not sure why the two mixed partial derivatives don't cancel; it might have something to do with the fact that this is a ternary data system (where x1 + x2 + x3 is constrained to equal 1). At any rate, is this possible in ForwardDiff, or do I need to do finite differentiation on the sigma obtained from a ForwardDiff hessian?

mcabbott commented 1 year ago

The obvious thing here is ForwardDiff.gradient(x -> det(ForwardDiff.hessian(g, x)), x). Does this fail?

tlorance commented 1 year ago

That might work; I didn't know you could do that. Just to be clear, though, my determinant is not the full determinant of the hessian (which would be a 3x3), so I'm still not sure how to construct it. I will experiment with the ForwardDiff.hessian function, see how its output is formatted, and see if I can select the first two rows and columns via an inline method.

tlorance commented 1 year ago

I am new enough to Julia that I am not entirely sure which "x"s in your formula should be replaced with my input and which should be left alone (since my function is defined as g(x::Vector)=...). However, it was easy to select the first two rows and columns via an inline method, so I'm sure this will work, as soon as I figure it out.

tlorance commented 1 year ago

Final question: is the partial derivative in row 2, column 1 of my formula in the corresponding location in the hessian?

tlorance commented 1 year ago

Well, the code runs and appears to function, but the answer is very different from what I obtained by hand-coding a finite difference, so I think I screwed something up.

longemen3000 commented 1 year ago

is this related to criticality conditions?

tlorance commented 1 year ago

I found an example in a journal article, coded the example, and managed to replicate it, so perhaps the code is OK and my hand-coded version is wrong. I still have issues, but they seem to be chemistry issues, not numerical methods issues, so: thank you very much, your solution worked like a charm!