Stand-alone code for numerical computing

For this week’s resource post, see the page Stand-alone code for numerical computing. It points to small, self-contained bits of code for special functions (log gamma, erf, etc.) and for random number generation (normal, Poisson, gamma, etc.).

The code is available in Python, C++, and C# versions. It could easily be translated into other languages since it hardly uses any language-specific features.

I wrote these functions for projects where you don’t have a numerical library available or would like to minimize dependencies. If you have access to a numerical library, such as SciPy in Python, then by all means use it (although SciPy is missing some of the random number generators provided here). In C++ and especially C#, it’s harder to find some of this functionality.

Last week: Code Project articles

Next week: Clinical trial software

One thought on “Stand-alone code for numerical computing

  1. I found an elegant version of ExpM1 for C#, based on rearranging the hyperbolic sine, which System.Math provides:

            public static double ExpM1(this double x)
            {
                if (x >= -0.69314718055994530941723212145818 && x <= 0.40546510810816438197801311546435)
                {
                    double y = 0.5 * x;
                    return 2 * Math.Exp(y) * Math.Sinh(y);
                }
                else
                {
                    return Math.Exp(x) - 1;
                }
            }
    

    More accurate than the naïve version in my testing, and I confirmed it handles all the special cases (+/-0, Nan, +/-Inf) correctly.

    I put that code in the public domain. Do whatever you want with it, no strings attached. Use at your own risk.

Comments are closed.