Comments (7)
So finally, thanks for this discussion, which lead to a more concise implementation.
from voronoifvm.jl.
Thanks for the hint! Not sure why I was not aware of that...
But it is not that bad now either:
julia> x=1.0e-15
1.0e-15
julia> x/expm1(x)
0.9999999999999994
julia> VoronoiFVM.bernoulli_horner(x)
0.9999999999999994
So I need to see what I can gain. Will try it out.
from voronoifvm.jl.
I checked it in python, and the two formulas match to within 1e-16 at your threshold, which is at the limits of float64.
I guess the only difference would be the speed difference in the two implementations in julia. The implementation I use in my C++ solver is equivalent to:
y = expm1(x)
// this test may only be true for x == 0
if (x != y)
{
b = x/y;
}
else
{
// including x is probably overkill
b = 1 / (1 + 0.5*x);
}
I am curious though about:
VoronoiFVM.jl/src/vfvm_functions.jl
Line 82 in 975d5b1
and why not use:
bm = bp + x
as done here:
VoronoiFVM.jl/src/vfvm_functions.jl
Line 86 in 975d5b1
from voronoifvm.jl.
Well, I don't recall why...
I started a PR with an implentation using expm1
which indeed appears to be more robust.
However I indeed will keep the Horner scheme around zero. Bernoulli function with expm1 is good for small arguments, however the automatic derivative (which I use to create the Jacobians) fails:
julia> B(x)=x/expm1(x);
julia> B0(x)=1/(1+0.5x);
julia> B0(0.0)
1.0
julia> B0(nextfloat(0.0))
1.0
julia> ForwardDiff.derivative(B0,0)
-0.5
julia> ForwardDiff.derivative(B0,nextfloat(0.0))
-0.5
julia> ForwardDiff.derivative(B,nextfloat(0.0))
NaN
from voronoifvm.jl.
As for the approximations which are now in the PR:
... thanks for nerd sniping me!
from voronoifvm.jl.
Very cool. I had to look up what Nerd Sniping was.
from voronoifvm.jl.
In my code, this is what I end up using for the derivative of B(x)
:
const auto ex1 = expm1(x);
if (x != ex1)
{
const auto ex2 = ex1 - (x * exp(x));
ret = ex2;
ret *= pow(ex1, -2);
}
else
{
DoubleType num = static_cast<DoubleType>(-0.5);
DoubleType den = static_cast<DoubleType>( 1.0);
num -= x / static_cast<DoubleType>(3.);
den += x;
ret = num / den;
}
and so it works well even with extended precision. The full implementation is here:
https://github.com/devsim/devsim/blob/main/src/MathEval/Bernoulli.cc#L102
where I have to account for numerical issues I was having with the Android version of expm1
not working well with the x != expm1(x)
test.
from voronoifvm.jl.
Related Issues (20)
- Discontinous quantities: unexpected behavior HOT 3
- Update _initialize! to reflect new boundary condition API
- Provide function which returns solution on boundary region HOT 2
- hypoellictic operator HOT 3
- Could please help to solve this problem? HOT 2
- Jump Conditions HOT 3
- Fix allocation in interpolation
- Better documentation of test functions
- VoronoiFVM.norm for transient solutions
- Postprocess extraction of gradients at boundaries HOT 11
- Precompilation
- couple two physics in two domains HOT 3
- ` make_all` in `docs/make.jl` ignores all input variables.
- Bug(s) in src/vfvm_assemblydata.jl HOT 1
- Drop support of Julia 1.6 HOT 1
- Try out DifferentiationInterface for sparse autodiff? HOT 2
- time dependent dirichlet boundary condition HOT 3
- time derivative dependent source/reaction term HOT 4
- Error with ODEProblem and sparse solution storage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from voronoifvm.jl.