GNU Scientific Library — statistics, random numbers, probability distributions, numerical integration, root finding, interpolation, FFT, ODEs, and special functions.
ppm install gsl
Requires libgsl-dev: sudo apt install libgsl-dev
Overview
GSL is the reference numerical library for C scientific computing, covering virtually every classical algorithm in applied mathematics and statistics.
It is tested to machine-precision accuracy and includes extensive documentation.
Statistics
uses gsl;
var data := [2.1, 4.5, 3.8, 7.2, 1.9, 5.5, 6.0, 3.3];
{ Full stats in one call }
var s := Stats(data);
Writeln('Mean: ' + FloatToStr(s.Mean));
Writeln('StdDev: ' + FloatToStr(s.StdDev));
Writeln('Median: ' + FloatToStr(s.Median));
Writeln('P25/P75: ' + FloatToStr(s.P25) + ' / ' + FloatToStr(s.P75));
Writeln('Skew: ' + FloatToStr(s.Skewness));
{ Linear regression }
var xs := [1.0, 2.0, 3.0, 4.0, 5.0];
var ys := [2.2, 4.1, 5.9, 8.0, 9.8];
var reg := LinearRegression(xs, ys);
Writeln('y = ' + FloatToStr(reg.Beta) + 'x + ' + FloatToStr(reg.Alpha));
Writeln('R² = ' + FloatToStr(reg.R2));
Random Numbers & Distributions
var rng := NewRngAuto(); { auto-seeded MT19937 }
{ Uniform }
Writeln(RngUniform(rng)); { [0, 1) }
Writeln(RngUniformInt(rng, 100)); { [0, 100) }
{ Normal }
Writeln(RngNormal(rng, 0, 1)); { standard normal }
Writeln(RngNormal(rng, 100, 15)); { IQ distribution }
{ Poisson — e.g. arrivals per minute }
Writeln(IntToStr(RngPoisson(rng, 3.5)));
{ Generate 1000 samples from any distribution }
var samples := RngSample(rng, distribGamma, 1000, 2.0, 1.0);
{ Shuffle in place }
Shuffle(rng, samples);
FreeRng(rng);
Numerical Integration
{ Integrate sin(x) from 0 to pi — result should be 2.0 }
var r := Integrate(0, 3.14159, 1e-6, 1e-6, 1000);
Writeln(FloatToStr(r.Value)); { ~2.0 }
Writeln(FloatToStr(r.AbsErr)); { absolute error estimate }
{ Integrate e^(-x²) from 0 to +inf = sqrt(pi)/2 }
var ri := IntegrateInf(0, 1e-8, 1e-8, 500);
Root Finding & Minimization
{ Find root of cos(x) in [0, 2] — should be pi/2 ≈ 1.5708 }
var root := FindRoot(0, 2, 1e-10, 100);
Writeln(FloatToStr(root.Root));
{ Minimize x² + sin(x) in [-2, 2] }
var minr := Minimize1D(-2, 2, 1e-8, 200);
Writeln('Minimum at x = ' + FloatToStr(minr.XMin));
Writeln('f(x) = ' + FloatToStr(minr.FMin));
Interpolation
var xs := [0.0, 1.0, 2.0, 3.0, 4.0];
var ys := [0.0, 0.84, 0.91, 0.14, -0.76]; { sin values }
var spl := NewSpline(xs, ys, splineCubic);
Writeln(FloatToStr(SplineEval(spl, 1.5))); { interpolated }
Writeln(FloatToStr(SplineEvalDeriv(spl, 1.5))); { derivative }
Writeln(FloatToStr(SplineIntegral(spl, 0, 4))); { integral 0..4 }
{ Evaluate at many points at once }
var query := [0.5, 1.0, 1.5, 2.0, 2.5];
var vals := SplineEvalArray(spl, query);
FreeSpline(spl);
FFT
{ Power spectrum of a signal with 512 samples }
var signal := { ... array of 512 doubles ... }
var power := PowerSpectrum(signal);
{ power[k] = magnitude² at frequency k/N }
{ Round-trip }
var freq := RFFT(signal);
var back := IRFFT(freq, 512);
API Reference
Statistics
| Function | Description |
| Stats(Data) | All stats in one pass: TGSLStats record. |
| Mean / Variance / StdDev / Median(Data) | Individual statistics. |
| Percentile(Data, P) | P-th percentile (0–100). |
| Skewness / Kurtosis(Data) | Distribution shape. |
| Covariance / Correlation(X, Y) | Pearson covariance and correlation. |
| SpearmanCorrelation(X, Y) | Rank-based correlation. |
| LinearRegression(X, Y) | Y = Alpha + Beta*X with R², covariances. |
| WeightedMean / WeightedVariance(Data, W) | Weighted statistics. |
| Sort(Data) / Sorted(Data) | In-place sort or sorted copy. |
Random Numbers
| Function | Description |
| NewRng(Type) / NewRngAuto | Create RNG (auto-seeded with current time). |
| SeedRng(Rng, Seed) | Set deterministic seed. |
| RngUniform / RngUniformInt | Uniform real [0,1) or integer [0,N). |
| RngNormal(Rng, Mean, Sigma) | Gaussian variate. |
| RngExponential / RngPoisson / RngBinomial | Common distributions. |
| RngGamma / RngBeta | Gamma and Beta variates. |
| RngSample(Rng, Dist, N, P1, P2) | Generate N samples from any distribution. |
| Shuffle(Rng, Data) | Fisher-Yates shuffle in place. |
| FreeRng(Rng) | Free RNG resources. |
Distributions — CDF / PDF
| Function | Description |
| NormalPDF / NormalCDF / NormalCDFInv | Gaussian PDF, CDF, quantile. |
| PoissonPMF / BinomialPMF | Discrete probability mass. |
| ChiSqCDF / TCdf | Chi-squared and Student-t CDFs. |
Integration, Roots, Minimization
| Function | Description |
| Integrate(A, B, AbsTol, RelTol, Limit) | Adaptive Gauss-Kronrod QAGS. |
| IntegrateInf(A, ...) | Semi-infinite integral [A, +∞). |
| GaussLegendre(A, B, Order) | Fixed-order quadrature (fast). |
| FindRoot(A, B, AbsTol, MaxIter) | Root finding in [A,B] via Brent. |
| FindRootNewton(X0, AbsTol, MaxIter) | Newton-Raphson near X0. |
| Minimize1D(A, B, AbsTol, MaxIter) | 1D minimization via Brent. |
Interpolation & FFT
| Function | Description |
| NewSpline(X, Y, Type) | Linear, cubic, Akima, or Steffen spline. |
| SplineEval / EvalDeriv / Integral | Evaluate spline, derivative, or integral. |
| SplineEvalArray(Spline, X) | Batch evaluation. |
| FreeSpline(Spline) | Free spline resources. |
| RFFT / IRFFT(Data) | Real FFT and inverse FFT. N must be power of 2. |
| PowerSpectrum(Data) | |FFT|² — frequency power at each bin. |
Special Functions
| Function | Description |
| GammaFn / LogGamma(X) | Gamma function and log-Gamma. |
| BetaFn(A, B) / BetaInc(X, A, B) | Beta and regularized incomplete beta. |
| Erf / Erfc(X) | Error function and complement. |
| BesselJ0 / J1 / Jn(N, X) | Bessel functions of the first kind. |
| LegendreP(L, X) | Legendre polynomial P_l(x). |
| BinomCoeff(N, K) | Binomial coefficient C(N,K). |
| Factorial(N) | N! as Double. |