Skip to main content

scirs2_core/api_reference/
math_reference.rs

1//! Mathematical reference for key algorithms used throughout SciRS2.
2//!
3//! Each [`MathReference`] describes an algorithm's mathematical foundation,
4//! formula, computational complexity, and academic references.
5
6/// A mathematical reference entry for a numerical algorithm.
7#[derive(Debug, Clone)]
8pub struct MathReference {
9    /// Algorithm name (e.g. "LU Decomposition")
10    pub algorithm: &'static str,
11    /// Plain-text description of the algorithm
12    pub description: &'static str,
13    /// Mathematical formula or key equation in plain text
14    pub formula: &'static str,
15    /// Computational complexity (time and space)
16    pub complexity: &'static str,
17    /// Academic / textbook references
18    pub references: &'static [&'static str],
19}
20
21/// Returns the full static math reference table.
22pub fn math_references() -> &'static [MathReference] {
23    &MATH_REFERENCES
24}
25
26/// Search math references by keyword (case-insensitive substring match).
27pub fn search_math(query: &str) -> Vec<&'static MathReference> {
28    let q = query.to_ascii_lowercase();
29    MATH_REFERENCES
30        .iter()
31        .filter(|r| {
32            r.algorithm.to_ascii_lowercase().contains(&q)
33                || r.description.to_ascii_lowercase().contains(&q)
34        })
35        .collect()
36}
37
38static MATH_REFERENCES: [MathReference; 20] = [
39    // 1. LU Decomposition
40    MathReference {
41        algorithm: "LU Decomposition",
42        description: "Factor a square matrix A into a product of a lower triangular matrix L \
43            and an upper triangular matrix U, with partial pivoting (PA = LU). \
44            Foundation for solving linear systems, computing determinants, and matrix inversion.",
45        formula: "PA = LU where P is permutation, L is unit lower triangular (L_ii = 1), U is upper triangular. \
46            Forward substitution: Ly = Pb, O(n^2). \
47            Back substitution: Ux = y, O(n^2). \
48            Total solve: O(2n^3/3) factorization + O(n^2) per right-hand side.",
49        complexity: "Time: O(2n^3/3) for factorization. Space: O(n^2) in-place.",
50        references: &[
51            "Golub & Van Loan, 'Matrix Computations', 4th ed., Ch. 3",
52            "Trefethen & Bau, 'Numerical Linear Algebra', Lecture 20-21",
53        ],
54    },
55    // 2. QR Decomposition (Householder)
56    MathReference {
57        algorithm: "QR Decomposition (Householder Reflections)",
58        description: "Factor an m x n matrix A into Q (orthogonal, m x m) and R (upper triangular, m x n) \
59            using Householder reflections. Each reflection zeroes out sub-diagonal entries of one column. \
60            Numerically stable and the standard method for least-squares and eigenvalue algorithms.",
61        formula: "A = QR where Q^T Q = I. Each Householder matrix: H_k = I - 2 v_k v_k^T / (v_k^T v_k). \
62            v_k chosen so H_k zeroes entries below diagonal in column k. \
63            For least squares: Rx = Q^T b (overdetermined systems).",
64        complexity: "Time: O(2mn^2 - 2n^3/3) for m x n matrix. Space: O(mn).",
65        references: &[
66            "Golub & Van Loan, 'Matrix Computations', 4th ed., Ch. 5",
67            "Trefethen & Bau, 'Numerical Linear Algebra', Lectures 7-11",
68        ],
69    },
70    // 3. SVD
71    MathReference {
72        algorithm: "Singular Value Decomposition (SVD)",
73        description: "Decompose any m x n matrix A = U Sigma V^T where U (m x m) and V (n x n) are orthogonal, \
74            Sigma is diagonal with non-negative singular values sigma_1 >= sigma_2 >= ... >= 0. \
75            The gold standard for rank determination, pseudoinverse, PCA, and low-rank approximation.",
76        formula: "A = U diag(sigma_1, ..., sigma_r, 0, ..., 0) V^T. \
77            Eckart-Young theorem: best rank-k approx is A_k = sum_{i=1}^k sigma_i u_i v_i^T. \
78            ||A - A_k||_2 = sigma_{k+1}, ||A - A_k||_F = sqrt(sum_{i>k} sigma_i^2).",
79        complexity: "Time: O(min(m n^2, m^2 n)) via Golub-Kahan bidiagonalization + QR iteration. Space: O(mn).",
80        references: &[
81            "Golub & Van Loan, 'Matrix Computations', 4th ed., Ch. 8",
82            "Demmel, 'Applied Numerical Linear Algebra', Ch. 3",
83        ],
84    },
85    // 4. Cholesky
86    MathReference {
87        algorithm: "Cholesky Decomposition",
88        description: "Factor a symmetric positive-definite (SPD) matrix A = L L^T where L is lower triangular \
89            with positive diagonal entries. About 2x faster than LU for SPD matrices. \
90            Used in Gaussian processes, Kalman filters, and constrained optimization.",
91        formula: "L_jj = sqrt(A_jj - sum_{k=0}^{j-1} L_jk^2). \
92            L_ij = (A_ij - sum_{k=0}^{j-1} L_ik L_jk) / L_jj for i > j. \
93            Existence iff A is SPD. Failure of algorithm indicates non-positive-definiteness.",
94        complexity: "Time: O(n^3/3). Space: O(n^2) (can overwrite lower triangle of A).",
95        references: &[
96            "Golub & Van Loan, 'Matrix Computations', 4th ed., Ch. 4.2",
97            "Press et al., 'Numerical Recipes', Ch. 2.9",
98        ],
99    },
100    // 5. Conjugate Gradient
101    MathReference {
102        algorithm: "Conjugate Gradient Method (CG)",
103        description: "Iterative solver for Ax = b where A is SPD. Generates conjugate directions that are \
104            A-orthogonal (d_i^T A d_j = 0 for i != j). Minimizes ||x - x*||_A in a Krylov subspace. \
105            Converges in at most n iterations (exact arithmetic); often much faster with preconditioning.",
106        formula: "r_0 = b - A x_0, d_0 = r_0. \
107            alpha_k = r_k^T r_k / (d_k^T A d_k). \
108            x_{k+1} = x_k + alpha_k d_k. \
109            r_{k+1} = r_k - alpha_k A d_k. \
110            beta_k = r_{k+1}^T r_{k+1} / (r_k^T r_k). \
111            d_{k+1} = r_{k+1} + beta_k d_k.",
112        complexity: "Time: O(n * nnz(A)) per iteration (one SpMV). Total: O(sqrt(kappa) * n * nnz(A)) \
113            where kappa = cond(A). Space: O(n).",
114        references: &[
115            "Hestenes & Stiefel, 1952, 'Methods of conjugate gradients for solving linear systems'",
116            "Shewchuk, 'An Introduction to the Conjugate Gradient Method Without the Agonizing Pain', 1994",
117        ],
118    },
119    // 6. GMRES
120    MathReference {
121        algorithm: "GMRES (Generalized Minimal Residual)",
122        description: "Iterative solver for general (non-symmetric) linear systems Ax = b. \
123            Minimizes ||b - A x_k||_2 over the Krylov subspace span{r_0, A r_0, ..., A^{k-1} r_0}. \
124            Uses Arnoldi iteration to build an orthonormal basis, then solves a small least-squares problem.",
125        formula: "Build orthonormal basis V_k via Arnoldi: A V_k = V_{k+1} H_k (upper Hessenberg). \
126            Minimize ||beta e_1 - H_k y||_2, then x_k = V_k y. \
127            Restarted GMRES(m): restart after m iterations to limit memory.",
128        complexity: "Time: O(k * nnz(A) + k^2 n) for k iterations. Space: O(kn) for storing Krylov basis. \
129            GMRES(m): O(m * nnz(A)) per cycle, O(mn) storage.",
130        references: &[
131            "Saad & Schultz, 'GMRES: A Generalized Minimal Residual Algorithm', 1986",
132            "Saad, 'Iterative Methods for Sparse Linear Systems', 2nd ed., Ch. 6",
133        ],
134    },
135    // 7. FFT (Cooley-Tukey)
136    MathReference {
137        algorithm: "Fast Fourier Transform (Cooley-Tukey)",
138        description: "Compute the DFT of length N in O(N log N) operations by recursively decomposing into \
139            smaller DFTs. The radix-2 variant requires N = 2^m; mixed-radix handles arbitrary N. \
140            Foundation for spectral analysis, convolution, correlation, and signal filtering.",
141        formula: "X[k] = sum_{n=0}^{N-1} x[n] W_N^{nk} where W_N = exp(-j 2 pi / N). \
142            Radix-2: X[k] = E[k] + W_N^k O[k] where E, O are DFTs of even/odd indexed samples. \
143            Inverse: x[n] = (1/N) sum_{k=0}^{N-1} X[k] W_N^{-nk}.",
144        complexity: "Time: O(N log N) (vs O(N^2) for naive DFT). Space: O(N) in-place.",
145        references: &[
146            "Cooley & Tukey, 'An Algorithm for the Machine Calculation of Complex Fourier Series', 1965",
147            "Van Loan, 'Computational Frameworks for the Fast Fourier Transform', SIAM, 1992",
148        ],
149    },
150    // 8. Newton's Method (root-finding)
151    MathReference {
152        algorithm: "Newton's Method (Newton-Raphson)",
153        description: "Iterative root-finding for f(x) = 0. Uses first-order Taylor expansion to linearize \
154            the function at each step. Quadratic convergence near simple roots. \
155            For systems: solves J(x_k) delta = -F(x_k) at each step.",
156        formula: "Scalar: x_{k+1} = x_k - f(x_k) / f'(x_k). \
157            Vector: x_{k+1} = x_k - J(x_k)^{-1} F(x_k) where J is the Jacobian. \
158            Convergence: |e_{k+1}| ~ C |e_k|^2 (quadratic) for simple roots.",
159        complexity: "Time per iteration: O(1) scalar, O(n^3) vector (Jacobian solve). \
160            Iterations to convergence: O(log log(1/eps)) near root.",
161        references: &[
162            "Burden & Faires, 'Numerical Analysis', Ch. 2",
163            "Kelley, 'Iterative Methods for Linear and Nonlinear Equations', SIAM, 1995",
164        ],
165    },
166    // 9. Gauss-Kronrod Quadrature
167    MathReference {
168        algorithm: "Gauss-Kronrod Adaptive Quadrature",
169        description: "Adaptive numerical integration that embeds a Gauss n-point rule within a Kronrod \
170            (2n+1)-point rule. The difference provides an error estimate for automatic step refinement. \
171            The standard adaptive quadrature method in most numerical libraries.",
172        formula: "G_n = sum_{i=1}^n w_i f(x_i) (Gauss rule, exact for polynomials up to deg 2n-1). \
173            K_{2n+1} = sum_{i=1}^{2n+1} w'_i f(x'_i) (Kronrod extension). \
174            Error estimate: |G_n - K_{2n+1}|. Bisect intervals with large errors.",
175        complexity: "Time: O(N f_evals) where N depends on integrand smoothness. \
176            G7-K15: 15 function evaluations per panel.",
177        references: &[
178            "Piessens et al., 'QUADPACK', Springer, 1983",
179            "Kronrod, 'Nodes and Weights of Quadrature Formulas', 1965 (English transl.)",
180        ],
181    },
182    // 10. Runge-Kutta Methods
183    MathReference {
184        algorithm: "Runge-Kutta Methods (RK4, RK45 Dormand-Prince)",
185        description: "Family of explicit ODE solvers for dy/dt = f(t, y). RK4 is the classical 4th-order method. \
186            RK45 (Dormand-Prince) uses embedded 4th and 5th order formulas for adaptive step-size control. \
187            The default ODE solver in most scientific computing libraries.",
188        formula: "RK4: k1 = h f(t, y), k2 = h f(t+h/2, y+k1/2), k3 = h f(t+h/2, y+k2/2), k4 = h f(t+h, y+k3). \
189            y_{n+1} = y_n + (k1 + 2k2 + 2k3 + k4)/6. \
190            Dormand-Prince: 7-stage, embedded pair gives error = |y5 - y4| for step control.",
191        complexity: "Time: O(s * n) per step where s is number of stages (4 for RK4, 7 for DP). \
192            Total steps depend on required accuracy and problem stiffness.",
193        references: &[
194            "Dormand & Prince, 'A family of embedded Runge-Kutta formulae', J. Comp. Appl. Math., 1980",
195            "Hairer, Norsett & Wanner, 'Solving Ordinary Differential Equations I', Springer",
196        ],
197    },
198    // 11. L-BFGS
199    MathReference {
200        algorithm: "L-BFGS (Limited-memory BFGS)",
201        description: "Quasi-Newton optimization method that approximates the inverse Hessian using \
202            the m most recent gradient differences. Ideal for large-scale unconstrained optimization \
203            where storing the full Hessian (n x n) is impractical.",
204        formula: "Store pairs (s_k, y_k) where s_k = x_{k+1} - x_k, y_k = g_{k+1} - g_k. \
205            Two-loop recursion computes H_k g_k using only the m stored pairs. \
206            Search direction: d_k = -H_k g_k. Line search: strong Wolfe conditions.",
207        complexity: "Time: O(mn) per iteration (vs O(n^2) for full BFGS). \
208            Space: O(mn) (vs O(n^2) for full BFGS). Typical m = 5-20.",
209        references: &[
210            "Liu & Nocedal, 'On the limited memory BFGS method for large scale optimization', Math. Programming, 1989",
211            "Nocedal & Wright, 'Numerical Optimization', 2nd ed., Ch. 7",
212        ],
213    },
214    // 12. Butterworth Filter Design
215    MathReference {
216        algorithm: "Butterworth Filter Design",
217        description: "Design maximally-flat magnitude IIR filters. The Butterworth filter has no ripple \
218            in either passband or stopband (monotonic response). Poles are equally spaced on a circle \
219            in the s-plane. Bilinear transform maps analog prototype to digital filter.",
220        formula: "|H(j omega)|^2 = 1 / (1 + (omega/omega_c)^{2N}) where N is filter order. \
221            Analog poles: s_k = omega_c exp(j pi (2k + N - 1) / (2N)) for k = 1, ..., N. \
222            Bilinear transform: s = 2/T * (z-1)/(z+1) with frequency pre-warping.",
223        complexity: "Time: O(N) for coefficient computation. O(N) per sample for filtering.",
224        references: &[
225            "Oppenheim & Schafer, 'Discrete-Time Signal Processing', 3rd ed., Ch. 7",
226            "Parks & Burrus, 'Digital Filter Design', Wiley, 1987",
227        ],
228    },
229    // 13. Welch's PSD Estimation
230    MathReference {
231        algorithm: "Welch's Method for Power Spectral Density",
232        description: "Estimate the power spectral density by averaging modified periodograms of \
233            overlapping windowed segments. Reduces variance of the periodogram estimate at the cost \
234            of reduced frequency resolution. Standard non-parametric spectral estimation method.",
235        formula: "PSD(f) = (1/K) sum_{k=1}^K (1/(f_s L U)) |sum_{n=0}^{L-1} w[n] x_k[n] e^{-j 2 pi f n / f_s}|^2 \
236            where U = (1/L) sum w[n]^2, K segments, L samples per segment. \
237            Variance reduction: var ~ var(periodogram) / K.",
238        complexity: "Time: O(K * L log L) for K segments of length L. Space: O(L).",
239        references: &[
240            "Welch, 'The use of FFT for the estimation of power spectra', IEEE Trans., 1967",
241            "Stoica & Moses, 'Spectral Analysis of Signals', Prentice Hall, 2005",
242        ],
243    },
244    // 14. Lanczos Approximation (Gamma function)
245    MathReference {
246        algorithm: "Lanczos Approximation for the Gamma Function",
247        description: "Compute Gamma(z) for complex z using a series approximation based on the \
248            Stirling series with improved convergence. Achieves 15+ digit accuracy with ~7 terms. \
249            The standard method for gamma function evaluation in most numerical libraries.",
250        formula: "Gamma(z+1) ~ sqrt(2 pi) (z + g + 0.5)^{z+0.5} e^{-(z+g+0.5)} A_g(z) \
251            where A_g(z) = c_0 + sum_{k=1}^N c_k / (z + k). \
252            g ~ 7 (Lanczos parameter), coefficients c_k are precomputed. \
253            Reflection: Gamma(z) = pi / (sin(pi z) Gamma(1-z)) for z < 0.5.",
254        complexity: "Time: O(N) ~ O(7) per evaluation. Space: O(1) (precomputed coefficients).",
255        references: &[
256            "Lanczos, 'A Precision Approximation of the Gamma Function', SIAM J. Numer. Anal., 1964",
257            "Pugh, 'An Analysis of the Lanczos Gamma Approximation', PhD thesis, 2004",
258        ],
259    },
260    // 15. Gauss-Legendre Quadrature
261    MathReference {
262        algorithm: "Gauss-Legendre Quadrature",
263        description: "Numerical integration using optimally chosen nodes and weights based on Legendre \
264            polynomials. An n-point rule integrates polynomials of degree up to 2n-1 exactly. \
265            The gold standard for smooth integrands on finite intervals.",
266        formula: "integral_{-1}^{1} f(x) dx ~ sum_{i=1}^n w_i f(x_i). \
267            Nodes x_i are roots of P_n(x) (Legendre polynomial of degree n). \
268            Weights: w_i = 2 / ((1 - x_i^2) [P'_n(x_i)]^2). \
269            Transform to [a,b]: integral_a^b f(x) dx = (b-a)/2 * sum w_i f((b-a)x_i/2 + (a+b)/2).",
270        complexity: "Time: O(n^2) to compute nodes/weights (Golub-Welsch), O(n) per integral evaluation.",
271        references: &[
272            "Golub & Welsch, 'Calculation of Gauss Quadrature Rules', Math. Comp., 1969",
273            "Abramowitz & Stegun, 'Handbook of Mathematical Functions', Ch. 25",
274        ],
275    },
276    // 16. Cubic Spline Interpolation
277    MathReference {
278        algorithm: "Cubic Spline Interpolation",
279        description: "Construct a piecewise cubic polynomial S(x) that passes through all data points \
280            with continuous first and second derivatives (C2 smoothness). Natural splines set \
281            S''(x_0) = S''(x_n) = 0. The most widely used interpolation method.",
282        formula: "On each interval [x_i, x_{i+1}]: \
283            S_i(x) = a_i + b_i(x-x_i) + c_i(x-x_i)^2 + d_i(x-x_i)^3. \
284            Continuity of S, S', S'' at interior knots gives a tridiagonal system for the c_i \
285            (second derivatives). Natural BC: c_0 = c_n = 0.",
286        complexity: "Time: O(n) for setup (tridiagonal solve). O(log n) per evaluation (binary search).",
287        references: &[
288            "de Boor, 'A Practical Guide to Splines', Springer, 2001",
289            "Burden & Faires, 'Numerical Analysis', Ch. 3.5",
290        ],
291    },
292    // 17. Student's t-test
293    MathReference {
294        algorithm: "Student's t-Test",
295        description: "Hypothesis test for comparing means. One-sample: test if population mean equals mu_0. \
296            Two-sample: test if two population means differ. Paired: test if mean of differences is zero. \
297            Assumes approximately normal distributions; robust for large samples.",
298        formula: "One-sample: t = (x_bar - mu_0) / (s / sqrt(n)), df = n - 1. \
299            Two-sample (equal var): t = (x1_bar - x2_bar) / (s_p sqrt(1/n1 + 1/n2)), df = n1 + n2 - 2. \
300            Welch: df = (s1^2/n1 + s2^2/n2)^2 / (s1^4/(n1^2(n1-1)) + s2^4/(n2^2(n2-1))). \
301            p-value: 2 * P(T > |t|) where T ~ t(df).",
302        complexity: "Time: O(n) for test statistic, O(1) for p-value (CDF evaluation).",
303        references: &[
304            "Student (W.S. Gosset), 'The probable error of a mean', Biometrika, 1908",
305            "Welch, 'The generalization of Student's problem', Biometrika, 1947",
306        ],
307    },
308    // 18. Pearson Correlation
309    MathReference {
310        algorithm: "Pearson Product-Moment Correlation",
311        description: "Measure the linear relationship between two continuous variables. \
312            Values range from -1 (perfect negative) through 0 (no linear relationship) to +1 (perfect positive). \
313            Assumes bivariate normality for inference; robust for large samples.",
314        formula: "r = sum((x_i - x_bar)(y_i - y_bar)) / sqrt(sum(x_i - x_bar)^2 * sum(y_i - y_bar)^2). \
315            Equivalently: r = cov(x, y) / (std(x) * std(y)). \
316            Significance test: t = r sqrt(n-2) / sqrt(1-r^2), df = n - 2.",
317        complexity: "Time: O(n) single-pass with SIMD acceleration. Space: O(1) streaming.",
318        references: &[
319            "Pearson, 'Notes on regression and inheritance', Proc. R. Soc. London, 1895",
320            "Fisher, 'Frequency distribution of the values of the correlation coefficient', Biometrika, 1915",
321        ],
322    },
323    // 19. Adaptive Simpson's Rule
324    MathReference {
325        algorithm: "Adaptive Simpson's Quadrature",
326        description: "Adaptive version of Simpson's 1/3 rule that recursively bisects intervals \
327            where the error estimate exceeds a tolerance. Achieves prescribed accuracy with fewer \
328            function evaluations than fixed-step methods for integrands with varying smoothness.",
329        formula: "Simpson's rule: S(a,b) = (h/3)[f(a) + 4f(m) + f(b)] where m = (a+b)/2. \
330            Error estimate: |S(a,b) - S(a,m) - S(m,b)| / 15 (Richardson extrapolation). \
331            If error > tol * (b-a)/(b0-a0), bisect and recurse.",
332        complexity: "Time: O(N * f_evals) where N adapts to integrand difficulty. \
333            Best case: O(1) for smooth functions; worst case: O(2^d) for d levels of refinement.",
334        references: &[
335            "Kuncir, 'Algorithm 103: Simpson's rule integrator', CACM, 1962",
336            "Lyness, 'Notes on the adaptive Simpson quadrature routine', JACM, 1969",
337        ],
338    },
339    // 20. Normal Distribution
340    MathReference {
341        algorithm: "Normal (Gaussian) Distribution",
342        description: "The most important probability distribution in statistics, arising from the \
343            Central Limit Theorem. Characterized by mean (mu) and standard deviation (sigma). \
344            The standard normal (mu=0, sigma=1) CDF is denoted Phi(x).",
345        formula: "PDF: f(x) = (1 / (sigma sqrt(2 pi))) exp(-(x - mu)^2 / (2 sigma^2)). \
346            CDF: Phi(x) = 0.5 (1 + erf((x - mu) / (sigma sqrt(2)))). \
347            PPF (quantile): uses Acklam's rational approximation (9-15 digit precision). \
348            MGF: M(t) = exp(mu t + sigma^2 t^2 / 2). \
349            Entropy: H = 0.5 ln(2 pi e sigma^2).",
350        complexity: "PDF: O(1). CDF: O(1) via rational approximation of erf. Random sampling: O(1) Box-Muller.",
351        references: &[
352            "Abramowitz & Stegun, 'Handbook of Mathematical Functions', Ch. 26",
353            "Acklam, 'An algorithm for computing the inverse normal CDF', 2010",
354        ],
355    },
356];