begin
    import Pkg as _Pkg
    haskey(ENV, "PLUTO_PROJECT") && _Pkg.activate(ENV["PLUTO_PROJECT"])
    using CairoMakie
    using Revise
    using VoronoiFVM
    using ForwardDiff: derivative
end

Bernoulli function test

We test the implementation of the Bernoulli function in VoronoiFVM against the evaluation with BigFloat. This allows to optimize thresholds for switching between evaluation expressions.

Reference with BigFLoat

function B_Big(x)
    bx = BigFloat(x)
    Float64(bx / expm1(bx))
end
B_Big (generic function with 1 method)
function DB_Big(x)
    bx = BigFloat(x)
    bone = one(BigFloat)
    bex = exp(bx)
    b = -(x * bex - bex + bone) / ((bex - bone) * (bex - bone))
    Float64(b)
end
DB_Big (generic function with 1 method)

Implementation using expm1

B(x)=x/expm1(x)
B (generic function with 1 method)

Approximation for small x

For small values of x, a Taylor approximation implemented using a Horner scheme is utilized, as the exponential expression runs into errors in the vicinity of zero and fails to evaluate at zero.. As as long as its error is large than that of the Taylor approximation calculated with the Taylor scheme, we should use the later one.

B(0.0)
NaN
fbernoulli(0.0)
1.0
B(nextfloat(0.0))
1.0
fbernoulli(nextfloat(0.0))
1.0
derivative(B,0.0)
NaN
derivative(fbernoulli, 0.0)
-0.5
derivative(B,nextfloat(0.0))
NaN
derivative(fbernoulli, nextfloat(0.0))
-0.5
smallX = collect(-0.5:(1.0e-4 + 1.0e-8):0.5);

Error comparison for VoronoiFVM implementation

largeX = -100:1.00001e-3:100;

Derivative error


Built with Julia 1.11.1 and

CairoMakie 0.12.16
ForwardDiff 0.10.38
Pkg 1.11.0
Revise 3.5.18
VoronoiFVM 1.25.1