AI, Deep Learning & 3D Game Engine Math Applications
Discover how textbook math equations directly power Large Language Models, 3D game shaders, quant finance, and modern cryptography with live code snippets.
- Dot Product (#L02): Measures Query-Key semantic alignment across tokens
- Matrix Multiplication (#L04): Parallelizes multi-head projection on GPU Tensor Cores
- Softmax (#P10): Converts raw logits into temperature-controlled token probabilities
- Cross-Entropy (#P09) & Chain Rule (#C03): Evaluates loss and backpropagates gradients
Large Language Models (LLM & Transformer)
Computes contextual token similarity via scaled dot products, normalizes weights with Softmax, and backpropagates Cross-Entropy Loss via the Chain Rule.
Dot Product & Cosine Similarity
LLM Transformer Attention (Q·K^T), RAG vector DB cosine search, and 3D Lambertian lighting (N·L)
const cosSim = (a:number[], b:number[]) => dot(a,b) / (norm(a) * norm(b));Matrix Multiplication & Linear Transformation
90% of NVIDIA GPU Tensor Core compute in Deep Learning (Y = XW) and 3D graphics MVP pipelines
const matMul2 = (A:number[][], B:number[][]) => [[A[0][0]*B[0][0]+A[0][1]*B[1][0], A[0][0]*B[0][1]+A[0][1]*B[1][1]]];Conditional Probability & Chain Rule
Core equation of autoregressive LLMs (like GPT), predicting the next token via P(w_t | w_1...w_{t-1}).
const pAgivenB = (pAB, pB) => pB > 0 ? pAB / pB : 0;Shannon Entropy & Cross-Entropy Loss
The universal training loss function (Cross-Entropy Loss) for LLMs, image classifiers, and decision tree Information Gain.
const bceLoss = (y, yHat) => -(y * Math.log(yHat + 1e-9) + (1 - y) * Math.log(1 - yHat + 1e-9));Softmax Function & Stochastic Gradient Descent
The beating heart of Transformer Self-Attention, LLM Temperature sampling, and all deep learning optimizers (SGD, AdamW).
const softmax = (zs, T=1) => { const exps = zs.map(z => Math.exp(z/T)); const s = exps.reduce((a,b)=>a+b,0); return exps.map(e => e/s); };Complete 50-Formula Developer Code & AI Matrix
Single perceptron linear layer (y = wx + b) in AI and UI layout linear interpolation (Lerp)
const solveLinear = (a: number, b: number) => -b / a;Polynomial Kernel (SVM) feature expansion in ML and symbolic computer algebra systems
const sqSum = (a: number, b: number) => a*a + 2*a*b + b*b;IEEE 754 floating-point exponent encoding and 3D shader gamma correction (c^(1/2.2))
const gammaCorrect = (c: number, g = 2.2) => Math.pow(c, 1 / g);3D Ray-Sphere intersection in ray tracing shaders and projectile trajectory impact time calculation
const D = b*b - 4*a*c; const x1 = (-b + Math.sqrt(D)) / (2*a);Control theory pole stability checks and matrix eigenvalue trace/determinant verification
const sumRoots = -b / a; const prodRoots = c / a;Binary search O(log₂ N) complexity and preventing numerical underflow via Log-Likelihood in AI
const logBase = (a: number, x: number) => Math.log(x) / Math.log(a);Reinforcement Learning discounted return (∑ γ^t R_t) convergence and compound interest valuation
const geoSum = (a: number, r: number, n: number) => a * (1 - Math.pow(r, n)) / (1 - r);Bernstein basis polynomials for 3D Bézier curves and ensemble classifier majority voting
const nCr = (n: number, k: number): number => k === 0 ? 1 : (n * nCr(n - 1, k - 1)) / k;FFT audio/RF signal processing, quantum computing qubit amplitudes, and fractal rendering
const mag = Math.hypot(re, im); const phase = Math.atan2(im, re);HTTPS/TLS RSA public-key encryption, blockchain digital signatures, and hash table indexing
const modPow = (a: bigint, e: bigint, m: bigint): bigint => /* binary exp */2D/3D game collision distance (Math.hypot) and foundational L2 metric in AI vector spaces
const hypotenuse = (a: number, b: number) => Math.hypot(a, b);KNN / K-Means clustering distance metric in ML and A* pathfinding heuristic in 3D games
const dist3D = (dx: number, dy: number, dz: number) => Math.hypot(dx, dy, dz);Standard angle unit for Three.js/WebGL shaders, Math.sin/cos, and robotics wheel odometry (l = rθ)
const degToRad = (deg: number) => (deg * Math.PI) / 180;Transformer LLM Sinusoidal/RoPE Positional Encoding and 3D game wave/orbit animation shaders
const posOnCircle = (r: number, rad: number) => ({ x: r * Math.cos(rad), y: r * Math.sin(rad) });Robotics & 3D character Inverse Kinematics (IK) joint angle solver and GPS triangulation
const cosC = (a*a + b*b - c*c) / (2 * a * b);Deriving 2D/3D rotation matrix composition R(α)R(β)=R(α+β) and RF QAM signal demodulation
const sinAdd = (a: number, b: number) => Math.sin(a)*Math.cos(b) + Math.cos(a)*Math.sin(b);Kepler satellite orbit simulation and Multivariate Gaussian covariance ellipses in ML
const eccentricity = (a: number, b: number) => Math.sqrt(1 - (b*b)/(a*a));3D globe 360° UV texture mapping, Three.js OrbitControls camera, and quantum spherical harmonics
const toXYZ = (r:number, th:number, ph:number) => [r*Math.sin(ph)*Math.cos(th), r*Math.cos(ph), r*Math.sin(ph)*Math.sin(th)];3D polygon manifold mesh validation in Blender/Unreal and VLSI planar graph routing
const eulerChar = (V: number, E: number, F: number) => V - E + F;SVG paths, CSS cubic-bezier easing, TrueType fonts, and autonomous vehicle / 3D camera path planning
const quadBezier = (p0:number, p1:number, p2:number, t:number) => (1-t)*(1-t)*p0 + 2*(1-t)*t*p1 + t*t*p2;Deep learning activations (Sigmoid, Softmax, GELU) and continuous compounding e^(rt) in quant finance
const approxE = (n: number) => Math.pow(1 + 1 / n, n);Physics engine velocity/acceleration finite differences and neural network loss slope
const diff = (f: (x:number)=>number, x: number, h = 1e-5) => (f(x + h) - f(x - h)) / (2 * h);Deep neural network Backpropagation and PyTorch/TensorFlow Autograd computational graphs
const dL_dw = dL_dy * dy_du * du_dw; // Backpropagation Chain RulePhysics engine velocity-to-position integration, 3D ray marching rendering equation, and probability CDF
const simpson = (f:(x:number)=>number, a:number, b:number) => ((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));Finite Element Method (FEM weak formulation) physics simulation and Laplace/Fourier derivative rules
const intXeX = (x: number) => (x - 1) * Math.exp(x) + 1; // ∫_0^x t*e^t dtCPU/GPU fast Math.sin/exp approximation and XGBoost / Newton-Raphson 2nd-order optimization
const sinTaylor = (x: number) => x - (x**3)/6 + (x**5)/120 - (x**7)/5040;LLM Rotary Position Embedding (RoPE e^(imθ)), FFT complex basis, and quantum phase gates
const eulerCis = (theta: number) => ({ re: Math.cos(theta), im: Math.sin(theta) });LLM/Deep Learning loss optimization (-∇L) and computer vision Sobel edge detection gradients
const grad2D = (a:number, b:number, x:number, y:number) => ({ gx: 2*a*x, gy: 2*b*y });Speech AI (Whisper Mel-Spectrograms), MP3/JPEG compression, 5G/Wi-Fi OFDM, and MRI reconstruction
const dftBin = (sig: number[], k: number) => sig.reduce((s, x, n) => s + x * Math.cos(2*Math.PI*k*n/sig.length), 0);Generative AI Diffusion ODE solvers, React-Spring UI physics animations, and vehicle suspension control
const zeta = c / (2 * Math.sqrt(m * k)); const w0 = Math.sqrt(k / m);Deep learning L1/L2 Weight Decay regularization, LayerNorm, and 3D game movement vector normalization
const norm2 = Math.hypot(vx, vy, vz); const unit = [vx/norm2, vy/norm2, vz/norm2];LLM Transformer Attention (Q·K^T), RAG vector DB cosine search, and 3D Lambertian lighting (N·L)
const cosSim = (a:number[], b:number[]) => dot(a,b) / (norm(a) * norm(b));3D mesh triangle surface normals, back-face culling, and physics torque (τ = r × F) calculation
const cross = (a:number[], b:number[]) => [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]];90% of NVIDIA GPU Tensor Core compute in Deep Learning (Y = XW) and 3D graphics MVP pipelines
const matMul2 = (A:number[][], B:number[][]) => [[A[0][0]*B[0][0]+A[0][1]*B[1][0], A[0][0]*B[0][1]+A[0][1]*B[1][1]]];3D mouse raycasting unprojection (MVP⁻¹) in games and Jacobian determinants in Normalizing Flow AI
const det2 = (a:number, b:number, c:number, d:number) => a * d - b * c;SPICE circuit simulation, structural mechanics solvers, and Least Squares Normal Equations
const det = a1*b2 - b1*a2; const x = (c1*b2 - b1*c2)/det; const y = (a1*c2 - c1*a2)/det;Three.js/Unreal/Unity 3D object & camera rotation and LiDAR point-cloud alignment (ICP)
const rot2D = (x:number, y:number, r:number) => ({ x: x*Math.cos(r) - y*Math.sin(r), y: x*Math.sin(r) + y*Math.cos(r) });Google PageRank algorithm, quantum mechanics Hamiltonian energy states (Hψ=Eψ), and structural resonance
const tr = a + d; const det = a*d - b*c; const l1 = (tr + Math.sqrt(tr*tr - 4*det))/2;LLM Low-Rank Adaptation (LoRA), PCA dimensionality reduction, recommender systems, and image compression
const explainedRatio = (s1:number, s2:number, s3:number) => (s1*s1) / (s1*s1 + s2*s2 + s3*s3);Three.js/Unity/Unreal skeletal animation (Quaternion SLERP) and spacecraft/drone attitude control
const quatFromAxisZ = (rad: number) => ({ w: Math.cos(rad/2), x: 0, y: 0, z: Math.sin(rad/2) });Used in algorithm search-space complexity analysis, hyperparameter grid search sizing, and feature subset selection.
const nCr = (n, r) => Array.from({length: r}, (_, i) => (n - i) / (i + 1)).reduce((a, b) => a * b, 1);Core equation of autoregressive LLMs (like GPT), predicting the next token via P(w_t | w_1...w_{t-1}).
const pAgivenB = (pAB, pB) => pB > 0 ? pAB / pB : 0;Powers spam email filtering (Naive Bayes), medical AI diagnostic calibration, autonomous driving Kalman/particle filters, and Bayesian optimization.
const bayes = (prior, sens, fpr) => (sens * prior) / (sens * prior + fpr * (1 - prior));Core to Reinforcement Learning expected reward maximization, Batch/Layer Normalization, and Xavier/He weight initialization.
const mean = xs.reduce((a,b)=>a+b,0)/xs.length; const variance = xs.reduce((s,x)=>s+(x-mean)**2,0)/xs.length;Used in ad CTR A/B testing, neural network Dropout masking probability modeling, and game gacha drop-rate simulation.
const binomPMF = (n, k, p) => nCr(n, k) * Math.pow(p, k) * Math.pow(1 - p, n - k);Powers Diffusion Models (Stable Diffusion) Gaussian noise scheduling, Variational Autoencoders (VAE), and 3D Gaussian Splatting (3DGS).
const normalPDF = (x, mu, sig) => Math.exp(-0.5 * ((x - mu) / sig) ** 2) / (sig * Math.sqrt(2 * Math.PI));Explains the 1/√n variance reduction in Mini-Batch SGD gradients and Monte Carlo Ray Tracing error bounds.
const stdError = (sigma, n) => sigma / Math.sqrt(n); const ci95 = (mean, se) => [mean - 1.96*se, mean + 1.96*se];Foundation of supervised machine learning linear predictors, quant finance portfolio Beta (β), and feature correlation analysis.
const slope = covXY / varX; const intercept = meanY - slope * meanX; const r = covXY / Math.sqrt(varX * varY);The universal training loss function (Cross-Entropy Loss) for LLMs, image classifiers, and decision tree Information Gain.
const bceLoss = (y, yHat) => -(y * Math.log(yHat + 1e-9) + (1 - y) * Math.log(1 - yHat + 1e-9));The beating heart of Transformer Self-Attention, LLM Temperature sampling, and all deep learning optimizers (SGD, AdamW).
const softmax = (zs, T=1) => { const exps = zs.map(z => Math.exp(z/T)); const s = exps.reduce((a,b)=>a+b,0); return exps.map(e => e/s); };