Skip to content

Commit

Permalink
Incorrect Jacobian Size for NLLS
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Mar 6, 2024
1 parent 390341f commit 81edf48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/internal/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ function JacobianCache(
JacobianOperator(prob, fu, u; jvp_autodiff, vjp_autodiff)
else
if has_analytic_jac
f.jac_prototype === nothing ? undefmatrix(u) : f.jac_prototype
f.jac_prototype === nothing ?
similar(fu, promote_type(eltype(fu), eltype(u)), length(fu), length(u)) :
copy(f.jac_prototype)
elseif f.jac_prototype === nothing
init_jacobian(jac_cache; preserve_immutable = Val(true))
else
Expand Down
19 changes: 19 additions & 0 deletions test/core/nlls_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ end
@test maximum(abs, sol.resid) < 1e-6
end
end

@testitem "NLLS Analytic Jacobian" begin
dataIn = 1:10
f(x, p) = x[1] * dataIn .^ 2 .+ x[2] * dataIn .+ x[3]
dataOut = f([1, 2, 3], nothing) + 0.1 * randn(10, 1)

resid(x, p) = f(x, p) - dataOut
jac(x, p) = [dataIn .^ 2 dataIn ones(10, 1)]
x0 = [1, 1, 1]

prob = NonlinearLeastSquaresProblem(resid, x0)
sol1 = solve(prob)

nlfunc = NonlinearFunction(resid; jac)
prob = NonlinearLeastSquaresProblem(nlfunc, x0)
sol2 = solve(prob)

@test sol1.u sol2.u
end

0 comments on commit 81edf48

Please sign in to comment.