Skip to content

Commit 3d8a5db

Browse files
committed
default output state.iterate
1 parent 06c792d commit 3d8a5db

5 files changed

Lines changed: 12 additions & 13 deletions

File tree

docs/src/interface.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ With these definitions in place you can already run (assuming you also choose a
128128
function heron_sqrt(x; maxiter = 10)
129129
prob = SqrtProblem(x)
130130
alg = HeronAlgorithm(StopAfterIteration(maxiter))
131-
state = solve(prob, alg) # allocates & runs
132-
return state.iterate
131+
return solve(prob, alg) # allocates & runs
133132
end
134133
135134
println("Approximate sqrt: ", heron_sqrt(16.0))
136135
```
137136

137+
Note that [`solve`](@ref) will default to returning `state.iterate`.
138+
If desired, this can be customized by altering [`finalize_state!`](@ref).
138139
We will refine this example with better halting logic and logging shortly.
139140

140141
## Reference: Core interface types & functions

docs/src/logging.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ end
7575
function heron_sqrt(x; stopping_criterion = StopAfterIteration(10))
7676
prob = SqrtProblem(x)
7777
alg = HeronAlgorithm(stopping_criterion)
78-
state = solve(prob, alg) # allocates & runs
79-
return state.iterate
78+
return solve(prob, alg) # allocates & runs
8079
end
8180
nothing # hide
8281
```
@@ -329,7 +328,7 @@ function solve!(problem::Problem, algorithm::Algorithm, state::State; kwargs...)
329328

330329
emit_message(problem, algorithm, state, :Stop)
331330

332-
return state
331+
return finalize_state!(problem, algorithm, state)
333332
end
334333
```
335334

docs/src/stopping_criterion.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ We can again combine everything into a single function, but now make the stoppin
112112
function heron_sqrt(x; stopping_criterion)
113113
prob = SqrtProblem(x)
114114
alg = HeronAlgorithm(stopping_criterion)
115-
state = solve(prob, alg) # allocates & runs
116-
return state.iterate, state.iteration
115+
return solve(prob, alg) # allocates & runs
117116
end
118117
119118
heron_sqrt(2; stopping_criterion = StopAfterIteration(10))

src/interface/interface.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ initialize_state!(::Problem, ::Algorithm, ::State; kwargs...)
2222
output = finalize_state!(problem::Problem, algorithm::Algorithm, state::State)
2323
2424
Finalize the solver and decide what values get returned from the [`solve!`](@ref) call.
25-
By default, this is a no-op and returns the `state`, but this allows for customization
26-
in cases where the details of the `state` can remain hidden.
25+
By default, this is a no-op and returns the `state.iterate`, but this allows for further
26+
customization in other cases, for example to clean up used resources or output other data.
2727
"""
28-
finalize_state!(problem::Problem, algorithm::Algorithm, state::State) = state
28+
finalize_state!(problem::Problem, algorithm::Algorithm, state::State) = state.iterate
2929

3030
# has to be defined before used in solve but is documented alphabetically after
3131

test/newton.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ end
6060
problem = RootFindingProblem(x -> f(x, a), x -> df(x, a))
6161
algorithm1 = NewtonMethod(StopAfterIteration(8))
6262
solution1 = solve(problem, algorithm1)
63-
@test solution1.iterate sqrt(a)
63+
@test solution1 sqrt(a)
6464
algorithm2 = NewtonMethod(StopAfterIteration(10))
6565
solution2 = solve(problem, algorithm2)
66-
@test solution2.iterate sqrt(a)
67-
@test abs(solution2.iterate - sqrt(a)) < abs(solution1.iterate - sqrt(a))
66+
@test solution2 sqrt(a)
67+
@test abs(solution2 - sqrt(a)) < abs(solution1 - sqrt(a))
6868
end

0 commit comments

Comments
 (0)