Special Functions Demo — Computing Gamma, Bessel & Zeta

The sangi special functions module handles functions that appear frequently in mathematical physics and statistics — the gamma function, Bessel functions, the zeta function — in a unified manner. This page presents three demos covering representative use cases.

Demo 1: The Gamma Function and Combinatorics

The gamma function $\Gamma(x)$ extends the factorial to real and complex numbers, with the famous values $\Gamma(n)=(n-1)!$ and $\Gamma(1/2)=\sqrt{\pi}$. It can also compute binomial coefficients.

=== Demo 1: Gamma function === gamma(0.5) = 1.772453851 (= sqrt(pi)) gamma(5.0) = 24 (= 4! = 24) binomialCoefficient(10, 3) = 120
$\Gamma(1/2)=\sqrt{\pi}\approx1.7724539$ and $\Gamma(5)=4!=24$. This confirms the gamma function as a continuous extension of the factorial.

Demo 2: The Error and Bessel Functions

The error function $\operatorname{erf}(x)$ is the integral of the normal distribution and appears in statistics and the diffusion equation. The Bessel functions $J_\nu(x)$ are solutions of the wave equation in cylindrical coordinates, with the values $J_0(0)=1$ and $J_1(0)=0$.

=== Demo 2: Error and Bessel functions === erf(1.0) = 0.8427007929 besselJ(0, 0.0) = 1 (= 1) besselJ(1, 0.0) = 0 (= 0)
$\operatorname{erf}(1)\approx0.8427$ corresponds to the probability of falling within $\pm1/\sqrt{2}\sigma$ for the standard normal distribution. $J_0(0)=1$ corresponds to the fundamental vibration at the center of a drum.

Demo 3: The Zeta Function and Elliptic Integral

The Riemann zeta function $\zeta(s)=\sum_{n\ge1} n^{-s}$ has closed-form values at even integers, with the famous $\zeta(2)=\pi^2/6$ (the Basel problem) and $\zeta(4)=\pi^4/90$. The complete elliptic integral $K(k)$ appears in the period of a simple pendulum and elsewhere, with $K(0)=\pi/2$.

=== Demo 3: Zeta function and elliptic integral === zeta(2.0) = 1.644934067 (= pi^2/6) zeta(4.0) = 1.082323234 (= pi^4/90) ellipticK(0.0) = 1.570796327 (= pi/2)
$\zeta(2)=\pi^2/6\approx1.6449$ is the answer to the Basel problem. $K(0)=\pi/2$ is the value (proportional to a $1/4$ period) of a simple pendulum in the small-amplitude limit.

Source Code and How to Run

example_specialfunctions.cpp (full source code)
// example_specialfunctions.cpp — Special functions demo
#include <math/special/special.hpp>
#include <iostream>
#include <iomanip>
using namespace sangi;
using namespace sangi::special;

int main() {
    std::cout << std::setprecision(10);

    // --- Demo 1: Gamma function and combinatorics ---
    std::cout << "=== Demo 1: Gamma function ===\n";
    std::cout << "  gamma(0.5) = " << gamma(0.5) << "   (= sqrt(pi))\n";
    std::cout << "  gamma(5.0) = " << gamma(5.0) << "   (= 4! = 24)\n";
    std::cout << "  binomialCoefficient(10, 3) = " << binomialCoefficient(10, 3) << "\n";

    // --- Demo 2: Error function and Bessel function ---
    std::cout << "\n=== Demo 2: Error and Bessel functions ===\n";
    std::cout << "  erf(1.0)       = " << erf(1.0) << "\n";
    std::cout << "  besselJ(0, 0.0) = " << besselJ(0, 0.0) << "   (= 1)\n";
    std::cout << "  besselJ(1, 0.0) = " << besselJ(1, 0.0) << "   (= 0)\n";

    // --- Demo 3: Zeta function and elliptic integral ---
    std::cout << "\n=== Demo 3: Zeta function and elliptic integral ===\n";
    std::cout << "  zeta(2.0)     = " << riemannZeta(2.0) << "   (= pi^2/6)\n";
    std::cout << "  zeta(4.0)     = " << riemannZeta(4.0) << "   (= pi^4/90)\n";
    std::cout << "  ellipticK(0.0) = " << ellipticK(0.0) << "   (= pi/2)\n";

    return 0;
}

For API details, see the Special Functions API Reference.

Build and Run

cd sangi
mkdir build && cd build
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release --target example-specialfunctions
examples\Release\example-specialfunctions.exe