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 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 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$.
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