lib_jsl_core/constants.rs
1#![allow(clippy::excessive_precision, clippy::approx_constant)]
2//! # `constants`
3//!
4//! A collection of `f64` constants commonly used in mathematics, physics,
5//! electromagnetics, signal processing, and machine learning.
6//!
7//! ## Layout
8//!
9//! | Module | Contents |
10//! |-------------|-------------------------------------------------------------------|
11//! | [`math`] | Pi, e, golden ratio, Euler–Mascheroni, Apéry, Catalan, ... |
12//! | [`physics`] | CODATA fundamental constants, thermodynamics, astronomy |
13//! | [`em`] | Vacuum permeability/permittivity, impedances, quantum EM units |
14//! | [`dsp`] | dB conversions, window coefficients, audio, noise, quantization |
15//! | [`ml`] | Activation constants, optimizer defaults, dataset statistics |
16//! | [`units`] | Angle, length, mass, pressure, energy conversion factors |
17//!
18//! ## Sources and conventions
19//!
20//! * Physical constants follow **CODATA 2022** (SI 2019 redefinition). Values
21//! that are *exact* by definition (`C`, `H`, `K_B`, `N_A`, `E_CHARGE`) are
22//! exact; measured values carry their CODATA uncertainty in the final digits.
23//! * Where a constant is algebraically derived from others (e.g. `HBAR`,
24//! `R_GAS`, `MU_B`) it is computed at compile time from its definition so
25//! that the set stays self-consistent.
26//! * All values are SI unless the name says otherwise (`_EV`, `_DEG`, `_HZ`...).
27//! * Works in `#![no_std]` contexts: only `core` is used.
28//!
29//! ## Usage
30//!
31//! ```ignore
32//! mod constants;
33//! use constants::{em, physics, math};
34//!
35//! let wavelength = physics::C / 2.4e9;
36//! let z0 = em::Z_0;
37//! let tau = math::TAU;
38//! ```
39//!
40//! See the book entry [Shared types, constants and utilities](https://jwkunz.github.io/lib_jsl/book/foundations/core-utils.html#constants).
41
42// =============================================================================
43// Mathematics
44// =============================================================================
45pub mod math {
46 //! Mathematical constants.
47
48 // ---- Re-exports of the standard library's constants ---------------------
49 pub use core::f64::consts::{
50 E, FRAC_1_PI, FRAC_1_SQRT_2, FRAC_2_PI, FRAC_2_SQRT_PI, FRAC_PI_2, FRAC_PI_3, FRAC_PI_4,
51 FRAC_PI_6, FRAC_PI_8, LN_2, LN_10, LOG2_10, LOG2_E, LOG10_2, LOG10_E, PI, SQRT_2, TAU,
52 };
53
54 // ---- Pi-related ---------------------------------------------------------
55 /// 2π/3
56 pub const TWO_PI_OVER_3: f64 = 2.0943951023931954;
57 /// 3π/2
58 pub const THREE_PI_OVER_2: f64 = 4.7123889803846897;
59 /// π²
60 pub const PI_SQUARED: f64 = 9.8696044010893586;
61 /// √π
62 pub const SQRT_PI: f64 = 1.7724538509055159;
63 /// √(2π) = √τ (Gaussian normalization)
64 pub const SQRT_TAU: f64 = 2.5066282746310002;
65 /// 1/√(2π) (standard normal pdf peak value)
66 pub const FRAC_1_SQRT_TAU: f64 = 0.3989422804014327;
67 /// 1/√π
68 pub const FRAC_1_SQRT_PI: f64 = 0.5641895835477563;
69 /// ln(π)
70 pub const LN_PI: f64 = 1.1447298858494002;
71 /// ln(√(2π)) = ½ ln(2π) (Gaussian log-likelihood term)
72 pub const LN_SQRT_TAU: f64 = 0.9189385332046727;
73 /// ln(2π)
74 pub const LN_TAU: f64 = 1.8378770664093455;
75 /// 4π (solid angle of a sphere, steradians)
76 pub const FOUR_PI: f64 = 12.566370614359172;
77
78 // ---- Algebraic numbers --------------------------------------------------
79 /// √3
80 pub const SQRT_3: f64 = 1.7320508075688772;
81 /// √5
82 pub const SQRT_5: f64 = 2.2360679774997897;
83 /// √6
84 pub const SQRT_6: f64 = 2.4494897427831781;
85 /// ∛2
86 pub const CBRT_2: f64 = 1.2599210498948732;
87 /// ∛3
88 pub const CBRT_3: f64 = 1.4422495703074083;
89 /// Golden ratio φ = (1 + √5)/2
90 pub const PHI: f64 = 1.6180339887498949;
91 /// 1/φ = φ − 1
92 pub const FRAC_1_PHI: f64 = 0.6180339887498949;
93 /// ln(φ)
94 pub const LN_PHI: f64 = 0.48121182505960347;
95 /// Silver ratio δ_S = 1 + √2
96 pub const SILVER_RATIO: f64 = 2.4142135623730951;
97 /// Plastic number (real root of x³ = x + 1)
98 pub const PLASTIC_NUMBER: f64 = 1.3247179572447460;
99
100 // ---- Special constants --------------------------------------------------
101 /// Euler–Mascheroni constant γ
102 pub const EULER_GAMMA: f64 = 0.57721566490153287;
103 /// Apéry's constant ζ(3)
104 pub const APERY: f64 = 1.2020569031595943;
105 /// ζ(2) = π²/6
106 pub const ZETA_2: f64 = 1.6449340668482264;
107 /// ζ(4) = π⁴/90
108 pub const ZETA_4: f64 = 1.0823232337111382;
109 /// Catalan's constant G
110 pub const CATALAN: f64 = 0.91596559417721901;
111 /// Glaisher–Kinkelin constant A
112 pub const GLAISHER: f64 = 1.2824271291006226;
113 /// Khinchin's constant K₀
114 pub const KHINCHIN: f64 = 2.6854520010653064;
115 /// Omega constant Ω = W(1), solution of Ω·e^Ω = 1
116 pub const OMEGA: f64 = 0.56714329040978387;
117 /// Twin prime constant C₂
118 pub const TWIN_PRIME: f64 = 0.66016181584686957;
119 /// Feigenbaum constant δ (period-doubling ratio)
120 pub const FEIGENBAUM_DELTA: f64 = 4.6692016091029907;
121 /// Feigenbaum constant α (period-doubling scaling)
122 pub const FEIGENBAUM_ALPHA: f64 = 2.5029078750958928;
123 /// Wilbraham–Gibbs constant (2/π)·Si(π); Gibbs overshoot peak ratio
124 pub const GIBBS: f64 = 1.1789797444721675;
125
126 // ---- Statistics ---------------------------------------------------------
127 /// 2√(2 ln 2): converts Gaussian σ → FWHM
128 pub const SIGMA_TO_FWHM: f64 = 2.3548200450309493;
129 /// 1/(2√(2 ln 2)): converts Gaussian FWHM → σ
130 pub const FWHM_TO_SIGMA: f64 = 0.42466090014400953;
131 /// Standard normal two-sided z-score for 90 % confidence
132 pub const Z_90: f64 = 1.6448536269514722;
133 /// Standard normal two-sided z-score for 95 % confidence
134 pub const Z_95: f64 = 1.9599639845400543;
135 /// Standard normal two-sided z-score for 99 % confidence
136 pub const Z_99: f64 = 2.5758293035489004;
137 /// Standard normal two-sided z-score for 99.9 % confidence
138 pub const Z_999: f64 = 3.2905267314918949;
139 /// Probability within ±1σ of a normal distribution
140 pub const P_1_SIGMA: f64 = 0.6826894921370859;
141 /// Probability within ±2σ of a normal distribution
142 pub const P_2_SIGMA: f64 = 0.9544997361036416;
143 /// Probability within ±3σ of a normal distribution
144 pub const P_3_SIGMA: f64 = 0.9973002039367398;
145
146 // ---- Floating-point helpers --------------------------------------------
147 /// `f64` machine epsilon (2⁻⁵²)
148 pub const EPSILON: f64 = f64::EPSILON;
149 /// `f32` machine epsilon (2⁻²³) promoted to `f64`
150 pub const EPSILON_F32: f64 = 1.1920928955078125e-7;
151 /// Smallest positive normal `f64`
152 pub const MIN_POSITIVE: f64 = f64::MIN_POSITIVE;
153 /// Largest finite `f64`
154 pub const MAX: f64 = f64::MAX;
155}
156
157// =============================================================================
158// Physics
159// =============================================================================
160pub mod physics {
161 //! Fundamental and derived physical constants (SI units, CODATA 2022).
162
163 use super::math::{PI, TAU};
164
165 // ---- Exact SI-defining constants ---------------------------------------
166 /// Speed of light in vacuum, m/s (exact)
167 pub const C: f64 = 299_792_458.0;
168 /// Planck constant, J·s (exact)
169 pub const H: f64 = 6.626_070_15e-34;
170 /// Elementary charge, C (exact)
171 pub const E_CHARGE: f64 = 1.602_176_634e-19;
172 /// Boltzmann constant, J/K (exact)
173 pub const K_B: f64 = 1.380_649e-23;
174 /// Avogadro constant, 1/mol (exact)
175 pub const N_A: f64 = 6.022_140_76e23;
176 /// Luminous efficacy of 540 THz radiation, lm/W (exact)
177 pub const K_CD: f64 = 683.0;
178 /// Cesium hyperfine transition frequency, Hz (exact)
179 pub const DELTA_NU_CS: f64 = 9_192_631_770.0;
180
181 // ---- Derived from the above (exact in the SI) --------------------------
182 /// Reduced Planck constant ħ = h/2π, J·s
183 pub const HBAR: f64 = H / TAU;
184 /// Molar gas constant R = N_A·k_B, J/(mol·K)
185 pub const R_GAS: f64 = N_A * K_B;
186 /// Faraday constant F = N_A·e, C/mol
187 pub const FARADAY: f64 = N_A * E_CHARGE;
188 /// Stefan–Boltzmann constant σ = 2π⁵k⁴/(15h³c²), W/(m²·K⁴)
189 pub const SIGMA_SB: f64 =
190 2.0 * PI * PI * PI * PI * PI * K_B * K_B * K_B * K_B / (15.0 * H * H * H * C * C);
191 /// First radiation constant c₁ = 2πhc², W·m²
192 pub const C1_RADIATION: f64 = TAU * H * C * C;
193 /// Second radiation constant c₂ = hc/k, m·K
194 pub const C2_RADIATION: f64 = H * C / K_B;
195 /// Electron-volt, J
196 pub const EV: f64 = E_CHARGE;
197
198 // ---- Measured constants (CODATA 2022) ----------------------------------
199 /// Newtonian constant of gravitation G, m³/(kg·s²)
200 pub const G: f64 = 6.674_30e-11;
201 /// Fine-structure constant α (dimensionless)
202 pub const ALPHA: f64 = 7.297_352_564_3e-3;
203 /// Inverse fine-structure constant 1/α
204 pub const INV_ALPHA: f64 = 137.035_999_177;
205 /// Electron mass, kg
206 pub const M_E: f64 = 9.109_383_713_9e-31;
207 /// Proton mass, kg
208 pub const M_P: f64 = 1.672_621_925_95e-27;
209 /// Neutron mass, kg
210 pub const M_N: f64 = 1.674_927_500_56e-27;
211 /// Atomic mass constant (unified atomic mass unit), kg
212 pub const AMU: f64 = 1.660_539_068_92e-27;
213 /// Proton-to-electron mass ratio
214 pub const MP_OVER_ME: f64 = 1_836.152_673_426;
215 /// Wien wavelength displacement constant b, m·K
216 pub const WIEN_B: f64 = 2.897_771_955_185_173e-3;
217 /// Wien frequency displacement constant, Hz/K
218 pub const WIEN_B_FREQ: f64 = 5.878_925_757_646_825e10;
219
220 // ---- Derived atomic-scale quantities -----------------------------------
221 /// Bohr radius a₀ = ħ/(m_e·c·α), m
222 pub const BOHR_RADIUS: f64 = HBAR / (M_E * C * ALPHA);
223 /// Compton wavelength of the electron λ_C = h/(m_e·c), m
224 pub const COMPTON_WAVELENGTH: f64 = H / (M_E * C);
225 /// Reduced Compton wavelength ƛ_C = ħ/(m_e·c), m
226 pub const COMPTON_WAVELENGTH_REDUCED: f64 = HBAR / (M_E * C);
227 /// Classical electron radius r_e = α·ħ/(m_e·c), m
228 pub const CLASSICAL_ELECTRON_RADIUS: f64 = ALPHA * HBAR / (M_E * C);
229 /// Thomson scattering cross-section σ_T = (8π/3)·r_e², m²
230 pub const THOMSON_CROSS_SECTION: f64 =
231 (8.0 * PI / 3.0) * CLASSICAL_ELECTRON_RADIUS * CLASSICAL_ELECTRON_RADIUS;
232 /// Rydberg constant R∞ = α²·m_e·c/(2h), 1/m (CODATA 2022 value: the formula with the
233 /// rounded α and m_e is off by ten times R∞'s uncertainty)
234 pub const RYDBERG: f64 = 10_973_731.568_157;
235 /// Hartree energy E_h = m_e·c²·α² = 2hc·R∞, J (CODATA 2022)
236 pub const HARTREE: f64 = 4.359_744_722_206e-18;
237 /// Rydberg energy = E_h / 2, J
238 pub const RYDBERG_ENERGY: f64 = HARTREE / 2.0;
239 /// Electron rest energy m_e·c², J
240 pub const ELECTRON_REST_ENERGY: f64 = M_E * C * C;
241 /// Proton rest energy m_p·c², J
242 pub const PROTON_REST_ENERGY: f64 = M_P * C * C;
243
244 // ---- Planck units (CODATA 2022) ----------------------------------------
245 /// Planck length, m
246 pub const PLANCK_LENGTH: f64 = 1.616_255e-35;
247 /// Planck mass, kg
248 pub const PLANCK_MASS: f64 = 2.176_434e-8;
249 /// Planck time, s
250 pub const PLANCK_TIME: f64 = 5.391_247e-44;
251 /// Planck temperature, K
252 pub const PLANCK_TEMPERATURE: f64 = 1.416_784e32;
253
254 // ---- Thermodynamics & mechanics ----------------------------------------
255 /// 0 °C in kelvin
256 pub const ZERO_CELSIUS: f64 = 273.15;
257 /// Standard temperature for chemistry (STP, IUPAC), K
258 pub const T_STP: f64 = 273.15;
259 /// Room temperature (20 °C), K
260 pub const T_ROOM: f64 = 293.15;
261 /// Triple point of water, K
262 pub const T_TRIPLE_WATER: f64 = 273.16;
263 /// Standard acceleration of gravity gₙ, m/s² (exact)
264 pub const STANDARD_GRAVITY: f64 = 9.806_65;
265 /// Standard atmosphere, Pa (exact)
266 pub const STANDARD_ATMOSPHERE: f64 = 101_325.0;
267 /// Standard-state pressure (IUPAC), Pa (exact)
268 pub const STANDARD_PRESSURE: f64 = 100_000.0;
269 /// Molar volume of an ideal gas at 273.15 K and 101.325 kPa, m³/mol
270 pub const MOLAR_VOLUME_STP: f64 = R_GAS * 273.15 / 101_325.0;
271 /// Specific gas constant of dry air, J/(kg·K)
272 pub const R_SPECIFIC_AIR: f64 = 287.052_874;
273 /// Specific heat of water at 15 °C, J/(kg·K) (approx.)
274 pub const CP_WATER: f64 = 4_184.0;
275 /// Density of water at 4 °C, kg/m³ (approx.)
276 pub const RHO_WATER: f64 = 999.97;
277 /// Density of dry air at 15 °C, 101.325 kPa, kg/m³ (ISA)
278 pub const RHO_AIR_ISA: f64 = 1.225;
279 /// Speed of sound in dry air at 20 °C, m/s
280 pub const SPEED_OF_SOUND_AIR_20C: f64 = 343.21;
281 /// Speed of sound in dry air at 0 °C, m/s
282 pub const SPEED_OF_SOUND_AIR_0C: f64 = 331.3;
283 /// Adiabatic index of air (diatomic ideal gas)
284 pub const GAMMA_AIR: f64 = 1.4;
285
286 // ---- Astronomy ----------------------------------------------------------
287 /// Astronomical unit, m (exact, IAU 2012)
288 pub const AU: f64 = 149_597_870_700.0;
289 /// Julian year, s (exact)
290 pub const JULIAN_YEAR: f64 = 31_557_600.0;
291 /// Light-year, m (exact)
292 pub const LIGHT_YEAR: f64 = 9_460_730_472_580_800.0;
293 /// Parsec, m (exact: 648000/π AU)
294 pub const PARSEC: f64 = 648_000.0 / PI * AU;
295 /// Nominal solar mass parameter GM☉, m³/s² (IAU 2015 nominal)
296 pub const GM_SUN: f64 = 1.327_124_4e20;
297 /// Solar mass, kg
298 pub const M_SUN: f64 = 1.988_41e30;
299 /// Nominal solar radius, m (IAU 2015)
300 pub const R_SUN: f64 = 6.957e8;
301 /// Nominal solar luminosity, W (IAU 2015)
302 pub const L_SUN: f64 = 3.828e26;
303 /// Earth mass, kg
304 pub const M_EARTH: f64 = 5.972_2e24;
305 /// Earth equatorial radius (WGS-84), m
306 pub const R_EARTH_EQUATORIAL: f64 = 6_378_137.0;
307 /// Earth mean radius, m
308 pub const R_EARTH_MEAN: f64 = 6_371_008.8;
309 /// Moon mass, kg
310 pub const M_MOON: f64 = 7.342e22;
311 /// Mean Earth–Moon distance, m
312 pub const EARTH_MOON_DISTANCE: f64 = 3.844e8;
313 /// Sidereal day, s
314 pub const SIDEREAL_DAY: f64 = 86_164.090_5;
315 /// Solar constant (total solar irradiance at 1 AU), W/m²
316 pub const SOLAR_CONSTANT: f64 = 1_361.0;
317 /// Earth's angular rotation rate, rad/s (WGS-84)
318 pub const EARTH_ANGULAR_VELOCITY: f64 = 7.292_115e-5;
319 /// Hubble constant (≈ 70 km/s/Mpc) expressed in 1/s — a rough reference value
320 pub const HUBBLE_APPROX: f64 = 70.0e3 / (1.0e6 * PARSEC);
321}
322
323// =============================================================================
324// Electromagnetics
325// =============================================================================
326pub mod em {
327 //! Electromagnetic constants and typical material properties.
328
329 use super::math::FOUR_PI;
330 use super::physics::{ALPHA, C, E_CHARGE, H, HBAR, M_E, M_P};
331
332 // ---- Free space ---------------------------------------------------------
333 /// Speed of light in vacuum, m/s (exact)
334 pub const C0: f64 = C;
335 /// Vacuum permeability μ₀, H/m (CODATA 2022; ≈ 4π × 10⁻⁷)
336 pub const MU_0: f64 = 1.256_637_061_27e-6;
337 /// Vacuum permittivity ε₀, F/m (CODATA 2022)
338 pub const EPS_0: f64 = 8.854_187_818_8e-12;
339 /// Characteristic impedance of free space Z₀ = μ₀·c, Ω
340 pub const Z_0: f64 = MU_0 * C;
341 /// Admittance of free space Y₀ = 1/Z₀, S
342 pub const Y_0: f64 = 1.0 / Z_0;
343 /// Coulomb constant kₑ = 1/(4π ε₀), N·m²/C²
344 pub const K_E: f64 = 1.0 / (FOUR_PI * EPS_0);
345 /// Classical μ₀ = 4π × 10⁻⁷ (pre-2019 SI exact definition), H/m
346 pub const MU_0_CLASSIC: f64 = FOUR_PI * 1.0e-7;
347 /// Classical ε₀ = 1/(μ₀_classic·c²), F/m
348 pub const EPS_0_CLASSIC: f64 = 1.0 / (MU_0_CLASSIC * C * C);
349 /// Elementary charge, C
350 pub const E: f64 = E_CHARGE;
351
352 // ---- Quantum electrical standards --------------------------------------
353 /// Magnetic flux quantum Φ₀ = h/(2e), Wb
354 pub const FLUX_QUANTUM: f64 = H / (2.0 * E_CHARGE);
355 /// Josephson constant K_J = 2e/h, Hz/V
356 pub const JOSEPHSON: f64 = 2.0 * E_CHARGE / H;
357 /// von Klitzing constant R_K = h/e², Ω
358 pub const VON_KLITZING: f64 = H / (E_CHARGE * E_CHARGE);
359 /// Conductance quantum G₀ = 2e²/h, S
360 pub const CONDUCTANCE_QUANTUM: f64 = 2.0 * E_CHARGE * E_CHARGE / H;
361 /// Bohr magneton μ_B = e·ħ/(2m_e), J/T
362 pub const BOHR_MAGNETON: f64 = E_CHARGE * HBAR / (2.0 * M_E);
363 /// Nuclear magneton μ_N = e·ħ/(2m_p), J/T
364 pub const NUCLEAR_MAGNETON: f64 = E_CHARGE * HBAR / (2.0 * M_P);
365 /// Electron charge-to-mass ratio e/m_e, C/kg
366 pub const E_OVER_ME: f64 = E_CHARGE / M_E;
367 /// Fine-structure constant α = e²/(4π ε₀ ħ c)
368 pub const FINE_STRUCTURE: f64 = ALPHA;
369 /// Electron g-factor magnitude |g_e|
370 pub const G_ELECTRON: f64 = 2.002_319_304_360_92;
371
372 // ---- Standard impedances (ohms) ----------------------------------------
373 /// Common RF system impedance, Ω
374 pub const Z_50: f64 = 50.0;
375 /// Common video/CATV impedance, Ω
376 pub const Z_75: f64 = 75.0;
377 /// Twisted-pair / balanced line impedance, Ω
378 pub const Z_100: f64 = 100.0;
379 /// Standard dipole radiation resistance (half-wave, in free space), Ω
380 pub const HALF_WAVE_DIPOLE_RESISTANCE: f64 = 73.13;
381 /// Half-wave dipole directivity (linear, ≈ 2.15 dBi)
382 pub const HALF_WAVE_DIPOLE_DIRECTIVITY: f64 = 1.640;
383 /// Short-dipole radiation directivity (linear, 1.5 ≈ 1.76 dBi)
384 pub const SHORT_DIPOLE_DIRECTIVITY: f64 = 1.5;
385
386 // ---- Unit conversions ---------------------------------------------------
387 /// Gauss → tesla
388 pub const GAUSS_TO_TESLA: f64 = 1.0e-4;
389 /// Oersted → A/m (1000/4π)
390 pub const OERSTED_TO_A_PER_M: f64 = 1000.0 / FOUR_PI;
391 /// Maxwell → weber
392 pub const MAXWELL_TO_WEBER: f64 = 1.0e-8;
393 /// Gaussian-cgs statcoulomb per coulomb (≈ 10c/…): 1 C = this many statC
394 pub const COULOMB_TO_STATCOULOMB: f64 = 2.997_924_58e9;
395 /// Skin-depth prefactor for non-magnetic conductors: δ = SKIN_DEPTH_K · √(ρ/f),
396 /// with ρ in Ω·m and f in Hz (K = 1/√(π·μ₀) ≈ 503.29)
397 pub const SKIN_DEPTH_K: f64 = 503.292_121_078_095_9;
398
399 // ---- Typical material properties at ≈ 20 °C ----------------------------
400 /// Resistivity of copper (annealed), Ω·m
401 pub const RHO_COPPER: f64 = 1.68e-8;
402 /// Resistivity of silver, Ω·m
403 pub const RHO_SILVER: f64 = 1.59e-8;
404 /// Resistivity of gold, Ω·m
405 pub const RHO_GOLD: f64 = 2.44e-8;
406 /// Resistivity of aluminum, Ω·m
407 pub const RHO_ALUMINUM: f64 = 2.65e-8;
408 /// Resistivity of iron, Ω·m
409 pub const RHO_IRON: f64 = 9.71e-8;
410 /// Conductivity of the International Annealed Copper Standard, S/m (exact)
411 pub const SIGMA_IACS: f64 = 5.8e7;
412 /// Copper temperature coefficient of resistance, 1/K
413 pub const ALPHA_COPPER: f64 = 3.93e-3;
414 /// Relative permittivity of PTFE (Teflon)
415 pub const EPS_R_PTFE: f64 = 2.1;
416 /// Relative permittivity of polyethylene
417 pub const EPS_R_POLYETHYLENE: f64 = 2.25;
418 /// Relative permittivity of silicon dioxide
419 pub const EPS_R_SIO2: f64 = 3.9;
420 /// Typical relative permittivity of FR-4 at ~1 GHz
421 pub const EPS_R_FR4: f64 = 4.4;
422 /// Relative permittivity of crystalline silicon
423 pub const EPS_R_SILICON: f64 = 11.7;
424 /// Relative permittivity of water at 20 °C (static)
425 pub const EPS_R_WATER: f64 = 80.1;
426 /// Relative permittivity of air (≈ 1.000 59 at STP)
427 pub const EPS_R_AIR: f64 = 1.000_59;
428}
429
430// =============================================================================
431// Signal processing
432// =============================================================================
433pub mod dsp {
434 //! Constants for DSP, audio, and RF noise/link-budget calculations.
435
436 use super::math::{LN_10, LOG10_2, LOG10_E};
437 use super::physics::K_B;
438
439 // ---- Decibels / nepers --------------------------------------------------
440 /// dB per neper (amplitude): 20·log₁₀(e)
441 pub const DB_PER_NEPER: f64 = 20.0 * LOG10_E;
442 /// nepers per dB (amplitude): ln(10)/20
443 pub const NEPER_PER_DB: f64 = LN_10 / 20.0;
444 /// Half-power point in dB: 10·log₁₀(½)
445 pub const HALF_POWER_DB: f64 = -3.010_299_956_639_812;
446 /// Half-power amplitude (−3 dB point of a magnitude response): 1/√2
447 pub const HALF_POWER_AMPLITUDE: f64 = core::f64::consts::FRAC_1_SQRT_2;
448 /// dB change per doubling of power: 10·log₁₀(2)
449 pub const DB_PER_POWER_DOUBLING: f64 = 10.0 * LOG10_2;
450 /// dB change per doubling of amplitude: 20·log₁₀(2)
451 pub const DB_PER_AMPLITUDE_DOUBLING: f64 = 20.0 * LOG10_2;
452 /// Butterworth Q of a 2nd-order section (maximally flat): 1/√2
453 pub const Q_BUTTERWORTH: f64 = core::f64::consts::FRAC_1_SQRT_2;
454 /// Critical-damping Q: 1/2
455 pub const Q_CRITICAL: f64 = 0.5;
456 /// Linkwitz–Riley 2nd-order Q (= 0.5)
457 pub const Q_LINKWITZ_RILEY: f64 = 0.5;
458 /// 2nd-order Bessel Q: 1/√3
459 pub const Q_BESSEL: f64 = 0.577_350_269_189_625_8;
460
461 // ---- Quantization -------------------------------------------------------
462 /// Ideal quantizer SNR slope: 20·log₁₀(2) dB per bit
463 pub const SQNR_DB_PER_BIT: f64 = 6.020_599_913_279_624;
464 /// Ideal sine-wave SQNR offset: 10·log₁₀(1.5) dB (SQNR = 6.02·N + 1.76)
465 pub const SQNR_OFFSET_DB: f64 = 1.760_912_590_556_812_4;
466 /// Q7 fixed-point scale (2⁷)
467 pub const Q7_SCALE: f64 = 128.0;
468 /// Q15 fixed-point scale (2¹⁵)
469 pub const Q15_SCALE: f64 = 32_768.0;
470 /// Q31 fixed-point scale (2³¹)
471 pub const Q31_SCALE: f64 = 2_147_483_648.0;
472
473 // ---- Standard sample rates (Hz) ----------------------------------------
474 pub const FS_8K: f64 = 8_000.0;
475 pub const FS_11K025: f64 = 11_025.0;
476 pub const FS_16K: f64 = 16_000.0;
477 pub const FS_22K05: f64 = 22_050.0;
478 pub const FS_32K: f64 = 32_000.0;
479 /// CD audio sample rate
480 pub const FS_44K1: f64 = 44_100.0;
481 /// Professional audio / DVD sample rate
482 pub const FS_48K: f64 = 48_000.0;
483 pub const FS_88K2: f64 = 88_200.0;
484 pub const FS_96K: f64 = 96_000.0;
485 pub const FS_176K4: f64 = 176_400.0;
486 pub const FS_192K: f64 = 192_000.0;
487
488 // ---- Audio / music ------------------------------------------------------
489 /// Concert pitch A4, Hz
490 pub const A4_HZ: f64 = 440.0;
491 /// MIDI note number for A4
492 pub const MIDI_A4: f64 = 69.0;
493 /// Equal-temperament semitone frequency ratio: 2^(1/12)
494 pub const SEMITONE_RATIO: f64 = 1.059_463_094_359_295_3;
495 /// Equal-temperament cent frequency ratio: 2^(1/1200)
496 pub const CENT_RATIO: f64 = 1.000_577_789_506_554_8;
497 /// Lower limit of nominal human hearing, Hz
498 pub const HEARING_MIN_HZ: f64 = 20.0;
499 /// Upper limit of nominal human hearing, Hz
500 pub const HEARING_MAX_HZ: f64 = 20_000.0;
501 /// Reference sound pressure in air (0 dB SPL), Pa
502 pub const P_REF_AIR: f64 = 20.0e-6;
503 /// Reference sound intensity (0 dB SPL), W/m²
504 pub const I_REF_AIR: f64 = 1.0e-12;
505 /// Reference sound power, W
506 pub const W_REF_AIR: f64 = 1.0e-12;
507 /// Reference sound pressure in water, Pa
508 pub const P_REF_WATER: f64 = 1.0e-6;
509
510 // ---- RF noise -----------------------------------------------------------
511 /// IEEE standard reference noise temperature T₀, K
512 pub const T0_NOISE: f64 = 290.0;
513 /// Thermal noise power density at T₀: k·T₀, W/Hz (≈ −174 dBm/Hz)
514 pub const KT0: f64 = K_B * T0_NOISE;
515 /// Thermal noise floor at T₀ in dBm/Hz: 10·log₁₀(k·T₀ / 1 mW) (≈ −173.98)
516 pub const KT0_DBM_PER_HZ: f64 = -173.975_187_194_228_1;
517 /// Thermal noise floor at 300 K in dBm/Hz: 10·log₁₀(k·300 K / 1 mW) (≈ −173.83)
518 pub const KT300_DBM_PER_HZ: f64 = -173.827_954_626_021;
519 /// Watts in 0 dBm
520 pub const DBM_REF_WATTS: f64 = 1.0e-3;
521
522 // ---- Window functions: cosine-sum coefficients -------------------------
523 // w[n] = a0 − a1·cos(2πn/N) + a2·cos(4πn/N) − a3·cos(6πn/N) + a4·cos(8πn/N)
524
525 /// Hann window a₀
526 pub const HANN_A0: f64 = 0.5;
527 /// Hann window a₁
528 pub const HANN_A1: f64 = 0.5;
529
530 /// Hamming window a₀ (classic)
531 pub const HAMMING_A0: f64 = 0.54;
532 /// Hamming window a₁ (classic)
533 pub const HAMMING_A1: f64 = 0.46;
534 /// Hamming window a₀ (exact, zeroes the first side-lobe): 25/46
535 pub const HAMMING_A0_EXACT: f64 = 25.0 / 46.0;
536 /// Hamming window a₁ (exact): 21/46
537 pub const HAMMING_A1_EXACT: f64 = 21.0 / 46.0;
538
539 /// Blackman window a₀ (classic)
540 pub const BLACKMAN_A0: f64 = 0.42;
541 /// Blackman window a₁ (classic)
542 pub const BLACKMAN_A1: f64 = 0.5;
543 /// Blackman window a₂ (classic)
544 pub const BLACKMAN_A2: f64 = 0.08;
545 /// Blackman window a₀ (exact): 7938/18608
546 pub const BLACKMAN_A0_EXACT: f64 = 7_938.0 / 18_608.0;
547 /// Blackman window a₁ (exact): 9240/18608
548 pub const BLACKMAN_A1_EXACT: f64 = 9_240.0 / 18_608.0;
549 /// Blackman window a₂ (exact): 1430/18608
550 pub const BLACKMAN_A2_EXACT: f64 = 1_430.0 / 18_608.0;
551
552 /// 4-term Blackman–Harris window coefficients (−92 dB sidelobes)
553 pub const BLACKMAN_HARRIS_A: [f64; 4] = [0.35875, 0.48829, 0.14128, 0.01168];
554 /// 4-term Nuttall window coefficients (continuous first derivative)
555 pub const NUTTALL_A: [f64; 4] = [0.3635819, 0.4891775, 0.1365995, 0.0106411];
556 /// 5-term flat-top window coefficients (SciPy / MATLAB convention)
557 pub const FLATTOP_A: [f64; 5] = [
558 0.21557895,
559 0.41663158,
560 0.277263158,
561 0.083578947,
562 0.006947368,
563 ];
564
565 // ---- Window figures of merit -------------------------------------------
566 /// Coherent gain (mean of window) — rectangular
567 pub const CG_RECT: f64 = 1.0;
568 /// Coherent gain — Hann
569 pub const CG_HANN: f64 = 0.5;
570 /// Coherent gain — Hamming
571 pub const CG_HAMMING: f64 = 0.54;
572 /// Coherent gain — Blackman
573 pub const CG_BLACKMAN: f64 = 0.42;
574 /// Noise-equivalent bandwidth in bins — rectangular
575 pub const ENBW_RECT: f64 = 1.0;
576 /// Noise-equivalent bandwidth in bins — Hann
577 pub const ENBW_HANN: f64 = 1.5;
578 /// Noise-equivalent bandwidth in bins — Hamming
579 pub const ENBW_HAMMING: f64 = 1.3628;
580 /// Noise-equivalent bandwidth in bins — Blackman
581 pub const ENBW_BLACKMAN: f64 = 1.7268;
582 /// Noise-equivalent bandwidth in bins — 4-term Blackman–Harris
583 pub const ENBW_BLACKMAN_HARRIS: f64 = 2.0044;
584 /// Kaiser window β giving ≈ 60 dB stop-band attenuation (≈ 5.65)
585 pub const KAISER_BETA_60DB: f64 = 5.653;
586 /// Kaiser window β giving ≈ 80 dB stop-band attenuation (≈ 7.86)
587 pub const KAISER_BETA_80DB: f64 = 7.857;
588 /// Kaiser window β commonly used for audio resampling
589 pub const KAISER_BETA_AUDIO: f64 = 8.6;
590}
591
592// =============================================================================
593// Machine learning
594// =============================================================================
595pub mod ml {
596 //! Constants and default hyper-parameters common in machine learning.
597 //!
598 //! Optimizer defaults follow the original papers / PyTorch conventions.
599
600 // ---- Activation functions ----------------------------------------------
601 /// √(2/π): GELU tanh-approximation prefactor
602 pub const SQRT_2_OVER_PI: f64 = 0.797_884_560_802_865_4;
603 /// GELU tanh-approximation cubic coefficient: 0.044715
604 pub const GELU_TANH_COEFF: f64 = 0.044_715;
605 /// 1/√2 for exact GELU via erf: x·½·(1 + erf(x/√2))
606 pub const FRAC_1_SQRT_2: f64 = core::f64::consts::FRAC_1_SQRT_2;
607 /// SELU α (Klambauer et al. 2017)
608 pub const SELU_ALPHA: f64 = 1.673_263_242_354_377_3;
609 /// SELU scale λ (Klambauer et al. 2017)
610 pub const SELU_SCALE: f64 = 1.050_700_987_355_480_5;
611 /// ELU default α
612 pub const ELU_ALPHA: f64 = 1.0;
613 /// Leaky-ReLU default negative slope (PyTorch)
614 pub const LEAKY_RELU_SLOPE: f64 = 0.01;
615 /// Leaky-ReLU slope commonly used in GANs
616 pub const LEAKY_RELU_SLOPE_GAN: f64 = 0.2;
617 /// Maximum of the sigmoid derivative σ'(0) = 1/4
618 pub const SIGMOID_DERIV_MAX: f64 = 0.25;
619 /// Softplus linearisation threshold (PyTorch default)
620 pub const SOFTPLUS_THRESHOLD: f64 = 20.0;
621
622 // ---- Weight-init gains --------------------------------------------------
623 /// He/Kaiming gain for ReLU: √2
624 pub const GAIN_RELU: f64 = core::f64::consts::SQRT_2;
625 /// Recommended gain for tanh: 5/3
626 pub const GAIN_TANH: f64 = 5.0 / 3.0;
627 /// Gain for linear / sigmoid layers
628 pub const GAIN_LINEAR: f64 = 1.0;
629 /// Gain for SELU (LeCun normal init): 3/4
630 pub const GAIN_SELU: f64 = 0.75;
631 /// Truncated-normal init standard deviation used by BERT / GPT-2
632 pub const INIT_STD_TRANSFORMER: f64 = 0.02;
633
634 // ---- Optimizer defaults -------------------------------------------------
635 /// Adam β₁
636 pub const ADAM_BETA1: f64 = 0.9;
637 /// Adam β₂
638 pub const ADAM_BETA2: f64 = 0.999;
639 /// Adam ε (original paper)
640 pub const ADAM_EPS: f64 = 1.0e-8;
641 /// Adam default learning rate
642 pub const ADAM_LR: f64 = 1.0e-3;
643 /// "Karpathy constant": commonly good Adam learning rate
644 pub const ADAM_LR_KARPATHY: f64 = 3.0e-4;
645 /// AdamW default weight decay (PyTorch)
646 pub const ADAMW_WEIGHT_DECAY: f64 = 1.0e-2;
647 /// Adam β₂ for large-LM training (GPT-3 / LLaMA style)
648 pub const ADAM_BETA2_LLM: f64 = 0.95;
649 /// Adam β₂ and ε used in the original Transformer paper
650 pub const ADAM_BETA2_TRANSFORMER: f64 = 0.98;
651 pub const ADAM_EPS_TRANSFORMER: f64 = 1.0e-9;
652 /// Original Transformer LR warm-up steps
653 pub const TRANSFORMER_WARMUP_STEPS: f64 = 4_000.0;
654 /// SGD momentum (classic)
655 pub const SGD_MOMENTUM: f64 = 0.9;
656 /// RMSprop smoothing constant α (PyTorch)
657 pub const RMSPROP_ALPHA: f64 = 0.99;
658 /// RMSprop ρ (Hinton / Keras)
659 pub const RMSPROP_RHO: f64 = 0.9;
660 /// RMSprop ε
661 pub const RMSPROP_EPS: f64 = 1.0e-8;
662 /// Adagrad ε
663 pub const ADAGRAD_EPS: f64 = 1.0e-10;
664 /// Default gradient-clipping norm
665 pub const GRAD_CLIP_NORM: f64 = 1.0;
666
667 // ---- Normalization / regularization ------------------------------------
668 /// BatchNorm ε (PyTorch / Keras ≈ 1e-5 / 1e-3)
669 pub const BATCHNORM_EPS: f64 = 1.0e-5;
670 /// BatchNorm running-stat momentum (PyTorch convention)
671 pub const BATCHNORM_MOMENTUM: f64 = 0.1;
672 /// LayerNorm ε (PyTorch)
673 pub const LAYERNORM_EPS: f64 = 1.0e-5;
674 /// LayerNorm ε (original Transformer / BERT)
675 pub const LAYERNORM_EPS_TRANSFORMER: f64 = 1.0e-12;
676 /// RMSNorm ε (LLaMA)
677 pub const RMSNORM_EPS: f64 = 1.0e-6;
678 /// Typical dropout rate for transformers
679 pub const DROPOUT_TRANSFORMER: f64 = 0.1;
680 /// Typical dropout rate for fully-connected layers (Srivastava et al.)
681 pub const DROPOUT_FC: f64 = 0.5;
682 /// Typical label-smoothing factor
683 pub const LABEL_SMOOTHING: f64 = 0.1;
684
685 // ---- Numerical safety ---------------------------------------------------
686 /// Keras backend epsilon for clipping probabilities
687 pub const PROB_CLIP_EPS: f64 = 1.0e-7;
688 /// ε for cosine similarity / L2 normalization (PyTorch)
689 pub const NORMALIZE_EPS: f64 = 1.0e-12;
690 /// log(ε) floor used to avoid −∞ in log-probabilities
691 pub const LOG_FLOOR: f64 = -36.841_361_487_904_73; // ln(1e-16)
692 /// Large negative number used for attention masking
693 pub const MASK_NEG_INF: f64 = -1.0e9;
694
695 // ---- Transformers / embeddings -----------------------------------------
696 /// Sinusoidal positional-encoding base (Vaswani et al.)
697 pub const POS_ENC_BASE: f64 = 10_000.0;
698 /// RoPE base θ (original RoFormer / LLaMA 1-2)
699 pub const ROPE_BASE: f64 = 10_000.0;
700 /// RoPE base θ (LLaMA 3)
701 pub const ROPE_BASE_LLAMA3: f64 = 500_000.0;
702
703 // ---- Information theory -------------------------------------------------
704 /// Convert nats → bits: log₂(e)
705 pub const NATS_TO_BITS: f64 = core::f64::consts::LOG2_E;
706 /// Convert bits → nats: ln(2)
707 pub const BITS_TO_NATS: f64 = core::f64::consts::LN_2;
708 /// Convert nats → hartleys (bans): log₁₀(e)
709 pub const NATS_TO_HARTLEYS: f64 = core::f64::consts::LOG10_E;
710 /// Differential entropy of N(0,1) in nats: ½·ln(2πe)
711 pub const STD_NORMAL_ENTROPY: f64 = 1.418_938_533_204_672_7;
712
713 // ---- Dataset statistics (per channel, inputs scaled to [0, 1]) ---------
714 /// ImageNet RGB mean
715 pub const IMAGENET_MEAN: [f64; 3] = [0.485, 0.456, 0.406];
716 /// ImageNet RGB standard deviation
717 pub const IMAGENET_STD: [f64; 3] = [0.229, 0.224, 0.225];
718 /// CIFAR-10 RGB mean
719 pub const CIFAR10_MEAN: [f64; 3] = [0.4914, 0.4822, 0.4465];
720 /// CIFAR-10 RGB standard deviation
721 pub const CIFAR10_STD: [f64; 3] = [0.2470, 0.2435, 0.2616];
722 /// CIFAR-100 RGB mean
723 pub const CIFAR100_MEAN: [f64; 3] = [0.5071, 0.4865, 0.4409];
724 /// CIFAR-100 RGB standard deviation
725 pub const CIFAR100_STD: [f64; 3] = [0.2673, 0.2564, 0.2762];
726 /// MNIST mean
727 pub const MNIST_MEAN: f64 = 0.1307;
728 /// MNIST standard deviation
729 pub const MNIST_STD: f64 = 0.3081;
730
731 // ---- Color → grayscale luma weights ------------------------------------
732 /// ITU-R BT.601 luma weights (R, G, B)
733 pub const LUMA_BT601: [f64; 3] = [0.299, 0.587, 0.114];
734 /// ITU-R BT.709 luma weights (R, G, B)
735 pub const LUMA_BT709: [f64; 3] = [0.2126, 0.7152, 0.0722];
736 /// ITU-R BT.2020 luma weights (R, G, B)
737 pub const LUMA_BT2020: [f64; 3] = [0.2627, 0.6780, 0.0593];
738}
739
740// =============================================================================
741// Unit conversions
742// =============================================================================
743pub mod units {
744 //! Conversion factors to SI. Multiply a value in the named unit by the
745 //! constant to obtain the SI value (e.g. `5.0 * INCH` → metres).
746
747 use super::math::PI;
748
749 // ---- Angle --------------------------------------------------------------
750 /// Degrees → radians
751 pub const DEG_TO_RAD: f64 = PI / 180.0;
752 /// Radians → degrees
753 pub const RAD_TO_DEG: f64 = 180.0 / PI;
754 /// Arcminutes → radians
755 pub const ARCMIN_TO_RAD: f64 = PI / 10_800.0;
756 /// Arcseconds → radians
757 pub const ARCSEC_TO_RAD: f64 = PI / 648_000.0;
758 /// Gradians → radians
759 pub const GRAD_TO_RAD: f64 = PI / 200.0;
760 /// Turns (revolutions) → radians
761 pub const TURN_TO_RAD: f64 = 2.0 * PI;
762 /// Hz → rad/s
763 pub const HZ_TO_RAD_S: f64 = 2.0 * PI;
764 /// RPM → rad/s
765 pub const RPM_TO_RAD_S: f64 = 2.0 * PI / 60.0;
766 /// Steradians in a full sphere
767 pub const SPHERE_SR: f64 = 4.0 * PI;
768
769 // ---- Length (m) ---------------------------------------------------------
770 pub const INCH: f64 = 0.0254;
771 pub const FOOT: f64 = 0.3048;
772 pub const YARD: f64 = 0.9144;
773 pub const MILE: f64 = 1_609.344;
774 pub const NAUTICAL_MILE: f64 = 1_852.0;
775 pub const MIL: f64 = 2.54e-5;
776 pub const ANGSTROM: f64 = 1.0e-10;
777
778 // ---- Mass (kg) ----------------------------------------------------------
779 pub const POUND: f64 = 0.453_592_37;
780 pub const OUNCE: f64 = 0.028_349_523_125;
781 pub const TONNE: f64 = 1_000.0;
782
783 // ---- Time (s) -----------------------------------------------------------
784 pub const MINUTE: f64 = 60.0;
785 pub const HOUR: f64 = 3_600.0;
786 pub const DAY: f64 = 86_400.0;
787 pub const WEEK: f64 = 604_800.0;
788
789 // ---- Pressure (Pa) ------------------------------------------------------
790 pub const ATM: f64 = 101_325.0;
791 pub const BAR: f64 = 100_000.0;
792 pub const MMHG: f64 = 133.322_387_415;
793 pub const TORR: f64 = 101_325.0 / 760.0;
794 pub const PSI: f64 = 6_894.757_293_168_361;
795
796 // ---- Energy / power -----------------------------------------------------
797 /// Thermochemical calorie, J
798 pub const CALORIE: f64 = 4.184;
799 /// International Table BTU, J
800 pub const BTU: f64 = 1_055.055_852_62;
801 /// Kilowatt-hour, J
802 pub const KWH: f64 = 3.6e6;
803 /// Electron-volt, J
804 pub const ELECTRON_VOLT: f64 = 1.602_176_634e-19;
805 /// Mechanical horsepower, W
806 pub const HORSEPOWER: f64 = 745.699_871_582_270_2;
807
808 // ---- Volume (m³) --------------------------------------------------------
809 pub const LITER: f64 = 1.0e-3;
810 pub const GALLON_US: f64 = 3.785_411_784e-3;
811
812 // ---- Force --------------------------------------------------------------
813 /// Pound-force, N
814 pub const LBF: f64 = 4.448_221_615_260_5;
815 /// Kilogram-force, N
816 pub const KGF: f64 = 9.806_65;
817
818 // ---- Temperature offsets -----------------------------------------------
819 /// Celsius → kelvin offset
820 pub const CELSIUS_OFFSET: f64 = 273.15;
821 /// Fahrenheit → Rankine offset
822 pub const FAHRENHEIT_RANKINE_OFFSET: f64 = 459.67;
823}