Skip to content

Commit 118fb0b

Browse files
committed
Add tools to extract x, F, and J from the trace.
1 parent e5967ca commit 118fb0b

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/NLsolve.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ include("nlsolve/nlsolve.jl")
5252
include("nlsolve/utils.jl")
5353
include("nlsolve/fixedpoint.jl")
5454

55+
include("trace_tools.jl")
56+
5557
end # module

src/trace_tools.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trace(r::SolverResults) = r.trace
2+
function x_trace(r::SolverResults)
3+
tr = trace(r).states
4+
!haskey(tr[1].metadata, "x") && error("Trace does not contain x. To get a trace of x, run nlsolve() with extended_trace = true")
5+
[ state.metadata["x"] for state in tr ]
6+
end
7+
function F_trace(r::SolverResults)
8+
tr = trace(r).states
9+
!haskey(tr[1].metadata, "f(x)") && error("Trace does not contain F. To get a trace of the residuals, run nlsolve() with extended_trace = true")
10+
[ state.metadata["f(x)"] for state in tr ]
11+
end
12+
function J_trace(r::SolverResults)
13+
tr = trace(r).states
14+
!haskey(tr[1].metadata, "g(x)") && error("Trace does not contain J. To get a trace of the Jacobian, run nlsolve() with extended_trace = true")
15+
[ state.metadata["g(x)"] for state in tr ]
16+
end

test/complex.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ for linesearches in [BackTracking(),StrongWolfe(),HagerZhang(),MoreThuente()] #S
3232
@test sol.g_calls == sol_real.g_calls
3333
@test all(sol_real.trace[i].stepnorm == sol_real.trace[i].stepnorm for i in 2:sol.iterations)
3434
@test all(norm(sol.trace[i].metadata["f(x)"]) norm(sol_real.trace[i].metadata["f(x)"]) for i in 1:5)
35+
NLsolve.x_trace(sol_real)
36+
NLsolve.F_trace(sol_real)
37+
NLsolve.J_trace(sol_real)
3538
end
3639
end

0 commit comments

Comments
 (0)