mathnet-numerics icon indicating copy to clipboard operation
mathnet-numerics copied to clipboard

Missing parentheses means BesselK1e incorrectly evaluates for small input values

Open meryck2000-blip opened this issue 1 month ago • 2 comments

https://github.com/mathnet/mathnet-numerics/blob/84e9432bc1e8a6b2ae7acd32cda4175abeaeaed1/src/Numerics/SpecialFunctions/ModifiedBessel.cs#L285

Should read: return (Math.Log(0.5 * x) * BesselI1(x) + Evaluate.ChebyshevA(BesselK1A, y) / x) * Math.Exp(x);

meryck2000-blip avatar Dec 04 '25 12:12 meryck2000-blip

@meryck2000-blip Thanks for pointing this out — you're right.

$K_{1e}(x)$ is defined as $e^x K_1(x)$, but because of the missing parentheses, the current implementation applies the $e^x$ factor only to one of the terms in the $K_1(x)$ expression. This is a bug.

Would you be willing to submit a Pull Request (PR) for this?

diluculo avatar Dec 05 '25 11:12 diluculo

@diluculo: Sure! Can fork and make PR - there are Bessel function unit tests already, but can't see any for this function. Probably best I do the job properly, and add test that compares K1e(x) vs e^x.K1(x) for a few key values and ranges (with comparison epsilon to allow small numerical differences). Can also validate against BesselKScaled(1, x), which should also produce the same result. Will take a look tonight!

meryck2000-blip avatar Dec 05 '25 12:12 meryck2000-blip