Skip to content

Commit 81edf48

Browse files
committed
Incorrect Jacobian Size for NLLS
1 parent 390341f commit 81edf48

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/internal/jacobian.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ function JacobianCache(
8383
JacobianOperator(prob, fu, u; jvp_autodiff, vjp_autodiff)
8484
else
8585
if has_analytic_jac
86-
f.jac_prototype === nothing ? undefmatrix(u) : f.jac_prototype
86+
f.jac_prototype === nothing ?
87+
similar(fu, promote_type(eltype(fu), eltype(u)), length(fu), length(u)) :
88+
copy(f.jac_prototype)
8789
elseif f.jac_prototype === nothing
8890
init_jacobian(jac_cache; preserve_immutable = Val(true))
8991
else

test/core/nlls_tests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,22 @@ end
9494
@test maximum(abs, sol.resid) < 1e-6
9595
end
9696
end
97+
98+
@testitem "NLLS Analytic Jacobian" begin
99+
dataIn = 1:10
100+
f(x, p) = x[1] * dataIn .^ 2 .+ x[2] * dataIn .+ x[3]
101+
dataOut = f([1, 2, 3], nothing) + 0.1 * randn(10, 1)
102+
103+
resid(x, p) = f(x, p) - dataOut
104+
jac(x, p) = [dataIn .^ 2 dataIn ones(10, 1)]
105+
x0 = [1, 1, 1]
106+
107+
prob = NonlinearLeastSquaresProblem(resid, x0)
108+
sol1 = solve(prob)
109+
110+
nlfunc = NonlinearFunction(resid; jac)
111+
prob = NonlinearLeastSquaresProblem(nlfunc, x0)
112+
sol2 = solve(prob)
113+
114+
@test sol1.u sol2.u
115+
end

0 commit comments

Comments
 (0)