i’m trying to solve a non linear system of equations via newton’s method. the problem is along the lines of:
f(model::Nothing,a,x) = sum(x)*a + log(a/sum(x))
function g(model::Nothing,a,x) = ForwardDiff.gradient(z-> f(model,a,z),x)
function neq(model,output,input,a1,a2,a3,nc::Int)
x1 = view(input,1:nc)
x2 = view(input,(nc+1):2*nc)
x3 = view(input,(2*nc+1):3*nc)
g1 = g(model,a1,x1)
output[1:nc] .= g1 .- a1
output[(nc+1):2*nc] .= g1
output[(2*nc+1):3*nc] .= g1
output[(nc+1):2*nc] .-= g(model,a2,x2)
output[(2*nc+1):3*nc] .- g(model,a3,x3)
return output
end
i can calculate the jacobian of this function via forwarddiff:
nc = 5
x = rand(3*nc)
F = copy(x)
f!(y,x) = neq(nothing,y,x,1.0,2.0,3.0,nc)
config = ForwardDiff.JacobianConfig(f!,F,x)
J = similar(x,(3*nc,3*nc))
ForwardDiff.jacobian!(J,f!,F,x,config)
I can also calculate the gradient config:
gconfig = ForwardDiff.GradientConfig(f,zeros(3))
my question is, how can i use a gradient config inside the neq function, while also providing a config for the jacobian calculation?
Ideally, something like this:
inner_config = ForwardDiff.GradientConfig(...) #need to figure this out
f!(y,x) = neq(nothing,y,x,1.0,2.0,3.0,nc,inner_config)
outer_config = ForwardDiff.JacobianConfig(f,F,x)
ForwardDiff.jacobian!(J,f!,F,x,outer_config)