# Hcubature\_count?

**URL:** <https://discourse.julialang.org/t/hcubature-count/115878>\
**Category:** Numerics\
**Tags:** quadrature, hcubature\
**Created:** [June 19, 2024, 5:55pm UTC](https://discourse.julialang.org/t/hcubature-count/115878 "2024-06-19T17:55:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [June 19, 2024, 5:55pm UTC](https://discourse.julialang.org/t/hcubature-count/115878/1 "2024-06-19T17:55:09Z")

</div>

Is there a \_count equivalent for hcubature in the same way that quadgk\_count is for quadgk? Thx.

---

<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:** [June 19, 2024, 6:34pm UTC](https://discourse.julialang.org/t/hcubature-count/115878/2 "2024-06-19T18:34:21Z")

</div>

Nothing is currently built in. Would be easy to add since `quadgk_count` is just a [6-line wrapper around `quadgk`](https://github.com/JuliaMath/QuadGK.jl/blob/a86aaf016f71a4956eb59c47b07ba5d029f2083a/src/adapt.jl#L313-L320), i.e.

```julia
function hcubature_count(f, a, b; kws...)
    count = 0
    i = hcubature(a, b; kws...) do x
        count += 1
        f(x)
    end
    return (i..., count)
end

```

If someone wants to submit a PR to HCubature.jl with something like this (and a test, and docs), that would be welcome. [hcubature\_count · Issue #68 · JuliaMath/HCubature.jl · GitHub](https://github.com/JuliaMath/HCubature.jl/issues/68)

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [June 19, 2024, 7:09pm UTC](https://discourse.julialang.org/t/hcubature-count/115878/3 "2024-06-19T19:09:57Z")

</div>

Thx!

Not sure how to give an interpretation to the large number I obtain

```julia
# non-singular example 
hcubature(x -> x[1]/norm(x)^3, (1,1,1), (2,2,2))

```

(0.08562994005985379, 1.2758662568828662e-9)

```julia
# non-singular example with count 
hcubature_count(x -> x[1]/norm(x)^3, (1,1,1), (2,2,2))[3]

```

10659

```julia
# singular example 

hcubature(x -> x[1]/norm(x)^3, (0,0,0), (1,1,1))

```

(0.9693880526621552, 1.4444861385160079e-8)

```julia
# singular example with count 
hcubature_count(x -> x[1]/norm(x)^3, (0,0,0), (1,1,1))[3]

```

1098471

---

<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:** [June 19, 2024, 7:53pm UTC](https://discourse.julialang.org/t/hcubature-count/115878/4 "2024-06-19T19:53:01Z")

</div>

> [@Domenico\_Lahaye](#):
>
> ```julia
> # singular example with count 
> hcubature_count(x -> x[1]/norm(x)^3, (0,0,0), (1,1,1))[3]
> 
> ```
> 
> 1098471

This is only about 100 points per dimension:

```julia
julia> cbrt(1098471)
103.18016038906488

```

which is quite reasonable for integrating a singular integrand to 8 digits (the default `rtol`).

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [June 19, 2024, 8:17pm UTC](https://discourse.julialang.org/t/hcubature-count/115878/5 "2024-06-19T20:17:37Z")

</div>

Thx again.

What do you consider to be a good measure of the degree of singularity of the integrand? Does information from truncated Taylor series map on-to-one to number of function evaluations?

I would expect 1/norm(x)^3 to be more singular than x[1]/norm(x)^3, but hcubature shows a lower function evaluation count.

The count for (x[1]\*x[1])/norm(x)^3 is lower than for x[1]/norm(x)^3.

Is my understanding correct that one could output and plot the history x. Do you consider these plots valuable to include in a tutorial?

---

<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:** [June 20, 2024, 1:01am UTC](https://discourse.julialang.org/t/hcubature-count/115878/6 "2024-06-20T01:01:54Z")

</div>

> [@Domenico\_Lahaye](#):
>
> What do you consider to be a good measure of the degree of singularity of the integrand? Does information from truncated Taylor series map on-to-one to number of function evaluations?

If it’s singular, you don’t even have a Taylor series.

If the function is analytic (has a convergent Taylor series in a neighborhood of the integration domain), then convergence is determined by things like the closest singularity/pole in the complex plane and/or how oscillatory the function is.

If it’s not analytic, then convergence is usually determined by the biggest “non-smoothness”. For example, the lowest derivative that is discontinuous. Or, if it has an integrable singularity (a point where the integrand blows up in the domain, but slowly enough that the integral is still finite), then by the fastest rate of blowing up.

> [@Domenico\_Lahaye](#):
>
> I would expect 1/norm(x)^3 to be more singular than x[1]/norm(x)^3, but hcubature shows a lower function evaluation count.

That’s because for `1/norm(x)^3` it simply gives up without giving a finite answer: this is _not_ an integrable singularity in 3d (the integral is \infty).

```julia
julia> hcubature_count(x -> 1/norm(x)^3, (0,0,0), (1,1,1)) # non-integrable singularity
(Inf, NaN, 67287)

julia> hcubature_count(x -> x[1]/norm(x)^3, (0,0,0), (1,1,1)) # integrable singularity
(0.9693880526621552, 1.4444861385160079e-8, 1098471)

```

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [June 20, 2024, 7:07am UTC](https://discourse.julialang.org/t/hcubature-count/115878/7 "2024-06-20T07:07:03Z")

</div>

Many thanks for your input. Much appreciated.
