ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

Derivative of a function of derivatives

Open tlorance opened this issue 1 year ago • 7 comments

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?

tlorance avatar Nov 16 '23 22:11 tlorance

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

mcabbott avatar Nov 17 '23 04:11 mcabbott

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 avatar Nov 17 '23 17:11 tlorance

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 avatar Nov 17 '23 17:11 tlorance

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

tlorance avatar Nov 17 '23 17:11 tlorance

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.

tlorance avatar Nov 17 '23 17:11 tlorance

is this related to criticality conditions?

longemen3000 avatar Nov 18 '23 02:11 longemen3000

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!

tlorance avatar Nov 18 '23 15:11 tlorance