You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To make everything tangible, we will work through a concrete example to illustrate the library's goals and concepts.
33
+
Our running example is Heron's / Babylonian method for estimating $\sqrt{S}$.
34
+
(see also the concise background on Wikipedia: [Babylonian method (Heron's method)](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)):
35
+
Starting from an initial guess $x_0$, we may converge to the solution by iterating:
Algorithms define a mutable step via [`step!`](@ref). For Heron's method:
95
+
96
+
```@example Heron
97
+
function AlgorithmsInterface.step!(problem::SqrtProblem, algorithm::HeronAlgorithm, state::HeronState)
98
+
S = problem.S
99
+
x = state.iterate
100
+
state.iterate = 0.5 * (x + S / x)
101
+
return state
102
+
end
103
+
```
104
+
105
+
Note that we are only focussing on the actual algorithm, and *not* incrementing the iteration counter.
106
+
These kinds of bookkeeping should be handled by the [`increment!(state)`](@ref) function, which will by default already increment the iteration counter.
107
+
The following generic functionality is therefore enough for our purposes, and does *not* need to be defined.
108
+
Nevertheless, if additional bookkeeping would be desired, this can be achieved by overloading that function:
109
+
110
+
```julia
111
+
function AlgorithmsInterface.increment!(state::State)
112
+
state.iteration +=1
113
+
return state
114
+
end
115
+
```
116
+
117
+
### Running the algorithm
118
+
119
+
With these definitions in place you can already run (assuming you also choose a stopping criterion – added in the next section):
120
+
121
+
```@example Heron
122
+
function heron_sqrt(x; maxiter = 10)
123
+
prob = SqrtProblem(x)
124
+
alg = HeronAlgorithm(StopAfterIteration(maxiter))
125
+
state = solve(prob, alg) # allocates & runs
126
+
return state.iterate
127
+
end
128
+
129
+
println("Approximate sqrt: ", heron_sqrt(16.0))
130
+
```
131
+
132
+
We will refine this example with better halting logic and logging shortly.
133
+
134
+
## Reference: Core interface types & functions
135
+
136
+
Below are the automatic API docs for the core interface pieces. Read them after grasping the example above – the intent should now be clearer.
26
137
27
138
```@autodocs
28
139
Modules = [AlgorithmsInterface]
@@ -31,7 +142,7 @@ Order = [:type, :function]
31
142
Private = true
32
143
```
33
144
34
-
## Algorithm
145
+
###Algorithm
35
146
36
147
```@autodocs
37
148
Modules = [AlgorithmsInterface]
@@ -40,7 +151,7 @@ Order = [:type, :function]
40
151
Private = true
41
152
```
42
153
43
-
## Problem
154
+
###Problem
44
155
45
156
```@autodocs
46
157
Modules = [AlgorithmsInterface]
@@ -49,11 +160,15 @@ Order = [:type, :function]
49
160
Private = true
50
161
```
51
162
52
-
## State
163
+
###State
53
164
54
165
```@autodocs
55
166
Modules = [AlgorithmsInterface]
56
167
Pages = ["interface/state.jl"]
57
168
Order = [:type, :function]
58
169
Private = true
59
-
```
170
+
```
171
+
172
+
### Next: Stopping criteria
173
+
174
+
Proceed to the stopping criteria section to add robust halting logic (iteration caps, time limits, tolerance on successive iterates, and combinations) to this square‑root example.
0 commit comments