# Does Roots.jl provide an API to obtain the trace of a root finding algorithm?

**URL:** <https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654>\
**Category:** General Usage\
**Tags:** roots\
**Created:** [August 2, 2021, 1:10am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654 "2021-08-02T01:10:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [August 2, 2021, 1:10am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/1 "2021-08-02T01:10:21Z")

</div>

I only find that it can print out such information by the kwargs `verbose=true`. But how can I obtain this just like the `Optim.jl` which store all information in the returned value? I want to count the number of evaluations of my function called by each root finding algorithm.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 2, 2021, 1:19am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/2 "2021-08-02T01:19:12Z")

</div>

> [@liuyxpp](#):
>
> I want to count the number of evaluations of my function called by each root finding algorithm.

Just increment a counter in your function. (You can use a local variable for the counter via a closure.)

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [August 2, 2021, 1:35am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/3 "2021-08-02T01:35:04Z")

</div>

Thanks! But what if I don’t want to modify my funciton (or the function is an external call to the C/C++ funciton?). Do you know any other root finding packages that keeps track of its iteration trace? Besides the number of evaluations, I’d also like to know the `x` and `f(x)` during each iteration.

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 2, 2021, 2:56am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/4 "2021-08-02T02:56:33Z")

</div>

“You can use a local variable for the counter via a closure.” means to do the following. You can use any function including a function which is an external call to the C function.

```julia
using Roots

function withcounter(f, n = 0)
    c = Ref(n)
    F(x...; y...) = (c[] += 1; f(x...; y...))
    getcounter() = c[]
    setcounter!(n) = c[] = n
    F, getcounter, setcounter!
end

f(x::Float64)::Float64 = @ccall sin(x::Float64)::Float64
@show f(1.0);

```

```julia
f(1.0) = 0.8414709848078965

```

```julia
F, getcounter, setcounter! = withcounter(x -> f(x) - 0.8414709848078965)
@show find_zero(F, 0.9)
@show getcounter();

```

```julia
find_zero(F, 0.9) = 1.0
getcounter() = 11

```

```julia
setcounter!(0)
@show find_zeros(F, 0, 2π)
@show getcounter();

```

```julia
find_zeros(F, 0, 2π) = [1.0, 2.141592653589793]
getcounter() = 1270

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 2, 2021, 11:34am UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/5 "2021-08-02T11:34:30Z")

</div>

In addition to the suggested ways, the state object keeps track of this. It can be accessed different ways, here is one:

```julia
using Roots
Z = ZeroProblem(sin, (3,4))
prob = init(Z, Roots.Secant()); solve!(prob)
prob.state.fnevals # 7

```

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [August 2, 2021, 1:08pm UTC](https://discourse.julialang.org/t/does-roots-jl-provide-an-api-to-obtain-the-trace-of-a-root-finding-algorithm/65654/6 "2021-08-02T13:08:21Z")

</div>

This solution leads me to explore the source code of `Roots.jl` package. And what I find is the kwargs `tracks` which is exactly what I want! Thanks all!

To track the number of evaluations and all `xs` and `fs`, we just provide a `Tracks` instance to the kwargs, such as

```julia
using Roots
tracks = Roots.Tracks(Float64[], Float64[])
find_zero(sin, (3,4), Roots.FalsePosition(), tracks=tracks, verbose=true)

```

Then we can show the tracks

```julia
Roots.show_tracks(tracks, Roots.FalsePosition())

```
